Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afd77d9

Browse files
author
Skip Montanaro
committed
Add definitions for "up" and "down" commands that print/display the current
Python file/line when the current C execution frame is inside PyEval_EvalFrame. These are commented out by default because GDB sometimes crashes as a result (seems like a GDB bug). Add a pyframe command that displays the current Python stack frame. If the marked lines are uncommented, it will also cause Emacs/XEmacs to display the current file/line.
1 parent 9ddb300 commit afd77d9

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

Misc/gdbinit

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ define pylocals
4343
end
4444
end
4545

46+
# A rewrite of the Python interpreter's line number calculator in GDB's
47+
# command language
48+
define lineno
49+
set $__co = f->f_code
50+
set $__lasti = f->f_lasti
51+
set $__sz = ((PyStringObject *)$__co->co_lnotab)->ob_size/2
52+
set $__p = (unsigned char *)((PyStringObject *)$__co->co_lnotab)->ob_sval
53+
set $__li = $__co->co_firstlineno
54+
set $__ad = 0
55+
while ($__sz-1 >= 0)
56+
set $__sz = $__sz - 1
57+
set $__ad = $__ad + *$__p
58+
set $__p = $__p + 1
59+
if ($__ad > $__lasti)
60+
break
61+
end
62+
set $__li = $__li + *$__p
63+
set $__p = $__p + 1
64+
end
65+
printf "%d", $__li
66+
end
67+
4668
# print the current frame - verbose
4769
define pyframev
4870
pyframe
@@ -52,7 +74,35 @@ end
5274
define pyframe
5375
set $__fn = (char *)((PyStringObject *)co->co_filename)->ob_sval
5476
set $__n = (char *)((PyStringObject *)co->co_name)->ob_sval
55-
printf "%s (%d): %s\n", $__fn, f->f_lineno, $__n
77+
printf "%s (", $__fn
78+
lineno
79+
printf "): %s\n", $__n
80+
### Uncomment these lines when using from within Emacs/XEmacs so it will
81+
### automatically track/display the current Python source line
82+
# printf "%c%c%s:", 032, 032, $__fn
83+
# lineno
84+
# printf ":1\n"
85+
end
86+
87+
### Use these at your own risk. It appears that a bug in gdb causes it
88+
### to crash in certain circumstances.
89+
90+
#define up
91+
# up-silently 1
92+
# printframe
93+
#end
94+
95+
#define down
96+
# down-silently 1
97+
# printframe
98+
#end
99+
100+
define printframe
101+
if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
102+
pyframe
103+
else
104+
frame
105+
end
56106
end
57107

58108
# Here's a somewhat fragile way to print the entire Python stack from gdb.
@@ -64,7 +114,7 @@ end
64114
# interpreter, but the test can be extended by an interested party). If
65115
# Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
66116
# tests succeeds as long as it's not true. In a similar fashion the if
67-
# statement tests to see if we are in eval_frame().
117+
# statement tests to see if we are in PyEval_EvalFrame().
68118

69119
# print the entire Python call stack
70120
define pystack

0 commit comments

Comments
 (0)