-
Notifications
You must be signed in to change notification settings - Fork 0
/
TraceSession.cs
153 lines (137 loc) · 5.27 KB
/
TraceSession.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
/*
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 static etwlib.NativeTraceConsumer;
using static etwlib.NativeTraceControl;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace etwlib
{
using static TraceLogger;
public abstract class TraceSession : IDisposable
{
protected EVENT_TRACE_LOGFILE m_LogFile;
protected bool m_Disposed;
protected long m_PerfFreq;
protected List<EnabledProvider> m_EnabledProviders;
public TraceSession()
{
m_LogFile = new EVENT_TRACE_LOGFILE();
m_Disposed = false;
m_PerfFreq = 0;
m_EnabledProviders = new List<EnabledProvider>();
}
protected virtual void Dispose(bool disposing)
{
Trace(TraceLoggerType.TraceSession,
TraceEventType.Information,
"Disposing FileTrace");
if (m_Disposed)
{
return;
}
m_Disposed = true;
}
public virtual void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public EnabledProvider AddProvider(Guid Id, string Name, byte traceLevel, ulong matchAnyKeyword, ulong matchAllKeyword)
{
var provider = new EnabledProvider(Id, Name, traceLevel, matchAllKeyword, matchAnyKeyword);
m_EnabledProviders.Add(provider);
return provider;
}
public EnabledProvider AddProvider(Guid Id, string Name, EventTraceLevel traceLevel, ulong matchAnyKeyword, ulong matchAllKeyword)
{
var provider = new EnabledProvider(Id, Name, (byte)traceLevel, matchAllKeyword, matchAnyKeyword);
m_EnabledProviders.Add(provider);
return provider;
}
public void AddProvider(EnabledProvider Provider)
{
m_EnabledProviders.Add(Provider);
}
public abstract void Start();
public abstract void Stop();
public void Consume(
EventRecordCallback EventCallback,
BufferCallback BufferCallback
)
{
m_LogFile.BufferCallback = BufferCallback;
m_LogFile.EventCallback = EventCallback;
var logFilePointer = Marshal.AllocHGlobal(Marshal.SizeOf(m_LogFile));
Marshal.StructureToPtr(m_LogFile, logFilePointer, false);
var handle = OpenTrace(logFilePointer);
//
// Marshal the structure back so we can get the PerfFreq
//
var logfile = (EVENT_TRACE_LOGFILE)Marshal.PtrToStructure(
logFilePointer, typeof(EVENT_TRACE_LOGFILE))!;
Marshal.FreeHGlobal(logFilePointer);
if (handle == -1 || handle == 0)
{
var error = "OpenTrace() returned an invalid handle: 0x" +
Marshal.GetLastWin32Error().ToString("X");
Trace(TraceLoggerType.TraceSession,
TraceEventType.Error,
error);
throw new Exception(error);
}
Trace(TraceLoggerType.TraceSession,
TraceEventType.Information,
"Trace session successfully opened, processing trace..");
try
{
//
// Update PerfFreq so event's timestamps can be parsed.
//
m_PerfFreq = logfile.LogfileHeader.PerfFreq.QuadPart;
//
// Blocking call. The caller's BufferCallback must return false to
// unblock this routine.
//
var status = ProcessTrace(
new long[1] { handle },
1,
nint.Zero,
nint.Zero);
if (status != ERROR_SUCCESS)
{
var error = "ProcessTrace() failed: 0x" + status.ToString("X") +
", GetLastError: " + Marshal.GetLastWin32Error().ToString("X");
Trace(TraceLoggerType.TraceSession,
TraceEventType.Error,
error);
throw new Exception(error);
}
Trace(TraceLoggerType.TraceSession,
TraceEventType.Information,
"Trace processing successfully completed.");
}
finally
{
CloseTrace(handle);
}
}
public long GetPerfFreq()
{
return m_PerfFreq;
}
}
}