windows下c++反射功能

背景

c++开发的软件在生产环境中运行出现一些bug,比如参数错误等问题,通过日志只能打印异常的行号或函数,但具体的结构体/类的参数内容打印却非常麻烦,无法做到一些更高级语言的反射功能。
要做到能够通过变量地址就打印变量的内容信息,就需要依赖ms提供的msdia120.dll和程序编译后的pdb文件。

原理

msdia120.dll 需要注册到目标机器,通过提供的接口函数可以解析pdb文件中的结构体、类,并提供这个成员变量的类型、相对的地址偏移。

效果

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <typeinfo>
#include "tcDump.h"
#include <string>
#include <vector>
#include <map>
#include <list>


struct STTEST
{
int a;
double b;
float c;
short d;
char szName[32];
void *p;
};

class CClassTest
{
public:
std::string m_strName;
std::vector<int> m_oVec;
std::map<int, std::string> m_oMap;
//std::list<int> m_oList; // 不支持,慢慢完善
};

int _tmain(int argc, _TCHAR* argv[])
{
if (0 == TCDUMP_INIT("ConsoleApplication1.pdb")){
return false;
}
//结构体
{
STTEST st = { 0 };
st.a = 1;
st.b = 1.2;
st.c = 2.3;
st.d = 655;
memcpy(st.szName, "hello world", 12);
st.p = (void*)0x123456;

auto json = TCDUMP(st);
if (NULL == json){
return false;
}

printf("%s\n", json);
}

//类对象
{
CClassTest oTest;
oTest.m_strName = "this is a test for string";
oTest.m_oVec.push_back(6);
oTest.m_oVec.push_back(7);
oTest.m_oVec.push_back(8);

oTest.m_oMap[0] = "this is 0";
oTest.m_oMap[1] = "this is 1";
oTest.m_oMap[2] = "this is 2";

auto json = TCDUMP(oTest);
if (NULL == json){
return false;
}

printf("%s\n", json);
}

return 0;
}

输出效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"a" : 1,
"b" : 1.2,
"c" : 2.2999999523162842,
"d" : 655,
"p" : "[0x00123456]",
"szName" : "hello world"
}

{
"m_oMap" : {
"0" : "this is 0",
"1" : "this is 1",
"2" : "this is 2"
},
"m_oVec" : [ 6, 7, 8 ],
"m_strName" : "this is a test for string"
}

后续在每个关键函数的入口增加一个宏定义,就可以轻松的定位参数错误的问题,而省去了大量的远程调试时间。

二次开发

visual studio 的安装路径下存在一个demo程序:DIA SDK 。可以通过修改其中的代码实现,当然
我的百度网盘有封装后的sdk更加容易使用。

https://pan.baidu.com/s/1takuuHhxGt_WkLQWGDeacQ

  • msdia120.dll
  • tcDump.dll
  • tcDump.h
  • tcDump.lib
  • tcDumpD.lib
  • tcDumpD.dll
  • 使用说明.txt