开发者

Debugging attached process with gdb - how to escape from a loop

I am debugging code that looks like this:

while (true){
   // do something ...
   size_t i = foo();  // <- bp set here
   if (flag_set) break;
}

// More code follows here ...

I want to break at the foo() function call, invoke it a few times and then jump out of the while loop completely (lets assume that we are guaranteed that the flag will be set - so we can break out of the loop.

How do I break out of the loop completely?开发者_C百科. finish simply runs to the next iteration. What I want to do is to exit the current "code chunk" (in this case, the while loop)


You want the advance command, which takes the same arguments as the break command. Using your code as an example (but with line numbers added):

10 while (true){
11   // do something ...
12   size_t i = foo();  // <- bp set here
13   if (flag_set) break;
14 }
15 
16 // More code follows here ...
17 someFunction();

Say your original breakpoint on line 12 was breakpoint 1, and after breaking a few times you wanted to skip to line 17, you would type something like:

disable 1

advance 17

which would disable breakpoint 1 (so it doesn't get hit for the rest of the loop) and then keep executing the program until it hit line 17.


Set a breakpoint before the loop. Then cursor to the foo() call, and use Debug|Run to Line. This is so useful that I have dedicated a function key to it.


Set a second breakpoint after the loop. disable the breakpoint inside the loop. cont. enable the breakpoint again.

I don't know of any easier way.


Try using the jump command. Per gdb help, on this system at least:

jump -- Continue program being debugged at specified line or address


What you need is until command. This is the easiest way to avoid stepping through the loop. From gdb manual:

Continue running until a source line past the current line, in the current stack frame, is reached. This command is used to avoid single stepping through a loop more than once. It is like the next command, except that when until encounters a jump, it automatically continues execution until the program counter is greater than the address of the jump.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜