Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have gdb jump past a throw statement at the end of a function?

Tags:

debugging

gdb

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.

like image 561
Richard Corden Avatar asked Aug 06 '09 15:08

Richard Corden


People also ask

How do you jump out of a function in GDB?

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.

Can you go back a line in GDB?

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.

What is a break point in GDB?

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.

What does finish do in GDB?

Description. This command continues execution of the current function until it returns to its caller.


1 Answers

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

  (gdb) set $eip = 0xValue
like image 138
Justin Avatar answered Oct 11 '22 21:10

Justin