| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | #include "common.h" |
| 6 | #include "eventpipeeventsource.h" |
| 7 | #include "eventpipe.h" |
| 8 | #include "eventpipeevent.h" |
| 9 | #include "eventpipemetadatagenerator.h" |
| 10 | #include "eventpipeprovider.h" |
| 11 | #include "eventpipesession.h" |
| 12 | |
| 13 | #ifdef FEATURE_PERFTRACING |
| 14 | |
| 15 | const WCHAR* EventPipeEventSource::s_pProviderName = W("Microsoft-DotNETCore-EventPipe" ); |
| 16 | const WCHAR* EventPipeEventSource::s_pProcessInfoEventName = W("ProcessInfo" ); |
| 17 | |
| 18 | EventPipeEventSource::EventPipeEventSource() |
| 19 | { |
| 20 | CONTRACTL |
| 21 | { |
| 22 | THROWS; |
| 23 | GC_TRIGGERS; |
| 24 | MODE_ANY; |
| 25 | } |
| 26 | CONTRACTL_END; |
| 27 | |
| 28 | m_pProvider = EventPipe::CreateProvider(SL(s_pProviderName), NULL, NULL); |
| 29 | |
| 30 | // Generate metadata. |
| 31 | const unsigned int numParams = 1; |
| 32 | EventPipeParameterDesc params[numParams]; |
| 33 | params[0].Type = EventPipeParameterType::String; |
| 34 | params[0].Name = W("CommandLine" ); |
| 35 | |
| 36 | size_t metadataLength = 0; |
| 37 | BYTE *pMetadata = EventPipeMetadataGenerator::GenerateEventMetadata( |
| 38 | 1, /* eventID */ |
| 39 | s_pProcessInfoEventName, |
| 40 | 0, /* keywords */ |
| 41 | 0, /* version */ |
| 42 | EventPipeEventLevel::LogAlways, |
| 43 | params, |
| 44 | numParams, |
| 45 | metadataLength); |
| 46 | |
| 47 | // Add the event. |
| 48 | m_pProcessInfoEvent = m_pProvider->AddEvent( |
| 49 | 1, /* eventID */ |
| 50 | 0, /* keywords */ |
| 51 | 0, /* eventVersion */ |
| 52 | EventPipeEventLevel::LogAlways, |
| 53 | pMetadata, |
| 54 | (unsigned int)metadataLength); |
| 55 | |
| 56 | // Delete the metadata after the event is created. |
| 57 | // The metadata blob will be copied into EventPipe-owned memory. |
| 58 | delete(pMetadata); |
| 59 | } |
| 60 | |
| 61 | EventPipeEventSource::~EventPipeEventSource() |
| 62 | { |
| 63 | CONTRACTL |
| 64 | { |
| 65 | NOTHROW; |
| 66 | GC_TRIGGERS; |
| 67 | MODE_ANY; |
| 68 | } |
| 69 | CONTRACTL_END; |
| 70 | |
| 71 | // Delete the provider and associated events. |
| 72 | // This is called in the shutdown path which can't throw. |
| 73 | // Catch exceptions and ignore failures. |
| 74 | EX_TRY |
| 75 | { |
| 76 | EventPipe::DeleteProvider(m_pProvider); |
| 77 | } |
| 78 | EX_CATCH { } |
| 79 | EX_END_CATCH(SwallowAllExceptions); |
| 80 | } |
| 81 | |
| 82 | void EventPipeEventSource::Enable(EventPipeSession *pSession) |
| 83 | { |
| 84 | CONTRACTL |
| 85 | { |
| 86 | THROWS; |
| 87 | GC_TRIGGERS; |
| 88 | MODE_ANY; |
| 89 | PRECONDITION(pSession != NULL); |
| 90 | } |
| 91 | CONTRACTL_END; |
| 92 | |
| 93 | EventPipeSessionProvider *pSessionProvider = new EventPipeSessionProvider( |
| 94 | s_pProviderName, |
| 95 | -1, |
| 96 | EventPipeEventLevel::LogAlways, |
| 97 | NULL); |
| 98 | pSession->AddSessionProvider(pSessionProvider); |
| 99 | } |
| 100 | |
| 101 | void EventPipeEventSource::SendProcessInfo(LPCWSTR pCommandLine) |
| 102 | { |
| 103 | CONTRACTL |
| 104 | { |
| 105 | NOTHROW; |
| 106 | GC_NOTRIGGER; |
| 107 | MODE_ANY; |
| 108 | } |
| 109 | CONTRACTL_END; |
| 110 | |
| 111 | EventData data[1]; |
| 112 | data[0].Ptr = (UINT64) pCommandLine; |
| 113 | data[0].Size = (unsigned int)(wcslen(pCommandLine) + 1) * 2; |
| 114 | data[0].Reserved = 0; |
| 115 | |
| 116 | EventPipe::WriteEvent(*m_pProcessInfoEvent, data, 1); |
| 117 | } |
| 118 | |
| 119 | #endif // FEATURE_PERFTRACING |
| 120 | |