ubuntu での GDB の基本的な使用と CMake 設定のデバッグ

ubuntuでのGDBの基本的な使い方

GDB は、GNU オープン ソース組織によってリリースされた UNIX/LINUX オペレーティング システムのコマンド ラインに基づく強力なプログラム デバッグ ツールです。C、C++ プログラムのデバッグに使用できます。

GDB のインストール

# 安装
sudo apt-get install gdb

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".

GDBの基本的な使い方

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;
}

cppをコンパイルする

g++ -o myHello -g ./main.cpp

を使用してgdbデバッグを実行します

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) 

実行後、コンソール カーソルは(gdb)最後に停止し、この時点で対話型デバッグを開始できます。

基本的なデバッグ コマンドは次のとおりです。
コマンド機能 注文 コマンドフルネーム
実行可能ファイルをロードする file <filename> -
コードを見る lまたl <行号> list <行号>
ブレークポイントを設定 b <行号> break <行号>
運転開始 r run
次のブレークポイントを実行します。 c continue
次のステップ n next
変数を表示 p <变量名> print <变量名>
ビュータイプ whatis <变量名> -
ブレークポイントを削除 d <行号> delete <行号>
すべてのブレークポイントをクリア clear -
デバッグを終了する quit -

CMake 構成 DGB デバッグ

CMakeLists.txt設定方法

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")

デバッグ モードを設定するには、次のように、ハードに記述せCMAKE_BUILD_TYPEずに cmake コマンドを実行するときに指定できます。CMakeLists.txt
cmake .. -DCMAKE_BUILD_TYPE=Debug

CMAKE_CXX_FLAGS または CMAKE_C_FLAGS を設定するのと同じ効果がある add_compile_options コマンドを使用することもできます;
add_compile_options はすべてのコンパイラ用です;

関連するオプション引数の機能説明

  • -w -W -ウォール
-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的基础上,尽量减少目标的大小,这个经常用于存储量比较小的设备。

おすすめ

転載: blog.csdn.net/youlinhuanyan/article/details/126085714