📄 puremodule.c
字号:
/* This module exports the C API to such Pure Software Inc. (tm) (now
* called Pure Atria Corporation) products as Purify (tm) and Quantify
* (tm). Other packages could be added, but I didn't have those products
* and thus lack the API documentation.
*
* Currently supported: Quantify 2.x, Purify 3.x
*
* You need to decide which products you want to incorporate into the
* module when you compile this file. The way to do this is to edit
* <Python>/Modules/Setup to pass the appropriate flags to the compiler.
* -DWITH_PURIFY compiles in the Purify support, and -DWITH_QUANTIFY
* compiles in the Quantify support. -DWITH_ALL_PURE compiles in both.
* You can also build a Purify'd or Quantify'd interpreter by passing in
* the LINKCC variable to make. E.g. if you want to build a Purify'd
* interpreter and are using gcc, build Python with this command:
*
* make LINKCC='purify gcc'
*
* It would be nice (and probably easy) to provide this file as a shared
* library, however since it doesn't appear that Pure gives us shared
* libraries of the stubs, it doesn't really matter. For now, you have to
* link this file in statically.
*
* Major bogosity. The purify.h header file exports purify_exit(), but
* guess what? It is not defined in the libpurify_stubs.a file! I tried
* to fake one here, hoping the Pure linker would Do The Right Thing when
* instrumented for Purify, but it doesn't seem to, so I don't export
* purify_exit() to the Python layer. In Python you should raise a
* SystemExit exception anyway.
*
* The actual purify.h and quantify.h files which embody the APIs are
* copyrighted by Pure Software, Inc. and are only attainable through them.
* This module assumes you have legally installed licenses of their
* software. Contact them on the Web via <http://www.pureatria.com/>
*
* Author: Barry Warsaw <bwarsaw@python.org>
* <bwarsaw@cnri.reston.va.us>
*/
#include "Python.h"
#if defined(WITH_PURIFY) || defined(WITH_ALL_PURE)
# include <purify.h>
# define HAS_PURIFY_EXIT 0 /* See note at top of file */
# define PURE_PURIFY_VERSION 3 /* not provided by purify.h */
#endif
#if defined(WITH_QUANTIFY) || defined(WITH_ALL_PURE)
# include <quantify.h>
# define PURE_QUANTIFY_VERSION 2 /* not provided by quantify.h */
#endif
#if defined(PURIFY_H) || defined(QUANTIFY_H)
# define COMMON_PURE_FUNCTIONS
#endif /* PURIFY_H || QUANTIFY_H */
typedef int (*VoidArgFunc)(void);
typedef int (*StringArgFunc)(char*);
typedef int (*PrintfishFunc)(const char*, ...);
typedef int (*StringIntArgFunc)(const char*, int);
static PyObject*
call_voidarg_function(VoidArgFunc func, PyObject *self, PyObject *args)
{
int status;
if (!PyArg_ParseTuple(args, ""))
return NULL;
status = func();
return Py_BuildValue("i", status);
}
static PyObject*
call_stringarg_function(StringArgFunc func, PyObject *self, PyObject *args)
{
int status;
char* stringarg;
if (!PyArg_ParseTuple(args, "s", &stringarg))
return NULL;
status = func(stringarg);
return Py_BuildValue("i", status);
}
static PyObject*
call_stringorint_function(StringArgFunc func, PyObject *self, PyObject *args)
{
int status;
int intarg;
char* stringarg;
/* according to the quantify.h file, the argument to
* quantify_*_recording_system_call can be an integer or a string,
* but the functions are prototyped as taking a single char*
* argument. Yikes!
*/
if (PyArg_ParseTuple(args, "i", &intarg))
/* func is prototyped as int(*)(char*)
* better shut up the compiler
*/
status = func((char*)intarg);
else {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "s", &stringarg))
return NULL;
else
status = func(stringarg);
}
return Py_BuildValue("i", status);
}
static PyObject*
call_printfish_function(PrintfishFunc func, PyObject *self, PyObject *args)
{
/* we support the printf() style vararg functions by requiring the
* formatting be done in Python. At the C level we pass just a string
* to the printf() style function.
*/
int status;
char* argstring;
if (!PyArg_ParseTuple(args, "s", &argstring))
return NULL;
status = func("%s", argstring);
return Py_BuildValue("i", status);
}
static PyObject*
call_intasaddr_function(StringArgFunc func, PyObject *self, PyObject *args)
{
long memrep;
int id;
if (!PyArg_ParseTuple(args, "l", &memrep))
return NULL;
id = func((char*)memrep);
return Py_BuildValue("i", id);
}
static PyObject*
call_stringandint_function(StringIntArgFunc func, PyObject *self,
PyObject *args)
{
long srcrep;
int size;
int status;
if (!PyArg_ParseTuple(args, "li", &srcrep, &size))
return NULL;
status = func((char*)srcrep, size);
return Py_BuildValue("i", status);
}
/* functions common to all products
*
* N.B. These printf() style functions are a bit of a kludge. Since the
* API doesn't provide vprintf versions of them, we can't call them
* directly. They don't support all the standard printf % modifiers
* anyway. The way to use these is to use Python's % string operator to do
* the formatting. By the time these functions get the thing to print,
* it's already a string, and they just use "%s" as the format string.
*/
#ifdef COMMON_PURE_FUNCTIONS
static PyObject*
pure_pure_logfile_printf(PyObject* self, PyObject* args)
{
return call_printfish_function(pure_logfile_printf, self, args);
}
static PyObject*
pure_pure_printf(PyObject* self, PyObject* args)
{
return call_printfish_function(pure_printf, self, args);
}
static PyObject*
pure_pure_printf_with_banner(PyObject* self, PyObject* args)
{
return call_printfish_function(pure_printf_with_banner, self, args);
}
#endif /* COMMON_PURE_FUNCTIONS */
/* Purify functions
*
* N.B. There are some interfaces described in the purify.h file that are
* not described in the manual.
*
* Unsigned longs purify_report_{address,number,type,result} are not
* accessible from the Python layer since they seem mostly useful when
* purify_stop_here() is called by the (C) debugger. The same is true of
* the purify_stop_here_internal() function so it isn't exported either.
* And purify_stop_here() should never be called directly.
*
* The header file says purify_{new,all,clear_new}_reports() are obsolete
* so they aren't exported.
*
* None of the custom dynamic loader functions are exported.
*
* purify_unsafe_memcpy() isn't exported.
*
* purify_{start,size}_of_block() aren't exported.
*
* The manual that I have says that the prototype for the second argument
* to purify_map_pool is:
*
* void (*fn)(char*)
*
* but the purify.h file declares it as:
*
* void (*fn)(char*, int, void*)
*
* and does not explain what the other arguments are for. I support the
* latter but I don't know if I do it right or usefully.
*
* The header file says that purify_describe() returns a char* which is the
* pointer passed to it. The manual says it returns an int, but I believe
* that is a typo.
*/
#ifdef PURIFY_H
static PyObject*
pure_purify_all_inuse(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_all_inuse, self, args);
}
static PyObject*
pure_purify_all_leaks(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_all_leaks, self, args);
}
static PyObject*
pure_purify_new_inuse(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_new_inuse, self, args);
}
static PyObject*
pure_purify_new_leaks(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_new_leaks, self, args);
}
static PyObject*
pure_purify_clear_inuse(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_clear_inuse, self, args);
}
static PyObject*
pure_purify_clear_leaks(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_clear_leaks, self, args);
}
static PyObject*
pure_purify_all_fds_inuse(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_all_fds_inuse, self, args);
}
static PyObject*
pure_purify_new_fds_inuse(PyObject *self, PyObject *args)
{
return call_voidarg_function(purify_new_fds_inuse, self, args);
}
static PyObject*
pure_purify_printf_with_call_chain(PyObject *self, PyObject *args)
{
return call_printfish_function(purify_printf_with_call_chain,
self, args);
}
static PyObject*
pure_purify_set_pool_id(PyObject *self, PyObject *args)
{
long memrep;
int id;
if (!PyArg_ParseTuple(args, "li:purify_set_pool_id", &memrep, &id))
return NULL;
purify_set_pool_id((char*)memrep, id);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
pure_purify_get_pool_id(PyObject *self, PyObject *args)
{
return call_intasaddr_function(purify_get_pool_id, self, args);
}
static PyObject*
pure_purify_set_user_data(PyObject *self, PyObject *args)
{
long memrep;
long datarep;
if (!PyArg_ParseTuple(args, "ll:purify_set_user_data", &memrep, &datarep))
return NULL;
purify_set_user_data((char*)memrep, (void*)datarep);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
pure_purify_get_user_data(PyObject *self, PyObject *args)
{
/* can't use call_intasaddr_function() since purify_get_user_data()
* returns a void*
*/
long memrep;
void* data;
if (!PyArg_ParseTuple(args, "l:purify_get_user_data", &memrep))
return NULL;
data = purify_get_user_data((char*)memrep);
return Py_BuildValue("l", (long)data);
}
/* this global variable is shared by both mapping functions:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -