# 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()
