blob: 42998c9cb927f8e77c42ae9fa0bd62f6c0832d04 [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
32 while $_i < f->f_nlocals
33 if f->f_localsplus + $_i != 0
34 set $_names = co->co_varnames
35 set $_name = PyString_AsString(PyTuple_GetItem($_names, $_i))
36 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
49 set $__co = f->f_code
50 set $__lasti = f->f_lasti
51 set $__sz = ((PyStringObject *)$__co->co_lnotab)->ob_size/2
52 set $__p = (unsigned char *)((PyStringObject *)$__co->co_lnotab)->ob_sval
53 set $__li = $__co->co_firstlineno
54 set $__ad = 0
55 while ($__sz-1 >= 0)
56 set $__sz = $__sz - 1
57 set $__ad = $__ad + *$__p
58 set $__p = $__p + 1
59 if ($__ad > $__lasti)
60 break
61 end
62 set $__li = $__li + *$__p
63 set $__p = $__p + 1
64 end
65 printf "%d", $__li
66end
67
Skip Montanaro0bb2a652004-11-17 16:04:15 +000068# print the current frame - verbose
69define pyframev
70 pyframe
Skip Montanaro74d07f22004-04-02 14:51:13 +000071 pylocals
72end
73
Skip Montanaro0bb2a652004-11-17 16:04:15 +000074define pyframe
75 set $__fn = (char *)((PyStringObject *)co->co_filename)->ob_sval
76 set $__n = (char *)((PyStringObject *)co->co_name)->ob_sval
Skip Montanaroafd77d92005-01-08 21:56:43 +000077 printf "%s (", $__fn
78 lineno
79 printf "): %s\n", $__n
80### Uncomment these lines when using from within Emacs/XEmacs so it will
81### automatically track/display the current Python source line
82# printf "%c%c%s:", 032, 032, $__fn
83# lineno
84# printf ":1\n"
85end
86
87### Use these at your own risk. It appears that a bug in gdb causes it
88### to crash in certain circumstances.
89
90#define up
91# up-silently 1
92# printframe
93#end
94
95#define down
96# down-silently 1
97# printframe
98#end
99
100define printframe
101 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
102 pyframe
103 else
104 frame
105 end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000106end
107
Skip Montanaro7a92d742004-04-02 14:53:55 +0000108# Here's a somewhat fragile way to print the entire Python stack from gdb.
109# It's fragile because the tests for the value of $pc depend on the layout
110# of specific functions in the C source code.
111
112# Explanation of while and if tests: We want to pop up the stack until we
113# land in Py_Main (this is probably an incorrect assumption in an embedded
114# interpreter, but the test can be extended by an interested party). If
115# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
116# tests succeeds as long as it's not true. In a similar fashion the if
Skip Montanaroafd77d92005-01-08 21:56:43 +0000117# statement tests to see if we are in PyEval_EvalFrame().
Skip Montanaro7a92d742004-04-02 14:53:55 +0000118
Skip Montanaro74d07f22004-04-02 14:51:13 +0000119# print the entire Python call stack
120define pystack
121 while $pc < Py_Main || $pc > Py_GetArgcArgv
Michael W. Hudson8c47f4a2004-08-07 20:11:22 +0000122 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
Skip Montanaro74d07f22004-04-02 14:51:13 +0000123 pyframe
124 end
125 up-silently 1
126 end
127 select-frame 0
128end
Skip Montanaro0bb2a652004-11-17 16:04:15 +0000129
130# print the entire Python call stack - verbose mode
131define pystackv
132 while $pc < Py_Main || $pc > Py_GetArgcArgv
133 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
134 pyframev
135 end
136 up-silently 1
137 end
138 select-frame 0
139end