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