-
Notifications
You must be signed in to change notification settings - Fork 758
Expand file tree
/
Copy pathCtfChannel.cs
More file actions
282 lines (235 loc) · 7.66 KB
/
CtfChannel.cs
File metadata and controls
282 lines (235 loc) · 7.66 KB
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
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
namespace Microsoft.Diagnostics.Tracing.Ctf
{
internal sealed class CtfChannel : Stream
{
private CtfMetadata _metadata;
private CtfStream _ctfStream;
private Stream _stream;
private long _position;
private byte[] _buffer = new byte[256];
private GCHandle _handle;
private long _packetSize;
private long _contentSize;
public CtfStream CtfStream { get { return _ctfStream; } }
public CtfChannel(Stream stream, CtfMetadata metadata)
{
_stream = stream;
_position = 0;
_metadata = metadata;
_handle = GCHandle.Alloc(_buffer, GCHandleType.Pinned);
ReadContext();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_handle.IsAllocated)
{
_handle.Free();
}
}
private bool ReadContext()
{
do
{
while (_packetSize > 0)
{
// Zip filestream can't seek, we have to read the rest of the packet.
int toRead = _buffer.Length <= _packetSize ? _buffer.Length : (int)_packetSize;
int read = _stream.Read(_buffer, 0, toRead);
_packetSize -= read;
_position += read;
if (read != toRead)
{
return false;
}
}
if (!ReadTraceHeader())
{
return false;
}
if (!ReadPacketContext())
{
return false;
}
} while (_contentSize == 0);
return true;
}
private bool ReadTraceHeader()
{
// Read Trace Header
CtfStruct traceHeader = _metadata.Trace.Header;
int traceHeaderSize = traceHeader.GetSize();
if (traceHeaderSize == CtfEvent.SizeIndeterminate)
{
throw new FormatException("Unexpected metadata format.");
}
int magicOffset = traceHeader.GetFieldOffset("magic");
if (magicOffset < 0)
{
throw new FormatException("Unexpected metadata format: No magic field.");
}
int streamIdOffset = traceHeader.GetFieldOffset("stream_id");
if (streamIdOffset < 0)
{
throw new FormatException("Unexpected metadata format: No stream_id field.");
}
// Convert to bytes instead of bits
magicOffset /= 8;
streamIdOffset /= 8;
traceHeaderSize /= 8;
if (TryReadExactlyCount(_buffer, 0, traceHeaderSize) != traceHeaderSize)
{
return false;
}
_position += traceHeaderSize;
uint magic = BitConverter.ToUInt32(_buffer, magicOffset);
if (magic != 0xc1fc1fc1)
{
throw new FormatException("Unknown magic number in trace header.");
}
uint streamId = BitConverter.ToUInt32(_buffer, streamIdOffset);
_ctfStream = _metadata.Streams[streamId];
return true;
}
private bool ReadPacketContext()
{
// Read Packet Context
CtfStruct packetContext = _ctfStream.PacketContext;
int packetContextSize = packetContext.GetSize();
if (packetContextSize == CtfEvent.SizeIndeterminate)
{
throw new FormatException("Unexpected metadata format.");
}
int contentSizeOffset = packetContext.GetFieldOffset("content_size");
if (contentSizeOffset < 0)
{
throw new FormatException("Unexpected metadata format: No context_size field.");
}
int packetSizeOffset = packetContext.GetFieldOffset("packet_size");
if (packetSizeOffset < 0)
{
throw new FormatException("Unexpected metadata format: No packet_size field.");
}
// Convert to bytes instead of bits
packetContextSize /= 8;
contentSizeOffset /= 8;
packetSizeOffset /= 8;
if (TryReadExactlyCount(_buffer, 0, packetContextSize) != packetContextSize)
{
return false;
}
_position += packetContextSize;
int headerSize = (_metadata.Trace.Header.GetSize() / 8) + packetContextSize;
_contentSize = (long)BitConverter.ToUInt64(_buffer, contentSizeOffset) / 8 - headerSize;
_packetSize = (long)BitConverter.ToUInt64(_buffer, packetSizeOffset) / 8 - headerSize;
return true;
}
private void ReadHeader()
{
int bytes = _ctfStream.EventHeader.GetSize();
}
public override bool CanRead
{
get
{
return true;
}
}
public override long Position
{
get
{
return _position;
}
set
{
throw new NotSupportedException();
}
}
public override int ReadByte()
{
if (_contentSize == 0 && !ReadContext())
{
return -1;
}
_contentSize--;
_packetSize--;
int value = _stream.ReadByte();
if (value != -1)
_position++;
Debug.Assert(value != -1);
return value;
}
public override int Read(byte[] buffer, int offset, int count)
{
if (_contentSize == 0 && !ReadContext())
{
return 0;
}
int toRead = count > _contentSize ? (int)_contentSize : count;
int read = TryReadExactlyCount(buffer, offset, toRead);
_contentSize -= read;
_packetSize -= read;
_position += read;
return read;
}
private int TryReadExactlyCount(byte[] buffer, int offset, int count)
{
int totalRead = 0;
while (totalRead < count)
{
var read = _stream.Read(buffer, offset + totalRead, count - totalRead);
if (read == 0)
{
return totalRead;
}
totalRead += read;
}
return totalRead;
}
#region Not Implemented
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return false;
}
}
public override long Length
{
get
{
throw new NotImplementedException();
}
}
public override void Flush()
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
#endregion
}
}