blob: 145cc3a31219c7ecdc2e3b87299f2d09d84c8c62 [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
46# print the current frame
47define pyframe
48 set $__fn = PyString_AsString(co->co_filename)
49 set $__n = PyString_AsString(co->co_name)
50 printf "%s (%d): %s\n", $__fn, f->f_lineno, $__n
51 pylocals
52end
53
Skip Montanaro7a92d742004-04-02 14:53:55 +000054# Here's a somewhat fragile way to print the entire Python stack from gdb.
55# It's fragile because the tests for the value of $pc depend on the layout
56# of specific functions in the C source code.
57
58# Explanation of while and if tests: We want to pop up the stack until we
59# land in Py_Main (this is probably an incorrect assumption in an embedded
60# interpreter, but the test can be extended by an interested party). If
61# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
62# tests succeeds as long as it's not true. In a similar fashion the if
63# statement tests to see if we are in eval_frame().
64
Skip Montanaro74d07f22004-04-02 14:51:13 +000065# print the entire Python call stack
66define pystack
67 while $pc < Py_Main || $pc > Py_GetArgcArgv
Michael W. Hudson8c47f4a2004-08-07 20:11:22 +000068 if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
Skip Montanaro74d07f22004-04-02 14:51:13 +000069 pyframe
70 end
71 up-silently 1
72 end
73 select-frame 0
74end