| 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 | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 46 | # A rewrite of the Python interpreter's line number calculator in GDB's | 
 | 47 | # command language | 
 | 48 | define lineno | 
| Neal Norwitz | 4655e44 | 2005-09-05 16:16:49 +0000 | [diff] [blame] | 49 |     set $__continue = 1 | 
| Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 50 |     set $__co = f->f_code | 
 | 51 |     set $__lasti = f->f_lasti | 
 | 52 |     set $__sz = ((PyStringObject *)$__co->co_lnotab)->ob_size/2 | 
 | 53 |     set $__p = (unsigned char *)((PyStringObject *)$__co->co_lnotab)->ob_sval | 
 | 54 |     set $__li = $__co->co_firstlineno | 
 | 55 |     set $__ad = 0 | 
| Neal Norwitz | 4655e44 | 2005-09-05 16:16:49 +0000 | [diff] [blame] | 56 |     while ($__sz-1 >= 0 && $__continue) | 
| Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 57 |       set $__sz = $__sz - 1 | 
 | 58 |       set $__ad = $__ad + *$__p | 
 | 59 |       set $__p = $__p + 1 | 
 | 60 |       if ($__ad > $__lasti) | 
| Neal Norwitz | 4655e44 | 2005-09-05 16:16:49 +0000 | [diff] [blame] | 61 | 	set $__continue = 0 | 
| Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 62 |       end | 
 | 63 |       set $__li = $__li + *$__p | 
 | 64 |       set $__p = $__p + 1 | 
 | 65 |     end | 
 | 66 |     printf "%d", $__li | 
 | 67 | end | 
 | 68 |  | 
| Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 69 | # print the current frame - verbose | 
 | 70 | define pyframev | 
 | 71 |     pyframe | 
| Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 72 |     pylocals | 
 | 73 | end | 
 | 74 |  | 
| Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 75 | define pyframe | 
 | 76 |     set $__fn = (char *)((PyStringObject *)co->co_filename)->ob_sval | 
 | 77 |     set $__n = (char *)((PyStringObject *)co->co_name)->ob_sval | 
| Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 78 |     printf "%s (", $__fn | 
 | 79 |     lineno | 
 | 80 |     printf "): %s\n", $__n | 
 | 81 | ### Uncomment these lines when using from within Emacs/XEmacs so it will | 
 | 82 | ### automatically track/display the current Python source line | 
 | 83 | #    printf "%c%c%s:", 032, 032, $__fn | 
 | 84 | #    lineno | 
 | 85 | #    printf ":1\n" | 
 | 86 | end | 
 | 87 |  | 
 | 88 | ### Use these at your own risk.  It appears that a bug in gdb causes it | 
 | 89 | ### to crash in certain circumstances. | 
 | 90 |  | 
 | 91 | #define up | 
 | 92 | #    up-silently 1 | 
 | 93 | #    printframe | 
 | 94 | #end | 
 | 95 |  | 
 | 96 | #define down | 
 | 97 | #    down-silently 1 | 
 | 98 | #    printframe | 
 | 99 | #end | 
 | 100 |  | 
 | 101 | define printframe | 
| Neil Schemenauer | f98e6b1 | 2005-08-13 00:28:41 +0000 | [diff] [blame] | 102 |     if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx | 
| Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 103 | 	pyframe | 
 | 104 |     else | 
 | 105 |         frame | 
 | 106 |     end | 
| Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 107 | end | 
 | 108 |  | 
| Skip Montanaro | 7a92d74 | 2004-04-02 14:53:55 +0000 | [diff] [blame] | 109 | # Here's a somewhat fragile way to print the entire Python stack from gdb. | 
 | 110 | # It's fragile because the tests for the value of $pc depend on the layout | 
 | 111 | # of specific functions in the C source code. | 
 | 112 |  | 
 | 113 | # Explanation of while and if tests: We want to pop up the stack until we | 
 | 114 | # land in Py_Main (this is probably an incorrect assumption in an embedded | 
 | 115 | # interpreter, but the test can be extended by an interested party).  If | 
 | 116 | # Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while | 
 | 117 | # tests succeeds as long as it's not true.  In a similar fashion the if | 
| Skip Montanaro | afd77d9 | 2005-01-08 21:56:43 +0000 | [diff] [blame] | 118 | # statement tests to see if we are in PyEval_EvalFrame(). | 
| Skip Montanaro | 7a92d74 | 2004-04-02 14:53:55 +0000 | [diff] [blame] | 119 |  | 
| Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 120 | # print the entire Python call stack | 
 | 121 | define pystack | 
 | 122 |     while $pc < Py_Main || $pc > Py_GetArgcArgv | 
| Michael W. Hudson | 8c47f4a | 2004-08-07 20:11:22 +0000 | [diff] [blame] | 123 |         if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx | 
| Skip Montanaro | 74d07f2 | 2004-04-02 14:51:13 +0000 | [diff] [blame] | 124 | 	    pyframe | 
 | 125 |         end | 
 | 126 |         up-silently 1 | 
 | 127 |     end | 
 | 128 |     select-frame 0 | 
 | 129 | end | 
| Skip Montanaro | 0bb2a65 | 2004-11-17 16:04:15 +0000 | [diff] [blame] | 130 |  | 
 | 131 | # print the entire Python call stack - verbose mode | 
 | 132 | define pystackv | 
 | 133 |     while $pc < Py_Main || $pc > Py_GetArgcArgv | 
 | 134 |         if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx | 
 | 135 | 	    pyframev | 
 | 136 |         end | 
 | 137 |         up-silently 1 | 
 | 138 |     end | 
 | 139 |     select-frame 0 | 
 | 140 | end |