blob: f87881e8d697762f1df3c87db6a3eec8b3d94f27 [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. Smith03efcf22010-10-17 18:38:04 +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
18# gdb version of Py_DECREF(obj) macro
19define py_decref
20 set $__obj = $arg0
21 set $__obj->ob_refcnt -= 1
22 if ($__obj->ob_refcnt == 0)
23 set $__obj = _Py_Dealloc($__obj)
24 end
25end
Barry Warsaw39e44d72001-01-23 16:25:19 +000026
27# Prints a representation of the object to stderr, along with the
28# number of reference counts it current has and the hex address the
29# object is allocated at. The argument must be a PyObject*
30define pyo
Gregory P. Smith03efcf22010-10-17 18:38:04 +000031 # side effect of calling _PyObject_Dump is to dump the object's
32 # info - assigning just prevents gdb from printing the
33 # NULL return value
34 set $_unused_void = _PyObject_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000035end
36
37# Prints a representation of the object to stderr, along with the
38# number of reference counts it current has and the hex address the
39# object is allocated at. The argument must be a PyGC_Head*
40define pyg
Gregory P. Smith03efcf22010-10-17 18:38:04 +000041 print _PyGC_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000042end
Jeremy Hyltonf64ec0f2003-10-03 20:56:15 +000043
Skip Montanaro74d07f22004-04-02 14:51:13 +000044# print the local variables of the current frame
45define pylocals
46 set $_i = 0
Georg Brandl9b21dbc2009-07-23 09:19:09 +000047 while $_i < f->f_code->co_nlocals
Skip Montanaro74d07f22004-04-02 14:51:13 +000048 if f->f_localsplus + $_i != 0
49 set $_names = co->co_varnames
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000050 set $_name = _PyUnicode_AsString(PyTuple_GetItem($_names, $_i))
Skip Montanaro74d07f22004-04-02 14:51:13 +000051 printf "%s:\n", $_name
Gregory P. Smith03efcf22010-10-17 18:38:04 +000052 py_decref $_name
53 pyo f->f_localsplus[$_i]
Skip Montanaro74d07f22004-04-02 14:51:13 +000054 end
55 set $_i = $_i + 1
56 end
57end
58
Skip Montanaroafd77d92005-01-08 21:56:43 +000059# A rewrite of the Python interpreter's line number calculator in GDB's
60# command language
61define lineno
Neal Norwitz4655e442005-09-05 16:16:49 +000062 set $__continue = 1
Skip Montanaroafd77d92005-01-08 21:56:43 +000063 set $__co = f->f_code
64 set $__lasti = f->f_lasti
Neal Norwitz44c19f62007-08-27 02:49:29 +000065 set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000066 set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
Skip Montanaroafd77d92005-01-08 21:56:43 +000067 set $__li = $__co->co_firstlineno
68 set $__ad = 0
Neal Norwitz4655e442005-09-05 16:16:49 +000069 while ($__sz-1 >= 0 && $__continue)
Skip Montanaroafd77d92005-01-08 21:56:43 +000070 set $__sz = $__sz - 1
71 set $__ad = $__ad + *$__p
72 set $__p = $__p + 1
73 if ($__ad > $__lasti)
Neal Norwitz4655e442005-09-05 16:16:49 +000074 set $__continue = 0
Skip Montanaroafd77d92005-01-08 21:56:43 +000075 end
76 set $__li = $__li + *$__p
77 set $__p = $__p + 1
78 end
79 printf "%d", $__li
80end
81
Skip Montanaro0bb2a652004-11-17 16:04:15 +000082# print the current frame - verbose
83define pyframev
84 pyframe
Skip Montanaro74d07f22004-04-02 14:51:13 +000085 pylocals
86end
87
Skip Montanaro0bb2a652004-11-17 16:04:15 +000088define pyframe
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000089 set $__fn = _PyUnicode_AsString(co->co_filename)
90 set $__n = _PyUnicode_AsString(co->co_name)
Skip Montanaroafd77d92005-01-08 21:56:43 +000091 printf "%s (", $__fn
92 lineno
93 printf "): %s\n", $__n
94### Uncomment these lines when using from within Emacs/XEmacs so it will
95### automatically track/display the current Python source line
96# printf "%c%c%s:", 032, 032, $__fn
97# lineno
98# printf ":1\n"
99end
100
101### Use these at your own risk. It appears that a bug in gdb causes it
102### to crash in certain circumstances.
103
104#define up
105# up-silently 1
106# printframe
107#end
108
109#define down
110# down-silently 1
111# printframe
112#end
113
114define printframe
Neil Schemenauerf98e6b12005-08-13 00:28:41 +0000115 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaroafd77d92005-01-08 21:56:43 +0000116 pyframe
117 else
118 frame
119 end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000120end
121
Skip Montanaro7a92d742004-04-02 14:53:55 +0000122# Here's a somewhat fragile way to print the entire Python stack from gdb.
123# It's fragile because the tests for the value of $pc depend on the layout
124# of specific functions in the C source code.
125
126# Explanation of while and if tests: We want to pop up the stack until we
127# land in Py_Main (this is probably an incorrect assumption in an embedded
128# interpreter, but the test can be extended by an interested party). If
129# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
130# tests succeeds as long as it's not true. In a similar fashion the if
Skip Montanaroae5465a2010-01-14 01:14:50 +0000131# statement tests to see if we are in PyEval_EvalFrameEx().
132
133# Note: The name of the main interpreter function and the function which
134# follow it has changed over time. This version of pystack works with this
135# version of Python. If you try using it with older or newer versions of
136# the interpreter you may will have to change the functions you compare with
137# $pc.
Skip Montanaro7a92d742004-04-02 14:53:55 +0000138
Skip Montanaro74d07f22004-04-02 14:51:13 +0000139# print the entire Python call stack
140define pystack
141 while $pc < Py_Main || $pc > Py_GetArgcArgv
Skip Montanaroae5465a2010-01-14 01:14:50 +0000142 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaro74d07f22004-04-02 14:51:13 +0000143 pyframe
144 end
145 up-silently 1
146 end
147 select-frame 0
148end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000149
150# print the entire Python call stack - verbose mode
151define pystackv
152 while $pc < Py_Main || $pc > Py_GetArgcArgv
Skip Montanaroae5465a2010-01-14 01:14:50 +0000153 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000154 pyframev
155 end
156 up-silently 1
157 end
158 select-frame 0
159end
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000160
161# generally useful macro to print a Unicode string
162def pu
163 set $uni = $arg0
164 set $i = 0
165 while (*$uni && $i++<100)
166 if (*$uni < 0x80)
167 print *(char*)$uni++
168 else
169 print /x *(short*)$uni++
170 end
171 end
172end