この記事を読む前に必要な基礎知識:
Python を使用して gdb を拡張する
Python でgraphviz を描画する
GDB を使用して赤黒の木を描画する
gdb の Python 拡張機能は前のセクションで紹介しました。「Python を使用した gdb の拡張」を参照してください。
また、Python にはgraphviz モジュールがあるため、gdb を使用して Python を呼び出し、graphviz を使用して Python で描画することができます。
アイデア
赤黒ツリーが挿入されるGDBブレークポイントを設定し、ブレークポイントにコマンドコマンドを定義し、赤黒ツリーのポインタをパラメータとしてPythonに渡します。
Python でパラメータを解析し、ドット構文を構築して表示する
コード例
.gdbinit
書類
layout src
source script/algc-test-rbtree-gdb.py
b 15
b 24
b 34
b 43
commands 1
print-rbt *tree->root
end
commands 2
print-rbt *tree->root
end
commands 3
print-rbt *tree->root
end
commands 4
print-rbt *tree->root
end
Pythonファイル
import gdb
import os
class PrintRBT(gdb.Command):
def __init__(self):
super().__init__("print-rbt", gdb.COMMAND_USER)
def invoke(self, arg, from_tty):
root = gdb.parse_and_eval(arg)
......
......
os.system("dot -Tsvg ./rbt.dot > ./rbt.svg")
os.system("dot -Tpng ./rbt.dot > ./rbt.png")
os.system("xdg-open ./rbt.svg")
PrintRBT()
結果を示す