When I'm debugging, I sometimes find it useful to "replay" the last few statements of code. For example:
void foo (int & i) {
i = 0;
++i;
i++;
}
While running this through the debugger, you can add a breakpoint at the top of the function body, and then from any statement inside of foo
if you type: "jump file.cc:2" the debugger will return back to i = 0
. I appreciate that this isn't always perfect, but sometimes it can be enough to find the bug you're searching for.
I'm currently investigating a problem that results in an exception being thrown. The exception is being thrown at the bottom of a 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 be able to do, is to put a breakpoint before throw int ()
, then to jump over just that statement, finish the function bar - so that I can then jump back up to the i = 0
line in foo.
Is there a way I can jump past throw int ()
, or finish out of bar
without executing the throw statement?
The problem appears to be that there's no statement after the throw
so I have nowhere to put the breakpoint I want to jump to.
UPDATE:
To highlight 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
The close curly for 'bar' is on line 4 of 't.cc', however gdb considers this as a breakpoint for foo
.
To do this, just type "break [functionname]". gdb will stop your program just before that function is called. Breakpoints stay set when your program ends, so you do not have to reset them unless you quit gdb and restart it. Examining data When your program is stopped you can examine or set the value of any variable.
If the target environment supports it, gdb can allow you to “rewind” the program by running it backward. A target environment that supports reverse execution should be able to “undo” the changes in machine state that have taken place as the program was executing normally.
A breakpoint makes your program stop whenever a certain point in the program is reached. For each breakpoint, you can add conditions to control in finer detail whether your program stops.
Description. This command continues execution of the current function until it returns to its caller.
Yes, you can. You need to set the instruction pointer to the value you want.
(gdb) set $eip = 0xValue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With