我被告知要修复旧版应用程序中的错误。
我可以重现一个错误,但是我不知道该错误会在哪个python源代码行执行。
我可以看到有关的故障strace
:文件被打开,但不应打开。
我想使相关的open()linux-syscall在python解释器中引发一个异常。我的目标:我希望看到stacktrace能够修复该错误。
这样,我可以避免在调试器中花很多时间来遍历许多行。
换句话说:如果执行了syscall,这会导致open("/somefile", O_RDONLY) = 4
python解释器的strace输出应以traceback退出。
有没有人解决?
如果您不明白我在寻找什么,请发表评论。
您可以在gdb下运行python,在open()
syscall上设置一个(有条件的)断点(或者,通过它调用libc中的存根函数),并在遇到断点时SIGINT
向python进程发送信号,并让它继续,随后应使用所需的堆栈跟踪中断python脚本的执行。
下面的shell脚本可自动执行该过程。
用法:
stack_trace_on_open
filename
-- python
script.py
[
script args
]
stack_trace_on_open:
#!/usr/bin/env bash
myname="$(basename "$0")"
if [[ $# -lt 4 || "$2" != '--' ]]
then
echo >&2 "Usage: $myname <filename> -- python <script.py> [script args ...]"
exit 1
fi
fname=$1
python_exe="$3"
shift 3
gdb -q "$python_exe" <<END
set breakpoint pending on
break open
condition 1 strcmp(\$rdi,"$fname") == 0
run "$@"
signal 2
cont
quit
END
示范:
$ cat test.py
import ctypes
clib = ctypes.CDLL(None)
fd = clib.open("/dev/urandom", 0)
clib.close(fd)
$ ./stack_trace_on_open /dev/urandom -- python test.py
Reading symbols from python...(no debugging symbols found)...done.
(gdb) (gdb) Function "open" not defined.
Breakpoint 1 (open) pending.
(gdb) (gdb) Starting program: /usr/bin/python "test.py"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, open64 () at ../sysdeps/unix/syscall-template.S:84
84 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) Continuing with signal SIGINT.
Breakpoint 1, open64 () at ../sysdeps/unix/syscall-template.S:84
84 in ../sysdeps/unix/syscall-template.S
(gdb) Continuing.
Traceback (most recent call last): # <--------
File "test.py", line 4, in <module> # <--------
fd = clib.open("/dev/urandom", 0) # <--------
KeyboardInterrupt
[Inferior 1 (process 14248) exited with code 01]
(gdb)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句