3

WinDBG and the related windows kernel debuggers support a "pc" command which runs the target until reaching the next call statement (in assembly). In other words, it breaks just prior to creating a new stack frame, sort of the opposite of "finish". "Start" in GDB runs until main starts, but in essence I want 'start' but with a wildcard of "any next frame".

I'm trying to locate a similar functionality in GDB, but have not found it.

is this possible?

Example WinDBG doc: http://windbg.info/doc/1-common-cmds.html#4_expr_and_cmds

1 Answer 1

8

Simple answer: no, step-to-next-call is not part of GDB commands.

GDB/Python-aware answer: no, it's not part of GDB commands, but it's easy to implement!

I'm not sure to understand if you want to stop before or after the call instruction execution.

  • To stop before, you need to stepi/nexti (next assembly instruction) until you see call in the current instruction:

    import gdb
    
    class StepBeforeNextCall (gdb.Command):
        def __init__ (self):
            super (StepBeforeNextCall, self).__init__ ("step-before-next-call",
                                                       gdb.COMMAND_OBSCURE)
    
        def invoke (self, arg, from_tty):
            arch = gdb.selected_frame().architecture()
    
            while True:
                current_pc = addr2num(gdb.selected_frame().read_register("pc"))
                disa = arch.disassemble(current_pc)[0]
                if "call" in disa["asm"]: # or startswith ?
                    break
    
                SILENT=True
                gdb.execute("stepi", to_string=SILENT)
    
            print("step-before-next-call: next instruction is a call.")
            print("{}: {}".format(hex(int(disa["addr"])), disa["asm"]))
    
    def addr2num(addr):
        try:
            return int(addr)  # Python 3
        except:
            return long(addr) # Python 2
    
    StepBeforeNextCall()
    
  • To stop after the call, you compute the current stack depth, then step until it's deeper:

    import gdb
    
    def callstack_depth():
        depth = 1
        frame = gdb.newest_frame()
        while frame is not None:
            frame = frame.older()
            depth += 1
        return depth
    
    class StepToNextCall (gdb.Command):
        def __init__ (self):
            super (StepToNextCall, self).__init__ ("step-to-next-call", 
                                                   gdb.COMMAND_OBSCURE)
    
        def invoke (self, arg, from_tty):
            start_depth = current_depth =callstack_depth()
    
            # step until we're one step deeper
            while current_depth == start_depth:
                SILENT=True
                gdb.execute("step", to_string=SILENT)
                current_depth = callstack_depth()
    
            # display information about the new frame
            gdb.execute("frame 0")
    
    StepToNextCall() 
    

just put that in a file, source it with GDB (or in your .gdbinit) and that will provide you the new commands step-before-next-call and step-to-next-call.

Relevant documentation is there:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.