只支持python3,并且编译Python时要去掉优化,在Makefile中把-O3改成-O0这样PyEval_EvalFrameEx的参数就不会被优化.
在~/.gdbinit加入下面代码
define pyframeargs
set $_f = (PyFrameObject *)$arg0
printf ">file name :%s\n", (char *)(&((PyUnicodeObject*)$_f->f_code->co_filename)->_base->_base + 1)
printf ">function:\":%s\"\nline:%d\n", (char *)(&((PyUnicodeObject*)$_f->f_code->co_name)->_base->_base + 1), $_f->f_lineno
set $_i = 0
printf ">function args:\n"
set $_vars = (PyCodeObject *)$_f->f_code->co_varnames
set $_values = $_f->f_valuestack
while $_i < $_f->f_code->co_argcount
set $_n= *((*(PyTupleObject *)$_vars)->ob_item + $_i)
set $_v= *($_values + $_i)
printf ">%s:", (char *)(&((PyUnicodeObject *)$_n)->_base->_base + 1)
printf "0x%x", $_v
printf "\n"
set $_i = $_i + 1
end
end
define pytype
p *(*(PyObject *)$arg0)->ob_type
end
define pystring
p (char *)(&((PyUnicodeObject*)$arg0)->_base->_base + 1)
end
define pyint
p (*(PyIntObject *)$arg0)->ob_ival
end
下面的是在切换到具体的frame 后,直接输入执行就可以。f 这个变量在有些frame 是带有的。
#打印文件名称
x/s (char *)(&((PyUnicodeObject*)f->f_filename->co_name)->_base->_base + 1)
#打印文件函数名称
x/s (char *)(&((PyUnicodeObject*)f->f_code->co_name)->_base->_base + 1)
#函数行号
p (*(PyFrameObject*)f)->f_lineno
#当前堆栈指向的代码
p *(PyCodeObject *)(*(PyFrameObject*)f)->f_code
#本地变量包括参数(PyTupleObject)
p *(PyCodeObject *)f->f_code->co_varnames
#函数参数个数
p f->f_code->co_argcount
#堆栈值
p *f->f_valuestack
下面是一个查看运行中python程序的底层堆栈
#!/bin/sh
if test $# -ne 1; then
echo "Usage: `basename $0 .sh` <process-id>" 1>&2
exit 1
fi
if test ! -r /proc/$1; then
echo "Process $1 not found." 1>&2
exit 1
fi
# GDB doesn't allow "thread apply all bt" when the process isn't
# threaded; need to peek at the process to determine if that or the
# simpler "bt" should be used.
backtrace="bt"
if test -d /proc/$1/task ; then
# Newer kernel; has a task/ directory.
if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
backtrace="thread apply all bt"
fi
elif test -f /proc/$1/maps ; then
# Older kernel; go by it loading libpthread.
if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
backtrace="thread apply all bt"
fi
fi
GDB=${GDB:-/usr/bin/gdb}
# Run GDB, strip out unwanted noise.
$GDB -p $1 <<EOF 2>&1 |
set width 0
set height 0
set pagination no
printpyframeargs 0
printpyframeargs 1
printpyframeargs 2
printpyframeargs 3
printpyframeargs 4
printpyframeargs 5
printpyframeargs 6
printpyframeargs 7
printpyframeargs 8
printpyframeargs 9
printpyframeargs 10
printpyframeargs 11
printpyframeargs 12
printpyframeargs 13
printpyframeargs 14
printpyframeargs 15
EOF
/bin/sed -n \
-e 's/^\((gdb) \)*//' \
-e '/^>/p' \
-e '/^#/p' \
-e '/^Thread/p'