How to prevent gdb to stop after next command

房东的猫 提交于 2021-02-17 04:01:10

问题


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).


回答1:


Why not simply break at some_function+0x4 or similar offset? To know the correct offset, click next ONCE, & note down the offset...

break some_function+0x4
commands
   up
   printf "some_string"
   continue
end



回答2:


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


来源:https://stackoverflow.com/questions/14261404/how-to-prevent-gdb-to-stop-after-next-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!