forked from Djuffin/dbg-linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.h
196 lines (170 loc) · 5.17 KB
/
metadata.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#define ASSERTX(expr) { if (!(expr)) { printf("ASSERT FAILED %d\n", __LINE__); } }
IMetaDataImport2 *get_metadata(ICorDebugModule *module)
{
IUnknown *unkMetadata;
module->GetMetaDataInterface(IID_IMetaDataImport2, (IUnknown **)&unkMetadata);
ASSERTX(unkMetadata != nullptr);
IMetaDataImport2 *metadata;
unkMetadata->QueryInterface(IID_IMetaDataImport2, (void**)&metadata);
ASSERTX(metadata != nullptr);
return metadata;
}
void print_class(ICorDebugClass *type)
{
ICorDebugModule *module = NULL;
type->GetModule(&module);
mdTypeDef tkType;
type->GetToken(&tkType);
auto metadata = get_metadata(module);
WCHAR type_name[200];
ULONG len;
DWORD flags;
mdToken baseType;
metadata->GetTypeDefProps(tkType,
type_name,
sizeof(type_name) / sizeof(type_name[0]),
&len,
&flags,
&baseType
);
printf("%s", to_ascii(type_name));
}
void print_function(ICorDebugFunction *func)
{
ICorDebugModule *module = NULL;
func->GetModule(&module);
mdMethodDef tkMethod;
mdTypeDef tkType;
func->GetToken(&tkMethod);
auto metadata = get_metadata(module);
WCHAR fname[200], type_name[200];
ULONG len;
DWORD flags;
metadata->GetMethodProps(tkMethod,
&tkType,
fname,
sizeof(fname) / sizeof(fname[0]),
&len,
NULL, //pdwAttr
NULL, //sig blob
NULL, //sig blob count
NULL, //RVA
&flags
);
mdToken baseType;
metadata->GetTypeDefProps(tkType,
type_name,
sizeof(type_name) / sizeof(type_name[0]),
&len,
&flags,
&baseType
);
printf("%s.%s\n", to_ascii(type_name), to_ascii(fname));
}
void print_callstack(ICorDebugThread *thread)
{
ICorDebugChain *chain = NULL;
int hr = thread->GetActiveChain(&chain);
if (chain == nullptr) {
printf("thread->GetActiveChain failed %08x\n", hr);
return;
}
ICorDebugFrameEnum *frameEnum = NULL;
hr = chain->EnumerateFrames(&frameEnum);
if (frameEnum == nullptr) {
printf("chain->EnumerateFrames failed %08x\n", hr);
return;
}
ICorDebugFrame *frames[100];
ULONG frameCount = 0;
frameEnum->Next(sizeof(frames)/sizeof(frames[0]),
frames,
&frameCount);
for (int i = 0; i < frameCount; i++)
{
printf("#%d: ", i);
ICorDebugFunction *func = NULL;
frames[i]->GetFunction(&func);
if (func)
{
print_function(func);
}
}
}
void print_all_callstakcs(ICorDebugController *controller)
{
ICorDebugThreadEnum *threadsEnum;
int hr = controller->EnumerateThreads(&threadsEnum);
if (threadsEnum == nullptr) {
printf("controller->EnumerateThreads failed %08x\n", hr);
return;
}
ICorDebugThread *threads[100];
ULONG count;
threadsEnum->Next(100, threads, &count);
for (int i = 0; i < count; i++)
{
printf("Thread #%d\n", i);
print_callstack(threads[i]);
}
}
void enum_types(ICorDebugModule *module)
{
auto metadata = get_metadata(module);
mdTypeDef typeTokens[3000];
ULONG typesCount;
HCORENUM typeEnum = NULL;
metadata->EnumTypeDefs(&typeEnum, typeTokens, sizeof(typeTokens) / sizeof(typeTokens[0]), &typesCount);
printf("Types found: %d\n", typesCount);
for (int i = 0; i < typesCount; i++)
{
auto tkType = typeTokens[i];
WCHAR name[200];
ULONG len;
DWORD flags;
mdToken baseType;
metadata->GetTypeDefProps(tkType,
name,
sizeof(name) / sizeof(name[0]),
&len,
&flags,
&baseType
);
printf("\t\ttype: %s\n", to_ascii(name));
HCORENUM methodEnum = NULL;
mdMethodDef methodTokens[3000];
ULONG methodCount;
metadata->EnumMethods(&methodEnum, tkType, methodTokens, sizeof(methodTokens) / sizeof(methodTokens[0]) ,&methodCount);
for (int j = 0; j < methodCount; j++)
{
metadata->GetMethodProps(methodTokens[j],
NULL, //class
name,
sizeof(name) / sizeof(name[0]),
&len,
NULL, //pdwAttr
NULL, //sig blob
NULL, //sig blob count
NULL, //RVA
&flags
);
printf("\t\t\tmethod: %s\n", to_ascii(name));
{
printf("\tBreakpoint being created\n");
ICorDebugFunction *mainFunc = NULL;
module->GetFunctionFromToken(methodTokens[j], &mainFunc);
ICorDebugFunctionBreakpoint *bp = NULL;
HRESULT hr = mainFunc->CreateBreakpoint(&bp);
if (hr == 0)
{
hr = bp->Activate(TRUE);
printf("\tActivate(TRUE) breakpoint %08x\n", hr);
}
else
{
printf("\tCreate breakpoint %s FAILED %08x\n", (char *)name, hr);
}
}
}
}
}