From bc5d78ee5f73e786cd12f5642313664197d8dded Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sun, 3 Apr 2022 10:31:28 +0530 Subject: [PATCH] gdb: Add custom script for breakpoint with conditions on backtrace --- gdb/.gdbinit.d/scripts/framebp.py | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 gdb/.gdbinit.d/scripts/framebp.py diff --git a/gdb/.gdbinit.d/scripts/framebp.py b/gdb/.gdbinit.d/scripts/framebp.py new file mode 100644 index 0000000..cb56cc0 --- /dev/null +++ b/gdb/.gdbinit.d/scripts/framebp.py @@ -0,0 +1,37 @@ +# https://osa1.net/posts/2020-04-25-breakpoint-backtrace-conditionals.html +import gdb # type: ignore + + +class FrameBp(gdb.Breakpoint): + def __init__(self, spec, *args, frame=None, **kwargs): + self.frame = frame + super().__init__(spec, *args, **kwargs) + + def stop(self): + frame = gdb.selected_frame().older() + + while frame: + if frame.name() == self.frame: + return True + + frame = frame.older() + + return False + + +class FrameBP(gdb.Command): + def __init__(self): + super().__init__("framebp", gdb.COMMAND_USER) + + def complete(self, _text, _word): + return gdb.COMPLETE_SYMBOL + + def invoke(self, args, _from_tty): + args = args.split() + if len(args) != 2: + print("Need function name and frame as arguments") + return + + FrameBp(args[0], frame=args[1]) + +FrameBP()