Debugging Segmentation Fault

We are going to use gdb to figure out why the following program causes a segmentation fault. The program is meant to read in a line of text from the user and print it. However, we will see that in it‘s current state it doesn‘t work as expected...

1 : #include <stdio.h>
2 : #include <stdlib.h>

3 : int main(int argc, char **argv)
4 : {
5 :   char *buf;
6 :
7 :   buf = malloc(1<<31);
8 :
9 :   fgets(buf, 1024, stdin);
10:   printf("%s\n", buf);
11:
12:   return 1;
13: }


The first step is to compile the program with debugging flags:

prompt> gcc -g segfault.c

Now we run the program:

prompt > a.out
Hello World!
Segmentation fault
prompt >

This is not what we want. Time to fire up gdb:

prompt > gdb a.out
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb)

We‘ll just run it and see what happens:

(gdb) run
Starting program: /home/dgawd/cpsc/363/a.out
test string

Program received signal SIGSEGV, Segmentation fault.
0x4007fc13 in _IO_getline_info () from /lib/libc.so.6

So we received the SIGSEGV signal from the operating system. This means that we tried to access an invalid memory address. Let‘s take a backtrace:

(gdb) backtrace
#0  0x4007fc13 in _IO_getline_info () from /lib/libc.so.6
#1  0x4007fb6c in _IO_getline () from /lib/libc.so.6
#2  0x4007ef51 in fgets () from /lib/libc.so.6
#3  0x80484b2 in main (argc=1, argv=0xbffffaf4) at segfault.c:10
#4  0x40037f5c in __libc_start_main () from /lib/libc.so.6

We are only interested in our own code here, so we want to switch to stack frame 3 and see where the program crashed:

(gdb) frame 3
#3  0x80484b2 in main (argc=1, argv=0xbffffaf4) at segfault.c:10
10        fgets(buf, 1024, stdin)

We crashed inside the call to fgets. In general, we can assume that library functions such as fgets work properly (if this isn‘t the case, we are in a lot of trouble). So the problem must be one of our arguments. You may not know that ‘stdin‘ is a global variable that is created by the stdio libraries. So we can assume this one is ok. That leaves us with ‘buf‘:

(gdb) print buf
$1 = 0x0

The value of buf is 0x0, which is the NULL pointer. This is not what we want - buf should point to the memory we allocated on line 8. So we‘re going to have to find out what happened there. First we want to kill the currently-running invocation of our program:

(gdb) kill
Kill the program being debugged? (y or n) y

Now set a breakpoint on line 8:

(gdb) break segfault.c:8
Breakpoint 1 at 0x8048486: file segfault.c, line 8.

Now run the program again:

(gdb) run
Starting program: /home/dgawd/cpsc/363/a.out 

Breakpoint 1, main (argc=1, argv=0xbffffaf4) at segfault.c:8
8         buf = malloc(1<<31);

We‘re going to check the value of buf before the malloc call. Since buf wasn‘t initialized, the value should be garbage, and it is:

(gdb) print buf
$2 = 0xbffffaa8 "Èúÿ¿#\177\[email protected]`\[email protected]\001"

Now step over the malloc call and examine buf again:

(gdb) next
10        fgets(buf, 1024, stdin);
(gdb) print buf
$3 = 0x0

After the call to malloc, buf is NULL. If you were to go check the man page for malloc, you would discover that malloc returns NULL when it cannot allocate the amount of memory requested. So our malloc must have failed. Let‘s go back and look at it again:

7 :   buf = malloc(1<<31);

Well, the value of the expression 1 << 31 (the integer 1 right-shifted 31 times) is 429497295, or 4GB (gigabytes). Very few machines have this kind of memory - mine only has 256MB. So of cousre malloc would fail. Furthermore, we are only reading in 1024 bytes in the fgets call. All that extra space would be wasted, even if we could allocate it. Change the 1<<31 to 1024 (or 1<<9), and the program will work as expected:

prompt >
Hello World!
Hello World!

prompt >

So now you know how to debug segmentation faults with gdb. This is extremely useful (I use it more often then I care to admit). The example also illustrated another very important point: ALWAYS CHECK THE RETURN VALUE OF MALLOC! Have a nice day.

From: http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html

时间: 2024-10-09 08:10:05

Debugging Segmentation Fault的相关文章

linux Ubuntu(Segmentation fault)段错误出现原因及调试方法

  在linux下编译了一个程序,尝试运行的时候出现: Segmentation fault (core dumped) 初步确认为...完全不知道是什么玩意. 于是找度娘了. ---------------------------------------------------------------------------- 出现原因 原来这个东西叫做段错误,就程序运行的时候出现内存错误.有很多原因会导致这样的内存错误,但是应该把这些问题归结于程序的错误,那么程序是出现了什么样的错误了呢,为

arpa/inet.h所引起的Segmentation fault及网络编程常见的头文件

最近在学习Linux网络编程方面的知识,感觉还是有些困难.主要是对协议过程的理解,还有socket的API的理解不够深刻.今天复习编写了一个TCP的服务端和客户端的程序实现client.c从命令行参数中获得一个字符串发给服务器,然后接收服务器返回的已处理的字符串并打印. server.c 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <unistd.h>

函数栈溢出引起的段错误segmentation fault

遇到了一个奇怪的问题: 有一个回调函数中发生了段错误,但经检查也没有什么明显的错误,然后用排除法一点一点屏蔽,最后定位在一个函数里出错,但这个函数没什么明显错误.最后把入口参数改为引用传递就不报错误. 但隔了一段时间这个函数又报错了,原因是我加一行代码,但这行代码就是一个赋值语句:于是我不甘心,又开始排除法,最后定位到一个变量,加上它报错,不加就不报错:我一直怀疑是不是linux对一个函数的大小有限制:于是将这个函数换成全局变量,而在此函数中用的此变量时候采用指针,诶,不再报段错误了,世界终于安

Segmentation Fault的一种定位方法

1. 介绍 网上有很多Segmentation Fault的调试方法,下面这篇文件就很好 <Linux环境下段错误的产生原因及调试方法小结> 然而笔者在实际的使用中由于各种原因总觉得不够用 于是在网上找到了一种个人觉得更直接的方法 2. 原理 Segmentation Fault发生的时候,程序收到了信号11(SIGSEGV) 我们捕获该信号,然后利用事故发生时保存的寄存器现场加上栈信息定位到具体行 这里参考了< C/C++捕获段错误,打印出错的具体位置(精确到哪一行) > 使用其

结构体指针之 段错误 具体解释(segmentation fault)

一个网友问了我一个问题.一个C程序执行出现了段错误,这个问题非常好.非常多刚開始学习的人都easy犯这个错误,详细代码例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 这个编译没有问题,可是执行是段错误    Segmentation fault 由于你定义了一个结构体指针p.用来指向此类

app打包时报错unable to execute command: Segmentation fault: 11

问题:打包时报错   clang: error:unable to execute command: Segmentation fault: 11 clang: error:linker command failed due to signal (use -v to see invocation) app在上架的时候遇到这个报错问题,一开始以为是证书或者是配置描述文件哪里出错了.作为新手,在试了好多方法依然无法解决之后,只能无奈按照app上架流程又完完全全的重新过了一遍,在点击Archive后的

记一次PHP“Segmentation fault”调试经历

遇到的问题: 在linux上安装php5.5.26.phalcon2.0扩展.xhprof扩展,均正常安装,并可单独运行.但放在一起运行时出现“Segmentation fault”错误.注:xhprof是从github上下载的最新版. 遇到问题时的上下文: 1: 新建项目,使用phalcon命令行工具 phalcon create-project store 2:修改入口文件,加入xhprof支持 3:web访问出现502,无任何php级别的错误日志 在终端下执行:php index.php

strcat函数造成的段错误(Segmentation fault)

转自:http://book.51cto.com/art/201311/419441.htm 3.21  strcat函数造成的段错误 代码示例 int main() { char dest[7]="12345"; char* src = "abcdefghigklmnopqrstuvwxyz"; strcat(dest, src); cout << "dest:" << dest << endl; retur

TQ2440实现触摸屏和qt图形 解决segmentation fault

使用触摸屏,首先安装触摸屏矫正程序. 下载并解压tslib-1.4,进入主目录,执行: 1 [[email protected] ~]#./autogen.sh 2 [[email protected] ~]#echo "ac_cv_func_malloc_0_nonnull=yes" >arm-none-linux-gnueabi.cache 3 [[email protected] ~]#./configure --host=arm-linux --cache-file=ar