Can I run gdb for the throw statement at the end of the function?

When I debug, sometimes it’s useful for me to β€œreplay” the last few code statements. For instance:

void foo (int & i) { i = 0; ++i; i++; } 

When you run this through the debugger, you can add a breakpoint at the top of the function body, and then from any statement inside foo , if you type: "jump file.cc:2" the debugger will go back to i = 0 . I appreciate that this is not always perfect, but sometimes it may be enough to find the error you are looking for.

I am currently studying a problem that throws an exception. The exception is the bottom of the called function, so something like:

 void bar () { throw int (); } void foo (int & i) { i = 0; ++i; bar (); i++; } int main () { try { int i; foo (i); } catch (...) { } } 

What I want to do is to set a breakpoint before throw int () , then jump over this statement, end the function panel so that I can then return to line i = 0 in Foo.

Is there a way I can jump over throw int () or end from bar without executing the throw statement?

The problem is that after throw there is no instruction, so I have nowhere to put a breakpoint on which I want to go.

UPDATE:

To emphasize what happens in my simple example above:

 This GDB was configured as "i486-slackware-linux"... (gdb) break bar Breakpoint 1 at 0x804856a: file t.cc, line 3. (gdb) run Starting program: ..../t Breakpoint 1, bar () at t.cc:3 (gdb) break t.cc:4 Breakpoint 2 at 0x8048592: file t.cc, line 4. (gdb) jump t.cc:4 Line 4 is not in `bar()'. Jump anyway? (y or n) y Continuing at 0x8048592. Breakpoint 2, foo ( i=@0xb80155eb ) at t.cc:6 

Closing the cursor for "bar" is on line 4 of "t.cc", however gdb considers this a breakpoint for foo .

+4
source share
5 answers

My bad spelling actually gave me an answer!

My variation of "disassemble" did not work, so looking for the right spelling, I eventually came across a "help stack":

Study stack. A stack consists of stack frames. Gdb assigns numbers to stack frames counting from scratch for the innermost (currently executable) frame.

At any time, gdb defines one frame as the "selected" frame. Variable search queries are executed relative to the selected frame. When a program is debugged, gdb selects the innermost frame. The commands below can be used to select other frames by number or address.

List of commands:

backtrace - print backtrace of all stack frames

bt - print backtrace of all stack frames

down - select and print the stack frame caused by this

frame - select and print a stack frame

return - make the selected stack stack returned to its caller

select-frame - select stack frame without printing

up - select and print a stack frame called this

The return command from the above list does exactly what I want in this case.

Thank you all for your help.

+5
source

Yes, you can. You need to set the instruction pointer to the desired value.

  (gdb) set $eip = 0xValue 
+5
source

In many cases, the compiler eliminates the end of the function because it is not available. You might want to make a flag that you can set to avoid this:

 void bar() { if (!debugFlag) throw int(); } 

Make sure the flag is global (not static), so the compiler cannot prove that it will never be written.

To skip the roll

 (gdb) set debugFlag = 1 

And be sure to install it later.

+2
source

Extending to @Justin's answer - while in bar() type of the disassemble function, pay attention to the address of the ret command, set eip to this address.

+2
source

%eip is platform specific and requires some work. It’s easier to just jump to the line number containing the end of the course. You need to combine this with something like bdonlan, suggesting in case the compiler optimizes the function of returning from the function as inaccessible.

 $ cat >x.cpp #include <stdio.h> static volatile int debug = 0; void f() { if (!debug) throw 1; } int main() { try { f(); puts("f didn't throw"); } catch(...) { puts("f threw"); } return 0; } $ g++ -g x.cpp -ox $ gdb x [...] (gdb) run Starting program: [...]/x Reading symbols for shared libraries . done f threw Program exited normally. (gdb) break f Breakpoint 1 at 0x1e30: file x.cpp, line 6. (gdb) run Starting program: [...]/x Breakpoint 1, f () at xc:6 6 if (!debug) (gdb) jump 8 Continuing at 0x1e73. f didn't throw Program exited normally. 
+1
source

All Articles