Skip to content

Instantly share code, notes, and snippets.

@paosidufygthrj
Last active December 2, 2015 01:23
Show Gist options
  • Save paosidufygthrj/c07fdc99f1d9ccf1c570 to your computer and use it in GitHub Desktop.
Save paosidufygthrj/c07fdc99f1d9ccf1c570 to your computer and use it in GitHub Desktop.

GDBでBoostライブラリのPrettyPrinterを導入

導入環境

  • ubuntu 14.04 LTS
  • g++ 4.8.4
  • gdb 7.7
  • python 3.4
  • Boost C++ Libraries 1.59.0

Boost-Pretty-Printer 入手

BoostライブラリのGDB PrettyPrinter が以下のレポジトリで公開されている。
ただし現在(2015/12/02)は一年近くメンテナンスされていない。

https://github.com/ruediger/Boost-Pretty-Printer

前述の環境では動かなかったため、フォークされたレポジトリを使用する。

https://github.com/mateidavid/Boost-Pretty-Printer/tree/merge-attempt

インストール

説明に沿って設定すれば基本的に問題なかった。
適用なディレクトリでレポジトリをクローンする。

$ git clone git@github.com:mateidavid/Boost-Pretty-Printer.git

~/.gdbinit ファイルに以下の内容を追加するか、新規作成する。
PATH-TO-THE-REPO/Boost-Pretty-Printerの部分は先ほどcloneしたディレクトリのパスを指定する。

python
import sys
sys.path.insert(0, 'PATH-TO-THE-REPO/Boost-Pretty-Printer')
import boost.latest ### see note on Entry Points below
boost.register_printers()
end

実行

以下のcppファイルでブレークしp hを実行した場合にboost::optionalの出力がわかりやすくなっている。

#include <string>
#include <boost/optional.hpp>

struct hoge {
    std::string text;
    int num;
};

int main() {
    boost::optional<hoge> h = hoge();
    h->text = "abcdefg";
    h->num = 100;
    return 0;
}
(gdb) p h
$1 = boost::optional<hoge> is initialized = {value = {text = {
static npos = <optimized out>, 
_M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
_M_p = 0x7ffff7dd93d8 <std::string::_Rep::_S_empty_rep_storage+24> ""}}, num = 0}}

作成日: 2015/12/02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment