Coredump of C code

Source: Internet
Author: User

Key points:

After the program ends with an exception, the core code file program is generated to add the-g compilation option, which contains debugging information to locate the exception points in combination with the gdb and coredump files.


After the program ends abnormally, The coredump file is generated. The automatically generated coredump file varies with systems and settings.


Sample Code:

#include 
 
  int core_dump() {int i;for (i = 5; i >= 0; i--) {printf("(%d, %d)\n", i, 100 / i);}return 0;}int main() {core_dump();return 0;}
 

The above code has an exception except 0. Compile and run the Code:

gcc main.c./a.out


Running result:

(5, 20)(4, 25)(3, 33)(2, 50)(1, 100)[1]    9662 floating point exception (core dumped)  ./a.out


To locate code errors more clearly, you can use gdb to analyze the coredump file:

~/examples/cpp/core_dump % gdb ./a.out coreGNU gdb (GDB) 7.7Copyright (C) 2014 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later 
 
  This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "i686-pc-linux-gnu".Type "show configuration" for configuration details.For bug reporting instructions, please see:
  .Find the GDB manual and other documentation resources online at:
  .For help, type "help".Type "apropos word" to search for commands related to "word"...Reading symbols from ./a.out...done.[New LWP 9662]warning: Could not load shared library symbols for linux-gate.so.1.Do you need "set solib-search-path" or "set sysroot"?Core was generated by `./a.out'.Program terminated with signal SIGFPE, Arithmetic exception.#0  0x08048415 in core_dump ()(gdb) where#0  0x08048415 in core_dump ()#1  0x0804844b in main ()(gdb) q
 

We can see that the specific code and line number are not provided above, because the-g compilation option needs to be added. For this reason, the coredumnp file provides comprehensive information:

~/examples/cpp/core_dump % gdb ./a.out coreGNU gdb (GDB) 7.7Copyright (C) 2014 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later 
 
  This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "i686-pc-linux-gnu".Type "show configuration" for configuration details.For bug reporting instructions, please see:
  .Find the GDB manual and other documentation resources online at:
  .For help, type "help".Type "apropos word" to search for commands related to "word"...Reading symbols from ./a.out...done.[New LWP 9758]warning: Could not load shared library symbols for linux-gate.so.1.Do you need "set solib-search-path" or "set sysroot"?Core was generated by `./a.out'.Program terminated with signal SIGFPE, Arithmetic exception.#0  0x08048415 in core_dump () at main.c:77                       printf("(%d, %d)\n", i, 100 / i);(gdb) where#0  0x08048415 in core_dump () at main.c:7#1  0x0804844b in main () at main.c:14(gdb) 
 

Supplement:

Of course, not all exceptions generate a coredump file. For details, see chapter 10 of Unix advanced programming (2nd. In some environments, the default value of coredump is 0 bytes. Therefore, ulimit-c 1024 (file size) is required ).


Related Article

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.