-
Notifications
You must be signed in to change notification settings - Fork 91
/
integration.h
357 lines (303 loc) · 8.8 KB
/
integration.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef OTEL_CLR_PROFILER_INTEGRATION_H_
#define OTEL_CLR_PROFILER_INTEGRATION_H_
#include <corhlpr.h>
#include <iomanip>
#include <sstream>
#include <vector>
#include "string.h"
#undef major
#undef minor
namespace trace
{
const size_t kPublicKeySize = 8;
// PublicKey represents an Assembly Public Key token, which is an 8 byte binary
// RSA key.
struct PublicKey
{
const BYTE data[kPublicKeySize];
PublicKey() : data{0}
{
}
PublicKey(const BYTE (&arr)[kPublicKeySize]) : data{arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7]}
{
}
inline bool operator==(const PublicKey& other) const
{
for (int i = 0; i < kPublicKeySize; i++)
{
if (data[i] != other.data[i])
{
return false;
}
}
return true;
}
inline WSTRING str() const
{
std::stringstream ss;
for (int i = 0; i < kPublicKeySize; i++)
{
ss << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(data[i]);
}
return ToWSTRING(ss.str());
}
};
// Version is an Assembly version in the form Major.Minor.Build.Revision
// (1.0.0.0)
struct Version
{
const unsigned short major;
const unsigned short minor;
const unsigned short build;
const unsigned short revision;
Version() : major(0), minor(0), build(0), revision(0)
{
}
Version(const unsigned short major, const unsigned short minor, const unsigned short build,
const unsigned short revision) :
major(major), minor(minor), build(build), revision(revision)
{
}
inline bool operator==(const Version& other) const
{
return major == other.major && minor == other.minor && build == other.build && revision == other.revision;
}
inline bool operator!=(const Version& other) const
{
return !(*this == other);
}
inline WSTRING str() const
{
std::stringstream ss;
ss << major << "." << minor << "." << build << "." << revision;
return ToWSTRING(ss.str());
}
inline bool operator<(const Version& other) const
{
if (major < other.major)
{
return true;
}
if (major == other.major && minor < other.minor)
{
return true;
}
if (major == other.major && minor == other.minor && build < other.build)
{
return true;
}
return false;
}
inline bool operator>(const Version& other) const
{
return other < *this;
}
inline bool operator<=(const Version& other) const
{
return !(*this > other);
}
inline bool operator>=(const Version& other) const
{
return !(*this < other);
}
};
// An AssemblyReference is a reference to a .Net assembly. In general it will
// look like:
// Some.Assembly.Name, Version=1.0.0.0, Culture=neutral,
// PublicKeyToken=abcdef0123456789
struct AssemblyReference
{
const WSTRING name;
const Version version;
const WSTRING locale;
const PublicKey public_key;
AssemblyReference()
{
}
AssemblyReference(const WSTRING& str);
inline bool operator==(const AssemblyReference& other) const
{
return name == other.name && version == other.version && locale == other.locale &&
public_key == other.public_key;
}
inline WSTRING str() const
{
const auto ss = name + WStr(", Version=") + version.str() + WStr(", Culture=") + locale +
WStr(", PublicKeyToken=") + public_key.str();
return ss;
}
static AssemblyReference* GetFromCache(const WSTRING& str);
};
// A MethodSignature is a byte array. The format is:
// [calling convention, number of parameters, return type, parameter type...]
// For types see CorElementType
struct MethodSignature
{
public:
const std::vector<BYTE> data;
MethodSignature()
{
}
MethodSignature(const std::vector<BYTE>& data) : data(data)
{
}
inline bool operator==(const MethodSignature& other) const
{
return data == other.data;
}
CorCallingConvention CallingConvention() const
{
return CorCallingConvention(data.empty() ? 0 : data[0]);
}
size_t NumberOfTypeArguments() const
{
if (data.size() > 1 && (CallingConvention() & IMAGE_CEE_CS_CALLCONV_GENERIC) != 0)
{
return data[1];
}
return 0;
}
size_t NumberOfArguments() const
{
if (data.size() > 2 && (CallingConvention() & IMAGE_CEE_CS_CALLCONV_GENERIC) != 0)
{
return data[2];
}
if (data.size() > 1)
{
return data[1];
}
return 0;
}
bool ReturnTypeIsObject() const
{
if (data.size() > 2 && (CallingConvention() & IMAGE_CEE_CS_CALLCONV_GENERIC) != 0)
{
return data[3] == ELEMENT_TYPE_OBJECT;
}
if (data.size() > 1)
{
return data[2] == ELEMENT_TYPE_OBJECT;
}
return false;
}
size_t IndexOfReturnType() const
{
if (data.size() > 2 && (CallingConvention() & IMAGE_CEE_CS_CALLCONV_GENERIC) != 0)
{
return 3;
}
if (data.size() > 1)
{
return 2;
}
return 0;
}
WSTRING str() const
{
std::stringstream ss;
for (auto& b : data)
{
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(b);
}
return ToWSTRING(ss.str());
}
BOOL IsInstanceMethod() const
{
return (CallingConvention() & IMAGE_CEE_CS_CALLCONV_HASTHIS) != 0;
}
};
struct TypeReference
{
const AssemblyReference assembly;
const WSTRING name;
const Version min_version;
const Version max_version;
TypeReference() : min_version(Version(0, 0, 0, 0)), max_version(Version(USHRT_MAX, USHRT_MAX, USHRT_MAX, USHRT_MAX))
{
}
TypeReference(const WSTRING& assembly_name, WSTRING type_name, Version min_version, Version max_version) :
assembly(*AssemblyReference::GetFromCache(assembly_name)),
name(type_name),
min_version(min_version),
max_version(max_version)
{
}
inline WSTRING get_cache_key() const
{
return WStr("[") + assembly.name + WStr("]") + name + WStr("_vMin_") + min_version.str() + WStr("_vMax_") +
max_version.str();
}
inline bool operator==(const TypeReference& other) const
{
return assembly == other.assembly && name == other.name && min_version == other.min_version &&
max_version == other.max_version;
}
};
struct MethodReference
{
const TypeReference type;
const WSTRING method_name;
const std::vector<WSTRING> signature_types;
MethodReference()
{
}
MethodReference(const WSTRING& assembly_name, WSTRING type_name, WSTRING method_name, Version min_version,
Version max_version, const std::vector<WSTRING>& signature_types) :
type(assembly_name, type_name, min_version, max_version),
method_name(method_name),
signature_types(signature_types)
{
}
inline bool operator==(const MethodReference& other) const
{
return type == other.type && method_name == other.method_name;
}
};
struct IntegrationDefinition
{
const MethodReference target_method;
const TypeReference integration_type;
const bool is_derived = false;
IntegrationDefinition()
{
}
IntegrationDefinition(MethodReference target_method, TypeReference integration_type, bool isDerived) :
target_method(target_method), integration_type(integration_type), is_derived(isDerived)
{
}
inline bool operator==(const IntegrationDefinition& other) const
{
return target_method == other.target_method && integration_type == other.integration_type &&
is_derived == other.is_derived;
}
};
typedef struct _CallTargetDefinition
{
WCHAR* targetAssembly;
WCHAR* targetType;
WCHAR* targetMethod;
WCHAR** signatureTypes;
USHORT signatureTypesLength;
USHORT targetMinimumMajor;
USHORT targetMinimumMinor;
USHORT targetMinimumPatch;
USHORT targetMaximumMajor;
USHORT targetMaximumMinor;
USHORT targetMaximumPatch;
WCHAR* integrationAssembly;
WCHAR* integrationType;
} CallTargetDefinition;
namespace
{
WSTRING GetNameFromAssemblyReferenceString(const WSTRING& wstr);
Version GetVersionFromAssemblyReferenceString(const WSTRING& wstr);
WSTRING GetLocaleFromAssemblyReferenceString(const WSTRING& wstr);
PublicKey GetPublicKeyFromAssemblyReferenceString(const WSTRING& wstr);
} // namespace
} // namespace trace
#endif // OTEL_CLR_PROFILER_INTEGRATION_H_