[LINUX] Debugging C / C ++ with gdb

Segmentation fault (core dump)

This message is almost always annoying when writing C or C ++ This is one of the places where beginners can easily stumble because the cause of the error is not output directly to the terminal.

Occurrence of core dump

Lambda:11	Em:1.01	ARI:0
Lambda:11	Em:1.02	ARI:0.290903
Segmentation fault (Core dump)

A Segmentation fault (core dump) occurs while executing Program for clustering (unsupervised machine learning) .. Suddenly the program stopped. A tool called gdb can help you find the cause.

What is gdb

https://ja.wikipedia.org/wiki/GNU%E3%83%87%E3%83%90%E3%83%83%E3%82%AC

The GNU Debugger (also just GDB) is a standard debugger that runs on GNU software systems. It is a highly portable debugger that works on many Unix-like systems and supports programming languages such as Ada, C, C ++, FORTRAN, and FreeBASIC.

gdb is a standard Unix CUI debugger. IDEs such as eclipse and Visual Studio have a GUI debugger as standard, but their purpose is similar to those.

how to use

First, compile with "-g" added to the compile option.

g++ [file name] -g -o

Core Dump features are disabled by default on many Linux distributions, so enable them.

ulimit -c unlimited

The meaning of this command is do not limit the size of the core to be generated.

Run the program.

./a.out
...
(abridgement)
...
Lambda:11	Em:1.01	ARI:0
Lambda:11	Em:1.02	ARI:0.290903
Segmentation fault (Core dump)

If you look at the current directory here, you should see a file named "core". Let's debug this core with GDB.

gdb ./a.out core

GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
[New LWP 10366]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00005599dc02eccb in Hcm::set_crispMembership (this=0x7ffcf0375ea0) at hcm.cxx:159
159	    CrispMembership[max_index][k]=1.0;

Here, the name of the function causing the problem is written at the bottom.

I want to take a closer look, so I'll backtrace.

(gdb) backtrace
#0  0x00005599dc02eccb in Hcm::set_crispMembership (this=0x7ffcf0375ea0) at hcm.cxx:159
#1  0x00005599dc03c300 in main () at qfcma_main_user_knowledge.cxx:112

I want to see the cause of the main function in detail, so specify # 1.

(gdb) frame 1
112	      test.set_crispMembership();

Now you have identified the function that caused the core dump.

You can also view the code for the cause and local variables as needed. Please refer to the following article for details.

More detailed articles about gdb

https://qiita.com/ShikiSumiya/items/a08363db88f21c81d351

Postscript

The cause of this core dump was that NaN was generated due to the calculation limit of the double type variable, and an out-of-array reference occurred as a result of trying to refer to the NaN stored in the array.

Recommended Posts

Debugging C / C ++ with gdb
Container-like # 1 made with C
json parse with gdb
Container-like # 2 made with C
Debugging pipelines with metaflow
Easy debugging with ipdb
Using X11 with ubuntu18.04 (C)
ABC163 C problem with python3
Try Google Mock with C
Debugging with pdb in Python
Useful when debugging with TouchDesigner
Format C source with pycparser
Remote debugging with Visual Studio 2017
ABC188 C problem with python3
ABC187 C problem with python
Solve ABC163 A ~ C with Python
Call C from Python with DragonFFI
Create Awaitable with Python / C API
Writing C language with Sympy (metaprogramming)
Solve ABC168 A ~ C with Python
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Segfault with 16 characters in C language
Speed up C / C ++ compilation with ccache
X86 assembler on Linux (linkage with C)
Easy C / C ++ multilingual binding with CMake + SWIG
[C] [python] Read with AquesTalk on Linux
Use C ++ functions from python with pybind11
RaspberryPi L Chika with Python and C #
[C, C ++, Python, JavaScript] L Chika with Edison
Communicate with I2C devices in Linux C