oompahloompah
oompahloompah

Reputation: 9333

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

Upvotes: 5

Views: 2528

Answers (5)

ks1322
ks1322

Reputation: 35716

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.

Upvotes: 0

Imron
Imron

Reputation: 822

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.

Upvotes: 2

TonyK
TonyK

Reputation: 17114

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.

Upvotes: 1

geekosaur
geekosaur

Reputation: 61369

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

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

Upvotes: 0

aschepler
aschepler

Reputation: 72311

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.

Upvotes: 0

Related Questions