horseyguy
horseyguy

Reputation: 29895

How can I step through nested function calls in GDB?

Sometimes I want to debug functions like this:

my_func1(my_func2(my_func3(val)));

Is there a way I can step through this nested call in GDB?

I want to step through my_func3, then my_func2, then my_func1, etc.

Upvotes: 7

Views: 15974

Answers (3)

Teja
Teja

Reputation: 141

If you know the where the function definition is in the source code one solution will be to put break point inside that function.

Upvotes: 1

hlovdal
hlovdal

Reputation: 28180

What command are you stepping with? next would go to next line when debugging my_func1(my_func2(my_func3(val)));, but step should enter my_func3. Example:

int my_func1(int i)
{
  return i;
}

int my_func2(int i)
{
  return i;
}

int my_func3(int i)
{
  return i;
}

int main(void)
{
  return my_func1(my_func2(my_func3(1)));
}

Debugged:

(gdb) b main
Breakpoint 1 at 0x4004a4: file c.c, line 19.
(gdb) run
Starting program: test

Breakpoint 1, main () at c.c:19
19    return my_func1(my_func2(my_func3(1)));
(gdb) step
my_func3 (i=1) at c.c:14
14    return i;
(gdb) step
15  }
(gdb) step
my_func2 (i=1) at c.c:9
9   return i;
(gdb) step
10  }
(gdb) step
my_func1 (i=1) at c.c:4
4   return i;
(gdb) step
5 }
(gdb) step
main () at c.c:20
20  }
(gdb) cont
Continuing.

Program exited with code 01.
(gdb)

Upvotes: 8

Adam Rosenfield
Adam Rosenfield

Reputation: 400146

Yes, although you may have get your hands dirty with the disassembly. First try the step command (abbreviation s). If that doesn't put you into my_func3(), try instead the stepi command (abbreviation si) to step one instruction at a time. This may take several invocations, since there can be a lot of instructions setting up the function call arguments and cleaning up afterwards.

Upvotes: 0

Related Questions