blob: e8f62ba64764236ee5e69af100dd7bb32e6c3bb0 [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
Barry Warsaw39e44d72001-01-23 16:25:19 +000017define pyo
Gregory P. Smith03efcf22010-10-17 18:38:04 +000018 # 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)
Barry Warsaw39e44d72001-01-23 16:25:19 +000022end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +020023document 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
Barry Warsaw39e44d72001-01-23 16:25:19 +000028
Florian Bruhin1f86fdc2019-09-09 11:06:37 +020029define pyg
30 print _PyGC_Dump($arg0)
31end
Skip Montanaro7286dbd2018-04-05 12:34:44 -050032document 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
Jeremy Hyltonf64ec0f2003-10-03 20:56:15 +000037
Skip Montanaro74d07f22004-04-02 14:51:13 +000038define pylocals
39 set $_i = 0
Georg Brandl9b21dbc2009-07-23 09:19:09 +000040 while $_i < f->f_code->co_nlocals
Skip Montanaro74d07f22004-04-02 14:51:13 +000041 if f->f_localsplus + $_i != 0
Marcel Plch3a9ccee2018-04-06 23:22:04 +020042 set $_names = f->f_code->co_varnames
43 set $_name = PyUnicode_AsUTF8(PyTuple_GetItem($_names, $_i))
Skip Montanaro74d07f22004-04-02 14:51:13 +000044 printf "%s:\n", $_name
Gregory P. Smith03efcf22010-10-17 18:38:04 +000045 pyo f->f_localsplus[$_i]
Skip Montanaro74d07f22004-04-02 14:51:13 +000046 end
47 set $_i = $_i + 1
48 end
49end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +020050document pylocals
51 Print the local variables of the current frame.
52end
Skip Montanaro74d07f22004-04-02 14:51:13 +000053
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 Montanaro0bb2a652004-11-17 16:04:15 +000078define pyframev
79 pyframe
Skip Montanaro74d07f22004-04-02 14:51:13 +000080 pylocals
81end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +020082document pyframev
83 Print the current frame - verbose
84end
Skip Montanaro74d07f22004-04-02 14:51:13 +000085
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 Montanaro74d07f22004-04-02 14:51:13 +0000137define pystack
138 while $pc < Py_Main || $pc > Py_GetArgcArgv
Marcel Plch3a9ccee2018-04-06 23:22:04 +0200139 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
Skip Montanaro74d07f22004-04-02 14:51:13 +0000140 pyframe
141 end
142 up-silently 1
143 end
144 select-frame 0
145end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +0200146document pystack
147 Print the entire Python call stack
Skip Montanaro7286dbd2018-04-05 12:34:44 -0500148end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +0200149
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000150define pystackv
151 while $pc < Py_Main || $pc > Py_GetArgcArgv
Marcel Plch3a9ccee2018-04-06 23:22:04 +0200152 if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000153 pyframev
154 end
155 up-silently 1
156 end
157 select-frame 0
158end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +0200159document pystackv
160 Print the entire Python call stack - verbose mode
Skip Montanaro7286dbd2018-04-05 12:34:44 -0500161end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +0200162
Florian Bruhin12211352020-04-23 14:49:26 +0200163define pu
Serhiy Storchakabfbfc8d2015-03-29 19:12:58 +0300164 set $uni = $arg0
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000165 set $i = 0
166 while (*$uni && $i++<100)
Serhiy Storchakabfbfc8d2015-03-29 19:12:58 +0300167 if (*$uni < 0x80)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000168 print *(char*)$uni++
169 else
170 print /x *(short*)$uni++
171 end
172 end
173end
Florian Bruhin1f86fdc2019-09-09 11:06:37 +0200174document pu
175 Generally useful macro to print a Unicode string
176end