Monday, March 12, 2012

GDB: what is back trace ?

  Back trace or bt  is a summary of how the application or program came to the current position. It displays one line per frame starting from the current frame as zero and followed by its caller as frame one and so on up the stack till the main function as a last frame or nth frame.

Looking into Stack:
   As most of us know, whenever a function call occures, the information about the call is stored  in a memory region called call stack. For each function one stack frame or frame is created with function arguements, local variables, return address etc in call stack.
        In GDB, using back trace or bt command , we can get the call stack info or summary of the back traces. we can move from one frame to other using up and down gdb commands. we can get the details of the local varaibles in a frame with info locals command. See the sample code below with gdb out put.

(gdb) bt
#0  func3 () at stackFrame.c:5
#1  0x00000000004004e0 in func2 () at stackFrame.c:12
#2  0x000000000040050a in func1 () at stackFrame.c:18
#3  0x0000000000400534 in func () at stackFrame.c:24
#4  0x000000000040055e in display () at stackFrame.c:30
#5  0x000000000040056e in main () at stackFrame.c:34
(gdb) info locals
e = 0
(gdb) up
#1  0x00000000004004e0 in func2 () at stackFrame.c:12
12              func3();
(gdb) info locals
d = 10
(gdb) up
#2  0x000000000040050a in func1 () at stackFrame.c:18
18              func2();
(gdb) up
#3  0x0000000000400534 in func () at stackFrame.c:24
24              func1();
(gdb) frame 1
#1  0x00000000004004e0 in func2 () at stackFrame.c:12
12              func3();
(gdb) bt
#0  func3 () at stackFrame.c:5
#1  0x00000000004004e0 in func2 () at stackFrame.c:12
#2  0x000000000040050a in func1 () at stackFrame.c:18
#3  0x0000000000400534 in func () at stackFrame.c:24
#4  0x000000000040055e in display () at stackFrame.c:30
#5  0x000000000040056e in main () at stackFrame.c:34
(gdb) up
#2  0x000000000040050a in func1 () at stackFrame.c:18
18              func2();
(gdb)


        From the above GDB sesion, where all the gdb commands shown in blue color, bt gives the back trace of the prorgam in which program stopped in func3(). so frame zero is func3() which is called by func2() and followed by func1(), func(),display() and main() as the last stack frame.  Using frame gdb command, we can directly go to the desired frame by specifying frame number from the back trace.

No comments:

Popular Posts