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