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
1001
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include "common.h"
#include "clrtypes.h"
#include "safemath.h"
#include "eventpipe.h"
#include "eventpipebuffermanager.h"
#include "eventpipeconfiguration.h"
#include "eventpipeevent.h"
#include "eventpipefile.h"
#include "eventpipeprovider.h"
#include "eventpipesession.h"
#include "eventpipejsonfile.h"
#include "sampleprofiler.h"
#ifdef FEATURE_PAL
#include "pal.h"
#endif // FEATURE_PAL
#ifdef FEATURE_PERFTRACING
CrstStatic EventPipe::s_configCrst;
bool EventPipe::s_tracingInitialized = false;
EventPipeConfiguration* EventPipe::s_pConfig = NULL;
EventPipeSession* EventPipe::s_pSession = NULL;
EventPipeBufferManager* EventPipe::s_pBufferManager = NULL;
EventPipeFile* EventPipe::s_pFile = NULL;
#ifdef _DEBUG
EventPipeFile* EventPipe::s_pSyncFile = NULL;
EventPipeJsonFile* EventPipe::s_pJsonFile = NULL;
#endif // _DEBUG
#ifdef FEATURE_PAL
// This function is auto-generated from /src/scripts/genEventPipe.py
extern "C" void InitProvidersAndEvents();
#else
void InitProvidersAndEvents();
#endif
EventPipeEventPayload::EventPipeEventPayload(BYTE *pData, unsigned int length)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
m_pData = pData;
m_pEventData = NULL;
m_eventDataCount = 0;
m_allocatedData = false;
m_size = length;
}
EventPipeEventPayload::EventPipeEventPayload(EventData **pEventData, unsigned int eventDataCount)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
m_pData = NULL;
m_pEventData = pEventData;
m_eventDataCount = eventDataCount;
m_allocatedData = false;
S_UINT32 tmp_size = S_UINT32(0);
for (unsigned int i=0; i<m_eventDataCount; i++)
{
tmp_size += S_UINT32((*m_pEventData)[i].Size);
}
if (tmp_size.IsOverflow())
{
// If there is an overflow, drop the data and create an empty payload
m_pEventData = NULL;
m_eventDataCount = 0;
m_size = 0;
}
else
{
m_size = tmp_size.Value();
}
}
EventPipeEventPayload::~EventPipeEventPayload()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if(m_allocatedData && m_pData != NULL)
{
delete[] m_pData;
m_pData = NULL;
}
}
void EventPipeEventPayload::Flatten()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if(m_size > 0)
{
if (!IsFlattened())
{
BYTE* tmp_pData = new (nothrow) BYTE[m_size];
if (tmp_pData != NULL)
{
m_allocatedData = true;
CopyData(tmp_pData);
m_pData = tmp_pData;
}
}
}
}
void EventPipeEventPayload::CopyData(BYTE *pDst)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if(m_size > 0)
{
if(IsFlattened())
{
memcpy(pDst, m_pData, m_size);
}
else if(m_pEventData != NULL)
{
unsigned int offset = 0;
for(unsigned int i=0; i<m_eventDataCount; i++)
{
memcpy(pDst + offset, (BYTE*)(*m_pEventData)[i].Ptr, (*m_pEventData)[i].Size);
offset += (*m_pEventData)[i].Size;
}
}
}
}
BYTE* EventPipeEventPayload::GetFlatData()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if (!IsFlattened())
{
Flatten();
}
return m_pData;
}
void EventPipe::Initialize()
{
STANDARD_VM_CONTRACT;
s_tracingInitialized = s_configCrst.InitNoThrow(
CrstEventPipe,
(CrstFlags)(CRST_REENTRANCY | CRST_TAKEN_DURING_SHUTDOWN | CRST_HOST_BREAKABLE));
s_pConfig = new EventPipeConfiguration();
s_pConfig->Initialize();
s_pBufferManager = new EventPipeBufferManager();
// This calls into auto-generated code to initialize the runtime providers
// and events so that the EventPipe configuration lock isn't taken at runtime
InitProvidersAndEvents();
}
void EventPipe::EnableOnStartup()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
// Test COMPLUS variable to enable tracing at start-up.
if((CLRConfig::GetConfigValue(CLRConfig::INTERNAL_EnableEventPipe) & 1) == 1)
{
SString outputPath;
outputPath.Printf("Process-%d.netperf", GetCurrentProcessId());
// Create a new session.
EventPipeSession *pSession = new EventPipeSession(
1024 /* 1 GB circular buffer */,
NULL, /* pProviders */
0 /* numProviders */);
// Get the configuration from the environment.
GetConfigurationFromEnvironment(outputPath, pSession);
// Enable the session.
Enable(outputPath, pSession);
}
}
void EventPipe::Shutdown()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
// We are shutting down, so if diasabling EventPipe throws, we need to move along anyway
EX_TRY
{
Disable();
}
EX_CATCH { }
EX_END_CATCH(SwallowAllExceptions);
if(s_pConfig != NULL)
{
delete(s_pConfig);
s_pConfig = NULL;
}
if(s_pBufferManager != NULL)
{
delete(s_pBufferManager);
s_pBufferManager = NULL;
}
}
void EventPipe::Enable(
LPCWSTR strOutputPath,
unsigned int circularBufferSizeInMB,
EventPipeProviderConfiguration *pProviders,
int numProviders)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
// Create a new session.
EventPipeSession *pSession = s_pConfig->CreateSession(circularBufferSizeInMB, pProviders, static_cast<unsigned int>(numProviders));
// Enable the session.
Enable(strOutputPath, pSession);
}
void EventPipe::Enable(LPCWSTR strOutputPath, EventPipeSession *pSession)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(pSession != NULL);
}
CONTRACTL_END;
// If tracing is not initialized or is already enabled, bail here.
if(!s_tracingInitialized || s_pConfig == NULL || s_pConfig->Enabled())
{
return;
}
// If the state or arguments are invalid, bail here.
if(pSession == NULL || !pSession->IsValid())
{
return;
}
// Take the lock before enabling tracing.
CrstHolder _crst(GetLock());
// Create the event pipe file.
SString eventPipeFileOutputPath(strOutputPath);
s_pFile = new EventPipeFile(eventPipeFileOutputPath);
#ifdef _DEBUG
if((CLRConfig::GetConfigValue(CLRConfig::INTERNAL_EnableEventPipe) & 2) == 2)
{
// Create a synchronous file.
SString eventPipeSyncFileOutputPath;
eventPipeSyncFileOutputPath.Printf("Process-%d.sync.netperf", GetCurrentProcessId());
s_pSyncFile = new EventPipeFile(eventPipeSyncFileOutputPath);
// Create a JSON file.
SString outputFilePath;
outputFilePath.Printf("Process-%d.PerfView.json", GetCurrentProcessId());
s_pJsonFile = new EventPipeJsonFile(outputFilePath);
}
#endif // _DEBUG
// Save the session.
s_pSession = pSession;
// Enable tracing.
s_pConfig->Enable(s_pSession);
// Enable the sample profiler
SampleProfiler::Enable();
}
void EventPipe::Disable()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
// Don't block GC during clean-up.
GCX_PREEMP();
// Take the lock before disabling tracing.
CrstHolder _crst(GetLock());
if(s_pConfig != NULL && s_pConfig->Enabled())
{
// Disable the profiler.
SampleProfiler::Disable();
// Disable tracing.
s_pConfig->Disable(s_pSession);
// Delete the session.
s_pConfig->DeleteSession(s_pSession);
s_pSession = NULL;
// Flush all write buffers to make sure that all threads see the change.
FlushProcessWriteBuffers();
// Write to the file.
LARGE_INTEGER disableTimeStamp;
QueryPerformanceCounter(&disableTimeStamp);
s_pBufferManager->WriteAllBuffersToFile(s_pFile, disableTimeStamp);
// Before closing the file, do rundown.
const unsigned int numRundownProviders = 2;
EventPipeProviderConfiguration rundownProviders[] =
{
{ W("Microsoft-Windows-DotNETRuntime"), 0x80020138, static_cast<unsigned int>(EventPipeEventLevel::Verbose) }, // Public provider.
{ W("Microsoft-Windows-DotNETRuntimeRundown"), 0x80020138, static_cast<unsigned int>(EventPipeEventLevel::Verbose) } // Rundown provider.
};
// The circular buffer size doesn't matter because all events are written synchronously during rundown.
s_pSession = s_pConfig->CreateSession(1 /* circularBufferSizeInMB */, rundownProviders, numRundownProviders);
s_pConfig->EnableRundown(s_pSession);
// Ask the runtime to emit rundown events.
if(g_fEEStarted && !g_fEEShutDown)
{
ETW::EnumerationLog::EndRundown();
}
// Disable the event pipe now that rundown is complete.
s_pConfig->Disable(s_pSession);
// Delete the rundown session.
s_pConfig->DeleteSession(s_pSession);
s_pSession = NULL;
if(s_pFile != NULL)
{
delete(s_pFile);
s_pFile = NULL;
}
#ifdef _DEBUG
if(s_pSyncFile != NULL)
{
delete(s_pSyncFile);
s_pSyncFile = NULL;
}
if(s_pJsonFile != NULL)
{
delete(s_pJsonFile);
s_pJsonFile = NULL;
}
#endif // _DEBUG
// De-allocate buffers.
s_pBufferManager->DeAllocateBuffers();
// Delete deferred providers.
// Providers can't be deleted during tracing because they may be needed when serializing the file.
s_pConfig->DeleteDeferredProviders();
}
}
bool EventPipe::Enabled()
{
LIMITED_METHOD_CONTRACT;
bool enabled = false;
if(s_pConfig != NULL)
{
enabled = s_pConfig->Enabled();
}
return enabled;
}
EventPipeProvider* EventPipe::CreateProvider(const SString &providerName, EventPipeCallback pCallbackFunction, void *pCallbackData)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
EventPipeProvider *pProvider = NULL;
if (s_pConfig != NULL)
{
pProvider = s_pConfig->CreateProvider(providerName, pCallbackFunction, pCallbackData);
}
return pProvider;
}
void EventPipe::DeleteProvider(EventPipeProvider *pProvider)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
// Take the lock to make sure that we don't have a race
// between disabling tracing and deleting a provider
// where we hold a provider after tracing has been disabled.
CrstHolder _crst(GetLock());
if(pProvider != NULL)
{
if(Enabled())
{
// Save the provider until the end of the tracing session.
pProvider->SetDeleteDeferred();
}
else
{
// Delete the provider now.
if (s_pConfig != NULL)
{
s_pConfig->DeleteProvider(pProvider);
}
}
}
}
void EventPipe::WriteEvent(EventPipeEvent &event, BYTE *pData, unsigned int length, LPCGUID pActivityId, LPCGUID pRelatedActivityId)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
EventPipeEventPayload payload(pData, length);
EventPipe::WriteEventInternal(event, payload, pActivityId, pRelatedActivityId);
}
void EventPipe::WriteEvent(EventPipeEvent &event, EventData **pEventData, unsigned int eventDataCount, LPCGUID pActivityId, LPCGUID pRelatedActivityId)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
EventPipeEventPayload payload(pEventData, eventDataCount);
EventPipe::WriteEventInternal(event, payload, pActivityId, pRelatedActivityId);
}
void EventPipe::WriteEventInternal(EventPipeEvent &event, EventPipeEventPayload &payload, LPCGUID pActivityId, LPCGUID pRelatedActivityId)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
// Exit early if the event is not enabled.
if(!event.IsEnabled())
{
return;
}
// Get the current thread;
Thread *pThread = GetThread();
if(pThread == NULL)
{
// We can't write an event without the thread object.
return;
}
if(s_pConfig == NULL)
{
// We can't procede without a configuration
return;
}
if(!s_pConfig->RundownEnabled() && s_pBufferManager != NULL)
{
if(!s_pBufferManager->WriteEvent(pThread, event, payload, pActivityId, pRelatedActivityId))
{
// This is used in DEBUG to make sure that we don't log an event synchronously that we didn't log to the buffer.
return;
}
}
else if(s_pConfig->RundownEnabled())
{
BYTE *pData = payload.GetFlatData();
if (pData != NULL)
{
// Write synchronously to the file.
// We're under lock and blocking the disabling thread.
// This copy occurs here (rather than at file write) because
// A) The FastSerializer API would need to change if we waited
// B) It is unclear there is a benefit to multiple file write calls
// as opposed a a buffer copy here
EventPipeEventInstance instance(
event,
pThread->GetOSThreadId(),
pData,
payload.GetSize(),
pActivityId,
pRelatedActivityId);
if(s_pFile != NULL)
{
// EventPipeFile::WriteEvent needs to allocate a metadata event
// and can therefore throw. In this context we will silently
// fail rather than disrupt the caller
EX_TRY
{
s_pFile->WriteEvent(instance);
}
EX_CATCH { }
EX_END_CATCH(SwallowAllExceptions);
}
}
}
// This section requires a call to GCX_PREEMP which violates the GC_NOTRIGGER contract
// It should only be enabled when debugging this specific component and contracts are off
#ifdef DEBUG_JSON_EVENT_FILE
{
GCX_PREEMP();
BYTE *pData = payload.GetFlatData();
if (pData != NULL)
{
// Create an instance of the event for the synchronous path.
EventPipeEventInstance instance(
event,
pThread->GetOSThreadId(),
pData,
payload.GetSize(),
pActivityId,
pRelatedActivityId);
// Write to the EventPipeFile if it exists.
if(s_pSyncFile != NULL)
{
s_pSyncFile->WriteEvent(instance);
}
// Write to the EventPipeJsonFile if it exists.
if(s_pJsonFile != NULL)
{
s_pJsonFile->WriteEvent(instance);
}
}
}
#endif // DEBUG_JSON_EVENT_FILE
}
void EventPipe::WriteSampleProfileEvent(Thread *pSamplingThread, EventPipeEvent *pEvent, Thread *pTargetThread, StackContents &stackContents, BYTE *pData, unsigned int length)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
EventPipeEventPayload payload(pData, length);
// Write the event to the thread's buffer.
if(s_pBufferManager != NULL)
{
// Specify the sampling thread as the "current thread", so that we select the right buffer.
// Specify the target thread so that the event gets properly attributed.
if(!s_pBufferManager->WriteEvent(pSamplingThread, *pEvent, payload, NULL /* pActivityId */, NULL /* pRelatedActivityId */, pTargetThread, &stackContents))
{
// This is used in DEBUG to make sure that we don't log an event synchronously that we didn't log to the buffer.
return;
}
}
#ifdef _DEBUG
{
GCX_PREEMP();
// Create an instance for the synchronous path.
SampleProfilerEventInstance instance(*pEvent, pTargetThread, pData, length);
stackContents.CopyTo(instance.GetStack());
// Write to the EventPipeFile.
if(s_pSyncFile != NULL)
{
s_pSyncFile->WriteEvent(instance);
}
// Write to the EventPipeJsonFile if it exists.
if(s_pJsonFile != NULL)
{
s_pJsonFile->WriteEvent(instance);
}
}
#endif // _DEBUG
}
bool EventPipe::WalkManagedStackForCurrentThread(StackContents &stackContents)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
Thread *pThread = GetThread();
if(pThread != NULL)
{
return WalkManagedStackForThread(pThread, stackContents);
}
return false;
}
bool EventPipe::WalkManagedStackForThread(Thread *pThread, StackContents &stackContents)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(pThread != NULL);
}
CONTRACTL_END;
stackContents.Reset();
StackWalkAction swaRet = pThread->StackWalkFrames(
(PSTACKWALKFRAMESCALLBACK) &StackWalkCallback,
&stackContents,
ALLOW_ASYNC_STACK_WALK | FUNCTIONSONLY | HANDLESKIPPEDFRAMES);
return ((swaRet == SWA_DONE) || (swaRet == SWA_CONTINUE));
}
StackWalkAction EventPipe::StackWalkCallback(CrawlFrame *pCf, StackContents *pData)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(pCf != NULL);
PRECONDITION(pData != NULL);
}
CONTRACTL_END;
// Get the IP.
UINT_PTR controlPC = (UINT_PTR)pCf->GetRegisterSet()->ControlPC;
if(controlPC == 0)
{
if(pData->GetLength() == 0)
{
// This happens for pinvoke stubs on the top of the stack.
return SWA_CONTINUE;
}
}
_ASSERTE(controlPC != 0);
// Add the IP to the captured stack.
pData->Append(
controlPC,
pCf->GetFunction()
);
// Continue the stack walk.
return SWA_CONTINUE;
}
EventPipeConfiguration* EventPipe::GetConfiguration()
{
LIMITED_METHOD_CONTRACT;
return s_pConfig;
}
CrstStatic* EventPipe::GetLock()
{
LIMITED_METHOD_CONTRACT;
return &s_configCrst;
}
void EventPipe::GetConfigurationFromEnvironment(SString &outputPath, EventPipeSession *pSession)
{
LIMITED_METHOD_CONTRACT;
// Set the output path if specified.
CLRConfigStringHolder wszOutputPath(CLRConfig::GetConfigValue(CLRConfig::INTERNAL_EventPipeOutputFile));
if(wszOutputPath != NULL)
{
outputPath.Set(wszOutputPath);
}
// Read the the provider configuration from the environment if specified.
CLRConfigStringHolder wszConfig(CLRConfig::GetConfigValue(CLRConfig::INTERNAL_EventPipeConfig));
if(wszConfig == NULL)
{
pSession->EnableAllEvents();
return;
}
size_t len = wcslen(wszConfig);
if(len <= 0)
{
pSession->EnableAllEvents();
return;
}
// Parses a string with the following format:
//
// ProviderName:Keywords:Level[,]*
//
// For example:
//
// Microsoft-Windows-DotNETRuntime:0xCAFEBABE:2,Microsoft-Windows-DotNETRuntimePrivate:0xDEADBEEF:1
//
// Each provider configuration is separated by a ',' and each component within the configuration is
// separated by a ':'.
const WCHAR ProviderSeparatorChar = ',';
const WCHAR ComponentSeparatorChar = ':';
size_t index = 0;
WCHAR *pProviderName = NULL;
UINT64 keywords = 0;
EventPipeEventLevel level = EventPipeEventLevel::Critical;
while(index < len)
{
WCHAR * pCurrentChunk = &wszConfig[index];
size_t currentChunkStartIndex = index;
size_t currentChunkEndIndex = 0;
// Find the next chunk.
while(index < len && wszConfig[index] != ProviderSeparatorChar)
{
index++;
}
currentChunkEndIndex = index++;
// Split the chunk into components.
size_t chunkIndex = currentChunkStartIndex;
// Get the provider name.
size_t provNameStartIndex = chunkIndex;
size_t provNameEndIndex = currentChunkEndIndex;
while(chunkIndex < currentChunkEndIndex && wszConfig[chunkIndex] != ComponentSeparatorChar)
{
chunkIndex++;
}
provNameEndIndex = chunkIndex++;
size_t provNameLen = provNameEndIndex - provNameStartIndex;
pProviderName = new WCHAR[provNameLen+1];
memcpy(pProviderName, &wszConfig[provNameStartIndex], provNameLen*sizeof(WCHAR));
pProviderName[provNameLen] = '\0';
// Get the keywords.
size_t keywordsStartIndex = chunkIndex;
size_t keywordsEndIndex = currentChunkEndIndex;
while(chunkIndex < currentChunkEndIndex && wszConfig[chunkIndex] != ComponentSeparatorChar)
{
chunkIndex++;
}
keywordsEndIndex = chunkIndex++;
size_t keywordsLen = keywordsEndIndex - keywordsStartIndex;
WCHAR *wszKeywords = new WCHAR[keywordsLen+1];
memcpy(wszKeywords, &wszConfig[keywordsStartIndex], keywordsLen*sizeof(WCHAR));
wszKeywords[keywordsLen] = '\0';
keywords = _wcstoui64(wszKeywords, NULL, 16);
delete[] wszKeywords;
wszKeywords = NULL;
// Get the level.
size_t levelStartIndex = chunkIndex;
size_t levelEndIndex = currentChunkEndIndex;
while(chunkIndex < currentChunkEndIndex && wszConfig[chunkIndex] != ComponentSeparatorChar)
{
chunkIndex++;
}
levelEndIndex = chunkIndex++;
size_t levelLen = levelEndIndex - levelStartIndex;
WCHAR *wszLevel = new WCHAR[levelLen+1];
memcpy(wszLevel, &wszConfig[levelStartIndex], levelLen*sizeof(WCHAR));
wszLevel[levelLen] = '\0';
level = (EventPipeEventLevel) wcstoul(wszLevel, NULL, 16);
delete[] wszLevel;
wszLevel = NULL;
// Add a new EventPipeSessionProvider.
EventPipeSessionProvider *pSessionProvider = new EventPipeSessionProvider(pProviderName, keywords, level);
pSession->AddSessionProvider(pSessionProvider);
// Free the provider name string.
if(pProviderName != NULL)
{
delete[] pProviderName;
pProviderName = NULL;
}
}
}
void QCALLTYPE EventPipeInternal::Enable(
__in_z LPCWSTR outputFile,
UINT32 circularBufferSizeInMB,
INT64 profilerSamplingRateInNanoseconds,
EventPipeProviderConfiguration *pProviders,
INT32 numProviders)
{
QCALL_CONTRACT;
BEGIN_QCALL;
SampleProfiler::SetSamplingRate((unsigned long)profilerSamplingRateInNanoseconds);
EventPipe::Enable(outputFile, circularBufferSizeInMB, pProviders, numProviders);
END_QCALL;
}
void QCALLTYPE EventPipeInternal::Disable()
{
QCALL_CONTRACT;
BEGIN_QCALL;
EventPipe::Disable();
END_QCALL;
}
INT_PTR QCALLTYPE EventPipeInternal::CreateProvider(
__in_z LPCWSTR providerName,
EventPipeCallback pCallbackFunc)
{
QCALL_CONTRACT;
EventPipeProvider *pProvider = NULL;
BEGIN_QCALL;
pProvider = EventPipe::CreateProvider(providerName, pCallbackFunc, NULL);
END_QCALL;
return reinterpret_cast<INT_PTR>(pProvider);
}
INT_PTR QCALLTYPE EventPipeInternal::DefineEvent(
INT_PTR provHandle,
UINT32 eventID,
__int64 keywords,
UINT32 eventVersion,
UINT32 level,
void *pMetadata,
UINT32 metadataLength)
{
QCALL_CONTRACT;
EventPipeEvent *pEvent = NULL;
BEGIN_QCALL;
_ASSERTE(provHandle != NULL);
_ASSERTE(pMetadata != NULL);
EventPipeProvider *pProvider = reinterpret_cast<EventPipeProvider *>(provHandle);
pEvent = pProvider->AddEvent(eventID, keywords, eventVersion, (EventPipeEventLevel)level, (BYTE *)pMetadata, metadataLength);
_ASSERTE(pEvent != NULL);
END_QCALL;
return reinterpret_cast<INT_PTR>(pEvent);
}
void QCALLTYPE EventPipeInternal::DeleteProvider(
INT_PTR provHandle)
{
QCALL_CONTRACT;
BEGIN_QCALL;
if(provHandle != NULL)
{
EventPipeProvider *pProvider = reinterpret_cast<EventPipeProvider*>(provHandle);
EventPipe::DeleteProvider(pProvider);
}
END_QCALL;
}
void QCALLTYPE EventPipeInternal::WriteEvent(
INT_PTR eventHandle,
UINT32 eventID,
void *pData,
UINT32 length,
LPCGUID pActivityId,
LPCGUID pRelatedActivityId)
{
QCALL_CONTRACT;
BEGIN_QCALL;
_ASSERTE(eventHandle != NULL);
EventPipeEvent *pEvent = reinterpret_cast<EventPipeEvent *>(eventHandle);
EventPipe::WriteEvent(*pEvent, (BYTE *)pData, length, pActivityId, pRelatedActivityId);
END_QCALL;
}
void QCALLTYPE EventPipeInternal::WriteEventData(
INT_PTR eventHandle,
UINT32 eventID,
EventData **pEventData,
UINT32 eventDataCount,
LPCGUID pActivityId,
LPCGUID pRelatedActivityId)
{
QCALL_CONTRACT;
BEGIN_QCALL;
_ASSERTE(eventHandle != NULL);
EventPipeEvent *pEvent = reinterpret_cast<EventPipeEvent *>(eventHandle);
EventPipe::WriteEvent(*pEvent, pEventData, eventDataCount, pActivityId, pRelatedActivityId);
END_QCALL;
}
#endif // FEATURE_PERFTRACING
|