GDB debugging Basics

Debugging with GDB requires some advance settings in Ubuntu system.

1. Install gdb

Some versions of the system may not be installed this tool, you need to install it yourself.

cv@cv:~# sudo apt install gdb

2. Set the ulimit parameters

ulimit is a built-in feature of the Linux system, set limits for the use by the Shell process and its children it generates resources.

ulimit -a displays all the current limit information.

cv@cv:~# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 515120
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1048576
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

In the Ubuntu system, the default core file size is zero, I've already set up, shown as core file size (blocks, -c) unlimited 

If the display is 0, that does not produce core files in the event Segmentation Fault (core dump), not by the GDB debugger. -C set by parameters. After setting can re-use the above parameters -a view the results.

cv@cv:~# ulimit -c unlimited

3. Set core_pattern

After sending the changed parameter represents the core dump, what to do with the core file.

cv@cv:~# cat /proc/sys/kernel/core_pattern
core

If the display is related parameters apport, expressed in the Ubuntu system, handling mechanism for the core dump it as a bug is a bug check if the bug is reported on, in which case we can not use our program error debugger GDB .

|/usr/share/apport/apport %p %s %c %P

Can be modified to core. It can be achieved by the following operation.

cv@cv: ~# sudo service apport stop

4. Set the translation parameters CMAKE_BUILD_TYPE

If set to RELEASE mode, the core file GDB displays core dump is generated when an error occurs, you can find little information, only the function name problems arise, of course, generally can easily navigate to the error location.

cv@cv: ~# gdb main core.31126
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 main...(no debugging symbols found)...done.
[New LWP 31126]
Core was generated by `./main'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000401761 in github::Option::Run() ()
(gdb) bt
#0  0x0000000000401761 in github::Option::Run() ()
#1  0x0000000000400ff0 in main ()
(gdb) q

In contrast, if set to DEBUG mode, the information obtained even richer.

cv@cv: ~# gdb main core.30926
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 main...done.
[New LWP 30926]
Core was generated by `./main'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004021c5 in github::Option::Run (this=0x7fff6ea3f6b0) at /root/mvs_project/git_repo_for_trail/src/CacheFunction.cc:42
42              std::cout << root->next->val << std::endl;
(gdb) bt
#0  0x00000000004021c5 in github::Option::Run (this=0x7fff6ea3f6b0) at /root/mvs_project/git_repo_for_trail/src/CacheFunction.cc:42
#1  0x0000000000401333 in main (argc=1, argv=0x7fff6ea3f7f8) at /root/mvs_project/git_repo_for_trail/main.cc:11
(gdb) q

Note that explicit information on both the results into the red section. Here I explicitly Ubuntu 16.04 system as an example.

5. Start Debugging

For example, I now file structure as follows

cv@cv: ~/mvs_project/git_repo_for_trail/build# tree -L 2 ..
..
|-- CMakeLists.txt
|-- README
|-- build
|-- main.cc
`-- src
    |-- CMakeLists.txt
    |-- CacheFunction.cc
    `-- CacheFunction.h

Which CMakeLists.txt document reads as follows

#===============================================================================
# CMAKE VERSION
#===============================================================================
cmake_minimum_required(VERSION 3.5)


#===============================================================================
# PROJECT CONFIG
#===============================================================================
project(GIT_REPO)


#===============================================================================
# CMAKE CXX_STANDARD SET, CMAKE FLAGS AND CUDA SET
#===============================================================================
# when version of cmake is higher than 3.1 CMAKE_CXX_STANDARD is valid
# otherwise we can just use set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE DEBUG)


#===============================================================================
# INCLUDE DIRECTORIES CONFIGURATION
#===============================================================================
include_directories( src )


#===============================================================================
# SUB-DIRECTORY SET
#===============================================================================
add_subdirectory(src)
include_directories("src")


#===============================================================================
# SOURCE FILES IN THE UPPEST FOLDER (such as main.cc CacheFunction.h
#                     CacheFunction.cc)
#===============================================================================
aux_source_directory(./src/ DIR_SRC)


#===============================================================================
# EXECUTABLE PROGRAM SETTING
#===============================================================================
add_executable(main main.cc ${DIR_SRC})


#===============================================================================
# TARGET LINK LIBRARIES NEEDED
#===============================================================================
target_link_libraries(main src)

Error occurs when running compiled by

cv@cv: ~/mvs_project/git_repo_for_trail/build# ./main
The string elements are:
h e l l o   w o r l d !
5
Segmentation fault (core dumped)

If, after this time provided the steps above, in the build folder will produce the corresponding core document

cv@cv: ~/mvs_project/git_repo_for_trail/build# ll
total 284
drwxr-xr-x 4 root root   4096 Jan  4 05:31 ./
drwxr-xr-x 5 root root   4096 Jan  4 05:31 ../
-rw-r--r-- 1 root root  11638 Jan  4 05:31 CMakeCache.txt
drwxr-xr-x 5 root root   4096 Jan  4 05:31 CMakeFiles/
-rw-r--r-- 1 root root   5987 Jan  4 05:31 Makefile
-rw-r--r-- 1 root root   1573 Jan  4 05:31 cmake_install.cmake
-rw------- 1 root root 561152 Jan  4 05:31 core.31126
-rwxr-xr-x 1 root root  19136 Jan  4 05:31 main*
drwxr-xr-x 3 root root   4096 Jan  4 05:31 src/

Thus, we can enter the GDB debugger

cv@cv: ~/mvs_project/git_repo_for_trail/build# gdb main core.31126

In GDB environment, use the above instructions and their meaning.

backtrace (or bt) view all levels of function calls and parameters 

list (or l) lists the source code, then the last position to the following, each row 10 column 

quit (or q) quit gdb debugging environment 

start running, the stop in front of the main function of the first command-line statement to wait

More commands and follow-up skills to be supplemented.


reference

[1]  to improve the system performance by ulimit

[2]  for the core dump debug ubuntu

[3] Debugging a segmentation fault using gdb

[4]  use and gdb debugger command summary

[5] gdb Debugging Full Example (Tutorial): ncurses

[6] CppCon 2015: Greg Law 'Give me 15 minutes & I'll change your view of GDB'

Guess you like

Origin www.cnblogs.com/phillee/p/12149030.html