34

I can tell GDB to return from a function immediately with return, and call a function with call myFunction.

But how do I get it break out of the current loop? i.e. to act as if it's hit a break; statement.

Is jump myfile.c:<linenumber> the way to do this?

sigjuice
  • 28,661
  • 12
  • 68
  • 93
John Carter
  • 53,924
  • 26
  • 111
  • 144
  • Do you want to execute the loop normally and `break` out of it, or alter execution by skipping the loop entirely? Asking to differentiate from https://stackoverflow.com/questions/14651073/is-there-a-gdb-command-to-finish-a-loop-construct – Ciro Santilli OurBigBook.com Jul 22 '17 at 18:44

4 Answers4

24

You can use until to make the loop end.

You should give it at the end of the loop.

This is useful if you do not need to step in to iterate a loop.

Nike
  • 1,223
  • 2
  • 19
  • 42
bala
  • 249
  • 2
  • 2
  • 1
    If I read the docs right ( http://www.delorie.com/gnu/docs/gdb/gdb_38.html ) that's equivalent to setting a temporary breakpoint after the loop - I was looking to break out of the loop. – John Carter Jun 01 '11 at 22:44
  • 2
    this tip is still valuable, tho not an answer to the question. Maybe the question should be expanded to include a spcific statement that you do not want this feature? – hochl May 05 '14 at 09:01
  • Can anyone explain to me why this has more upvotes than the older and accepted answer? – Nike Dec 12 '22 at 04:08
  • @Nike the 'jump' is a rather crude way of forcing the cursor to move, and involves figuring out the right line number. `until` is purpose built for skipping over for loops – Cheetaiean Apr 19 '23 at 18:32
  • @Cheetaiean "until" what? "Until" is usually a word that comes before another word. – Nike Apr 19 '23 at 18:49
14

jump looks like what you want. See Continuing at a Different Address

sigjuice
  • 28,661
  • 12
  • 68
  • 93
  • `jump [line number]` got me out of the loop and to the line I wanted to go. This is the answer I was looking for (and it's also the one that OP accepted). I have no idea why the other (poorly written, in my opinion, at least until I edited it) answer got more upvotes. – Nike Dec 12 '22 at 04:10
5

I do this:
1. do a source listing.
2. Set a breakpoint at the next line where loop ends.
3. Continue

sud03r
  • 19,109
  • 16
  • 77
  • 96
1

One of the ways could be to set the condition of the loop to false. But this would mean that you would have to wait for the current iteration to finish.

So to summarize the steps would be:
1. Set a breakpoint at the last line of the loop
2. Continue
3. When breakpoint hits, set the loop condition variable to false.

It won't work as direct break statement though.

Sukanto
  • 992
  • 3
  • 11
  • 21