gdbinit 4.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# 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)
12 13 14
#
# NOTE: If you have gdb 7 or later, it supports debugging of Python directly
# with embedded macros that you may find superior to what is in here.
15
# See Tools/gdb/libpython.py and http://bugs.python.org/issue8032.
16

17 18 19 20 21
document pyo
  Prints a representation of the object to stderr, along with the
  number of reference counts it currently has and the hex address the
  object is allocated at.  The argument must be a PyObject*
end
22
define pyo
23 24 25 26
    # side effect of calling _PyObject_Dump is to dump the object's
    # info - assigning just prevents gdb from printing the
    # NULL return value
    set $_unused_void = _PyObject_Dump($arg0)
27 28
end

29 30 31 32 33
document pyg
  Prints a representation of the object to stderr, along with the
  number of reference counts it currently has and the hex address the
  object is allocated at.  The argument must be a PyGC_Head*
end
34
define pyg
35
    print _PyGC_Dump($arg0)
36
end
37

38 39 40
document pylocals
  Print the local variables of the current frame.
end
41 42
define pylocals
    set $_i = 0
43
    while $_i < f->f_code->co_nlocals
44
	if f->f_localsplus + $_i != 0
45 46
	    set $_names = f->f_code->co_varnames
	    set $_name = PyUnicode_AsUTF8(PyTuple_GetItem($_names, $_i))
47
	    printf "%s:\n", $_name
48
            pyo f->f_localsplus[$_i]
49 50 51 52 53
	end
        set $_i = $_i + 1
    end
end

54 55 56
# A rewrite of the Python interpreter's line number calculator in GDB's
# command language
define lineno
57
    set $__continue = 1
58 59
    set $__co = f->f_code
    set $__lasti = f->f_lasti
60
    set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
61
    set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
62 63
    set $__li = $__co->co_firstlineno
    set $__ad = 0
64
    while ($__sz-1 >= 0 && $__continue)
65 66 67 68
      set $__sz = $__sz - 1
      set $__ad = $__ad + *$__p
      set $__p = $__p + 1
      if ($__ad > $__lasti)
69
	set $__continue = 0
70 71 72
      else
        set $__li = $__li + *$__p
        set $__p = $__p + 1
73 74
      end
    end
75
    printf "%d", $__li
76 77
end

78 79 80
document pyframev
  Print the current frame - verbose
end
81 82 83 84 85
define pyframev
    pyframe
    pylocals
end

86
define pyframe
87 88
    set $__fn = PyUnicode_AsUTF8(f->f_code->co_filename)
    set $__n = PyUnicode_AsUTF8(f->f_code->co_name)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    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
113
    if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
114 115 116 117
	pyframe
    else
        frame
    end
118 119
end

120 121 122 123 124 125 126 127 128
# 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
129 130 131 132 133 134 135
# statement tests to see if we are in PyEval_EvalFrameEx().

# Note: The name of the main interpreter function and the function which
# follow it has changed over time.  This version of pystack works with this
# version of Python.  If you try using it with older or newer versions of
# the interpreter you may will have to change the functions you compare with
# $pc.
136

137 138 139
document pystack
  Print the entire Python call stack
end
140 141
define pystack
    while $pc < Py_Main || $pc > Py_GetArgcArgv
142
        if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
143 144 145 146 147 148
	    pyframe
        end
        up-silently 1
    end
    select-frame 0
end
149

150 151 152
document pystackv
  Print the entire Python call stack - verbose mode
end
153 154
define pystackv
    while $pc < Py_Main || $pc > Py_GetArgcArgv
155
        if $pc > PyEval_EvalFrameEx && $pc < _PyEval_EvalFrameDefault
156 157 158 159 160 161
	    pyframev
        end
        up-silently 1
    end
    select-frame 0
end
Benjamin Peterson's avatar
Benjamin Peterson committed
162

163 164 165
document pu
  Generally useful macro to print a Unicode string
end
Benjamin Peterson's avatar
Benjamin Peterson committed
166
def pu
167
  set $uni = $arg0
Benjamin Peterson's avatar
Benjamin Peterson committed
168 169
  set $i = 0
  while (*$uni && $i++<100)
170
    if (*$uni < 0x80)
Benjamin Peterson's avatar
Benjamin Peterson committed
171 172 173 174 175 176
      print *(char*)$uni++
    else
      print /x *(short*)$uni++
    end
  end
end