gdbinit 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# -*- ksh -*-
#
# If you use the GNU debugger gdb to debug the Python C runtime, you
# might find some of the following commands useful.  Copy this to your
# ~/.gdbinit file and it'll get loaded into gdb automatically when you
# start it up.  Then, at the gdb prompt you can do things like:
#
#    (gdb) pyo apyobjectptr
#    <module 'foobar' (built-in)>
#    refcounts: 1
#    address    : 84a7a2c
#    $1 = void
#    (gdb)

# Prints a representation of the object to stderr, along with the
# number of reference counts it current has and the hex address the
# object is allocated at.  The argument must be a PyObject*
define pyo
19
print _PyObject_Dump($arg0)
20 21 22 23 24 25
end

# Prints a representation of the object to stderr, along with the
# number of reference counts it current has and the hex address the
# object is allocated at.  The argument must be a PyGC_Head*
define pyg
26
print _PyGC_Dump($arg0)
27
end
28

29 30 31 32 33 34
# print the local variables of the current frame
define pylocals
    set $_i = 0
    while $_i < f->f_nlocals
	if f->f_localsplus + $_i != 0
	    set $_names = co->co_varnames
35
	    set $_name = PyUnicode_AsString(PyTuple_GetItem($_names, $_i))
36 37 38 39 40 41 42 43 44 45
	    printf "%s:\n", $_name
	    # side effect of calling _PyObject_Dump is to dump the object's
	    # info - assigning just prevents gdb from printing the
	    # NULL return value
	    set $_val = _PyObject_Dump(f->f_localsplus[$_i])
	end
        set $_i = $_i + 1
    end
end

46 47 48
# A rewrite of the Python interpreter's line number calculator in GDB's
# command language
define lineno
49
    set $__continue = 1
50 51
    set $__co = f->f_code
    set $__lasti = f->f_lasti
52
    set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
53 54 55
    set $__p = (unsigned char *)((PyStringObject *)$__co->co_lnotab)->ob_sval
    set $__li = $__co->co_firstlineno
    set $__ad = 0
56
    while ($__sz-1 >= 0 && $__continue)
57 58 59 60
      set $__sz = $__sz - 1
      set $__ad = $__ad + *$__p
      set $__p = $__p + 1
      if ($__ad > $__lasti)
61
	set $__continue = 0
62 63 64 65 66 67 68
      end
      set $__li = $__li + *$__p
      set $__p = $__p + 1
    end
    printf "%d", $__li
end

69 70 71 72 73 74
# print the current frame - verbose
define pyframev
    pyframe
    pylocals
end

75
define pyframe
76
    set $__fn = (char *)((PyStringObject *)co->co_filename)->ob_sval
77
    set $__n = PyUnicode_AsString(co->co_name)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    printf "%s (", $__fn
    lineno
    printf "): %s\n", $__n
### Uncomment these lines when using from within Emacs/XEmacs so it will
### automatically track/display the current Python source line
#    printf "%c%c%s:", 032, 032, $__fn
#    lineno
#    printf ":1\n"
end

### Use these at your own risk.  It appears that a bug in gdb causes it
### to crash in certain circumstances.

#define up
#    up-silently 1
#    printframe
#end

#define down
#    down-silently 1
#    printframe
#end

define printframe
Neil Schemenauer's avatar
Neil Schemenauer committed
102
    if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
103 104 105 106
	pyframe
    else
        frame
    end
107 108
end

109 110 111 112 113 114 115 116 117
# Here's a somewhat fragile way to print the entire Python stack from gdb.
# It's fragile because the tests for the value of $pc depend on the layout
# of specific functions in the C source code.

# Explanation of while and if tests: We want to pop up the stack until we
# land in Py_Main (this is probably an incorrect assumption in an embedded
# interpreter, but the test can be extended by an interested party).  If
# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
# tests succeeds as long as it's not true.  In a similar fashion the if
118
# statement tests to see if we are in PyEval_EvalFrame().
119

120 121 122
# print the entire Python call stack
define pystack
    while $pc < Py_Main || $pc > Py_GetArgcArgv
123
        if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
124 125 126 127 128 129
	    pyframe
        end
        up-silently 1
    end
    select-frame 0
end
130 131 132 133 134 135 136 137 138 139 140

# print the entire Python call stack - verbose mode
define pystackv
    while $pc < Py_Main || $pc > Py_GetArgcArgv
        if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
	    pyframev
        end
        up-silently 1
    end
    select-frame 0
end