| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 1 | This file describes some special Python build types enabled via | 
 | 2 | compile-time preprocessor defines. | 
 | 3 |  | 
 | 4 | --------------------------------------------------------------------------- | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 5 | Py_REF_DEBUG                                              introduced in 1.4 | 
 | 6 |                                                  named REF_DEBUG before 1.4 | 
| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 7 |  | 
 | 8 | Turn on aggregate reference counting.  This arranges that extern | 
 | 9 | _Py_RefTotal hold a count of all references, the sum of ob_refcnt across | 
 | 10 | all objects.  In a debug-mode build, this is where the "8288" comes from | 
 | 11 | in | 
 | 12 |  | 
 | 13 |     >>> 23 | 
 | 14 |     23 | 
 | 15 |     [8288 refs] | 
 | 16 |     >>> | 
 | 17 |  | 
 | 18 | Note that if this count increases when you're not storing away new objects, | 
 | 19 | there's probably a leak.  Remember, though, that in interactive mode the | 
 | 20 | special name "_" holds a reference to the last result displayed! | 
 | 21 |  | 
 | 22 | Py_REF_DEBUG also checks after every decref to verify that the refcount | 
 | 23 | hasn't gone negative, and causes an immediate fatal error if it has. | 
 | 24 |  | 
 | 25 | Special gimmicks: | 
 | 26 |  | 
 | 27 | sys.gettotalrefcount() | 
 | 28 |     Return current total of all refcounts. | 
 | 29 |     Available under Py_REF_DEBUG in Python 2.3. | 
 | 30 |     Before 2.3, Py_TRACE_REFS was required to enable this function. | 
 | 31 | --------------------------------------------------------------------------- | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 32 | Py_TRACE_REFS                                             introduced in 1.4 | 
 | 33 |                                                 named TRACE_REFS before 1.4 | 
| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 34 |  | 
 | 35 | Turn on heavy reference debugging.  This is major surgery.  Every PyObject | 
 | 36 | grows two more pointers, to maintain a doubly-linked list of all live | 
 | 37 | heap-allocated objects (note that, e.g., most builtin type objects are not | 
 | 38 | in this list, as they're statically allocated).  Note that because the | 
 | 39 | fundamental PyObject layout changes, Python modules compiled with | 
 | 40 | Py_TRACE_REFS are incompatible with modules compiled without it. | 
 | 41 |  | 
 | 42 | Py_TRACE_REFS implies Py_REF_DEBUG. | 
 | 43 |  | 
 | 44 | Special gimmicks: | 
 | 45 |  | 
 | 46 | sys.getobjects(max[, type]) | 
| Tim Peters | a788f5e | 2002-07-10 18:47:03 +0000 | [diff] [blame] | 47 |     Return list of the (no more than) max most-recently allocated objects, | 
 | 48 |     most recently allocated first in the list, least-recently allocated | 
 | 49 |     last in the list.  max=0 means no limit on list length. | 
 | 50 |     If an optional type object is passed, the list is also restricted to | 
 | 51 |     objects of that type. | 
 | 52 |     The return list itself, and some temp objects created just to call | 
 | 53 |     sys.getobjects(), are excluded from the return list.  Note that the | 
 | 54 |     list returned is just another object, though, so may appear in the | 
 | 55 |     return list the next time you call getobjects(); note that every | 
 | 56 |     object in the list is kept alive too, simply by virtue of being in | 
 | 57 |     the list. | 
| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 58 |  | 
 | 59 | envar PYTHONDUMPREFS | 
 | 60 |     If this envar exists, Py_Finalize() arranges to print a list of | 
 | 61 |     all still-live heap objects. | 
 | 62 | --------------------------------------------------------------------------- | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 63 | PYMALLOC_DEBUG                                            introduced in 2.3 | 
| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 64 |  | 
| Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 65 | When pymalloc is enabled (WITH_PYMALLOC is defined), calls to the PyObject_ | 
 | 66 | memory routines are handled by Python's own small-object allocator, while | 
 | 67 | calls to the PyMem_ memory routines are directed to the system malloc/ | 
 | 68 | realloc/free.  If PYMALLOC_DEBUG is also defined, calls to both PyObject_ | 
 | 69 | and PyMem_ memory routines are directed to a special debugging mode of | 
 | 70 | Python's small-object allocator. | 
 | 71 |  | 
 | 72 | This mode fills dynamically allocated memory blocks with special, | 
 | 73 | recognizable bit patterns, and adds debugging info on each end of | 
 | 74 | dynamically allocated memory blocks.  The special bit patterns are: | 
 | 75 |  | 
 | 76 | #define CLEANBYTE     0xCB   /* clean (newly allocated) memory */ | 
 | 77 | #define DEADBYTE      0xDB   /* dead (newly freed) memory */ | 
 | 78 | #define FORBIDDENBYTE 0xFB   /* fordidden -- untouchable bytes */ | 
 | 79 |  | 
 | 80 | Strings of these bytes are unlikely to be valid addresses, floats, or 7-bit | 
 | 81 | ASCII strings. | 
 | 82 |  | 
 | 83 | 8 bytes are added at each end of each block of N bytes requested.  The | 
 | 84 | memory layout is like so, where p represents the address returned by a | 
| Tim Peters | 20c8a04 | 2002-07-11 00:02:52 +0000 | [diff] [blame] | 85 | malloc-like or realloc-like function (p[i:j] means the slice of bytes | 
 | 86 | from *(p+i) inclusive up to *(p+j) exclusive; note that the treatment | 
 | 87 | of negative indices differs from a Python slice): | 
| Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 88 |  | 
 | 89 | p[-8:-4] | 
 | 90 |     Number of bytes originally asked for.  4-byte unsigned integer, | 
 | 91 |     big-endian (easier to read in a memory dump). | 
 | 92 | p[-4:0] | 
 | 93 |     Copies of FORBIDDENBYTE.  Used to catch under- writes and reads. | 
 | 94 | p[0:N] | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 95 |     The requested memory, filled with copies of CLEANBYTE, used to catch | 
 | 96 |     reference to uninitialized memory. | 
| Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 97 |     When a realloc-like function is called requesting a larger memory | 
 | 98 |     block, the new excess bytes are also filled with CLEANBYTE. | 
 | 99 |     When a free-like function is called, these are overwritten with | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 100 |     DEADBYTE, to catch reference to freed memory.  When a realloc- | 
| Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 101 |     like function is called requesting a smaller memory block, the excess | 
 | 102 |     old bytes are also filled with DEADBYTE. | 
 | 103 | p[N:N+4] | 
 | 104 |     Copies of FORBIDDENBYTE.  Used to catch over- writes and reads. | 
 | 105 | p[N+4:N+8] | 
 | 106 |     A serial number, incremented by 1 on each call to a malloc-like or | 
 | 107 |     realloc-like function. | 
 | 108 |     4-byte unsigned integer, big-endian. | 
 | 109 |     If "bad memory" is detected later, the serial number gives an | 
 | 110 |     excellent way to set a breakpoint on the next run, to capture the | 
| Tim Peters | 20c8a04 | 2002-07-11 00:02:52 +0000 | [diff] [blame] | 111 |     instant at which this block was passed out.  The static function | 
 | 112 |     bumpserialno() in obmalloc.c is the only place the serial number | 
 | 113 |     is incremented, and exists so you can set such a breakpoint easily. | 
| Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 114 |  | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 115 | A realloc-like or free-like function first checks that the FORBIDDENBYTEs | 
| Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 116 | at each end are intact.  If they've been altered, diagnostic output is | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 117 | written to stderr, and the program is aborted via Py_FatalError().  The | 
 | 118 | other main failure mode is provoking a memory error when a program | 
 | 119 | reads up one of the special bit patterns and tries to use it as an address. | 
 | 120 | If you get in a debugger then and look at the object, you're likely | 
 | 121 | to see that it's entirely filled with 0xDB (meaning freed memory is | 
 | 122 | getting used) or 0xCB (meaning uninitialized memory is getting used). | 
| Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 123 |  | 
 | 124 | Note that PYMALLOC_DEBUG requires WITH_PYMALLOC. | 
 | 125 |  | 
| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 126 | Special gimmicks: | 
 | 127 |  | 
 | 128 | envar PYTHONMALLOCSTATS | 
 | 129 |     If this envar exists, a report of pymalloc summary statistics is | 
 | 130 |     printed to stderr whenever a new arena is allocated, and also | 
 | 131 |     by Py_Finalize(). | 
 | 132 | --------------------------------------------------------------------------- | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 133 | Py_DEBUG                                                  introduced in 1.5 | 
 | 134 |                                                      named DEBUG before 1.5 | 
| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 135 |  | 
 | 136 | This is what is generally meant by "a debug build" of Python. | 
 | 137 |  | 
 | 138 | Py_DEBUG implies Py_REF_DEBUG, Py_TRACE_REFS, and PYMALLOC_DEBUG (if | 
| Tim Peters | 20c8a04 | 2002-07-11 00:02:52 +0000 | [diff] [blame] | 139 | WITH_PYMALLOC is enabled).  In addition, C assert()s are enabled (via | 
 | 140 | the C way:  by not defining NDEBUG), and some routines do additional | 
 | 141 | sanity checks inside "#ifdef Py_DEBUG" blocks. | 
| Tim Peters | 6045d48 | 2002-07-09 18:35:34 +0000 | [diff] [blame] | 142 | --------------------------------------------------------------------------- | 
| Tim Peters | 62fc52e | 2002-07-11 00:23:58 +0000 | [diff] [blame] | 143 | COUNT_ALLOCS                                            introduced in 0.9.9 | 
| Tim Peters | 8acdf7a | 2002-07-11 00:38:05 +0000 | [diff] [blame] | 144 |                                              partly broken in 2.2 and 2.2.1 | 
| Tim Peters | 48ba649 | 2002-07-09 19:24:54 +0000 | [diff] [blame] | 145 |  | 
 | 146 | Each type object grows three new members: | 
 | 147 |  | 
 | 148 |     /* Number of times an object of this type was allocated. */ | 
| Guido van Rossum | 0c08864 | 2002-07-11 01:04:32 +0000 | [diff] [blame] | 149 |     int tp_allocs; | 
| Tim Peters | 48ba649 | 2002-07-09 19:24:54 +0000 | [diff] [blame] | 150 |  | 
 | 151 |     /* Number of times an object of this type was deallocated. */ | 
| Guido van Rossum | 0c08864 | 2002-07-11 01:04:32 +0000 | [diff] [blame] | 152 |     int tp_frees; | 
| Tim Peters | 48ba649 | 2002-07-09 19:24:54 +0000 | [diff] [blame] | 153 |  | 
| Guido van Rossum | 0c08864 | 2002-07-11 01:04:32 +0000 | [diff] [blame] | 154 |     /* Highwater mark:  the maximum value of tp_allocs - tp_frees so | 
 | 155 |      * far; or, IOW, the largest number of objects of this type alive at | 
 | 156 |      * the same time. | 
 | 157 |      */ | 
 | 158 |     int tp_maxalloc; | 
| Tim Peters | 48ba649 | 2002-07-09 19:24:54 +0000 | [diff] [blame] | 159 |  | 
 | 160 | Allocation and deallocation code keeps these counts up to date. | 
 | 161 | Py_Finalize() displays a summary of the info returned by sys.getcounts() | 
 | 162 | (see below), along with assorted other special allocation counts (like | 
 | 163 | the number of tuple allocations satisfied by a tuple free-list, the number | 
 | 164 | of 1-character strings allocated, etc). | 
 | 165 |  | 
 | 166 | Before Python 2.2, type objects were immortal, and the COUNT_ALLOCS | 
 | 167 | implementation relies on that.  As of Python 2.2, heap-allocated type/ | 
 | 168 | class objects can go away.  COUNT_ALLOCS can blow up in 2.2 and 2.2.1 | 
 | 169 | because of this; this was fixed in 2.2.2.  Use of COUNT_ALLOCS makes | 
 | 170 | all heap-allocated type objects immortal, except for those for which no | 
 | 171 | object of that type is ever allocated. | 
 | 172 |  | 
 | 173 | Special gimmicks: | 
 | 174 |  | 
 | 175 | sys.getcounts() | 
 | 176 |     Return a list of 4-tuples, one entry for each type object for which | 
 | 177 |     at least one object of that type was allocated.  Each tuple is of | 
 | 178 |     the form: | 
 | 179 |  | 
 | 180 |         (tp_name, tp_allocs, tp_frees, tp_maxalloc) | 
 | 181 |  | 
| Tim Peters | 44c1a7b | 2002-07-09 19:27:20 +0000 | [diff] [blame] | 182 |     Each distinct type object gets a distinct entry in this list, even | 
| Tim Peters | 48ba649 | 2002-07-09 19:24:54 +0000 | [diff] [blame] | 183 |     if two or more type objects have the same tp_name (in which case | 
 | 184 |     there's no way to distinguish them by looking at this list).  The | 
 | 185 |     list is ordered by time of first object allocation:  the type object | 
 | 186 |     for which the first allocation of an object of that type occurred | 
 | 187 |     most recently is at the front of the list. | 
 | 188 | --------------------------------------------------------------------------- |