blob: f869e7c05e67404ccb51b624a18722246165748e [file] [log] [blame]
Georg Brandlc1660762010-12-28 11:38:12 +00001This file describes some special Python build types enabled via compile-time
2preprocessor defines.
Tim Peters6045d482002-07-09 18:35:34 +00003
Georg Brandlc1660762010-12-28 11:38:12 +00004IMPORTANT: if you want to build a debug-enabled Python, it is recommended that
5you use ``./configure --with-pydebug``, rather than the options listed here.
Antoine Pitrou3cbb24d2010-12-15 15:47:34 +00006
7However, if you wish to define some of these options individually, it is best
8to define them in the EXTRA_CFLAGS make variable;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009``make EXTRA_CFLAGS="-DPy_REF_DEBUG"``.
Brett Cannona2675632005-04-19 20:28:09 +000010
Tim Peters6045d482002-07-09 18:35:34 +000011
Georg Brandlc1660762010-12-28 11:38:12 +000012Py_REF_DEBUG
13------------
14
15Turn on aggregate reference counting. This arranges that extern _Py_RefTotal
16hold a count of all references, the sum of ob_refcnt across all objects. In a
17debug-mode build, this is where the "8288" comes from in
Tim Peters6045d482002-07-09 18:35:34 +000018
19 >>> 23
20 23
21 [8288 refs]
22 >>>
23
24Note that if this count increases when you're not storing away new objects,
Georg Brandlc1660762010-12-28 11:38:12 +000025there's probably a leak. Remember, though, that in interactive mode the special
26name "_" holds a reference to the last result displayed!
Tim Peters6045d482002-07-09 18:35:34 +000027
Georg Brandlc1660762010-12-28 11:38:12 +000028Py_REF_DEBUG also checks after every decref to verify that the refcount hasn't
29gone negative, and causes an immediate fatal error if it has.
Tim Peters6045d482002-07-09 18:35:34 +000030
31Special gimmicks:
32
33sys.gettotalrefcount()
34 Return current total of all refcounts.
Tim Peters6045d482002-07-09 18:35:34 +000035
Georg Brandlc1660762010-12-28 11:38:12 +000036
37Py_TRACE_REFS
38-------------
39
40Turn on heavy reference debugging. This is major surgery. Every PyObject grows
41two more pointers, to maintain a doubly-linked list of all live heap-allocated
42objects. Most built-in type objects are not in this list, as they're statically
43allocated. Starting in Python 2.3, if COUNT_ALLOCS (see below) is also defined,
44a static type object T does appear in this list if at least one object of type T
45has been created.
Tim Peters78be7992003-03-23 02:51:01 +000046
47Note that because the fundamental PyObject layout changes, Python modules
Georg Brandlc1660762010-12-28 11:38:12 +000048compiled with Py_TRACE_REFS are incompatible with modules compiled without it.
Tim Peters6045d482002-07-09 18:35:34 +000049
50Py_TRACE_REFS implies Py_REF_DEBUG.
51
52Special gimmicks:
53
54sys.getobjects(max[, type])
Georg Brandlc1660762010-12-28 11:38:12 +000055 Return list of the (no more than) max most-recently allocated objects, most
56 recently allocated first in the list, least-recently allocated last in the
57 list. max=0 means no limit on list length. If an optional type object is
58 passed, the list is also restricted to objects of that type. The return
59 list itself, and some temp objects created just to call sys.getobjects(),
60 are excluded from the return list. Note that the list returned is just
61 another object, though, so may appear in the return list the next time you
62 call getobjects(); note that every object in the list is kept alive too,
63 simply by virtue of being in the list.
Tim Peters6045d482002-07-09 18:35:34 +000064
Georg Brandlc1660762010-12-28 11:38:12 +000065envvar PYTHONDUMPREFS
66 If this envvar exists, Py_Finalize() arranges to print a list of all
67 still-live heap objects. This is printed twice, in different formats,
68 before and after Py_Finalize has cleaned up everything it can clean up. The
69 first output block produces the repr() of each object so is more
70 informative; however, a lot of stuff destined to die is still alive then.
71 The second output block is much harder to work with (repr() can't be invoked
72 anymore -- the interpreter has been torn down too far), but doesn't list any
73 objects that will die. The tool script combinerefs.py can be run over this
74 to combine the info from both output blocks. The second output block, and
Tim Peters21d7d4d2003-04-18 00:45:59 +000075 combinerefs.py, were new in Python 2.3b1.
Georg Brandlc1660762010-12-28 11:38:12 +000076
77
78PYMALLOC_DEBUG
79--------------
Tim Peters6045d482002-07-09 18:35:34 +000080
Tim Peters889f61d2002-07-10 19:29:49 +000081When pymalloc is enabled (WITH_PYMALLOC is defined), calls to the PyObject_
Georg Brandlc1660762010-12-28 11:38:12 +000082memory routines are handled by Python's own small-object allocator, while calls
83to the PyMem_ memory routines are directed to the system malloc/ realloc/free.
84If PYMALLOC_DEBUG is also defined, calls to both PyObject_ and PyMem_ memory
85routines are directed to a special debugging mode of Python's small-object
86allocator.
Tim Peters889f61d2002-07-10 19:29:49 +000087
Georg Brandlc1660762010-12-28 11:38:12 +000088This mode fills dynamically allocated memory blocks with special, recognizable
89bit patterns, and adds debugging info on each end of dynamically allocated
90memory blocks. The special bit patterns are:
Tim Peters889f61d2002-07-10 19:29:49 +000091
92#define CLEANBYTE 0xCB /* clean (newly allocated) memory */
93#define DEADBYTE 0xDB /* dead (newly freed) memory */
Thomas Wouters89f507f2006-12-13 04:49:30 +000094#define FORBIDDENBYTE 0xFB /* forbidden -- untouchable bytes */
Tim Peters889f61d2002-07-10 19:29:49 +000095
96Strings of these bytes are unlikely to be valid addresses, floats, or 7-bit
97ASCII strings.
98
Georg Brandlc1660762010-12-28 11:38:12 +000099Let S = sizeof(size_t). 2*S bytes are added at each end of each block of N bytes
100requested. The memory layout is like so, where p represents the address
101returned by a malloc-like or realloc-like function (p[i:j] means the slice of
102bytes from *(p+i) inclusive up to *(p+j) exclusive; note that the treatment of
103negative indices differs from a Python slice):
Tim Peters889f61d2002-07-10 19:29:49 +0000104
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000105p[-2*S:-S]
Georg Brandlc1660762010-12-28 11:38:12 +0000106 Number of bytes originally asked for. This is a size_t, big-endian (easier
107 to read in a memory dump).
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000108p[-S:0]
Tim Peters889f61d2002-07-10 19:29:49 +0000109 Copies of FORBIDDENBYTE. Used to catch under- writes and reads.
110p[0:N]
Tim Peters62fc52e2002-07-11 00:23:58 +0000111 The requested memory, filled with copies of CLEANBYTE, used to catch
Georg Brandlc1660762010-12-28 11:38:12 +0000112 reference to uninitialized memory. When a realloc-like function is called
113 requesting a larger memory block, the new excess bytes are also filled with
114 CLEANBYTE. When a free-like function is called, these are overwritten with
115 DEADBYTE, to catch reference to freed memory. When a realloc- like function
116 is called requesting a smaller memory block, the excess old bytes are also
117 filled with DEADBYTE.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000118p[N:N+S]
Tim Peters889f61d2002-07-10 19:29:49 +0000119 Copies of FORBIDDENBYTE. Used to catch over- writes and reads.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000120p[N+S:N+2*S]
Tim Peters889f61d2002-07-10 19:29:49 +0000121 A serial number, incremented by 1 on each call to a malloc-like or
Georg Brandlc1660762010-12-28 11:38:12 +0000122 realloc-like function. Big-endian size_t. If "bad memory" is detected
123 later, the serial number gives an excellent way to set a breakpoint on the
124 next run, to capture the instant at which this block was passed out. The
125 static function bumpserialno() in obmalloc.c is the only place the serial
126 number is incremented, and exists so you can set such a breakpoint easily.
Tim Peters889f61d2002-07-10 19:29:49 +0000127
Georg Brandlc1660762010-12-28 11:38:12 +0000128A realloc-like or free-like function first checks that the FORBIDDENBYTEs at
129each end are intact. If they've been altered, diagnostic output is written to
130stderr, and the program is aborted via Py_FatalError(). The other main failure
131mode is provoking a memory error when a program reads up one of the special bit
132patterns and tries to use it as an address. If you get in a debugger then and
133look at the object, you're likely to see that it's entirely filled with 0xDB
134(meaning freed memory is getting used) or 0xCB (meaning uninitialized memory is
135getting used).
Tim Peters889f61d2002-07-10 19:29:49 +0000136
137Note that PYMALLOC_DEBUG requires WITH_PYMALLOC.
138
Tim Peters6045d482002-07-09 18:35:34 +0000139Special gimmicks:
140
Georg Brandlc1660762010-12-28 11:38:12 +0000141envvar PYTHONMALLOCSTATS
142 If this envvar exists, a report of pymalloc summary statistics is printed to
143 stderr whenever a new arena is allocated, and also by Py_Finalize().
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000144
145Changed in 2.5: The number of extra bytes allocated is 4*sizeof(size_t).
146Before it was 16 on all boxes, reflecting that Python couldn't make use of
147allocations >= 2**32 bytes even on 64-bit boxes before 2.5.
Georg Brandlc1660762010-12-28 11:38:12 +0000148
149
150Py_DEBUG
151--------
Tim Peters6045d482002-07-09 18:35:34 +0000152
153This is what is generally meant by "a debug build" of Python.
154
Georg Brandlc1660762010-12-28 11:38:12 +0000155Py_DEBUG implies LLTRACE, Py_REF_DEBUG, Py_TRACE_REFS, and PYMALLOC_DEBUG (if
156WITH_PYMALLOC is enabled). In addition, C assert()s are enabled (via the C way:
157by not defining NDEBUG), and some routines do additional sanity checks inside
158"#ifdef Py_DEBUG" blocks.
159
160
161COUNT_ALLOCS
162------------
Tim Peters48ba6492002-07-09 19:24:54 +0000163
164Each type object grows three new members:
165
166 /* Number of times an object of this type was allocated. */
Guido van Rossum0c088642002-07-11 01:04:32 +0000167 int tp_allocs;
Tim Peters48ba6492002-07-09 19:24:54 +0000168
169 /* Number of times an object of this type was deallocated. */
Guido van Rossum0c088642002-07-11 01:04:32 +0000170 int tp_frees;
Tim Peters48ba6492002-07-09 19:24:54 +0000171
Guido van Rossum0c088642002-07-11 01:04:32 +0000172 /* Highwater mark: the maximum value of tp_allocs - tp_frees so
173 * far; or, IOW, the largest number of objects of this type alive at
174 * the same time.
175 */
176 int tp_maxalloc;
Tim Peters48ba6492002-07-09 19:24:54 +0000177
Georg Brandlc1660762010-12-28 11:38:12 +0000178Allocation and deallocation code keeps these counts up to date. Py_Finalize()
179displays a summary of the info returned by sys.getcounts() (see below), along
180with assorted other special allocation counts (like the number of tuple
181allocations satisfied by a tuple free-list, the number of 1-character strings
182allocated, etc).
Tim Peters48ba6492002-07-09 19:24:54 +0000183
184Before Python 2.2, type objects were immortal, and the COUNT_ALLOCS
Georg Brandlc1660762010-12-28 11:38:12 +0000185implementation relies on that. As of Python 2.2, heap-allocated type/ class
186objects can go away. COUNT_ALLOCS can blow up in 2.2 and 2.2.1 because of this;
187this was fixed in 2.2.2. Use of COUNT_ALLOCS makes all heap-allocated type
188objects immortal, except for those for which no object of that type is ever
189allocated.
Tim Peters48ba6492002-07-09 19:24:54 +0000190
Tim Peters78be7992003-03-23 02:51:01 +0000191Starting with Python 2.3, If Py_TRACE_REFS is also defined, COUNT_ALLOCS
Georg Brandlc1660762010-12-28 11:38:12 +0000192arranges to ensure that the type object for each allocated object appears in the
193doubly-linked list of all objects maintained by Py_TRACE_REFS.
Tim Peters78be7992003-03-23 02:51:01 +0000194
Tim Peters48ba6492002-07-09 19:24:54 +0000195Special gimmicks:
196
197sys.getcounts()
Georg Brandlc1660762010-12-28 11:38:12 +0000198 Return a list of 4-tuples, one entry for each type object for which at least
199 one object of that type was allocated. Each tuple is of the form:
Tim Peters48ba6492002-07-09 19:24:54 +0000200
201 (tp_name, tp_allocs, tp_frees, tp_maxalloc)
202
Georg Brandlc1660762010-12-28 11:38:12 +0000203 Each distinct type object gets a distinct entry in this list, even if two or
204 more type objects have the same tp_name (in which case there's no way to
205 distinguish them by looking at this list). The list is ordered by time of
206 first object allocation: the type object for which the first allocation of
207 an object of that type occurred most recently is at the front of the list.
208
209
210LLTRACE
211-------
Michael W. Hudsona6255232002-07-30 09:49:29 +0000212
Michael W. Hudson46e6d922005-01-18 15:53:59 +0000213Compile in support for Low Level TRACE-ing of the main interpreter loop.
Michael W. Hudsona6255232002-07-30 09:49:29 +0000214
Georg Brandlc1660762010-12-28 11:38:12 +0000215When this preprocessor symbol is defined, before PyEval_EvalFrame (eval_frame in
2162.3 and 2.2, eval_code2 before that) executes a frame's code it checks the
217frame's global namespace for a variable "__lltrace__". If such a variable is
218found, mounds of information about what the interpreter is doing are sprayed to
219stdout, such as every opcode and opcode argument and values pushed onto and
220popped off the value stack.
Michael W. Hudsona6255232002-07-30 09:49:29 +0000221
222Not useful very often, but very useful when needed.
Jeremy Hylton985eba52003-02-05 23:13:00 +0000223
Georg Brandlc1660762010-12-28 11:38:12 +0000224
225CALL_PROFILE
226------------
Jeremy Hylton985eba52003-02-05 23:13:00 +0000227
228Count the number of function calls executed.
229
Georg Brandlc1660762010-12-28 11:38:12 +0000230When this symbol is defined, the ceval mainloop and helper functions count the
231number of function calls made. It keeps detailed statistics about what kind of
232object was called and whether the call hit any of the special fast paths in the
233code.
Michael W. Hudson800ba232004-08-12 18:19:17 +0000234
Michael W. Hudson800ba232004-08-12 18:19:17 +0000235
Georg Brandlc1660762010-12-28 11:38:12 +0000236WITH_TSC
237--------
238
239Super-lowlevel profiling of the interpreter. When enabled, the sys module grows
240a new function:
Michael W. Hudson800ba232004-08-12 18:19:17 +0000241
242settscdump(bool)
Georg Brandlc1660762010-12-28 11:38:12 +0000243 If true, tell the Python interpreter to dump VM measurements to stderr. If
244 false, turn off dump. The measurements are based on the processor's
245 time-stamp counter.
Michael W. Hudson800ba232004-08-12 18:19:17 +0000246
Georg Brandlc1660762010-12-28 11:38:12 +0000247This build option requires a small amount of platform specific code. Currently
248this code is present for linux/x86 and any PowerPC platform that uses GCC
249(i.e. OS X and linux/ppc).
Michael W. Hudson800ba232004-08-12 18:19:17 +0000250
Georg Brandlc1660762010-12-28 11:38:12 +0000251On the PowerPC the rate at which the time base register is incremented is not
252defined by the architecture specification, so you'll need to find the manual for
253your specific processor. For the 750CX, 750CXe and 750FX (all sold as the G3)
254we find:
Michael W. Hudson800ba232004-08-12 18:19:17 +0000255
Georg Brandlc1660762010-12-28 11:38:12 +0000256 The time base counter is clocked at a frequency that is one-fourth that of
257 the bus clock.
Michael W. Hudson800ba232004-08-12 18:19:17 +0000258
259This build is enabled by the --with-tsc flag to configure.