-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProviderParser.cs
1290 lines (1219 loc) · 48.5 KB
/
ProviderParser.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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.Runtime.InteropServices;
namespace etwlib
{
using static TraceLogger;
using static NativeTraceConsumer;
public class ManifestNotFoundException : Exception
{
public ManifestNotFoundException(string message)
: base(message)
{
}
}
public static class ProviderParser
{
public
static
ParsedEtwProvider?
GetProvider(string Name)
{
var providers = GetProviders();
return providers.FirstOrDefault(p => p.Name?.ToLower() == Name?.ToLower());
}
public
static
ParsedEtwProvider?
GetProvider(Guid Id)
{
var providers = GetProviders();
return providers.FirstOrDefault(p => p.Id == Id);
}
public
static
List<ParsedEtwProvider>
GetProviders()
{
var results = new List<ParsedEtwProvider>();
nint buffer = nint.Zero;
try
{
uint result = 0;
uint bufferSize = 0;
result = TdhEnumerateProviders(nint.Zero, ref bufferSize);
if (result != ERROR_INSUFFICIENT_BUFFER)
{
var error = $"TdhEnumerateProviders failed: 0x{result:X}";
Trace(TraceLoggerType.EtwProviderParser,
TraceEventType.Error,
error);
throw new Exception(error);
}
buffer = Marshal.AllocHGlobal((int)bufferSize);
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
result = TdhEnumerateProviders(buffer, ref bufferSize);
if (result != ERROR_SUCCESS)
{
var error = $"TdhEnumerateProviders failed(2): 0x{result:X}";
Trace(TraceLoggerType.EtwProviderParser,
TraceEventType.Error,
error);
throw new Exception(error);
}
var providerInfoList = (PROVIDER_ENUMERATION_INFO)Marshal.PtrToStructure(
buffer, typeof(PROVIDER_ENUMERATION_INFO))!;
if (providerInfoList.NumberOfProviders == 0)
{
throw new Exception("There are no ETW providers.");
}
var pointer = nint.Add(buffer,
(int)Marshal.OffsetOf<PROVIDER_ENUMERATION_INFO>("TraceProviderInfoArray").ToInt64());
var end = nint.Add(buffer, (int)bufferSize);
for (int i = 0; i < providerInfoList.NumberOfProviders; i++)
{
var providerInfo = (TRACE_PROVIDER_INFO)Marshal.PtrToStructure(
pointer, typeof(TRACE_PROVIDER_INFO))!;
var provider = new ParsedEtwProvider();
provider.Id = providerInfo.ProviderGuid;
//
// The "source" retrieved using this TDH API only returns 0 or 1 for
// XML vs MOF. But providers specified in ETW events retrieved at
// runtime can be from other non-manifest sources like TraceLog.
//
provider.Source = providerInfo.SchemaSource == 0 ? "xml" : "MOF";
provider.Name = "(unnamed)";
provider.HasManifest = IsManifestKnown(provider.Id);
if (providerInfo.ProviderNameOffset != 0)
{
var target = nint.Add(buffer, (int)providerInfo.ProviderNameOffset);
if (target > end)
{
throw new Exception($"Provider name offset 0x{providerInfo.ProviderNameOffset:X} " +
$"extends beyond buffer's end address 0x{end}");
}
var name = Marshal.PtrToStringUni(target)!;
if (!string.IsNullOrEmpty(name))
{
provider.Name = name;
}
}
results.Add(provider);
pointer = nint.Add(pointer, Marshal.SizeOf(typeof(TRACE_PROVIDER_INFO)));
}
results.Sort();
return results;
}
catch (Exception ex)
{
Trace(TraceLoggerType.EtwProviderParser,
TraceEventType.Error,
$"Exception in GetProviders(): {ex.Message}");
throw;
}
finally
{
if (buffer != nint.Zero)
{
Marshal.FreeHGlobal(buffer);
}
}
}
public
static
ParsedEtwManifest
GetManifest(Guid ProviderGuid, [In, Optional] string ManifestLocation)
{
var parsedManifest = new ParsedEtwManifest();
var fieldResults = new List<ParsedEtwManifestField>();
var manifestLoaded = false;
//
// If a manifest location was provided, ask TDH to load it.
//
if (!string.IsNullOrEmpty(ManifestLocation))
{
try
{
LoadProviderManifest(ManifestLocation);
manifestLoaded = true;
}
catch (Exception ex)
{
throw new Exception($"TDH was unable to load the manifest " +
$"{ManifestLocation}: {ex.Message}");
}
}
//
// Get all supported keywords from TDH, minus reserved keywords.
//
// Note: The top 16 bits of the keyword are reserved by Microsoft, and appear
// to be created from channel information (ie, Debug=0x8000000000000000). We
// can't include them in the manifest, because MC.exe only considers the first
// two bytes in the keyword mask string - so 0x8000 in this example might
// collide with a custom keyword, causing TDH to throw an XML error.
//
parsedManifest.Keywords = GetProviderSupportedFields(
ProviderGuid, 0x0000ffffffffffff, EVENT_FIELD_TYPE.KeywordInformation);
//
// Get all supported channels from TDH
//
for (int i = 0; i < char.MaxValue; i++)
{
fieldResults = GetProviderSupportedFields(
ProviderGuid, (ulong)i, EVENT_FIELD_TYPE.ChannelInformation);
if (fieldResults.Count > 0)
{
parsedManifest.Channels.AddRange(fieldResults);
}
}
//
// Get all supported tasks from TDH (if any)
//
for (int i = 0; i < ushort.MaxValue; i++)
{
fieldResults = GetProviderSupportedFields(
ProviderGuid, (ulong)i, EVENT_FIELD_TYPE.TaskInformation);
foreach (var field in fieldResults)
{
if (!parsedManifest.Tasks.ContainsKey(field))
{
parsedManifest.Tasks.Add(field, new List<ParsedEtwManifestField>());
}
}
}
//
// Get all supported task-specific opcodes from TDH (if any)
//
foreach (var task in parsedManifest.Tasks.Keys)
{
//
// Custom opcodes must be in the range from 10 through 239
//
for (int i = 10; i <= 239; i++)
{
//
// Bits 0 - 15 must contain the task value
// Bits 16 - 23 must contain the opcode value
//
var value = ((ulong)i) << 16 | (ushort)task.Value;
fieldResults = GetProviderSupportedFields(
ProviderGuid, value, EVENT_FIELD_TYPE.OpcodeInformation);
if (fieldResults.Count > 0)
{
parsedManifest.Tasks[task].AddRange(fieldResults);
}
}
}
//
// Create a "default task" for reserved opcodes (eg, "win:Start") are 0-9.
//
var defaultTask = new ParsedEtwManifestField("(no task)", "default task", 0);
parsedManifest.Tasks.Add(defaultTask, new List<ParsedEtwManifestField>());
for (int i = 0; i < 10; i++)
{
//
// Bits 0 - 15 must contain the task value (no task here)
// Bits 16 - 23 must contain the opcode value
//
var value = ((ulong)i) << 16;
fieldResults = GetProviderSupportedFields(
ProviderGuid, value, EVENT_FIELD_TYPE.OpcodeInformation);
if (fieldResults.Count > 0)
{
parsedManifest.Tasks[defaultTask].AddRange(fieldResults);
}
}
//
// There can be opcodes that are not nested in tasks, almost like a "global"
// opcode that can be used with any task. If the provider defines such an
// opcode, the TDH api will rightly return global opcodes as well as task-
// specific opcodes whenever a task is queried (above). This means global
// opcodes will be repeated in every task's opcode list. Oddly, MC.exe will
// complain about a manifest that has global opcodes repeated as task-local.
// So we need to go cull all those out now into a separate bin, so our manifest
// generation code knows to put them in their own dedicated "<opcodes>" block.
//
for (int i = 0; i < parsedManifest.Tasks.Count; i++)
{
var opcodes = parsedManifest.Tasks.ElementAt(i).Value;
for (int j = i + 1; j < parsedManifest.Tasks.Count - 1; j++)
{
var opcodes2 = parsedManifest.Tasks.ElementAt(j).Value;
var duplicates = opcodes.Intersect(opcodes2).ToList();
duplicates.ForEach(d =>
{
if (!parsedManifest.GlobalOpcodes.Contains(d))
{
parsedManifest.GlobalOpcodes.Add(d);
}
});
}
}
for (int i = 0; i < parsedManifest.Tasks.Count; i++)
{
_ = parsedManifest.Tasks.ElementAt(i).Value.RemoveAll(
r => parsedManifest.GlobalOpcodes.Any(a => a == r));
}
//
// This is a bit hacky. Use our EventParser class to parse meta information
// about a provider's events from their manifest. This is overloading the
// intended purpose of the parser (which is to parse actual live events, not
// structural manifest events)
//
var events = GetProviderSupportedEvents(ProviderGuid);
foreach (var parsedEvent in events)
{
//
// The provider should be the same across every event, but just
// assign it always.
//
if (string.IsNullOrEmpty(parsedManifest.Provider.Name) &&
!string.IsNullOrEmpty(parsedEvent.Provider.Name))
{
parsedManifest.Provider = parsedEvent.Provider;
parsedManifest.StringTable.Add(parsedManifest.Provider.Name);
}
//
// Extract template data
//
var template = "";
if (parsedEvent.TemplateData != null && parsedEvent.TemplateData.Count > 0)
{
var found = false;
foreach (var kvp in parsedManifest.Templates)
{
var existingTemplate = kvp.Value;
if (parsedEvent.TemplateData.Intersect(
existingTemplate).Count() == parsedEvent.TemplateData.Count())
{
found = true;
break;
}
}
if (!found)
{
var name = $"Args{parsedEvent.EventId}_{parsedEvent.Version}";
if (parsedManifest.Templates.ContainsKey(name))
{
Debug.Assert(false);
continue;
}
parsedManifest.Templates.Add(name, parsedEvent.TemplateData);
template = name;
}
}
//
// Lookup values from parsed manifest.
//
var channel = "";
var opcode = "";
var task = "";
var keywords = "";
if (parsedEvent.KeywordsUlong != 0)
{
//
// We have to mask off reserved keywords here also, because we excluded
// them above when we queries all known keywords.
//
var value = parsedEvent.KeywordsUlong & 0x0000ffffffffffff;
var keywordFields = GetProviderSupportedFields(
ProviderGuid,
value,
EVENT_FIELD_TYPE.KeywordInformation);
foreach (var keyword in keywordFields)
{
Debug.Assert(parsedManifest.Keywords.Contains(keyword));
}
keywords = string.Join(' ', keywordFields.Select(
k => k.Name).ToList());
}
if (parsedEvent.Channel != null)
{
var match = parsedManifest.Channels.FirstOrDefault(
c => c.Value == parsedEvent.Channel.Value);
Debug.Assert(match != null);
channel = match.Name;
}
if (parsedEvent.Opcode != null)
{
ParsedEtwManifestField? matchingOpcode = null;
if (parsedEvent.Opcode.Value < 10)
{
//
// Reserved opcodes (0-10) are always kept in the "default task" entry
// of the task list, as they would never show up in a task-local or
// global opcode list.
//
matchingOpcode = parsedManifest.Tasks[defaultTask].FirstOrDefault(
o => o.Value == parsedEvent.Opcode.Value)!;
}
else if (parsedEvent.Task == null || parsedEvent.Task.Value == 0)
{
//
// In the case where the task was not provided (0) and opcode is
// custom (> 10), we keep those opcodes in the global opcode list.
//
matchingOpcode = parsedManifest.GlobalOpcodes.FirstOrDefault(
opcode => opcode.Value == parsedEvent.Opcode.Value)!;
//
// Something is buggy with TDH api for opcode/task resolution.
// If TDH didn't give us the opcode when we queried for it
// earlier, just add it now.
//
if (matchingOpcode == null)
{
matchingOpcode = new ParsedEtwManifestField(
parsedEvent.Opcode.Name, "", parsedEvent.Opcode.Value);
parsedManifest.GlobalOpcodes.Add(matchingOpcode);
}
}
else
{
//
// A task was provided and the opcode is custom, it might be in the
// task-local list for the corresponding task.
//
var matchingTask = parsedManifest.Tasks.FirstOrDefault(
t => t.Key.Value == parsedEvent.Task.Value);
matchingOpcode = parsedManifest.Tasks[matchingTask.Key].FirstOrDefault(
opcode => opcode.Value == parsedEvent.Opcode.Value)!;
//
// That is, if the provider followed the rules. *eyeroll*
//
if (matchingOpcode == null)
{
matchingOpcode = new ParsedEtwManifestField(
parsedEvent.Opcode.Name, "", parsedEvent.Opcode.Value);
matchingTask.Value.Add(matchingOpcode);
}
}
Debug.Assert(matchingOpcode != null);
opcode = matchingOpcode.Name;
}
if (parsedEvent.Task != null && parsedEvent.Task.Value > 0)
{
var matchingTask = parsedManifest.Tasks.FirstOrDefault(
t => t.Key.Value == parsedEvent.Task.Value);
task = matchingTask.Key.Name;
}
//
// Build an event from the parsed values
//
var manifestEvent = new ParsedEtwManifestEvent(parsedEvent.EventId.ToString(),
parsedEvent.Version.ToString(),
opcode,
channel,
parsedEvent.Level,
keywords,
task,
template);
//
// TODO: Research this condition. Rarely, a provider will have
// seemingly duplicate events.
//
// Debug.Assert(!parsedManifest.Events.Contains(manifestEvent));
parsedManifest.Events.Add(manifestEvent);
}
//
// Build a list of all unique strings from parsed structures
//
parsedManifest.Keywords.ForEach(k => parsedManifest.StringTable.Add(k.Name));
parsedManifest.Channels.ForEach(k => parsedManifest.StringTable.Add(k.Name));
parsedManifest.Tasks.Keys.Where(k => k.Name != "(no task)").ToList().ForEach(
t => parsedManifest.StringTable.Add(t.Name));
parsedManifest.Tasks.Values.ToList().ForEach(
opcodeList => opcodeList.ForEach(opcode =>
{
if (!parsedManifest.StringTable.Contains(opcode.Name))
{
parsedManifest.StringTable.Add(opcode.Name);
}
}));
parsedManifest.GlobalOpcodes.ForEach(g => parsedManifest.StringTable.Add(g.Name));
//
// Make the string list unique and sorted
//
parsedManifest.StringTable = parsedManifest.StringTable.Distinct().ToList();
parsedManifest.StringTable.Sort();
if (manifestLoaded)
{
_ = TdhUnloadManifest(ManifestLocation);
}
return parsedManifest;
}
public
static
Dictionary<ParsedEtwProvider, ParsedEtwManifest>
GetManifests(int Limit = 0)
{
var results = new Dictionary<ParsedEtwProvider, ParsedEtwManifest>();
var providers = GetProviders();
foreach (var provider in providers)
{
try
{
if (Limit > 0 && results.Count >= Limit)
{
Trace(TraceLoggerType.EtwProviderParser,
TraceEventType.Verbose,
$"Processed {Limit} providers, stopping.");
break;
}
if (results.ContainsKey(provider))
{
//
// This is rare; i'm guessing it is a synchronization problem
// with the TDH api.
//
Trace(TraceLoggerType.EtwProviderParser,
TraceEventType.Error,
$"The provider {provider} appears twice!");
continue;
}
var manifest = GetManifest(provider.Id);
results.Add(provider, manifest);
}
catch (ManifestNotFoundException)
{
//
// Swallow this exception. It's possible the provider is using
// TraceLogging, MOF or some other classic ETW mechanism that
// precludes a published XML manifest.
//
Trace(TraceLoggerType.EtwProviderParser,
TraceEventType.Warning,
$"Unable to locate published manifest for {provider}");
}
}
return results;
}
public
static
bool
IsManifestKnown(Guid ProviderGuid)
{
var buffer = nint.Zero;
try
{
uint bufferSize = 0;
for (; ; )
{
var status = TdhEnumerateManifestProviderEvents(
ref ProviderGuid,
buffer,
ref bufferSize);
switch (status)
{
case ERROR_SUCCESS:
case ERROR_INSUFFICIENT_BUFFER:
{
return true;
}
case ERROR_NOT_FOUND:
case ERROR_FILE_NOT_FOUND:
case ERROR_RESOURCE_TYPE_NOT_FOUND:
case ERROR_MUI_FILE_NOT_FOUND:
{
return false;
}
default:
{
return false;
}
}
}
}
catch (Exception)
{
return false;
}
}
private
static
List<ParsedEtwEvent>
GetProviderSupportedEvents(Guid ProviderGuid)
{
var buffer = nint.Zero;
try
{
uint bufferSize = 0;
for (; ; )
{
var status = TdhEnumerateManifestProviderEvents(
ref ProviderGuid,
buffer,
ref bufferSize);
if (status == ERROR_INSUFFICIENT_BUFFER)
{
buffer = Marshal.AllocHGlobal((int)bufferSize);
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
continue;
}
else if (status == ERROR_SUCCESS)
{
break;
}
switch (status)
{
case ERROR_EMPTY:
{
//
// It's unclear if this is an error but it seems to happen
// somewhat rarely.
//
return new List<ParsedEtwEvent>();
}
case ERROR_INVALID_DATA:
{
//
// It's unclear if this is an error but it seems to happen
// somewhat rarely.
//
return new List<ParsedEtwEvent>();
}
case ERROR_NOT_FOUND:
case ERROR_FILE_NOT_FOUND:
case ERROR_RESOURCE_TYPE_NOT_FOUND:
case ERROR_MUI_FILE_NOT_FOUND:
case ERROR_RESOURCE_NOT_PRESENT:
case ERROR_MUI_FILE_NOT_LOADED:
{
//
// We throw this exception here because it's our first chance
// to know if this return value truly means "missing manifest"
//
throw new ManifestNotFoundException($"ETW was unable to locate " +
$"the manifest or schema for provider {ProviderGuid}. " +
$"Usually this is caused by an incorrect field in the " +
$"input event descriptor, such as incorrect event ID or " +
$"version number.");
}
default:
{
throw new Exception($"TdhEnumerateManifestProviderEvents " +
$"failed: 0x{status:X}");
}
}
}
return ParseProviderEventArray(ProviderGuid, buffer);
}
catch (Exception ex)
{
Trace(TraceLoggerType.EtwProviderParser,
TraceEventType.Error,
"Exception in ParseProviderEventArray(): " +
ex.Message);
throw;
}
finally
{
if (buffer != nint.Zero)
{
Marshal.FreeHGlobal(buffer);
}
}
}
private
static
List<ParsedEtwEvent>
ParseProviderEventArray(Guid ProviderGuid, nint EventArrayBuffer)
{
var array = (PROVIDER_EVENT_INFO)Marshal.PtrToStructure(
EventArrayBuffer, typeof(PROVIDER_EVENT_INFO))!;
int offset = Marshal.OffsetOf(typeof(PROVIDER_EVENT_INFO),
"EventDescriptorsArray").ToInt32();
nint arrayStart = nint.Add(EventArrayBuffer, offset);
Debug.Assert(array.NumberOfEvents > 0);
var events = MarshalHelper.MarshalArray<EVENT_DESCRIPTOR>(arrayStart,
array.NumberOfEvents);
var eventDescriptorBuffer = nint.Zero;
var results = new List<ParsedEtwEvent>();
try
{
eventDescriptorBuffer = Marshal.AllocHGlobal(
Marshal.SizeOf(typeof(EVENT_DESCRIPTOR)));
if (eventDescriptorBuffer == nint.Zero)
{
throw new Exception("Out of memory");
}
foreach (var evt in events)
{
Marshal.StructureToPtr(evt, eventDescriptorBuffer, false);
var traceEventInfoBuffer = nint.Zero;
uint traceEventInfoBufferSize = 0;
for (; ; )
{
var status = TdhGetManifestEventInformation(
ref ProviderGuid,
eventDescriptorBuffer,
traceEventInfoBuffer,
ref traceEventInfoBufferSize);
if (status == ERROR_INSUFFICIENT_BUFFER)
{
Debug.Assert(traceEventInfoBuffer == nint.Zero);
traceEventInfoBuffer = Marshal.AllocHGlobal(
(int)traceEventInfoBufferSize);
if (traceEventInfoBuffer == nint.Zero)
{
throw new Exception("Out of memory");
}
}
else if (status == ERROR_SUCCESS)
{
break;
}
else
{
throw new Exception($"TdhGetManifestEventInformation " +
$"failed: 0x{status:X}");
}
}
Debug.Assert(traceEventInfoBuffer != nint.Zero);
using (var parserBuffers = new EventParserBuffers())
{
//
// NB: ownership of allocated traceEventInfoBuffer passed to
// EventParser dtor!
//
var parser = new EventParser(ProviderGuid, evt, traceEventInfoBuffer, parserBuffers);
try
{
var parsedEvent = parser.Parse();
Debug.Assert(parsedEvent != null);
results.Add(parsedEvent);
}
catch (Exception ex)
{
throw new Exception($"Exception parsing event: {ex.Message}");
}
}
}
return results;
}
finally
{
if (eventDescriptorBuffer != nint.Zero)
{
Marshal.FreeHGlobal(eventDescriptorBuffer);
}
}
}
private
static
List<ParsedEtwManifestField>
GetProviderSupportedFields(Guid ProviderGuid, ulong FieldValue, EVENT_FIELD_TYPE FieldType)
{
var results = new List<ParsedEtwManifestField>();
var buffer = nint.Zero;
try
{
buffer = GetProviderFieldBuffer(ProviderGuid, FieldValue, FieldType);
if (buffer == nint.Zero)
{
return results;
}
var array = (PROVIDER_FIELD_INFOARRAY)Marshal.PtrToStructure(
buffer, typeof(PROVIDER_FIELD_INFOARRAY))!;
int offset = Marshal.OffsetOf(typeof(PROVIDER_FIELD_INFOARRAY),
"FieldInfoArray").ToInt32();
nint arrayStart = nint.Add(buffer, offset);
Debug.Assert(array.NumberOfElements > 0);
var fields = MarshalHelper.MarshalArray<PROVIDER_FIELD_INFO>(arrayStart,
array.NumberOfElements);
foreach (var field in fields)
{
var value = field.Value;
if (FieldType == EVENT_FIELD_TYPE.OpcodeInformation)
{
value &= 0xff0000; // clear task value
value >>= 16; // shift back to char
}
var parsedField = new ParsedEtwManifestField("", "", value);
if (field.NameOffset > 0)
{
parsedField.Name = Marshal.PtrToStringUni(
nint.Add(buffer, (int)field.NameOffset))!;
}
if (field.DescriptionOffset > 0)
{
parsedField.Description = Marshal.PtrToStringUni(
nint.Add(buffer, (int)field.DescriptionOffset))!;
}
results.Add(parsedField);
}
Marshal.FreeHGlobal(buffer);
buffer = nint.Zero;
}
finally
{
if (buffer != nint.Zero)
{
Marshal.FreeHGlobal(buffer);
}
}
return results;
}
private static nint GetProviderFieldBuffer(Guid ProviderGuid, ulong FieldValue, EVENT_FIELD_TYPE FieldType)
{
var buffer = nint.Zero;
uint bufferSize = 0;
for (; ; )
{
var status = TdhQueryProviderFieldInformation(
ref ProviderGuid,
FieldValue,
FieldType,
buffer,
ref bufferSize);
if (status == ERROR_INSUFFICIENT_BUFFER)
{
Debug.Assert(buffer == nint.Zero);
buffer = Marshal.AllocHGlobal((int)bufferSize);
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
}
else if (status == ERROR_SUCCESS)
{
break;
}
else if (status == ERROR_NOT_FOUND || status == ERROR_FILE_NOT_FOUND)
{
//
// Note: in this case, TDH might have loaded the schema but returned
// this value because the requested field type doesn't exist in the
// schema. Very confusing to return this error code for this situation
// as well as the more catastrophic case where the schema doesn't exist
// _at all_.
// We do NOT throw ManifestNotFoundException here.
//
return nint.Zero;
}
else
{
return nint.Zero;
}
}
Debug.Assert(buffer != nint.Zero);
return buffer;
}
private static void LoadProviderManifest(string Location)
{
if (string.IsNullOrEmpty(Location) ||
!File.Exists(Location))
{
throw new ManifestNotFoundException($"Invalid manifest location {Location}");
}
var result = TdhLoadManifest(Location);
switch (result)
{
case ERROR_SUCCESS:
{
break;
}
case ERROR_FILE_NOT_FOUND:
{
throw new ManifestNotFoundException($"TdhLoadManifest failed: " +
$"File not found {Location}");
}
case ERROR_INVALID_PARAMETER:
{
throw new Exception($"TdhLoadManifest failed: " +
$"Invalid manifest {Location}");
}
case ERROR_XML_PARSE_ERROR:
{
throw new Exception($"TdhLoadManifest failed: " +
$"File has invalid XML {Location}");
}
default:
{
throw new Exception($"TdhLoadManifest failed: 0x{result:X}");
}
}
}
public static string TdhInputTypeToSchemaType(TdhInputType Type)
{
//
// see https://learn.microsoft.com/en-us/windows/win32/wes/eventmanifestschema-inputtype-complextype
//
switch (Type)
{
case TdhInputType.UnicodeString:
{
return "win:UnicodeString";
}
case TdhInputType.AnsiString:
{
return "win:AnsiString";
}
case TdhInputType.Int8:
{
return "win:Int8";
}
case TdhInputType.UInt8:
{
return "win:UInt8";
}
case TdhInputType.Int16:
{
return "win:Int16";
}
case TdhInputType.UInt16:
{
return "win:UInt16";
}
case TdhInputType.Int32:
{
return "win:Int32";
}
case TdhInputType.UInt32:
{
return "win:UInt32";
}
case TdhInputType.Int64:
{
return "win:Int64";
}
case TdhInputType.UInt64:
{
return "win:UInt64";
}
case TdhInputType.Float:
{
return "win:Float";
}
case TdhInputType.Double:
{
return "win:Double";
}
case TdhInputType.Boolean:
{
return "win:Boolean";
}
case TdhInputType.Binary:
{
return "win:Binary";
}
case TdhInputType.GUID:
{
return "win:GUID";
}
case TdhInputType.Pointer:
{
return "win:Pointer";
}
case TdhInputType.FILETIME:
{
return "win:FILETIME";
}
case TdhInputType.SYSTEMTIME:
{
return "win:SYSTEMTIME";
}