如何查看GDB中的功能框架堆栈

发布于 2025-01-24 12:53:51 字数 738 浏览 0 评论 0原文

我是C和GDB的新手。我有一个带有一些递归功能的代码,因此我认为堆栈呼叫会在堆栈中堆积。无论如何,我可以使用GDB查看它吗?我尝试使用Google以获取一些GDB命令,但无法弄清楚如何正确使用它以查看函数调用堆栈。有人可以建议如何使用GDB做到这一点吗?这是我的代码:

#include <stdlib.h>
#include <stdio.h>

int proc(int *a, int n) {
    if(n == 0) {
    return 0;
    } else {
    printf("%0d \n", a[n-1]);
    return proc(a , n-1) + a[n-1];  
    }
};


int main (int argc, char** argv) {
    int arrsize = 4;
    int *b = malloc(7*sizeof(int));
    b[0] = 7;
    b[1] = 6;
    b[2] = 5;
    b[3] = 3;
    b[4] = 2;
    b[5] = -4;
    b[6] = -5;
    proc(b, 4); // so I call this recursive function with n = 4, so I guess there should be 4 recursive calls. I just want to see in memory how the call is being built up. 

};



I am new to c and gdb. I have a piece of code with some recursion function, so I think a stack of function call will get build-up in the stack. Is there anyway I can view it using gdb? I tried to google for some gdb commands, but not able to figure out how to use it properly to view the function call stack. Could someone suggests how to do it with gdb? this is my code:

#include <stdlib.h>
#include <stdio.h>

int proc(int *a, int n) {
    if(n == 0) {
    return 0;
    } else {
    printf("%0d \n", a[n-1]);
    return proc(a , n-1) + a[n-1];  
    }
};


int main (int argc, char** argv) {
    int arrsize = 4;
    int *b = malloc(7*sizeof(int));
    b[0] = 7;
    b[1] = 6;
    b[2] = 5;
    b[3] = 3;
    b[4] = 2;
    b[5] = -4;
    b[6] = -5;
    proc(b, 4); // so I call this recursive function with n = 4, so I guess there should be 4 recursive calls. I just want to see in memory how the call is being built up. 

};



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2025-01-31 12:53:51

将测试程序放入/tmp/test.c中,然后:

$ gcc -g3 -O0 -o test.x test.c
$ gdb -q test.x
Reading symbols from test.x...
(gdb) b proc
Breakpoint 1 at 0x401145: file test.c, line 5.
(gdb) r
Starting program: /tmp/test.x 

Breakpoint 1, proc (a=0x4052a0, n=4) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=4) at test.c:5
#1  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
3 

Breakpoint 1, proc (a=0x4052a0, n=3) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=3) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#2  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
5 

Breakpoint 1, proc (a=0x4052a0, n=2) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=2) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=3) at test.c:9
#2  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#3  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
6 

Breakpoint 1, proc (a=0x4052a0, n=1) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=1) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=2) at test.c:9
#2  0x000000000040118d in proc (a=0x4052a0, n=3) at test.c:9
#3  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#4  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
7 

Breakpoint 1, proc (a=0x4052a0, n=0) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=0) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=1) at test.c:9
#2  0x000000000040118d in proc (a=0x4052a0, n=2) at test.c:9
#3  0x000000000040118d in proc (a=0x4052a0, n=3) at test.c:9
#4  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#5  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
[Inferior 1 (process 3685291) exited normally]
(gdb) quit
$ 

Placing your test program into /tmp/test.c, then:

$ gcc -g3 -O0 -o test.x test.c
$ gdb -q test.x
Reading symbols from test.x...
(gdb) b proc
Breakpoint 1 at 0x401145: file test.c, line 5.
(gdb) r
Starting program: /tmp/test.x 

Breakpoint 1, proc (a=0x4052a0, n=4) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=4) at test.c:5
#1  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
3 

Breakpoint 1, proc (a=0x4052a0, n=3) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=3) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#2  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
5 

Breakpoint 1, proc (a=0x4052a0, n=2) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=2) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=3) at test.c:9
#2  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#3  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
6 

Breakpoint 1, proc (a=0x4052a0, n=1) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=1) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=2) at test.c:9
#2  0x000000000040118d in proc (a=0x4052a0, n=3) at test.c:9
#3  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#4  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
7 

Breakpoint 1, proc (a=0x4052a0, n=0) at test.c:5
5       if(n == 0) {
(gdb) bt
#0  proc (a=0x4052a0, n=0) at test.c:5
#1  0x000000000040118d in proc (a=0x4052a0, n=1) at test.c:9
#2  0x000000000040118d in proc (a=0x4052a0, n=2) at test.c:9
#3  0x000000000040118d in proc (a=0x4052a0, n=3) at test.c:9
#4  0x000000000040118d in proc (a=0x4052a0, n=4) at test.c:9
#5  0x000000000040123b in main (argc=1, argv=0x7fffffffb118) at test.c:24
(gdb) c
Continuing.
[Inferior 1 (process 3685291) exited normally]
(gdb) quit
$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文