0

I am trying to implement a single stepping using GDB command file. I would like to stop executing the command file whenever I encounter any breakpoint.

Is there a way to detect that a breakpoint was hit? and also how can I stop executing the command file after that.

I checked the documantation and it seems like I only can have break commands for certain breakpoints. But not for a random breakpoint.

I have something like this:

printf "single stepping\n"
set $steps_count = 0
     while ($steps_count < 5)
         set $steps_count = $steps_count + 1
          printf "program counter 0x%x\n", $pc
         printf "next #%d\n", $steps_count
         next
     end

And this is what I want to achieve:

printf "single stepping\n"
set $steps_count = 0
     while (# breakpoint is not hit)
         set $steps_count = $steps_count + 1
          printf "program counter 0x%x\n", $pc
         printf "next #%d\n", $steps_count
         next
     end
# end the execution of the command file.

update: I tried to use the follwing:

# set the end breakpoint
break *0x199e
commands
    stop 1 
    quit
end

break *0x1980
# set a certain once we encounter the first breakpoint
commands
    set $count = 0
    while $count < 5
        printf "#PC: 0x%x", $pc
        set $count = $count + 1
        stepi
    end
end

This should start logging the program counter once the first breakpoint is hit. But I don't know why this break command doesn't work. Once the breakpoint is hit the program stops and the while loop is not entered!

Update: according to the gdb documentation:

Any other commands in the command list, after a command that resumes execution, are ignored. This is because any time you resume execution (even with a simple next or step), you may encounter another breakpoint--which could have its own command list, leading to ambiguities about which list to execute.

so I don't think it is a good idea to put a loop that has step or next in break commands.

mad0x60
  • 312
  • 1
  • 14
  • For the update, could you share the gdb output you get (and perhaps the version)? – Christian B Sep 18 '19 at 06:07
  • @ChristianB I updated the question with what I found in the documentation. Thanks – mad0x60 Sep 18 '19 at 08:26
  • It seems like break commands don't get executed in my command files. I asked this in a different question already. https://stackoverflow.com/questions/57988776/gdb-break-commands-dont-get-executed-in-command-files – mad0x60 Sep 18 '19 at 08:55

1 Answers1

0

Try something like:

commands 1-10 # for breakpoints number 1 through 10
set $breakpoint_hit = 1
end

Then check for that in your loop.

Christian B
  • 581
  • 4
  • 7