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 "eventpipeevent.h"
7#include "eventpipeprovider.h"
8
9#ifdef FEATURE_PERFTRACING
10
11EventPipeEvent::EventPipeEvent(EventPipeProvider &provider, INT64 keywords, unsigned int eventID, unsigned int eventVersion, EventPipeEventLevel level, bool needStack, BYTE *pMetadata, unsigned int metadataLength)
12{
13 CONTRACTL
14 {
15 THROWS;
16 GC_NOTRIGGER;
17 MODE_ANY;
18 }
19 CONTRACTL_END;
20
21 m_pProvider = &provider;
22 m_keywords = keywords;
23 m_eventID = eventID;
24 m_eventVersion = eventVersion;
25 m_level = level;
26 m_needStack = needStack;
27 m_enabled = false;
28 if (pMetadata != NULL)
29 {
30 m_pMetadata = new BYTE[metadataLength];
31 memcpy(m_pMetadata, pMetadata, metadataLength);
32 m_metadataLength = metadataLength;
33 }
34 else
35 {
36 // if metadata is not provided, we have to build the minimum version. It's required by the serialization contract
37 m_pMetadata = BuildMinimumMetadata();
38 m_metadataLength = GetMinimumMetadataLength();
39 }
40}
41
42EventPipeEvent::~EventPipeEvent()
43{
44 CONTRACTL
45 {
46 NOTHROW;
47 GC_NOTRIGGER;
48 MODE_ANY;
49 }
50 CONTRACTL_END;
51
52 if (m_pMetadata != NULL)
53 {
54 delete[] m_pMetadata;
55 m_pMetadata = NULL;
56 }
57}
58
59unsigned int EventPipeEvent::GetMinimumMetadataLength()
60{
61 LIMITED_METHOD_CONTRACT;
62
63 unsigned int minimumMetadataLength =
64 sizeof(m_eventID) +
65 (SString::Empty().GetCount() + 1) * sizeof(WCHAR) + // size of empty unicode string
66 sizeof(m_keywords) +
67 sizeof(m_eventVersion) +
68 sizeof(m_level) +
69 sizeof(unsigned int); // parameter count
70
71 return minimumMetadataLength;
72}
73
74BYTE *EventPipeEvent::BuildMinimumMetadata()
75{
76 CONTRACTL
77 {
78 THROWS;
79 GC_NOTRIGGER;
80 MODE_ANY;
81 }
82 CONTRACTL_END;
83
84 unsigned int minmumMetadataLength = GetMinimumMetadataLength();
85
86 BYTE *minmumMetadata = new BYTE[minmumMetadataLength];
87 BYTE *currentPtr = minmumMetadata;
88
89 // the order of fields is defined in coreclr\src\mscorlib\shared\System\Diagnostics\Tracing\EventSource.cs DefineEventPipeEvents method
90 memcpy(currentPtr, &m_eventID, sizeof(m_eventID));
91 currentPtr += sizeof(m_eventID);
92
93 SString eventName = SString::Empty();
94 unsigned int eventNameSize = (eventName.GetCount() + 1) * sizeof(WCHAR);
95 memcpy(currentPtr, (BYTE*)eventName.GetUnicode(), eventNameSize);
96 currentPtr += eventNameSize;
97
98 memcpy(currentPtr, &m_keywords, sizeof(m_keywords));
99 currentPtr += sizeof(m_keywords);
100
101 memcpy(currentPtr, &m_eventVersion, sizeof(m_eventVersion));
102 currentPtr += sizeof(m_eventVersion);
103
104 memcpy(currentPtr, &m_level, sizeof(m_level));
105 currentPtr += sizeof(m_level);
106
107 unsigned int noParameters = 0;
108 memcpy(currentPtr, &noParameters, sizeof(noParameters));
109 currentPtr += sizeof(noParameters);
110
111 return minmumMetadata;
112}
113
114EventPipeProvider* EventPipeEvent::GetProvider() const
115{
116 LIMITED_METHOD_CONTRACT;
117
118 return m_pProvider;
119}
120
121INT64 EventPipeEvent::GetKeywords() const
122{
123 LIMITED_METHOD_CONTRACT;
124
125 return m_keywords;
126}
127
128unsigned int EventPipeEvent::GetEventID() const
129{
130 LIMITED_METHOD_CONTRACT;
131
132 return m_eventID;
133}
134
135unsigned int EventPipeEvent::GetEventVersion() const
136{
137 LIMITED_METHOD_CONTRACT;
138
139 return m_eventVersion;
140}
141
142EventPipeEventLevel EventPipeEvent::GetLevel() const
143{
144 LIMITED_METHOD_CONTRACT;
145
146 return m_level;
147}
148
149bool EventPipeEvent::NeedStack() const
150{
151 LIMITED_METHOD_CONTRACT;
152
153 return m_needStack;
154}
155
156bool EventPipeEvent::IsEnabled() const
157{
158 LIMITED_METHOD_CONTRACT;
159
160 return m_enabled;
161}
162
163BYTE *EventPipeEvent::GetMetadata() const
164{
165 LIMITED_METHOD_CONTRACT;
166
167 return m_pMetadata;
168}
169
170unsigned int EventPipeEvent::GetMetadataLength() const
171{
172 LIMITED_METHOD_CONTRACT;
173
174 return m_metadataLength;
175}
176
177void EventPipeEvent::RefreshState()
178{
179 LIMITED_METHOD_CONTRACT;
180
181 m_enabled = m_pProvider->EventEnabled(m_keywords, m_level);
182}
183
184#endif // FEATURE_PERFTRACING
185