0
votes

Say that main() calls foo() which calls bar(). Obviously foo() and bar() are functions defined by the user/programmer. Also, say that these functions are called in the middle of the caller's function.

If I am in Foo() and I suddenly step into bar() accidentally, how do I leave bar() and continue Foo()'s immediate instruction after the call is made in Gdb?

I tried to use the "finish" command, but I've seen it finish foo()'s function call and return me back to main.

Also I used the "ni" command only when program counter points to the "call bar()" instruction inside foo(), but that didn't work since it's not a library function. I assume "ni" is used to skip library functions.

For now, the only idea I have is to create a breakpoint to the instruction after the "call bar()", but I feel like there's a better way to do this. Is there?

Thanks in advance.

1
Finish works fine for me. Even GDB manual says "Continue running until just after function in the selected stack frame returns." about the Finish commandThe Philomath
Finish should work, but there may be issues, if you are debugging optimized code or lacking debug information.dbrank0

1 Answers

2
votes

I tried to use the "finish" command, but I've seen it finish foo()'s function call and return me back to main.

This is true if you are in foo()'s gdb frame (frame 1) when calling finish. Try to switch to bar()'s frame (frame 0) and call finish. gdb should finish only one frame (frame 0) in this case.