Debugging program segment errors with core dump

Source: Internet
Author: User

Some programs can be compiled, but the segment fault (segment error) will occur during running ). this is usually caused by pointer errors. however, this does not prompt the file-> line like a compilation error, but does not have any information, making debugging difficult.

 

1. In general Linux systems, core dump files are not generated by default. You can use ulimit-C to view the size of the core dump file. Generally, the starting value is 0.
You can set the size of the core file, ulimit-C 1024 (Kbytes unit) or ulimit-C unlimited. (Note: Using-C unlimited does not limit the core file size)
2. Generate the core file: $ sudo sh-c "ulimit-C unlimited;./main_arm" # main_arm is the executable file name.
3. Remember to add-g for debugging during compilation. Be sure not to add the optimization option-OS (or-O0-O1 ....)
4. Use GDB to view the core file:
GDB [exec file] [core file]
For example, GDB main_arm Core
After entering GDB, use the BT or where command to view backtrace to check where the program is running and locate the file line of core dump.
5. Example:

[HTML]View plaincopy
  1. An error occurs when a program cm_test_tool is running and a core file is generated as follows:
  2. -RW-r-1 root cm_test_tool.c
  3. -RW-r-1 root cm_test_tool.o
  4. -Rwxr-XR-x 1 root cm_test_tool
  5. -RW --- 1 root core.19344
  6. -RW --- 1 root core.19351
  7. -RW-r-1 root cm_test_tool.cfg
  8. -RW-r-1 root cm_test_tool.res
  9. -RW-r-1 root cm_test_tool.log
  10. [[Email protected] _ sim2 mam 2cm] #
  11. You can use the command GDB for search. The parameter 1 is the name of the application, and the parameter 2 is the core file. Run
  12. The result of GDB cm_test_tool core.19344 is as follows:
  13. [[Email protected] _ sim2 mam 2cm] # GDB cm_test_tool core.19344
  14. Gnu gdb Red Hat Linux (5.2.1-4)
  15. Copyright 2002 Free Software Foundation, Inc.
  16. GDB is free software, covered by the GNU General Public License, and you are
  17. Welcome to change it and/or distribute copies of it under certain conditions.
  18. Type "show copying" to see the conditions.
  19. There is absolutely no warranty for GDB. Type "show warranty" for details.
  20. This GDB was configured as "i386-redhat-linux "...
  21. Core was generated by './cm_test_tool '.
  22. Program terminated with signal 11, segmentation fault.
  23. Reading symbols from/lib/i686/libpthread. so.0... Done.
  24. Loaded symbols for/lib/i686/libpthread. so.0
  25. Reading symbols from/lib/i686/libm. so.6... Done.
  26. Loaded symbols for/lib/i686/libm. so.6
  27. Reading symbols from/usr/lib/libz. so.1... Done.
  28. Loaded symbols for/usr/lib/libz. so.1
  29. Reading symbols from/usr/lib/libstdc ++. so.5... Done.
  30. Loaded symbols for/usr/lib/libstdc ++. so.5
  31. Reading symbols from/lib/i686/libc. so.6... Done.
  32. Loaded symbols for/lib/i686/libc. so.6
  33. Reading symbols from/lib/libgcc_s.so.1... Done.
  34. Loaded symbols for/lib/libgcc_s.so.1
  35. Reading symbols from/lib/ld-linux.so.2... Done.
  36. Loaded symbols for/lib/ld-linux.so.2
  37. Reading symbols from/lib/libnss_files.so.2... Done.
  38. Loaded symbols for/lib/libnss_files.so.2
  39. #0 0 × 4202cec1 in _ strtoul_internal () from/lib/i686/libc. so.6
  40. (GDB)
  41. Enter the gdb prompt, enter where, and find the location and stack of the error, as shown below:
  42. (GDB) Where
  43. #0 0 × 4202cec1 in _ strtoul_internal () from/lib/i686/libc. so.6
  44. #1 0 × 4202d4e7 in strtoul () from/lib/i686/libc. so.6
  45. #2 0 × 0804b4da in getmaxidfromdb (get_type = 2, max_id = 0 × 806fd20) at cm_test_tool.c: 788
  46. #3 0 × 0804b9d7 in constrctvodprogram (vod_program = 0 × 40345bdc) at cm_test_tool.c: 946
  47. #4 0 × 0804a2f4 in tvrequestthread (ARG = 0 × 0) at cm_test_tool.c: 372
  48. #5 0x40021941 in pthread_start_thread () from/lib/i686/libpthread. so.0
  49. (GDB)

At this point, we can see that the location of the file error is the getmaxidfromdb function. The two parameters are 2 and 0 × 806fd20, respectively. This function is located in the source code line 788. Based on this, we can find and solve the root cause of the problem.

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.