Linux系统调试篇——GDB调试入门

简介: Linux系统调试篇——GDB调试入门

本篇讲解使用GDB调试Linux应用程序,以下以 hellowld.c 为例介绍 GDB 的调试入门:

编写代码

#include <stdio.h>
int main(int argc, char **argv)
{
    int i;
    int result = 0;
    if(1 >= argc)
    {
        printf("Helloworld.\n");
    }
    printf("Hello World %s!\n",argv[1]);
    for(i = 1; i <= 100; i++)  {
        result += i;
    }
    printf("result = %d\n", result );
    return 0;
}

编译时加上 -g 参数:

gcc helloworld.c -o hellowrld -g

启动调试

$ gdb helloWorld
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-12.el8
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 "x86_64-redhat-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 helloworld...done.
(gdb) run                  <----------------------------- 不带参数运行
Starting program: /home/zhuzhg/helloworld
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
helloworld.
result = 5050
[Inferior 1 (process 1069013) exited normally]
(gdb) run China            <----------------------------- 带参数运行
Starting program: /home/zhuzhg/helloworld China
Hello World China!
result = 5050
[Inferior 1 (process 1071086) exited normally]
(gdb)

断点

设置断点

  • 文件行号断点:break hellowrld.c:9
  • 函数断点:break main
  • 条件断点:break helloworld.c:17 if c == 10
  • 临时断点, 假设某处的断点只想生效一次,那么可以设置临时断点,这样断点后面就不复存在了:tbreak helleworld.c:9
  • 禁用或启动断点:
disable                 # 禁用所有断点
  disable bnum            # 禁用标号为bnum的断点
  enable                  # 启用所有断点
  enable bnum             # 启用标号为bnum的断点
  enable delete bnum      # 启动标号为bnum的断点,并且在此之后删除该断点
  • 断点清除:
clear                   # 删除当前行所有breakpoints
  clear function          # 删除函数名为function处的断点
  clear filename:function # 删除文件filename中函数function处的断点
  clear lineNum           # 删除行号为lineNum处的断点
  clear f:lename:lineNum  # 删除文件filename中行号为lineNum处的断点
  delete                  # 删除所有breakpoints,watchpoints和catchpoints
  delete bnum             # 删除断点号为bnum的断点

变量查看

  • 变量查看: 最常见的使用便是使用print(可简写为p)打印变量内容。
    以上述程序为例:
gdb helloworld
break helloworld.c:17 if i == 0
(gdb) run
Starting program: /home/book/helloworld
helloworld.
Breakpoint 2, main (argc=1, argv=0x7fffffffdca8) at helloworld.c:17
17            result += i;
(gdb) print i                <------------------ 查看变量 i 当前的值
$1 = 10
(gdb) print result           <------------------ 查看变量 result 当前的值
$2 = 45
(gdb) print argc             <------------------ 查看变量 argc 当前的值
$3 = 1
(gdb) print str
$4 = 0x4006c8 "Hello World" <------------------ 查看变量 str 当前的值
  • 查看内存: examine(简写为x)可以用来查看内存地址中的值。语法如下:
x/[n][f][u] addr
  • 其中:
    单元类型常见有如下:
    示例:
(gdb) x/4b str
0x4006c8:    01001000    01100101    01101100    01101100
  • 可以看到,变量 str 的四个字节都以二进制的方式打印出来了。
  • b 字节
  • h 半字,即双字节
  • w 字,即四字节
  • g 八字节
  • n 表示要显示的内存单元数,默认值为1
  • f 表示要打印的格式,前面已经提到了格式控制字符
  • u 要打印的单元长度
  • addr 内存地址
  • 查看寄存器内容:info registers
ra             0x3ff7ef2282     0x3ff7ef2282 <__libc_start_main+160>
sp             0x3ffffffaa0     0x3ffffffaa0
gp             0x2aaaaac800     0x2aaaaac800
tp             0x3ff7fdd250     0x3ff7fdd250
t0             0x3ff7ed60b0     274742468784
t1             0x3ff7ef21e2     274742583778
t2             0x2aaaaac4f0     183251944688
fp             0x3ffffffab0     0x3ffffffab0
s1             0x0      0
a0             0x1      1
a1             0x3ffffffc28     274877905960
a2             0x3ffffffc38     274877905976
a3             0x0      0
a4             0x3ffffffad8     274877905624
a5             0x0      0
a6             0x3ff7fd88a8     274743527592
(内容过多未显示完全)

单步调试

  • 单步执行-next:
    next命令(可简写为n)用于在程序断住后,继续执行下一条语句,假设已经启动调试,并在第12行停住,如果要继续执行,则使用n执行下一条语句,如果后面跟上数字num,则表示执行该命令num次,就达到继续执行n行的效果了:
gdb helloworld                     <------------------------------- 加载程序
   (gdb) break helloworld.c:18        <------------------------------- 设置断点
   (gdb) run                          <------------------------------- 启动调试
   The program being debugged has been started already.
   Start it from the beginning? (y or n) y
   Starting program: /home/book/helloworld
   Helleo World.
   Breakpoint 2, main (argc=1, argv=0x7fffffffdca8) at helloworld.c:18         <-------- 程序在 18 行暂停
   18            result += i;
   Breakpoint 2, main (argc=1, argv=0x7fffffffdca8) at helloworld.c:18
   18            result += i;
   (gdb) next                                    <--------  单步执行
   17        for(i = 1; i <= 100; i++)  {
   Breakpoint 2, main (argc=1, argv=0x7fffffffdca8) at helloworld.c:18
   18            result += i;
   (gdb) next 2                                  <--------  执行两次
   Breakpoint 2, main (argc=1, argv=0x7fffffffdca8) at helloworld.c:18
   18            result += i;
  • 单步进入-step:
    如果我们想跟踪函数内部的情况,可以使用step命令(可简写为s),它可以单步跟踪到函数内部,但前提是该函数有调试信息并且有源码信息。
  • 断点继续-continue:
    continue命令(可简写为c),它会继续执行程序,直到再次遇到断点处。

end

往期推荐

入职Linux驱动工程师后,我才知道的真相......

机遇:我是如何走向Linux驱动的...

当我用几道题考了一遍做Linux驱动的同事......

“不是所有的驱动岗,都值得你去”

Linux驱动面试高频考点

相关实践学习
阿里云图数据库GDB入门与应用
图数据库(Graph Database,简称GDB)是一种支持Property Graph图模型、用于处理高度连接数据查询与存储的实时、可靠的在线数据库服务。它支持Apache TinkerPop Gremlin查询语言,可以帮您快速构建基于高度连接的数据集的应用程序。GDB非常适合社交网络、欺诈检测、推荐引擎、实时图谱、网络/IT运营这类高度互连数据集的场景。 GDB由阿里云自主研发,具备如下优势: 标准图查询语言:支持属性图,高度兼容Gremlin图查询语言。 高度优化的自研引擎:高度优化的自研图计算层和存储层,云盘多副本保障数据超高可靠,支持ACID事务。 服务高可用:支持高可用实例,节点故障迅速转移,保障业务连续性。 易运维:提供备份恢复、自动升级、监控告警、故障切换等丰富的运维功能,大幅降低运维成本。 产品主页:https://www.aliyun.com/product/gdb
相关文章
|
20天前
|
运维 Java Linux
(九)JVM成神路之性能调优、GC调试、各内存区、Linux参数大全及实用小技巧
本章节主要用于补齐之前GC篇章以及JVM运行时数据区的一些JVM参数,更多的作用也可以看作是JVM的参数列表大全。对于开发者而言,能够控制JVM的部分也就只有启动参数了,同时,对于JVM的性能调优而言,JVM的参数也是基础。
|
25天前
|
Linux C# iOS开发
如何用 WinDbg 调试Linux上的 .NET程序
【7月更文挑战第13天】 1. `dotnet-dump`: Collects process dumps with `dotnet-dump collect -p &lt;process_id&gt;`. 2. `lldb`: Debugs Mono runtime apps on macOS/Linux. 3. **Visual Studio Code**: Remotely debugs .NET via the C# extension. 4. **JetBrains Rider**: Supports remote debugging of .NET on Linux.
|
2月前
|
NoSQL Linux C语言
Linux gdb调试的时候没有对应的c调试信息库怎么办?
Linux gdb调试的时候没有对应的c调试信息库怎么办?
26 1
|
2月前
|
NoSQL 编译器 Linux
【Linux】--- Linux编译器-gcc/g++、调试器-gdb、项目自动化构建工具-make/Makefile 使用
【Linux】--- Linux编译器-gcc/g++、调试器-gdb、项目自动化构建工具-make/Makefile 使用
29 0
|
2月前
|
NoSQL Linux C语言
Linux gdb调试的时候没有对应的c调试信息库怎么办?
Linux gdb调试的时候没有对应的c调试信息库怎么办?
20 0
|
2月前
|
NoSQL Linux C++
Linux C/C++ gdb调试正在运行的程序
Linux C/C++ gdb调试正在运行的程序
|
3月前
|
监控 网络协议 Java
Linux 网络编程从入门到进阶 学习指南
在上一篇文章中,我们探讨了 Linux 系统编程的诸多基础构件,包括文件操作、进程管理和线程同步等,接下来,我们将视野扩展到网络世界。在这个新篇章里,我们要让应用跳出单机限制,学会在网络上跨机器交流信息。
Linux 网络编程从入门到进阶 学习指南
|
10月前
|
存储 Linux C语言
Linux:入门学习知识及常见指令
Linux:入门学习知识及常见指令
|
3月前
|
存储 消息中间件 网络协议
Linux 系统编程从入门到进阶 学习指南
本文旨在为初学者提供一个清晰的 Linux 系统编程入门指南,带你步入 Linux 系统编程的世界,从基本概念到实用技能,一步步建立起您的知识体系。
Linux 系统编程从入门到进阶 学习指南
|
运维 Linux Shell
Linux权限维持入门学习(上)
Linux权限维持入门学习
117 0

热门文章

最新文章