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 | |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 29 | # print the local variables of the current frame |
| 30 | define 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 |
| 44 | end |
| 45 | |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame^] | 46 | # print the current frame - verbose |
| 47 | define pyframev |
| 48 | pyframe |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 49 | pylocals |
| 50 | end |
| 51 | |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame^] | 52 | define pyframe |
| 53 | set $__fn = (char *)((PyStringObject *)co->co_filename)->ob_sval |
| 54 | set $__n = (char *)((PyStringObject *)co->co_name)->ob_sval |
| 55 | printf "%s (%d): %s\n", $__fn, f->f_lineno, $__n |
| 56 | end |
| 57 | |
Skip Montanaro | 7a92d74 | 2004-04-02 14:53:55 +0000 | [diff] [blame] | 58 | # Here's a somewhat fragile way to print the entire Python stack from gdb. |
| 59 | # It's fragile because the tests for the value of $pc depend on the layout |
| 60 | # of specific functions in the C source code. |
| 61 | |
| 62 | # Explanation of while and if tests: We want to pop up the stack until we |
| 63 | # land in Py_Main (this is probably an incorrect assumption in an embedded |
| 64 | # interpreter, but the test can be extended by an interested party). If |
| 65 | # Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while |
| 66 | # tests succeeds as long as it's not true. In a similar fashion the if |
| 67 | # statement tests to see if we are in eval_frame(). |
| 68 | |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 69 | # print the entire Python call stack |
| 70 | define pystack |
| 71 | while $pc < Py_Main || $pc > Py_GetArgcArgv |
Michael W. Hudson | 8c47f4a | 2004-08-07 20:11:22 +0000 | [diff] [blame] | 72 | if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 73 | pyframe |
| 74 | end |
| 75 | up-silently 1 |
| 76 | end |
| 77 | select-frame 0 |
| 78 | end |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame^] | 79 | |
| 80 | # print the entire Python call stack - verbose mode |
| 81 | define pystackv |
| 82 | while $pc < Py_Main || $pc > Py_GetArgcArgv |
| 83 | if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx |
| 84 | pyframev |
| 85 | end |
| 86 | up-silently 1 |
| 87 | end |
| 88 | select-frame 0 |
| 89 | end |