GDB的堆栈

本文介绍如何使用GDB调试器进行程序调试,包括设置断点、查看变量值及跟踪调用堆栈等操作。同时展示了如何通过生成核心转储文件来定位和解决程序崩溃的问题。
AI助手已提取文章相关产品:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX (1UL << 20)
typedef unsigned long long u64;
typedef unsigned int u32;
u32 max_addend=MAX;
u64 sum_till_MAX(u32 n)
{
u64 sum;
n++;
sum=n;
if(n<max_addend)
sum+=sum_till_MAX(n);
return sum;
}
int main(int argc,char** argv){
u64 sum=0;
if((argc==2) && isdigit(*(argv[1])))
max_addend=strtoul(argv[1],NULL,0);
if(max_addend>MAX||max_addend==0){
fprintf(stderr,"Invalid number is specified by haoningge \n");
return 1;
}
sum=sum_till_MAX(0);
printf("sum(0..%lu)=%llu\n",max_addend,sum);
return 0;
}
~

运行

root@ubuntu:~/gdb# ./sum 10
sum(0..10)=55
root@ubuntu:~/gdb#


在gdb中运行

r 10
或者
set args 10
运行带的参数,如果直接r会段错误,因为没有传值

bt
看堆栈

i r eip ebp
info register eip ebp
看寄存器的值

x/40w $sp
看栈顶指针




http://hi.baidu.com/lihui_lihux/item/b1d2e71071db8f081994ec31
---------------------------------


Let us write a program which will generate a core dump.

  #include <iostream>

using namespace std;

int divint(int, int);

int main() {
int x = 5, y = 2;
cout << divint(x, y);
x =3; y = 0;
cout << divint(x, y);
return 0;
}

int divint(int a, int b)
{
return a / b;
}


To enable debugging, the program must be compiled with the -g option.

$g++ -g crash.cc -o crash
NOTE: We are using g++ compiler because we have used C++ source code.

Now, when run this program on your linux machine, it produces the result:

Floating point exception (core dumped)
You will find a core file in your current directory.

Now to debug the problem, start gdb debugger at command prompt:

$gdb crash
# Gdb prints summary information and then the (gdb) prompt

(gdb) r
Program received signal SIGFPE, Arithmetic exception.
0x08048681 in divint(int, int) (a=3, b=0) at crash.cc:21
21 return a / b;
# 'r' runs the program inside the debugger
# In this case the program crashed and gdb prints out some
# relevant information. In particular, it crashed trying
# to execute line 21 of crash.cc. The function parameters
# 'a' and 'b' had values 3 and 0 respectively.

(gdb) l
# l is short for 'list'. Useful for seeing the context of
# the crash, lists code lines near around 21 of crash.cc

(gdb) where
#0 0x08048681 in divint(int, int) (a=3, b=0) at crash.cc:21
#1 0x08048654 in main () at crash.cc:13
# Equivalent to 'bt' or backtrace. Produces what is known
# as a 'stack trace'. Read this as follows: The crash occurred
# in the function divint at line 21 of crash.cc. This, in turn,
# was called from the function main at line 13 of crash.cc

(gdb) up
# Move from the default level '0' of the stack trace up one level
# to level 1.

(gdb) list
# list now lists the code lines near line 13 of crash.cc

(gdb) p x

# print the value of the local (to main) variable x
In this example, it is fairly obvious that the crash occurs because of the attempt to divide an integer by 0.

To debug a program 'crash' that has crashed and produced a core file named 'core', type the following at the command line:

   gdb crash core 

As this is mostly equivalent to starting gdb and typing the 'r' command, all of the commands above could now be used to debug the file.

您可能感兴趣的与本文相关内容

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值