#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.
分享到:
相关推荐
gdb调试信息堆栈信息gdb调试信息堆栈信息gdb调试信息堆栈信息gdb调试信息堆栈信息gdb调试信息堆栈信息
如果工程很大,头文件很多,而有几个头文件又经常要用的,那么: 1、把这些头文件全部写到一个头文件中,比如:preh.h 2、写一个preh.c,里面的包含库文件,只要一句话#include"preh.h" 3、对于preh.c,在project ...
通过列举linux平台下的例子,并结合gdb描述了堆栈溢的过程。
比较全面的GDB调试使用文档。 GDB概述 使用GDB GDB的命令概貌 GDB中运行UNIX的shell程序 在GDB中运行程序 调试已运行的程序 暂停 / 恢复程序运行 一、设置断点(BreakPoint) 二、设置观察点(WatchPoint)...
堆栈检查器 一个gdb命令,用于检查堆栈上对象的大小。 如何 使用gdb导航到特定的堆栈框架(运行直到堆栈溢出或在某处设置断点)。 然后,只需运行: source stack -inspector.py stack -inspector
(gdb) bt:查看函数堆栈 (gdb) finish:退出函数 (gdb) shell 命令行:执行shell命令行 (gdb) set args 参数:指定运行时的参数 (gdb) show args:查看设置好的参数 (gdb) show paths:查看程序运行路径; set ...
(gdb) bt //backtrace 打印堆栈 (gdb) break xx.cpp:xx thread all //在所有线程上打断点 (gdb) thread apply ID1 ID2 command //让一个或多个线程执行GDB命令 (gdb) set scheduler-locking off|on|step //只有当前...
gdb调试中,具有调用关系的函数在堆栈中的关系。
(gdb) bt:查看函数堆栈 (gdb) finish:退出函数 (gdb) shell 命令行:执行shell命令行 (gdb) set args 参数:指定运行时的参数 (gdb) show args:查看设置好的参数 (gdb) show paths:查看程序运行路径; set ...
GDB提供了很多调试功能,包括设置断点、单步执行、查看变量、回溯函数调用堆栈等。通过这些功能,程序员可以有效地诊断和修复程序中的错误。 在使用GDB时,您需要在终端或控制台中启动您的程序,并使用以下命令之一...
gdb调试常用命令总结, gdb调试线程挂住问题打印堆栈信息等
本文档详细描述了在实际工程调试过程中如何使用gdb来分析core文件。结合实际中出现的一些问题,配图描述了如何通过gdb工具从堆栈,回溯等信息判断出来问题所在。对实际工程调试起到一定指导作用
linux使用gdb调试方法详解,包含 GDB 命令、在 GDB 里运行程序的例子,如何打断点和继续运行、查看堆栈信息、检查源文件,查看内存信息和各种数据
通常情况下,corefile文件会包含了程序运行时的内存,寄存器状态,堆栈指针,内存管理信息还有各种函数调用堆栈信息等,我们可以理解为是程序工作当前状态存储生成的一个文件,许多的程序出错的时候都会产生一个core...
基于Linux64位的多线程小程序,答案就在某个线程的堆栈中,请使用gdb命令查看哈
gdb调试 GDB(GNU 调试器)**是一种强大的命令行调试器,用于调试 C、C++、Fortran 和汇编语言程序。它允许开发人员逐步执行程序,检查变量的值...**分析调用堆栈:**使用 backtrace 和 where 命令分析程序的调用堆栈。
gdb使用速查包括:通过gdb启动程序,设置断点,跟踪执行,调用堆栈,查看信息,多线程,多进程 等
栈溢出获取shell 用gdb调试缓冲区溢出 初尝Linux栈溢出 用gdb调试缓冲区溢出 栈的观察
基于浏览器的gdb前端(gnu调试器) 文档: : 源代码: :
gdb test 进入调试 需要调试的地方打下断点,run运行到断点处。 r 运行到断点处,info thread可以查看被调试的线程。 thread apply all bt 让所有线程打印堆栈信息 set scheduler-locking off|on|step 在使用...