Barry Warsaw | 39e44d7 | 2001-01-23 16:25:19 +0000 | [diff] [blame] | 1 | # If you use the GNU debugger gdb to debug the Python C runtime, you |
| 2 | # might find some of the following commands useful. Copy this to your |
| 3 | # ~/.gdbinit file and it'll get loaded into gdb automatically when you |
| 4 | # start it up. Then, at the gdb prompt you can do things like: |
| 5 | # |
| 6 | # (gdb) pyo apyobjectptr |
| 7 | # <module 'foobar' (built-in)> |
| 8 | # refcounts: 1 |
| 9 | # address : 84a7a2c |
| 10 | # $1 = void |
| 11 | # (gdb) |
Gregory P. Smith | 03efcf2 | 2010-10-17 18:38:04 +0000 | [diff] [blame] | 12 | # |
| 13 | # NOTE: If you have gdb 7 or later, it supports debugging of Python directly |
| 14 | # with embedded macros that you may find superior to what is in here. |
Gregory P. Smith | 3ebc22a | 2010-10-17 19:40:59 +0000 | [diff] [blame] | 15 | # See Tools/gdb/libpython.py and http://bugs.python.org/issue8032. |
Gregory P. Smith | 03efcf2 | 2010-10-17 18:38:04 +0000 | [diff] [blame] | 16 | |
Barry Warsaw | 39e44d7 | 2001-01-23 16:25:19 +0000 | [diff] [blame] | 17 | # Prints a representation of the object to stderr, along with the |
| 18 | # number of reference counts it current has and the hex address the |
| 19 | # object is allocated at. The argument must be a PyObject* |
| 20 | define pyo |
Gregory P. Smith | 03efcf2 | 2010-10-17 18:38:04 +0000 | [diff] [blame] | 21 | # side effect of calling _PyObject_Dump is to dump the object's |
| 22 | # info - assigning just prevents gdb from printing the |
| 23 | # NULL return value |
| 24 | set $_unused_void = _PyObject_Dump($arg0) |
Barry Warsaw | 39e44d7 | 2001-01-23 16:25:19 +0000 | [diff] [blame] | 25 | end |
| 26 | |
| 27 | # Prints a representation of the object to stderr, along with the |
| 28 | # number of reference counts it current has and the hex address the |
| 29 | # object is allocated at. The argument must be a PyGC_Head* |
| 30 | define pyg |
Gregory P. Smith | 03efcf2 | 2010-10-17 18:38:04 +0000 | [diff] [blame] | 31 | print _PyGC_Dump($arg0) |
Barry Warsaw | 39e44d7 | 2001-01-23 16:25:19 +0000 | [diff] [blame] | 32 | end |
Jeremy Hylton | f64ec0f | 2003-10-03 20:56:15 +0000 | [diff] [blame] | 33 | |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 34 | # print the local variables of the current frame |
| 35 | define pylocals |
| 36 | set $_i = 0 |
Georg Brandl | 9b21dbc | 2009-07-23 09:19:09 +0000 | [diff] [blame] | 37 | while $_i < f->f_code->co_nlocals |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 38 | if f->f_localsplus + $_i != 0 |
| 39 | set $_names = co->co_varnames |
Neal Norwitz | 8f2f22a | 2008-08-24 20:59:23 +0000 | [diff] [blame] | 40 | set $_name = _PyUnicode_AsString(PyTuple_GetItem($_names, $_i)) |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 41 | printf "%s:\n", $_name |
Gregory P. Smith | 03efcf2 | 2010-10-17 18:38:04 +0000 | [diff] [blame] | 42 | pyo f->f_localsplus[$_i] |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 43 | end |
| 44 | set $_i = $_i + 1 |
| 45 | end |
| 46 | end |
| 47 | |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 48 | # A rewrite of the Python interpreter's line number calculator in GDB's |
| 49 | # command language |
| 50 | define lineno |
Neal Norwitz | 4655e44 | 2005-09-05 16:16:49 +0000 | [diff] [blame] | 51 | set $__continue = 1 |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 52 | set $__co = f->f_code |
| 53 | set $__lasti = f->f_lasti |
Neal Norwitz | 44c19f6 | 2007-08-27 02:49:29 +0000 | [diff] [blame] | 54 | set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2 |
Neal Norwitz | 8f2f22a | 2008-08-24 20:59:23 +0000 | [diff] [blame] | 55 | set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 56 | set $__li = $__co->co_firstlineno |
| 57 | set $__ad = 0 |
Neal Norwitz | 4655e44 | 2005-09-05 16:16:49 +0000 | [diff] [blame] | 58 | while ($__sz-1 >= 0 && $__continue) |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 59 | set $__sz = $__sz - 1 |
| 60 | set $__ad = $__ad + *$__p |
| 61 | set $__p = $__p + 1 |
| 62 | if ($__ad > $__lasti) |
Neal Norwitz | 4655e44 | 2005-09-05 16:16:49 +0000 | [diff] [blame] | 63 | set $__continue = 0 |
Georg Brandl | 29848f0 | 2010-10-21 12:59:14 +0000 | [diff] [blame] | 64 | else |
| 65 | set $__li = $__li + *$__p |
| 66 | set $__p = $__p + 1 |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 67 | end |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 68 | end |
Georg Brandl | 0a9397f | 2010-11-03 07:41:00 +0000 | [diff] [blame] | 69 | printf "%d", $__li |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 70 | end |
| 71 | |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 72 | # print the current frame - verbose |
| 73 | define pyframev |
| 74 | pyframe |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 75 | pylocals |
| 76 | end |
| 77 | |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 78 | define pyframe |
Neal Norwitz | 8f2f22a | 2008-08-24 20:59:23 +0000 | [diff] [blame] | 79 | set $__fn = _PyUnicode_AsString(co->co_filename) |
| 80 | set $__n = _PyUnicode_AsString(co->co_name) |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 81 | printf "%s (", $__fn |
| 82 | lineno |
| 83 | printf "): %s\n", $__n |
| 84 | ### Uncomment these lines when using from within Emacs/XEmacs so it will |
| 85 | ### automatically track/display the current Python source line |
| 86 | # printf "%c%c%s:", 032, 032, $__fn |
| 87 | # lineno |
| 88 | # printf ":1\n" |
| 89 | end |
| 90 | |
| 91 | ### Use these at your own risk. It appears that a bug in gdb causes it |
| 92 | ### to crash in certain circumstances. |
| 93 | |
| 94 | #define up |
| 95 | # up-silently 1 |
| 96 | # printframe |
| 97 | #end |
| 98 | |
| 99 | #define down |
| 100 | # down-silently 1 |
| 101 | # printframe |
| 102 | #end |
| 103 | |
| 104 | define printframe |
Neil Schemenauer | f98e6b1 | 2005-08-13 00:28:41 +0000 | [diff] [blame] | 105 | if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx |
Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 106 | pyframe |
| 107 | else |
| 108 | frame |
| 109 | end |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 110 | end |
| 111 | |
Skip Montanaro | 7a92d74 | 2004-04-02 14:53:55 +0000 | [diff] [blame] | 112 | # Here's a somewhat fragile way to print the entire Python stack from gdb. |
| 113 | # It's fragile because the tests for the value of $pc depend on the layout |
| 114 | # of specific functions in the C source code. |
| 115 | |
| 116 | # Explanation of while and if tests: We want to pop up the stack until we |
| 117 | # land in Py_Main (this is probably an incorrect assumption in an embedded |
| 118 | # interpreter, but the test can be extended by an interested party). If |
| 119 | # Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while |
| 120 | # tests succeeds as long as it's not true. In a similar fashion the if |
Skip Montanaro | ae5465a | 2010-01-14 01:14:50 +0000 | [diff] [blame] | 121 | # statement tests to see if we are in PyEval_EvalFrameEx(). |
| 122 | |
| 123 | # Note: The name of the main interpreter function and the function which |
| 124 | # follow it has changed over time. This version of pystack works with this |
| 125 | # version of Python. If you try using it with older or newer versions of |
| 126 | # the interpreter you may will have to change the functions you compare with |
| 127 | # $pc. |
Skip Montanaro | 7a92d74 | 2004-04-02 14:53:55 +0000 | [diff] [blame] | 128 | |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 129 | # print the entire Python call stack |
| 130 | define pystack |
| 131 | while $pc < Py_Main || $pc > Py_GetArgcArgv |
Skip Montanaro | ae5465a | 2010-01-14 01:14:50 +0000 | [diff] [blame] | 132 | if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx |
Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 133 | pyframe |
| 134 | end |
| 135 | up-silently 1 |
| 136 | end |
| 137 | select-frame 0 |
| 138 | end |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 139 | |
| 140 | # print the entire Python call stack - verbose mode |
| 141 | define pystackv |
| 142 | while $pc < Py_Main || $pc > Py_GetArgcArgv |
Skip Montanaro | ae5465a | 2010-01-14 01:14:50 +0000 | [diff] [blame] | 143 | if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx |
Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 144 | pyframev |
| 145 | end |
| 146 | up-silently 1 |
| 147 | end |
| 148 | select-frame 0 |
| 149 | end |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 150 | |
| 151 | # generally useful macro to print a Unicode string |
| 152 | def pu |
| 153 | set $uni = $arg0 |
| 154 | set $i = 0 |
| 155 | while (*$uni && $i++<100) |
| 156 | if (*$uni < 0x80) |
| 157 | print *(char*)$uni++ |
| 158 | else |
| 159 | print /x *(short*)$uni++ |
| 160 | end |
| 161 | end |
| 162 | end |