我正在使用nlohmann's json C++ implementation进行一个项目
如何在GDB中轻松浏览nlohmann的JSON键/VAL?
我尝试使用这个STL gdb wrapping,因为它提供帮助器来探索nlohmann的JSON-LIB使用的标准C++库结构。 但我觉得不方便
下面是一个简单的用例:
json foo;
foo["flex"] = 0.2;
foo["awesome_str"] = "bleh";
foo["nested"] = {{"bar", "barz"}};
我希望在GDB中拥有什么:
(gdb) p foo
{
"flex" : 0.2,
"awesome_str": "bleh",
"nested": etc.
}
当前行为
(gdb) p foo
$1 = {
m_type = nlohmann::detail::value_t::object,
m_value = {
object = 0x129ccdd0,
array = 0x129ccdd0,
string = 0x129ccdd0,
boolean = 208,
number_integer = 312266192,
number_unsigned = 312266192,
number_float = 1.5427999782486669e-315
}
}
(gdb) p foo.at("flex")
Cannot evaluate function -- may be inlined // I suppose it depends on my compilation process. But I guess it does not invalidate the question.
(gdb) p *foo.m_value.object
$2 = {
_M_t = {
_M_impl = {
<std::allocator<std::_Rb_tree_node<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long, unsigned long long, double, std::allocator, nlohmann::adl_serializer> > > >> = {
<__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long, unsigned long long, double, std::allocator, nlohmann::adl_serializer> > > >> = {<No data fields>}, <No data fields>},
<std::_Rb_tree_key_compare<std::less<void> >> = {
_M_key_compare = {<No data fields>}
},
<std::_Rb_tree_header> = {
_M_header = {
_M_color = std::_S_red,
_M_parent = 0x4d72d0,
_M_left = 0x4d7210,
_M_right = 0x4d7270
},
_M_node_count = 5
}, <No data fields>}
}
}
我找到了自己的答案,进一步阅读了关于print of std::string的GDB功能和堆栈溢出问题。 短路径最简单。 另一条路很难走,但我很高兴我做到了这一点。还有很多改进的余地
短路径v3.1.2
我简单地定义了一个gdb命令,如下所示:
在gdb中使用它:
短路径v3.7.0[编辑]2019-onv-06您也可以使用新的to_string()方法,但我无法让它使用带有活动进程的GDB。下面的方法仍然有效
2020年4月18日:运行完整的PYTHON GDB(使用实时进程和调试符号)
Edit 2020-april-26:这里的代码(偏移量)是蓝色的,不兼容所有平台/JSON库编译。github项目在这方面要成熟得多(到目前为止已经测试了3个平台)。代码仍保留在那里就像一样,因为我不会维护2个代码库
版本:
GNU gdb (GDB) 8.3 for GNAT Community 2019 [rev=gdb-8.3-ref-194-g3fc1095]
GPRBUILD
/GNAT Community 2019 (20190517) (x86_64-pc-mingw32)
构建的c++项目以下python代码应加载到gdb中。我使用源于gdb的.gdbinit文件
Github repo:https://github.com/LoneWanderer-GH/nlohmann-json-gdb
GDB脚本
您可以自由选择加载方式(自动、非自动、IDE插件等)
Python脚本
一些(光照测试):
gpr文件:
main.cpp
#include//我正在使用回购发行版中的独立json.hpp #包括
产出:
编辑2019-3-24:添加给出的精度
编辑2020年4月18日:在与python/gdb/stl进行了一个漫长的斗争之后,我通过python漂亮打印机的gdb文档的方式做了一些工作。请原谅任何错误或误解,我为此撞了一整晚的头,现在一切都乱七八糟
编辑2020年4月18日(2):rb树节点和树大小可以以更“内存”的方式进行遍历(见上文)
编辑2020年4月26日:添加有关GDB python打印机的警告
我的解决方案是编辑~/.gdbinit文件
这使得“jsontostring”命令在每个gdb会话上都可用,而无需寻找任何文件
(gdb) jsontostring object
相关问题 更多 >
编程相关推荐