blob: 3b4dec6198d8f369d281ce6540f7d4ba810baaca [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 +000017# Prints a representation of the object to stderr, along with the
18# number of reference counts it current has and the hex address the
19# object is allocated at. The argument must be a PyObject*
20define pyo
Gregory P. Smith03efcf22010-10-17 18:38:04 +000021 # side effect of calling _PyObject_Dump is to dump the object's
22 # info - assigning just prevents gdb from printing the
23 # NULL return value
24 set $_unused_void = _PyObject_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000025end
26
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 PyGC_Head*
30define pyg
Gregory P. Smith03efcf22010-10-17 18:38:04 +000031 print _PyGC_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000032end
Jeremy Hyltonf64ec0f2003-10-03 20:56:15 +000033
Skip Montanaro74d07f22004-04-02 14:51:13 +000034# print the local variables of the current frame
35define pylocals
36 set $_i = 0
Georg Brandl9b21dbc2009-07-23 09:19:09 +000037 while $_i < f->f_code->co_nlocals
Skip Montanaro74d07f22004-04-02 14:51:13 +000038 if f->f_localsplus + $_i != 0
39 set $_names = co->co_varnames
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000040 set $_name = _PyUnicode_AsString(PyTuple_GetItem($_names, $_i))
Skip Montanaro74d07f22004-04-02 14:51:13 +000041 printf "%s:\n", $_name
Gregory P. Smith03efcf22010-10-17 18:38:04 +000042 pyo f->f_localsplus[$_i]
Skip Montanaro74d07f22004-04-02 14:51:13 +000043 end
44 set $_i = $_i + 1
45 end
46end
47
Skip Montanaroafd77d92005-01-08 21:56:43 +000048# A rewrite of the Python interpreter's line number calculator in GDB's
49# command language
50define lineno
Neal Norwitz4655e442005-09-05 16:16:49 +000051 set $__continue = 1
Skip Montanaroafd77d92005-01-08 21:56:43 +000052 set $__co = f->f_code
53 set $__lasti = f->f_lasti
Neal Norwitz44c19f62007-08-27 02:49:29 +000054 set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000055 set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
Skip Montanaroafd77d92005-01-08 21:56:43 +000056 set $__li = $__co->co_firstlineno
57 set $__ad = 0
Neal Norwitz4655e442005-09-05 16:16:49 +000058 while ($__sz-1 >= 0 && $__continue)
Skip Montanaroafd77d92005-01-08 21:56:43 +000059 set $__sz = $__sz - 1
60 set $__ad = $__ad + *$__p
61 set $__p = $__p + 1
62 if ($__ad > $__lasti)
Neal Norwitz4655e442005-09-05 16:16:49 +000063 set $__continue = 0
Skip Montanaroafd77d92005-01-08 21:56:43 +000064 end
65 set $__li = $__li + *$__p
66 set $__p = $__p + 1
67 end
68 printf "%d", $__li
69end
70
Skip Montanaro0bb2a652004-11-17 16:04:15 +000071# print the current frame - verbose
72define pyframev
73 pyframe
Skip Montanaro74d07f22004-04-02 14:51:13 +000074 pylocals
75end
76
Skip Montanaro0bb2a652004-11-17 16:04:15 +000077define pyframe
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000078 set $__fn = _PyUnicode_AsString(co->co_filename)
79 set $__n = _PyUnicode_AsString(co->co_name)
Skip Montanaroafd77d92005-01-08 21:56:43 +000080 printf "%s (", $__fn
81 lineno
82 printf "): %s\n", $__n
83### Uncomment these lines when using from within Emacs/XEmacs so it will
84### automatically track/display the current Python source line
85# printf "%c%c%s:", 032, 032, $__fn
86# lineno
87# printf ":1\n"
88end
89
90### Use these at your own risk. It appears that a bug in gdb causes it
91### to crash in certain circumstances.
92
93#define up
94# up-silently 1
95# printframe
96#end
97
98#define down
99# down-silently 1
100# printframe
101#end
102
103define printframe
Neil Schemenauerf98e6b12005-08-13 00:28:41 +0000104 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaroafd77d92005-01-08 21:56:43 +0000105 pyframe
106 else
107 frame
108 end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000109end
110
Skip Montanaro7a92d742004-04-02 14:53:55 +0000111# Here's a somewhat fragile way to print the entire Python stack from gdb.
112# It's fragile because the tests for the value of $pc depend on the layout
113# of specific functions in the C source code.
114
115# Explanation of while and if tests: We want to pop up the stack until we
116# land in Py_Main (this is probably an incorrect assumption in an embedded
117# interpreter, but the test can be extended by an interested party). If
118# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
119# tests succeeds as long as it's not true. In a similar fashion the if
Skip Montanaroae5465a2010-01-14 01:14:50 +0000120# statement tests to see if we are in PyEval_EvalFrameEx().
121
122# Note: The name of the main interpreter function and the function which
123# follow it has changed over time. This version of pystack works with this
124# version of Python. If you try using it with older or newer versions of
125# the interpreter you may will have to change the functions you compare with
126# $pc.
Skip Montanaro7a92d742004-04-02 14:53:55 +0000127
Skip Montanaro74d07f22004-04-02 14:51:13 +0000128# print the entire Python call stack
129define pystack
130 while $pc < Py_Main || $pc > Py_GetArgcArgv
Skip Montanaroae5465a2010-01-14 01:14:50 +0000131 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaro74d07f22004-04-02 14:51:13 +0000132 pyframe
133 end
134 up-silently 1
135 end
136 select-frame 0
137end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000138
139# print the entire Python call stack - verbose mode
140define pystackv
141 while $pc < Py_Main || $pc > Py_GetArgcArgv
Skip Montanaroae5465a2010-01-14 01:14:50 +0000142 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000143 pyframev
144 end
145 up-silently 1
146 end
147 select-frame 0
148end
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000149
150# generally useful macro to print a Unicode string
151def pu
152 set $uni = $arg0
153 set $i = 0
154 while (*$uni && $i++<100)
155 if (*$uni < 0x80)
156 print *(char*)$uni++
157 else
158 print /x *(short*)$uni++
159 end
160 end
161end