【源码】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 打开指定的程序后,可以用 listl 命令查看该程序的源码。GDB: The GNU Project Debugger 9. Examining Source Files 对该命令有详细地介绍,这里摘录可能会用到的用法:

commanddescription
list linenumPrint lines centered around line number linenum in the current source file.
list functionPrint lines centered around the beginning of function function.
list first,lastPrint lines from first to last.
list ,lastPrint lines ending with last.
list first,Print lines starting with first.
list filename:linenumPrint lines centered around linenum in the source file filename.
list filename:functionPrint lines centered around the beginning of the function function in the file filename.

如,显示当前文件的 20 到 23 行:

list 20, 23

输出如下:
list 20, 23

4. 设置断点,查看断点和取消断点

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

commanddescription
break locationSet a breakpoint at the given location, which can specify a function name, a line number, or an address of an instruction.
breakif condSet 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 breakPrint 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 numberDelete 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

输出如下:
break

5. 开始单步调试,下一步, 继续到下个断点,进入函数,跳出当前函数,查看变量值,查看变量类型。

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

如,

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

参考文献

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值