blob: 2c89a7abf6ca6c41805613f26f2cb5fdcafb24c2 [file] [log] [blame]
Barry Warsaw39e44d72001-01-23 16:25:19 +00001# 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)
Gregory P. Smith14684292010-10-17 18:49:46 +000012#
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 https://fedoraproject.org/wiki/Features/EasierPythonDebugging
16# and http://bugs.python.org/issue8032 for more gdb 7 python information.
17
Barry Warsaw39e44d72001-01-23 16:25:19 +000018# Prints a representation of the object to stderr, along with the
19# number of reference counts it current has and the hex address the
20# object is allocated at. The argument must be a PyObject*
21define pyo
Gregory P. Smith14684292010-10-17 18:49:46 +000022 # side effect of calling _PyObject_Dump is to dump the object's
23 # info - assigning just prevents gdb from printing the
24 # NULL return value
25 set $_unused_void = _PyObject_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000026end
27
28# Prints a representation of the object to stderr, along with the
29# number of reference counts it current has and the hex address the
30# object is allocated at. The argument must be a PyGC_Head*
31define pyg
Gregory P. Smith14684292010-10-17 18:49:46 +000032 print _PyGC_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000033end
Jeremy Hyltonf64ec0f2003-10-03 20:56:15 +000034
Skip Montanaro74d07f22004-04-02 14:51:13 +000035# print the local variables of the current frame
36define pylocals
37 set $_i = 0
Georg Brandlc5605df2009-08-13 08:26:44 +000038 while $_i < f->f_code->co_nlocals
Skip Montanaro74d07f22004-04-02 14:51:13 +000039 if f->f_localsplus + $_i != 0
40 set $_names = co->co_varnames
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000041 set $_name = _PyUnicode_AsString(PyTuple_GetItem($_names, $_i))
Skip Montanaro74d07f22004-04-02 14:51:13 +000042 printf "%s:\n", $_name
Gregory P. Smith14684292010-10-17 18:49:46 +000043 pyo f->f_localsplus[$_i]
Skip Montanaro74d07f22004-04-02 14:51:13 +000044 end
45 set $_i = $_i + 1
46 end
47end
48
Skip Montanaroafd77d92005-01-08 21:56:43 +000049# A rewrite of the Python interpreter's line number calculator in GDB's
50# command language
51define lineno
Neal Norwitz4655e442005-09-05 16:16:49 +000052 set $__continue = 1
Skip Montanaroafd77d92005-01-08 21:56:43 +000053 set $__co = f->f_code
54 set $__lasti = f->f_lasti
Neal Norwitz44c19f62007-08-27 02:49:29 +000055 set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000056 set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
Skip Montanaroafd77d92005-01-08 21:56:43 +000057 set $__li = $__co->co_firstlineno
58 set $__ad = 0
Neal Norwitz4655e442005-09-05 16:16:49 +000059 while ($__sz-1 >= 0 && $__continue)
Skip Montanaroafd77d92005-01-08 21:56:43 +000060 set $__sz = $__sz - 1
61 set $__ad = $__ad + *$__p
62 set $__p = $__p + 1
63 if ($__ad > $__lasti)
Neal Norwitz4655e442005-09-05 16:16:49 +000064 set $__continue = 0
Georg Brandld62ecbf2010-11-26 08:52:36 +000065 else
66 set $__li = $__li + *$__p
67 set $__p = $__p + 1
Skip Montanaroafd77d92005-01-08 21:56:43 +000068 end
Skip Montanaroafd77d92005-01-08 21:56:43 +000069 end
Georg Brandlf65e25b2010-11-26 09:05:43 +000070 printf "%d", $__li
Skip Montanaroafd77d92005-01-08 21:56:43 +000071end
72
Skip Montanaro0bb2a652004-11-17 16:04:15 +000073# print the current frame - verbose
74define pyframev
75 pyframe
Skip Montanaro74d07f22004-04-02 14:51:13 +000076 pylocals
77end
78
Skip Montanaro0bb2a652004-11-17 16:04:15 +000079define pyframe
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000080 set $__fn = _PyUnicode_AsString(co->co_filename)
81 set $__n = _PyUnicode_AsString(co->co_name)
Skip Montanaroafd77d92005-01-08 21:56:43 +000082 printf "%s (", $__fn
83 lineno
84 printf "): %s\n", $__n
85### Uncomment these lines when using from within Emacs/XEmacs so it will
86### automatically track/display the current Python source line
87# printf "%c%c%s:", 032, 032, $__fn
88# lineno
89# printf ":1\n"
90end
91
92### Use these at your own risk. It appears that a bug in gdb causes it
93### to crash in certain circumstances.
94
95#define up
96# up-silently 1
97# printframe
98#end
99
100#define down
101# down-silently 1
102# printframe
103#end
104
105define printframe
Neil Schemenauerf98e6b12005-08-13 00:28:41 +0000106 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaroafd77d92005-01-08 21:56:43 +0000107 pyframe
108 else
109 frame
110 end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000111end
112
Skip Montanaro7a92d742004-04-02 14:53:55 +0000113# Here's a somewhat fragile way to print the entire Python stack from gdb.
114# It's fragile because the tests for the value of $pc depend on the layout
115# of specific functions in the C source code.
116
117# Explanation of while and if tests: We want to pop up the stack until we
118# land in Py_Main (this is probably an incorrect assumption in an embedded
119# interpreter, but the test can be extended by an interested party). If
120# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
121# tests succeeds as long as it's not true. In a similar fashion the if
Skip Montanaroafd77d92005-01-08 21:56:43 +0000122# statement tests to see if we are in PyEval_EvalFrame().
Skip Montanaro7a92d742004-04-02 14:53:55 +0000123
Skip Montanaro74d07f22004-04-02 14:51:13 +0000124# print the entire Python call stack
125define pystack
126 while $pc < Py_Main || $pc > Py_GetArgcArgv
Michael W. Hudson8c47f4a2004-08-07 20:11:22 +0000127 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
Skip Montanaro74d07f22004-04-02 14:51:13 +0000128 pyframe
129 end
130 up-silently 1
131 end
132 select-frame 0
133end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000134
135# print the entire Python call stack - verbose mode
136define pystackv
137 while $pc < Py_Main || $pc > Py_GetArgcArgv
138 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
139 pyframev
140 end
141 up-silently 1
142 end
143 select-frame 0
144end
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000145
146# generally useful macro to print a Unicode string
147def pu
148 set $uni = $arg0
149 set $i = 0
150 while (*$uni && $i++<100)
151 if (*$uni < 0x80)
152 print *(char*)$uni++
153 else
154 print /x *(short*)$uni++
155 end
156 end
157end