如何在GDB中逐步执行嵌套函数调用?

问题描述 投票:5回答:3

有时我想调试这样的函数:

my_func1(my_func2(my_func3(val)));

有没有办法在GDB中逐步完成这个嵌套调用?

我想逐步执行my_func3,然后是my_func2,然后是my_func1等。

gdb
3个回答
8
投票

你踩着什么命令?调试next时,my_func1(my_func2(my_func3(val)));将进入下一行,但step应该进入my_func3。例:

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)));
}

调试:

(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)

1
投票

如果你知道函数定义在源代码中的位置,一个解决方案就是将断点放在该函数中。


0
投票

是的,虽然你可能已经弄脏了拆卸。首先尝试step命令(缩写s)。如果这不会让你进入my_func3(),请尝试使用stepi命令(缩写si)一次执行一条指令。这可能需要多次调用,因为可能有很多指令设置函数调用参数并在之后进行清理。

© www.soinside.com 2019 - 2024. All rights reserved.