Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I step through nested function calls in GDB?

Tags:

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.

like image 259
horseyguy Avatar asked Aug 13 '09 02:08

horseyguy


People also ask

How do I step over a breakpoint in GDB?

If you want to execute the entire function with one keypress, type "next" or "n". This is equivalent to the "step over" command of most debuggers. If you want gdb to resume normal execution, type "continue" or "c". gdb will run until your program ends, your program crashes, or gdb encounters a breakpoint.

How do you step out in GDB?

Those who use Visual Studio will be familiar with the Shift + F11 hotkey, which steps out of a function, meaning it continues execution of the current function until it returns to its caller, at which point it stops.

Can you call functions in GDB?

To call a function in the program, GDB has to temporarily modify the state of the inferior. This has potentially undesired side effects. Also, having GDB call nested functions is likely to be erroneous and may even crash the program being debugged.


2 Answers

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)
like image 95
hlovdal Avatar answered Sep 19 '22 22:09

hlovdal


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

like image 28
Teja Avatar answered Sep 22 '22 22:09

Teja