当前位置: 首页 > news >正文

OS15.【Linux】gdb调试器的简单使用

目录

1.调试前的准备活动

2.断点的分类

3.测试代码

3.几个命令

list(简称l)

list  函数名(或者l 函数名)

list  行号(或者l 行号)

break(简称b)

break 行号(或b 行号)

break 函数名

break 文件名:函数名

run(简称r)

info(简称i)

info break(简称i b)

info locals

delete(简称d)

delete 编号(简称d 编号)

delete breakpoints(简称d breakpoints)

next(简称n)

step(简称s)

print(简称p)

print 变量名(简称p 变量名)

display

undisplay

until 行号

finish

return

continue(简称c)

disable

enable

set var

breaktrace(简称bt)

quit(简称q)


1.调试前的准备活动

前置知识:linux的gcc或g++编译器生成的可执行文件默认是release版本的,而如果想方便地使用gdb调试可执行文件,需要debug版本的,因此在使用gcc或g++编译时建议加上-g选项来生成debug版本的程序

注:如果使用gdb调试release版本的可执行文件会显示no debugging symbols found

gcc test.c -o test.out -std=c99

2.断点的分类

一共两类断点:软件断点和硬件断点

软件断点:将软件断点指令临时替换原指令来达到暂停执行程序的作用

硬件断点:顾名思义,通过硬件来下断点,简单叙述原理:通过微处理器调试模块中的比较器(comparators),比较器被设置为一个程序计数器PC的值,当值匹配时会触发断点暂停程序

参考文献:https://www.beningo.com/embedded-basics-hward-and-soft-breakpoints/#

 摘下来重点的两段:

The first breakpoint type that is used and generally preferred is a hardware breakpoint. Every microcontroller has comparators which are part of the debugging module. For example, the ARM Cortex-M microcontrollers can have 2 – 4 comparators in their debugging module. The comparator is set with a program counter value and when a match occurs, a debug event is raised and the program halts. Hardware breakpoints are the fastest and the most used breakpoint.

A software breakpoint is typically an instruction that temporarily replaces an instruction in RAM that is either an illegal instruction and causes a fault or is designed to cause the application to break. A perfect example is the BKPT instruction in the ARM instruction set. When the CPU reaches this instruction, it halts execution. Software breakpoints can only be used for application code that reside in RAM. The reason is that an instruction is literally swapped out for the breakpoint instruction. Once a developer steps past the BKPT, the originally code that would have executed at that location is ran.

3.测试代码

本文将围绕下面的C语言代码展开调试:

#include <stdio.h>
int loopfunction(int times)
{printf("Loopfunction is running......\n");int sum=0;for (int i=0;i<times;i++){sum+=i;}return sum;
}int main()
{printf("Main function is running......\n");int ret=loopfunction(100);printf("ret=%d\n",ret);printf("return 0.\n");return 0;
}

编译命令:

gcc test.c -o test.out -std=c99 -g

 使用gdb test.out来启动调试

3.几个命令

gdb全称是gnu debugger,是GNU开源组织发布的一款调试器

在线参考手册:https://sourceware.org/gdb/

pdf参考手册下载链接:https://sourceware.org/gdb/current/onlinedocs/gdb.pdf

list(简称l)

作用:打印源代码(必须有源代码文件,否则无法打印,list显示的源代码不是反编译出来的)

 默认情况下一次只能显示10行

继续执行该命令会向下展示代码:

(其实按回车也可以,按回车可以执行之前执行过的代码

list  函数名(或者l 函数名)

作用:查看对应函数内的代码

list  行号(或者l 行号)

作用:显示指定行号周围的代码

break(简称b)

作用: 下软件断点

在VS上可以点击行号的旁边来下断点

break 行号(或b 行号)

在gdb中,使用b 行号对处于指定行号下的代码下软件断点

注意:退出调试后断点是不会保存的

break 函数名

作用:在某个函数开头设置断点

break 文件名:函数名

作用:为某个文件的某个函数下断点,适合跨文件调试

run(简称r)

作用:运行程序,如果有断点会停下来

例如在int ret=loopfunction(100);处下断点,再运行

info(简称i)

info break(简称i b)

查看断点

其中的Enb是Enable是缩写,指断点是否开启,开启是y,关闭是n

info locals

作用:查看局部变量

Visual Studio也有这个功能

delete(简称d)

delete 编号(简称d 编号)

作用:删除断点,注意加的是编号不是行号

查看断点的编号: info b的打印结果的第一列(Num

delete breakpoints(简称d breakpoints)

作用:删除所有断点

next(简称n)

作用:逐过程调试,即逐行代码调试,但遇到函数调用不进入函数内部

Visual Studio上的逐过程可按F10

例如分别在int ret = loopfunction(100);下断点,且这个断点是开启的

执行到断点处再执行n命令,会发现并没有进入函数内部

step(简称s)

作用:逐语句调试,即逐行代码调试,遇到函数调用会进入函数内部

Visual Studio上的逐过程可按F11

例如分别在int ret = loopfunction(100);下断点,且这个断点是开启的

执行到断点处再执行n命令,会发现进入了函数内部

print(简称p)

print 变量名(简称p 变量名)

当然不仅仅可以查看变量名,也可以查看变量的地址

display

作用:长显示,能边调试边查看变量类似于Visual Studio的监视窗口

undisplay

undisplay 编号 可以取消对应编号的值的显示

until 行号

作用:跳出循环(前提是循环内没有断点,否则会重新回到里面),指定程序运行到某一行就停下来

finish

作用:执行完当前函数并回到上一层调用处停下来

return

和finish不同的是:立即结束执行当前函数并返回

continue(简称c)

作用:从一个断点直接运行到下一个断点

disable

作用:禁用断点(不删断点,但让断点不触发)

在Visual Studio里面可以右击禁用断点

enable

作用:启用断点

在Visual Studio里面也可以右击启用断点 

set var

作用:设置变量值

breaktrace(简称bt)

作用:查看各级函数调用及参数

类似Visual Studio里面的调用堆栈的功能

例如进入到loopfunction里面,执行bt命令

quit(简称q)

作用:退出调试

http://www.lryc.cn/news/579713.html

相关文章:

  • 力扣网编程第80题:删除有序数组中的重复项(简单)
  • springsecurity---使用流程、加密机制、自定义密码匹配器、token字符串生成
  • 【STM32实践篇】:I2C驱动编写
  • Vue如何处理数据、v-HTML的使用及总结
  • 8分钟讲完 Tomcat架构及工作原理
  • Node.js与Webpack
  • 前端面试专栏-算法篇:17. 排序算法
  • Spring SseEmitter 系统详细讲解
  • XILINX FPGA如何做时序分析和时序优化?
  • 手机内存融合是什么意思
  • Redis—哨兵模式
  • C++之路:类基础、构造析构、拷贝构造函数
  • 算法学习笔记:5.后缀数组——从原理到实战,涵盖 LeetCode 与考研 408 例题
  • MySQL 学习 之 你还在用 TIMESTAMP 吗?
  • Functionize 结合了 AI 与云平台的现代化自动化测试工具
  • MySQL 8.0 OCP 1Z0-908 题目解析(16)
  • curl for android
  • 高通QCS8550部署Yolov10模型与性能测试
  • ADC笔试面试题型和详细解析下
  • 蒙特卡洛方法:随机抽样的艺术与科学
  • c++ 的标准库 --- std::
  • {{ }}和v-on:click
  • 重学React(二):添加交互
  • 前端单元测试覆盖率工具有哪些,分别有什么优缺点
  • 鸿蒙操作系统核心特性解析:从分布式架构到高效开发的全景技术图谱
  • 深度学习-逻辑回归
  • 异步Websocket构建聊天室
  • 认识kubernetes kubeadm安装k8s
  • 触发器设计美国VPS:优化数据库性能的关键策略
  • 基于连接感知的实时困倦分类图神经网络