我正在尝试使用 python 编写 GDB 脚本。我有一个 GDB 的 native 脚本文件,它提供一个 python 脚本文件。在 .gdb 文件中,我在不同的函数上声明了一些断点。我能够使用 python 脚本在这些断点上执行下一步/步骤/继续并打印不同的变量。但是我有一个独特的 python 函数,用于每个带有特定打印的断点。我想让它变得更好、更通用。
我想要的是在 python 代码中有一个函数和一种识别哪个断点被击中的方法,这样我就可以根据断点打印不同的变量。如果我只是打印它们,那么我将得到超出范围的错误。
我已经检查过,GDB 还允许通过在 python 代码中定义断点来使用 python 中的断点,如前所述 here .
是否有另一种方法来完成此任务(将断点定义保留在 python 代码之外)或者使用 gdb Breakpoint 类是唯一的方法? 我想要的只是一个检查,它可以帮助确定它是哪个断点。
谢谢
请您参考如下方法:
Is there another way to do this task (keeping breakpoint definitions out of python code) All I want is a check which can help identify which breakpoint it is.
至少在 gdb 7.6 中是可能的。请参阅我的回答中的第一个示例。您还可以在 python 脚本中设置所有断点,如第二个示例所示。但首先,这是一个测试 C++ 程序,将在示例中使用。
>cat main.cpp
int a()
{
int p = 0;
p = p +1;
return p;
}
int b()
{
return a();
}
int c()
{
return a();
}
int main()
{
c();
b();
a();
return 0;
}
第一个示例 - native gdb 脚本中的断点和另一个脚本中的 python 函数。我使用事件(Events-In-Python):
所以这是一个带有一些断点的原生脚本:
>cat my_check.gdb
b main.cpp:20
b main.cpp:21
b b
source my_check.py
r
q
这是一个 python 脚本 my_check.py:
def my_breakpoint_handler (event):
if (isinstance(event, gdb.BreakpointEvent)):
print event.breakpoint.location
if event.breakpoint.location == "b":
gdb.write("Breakpoint in b()\n")
gdb.execute("bt")
elif event.breakpoint.location == "main.cpp:20":
gdb.write("Breakpoint in main.cpp:20\n")
gdb.execute("info frame")
elif event.breakpoint.location == "main.21":
gdb.write("Breakpoint in main.cpp:21\n")
gdb.write("some info")
else:
pass
gdb.execute("c")
gdb.events.stop.connect(my_breakpoint_handler)
这是测试本身:
>gdb -q -x my_check.gdb a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.breakpoint/a.out...done.
Breakpoint 1 at 0x40056a: file main.cpp, line 20.
Breakpoint 2 at 0x40056f: file main.cpp, line 21.
Breakpoint 3 at 0x400554: file main.cpp, line 10.
Breakpoint 1, main () at main.cpp:20
20 c();
main.cpp:20
Breakpoint in main.cpp:20
Stack level 0, frame at 0x7fffffffe0d0:
rip = 0x40056a in main (main.cpp:20); saved rip 0x3c4121ecdd
source language c++.
Arglist at 0x7fffffffe0c0, args:
Locals at 0x7fffffffe0c0, Previous frame's sp is 0x7fffffffe0d0
Saved registers:
rbp at 0x7fffffffe0c0, rip at 0x7fffffffe0c8
Breakpoint 2, main () at main.cpp:21
21 b();
main.cpp:21
Breakpoint 3, b () at main.cpp:10
10 return a();
b
Breakpoint in b()
#0 b () at main.cpp:10
#1 0x0000000000400574 in main () at main.cpp:21
[Inferior 1 (process 20798) exited normally]
第二个例子 - python脚本中的所有断点和python函数。
这是一个 python gdb 脚本:
>cat my_check2.py
class MyBreakpoint (gdb.Breakpoint):
def stop (self):
print self.location
if self.location == "b":
gdb.write("Breakpoint in b()\n")
gdb.execute("bt")
elif self.location == "main.cpp:20":
gdb.write("Breakpoint in main.cpp:20\n")
gdb.execute("info frame")
elif self.location == "main.21":
gdb.write("Breakpoint in main.cpp:21\n")
return False
MyBreakpoint("main.cpp:20")
MyBreakpoint("main.cpp:21")
MyBreakpoint("b")
gdb.execute("r")
gdb.execute("q")
这里用到了:
>gdb -q -x my_check2.py a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.breakpoint/a.out...done.
Breakpoint 1 at 0x40056a: file main.cpp, line 20.
Breakpoint 2 at 0x40056f: file main.cpp, line 21.
Breakpoint 3 at 0x400554: file main.cpp, line 10.
main.cpp:20
Breakpoint in main.cpp:20
Stack level 0, frame at 0x7fffffffe0d0:
rip = 0x40056a in main (main.cpp:20); saved rip 0x3c4121ecdd
source language c++.
Arglist at 0x7fffffffe0c0, args:
Locals at 0x7fffffffe0c0, Previous frame's sp is 0x7fffffffe0d0
Saved registers:
rbp at 0x7fffffffe0c0, rip at 0x7fffffffe0c8
main.cpp:21
b
Breakpoint in b()
#0 b () at main.cpp:10
#1 0x0000000000400574 in main () at main.cpp:21
[Inferior 1 (process 27434) exited normally]
