【源码】RapidJSON 源码剖析(0.1):调试工具 GDB 的使用
【源码】RapidJSON 源码剖析(0.1):调试工具 GDB 的使用
正式开始源码阅读之前,有必要了解一下源码阅读中用到的调试工具 GDB。
GDB(GNU Debugger) 是一种可以运行在多种类 Unix 系统上的,可移植的,适用于多种编程语言的调试器。
(The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages.)
GDB: The GNU Project Debugger 中对 GDB 有如下描述:
GDB 可以提供以下四种操作来帮助你捕获 bug:
• 启动你的程序,指定可能影响程序运行行为的内容。
• 让你的程序在指定的条件下停止。
• 当你的程序停止时, 检测你的程序发生的事。
• 改变你的程序的内容, 你可以纠正一个 bug 的影响,以便继续研究下一个 bug。
(gdb can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
• Start your program, specifying anything that might affect its behavior.
• Make your program stop on specified conditions.
• Examine what has happened, when your program has stopped.
• Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.)
调试工具 GDB 的使用
0. 待调试程序
本文调试的样例程序来自于 RapidJSON 官方文档,具体源代码如下:
// rapidjson/example/simpledom/simpledom.cpp`
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
int main()
{
// 1. 把 JSON 解析至 DOM。
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json);
// 2. 利用 DOM 作出修改。
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
// 3. 把 DOM 转换(stringify)成 JSON。
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;
return 0;
}
1. 编译
要使编译生成的可执行文件带有可被 GDB 识别的调试信息,需要在编译时加上参数 -g
。如:
g++ -g -std=c++11 simpledom.cpp -o simpledom.out
2. 用 GDB 启动你的程序
用 gdb <待调试软件名>
命令启动调试待调试软件。如:
gdb simpledom.out
3. 显示程序源码
当用 GDB 打开指定的程序后,可以用 list
或 l
命令查看该程序的源码。GDB: The GNU Project Debugger 9. Examining Source Files 对该命令有详细地介绍,这里摘录可能会用到的用法:
command | description |
---|---|
list linenum | Print lines centered around line number linenum in the current source file. |
list function | Print lines centered around the beginning of function function. |
list first,last | Print lines from first to last. |
list ,last | Print lines ending with last. |
list first, | Print lines starting with first. |
list filename:linenum | Print lines centered around linenum in the source file filename. |
list filename:function | Print lines centered around the beginning of the function function in the file filename. |
如,显示当前文件的 20 到 23 行:
list 20, 23
输出如下:
4. 设置断点,查看断点和取消断点
设置断点是 break
或 b
命令,查看断点用 info break
或 info b
命令,删除断点用 delete
或 d
命令。具体用法如下表:
command | description |
---|---|
break location | Set a breakpoint at the given location, which can specify a function name, a line number, or an address of an instruction. |
break … if cond | Set a breakpoint with condition cond; evaluate the expression cond each time the breakpoint is reached, and stop only if the value is nonzero—that is, if cond evaluates as true.‘…’ stands for one of the possible arguments described above (or no argument) specifying where to break. |
info break | Print a table of all breakpoints, watchpoints, and catchpoints set and not deleted. For each breakpoint, following columns are printed: Breakpoint Numbers, Type, Disposition, Enabled(‘y’) or Disabled(‘n’), Address, What. |
delete breakpoint number | Delete the breakpoint specified by breakpoint number, If no argument is specified, delete all breakpoints. |
如,在 14 行和 18 行设置断点, 然后查看断点信息,最后取消 18行断点,再查看断点信息:
break 14
break 18
info break
delete 2
info break
输出如下:
5. 开始单步调试,下一步, 继续到下个断点,进入函数,跳出当前函数,查看变量值,查看变量类型。
start
命令从 main
函数开始单步调试。next
或 n
命令进入下一步。continue
或 c
命令继续执行程序到下一个断点。
step
或 s
命令进入函数内部, finish
跳出当前函数。
print <变量名>
或 p <变量名>
显示指定变量的值,ptype <变量名>
查看变量的类型。
如,
- 通过
start
命令开始单步调试代码
- 进入下一步
- 查看变量
json
的值和类型
- 在 14 行处设置一个断点
- 执行到下一个断点(14 行处)
- 跳转进
Parse
函数
- 跳出
Parse
函数
- 继续执行完整个程序
参考文献
【源码】RapidJSON 源码剖析(0.1):调试工具 GDB 的使用的更多相关文章
- Spring Boot 揭秘与实战 源码分析 - 工作原理剖析
文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...
- 保姆级教程——Ubuntu16.04 Server下深度学习环境搭建:安装CUDA8.0,cuDNN6.0,Bazel0.5.4,源码编译安装TensorFlow1.4.0(GPU版)
写在前面 本文叙述了在Ubuntu16.04 Server下安装CUDA8.0,cuDNN6.0以及源码编译安装TensorFlow1.4.0(GPU版)的亲身经历,包括遇到的问题及解决办法,也有一些 ...
- [笔记] Ubuntu 18.04源码编译安装OpenCV 4.0流程
标准常规安装方法安装的OpenCV版本比较低,想尝鲜使用4.0版本,只好源码安装. 安装环境 OS:Ubuntu 18.04 64 bit 显卡:NVidia GTX 1080 CUDA:10.0 c ...
- 修改和编译spring源码,构建jar(spring-context-4.0.2.RELEASE)
上周在定位问题时,发现Spring容器实例化Bean的时候抛出异常,为了查看更详细的信息,决定修改spring-context-4.0.2.RELEASE.jar中的CommonAnnotationB ...
- 大话Spark(6)-源码之SparkContext原理剖析
SparkContext是整个spark程序通往集群的唯一通道,他是程序的起点,也是程序的终点. 我们的每一个spark个程序都需要先创建SparkContext,接着调用SparkContext的方 ...
- EventBus源码解析 源码阅读记录
EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...
- ios源码-ios游戏源码-ios源码下载
游戏源码 一款休闲类的音乐小游戏源码 该源码实现了一款休闲类的音乐小游戏源码,该游戏的源码很简单,而且游戏的玩法也很容易学会,只要我们点击视图中的grid,就可以 人气:2943运行环境:/Xco ...
- C#UDP(接收和发送源码)源码完整
C#UDP(接收和发送源码)源码完整 最近做了一个UDP的服务接收和发送的东西.希望能对初学的朋友一点帮助. 源码如下: 一.逻辑--UdpServer.cs using System;using S ...
- android源码-安卓源码-Android源码下载-安卓游戏源码
android源码 高仿精仿金山手机卫士应用源码V1.2 高仿精仿金山手机卫士应用源码,该应用的级别实现了金山卫士的级别功能了,可以说跟现实中我们使用的金山卫士应用的功能几乎差不 人气:9286 ...
- zxing源码分析——QR码部分
Android应用横竖屏切换 zxing源码分析——DataMatrix码部分 zxing源码分析——QR码部分 2013-07-10 17:16:03| 分类: 默认分类 | 标签: |字号大中 ...
随机推荐
- Day21:方法重写以及注意细节
目录 方法重写 什么是方法重写? 方法重写有什么用? 方法重写的注意细节 方法重写 什么是方法重写? 方法重写指的是当子类和父类出现了一摸一样的方法声明 方法重写有什么用? 当父类中有一个方法时,子类 ...
- Devexpress控件pivotGridControl显示字段面板
可在窗口加载的时候使用函数 pivotGridControl1.ShowCustomization(); 大家如果有问题可以 Console.WriteLine("加群"+&quo ...
- psutil.AccessDenied: psutil.AccessDenied
解决办法 import psutil for proc in psutil.process_iter(): try: print(proc.name()) except (psutil.NoSuchP ...
- 关于linux上实现arp攻击截取密码
前言 这几天简单的研究了一下arp攻击,有一些进展,记录一下 环境准备 这里我是利用arpspoof 这个软件简单实现arp攻击,这个命令是属于dsniff 软件包中的 所以首先安装软件 sudo a ...
- javaweb string
今天遇到一个跨域请求jsonp格式报错,其原因是其中一个参数过从我方数据库取出就带有换行格式的,类似于: 这条数据竟然自带格式换行. 而我们现常用的trim()只能去掉字符串的头部和尾部的空格, 而要 ...
- sql注入的一丢丢
- python基础(数据库、可视化软件Navicat、python操作MySQL)
多表查询的两种方法 数据准备: 建表 create table dep( id int primary key auto_increment, name varchar(20) ); create t ...
- Spring IOC官方文档学习笔记(二)之Bean概述
1.Bean概述 (1) Spring IoC容器管理一个或多个bean,这些bean是根据我们所提供的配置元数据来创建的,在容器内部,BeanDefinition对象就代表了bean的配置元数据,它 ...
- 8、ThreadPoolTaskExecutor线程并发
一.线程池的优点: 1.降低资源消耗.通过重复利用自己创建的线程降低线程创建和销毁造成的消耗. 2.提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行. 3.提高线程的可管理性.线程是 ...
- 1、mybatis逆向工程
mybatis逆向工程可以针对单表自动生成mybatis执行所需要的mapper.java.mapper.xml代码(dao层),可以让程序员将更多的精力放在繁杂的业务逻辑上(service层与con ...