【源码】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
函数
- 继续执行完整个程序
参考文献
最新文章
- Java 中的内存泄露
- Eclipse配置PyDev插件来实现python开发环境
- Swift3.0语言教程组合字符串
- LilyPad Arduino可穿戴技术和电子织物控制器板简介
- Andrew Ng机器学习公开课笔记 -- Online Learning
- UITableViewCell 设置圆角
- Android NDK调试C++源码(转)
- 【汉诺塔问题】UVa 10795 - A Different Task
- QT使用UAC(经过验证)
- jsp 页面获取xml的内容
- 『重构--改善既有代码的设计』读书笔记----Self Encapsulate Field
- 使用URL创建网络连接、网络流的阻塞问题
- php 查看文档
- 微信JS分享功能--微信JS系列文章(二)
- BZOJ_2124_等差子序列_线段树+Hash
- libevent和libcurl实现http和https服务器 cJSON使用
- 1.docker常用命令
- Java 前后端List传值
- mysql 开发进阶篇系列 47 物理备份与恢复(xtrabackup 的完全备份恢复,恢复后重启失败总结)
- abap 断言
热门文章
- 交叉编译GDB
- sublime text设置build system automatic提示no build system
- MIT6.828 Lab 1: C, Assembly, Tools, and Bootstrapping
- ArcObjects SDK开发 006 ICommand和ITool接口
- day14 I/O流——序列化与反序列化 &; 计算机网络五层架构 &; TCP的建立连接与断开连接
- python 爬取豆瓣电影评论,并进行词云展示
- C#关于委托的一些事,开发日志
- 网络爬虫之requests模块,自动办公领域之openpyx模块
- vivo 低代码平台【后羿】的探索与实践
- C语言常用知识总结