Turn from: http://blog.chinaunix.net/uid-27033491-id-3329298.html
When using GDB for debugging, it is particularly important to use the view stack information, especially when debugging the kernel. The GDB stack trace allows you to see a list of all called functions and the information on the stack for each function.
---------------------------------------------------------------------------------
One, simple example.
#include <stdio.h>
int sum (int m,int n)
{
int i = 3;
int j = 4;
return m+n;
}
int main (void)
{
int m = 10;
int n = 9;
int ret = 0;
ret = SUM (m,n);
printf ("ret =%d\n", ret);
return 0;
(GDB) bt
#0 sum (m=10, n=9) at Sum.c:5
#1 0x08048418 in Main () Sum.c:16 each time a function call is generated, a stack frame (stack frame) is created on the stack, which is a data
The unit is used to describe the function, describe the function's address, parameters, and the value of the function's local variables.
Using the BT command, you can display all of the call information for this stack.
As can be seen from the above display, the stack has two stack frames (stack frame), used to describe function main and function sum. The preceding #0 represents the label of the SUM function stack box. #1表示main函数栈框的标号. The latest stack box labeled 0.main function Stack box label Max.
(GDB) Frame 1 #1 0x08048418 in Main () at sum.c:16 ret = SUM (m,n); Frame 1 means Select the Stack box 1, which is the stack box with the main function selected, because I want to view the main function's information at this time.
(GDB) Info locals
m = 10
n = 9
RET = 0 This time you can view the value of the local variable in the main function stack box by using info locals.
-----------------------------------------------------------------------------------
Second, the recursive program is debugged using the GDB stack trace.
#include <stdio.h>
Long long func (int n)
{
int i = 0;
if (n > 20) {
printf ("N Too large!\n");
return-1;
}
if (n = = 0)
return 1;
else {
i = n * func (n-1);
return i;
}
}
int main (void)
{
Long long ret;
ret = func (10);
printf ("ret =%lld\n", ret);
return 0;
(GDB) bt
#0 func (n=7) at Test.c:7
#1 0x0804843f in func (n=8) at test.c:14
#2 0x0804843f in func (n=9) at test.c:14
#3 0x0804843f in func (n=10) at test.c:14
#4 0x08048469 in Main () test.c:22 as shown above, you can clearly see how recursion goes deep into the first layer, and the value of that layer's local variables.
---------------------------------------------------------------------------------
Three, GDB uses a manual to say how to view the stack, translated documents as follows:
GDB views the stack. RAR----------------------------------------------------------------------------------
Reference: http://www.ibm.com/developerworks/cn/linux/sdk/gdb/index.html