Andrew Lamarra
Andrew Lamarra

Reputation: 537

Step to Next Branching Instruction in GDB

Is there a way to continue execution until a branching instruction is reached in GDB? Much like the WinDbg "ph" command. If not, can it be scripted in Python?

Upvotes: 1

Views: 173

Answers (1)

Andrew Lamarra
Andrew Lamarra

Reputation: 537

I finally came across this question:
Does GDB have a "step-to-next-call" instruction?

I was able to modify the code there to create my own "ph" command:

import gdb

mips_branches = ["beq", "beqz", "bne", "bnez", "bgtz", "bltz", "bgez", "blez", "j", "jr", "jal", "jalr"]
arm_branches = ["b", "bl", "blx", "bx", "beq"]

class StepToNextBranch (gdb.Command):
    def __init__ (self):
        super (StepToNextBranch, self).__init__ ("ph", gdb.COMMAND_OBSCURE)

    def invoke (self, arg, from_tty):
        arch = gdb.selected_frame().architecture()

        while True:
            SILENT=True
            gdb.execute("nexti", to_string=SILENT)
            current_pc = int(gdb.selected_frame().read_register("pc"))
            disa = arch.disassemble(current_pc)[0]
            opcode = disa["asm"].split("\t")[0]
            if opcode in mips_branches or opcode in arm_branches:
                break

        gdb.execute("context")

StepToNextBranch()

I'm not as familiar with ARM as MIPS so I'm not sure this covers everything. Eventually, I'll add x86 branching instructions.

Also, that gdb.execute("context") line is there because I use GEF.

Upvotes: 1

Related Questions