blob: d1a032165f8520ac23196a92f6b1690f82fdfb11 [file] [log] [blame]
Georg Brandlc1660762010-12-28 11:38:12 +00001This file describes some special Python build types enabled via compile-time
Paul Ganssle5c403b22019-04-27 14:14:35 -04002preprocessor directives.
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
Benjamin Petersonff57aef2014-03-30 17:16:09 -040016hold a count of all references, the sum of ob_refcnt across all objects.
17Passing ``-X showrefcount`` on the command line causes the interactive
18interpreter to print the reference count total as well the number of memory
19blocks allocated after each statement:
Tim Peters6045d482002-07-09 18:35:34 +000020
21 >>> 23
22 23
Benjamin Petersonff57aef2014-03-30 17:16:09 -040023 [8288 refs, 14332 blocks]
Tim Peters6045d482002-07-09 18:35:34 +000024 >>>
25
26Note that if this count increases when you're not storing away new objects,
Georg Brandlc1660762010-12-28 11:38:12 +000027there's probably a leak. Remember, though, that in interactive mode the special
28name "_" holds a reference to the last result displayed!
Tim Peters6045d482002-07-09 18:35:34 +000029
Georg Brandlc1660762010-12-28 11:38:12 +000030Py_REF_DEBUG also checks after every decref to verify that the refcount hasn't
31gone negative, and causes an immediate fatal error if it has.
Tim Peters6045d482002-07-09 18:35:34 +000032
Victor Stinner235e7b22019-04-25 01:46:31 +020033Py_DEBUG implies Py_REF_DEBUG.
34
Tim Peters6045d482002-07-09 18:35:34 +000035Special gimmicks:
36
37sys.gettotalrefcount()
38 Return current total of all refcounts.
Tim Peters6045d482002-07-09 18:35:34 +000039
Georg Brandlc1660762010-12-28 11:38:12 +000040
41Py_TRACE_REFS
42-------------
43
Victor Stinner235e7b22019-04-25 01:46:31 +020044Build option: ``./configure --with-trace-refs``.
45
Georg Brandlc1660762010-12-28 11:38:12 +000046Turn on heavy reference debugging. This is major surgery. Every PyObject grows
47two more pointers, to maintain a doubly-linked list of all live heap-allocated
48objects. Most built-in type objects are not in this list, as they're statically
49allocated. Starting in Python 2.3, if COUNT_ALLOCS (see below) is also defined,
50a static type object T does appear in this list if at least one object of type T
51has been created.
Tim Peters78be7992003-03-23 02:51:01 +000052
53Note that because the fundamental PyObject layout changes, Python modules
Georg Brandlc1660762010-12-28 11:38:12 +000054compiled with Py_TRACE_REFS are incompatible with modules compiled without it.
Tim Peters6045d482002-07-09 18:35:34 +000055
Tim Peters6045d482002-07-09 18:35:34 +000056Special gimmicks:
57
58sys.getobjects(max[, type])
Georg Brandlc1660762010-12-28 11:38:12 +000059 Return list of the (no more than) max most-recently allocated objects, most
60 recently allocated first in the list, least-recently allocated last in the
61 list. max=0 means no limit on list length. If an optional type object is
62 passed, the list is also restricted to objects of that type. The return
63 list itself, and some temp objects created just to call sys.getobjects(),
64 are excluded from the return list. Note that the list returned is just
65 another object, though, so may appear in the return list the next time you
66 call getobjects(); note that every object in the list is kept alive too,
67 simply by virtue of being in the list.
Tim Peters6045d482002-07-09 18:35:34 +000068
Georg Brandlc1660762010-12-28 11:38:12 +000069envvar PYTHONDUMPREFS
Martin Panterb4ce1fc2015-11-30 03:18:29 +000070 If this envvar exists, Py_FinalizeEx() arranges to print a list of all
Georg Brandlc1660762010-12-28 11:38:12 +000071 still-live heap objects. This is printed twice, in different formats,
Martin Panterb4ce1fc2015-11-30 03:18:29 +000072 before and after Py_FinalizeEx has cleaned up everything it can clean up. The
Georg Brandlc1660762010-12-28 11:38:12 +000073 first output block produces the repr() of each object so is more
74 informative; however, a lot of stuff destined to die is still alive then.
75 The second output block is much harder to work with (repr() can't be invoked
76 anymore -- the interpreter has been torn down too far), but doesn't list any
77 objects that will die. The tool script combinerefs.py can be run over this
78 to combine the info from both output blocks. The second output block, and
Tim Peters21d7d4d2003-04-18 00:45:59 +000079 combinerefs.py, were new in Python 2.3b1.
Georg Brandlc1660762010-12-28 11:38:12 +000080
81
82PYMALLOC_DEBUG
83--------------
Tim Peters6045d482002-07-09 18:35:34 +000084
Tim Peters889f61d2002-07-10 19:29:49 +000085When pymalloc is enabled (WITH_PYMALLOC is defined), calls to the PyObject_
Georg Brandlc1660762010-12-28 11:38:12 +000086memory routines are handled by Python's own small-object allocator, while calls
87to the PyMem_ memory routines are directed to the system malloc/ realloc/free.
88If PYMALLOC_DEBUG is also defined, calls to both PyObject_ and PyMem_ memory
89routines are directed to a special debugging mode of Python's small-object
90allocator.
Tim Peters889f61d2002-07-10 19:29:49 +000091
Georg Brandlc1660762010-12-28 11:38:12 +000092This mode fills dynamically allocated memory blocks with special, recognizable
93bit patterns, and adds debugging info on each end of dynamically allocated
94memory blocks. The special bit patterns are:
Tim Peters889f61d2002-07-10 19:29:49 +000095
96#define CLEANBYTE 0xCB /* clean (newly allocated) memory */
97#define DEADBYTE 0xDB /* dead (newly freed) memory */
Thomas Wouters89f507f2006-12-13 04:49:30 +000098#define FORBIDDENBYTE 0xFB /* forbidden -- untouchable bytes */
Tim Peters889f61d2002-07-10 19:29:49 +000099
100Strings of these bytes are unlikely to be valid addresses, floats, or 7-bit
101ASCII strings.
102
Georg Brandlc1660762010-12-28 11:38:12 +0000103Let S = sizeof(size_t). 2*S bytes are added at each end of each block of N bytes
104requested. The memory layout is like so, where p represents the address
105returned by a malloc-like or realloc-like function (p[i:j] means the slice of
106bytes from *(p+i) inclusive up to *(p+j) exclusive; note that the treatment of
107negative indices differs from a Python slice):
Tim Peters889f61d2002-07-10 19:29:49 +0000108
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000109p[-2*S:-S]
Georg Brandlc1660762010-12-28 11:38:12 +0000110 Number of bytes originally asked for. This is a size_t, big-endian (easier
111 to read in a memory dump).
Tim Petersdf099f52013-09-19 21:06:37 -0500112p[-S]
113 API ID. See PEP 445. This is a character, but seems undocumented.
114p[-S+1:0]
Tim Peters889f61d2002-07-10 19:29:49 +0000115 Copies of FORBIDDENBYTE. Used to catch under- writes and reads.
116p[0:N]
Tim Peters62fc52e2002-07-11 00:23:58 +0000117 The requested memory, filled with copies of CLEANBYTE, used to catch
Georg Brandlc1660762010-12-28 11:38:12 +0000118 reference to uninitialized memory. When a realloc-like function is called
119 requesting a larger memory block, the new excess bytes are also filled with
120 CLEANBYTE. When a free-like function is called, these are overwritten with
121 DEADBYTE, to catch reference to freed memory. When a realloc- like function
122 is called requesting a smaller memory block, the excess old bytes are also
123 filled with DEADBYTE.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000124p[N:N+S]
Tim Peters889f61d2002-07-10 19:29:49 +0000125 Copies of FORBIDDENBYTE. Used to catch over- writes and reads.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000126p[N+S:N+2*S]
Tim Peters889f61d2002-07-10 19:29:49 +0000127 A serial number, incremented by 1 on each call to a malloc-like or
Georg Brandlc1660762010-12-28 11:38:12 +0000128 realloc-like function. Big-endian size_t. If "bad memory" is detected
129 later, the serial number gives an excellent way to set a breakpoint on the
130 next run, to capture the instant at which this block was passed out. The
131 static function bumpserialno() in obmalloc.c is the only place the serial
132 number is incremented, and exists so you can set such a breakpoint easily.
Tim Peters889f61d2002-07-10 19:29:49 +0000133
Georg Brandlc1660762010-12-28 11:38:12 +0000134A realloc-like or free-like function first checks that the FORBIDDENBYTEs at
135each end are intact. If they've been altered, diagnostic output is written to
136stderr, and the program is aborted via Py_FatalError(). The other main failure
137mode is provoking a memory error when a program reads up one of the special bit
138patterns and tries to use it as an address. If you get in a debugger then and
139look at the object, you're likely to see that it's entirely filled with 0xDB
140(meaning freed memory is getting used) or 0xCB (meaning uninitialized memory is
141getting used).
Tim Peters889f61d2002-07-10 19:29:49 +0000142
Victor Stinner235e7b22019-04-25 01:46:31 +0200143Note that PYMALLOC_DEBUG requires WITH_PYMALLOC. Py_DEBUG implies
144PYMALLOC_DEBUG (if WITH_PYMALLOC is enabled).
Tim Peters889f61d2002-07-10 19:29:49 +0000145
Tim Peters6045d482002-07-09 18:35:34 +0000146Special gimmicks:
147
Georg Brandlc1660762010-12-28 11:38:12 +0000148envvar PYTHONMALLOCSTATS
149 If this envvar exists, a report of pymalloc summary statistics is printed to
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000150 stderr whenever a new arena is allocated, and also by Py_FinalizeEx().
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000151
152Changed in 2.5: The number of extra bytes allocated is 4*sizeof(size_t).
153Before it was 16 on all boxes, reflecting that Python couldn't make use of
154allocations >= 2**32 bytes even on 64-bit boxes before 2.5.
Georg Brandlc1660762010-12-28 11:38:12 +0000155
156
157Py_DEBUG
158--------
Tim Peters6045d482002-07-09 18:35:34 +0000159
160This is what is generally meant by "a debug build" of Python.
161
Victor Stinner235e7b22019-04-25 01:46:31 +0200162Py_DEBUG implies LLTRACE, Py_REF_DEBUG, and PYMALLOC_DEBUG (if
Georg Brandlc1660762010-12-28 11:38:12 +0000163WITH_PYMALLOC is enabled). In addition, C assert()s are enabled (via the C way:
164by not defining NDEBUG), and some routines do additional sanity checks inside
165"#ifdef Py_DEBUG" blocks.
166
167
168COUNT_ALLOCS
169------------
Tim Peters48ba6492002-07-09 19:24:54 +0000170
171Each type object grows three new members:
172
173 /* Number of times an object of this type was allocated. */
Guido van Rossum0c088642002-07-11 01:04:32 +0000174 int tp_allocs;
Tim Peters48ba6492002-07-09 19:24:54 +0000175
176 /* Number of times an object of this type was deallocated. */
Guido van Rossum0c088642002-07-11 01:04:32 +0000177 int tp_frees;
Tim Peters48ba6492002-07-09 19:24:54 +0000178
Guido van Rossum0c088642002-07-11 01:04:32 +0000179 /* Highwater mark: the maximum value of tp_allocs - tp_frees so
180 * far; or, IOW, the largest number of objects of this type alive at
181 * the same time.
182 */
183 int tp_maxalloc;
Tim Peters48ba6492002-07-09 19:24:54 +0000184
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000185Allocation and deallocation code keeps these counts up to date. Py_FinalizeEx()
Georg Brandlc1660762010-12-28 11:38:12 +0000186displays a summary of the info returned by sys.getcounts() (see below), along
187with assorted other special allocation counts (like the number of tuple
188allocations satisfied by a tuple free-list, the number of 1-character strings
189allocated, etc).
Tim Peters48ba6492002-07-09 19:24:54 +0000190
191Before Python 2.2, type objects were immortal, and the COUNT_ALLOCS
Georg Brandlc1660762010-12-28 11:38:12 +0000192implementation relies on that. As of Python 2.2, heap-allocated type/ class
193objects can go away. COUNT_ALLOCS can blow up in 2.2 and 2.2.1 because of this;
194this was fixed in 2.2.2. Use of COUNT_ALLOCS makes all heap-allocated type
195objects immortal, except for those for which no object of that type is ever
196allocated.
Tim Peters48ba6492002-07-09 19:24:54 +0000197
Tim Peters78be7992003-03-23 02:51:01 +0000198Starting with Python 2.3, If Py_TRACE_REFS is also defined, COUNT_ALLOCS
Georg Brandlc1660762010-12-28 11:38:12 +0000199arranges to ensure that the type object for each allocated object appears in the
200doubly-linked list of all objects maintained by Py_TRACE_REFS.
Tim Peters78be7992003-03-23 02:51:01 +0000201
Tim Peters48ba6492002-07-09 19:24:54 +0000202Special gimmicks:
203
204sys.getcounts()
Georg Brandlc1660762010-12-28 11:38:12 +0000205 Return a list of 4-tuples, one entry for each type object for which at least
206 one object of that type was allocated. Each tuple is of the form:
Tim Peters48ba6492002-07-09 19:24:54 +0000207
208 (tp_name, tp_allocs, tp_frees, tp_maxalloc)
209
Georg Brandlc1660762010-12-28 11:38:12 +0000210 Each distinct type object gets a distinct entry in this list, even if two or
211 more type objects have the same tp_name (in which case there's no way to
212 distinguish them by looking at this list). The list is ordered by time of
213 first object allocation: the type object for which the first allocation of
214 an object of that type occurred most recently is at the front of the list.
215
216
217LLTRACE
218-------
Michael W. Hudsona6255232002-07-30 09:49:29 +0000219
Michael W. Hudson46e6d922005-01-18 15:53:59 +0000220Compile in support for Low Level TRACE-ing of the main interpreter loop.
Michael W. Hudsona6255232002-07-30 09:49:29 +0000221
Benjamin Peterson6e820c02015-03-01 20:59:22 -0500222When this preprocessor symbol is defined, before PyEval_EvalFrame executes a
223frame's code it checks the frame's global namespace for a variable
Danish Prakash09efe492018-08-25 07:30:25 +0530224"__ltrace__". If such a variable is found, mounds of information about what
Benjamin Peterson6e820c02015-03-01 20:59:22 -0500225the interpreter is doing are sprayed to stdout, such as every opcode and opcode
226argument and values pushed onto and popped off the value stack.
Michael W. Hudsona6255232002-07-30 09:49:29 +0000227
228Not useful very often, but very useful when needed.
Victor Stinner235e7b22019-04-25 01:46:31 +0200229
230Py_DEBUG implies LLTRACE.