[英]How to identify specific GDB breakpoint in python code?
我正在嘗試使用python編寫GDB腳本。 我有一個GDB的本機腳本文件,該文件可提供python腳本文件。 在.gdb文件中,我聲明了一些針對不同功能的斷點。 我可以使用python腳本對這些斷點執行下一步/步驟/繼續操作,並打印不同的變量。 但是我為每個斷點提供了獨特的python函數,並具有特定的打印效果。我想使其更好,更通用。
我想要的是在python代碼中具有一個函數,以及一種確定命中哪個斷點的方法,以便我可以根據斷點打印不同的變量。 如果無論如何我只是打印它們,那么我將得到超出范圍的錯誤。
我已經檢查和GDB也允許通過所提到的Python代碼定義斷點使用斷點玩弄在python 這里 。
還有另一種方法可以執行此任務(將斷點定義保留在python代碼之外),還是使用gdb Breakpoint類是唯一方法? 我所需要的只是一項檢查,可以幫助您確定它是哪個斷點。
謝謝
還有另一種方法可以執行此任務(將斷點定義保留在python代碼之外)。我只需要進行檢查即可幫助確定它是哪個斷點。
至少在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;
}
第一個示例 -本機gdb腳本中的斷點和另一個腳本中的python函數。 我使用事件( Python中的Events ):
因此,這是帶有一些斷點的本機腳本:
>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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.