[Segfault] Nasty section error How to debug "Segment Fault" on Linux

Source: Internet
Author: User
Tags assert

Overview

We can get segfault by several reasons:

    • Aligned access to unaligned memory (usally see in ARM NEON)
    • Cross-border Access

      int temp[2] = {0};temp[22//  Segfault
    • Write on read-only access

      Char " Haha " ; temp[12//  Segfault
    • Others
How to find which code line results in Segfaultstep 1:debug

DEBUG flavour usually add some assert () statements about the memory alignment access.

Step 2:using GDB

If DEBUG flavour did not give any assert report, or you have a fixed all of the Assert () report, but it still aborts with SEGFA Ult. What ' s next?

Usually, we can add "-g" compiler flag and rebuild the executable, then use GDB to locate where is the Segfault.

1 /*main.c*/2#include <stdio.h>3#include <stdlib.h>4 extern intF0 ();5 extern intF2 ();6 extern intf4 ();7 intF1 ();8 intf3 ();9 intMain ()Ten { Oneprintf"Test for segfault.\n"); A     returnf4 (); - } - intF1 () the { -     returnF0 (); - } - intf3 () + { -     returnF2 (); + } A   at /*segfault.c*/ -#include <stdio.h> - CharF0 () - { -     Char*tmp ="Haha"; -tmp[0] ='h'; in     returntmp[4]; - } to CharF2 () + { -     returnF1 (); the } * Charf4 () $ {Panax Notoginseng     returnf3 (); -}
Example Code

Build and run on Terminal:

gcc main.c segfault.c-o segfault.x$. / for segfault.segmentation fault (core dumped)

Then with GDB:

$GCC-g-o3 MAIN.C segfault.c-o segfault.x$ gdb./segfault.xgnu gdb (Ubuntu7.7-0ubuntu3.1)7.7Copyright (C) theFree Software Foundation, Inc. (GDB) rstarting program:/home/jxion/jxion_porting_server/users_jxion/test_segfault/segfault.xtest forSegfault.program received signal SIGSEGV, segmentation fault.f0 () at segfault.c:66tmp[0] ='h';(gdb) bt#0F0 () at segfault.c:6#1  0x0000000000400557 inchF1 () at MAIN.C: +#2  0x000000000040058b inchF2 () at segfault.c: A#3  0x0000000000400567 inchF3 () at MAIN.C: -#4  0x000000000040059b inchF4 () at segfault.c: -#5  0x00007ffff7a35ec5 inch__libc_start_main (main=0x400440<main>, argc=1, argv=0x7fffffffd6f8, init=<optimized Out>, fini=<optimized out>, rtld_fini=<optimized Out>, stack_end=0x7fffffffd6e8) at libc-START.C:287#6  0x0000000000400482 inch_start () (GDB)

Now you can get the all info to you need.

[Segfault] Nasty section error How to debug "Segment Fault" on Linux

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.