Use GDB to debug

What is GDB?

GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.
https://sourceware.org/gdb/onlinedocs/gdb/index.html

Off-line Debug

When you got segmentation fault, you can use gdb to load the core dump file to debug what happened.
But Linux default did not enable core dump file, you will need to enable it by the following command.
Beware, ulimit command will set the core dump file size to unlimited, and it will be only enabled in the current shell.
ulimit -c unlimited
sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t
Compile your program with debug symbols, add "-g" to compile options.
g++ -g -std=c++11 -o myprogram myprogram.cpp `pkg-config opencv --cflags --libs`
Now, run your program again, then you will find the core dump file under /tmp
Ex: /tmp/core-myprogram.17521.evan-desktop.1565936379
Use gdb to load the binary (with symbol) and core dump file
gdb myprogram /tmp/coredump
You will see the message like followings:
$ gdb ./opencv_gstreamer_csi_camera /tmp/core-opencv_gstreame.12124.evan-desktop.1565941066
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 "aarch64-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 ./opencv_gstreamer_csi_camera...done.

warning: core file may not match specified executable file.
[New LWP 12124]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
Core was generated by `./opencv_gstreamer_csi_camera myfile'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  strlen () at ../sysdeps/aarch64/strlen.S:94
94 ../sysdeps/aarch64/strlen.S: No such file or directory.
(gdb)
Use 'bt' to print the backtrace
(gdb) bt
#0  0x0000007f9fb86450 in strlen () at ../sysdeps/aarch64/strlen.S:94
#1  0x0000007f9fdabdc4 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*) ()
    at /usr/lib/aarch64-linux-gnu/libstdc++.so.6
#2  0x0000005576eff184 in main(int, char**) (argc=2, argv=0x7fd85bcf98)
    at opencv_gstreamer_csi_camera.cpp:80
switch to frame 2 to see what happened in line 80
(gdb) f 2
#2  0x0000005576eff184 in main (argc=2, argv=0x7fd85bcf98)
    at opencv_gstreamer_csi_camera.cpp:80
80             filename = argv[++i];
use 'p' to print variable you interested
(gdb) p i
$1 = 2
(gdb) p filename
$2 = "/media/evan/USB/CLIP2564_1900_100_50_64.AVI"
If your program is multi-threading, use 't' to list and switch to other thread
(gdb) t
[Current thread is 1 (Thread 0x7f99493010 (LWP 12124))]
(gdb) t 1
[Switching to thread 1 (Thread 0x7f99493010 (LWP 12124))]
#2  0x0000005576eff184 in main (argc=2, argv=0x7fd85bcf98)
    at opencv_gstreamer_csi_camera.cpp:80
80             filename = argv[++i];
Don't forget to use google to find more useful gdb commands

On-Line debug

Please refer to this page.

Reference

https://www.gnu.org/software/gdb/

留言

張貼留言

這個網誌中的熱門文章

以樂透為例,用Python統計馬可夫矩陣

將 Jenkins Job 的歷史結果整理出視覺化的 Daily Report mail (一)

如何用 Jenkins API 取得 Job Build Result