-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProgram.cs
More file actions
296 lines (247 loc) · 12.1 KB
/
Program.cs
File metadata and controls
296 lines (247 loc) · 12.1 KB
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
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT License.
Abstract:
Reads from Manifested ETW (via Microsoft.Windows.EventTracing aka 'DataLayer' ) and converts into CLOG property bag. This bag is then sent into a generic CLOG function for output to STDOUT
ETL file containing ETW data -> DataLayer -> clog2text_windows -> generic clog STDOUT
--*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using clogutils;
using CommandLine;
using Microsoft.Windows.EventTracing;
using Microsoft.Windows.EventTracing.Events;
using static clogutils.CLogConsoleTrace;
namespace clog2text_windows
{
internal class Program
{
private static int Main(string[] cmdLineArgs)
{
ParserResult<CommandLineArguments> o = Parser.Default.ParseArguments<CommandLineArguments>(cmdLineArgs);
return o.MapResult(
options =>
{
string sidecarJson = File.ReadAllText(options.SideCarFile);
CLogSidecar sidecar = CLogSidecar.FromJson(sidecarJson);
TextReader file = Console.In;
if (!File.Exists(options.ETLFile))
{
TraceLine(TraceType.Err, $"ETL File {options.ETLFile} doesnt exist");
return -1;
}
StreamWriter outputfile = null;
if (!String.IsNullOrEmpty(options.OutputFile))
outputfile = new StreamWriter(new FileStream(options.OutputFile, FileMode.Create));
try
{
TraceProcessorSettings traceSettings = new TraceProcessorSettings { AllowLostEvents = true, AllowTimeInversion = true };
using (ITraceProcessor etwfile = TraceProcessor.Create(options.ETLFile, traceSettings))
{
HashSet<Guid> ids = new HashSet<Guid>();
foreach (var m in sidecar.EventBundlesV2)
{
foreach (var prop in m.Value.ModuleProperites)
{
if (prop.Key.Equals("MANIFESTED_ETW"))
{
ids.Add(new Guid(prop.Value["ETW_Provider"]));
}
else if (prop.Key.Equals("TRACELOGGING"))
{
ids.Add(new Guid(prop.Value["ETW_Provider"]));
}
}
}
var events = etwfile.UseGenericEvents(ids.ToArray());
etwfile.Process();
foreach (var e in events.Result.Events)
{
string line = "";
try
{
Dictionary<string, IClogEventArg> fixedUpArgs = new Dictionary<string, IClogEventArg>();
string errorString = "ERROR";
if (null == e.Fields)
{
continue;
}
Dictionary<string, IClogEventArg> args = new Dictionary<string, IClogEventArg>();
foreach (var f in e.Fields)
{
args[f.Name] = new ManifestedETWEvent(f);
}
CLogDecodedTraceLine bundle = null;
int eidAsInt = -1;
foreach (var b in sidecar.EventBundlesV2)
{
Dictionary<string, string> keys;
if (!e.IsTraceLogging)
{
if (!b.Value.ModuleProperites.TryGetValue("MANIFESTED_ETW", out keys))
{
continue;
}
string eid;
if (!keys.TryGetValue("EventID", out eid))
{
continue;
}
eidAsInt = Convert.ToInt32(eid);
if (eidAsInt == e.Id)
{
bundle = b.Value;
errorString = "ERROR:" + eidAsInt;
break;
}
}
else
{
if (e.ActivityName.Equals(b.Key))
{
bundle = b.Value;
errorString = "ERROR:" + b.Key;
break;
}
}
}
if (null == bundle)
{
continue;
}
Dictionary<string, string> argMap;
if (e.IsTraceLogging)
{
argMap = new Dictionary<string, string>();
foreach (var arg in args)
{
argMap[arg.Key] = arg.Key;
}
}
else
{
argMap = sidecar.GetTracelineMetadata(bundle, "MANIFESTED_ETW");
}
var types = CLogFileProcessor.BuildTypes(sidecar.ConfigFile, null, bundle.TraceString, null, out CLogFileProcessor.DecomposedString clean);
if (0 == types.Length)
{
errorString = bundle.TraceString;
goto toPrint;
}
int argIndex = 0;
foreach (var type in types)
{
var arg = bundle.splitArgs[argIndex];
CLogEncodingCLogTypeSearch node = sidecar.ConfigFile.FindType(arg);
switch (node.EncodingType)
{
case CLogEncodingType.Synthesized:
continue;
case CLogEncodingType.Skip:
continue;
}
string lookupArgName = argMap[arg.MacroVariableName];
if (!args.ContainsKey(lookupArgName))
{
Console.WriteLine($"Argmap missing {lookupArgName}");
throw new Exception("InvalidType : " + node.DefinationEncoding);
}
if (0 != node.DefinationEncoding.CompareTo(type.TypeNode.DefinationEncoding))
{
Console.WriteLine("Invalid Types in Traceline");
throw new Exception("InvalidType : " + node.DefinationEncoding);
}
fixedUpArgs[arg.MacroVariableName] = args[lookupArgName];
++argIndex;
}
toPrint:
EventInformation ei = new EventInformation();
ei.Timestamp = e.Timestamp.DateTimeOffset;
ei.ProcessId = e.ProcessId.ToString("x");
ei.ThreadId = e.ThreadId.ToString("x");
DecodeAndTraceToConsole(outputfile, bundle, errorString, sidecar.ConfigFile, fixedUpArgs, ei, options.ShowTimestamps, options.ShowCPUInfo);
}
catch (Exception)
{
Console.WriteLine($"Invalid TraceLine : {line}");
}
}
}
}
catch (Exception e)
{
CLogConsoleTrace.TraceLine(TraceType.Err, "ERROR : " + e);
if (null != outputfile)
{
outputfile.WriteLine("ERROR : " + e);
}
}
finally
{
if (null != outputfile)
{
outputfile.Flush();
outputfile.Close();
}
}
return 0;
}, err =>
{
Console.WriteLine("Bad Args : " + err);
return -1;
});
}
public class ManifestedETWEvent : IClogEventArg
{
private readonly IGenericEventField _event;
public ManifestedETWEvent(IGenericEventField e)
{
_event = e;
}
public byte[] AsBinary
{
get
{
switch (_event.Type)
{
case GenericEventFieldType.ByteList:
return _event.AsByteList.ToArray();
case GenericEventFieldType.Binary:
return _event.AsBinary.ToArray();
default:
throw new InvalidDataException("Invalid ETW Encoding : " + _event.Type);
}
}
}
public ulong AsPointer
{
get
{
switch (_event.Type)
{
case GenericEventFieldType.UInt64:
return _event.AsUInt64;
case GenericEventFieldType.Address:
return (ulong)_event.AsAddress.Value;
default:
throw new InvalidDataException("InvalidTypeForPointer:" + _event.Type);
}
}
}
public string AsString
{
get { return _event.ToString(); }
}
public int AsInt32
{
get { return _event.AsInt32; }
}
public uint AsUInt32
{
get { return _event.AsUInt32; }
}
}
}
}