概述
c/c++如果在日志中查看某个结构体/类的每个变量名,变量值信息,只能通过printf逐个格式化,非常繁琐,如何做到类似protobuff转json的序列化功能呢?
该dll库先通过分析pdb文件获取结构体/类的变量名称、变量地址,并将指定的对象序列化成完整json字符串,极大降低了开发者工作量。
demo效果
在release下执行tcdumpTest.exe 看效果
注意:使用前先看‘使用说明.txt’,需要先注册\DIA SDK\bin\msdia120.dll。
定义类、结构体:
1
2
3
4
5
6
7
8
9
10
11Class Ctest
{
Public:
Int M_i = 1;
Float M_f = 0.1;
Double M_d = 0.2;
Char M_c = 'a';
Std::String M_str = "Hello World";
Std::Vector<Int> M_v;
Std::Map<Int, Std::String> M_map;
};赋值:
1
2
3
4
5
6Ctest Test;
Test.M_v.Push_back(1);
Test.M_v.Push_back(2);
Test.M_v.Push_back(3);
Test.M_map.Insert({ 1,"hello" });
test.m_map.insert({ 2,"world" });初始化tcDump,并序列化对象
1
2
3
4
5
6
7
8
9
10
11int ret = TCDUMP_INIT(R"(..\tcDumpTest.pdb)");
if (ret == 0) {
std::cout << "load pdb failed !!!" << std::endl;
return 0;
}
// 打印 test 变量内容,已json格式输出
auto retJson = TCDUMP(test);
if (NULL == retJson) {
return false;
}
std::cout << retJson << std::endl;序列化结果retJson输出
- 附加接口
能够通过已知结构体大小返回大小一致的结构体名称1
2
3/ 打印pdb中大小为 n的类和结构体名称
std::cout << tcDump_GetSizeClass(sizeof(CTest)) << std::endl;
输出:---{ CTest }---
实现原理
com组件查找pdb文件
使用visual studio 编译后的c/c++工程在输出目录都会输出xxx.pdb文件,该pdb文件中包含了工程中类型的符号文件,在软件调试过程中非常重要。pdb文件格式的解析依赖com组件msdia120.dll,在成功注册msdia120.dll组件后,通过提供的方法对pdb文件进行解析:1
2
3
4
5
6
7
8
9
10
11
12
13HRESULT hr = ::CoInitialize(NULL);
{
CComPtr<IDiaDataSource> pDiaDataSource;
CComPtr<IDiaSession> pDiaSession;
CComPtr<IDiaSymbol> pGlobalSymbol;
CComBSTR bstrFilename = pdb_path;
if (LoadDataFromPdb(bstrFilename, pDiaDataSource, pDiaSession, pGlobalSymbol)) {
LoadAllUDTs(pGlobalSymbol);//解析所有数据类型,保存在全局变量g_map_udt中
LoadAllEnums(pGlobalSymbol);
ret = 1;
}
}
::CoUninitialize();
对变量进行序列化
1 | #define TCDUMP(ref_var) \ |
通过输入变量地址,通过关键字typeid获取变量类型,按照不同的变量类型进行不同方式的处理,比如已内置变量类型为列:1
2
3if (dump_as_builtin(type, ptr, root)) {
return true;
}
1 | bool dump_inv_as_builtin(const std::string& type, void* ptr, const Json::Value& root) |