hodondo
hodondo

Reputation: 91

how to view function frame stack in gdb

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. 

};



Upvotes: 0

Views: 161

Answers (1)

Andrew
Andrew

Reputation: 4751

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
$ 

Upvotes: 1

Related Questions