Uso básico de GDB bajo ubuntu
GDB es una poderosa herramienta de depuración de programas basada en la línea de comandos bajo el sistema operativo UNIX/LINUX lanzado por la organización de código abierto GNU. Se puede utilizar para depurar el programa C, C++.
instalación de GDB
# 安装
sudo apt-get install gdb
Ver el número de versión de dgb
# 查看版本
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".
Uso básico de GDB
Crear un archivo de prueba cpp
#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;
}
compilar cpp
g++ -o myHello -g ./main.cpp
gdb
Ejecutar depuración usando
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)
Después de la ejecución, el cursor de la consola se detiene al (gdb)
final y puede iniciar la depuración interactiva en este momento:
Los comandos básicos de depuración son los siguientes
función de comando | Orden | comando nombre completo |
---|---|---|
cargar un ejecutable | file <filename> |
- |
ver código | l ol <行号> |
list <行号> |
establecer punto de interrupción | b <行号> |
break <行号> |
iniciar operación | r |
run |
Ejecute el siguiente punto de interrupción: | c |
continue |
Próximo paso | n |
next |
ver variables | p <变量名> |
print <变量名> |
tipo de vista | whatis <变量名> |
- |
eliminar punto de interrupción | d <行号> |
delete <行号> |
borrar todos los puntos de interrupción | clear |
- |
salir de depuración | quit |
- |
CMake configuración DGB depuración
como CMakeLists.txt
configurar
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")
Para configurar el modo de depuración,
CMAKE_BUILD_TYPE
puede especificarlo al ejecutar el comando cmake, sinCMakeLists.txt
escribirlo demasiado, de la siguiente manera:
cmake .. -DCMAKE_BUILD_TYPE=Debug
También puede usar el comando add_compile_options, que tiene el mismo efecto que configurar CMAKE_CXX_FLAGS o CMAKE_C_FLAGS;
add_compile_options es para todos los compiladores;
Explicación funcional de los argumentos de opción relacionados
- -w -W -pared
-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的基础上,尽量减少目标的大小,这个经常用于存储量比较小的设备。