Skip to content

Commit 786ea6b

Browse files
author
Skip Montanaro
committed
Add pystack definition to Misc/gdbinit with some explanation of its behavior
and add flag comments to ceval.c and main.c alerting people to the coupling between pystack and the layout of those files.
1 parent 87f1013 commit 786ea6b

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎Misc/gdbinit‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,26 @@ x/s ((PyStringObject*)f->f_code->co_filename)->ob_sval
3434
x/s ((PyStringObject*)f->f_code->co_name)->ob_sval
3535
p f->f_lineno
3636
end
37+
38+
# Here's a somewhat fragile way to print the entire Python stack from gdb.
39+
# It's fragile because the tests for the value of $pc depend on the layout
40+
# of specific functions in the C source code.
41+
42+
# Explanation of while and if tests: We want to pop up the stack until we
43+
# land in Py_Main (this is probably an incorrect assumption in an embedded
44+
# interpreter, but the test can be extended by an interested party). If
45+
# Py_Main <= $pc <= Py_GetArgcArv is true $pc is in Py_Main(), so the while
46+
# tests succeeds as long as it's not true. In a similar fashion the if
47+
# statement tests to see if we are in eval_frame().
48+
49+
define pystack
50+
while $pc < Py_Main || $pc > Py_GetArgcArgv
51+
if $pc > eval_frame && $pc < PyEval_EvalCodeEx
52+
set $__fn = PyString_AsString(co->co_filename)
53+
set $__n = PyString_AsString(co->co_name)
54+
printf "%s (%d): %s\n", $__fn, f->f_lineno, $__n
55+
end
56+
up-silently 1
57+
end
58+
select-frame 0
59+
end

‎Modules/main.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ Py_Main(int argc, char **argv)
452452
return sts;
453453
}
454454

455+
/* this is gonna seem *real weird*, but if you put some other code between
456+
Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
457+
while statement in Misc/gdbinit:ppystack */
455458

456459
/* Make the *original* argc/argv available to other modules.
457460
This is rare, but it is needed by the secureware extension. */

‎Python/ceval.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,10 @@ eval_frame(PyFrameObject *f)
24452445
return retval;
24462446
}
24472447

2448+
/* this is gonna seem *real weird*, but if you put some other code between
2449+
eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2450+
the if statement in Misc/gdbinit:ppystack */
2451+
24482452
PyObject *
24492453
PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
24502454
PyObject **args, int argcount, PyObject **kws, int kwcount,

0 commit comments

Comments
 (0)