Debugging Segmentation Fault

Source: Internet
Author: User

We is going to the use of gdb to figure out what the following program causes a segmentation fault. The program was meant to read in a line of text from the user and print it. However, we'll see this 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:

This isn't what we want. Time to fire up GDB:

prompt > gdb a.outGNU gdb 5.0Copyright 2000 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome 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.  

We ' ll just run it and see what happens:

(gdb) runStarting program: /home/dgawd/cpsc/363/a.out test stringProgram 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, 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. 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:1010        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're in a lot of T Rouble). So the problem must is one of our arguments. Know that ' stdin ' are a global variable that's created by the Stdio libraries. So we can assume the 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 isn't what we want-buf should point to the memory we allocated on line 8. So we ' re going to has to find the what happened there. First we want to kill the currently-running invocation of our program:

(gdb) killKill the program being debugged? (y or n) y

Now set a breakpoint in line 8:

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

Now run the program again:

(gdb) runStarting program: /home/dgawd/cpsc/363/a.out Breakpoint 1, main (argc=1, argv=0xbffffaf4) at segfault.c:88         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 being garbage, and it is:

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

Now step through the malloc call and examine buf again:

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

After the call to malloc, BUF is NULL. If you were to go check the Mans page for malloc, you would discover that malloc returns NULL when it cannot allocate the A Mount of memory requested. So our malloc must has failed. Let's go back and look at it again:

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

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

prompt >Hello World!Hello World!prompt >

So now you know the debug segmentation faults with GDB. This is the extremely useful (I use it more often and I care to admit). The example also illustrated another very important point:always CHECK the RETURN VALUE of malloc! There is a nice day.

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

Debugging Segmentation Fault

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.