3 # If you use the GNU debugger gdb to debug the Python C runtime, you
4 # might find some of the following commands useful. Copy this to your
5 # ~/.gdbinit file and it'll get loaded into gdb automatically when you
6 # start it up. Then, at the gdb prompt you can do things like:
8 # (gdb) pyo apyobjectptr
9 # <module 'foobar' (built-in)>
15 # Prints a representation of the object to stderr, along with the
16 # number of reference counts it current has and the hex address the
17 # object is allocated at. The argument must be a PyObject*
19 print _PyObject_Dump($arg0)
22 # Prints a representation of the object to stderr, along with the
23 # number of reference counts it current has and the hex address the
24 # object is allocated at. The argument must be a PyGC_Head*
26 print _PyGC_Dump($arg0)
29 # print the local variables of the current frame
32 while $_i < f->f_code->co_nlocals
33 if f->f_localsplus + $_i != 0
34 set $_names = co->co_varnames
35 set $_name = _PyUnicode_AsString(PyTuple_GetItem($_names, $_i))
36 printf "%s:\n", $_name
37 # side effect of calling _PyObject_Dump is to dump the object's
38 # info - assigning just prevents gdb from printing the
40 set $_val = _PyObject_Dump(f->f_localsplus[$_i])
46 # A rewrite of the Python interpreter's line number calculator in GDB's
51 set $__lasti = f->f_lasti
52 set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
53 set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
54 set $__li = $__co->co_firstlineno
56 while ($__sz-1 >= 0 && $__continue)
58 set $__ad = $__ad + *$__p
63 set $__li = $__li + *$__p
69 # print the current frame - verbose
76 set $__fn = _PyUnicode_AsString(co->co_filename)
77 set $__n = _PyUnicode_AsString(co->co_name)
80 printf "): %s\n", $__n
81 ### Uncomment these lines when using from within Emacs/XEmacs so it will
82 ### automatically track/display the current Python source line
83 # printf "%c%c%s:", 032, 032, $__fn
88 ### Use these at your own risk. It appears that a bug in gdb causes it
89 ### to crash in certain circumstances.
102 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
109 # Here's a somewhat fragile way to print the entire Python stack from gdb.
110 # It's fragile because the tests for the value of $pc depend on the layout
111 # of specific functions in the C source code.
113 # Explanation of while and if tests: We want to pop up the stack until we
114 # land in Py_Main (this is probably an incorrect assumption in an embedded
115 # interpreter, but the test can be extended by an interested party). If
116 # Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
117 # tests succeeds as long as it's not true. In a similar fashion the if
118 # statement tests to see if we are in PyEval_EvalFrame().
120 # print the entire Python call stack
122 while $pc < Py_Main || $pc > Py_GetArgcArgv
123 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
131 # print the entire Python call stack - verbose mode
133 while $pc < Py_Main || $pc > Py_GetArgcArgv
134 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
142 # generally useful macro to print a Unicode string
146 while (*$uni && $i++<100)
150 print /x *(short*)$uni++