| Barry Warsaw | 39e44d7 | 2001-01-23 16:25:19 +0000 | [diff] [blame] | 1 | # -*- 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* | 
 | 18 | define pyo | 
| Barry Warsaw | bbd89b6 | 2001-01-24 04:18:13 +0000 | [diff] [blame] | 19 | print _PyObject_Dump($arg0) | 
| Barry Warsaw | 39e44d7 | 2001-01-23 16:25:19 +0000 | [diff] [blame] | 20 | end | 
 | 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* | 
 | 25 | define pyg | 
| Barry Warsaw | bbd89b6 | 2001-01-24 04:18:13 +0000 | [diff] [blame] | 26 | print _PyGC_Dump($arg0) | 
| Barry Warsaw | 39e44d7 | 2001-01-23 16:25:19 +0000 | [diff] [blame] | 27 | end | 
| Jeremy Hylton | f64ec0f | 2003-10-03 20:56:15 +0000 | [diff] [blame] | 28 |  | 
 | 29 | # If you are in an eval_frame() function, calling pyframe with no | 
 | 30 | # arguments will print the filename, function name, and line number. | 
 | 31 | # It assumes that f is the name of the current frame. | 
 | 32 | define pyframe | 
 | 33 | x/s ((PyStringObject*)f->f_code->co_filename)->ob_sval | 
 | 34 | x/s ((PyStringObject*)f->f_code->co_name)->ob_sval | 
 | 35 | p f->f_lineno | 
 | 36 | end | 
| Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 37 |  | 
 | 38 | # Here's a somewhat fragile way to print the entire Python stack from gdb. | 
 | 39 | # It's fragile because the tests for the value of $pc depend on the layout | 
 | 40 | # of specific functions in the C source code. | 
 | 41 |  | 
 | 42 | # Explanation of while and if tests: We want to pop up the stack until we | 
 | 43 | # land in Py_Main (this is probably an incorrect assumption in an embedded | 
 | 44 | # interpreter, but the test can be extended by an interested party).  If | 
 | 45 | # Py_Main <= $pc <= Py_GetArgcArv is true $pc is in Py_Main(), so the while | 
 | 46 | # tests succeeds as long as it's not true.  In a similar fashion the if | 
 | 47 | # statement tests to see if we are in eval_frame(). | 
 | 48 |  | 
 | 49 | define pystack | 
 | 50 |     while $pc < Py_Main || $pc > Py_GetArgcArgv | 
 | 51 |         if $pc > eval_frame && $pc < PyEval_EvalCodeEx | 
 | 52 |             set $__fn = PyString_AsString(co->co_filename) | 
 | 53 |             set $__n = PyString_AsString(co->co_name) | 
 | 54 |             printf "%s (%d): %s\n",  $__fn, f->f_lineno, $__n | 
 | 55 |         end | 
 | 56 |         up-silently 1 | 
 | 57 |     end | 
 | 58 |     select-frame 0 | 
 | 59 | end |