【源码】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 打开指定的程序后,可以用 listl 命令查看该程序的源码。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. 设置断点,查看断点和取消断点

设置断点是 breakb 命令,查看断点用 info breakinfo b 命令,删除断点用 deleted 命令。具体用法如下表:

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.
breakif 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 函数开始单步调试。nextn 命令进入下一步。continuec 命令继续执行程序到下一个断点。
steps 命令进入函数内部, finish 跳出当前函数。
print <变量名>p <变量名> 显示指定变量的值,ptype <变量名> 查看变量的类型。

如,

  • 通过 start 命令开始单步调试代码
  • 进入下一步
  • 查看变量 json 的值和类型
  • 在 14 行处设置一个断点
  • 执行到下一个断点(14 行处)
  • 跳转进 Parse 函数
  • 跳出 Parse 函数
  • 继续执行完整个程序

参考文献

最新文章

  1. Java 中的内存泄露
  2. Eclipse配置PyDev插件来实现python开发环境
  3. Swift3.0语言教程组合字符串
  4. LilyPad Arduino可穿戴技术和电子织物控制器板简介
  5. Andrew Ng机器学习公开课笔记 -- Online Learning
  6. UITableViewCell 设置圆角
  7. Android NDK调试C++源码(转)
  8. 【汉诺塔问题】UVa 10795 - A Different Task
  9. QT使用UAC(经过验证)
  10. jsp 页面获取xml的内容
  11. 『重构--改善既有代码的设计』读书笔记----Self Encapsulate Field
  12. 使用URL创建网络连接、网络流的阻塞问题
  13. php 查看文档
  14. 微信JS分享功能--微信JS系列文章(二)
  15. BZOJ_2124_等差子序列_线段树+Hash
  16. libevent和libcurl实现http和https服务器 cJSON使用
  17. 1.docker常用命令
  18. Java 前后端List传值
  19. mysql 开发进阶篇系列 47 物理备份与恢复(xtrabackup 的完全备份恢复,恢复后重启失败总结)
  20. abap 断言

热门文章

  1. 交叉编译GDB
  2. sublime text设置build system automatic提示no build system
  3. MIT6.828 Lab 1: C, Assembly, Tools, and Bootstrapping
  4. ArcObjects SDK开发 006 ICommand和ITool接口
  5. day14 I/O流——序列化与反序列化 &amp; 计算机网络五层架构 &amp; TCP的建立连接与断开连接
  6. python 爬取豆瓣电影评论,并进行词云展示
  7. C#关于委托的一些事,开发日志
  8. 网络爬虫之requests模块,自动办公领域之openpyx模块
  9. vivo 低代码平台【后羿】的探索与实践
  10. C语言常用知识总结