blob: ca164cda895978e3b26a15631899bc7219a98882 [file] [log] [blame]
Barry Warsaw39e44d72001-01-23 16:25:19 +00001# -*- ksh -*-
2#
3# If you use the GNU debugger gdb to debug the Python C runtime, you
4# might find some of the following commands useful. Copy this to your
5# ~/.gdbinit file and it'll get loaded into gdb automatically when you
6# start it up. Then, at the gdb prompt you can do things like:
7#
8# (gdb) pyo apyobjectptr
9# <module 'foobar' (built-in)>
10# refcounts: 1
11# address : 84a7a2c
12# $1 = void
13# (gdb)
14
15# Prints a representation of the object to stderr, along with the
16# number of reference counts it current has and the hex address the
17# object is allocated at. The argument must be a PyObject*
18define pyo
Barry Warsawbbd89b62001-01-24 04:18:13 +000019print _PyObject_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000020end
21
22# Prints a representation of the object to stderr, along with the
23# number of reference counts it current has and the hex address the
24# object is allocated at. The argument must be a PyGC_Head*
25define pyg
Barry Warsawbbd89b62001-01-24 04:18:13 +000026print _PyGC_Dump($arg0)
Barry Warsaw39e44d72001-01-23 16:25:19 +000027end
Jeremy Hyltonf64ec0f2003-10-03 20:56:15 +000028
Skip Montanaro74d07f22004-04-02 14:51:13 +000029# print the local variables of the current frame
30define pylocals
31 set $_i = 0
Georg Brandl9b21dbc2009-07-23 09:19:09 +000032 while $_i < f->f_code->co_nlocals
Skip Montanaro74d07f22004-04-02 14:51:13 +000033 if f->f_localsplus + $_i != 0
34 set $_names = co->co_varnames
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000035 set $_name = _PyUnicode_AsString(PyTuple_GetItem($_names, $_i))
Skip Montanaro74d07f22004-04-02 14:51:13 +000036 printf "%s:\n", $_name
37 # side effect of calling _PyObject_Dump is to dump the object's
38 # info - assigning just prevents gdb from printing the
39 # NULL return value
40 set $_val = _PyObject_Dump(f->f_localsplus[$_i])
41 end
42 set $_i = $_i + 1
43 end
44end
45
Skip Montanaroafd77d92005-01-08 21:56:43 +000046# A rewrite of the Python interpreter's line number calculator in GDB's
47# command language
48define lineno
Neal Norwitz4655e442005-09-05 16:16:49 +000049 set $__continue = 1
Skip Montanaroafd77d92005-01-08 21:56:43 +000050 set $__co = f->f_code
51 set $__lasti = f->f_lasti
Neal Norwitz44c19f62007-08-27 02:49:29 +000052 set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000053 set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
Skip Montanaroafd77d92005-01-08 21:56:43 +000054 set $__li = $__co->co_firstlineno
55 set $__ad = 0
Neal Norwitz4655e442005-09-05 16:16:49 +000056 while ($__sz-1 >= 0 && $__continue)
Skip Montanaroafd77d92005-01-08 21:56:43 +000057 set $__sz = $__sz - 1
58 set $__ad = $__ad + *$__p
59 set $__p = $__p + 1
60 if ($__ad > $__lasti)
Neal Norwitz4655e442005-09-05 16:16:49 +000061 set $__continue = 0
Skip Montanaroafd77d92005-01-08 21:56:43 +000062 end
63 set $__li = $__li + *$__p
64 set $__p = $__p + 1
65 end
66 printf "%d", $__li
67end
68
Skip Montanaro0bb2a652004-11-17 16:04:15 +000069# print the current frame - verbose
70define pyframev
71 pyframe
Skip Montanaro74d07f22004-04-02 14:51:13 +000072 pylocals
73end
74
Skip Montanaro0bb2a652004-11-17 16:04:15 +000075define pyframe
Neal Norwitz8f2f22a2008-08-24 20:59:23 +000076 set $__fn = _PyUnicode_AsString(co->co_filename)
77 set $__n = _PyUnicode_AsString(co->co_name)
Skip Montanaroafd77d92005-01-08 21:56:43 +000078 printf "%s (", $__fn
79 lineno
80 printf "): %s\n", $__n
81### Uncomment these lines when using from within Emacs/XEmacs so it will
82### automatically track/display the current Python source line
83# printf "%c%c%s:", 032, 032, $__fn
84# lineno
85# printf ":1\n"
86end
87
88### Use these at your own risk. It appears that a bug in gdb causes it
89### to crash in certain circumstances.
90
91#define up
92# up-silently 1
93# printframe
94#end
95
96#define down
97# down-silently 1
98# printframe
99#end
100
101define printframe
Neil Schemenauerf98e6b12005-08-13 00:28:41 +0000102 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaroafd77d92005-01-08 21:56:43 +0000103 pyframe
104 else
105 frame
106 end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000107end
108
Skip Montanaro7a92d742004-04-02 14:53:55 +0000109# Here's a somewhat fragile way to print the entire Python stack from gdb.
110# It's fragile because the tests for the value of $pc depend on the layout
111# of specific functions in the C source code.
112
113# Explanation of while and if tests: We want to pop up the stack until we
114# land in Py_Main (this is probably an incorrect assumption in an embedded
115# interpreter, but the test can be extended by an interested party). If
116# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
117# tests succeeds as long as it's not true. In a similar fashion the if
Skip Montanaroae5465a2010-01-14 01:14:50 +0000118# statement tests to see if we are in PyEval_EvalFrameEx().
119
120# Note: The name of the main interpreter function and the function which
121# follow it has changed over time. This version of pystack works with this
122# version of Python. If you try using it with older or newer versions of
123# the interpreter you may will have to change the functions you compare with
124# $pc.
Skip Montanaro7a92d742004-04-02 14:53:55 +0000125
Skip Montanaro74d07f22004-04-02 14:51:13 +0000126# print the entire Python call stack
127define pystack
128 while $pc < Py_Main || $pc > Py_GetArgcArgv
Skip Montanaroae5465a2010-01-14 01:14:50 +0000129 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaro74d07f22004-04-02 14:51:13 +0000130 pyframe
131 end
132 up-silently 1
133 end
134 select-frame 0
135end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000136
137# print the entire Python call stack - verbose mode
138define pystackv
139 while $pc < Py_Main || $pc > Py_GetArgcArgv
Skip Montanaroae5465a2010-01-14 01:14:50 +0000140 if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000141 pyframev
142 end
143 up-silently 1
144 end
145 select-frame 0
146end
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000147
148# generally useful macro to print a Unicode string
149def pu
150 set $uni = $arg0
151 set $i = 0
152 while (*$uni && $i++<100)
153 if (*$uni < 0x80)
154 print *(char*)$uni++
155 else
156 print /x *(short*)$uni++
157 end
158 end
159end