-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsedEtwManifest.cs
277 lines (268 loc) · 11.9 KB
/
ParsedEtwManifest.cs
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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
using System.Diagnostics;
using System.Text;
namespace etwlib
{
public class ParsedEtwManifest
{
public ParsedEtwProvider Provider { get; set; }
public List<ParsedEtwManifestEvent> Events { get; set; }
public List<ParsedEtwManifestField> Channels { get; set; }
public List<ParsedEtwManifestField> Keywords { get; set; }
public Dictionary<ParsedEtwManifestField, List<ParsedEtwManifestField>> Tasks { get; set; }
public List<ParsedEtwManifestField> GlobalOpcodes { get; set; }
public Dictionary<string, List<ParsedEtwTemplateItem>> Templates { get; set; }
public List<string> StringTable { get; set; }
public ParsedEtwManifest()
{
Provider = new ParsedEtwProvider();
Events = new List<ParsedEtwManifestEvent>();
Channels = new List<ParsedEtwManifestField>();
Keywords = new List<ParsedEtwManifestField>();
Tasks = new Dictionary<ParsedEtwManifestField, List<ParsedEtwManifestField>>();
GlobalOpcodes = new List<ParsedEtwManifestField>();
Templates = new Dictionary<string, List<ParsedEtwTemplateItem>>();
StringTable = new List<string>();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Provider: {Provider}");
foreach (var evt in Events)
{
sb.AppendLine($" {evt}");
}
sb.AppendLine($"Channels:");
foreach (var channel in Channels)
{
sb.AppendLine($" {channel}");
}
sb.AppendLine($"Keywords:");
foreach (var kw in Keywords)
{
sb.AppendLine($" {kw}");
}
sb.AppendLine($"Tasks:");
foreach (var kvp in Tasks)
{
var task = kvp.Key;
sb.AppendLine($" {task} opcodes:");
foreach (var opcode in kvp.Value)
{
sb.AppendLine($" {opcode}");
}
}
if (GlobalOpcodes.Count > 0)
{
sb.AppendLine($" Global opcodes:");
foreach (var opcode in GlobalOpcodes)
{
sb.AppendLine($" {opcode}");
}
}
sb.AppendLine($"Templates:");
foreach (var kvp in Templates)
{
sb.AppendLine($" Template {kvp.Key}:");
foreach (var item in kvp.Value)
{
sb.AppendLine($" {item}");
}
}
sb.AppendLine($"String table:");
foreach (var str in StringTable)
{
sb.AppendLine($" {str}");
}
return sb.ToString();
}
public string ToXml()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(@"<?xml version=""1.0"" encoding=""utf-8""?>
<instrumentationManifest xmlns=""http://schemas.microsoft.com/win/2004/08/events""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xs=""http://www.w3.org/2001/XMLSchema""
xmlns:win=""http://manifests.microsoft.com/win/2004/08/windows/events""
xsi:schemaLocation=""http://schemas.microsoft.com/win/2004/08/events eventman.xsd"">");
sb.AppendLine("<instrumentation>");
sb.AppendLine(" <events>");
sb.AppendLine($" <provider symbol=\"{Provider.Name}\" name=\"{Provider.Name}\" " +
$"guid=\"{{{Provider.Id}}}\" source=\"Xml\" messageFileName=\"noMessageFile\" " +
$"resourceFileName=\"noResourceFile\">");
sb.AppendLine(" <events>");
foreach (var evt in Events)
{
var template = !string.IsNullOrEmpty(evt.Template) ?
$" template=\"{evt.Template}\"" : "";
var task = !string.IsNullOrEmpty(evt.Task) ?
$" task=\"{evt.Task}\"" : "";
var opcode = !string.IsNullOrEmpty(evt.Opcode) ?
$" opcode=\"{evt.Opcode}\"" : "";
var channel = !string.IsNullOrEmpty(evt.Channel) ?
$" channel=\"{evt.Channel}\"" : "";
var keywords = !string.IsNullOrEmpty(evt.Keywords) ?
$" keywords=\"{evt.Keywords}\"" : "";
var level = ProviderParser.TraceLevelToSchemaType(evt.Level);
sb.AppendLine($" <event value=\"{evt.Id}\" " +
$"version=\"{evt.Version}\" level=\"{level}\" " +
$"{channel}{task}{opcode}{keywords}{template}/>");
}
sb.AppendLine(" </events>");
sb.AppendLine(" <channels>");
var knownChannelTypes = new List<string> {
"Admin", "Operational", "Analytic", "Debug" };
foreach (var channel in Channels)
{
var chType = "unknown";
//
// Windows supports 4 channel types according to
// https://learn.microsoft.com/en-us/windows/win32/wes/defining-channels
// and the convention is to include the type in the channel name:
// <myChannelName>/<Type>
// This is either outdated or plain wrong, as Windows-Kernel-Registry uses
// "Performance". MC won't accept it and TDH won't load it.
//
var pos = channel.Name.IndexOf("/");
if (pos >= 0)
{
var start = pos + 1;
var length = channel.Name.Length - start;
chType = channel.Name.Substring(start, length);
if (!knownChannelTypes.Contains(chType))
{
chType = "Debug"; // lol whatever
}
}
sb.AppendLine($" <channel value=\"{channel.Value}\" " +
$"name=\"{channel.Name}\" type=\"{chType}\"/>");
}
sb.AppendLine(" </channels>");
sb.AppendLine(" <keywords>");
foreach (var kw in Keywords)
{
var name = kw.Name;
int index = StringTable.FindIndex(s => s == name);
Debug.Assert(index >= 0);
sb.AppendLine($" <keyword name=\"{name}\" mask=\"" +
$"0x{kw.Value:X}\" message=\"$(string.string{index})\" />");
}
sb.AppendLine(" </keywords>");
sb.AppendLine(" <tasks>");
foreach (var kvp in Tasks)
{
var task = kvp.Key;
if (task.Value == 0 || string.IsNullOrEmpty(task.Name))
{
//
// Tasks with value of 0 are default tasks.
//
continue;
}
sb.AppendLine($" <task value=\"{task.Value}\" name=\"" +
$"{task.Name}\" message=\"$(string.string{task.Value})\">");
if (kvp.Value.Count > 0)
{
sb.AppendLine(" <opcodes>");
foreach (var opcode in kvp.Value)
{
var message = "";
if (!string.IsNullOrEmpty(opcode.Name))
{
int index2 = StringTable.FindIndex(s => s == opcode.Name);
Debug.Assert(index2 >= 0);
message = $" message=\"$(string.string{index2})\"";
}
int index = StringTable.FindIndex(s => s == opcode.Name);
Debug.Assert(index >= 0);
sb.AppendLine($" <opcode value=\"{opcode.Value}\" name=\"" +
$"{opcode.Name}\"{message}/>");
}
sb.AppendLine(" </opcodes>");
}
sb.AppendLine($" </task>");
}
sb.AppendLine(" </tasks>");
if (GlobalOpcodes.Count > 0)
{
sb.AppendLine(" <opcodes>");
foreach (var opcode in GlobalOpcodes)
{
var message = "";
if (!string.IsNullOrEmpty(opcode.Name))
{
int index = StringTable.FindIndex(s => s == opcode.Name);
Debug.Assert(index >= 0);
message = $" message=\"$(string.string{index})\"";
}
sb.AppendLine($" <opcode value=\"{opcode.Value}\" name=\"" +
$"{opcode.Name}\"{message}/>");
}
sb.AppendLine(" </opcodes>");
}
sb.AppendLine(" <templates>");
foreach (var kvp in Templates)
{
sb.AppendLine($" <template tid=\"{kvp.Key}\">");
foreach (var item in kvp.Value)
{
//
// Length or count is the name of the field that contains that value
// in an actual runtime event
//
var lengthOrCountAttribute = "";
if (item.FieldBackreference != null)
{
if (item.FieldBackreference.IsCountedField)
{
lengthOrCountAttribute =
$"count=\"{item.FieldBackreference.FieldName}\"";
}
else
{
lengthOrCountAttribute =
$"length=\"{item.FieldBackreference.FieldName}\"";
}
}
var inType = ProviderParser.TdhInputTypeToSchemaType(item.InType);
var outType = ProviderParser.TdhOutputTypeToSchemaType(item.OutType);
sb.AppendLine($" <data name=\"{item.Name}\" inType=\"" +
$"{inType}\" outType=\"{outType}\" {lengthOrCountAttribute} />");
}
sb.AppendLine($" </template>");
}
sb.AppendLine(" </templates>");
sb.AppendLine(" </provider>");
sb.AppendLine(" </events>");
sb.AppendLine("</instrumentation>");
sb.AppendLine("<localization>");
sb.AppendLine(@" <resources culture=""en-US"">");
sb.AppendLine(" <stringTable>");
int count = 0;
foreach (var str in StringTable)
{
sb.AppendLine($" <string id=\"string{count++}\" value=\"{str}\" />");
}
sb.AppendLine(" </stringTable>");
sb.AppendLine(" </resources>");
sb.AppendLine("</localization>");
sb.AppendLine("</instrumentationManifest>");
return sb.ToString();
}
}
}