blob: 05b2612d3204548a15f88c2167aac5dc1d585ad9 [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 Montanaro786ea6b2004-03-01 15:44:05 +000029# Here's a somewhat fragile way to print the entire Python stack from gdb.
30# It's fragile because the tests for the value of $pc depend on the layout
31# of specific functions in the C source code.
32
33# Explanation of while and if tests: We want to pop up the stack until we
34# land in Py_Main (this is probably an incorrect assumption in an embedded
35# interpreter, but the test can be extended by an interested party). If
36# Py_Main <= $pc <= Py_GetArgcArv is true $pc is in Py_Main(), so the while
37# tests succeeds as long as it's not true. In a similar fashion the if
38# statement tests to see if we are in eval_frame().
39
Skip Montanaro74d07f22004-04-02 14:51:13 +000040# print the local variables of the current frame
41define pylocals
42 set $_i = 0
43 while $_i < f->f_nlocals
44 if f->f_localsplus + $_i != 0
45 set $_names = co->co_varnames
46 set $_name = PyString_AsString(PyTuple_GetItem($_names, $_i))
47 printf "%s:\n", $_name
48 # side effect of calling _PyObject_Dump is to dump the object's
49 # info - assigning just prevents gdb from printing the
50 # NULL return value
51 set $_val = _PyObject_Dump(f->f_localsplus[$_i])
52 end
53 set $_i = $_i + 1
54 end
55end
56
57# print the current frame
58define pyframe
59 set $__fn = PyString_AsString(co->co_filename)
60 set $__n = PyString_AsString(co->co_name)
61 printf "%s (%d): %s\n", $__fn, f->f_lineno, $__n
62 pylocals
63end
64
65# print the entire Python call stack
66define pystack
67 while $pc < Py_Main || $pc > Py_GetArgcArgv
68 if $pc > eval_frame && $pc < PyEval_EvalCodeEx
69 pyframe
70 end
71 up-silently 1
72 end
73 select-frame 0
74end
75
Skip Montanaro786ea6b2004-03-01 15:44:05 +000076define pystack
77 while $pc < Py_Main || $pc > Py_GetArgcArgv
78 if $pc > eval_frame && $pc < PyEval_EvalCodeEx
79 set $__fn = PyString_AsString(co->co_filename)
80 set $__n = PyString_AsString(co->co_name)
81 printf "%s (%d): %s\n", $__fn, f->f_lineno, $__n
82 end
83 up-silently 1
84 end
85 select-frame 0
86end