在不需要的系统调用上引发异常



我被告知要修复遗留应用程序中的错误。

我可以重现一个错误,但我不知道错误是在哪个 python 源代码行执行的。

我可以看到strace的相关故障:一个文件被打开,不应该被打开。

我想让相关的 open() linux-syscall 在 python 解释器中引发异常。我的目标:我希望看到堆栈跟踪以便能够修复错误。

这样,我可以避免使用调试器耗时地单步执行大量行。

换句话说也是如此:如果系统调用被执行,这会导致open("/somefile", O_RDONLY) = 4python 解释器应该通过回溯退出的 strace 输出。

有人有解决方案吗?

如果您不明白我在寻找什么,请发表评论。

我们可以在导入模块之前对open做一个补丁,这里有一个例子:

test.py

def func():
with open('test', 'w') as f:
pass

test2.py

try:
import __builtin__ # for python2
except ImportError:
import builtins as __builtin__ #for python3
import copy
import traceback
orig_open = copy.copy(__builtin__.open)
def myopen(*args):
traceback.print_stack()
return orig_open(*args)
__builtin__.open = myopen
from test import func # Note that we import the module after patching on open()
func()

func()test2.py中被调用时,调用堆栈将被打印出来:

$ python test2.py 
File "test2.py", line 19, in <module>
func()
File "/tmp/test.py", line 4, in func
with open('test', 'w') as f:
File "test2.py", line 12, in myopen
traceback.print_stack()

你可以在 gdb 下运行 python,在open()syscall 上设置一个(条件)断点(或者更确切地说,libc 中的存根函数通过它调用它),当遇到断点时,向 python 进程发送一个SIGINT信号并让它继续,然后 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)

您可以使用strace命令行工具-e inject

man strace

-e inject=syscall_set
Perform syscall tampering for the specified set of syscalls.  
The syntax of the syscall_set specification
is the same as in the -e trace option.
At least one of error, retval, signal, delay_enter, 
or delay_exit options has to be specified.  error and
retval are mutually exclusive.
If :error=errno option is specified, a fault is injected
into a syscall invocation: the syscall number is
replaced by -1 which corresponds to an invalid syscall
(unless a syscall is specified with :syscall=  option), 
and the error code is specified using a symbolic errno 
value like ENOSYS or a numeric value within
1..4095 range.

最新更新