Basic use of GDB under ubuntu
GDB is a powerful program debugging tool based on the command line under the UNIX/LINUX operating system released by the GNU open source organization. Can be used to debug C, C + + program.
GDB installation
# 安装
sudo apt-get install gdb
View dgb version number
# 查看版本
gdb --version
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".
Basic use of GDB
Create a cpp test file
#include <iostream>
#include <stdio.h>
void ShowRevertNum(int iNum) {
while (iNum > 10)
{
printf("%d", iNum % 10);
iNum = iNum / 10;
}
printf("%d\n", iNum);
}
int main() {
std::cout<<"Hello world!"<<std::endl;
int iNum;
printf("Please input a number :");
scanf("%d", &iNum);
printf("After revert : ");
ShowRevertNum(iNum);
return 0;
}
compile cpp
g++ -o myHello -g ./main.cpp
gdb
Execute debugging using
gdb myHello
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 myHello...done.
(gdb)
After execution, the console cursor stops at (gdb)
the end, and you can start interactive debugging at this time:
The basic debugging commands are as follows
command function | Order | command full name |
---|---|---|
load an executable | file <filename> |
- |
view code | l orl <行号> |
list <行号> |
set breakpoint | b <行号> |
break <行号> |
start operation | r |
run |
Execute the next breakpoint: | c |
continue |
Next step | n |
next |
view variables | p <变量名> |
print <变量名> |
view type | whatis <变量名> |
- |
delete breakpoint | d <行号> |
delete <行号> |
clear all breakpoints | clear |
- |
exit debug | quit |
- |
CMake configuration DGB debugging
How to CMakeLists.txt
set
SET(CMAKE_BUILD_TYPE "Debug")
# 添加对gdb的支持
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
To set the Debug mode,
CMAKE_BUILD_TYPE
you can specify it when executing the cmake command, withoutCMakeLists.txt
writing it hard, as follows:
cmake .. -DCMAKE_BUILD_TYPE=Debug
You can also use the add_compile_options command, which has the same effect as setting CMAKE_CXX_FLAGS or CMAKE_C_FLAGS;
add_compile_options is for all compilers;
Functional Explanation of Related Option Arguments
- -w -W -Wall
-w 关闭编译警告。平时编写c/c++代码如果不规范,编译的时候会抛出很多警告。但是一般的警告都是可以忽略的,比如类型转换。编译的时候可以加-w关闭警告
-W 也是关闭编译警告,但是比-w智能一些,它只会显示编辑器认为会出错的警告
-Wall, 显示所有警告。
- -g -g3
gcc 支持4中级别的调试信息
-g0表示不生成调试信息
-g3表示生成最多的调试信息
-g默认为-g2。
一般的调试信息包括行号,函数,外部变量。-g3包含其他额外的调试信息,比如宏定义。
- -O0 -O1 -O2 -O3 -Os
-O系列选项主要用于优化代码。
-O0 不优化
-O和-O1是等价的,不影响编译速度,并且会采用一些优化算法,降低代码大小并提高代码运行速度。
-O2,会降低编译速度,但是除了包含-O1的优化算法之外,还会采用一些其他的优化算法来提高代码运行速度。
-O3,除了包含-O2所有的优化外,会采取一些向量化算法,提高代码的并行执行程度,使之更充分地利用现代cpu的流水线和cache。
-Os,-O3即使是增加代码的大小,也要提高运行速度。而这个选项在-O2的基础上,尽量减少目标的大小,这个经常用于存储量比较小的设备。