blob: afefe0818e4c77abb6437cd1a54c2d2a19dae1d6 [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.
Gregory P. Smith3ebc22a2010-10-17 19:40:59 +000015# See Tools/gdb/libpython.py and http://bugs.python.org/issue8032.
Gregory P. Smith03efcf22010-10-17 18:38:04 +000016
Skip Montanaro7286dbd2018-04-05 12:34:44 -050017document pyo
18 Prints a representation of the object to stderr, along with the
19 number of reference counts it currently has and the hex address the
20 object is allocated at. The argument must be a PyObject*
21end
Barry Warsaw39e44d72001-01-23 16:25:19 +000022define pyo
Gregory P. Smith03efcf22010-10-17 18:38:04 +000023 # side effect of calling _PyObject_Dump is to dump the object's
24 # info - assigning just prevents gdb from printing the
25 # NULL return value
26 set $_unused_void = _PyObject_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000027end
28
Skip Montanaro7286dbd2018-04-05 12:34:44 -050029document pyg
30 Prints a representation of the object to stderr, along with the
31 number of reference counts it currently has and the hex address the
32 object is allocated at. The argument must be a PyGC_Head*
33end
Barry Warsaw39e44d72001-01-23 16:25:19 +000034define pyg
Gregory P. Smith03efcf22010-10-17 18:38:04 +000035 print _PyGC_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000036end
Jeremy Hyltonf64ec0f2003-10-03 20:56:15 +000037
Skip Montanaro7286dbd2018-04-05 12:34:44 -050038document pylocals
39 Print the local variables of the current frame.
40end
Skip Montanaro74d07f22004-04-02 14:51:13 +000041define pylocals
42 set $_i = 0
Georg Brandl9b21dbc2009-07-23 09:19:09 +000043 while $_i < f->f_code->co_nlocals
Skip Montanaro74d07f22004-04-02 14:51:13 +000044 if f->f_localsplus + $_i != 0
Marcel Plch3a9ccee2018-04-06 23:22:04 +020045 set $_names = f->f_code->co_varnames
46 set $_name = PyUnicode_AsUTF8(PyTuple_GetItem($_names, $_i))
Skip Montanaro74d07f22004-04-02 14:51:13 +000047 printf "%s:\n", $_name
Gregory P. Smith03efcf22010-10-17 18:38:04 +000048 pyo f->f_localsplus[$_i]
Skip Montanaro74d07f22004-04-02 14:51:13 +000049 end
50 set $_i = $_i + 1
51 end
52end
53
Skip Montanaroafd77d92005-01-08 21:56:43 +000054# A rewrite of the Python interpreter's line number calculator in GDB's
55# command language
56define lineno
Neal Norwitz4655e442005-09-05 16:16:49 +000057 set $__continue = 1
Skip Montanaroafd77d92005-01-08 21:56:43 +000058 set $__co = f->f_code
59 set $__lasti = f->f_lasti
Neal Norwitz44c19f62007-08-27 02:49:29 +000060 set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000061 set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
Skip Montanaroafd77d92005-01-08 21:56:43 +000062 set $__li = $__co->co_firstlineno
63 set $__ad = 0
Neal Norwitz4655e442005-09-05 16:16:49 +000064 while ($__sz-1 >= 0 && $__continue)
Skip Montanaroafd77d92005-01-08 21:56:43 +000065 set $__sz = $__sz - 1
66 set $__ad = $__ad + *$__p
67 set $__p = $__p + 1
68 if ($__ad > $__lasti)
Neal Norwitz4655e442005-09-05 16:16:49 +000069 set $__continue = 0
Georg Brandl29848f02010-10-21 12:59:14 +000070 else
71 set $__li = $__li + *$__p
72 set $__p = $__p + 1
Skip Montanaroafd77d92005-01-08 21:56:43 +000073 end
Skip Montanaroafd77d92005-01-08 21:56:43 +000074 end
Georg Brandl0a9397f2010-11-03 07:41:00 +000075 printf "%d", $__li
Skip Montanaroafd77d92005-01-08 21:56:43 +000076end
77
Skip Montanaro7286dbd2018-04-05 12:34:44 -050078document pyframev
79 Print the current frame - verbose
80end
Skip Montanaro0bb2a652004-11-17 16:04:15 +000081define pyframev
82 pyframe
Skip Montanaro74d07f22004-04-02 14:51:13 +000083 pylocals
84end
85
Skip Montanaro0bb2a652004-11-17 16:04:15 +000086define pyframe
Marcel Plch3a9ccee2018-04-06 23:22:04 +020087 set $__fn = PyUnicode_AsUTF8(f->f_code->co_filename)
88 set $__n = PyUnicode_AsUTF8(f->f_code->co_name)
Skip Montanaroafd77d92005-01-08 21:56:43 +000089 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
Marcel Plch3a9ccee2018-04-06 23:22:04 +0200113 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
Skip Montanaroafd77d92005-01-08 21:56:43 +0000114 pyframe
115 else
116 frame
117 end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000118end
119
Skip Montanaro7a92d742004-04-02 14:53:55 +0000120# 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
Skip Montanaroae5465a2010-01-14 01:14:50 +0000129# 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.
Skip Montanaro7a92d742004-04-02 14:53:55 +0000136
Skip Montanaro7286dbd2018-04-05 12:34:44 -0500137document pystack
138 Print the entire Python call stack
139end
Skip Montanaro74d07f22004-04-02 14:51:13 +0000140define pystack
141 while $pc < Py_Main || $pc > Py_GetArgcArgv
Marcel Plch3a9ccee2018-04-06 23:22:04 +0200142 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
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
Skip Montanaro7286dbd2018-04-05 12:34:44 -0500150document pystackv
151 Print the entire Python call stack - verbose mode
152end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000153define pystackv
154 while $pc < Py_Main || $pc > Py_GetArgcArgv
Marcel Plch3a9ccee2018-04-06 23:22:04 +0200155 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000156 pyframev
157 end
158 up-silently 1
159 end
160 select-frame 0
161end
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000162
Skip Montanaro7286dbd2018-04-05 12:34:44 -0500163document pu
164 Generally useful macro to print a Unicode string
165end
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000166def pu
Serhiy Storchakabfbfc8d2015-03-29 19:12:58 +0300167 set $uni = $arg0
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000168 set $i = 0
169 while (*$uni && $i++<100)
Serhiy Storchakabfbfc8d2015-03-29 19:12:58 +0300170 if (*$uni < 0x80)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000171 print *(char*)$uni++
172 else
173 print /x *(short*)$uni++
174 end
175 end
176end