cdebugginggdb

How to prevent gdb to stop after next or finish command


I am trying to define a chain of commands, which shall be invoked after a breakpoint in gdb:

    break some_function
    commands
       up
       next
       printf "some_string"
       continue
    end

In this case (for example) I want to break at some_function, go up in the stack frame and jump right behind this function via the next command, then print "some_string" (or maybe some variable, which was changed by the function) and then just to continue. But that doesn't work, since gdb will just stop after the next command and wait for the user to input something, disregarding the following commands.

Edit: Ok, the example I gave above did not correctly fit my description. What I really wanted (Thanks goes to the commenter Nikolai, see below) was something like that:

    break some_function
    commands
       finish
       printf "some_string"
       continue
    end

This shall break at 'some_function', execute that function, return and print right after the execution of 'some_function' the string 'some_string'. The problem I had previously with the next command now appears with the finish command: execution will stop after this command and gdb will wait for user input, disregarding the following printf and continue statements. I am sorry, that this question got a bit confusing. I am not happy about it myself, but posting it again, wouldn't be a better solution (since the comments would be lost and it would be cross-posting).


Solution

  • Ok, I think I found the answer myself: gdb seems to set internally a breakpoint to the finish and the next command. However, one can define a hook, to overcome breaking at this breakpoint. The best method I think is to generate an own version of the finish (or the next command) to avoid side effects, so this is what one can do:

        define myfinish
          finish
        end
    
        define hook-myfinish
          printf "some_string"
          continue
        end
    
        break some_function
        commands
          myfinish
        end
    

    It is advisable to use the silent statement at the beginning of the breaks commands section, to suppress additional output when breaking:

        break some_function
        commands
          silent
          myfinish
        end