1# If you use the GNU debugger gdb to debug the Python C runtime, you 2# might find some of the following commands useful. Copy this to your 3# ~/.gdbinit file and it'll get loaded into gdb automatically when you 4# start it up. Then, at the gdb prompt you can do things like: 5# 6# (gdb) pyo apyobjectptr 7# <module 'foobar' (built-in)> 8# refcounts: 1 9# address : 84a7a2c 10# $1 = void 11# (gdb) 12# 13# NOTE: If you have gdb 7 or later, it supports debugging of Python directly 14# with embedded macros that you may find superior to what is in here. 15# See Tools/gdb/libpython.py and http://bugs.python.org/issue8032. 16 17define pyo 18 # side effect of calling _PyObject_Dump is to dump the object's 19 # info - assigning just prevents gdb from printing the 20 # NULL return value 21 set $_unused_void = _PyObject_Dump($arg0) 22end 23document pyo 24 Prints a representation of the object to stderr, along with the 25 number of reference counts it currently has and the hex address the 26 object is allocated at. The argument must be a PyObject* 27end 28 29define pyg 30 print _PyGC_Dump($arg0) 31end 32document pyg 33 Prints a representation of the object to stderr, along with the 34 number of reference counts it currently has and the hex address the 35 object is allocated at. The argument must be a PyGC_Head* 36end 37 38define pylocals 39 set $_i = 0 40 while $_i < f->f_code->co_nlocals 41 if f->f_localsplus + $_i != 0 42 set $_names = f->f_code->co_varnames 43 set $_name = PyUnicode_AsUTF8(PyTuple_GetItem($_names, $_i)) 44 printf "%s:\n", $_name 45 pyo f->f_localsplus[$_i] 46 end 47 set $_i = $_i + 1 48 end 49end 50document pylocals 51 Print the local variables of the current frame. 52end 53 54# A rewrite of the Python interpreter's line number calculator in GDB's 55# command language 56define lineno 57 set $__continue = 1 58 set $__co = f->f_code 59 set $__lasti = f->f_lasti 60 set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2 61 set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval 62 set $__li = $__co->co_firstlineno 63 set $__ad = 0 64 while ($__sz-1 >= 0 && $__continue) 65 set $__sz = $__sz - 1 66 set $__ad = $__ad + *$__p 67 set $__p = $__p + 1 68 if ($__ad > $__lasti) 69 set $__continue = 0 70 else 71 set $__li = $__li + *$__p 72 set $__p = $__p + 1 73 end 74 end 75 printf "%d", $__li 76end 77 78define pyframev 79 pyframe 80 pylocals 81end 82document pyframev 83 Print the current frame - verbose 84end 85 86define pyframe 87 set $__fn = PyUnicode_AsUTF8(f->f_code->co_filename) 88 set $__n = PyUnicode_AsUTF8(f->f_code->co_name) 89 printf "%s (", $__fn 90 lineno 91 printf "): %s\n", $__n 92### Uncomment these lines when using from within Emacs/XEmacs so it will 93### automatically track/display the current Python source line 94# printf "%c%c%s:", 032, 032, $__fn 95# lineno 96# printf ":1\n" 97end 98 99### Use these at your own risk. It appears that a bug in gdb causes it 100### to crash in certain circumstances. 101 102#define up 103# up-silently 1 104# printframe 105#end 106 107#define down 108# down-silently 1 109# printframe 110#end 111 112define printframe 113 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault 114 pyframe 115 else 116 frame 117 end 118end 119 120# Here's a somewhat fragile way to print the entire Python stack from gdb. 121# It's fragile because the tests for the value of $pc depend on the layout 122# of specific functions in the C source code. 123 124# Explanation of while and if tests: We want to pop up the stack until we 125# land in Py_Main (this is probably an incorrect assumption in an embedded 126# interpreter, but the test can be extended by an interested party). If 127# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while 128# tests succeeds as long as it's not true. In a similar fashion the if 129# statement tests to see if we are in PyEval_EvalFrameEx(). 130 131# Note: The name of the main interpreter function and the function which 132# follow it has changed over time. This version of pystack works with this 133# version of Python. If you try using it with older or newer versions of 134# the interpreter you may will have to change the functions you compare with 135# $pc. 136 137define pystack 138 while $pc < Py_Main || $pc > Py_GetArgcArgv 139 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault 140 pyframe 141 end 142 up-silently 1 143 end 144 select-frame 0 145end 146document pystack 147 Print the entire Python call stack 148end 149 150define pystackv 151 while $pc < Py_Main || $pc > Py_GetArgcArgv 152 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault 153 pyframev 154 end 155 up-silently 1 156 end 157 select-frame 0 158end 159document pystackv 160 Print the entire Python call stack - verbose mode 161end 162 163define pu 164 set $uni = $arg0 165 set $i = 0 166 while (*$uni && $i++<100) 167 if (*$uni < 0x80) 168 print *(char*)$uni++ 169 else 170 print /x *(short*)$uni++ 171 end 172 end 173end 174document pu 175 Generally useful macro to print a Unicode string 176end 177