设为首页 加入收藏

TOP

前端学习 C 语言 —— GDB调试器(一)
2023-07-23 13:25:11 】 浏览:124
Tags:语言 GDB

GDB调试器

我们在讲指针时用 GDB 调试段错误。

本篇将详细介绍 gdb 的最常用命令日志记录检测点,最后介绍如何用 gdb 调试进程以及用gdb 调试一个开源项目的调试版本 —— glmark2。

gdb介绍

GDB, the GNU Project debugger —— gdb官网

gdb 是一款调试器,能打断点。支持多种语言,例如 c、c++、go。

Tip:有关 GNU Project,请看本篇扩展

官网显示最新版本是13.2(20230704)。点击官网顶部[documentation]可查看文档。

安装GDB

笔者已经用 apt 源安装了gbd:

jjj-pc:~/pj/glmark2$ sudo apt install gdb
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
gdb 已经是最新版 (9.1-0kylin1)。
下列软件包是自动安装的并且现在不需要了:
  archdetect-deb dmeventd libaio1 libdebian-installer4 libdevmapper-event1.02.1 liblvm2cmd2.03 localechooser-data lvm2 user-setup
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 6 个软件包未被升级。

笔者gbd版本是 9.1

jjj-pc:~/pj/glmark2$ gdb --version
GNU gdb (Ubuntu 9.1-0kylin1) 9.1
Copyright (C) 2020 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.

最常用命令

man gdb 告诉我们最常用的命令有:break、run、print、c、next、list、step、quit。

// 以下是一些最常用的GDB命令:
Here are some of the most frequently needed GDB commands:

       break [file:]function
           Set a breakpoint at function (in file).

       run [arglist]
           Start your program (with arglist, if specified).

       bt  Backtrace: display the program stack.

       print expr
           // 显示表达式的值
           Display the value of an expression.

       c   Continue running your program (after stopping, e.g. at a breakpoint).

       next
           // 执行下一条程序语句(在停止后);跳过该行中的任何函数调用。
           Execute next program line (after stopping); step over any function calls in the line.

       edit [file:]function
           look at the program line where it is presently stopped.

       list [file:]function
           type the text of the program in the vicinity of where it is presently stopped.

       step
           Execute next program line (after stopping); step into any function calls in the line.

       help [name]
           Show information about GDB command name, or general information about using GDB.

       quit
           Exit from GDB.

准备一段 C 代码用作gdb命令学习:

#include <stdio.h>

// add 函数
int add(int a, int b) {
    int sum = a + b;
    return sum;
}

int main() {
    int num1 = 3;
    int num2 = 5;
    
    int result = add(num1, num2);
    
    printf("两个整数的和为:%d\n", result);
    
    return 0;
}

run 和 quit

通过 gdb demo 运行进入gdb模式,输入 run 运行程序,输入 quit 则退出gdb。详细请看:

// 通过 -g 编译出有调试信息的可执行文件
jjj-pc:~/pj$ gcc demo.c -o demo -g
// gdb 运行
jjj-pc:~/pj$ gdb demo
GNU gdb (Ubuntu 9.1-0kylin1) 9.1
Copyright (C) 2020 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 demo...
// 输入 run 运行程序
(gdb) run
Starting program: /home/jjj/pj/demo
两个整数的和为:8
[Inferior 1 (process 3022770) exited normally]
// 输入 quit 退出
(gdb) quit
jjj-pc:~/pj$

Tip: ctrl + z 能直接退出gdb

list

如果不知道在

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇进程间通信总结 下一篇C语言:数据结构之单链表(一)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目