Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CtfPlayback/EventStreams/CtfEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using CtfPlayback.EventStreams.Interfaces;
using CtfPlayback.FieldValues;
using CtfPlayback.Metadata;
using CtfPlayback.Metadata.Interfaces;

namespace CtfPlayback.EventStreams
Expand All @@ -25,6 +26,8 @@ public CtfEvent(PacketReader packetReader, ICtfMetadata metadata, ICtfPacket own

public ulong ByteOffsetWithinPacket { get; private set; }

public uint StreamId => this.owningPacket.StreamId;

public CtfTimestamp Timestamp { get; private set; }

public CtfFieldValue StreamDefinedEventHeader { get; private set; }
Expand Down Expand Up @@ -102,17 +105,15 @@ public void Read()

private void ReadStreamEventHeader()
{
uint streamIndex = this.owningPacket.StreamId;
var eventHeaderDescriptor = this.packetReader.Metadata.Streams[(int)streamIndex].EventHeader;
var eventHeaderDescriptor = this.packetReader.Metadata.GetStream(this.owningPacket.StreamId).EventHeader;
Debug.Assert(eventHeaderDescriptor != null);

StreamDefinedEventHeader = eventHeaderDescriptor.Read(this.packetReader);
}

private void ReadStreamEventContext()
{
uint streamIndex = this.owningPacket.StreamId;
var eventContextDescriptor = this.packetReader.Metadata.Streams[(int)streamIndex].EventContext;
var eventContextDescriptor = this.packetReader.Metadata.GetStream(this.owningPacket.StreamId).EventContext;
if (eventContextDescriptor == null)
{
return;
Expand Down
8 changes: 6 additions & 2 deletions CtfPlayback/EventStreams/CtfPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using CtfPlayback.EventStreams.Interfaces;
using CtfPlayback.FieldValues;
using CtfPlayback.Metadata;
using CtfPlayback.Metadata.Interfaces;

namespace CtfPlayback.EventStreams
Expand Down Expand Up @@ -61,10 +62,12 @@ public bool ReadPacketMetadata()
this.PacketByteOffset = this.packetReader.CountOfBytesProcessed;

ReadPacketHeader();
ReadPacketContext();

// The packet context layout depends on the stream, which is identified in the packet header.
this.DetermineStreamIndex();

ReadPacketContext();
Comment on lines 64 to +69

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So thinking out this logic -- this likely would have been previously a latent bug when reading packet metadata? given that the when that streamId is updated controls data read within ReadPacketContext().

Mostly just confirming here that the streamId needs to be updated to the current packet here, and there isn't some desired behavior for off-by-one or similar.


this.packetReader.SetPacketSize(this.packetReader.PlaybackCustomization.GetBitsInPacket(this));

this.BitsInPacketContent =
Expand Down Expand Up @@ -116,7 +119,7 @@ private void ReadPacketHeader()

private void ReadPacketContext()
{
this.StreamPacketContext = this.Metadata.Streams[(int)this.StreamId].PacketContext.Read(this.packetReader) as CtfStructValue;
this.StreamPacketContext = this.Metadata.GetStream(this.StreamId).PacketContext.Read(this.packetReader) as CtfStructValue;
}

private ulong GetRemainingBits()
Expand All @@ -135,6 +138,7 @@ private void DetermineStreamIndex()
if (streamIdDescriptor == null)
{
this.StreamId = 0;
return;
}

bool streamIdFound = this.TracePacketHeader.FieldsByName.TryGetValue("stream_id", out var streamIdFieldValue);
Expand Down
5 changes: 5 additions & 0 deletions CtfPlayback/EventStreams/Interfaces/ICtfEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public interface ICtfEvent
/// </summary>
ulong ByteOffsetWithinPacket { get; }

/// <summary>
/// Id of the stream (stream class) that contains this event.
/// </summary>
uint StreamId { get; }

/// <summary>
/// Timestamp associated with the event
/// </summary>
Expand Down
273 changes: 273 additions & 0 deletions CtfPlayback/Metadata/Ctf2/Ctf2Json.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace CtfPlayback.Metadata.Ctf2
{
/// <summary>
/// A JSON number, kept in its textual form so that 64-bit unsigned values are not truncated.
/// </summary>
internal sealed class Ctf2JsonNumber
{
internal Ctf2JsonNumber(string text)
{
this.Text = text;
}

internal string Text { get; }

internal bool IsNegative => this.Text.StartsWith("-", StringComparison.Ordinal);

internal long AsLong() => long.Parse(this.Text, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);

internal ulong AsULong() => ulong.Parse(this.Text, NumberStyles.None, CultureInfo.InvariantCulture);

public override string ToString() => this.Text;
}

/// <summary>
/// Minimal JSON reader used for CTF 2 metadata. Objects are returned as
/// <see cref="Dictionary{TKey,TValue}"/> of string to object, arrays as <see cref="List{T}"/> of object,
/// numbers as <see cref="Ctf2JsonNumber"/>, and strings, booleans and null as their .NET equivalents.
/// </summary>
internal sealed class Ctf2Json
{
private readonly string text;
private int position;

private Ctf2Json(string text)
{
this.text = text;
}

internal static object Parse(string text)
{
var reader = new Ctf2Json(text);
reader.SkipWhitespace();
var value = reader.ReadValue();
reader.SkipWhitespace();
if (reader.position != text.Length)
{
throw reader.Error("Unexpected trailing characters");
}

return value;
}

private object ReadValue()
{
if (this.position >= this.text.Length)
{
throw this.Error("Unexpected end of JSON");
}

char c = this.text[this.position];
switch (c)
{
case '{':
return this.ReadObject();
case '[':
return this.ReadArray();
case '"':
return this.ReadString();
case 't':
this.Expect("true");
return true;
case 'f':
this.Expect("false");
return false;
case 'n':
this.Expect("null");
return null;
default:
if (c == '-' || (c >= '0' && c <= '9'))
{
return this.ReadNumber();
}

throw this.Error($"Unexpected character '{c}'");
}
}

private Dictionary<string, object> ReadObject()
{
var result = new Dictionary<string, object>(StringComparer.Ordinal);
this.position++;
this.SkipWhitespace();
if (this.TryConsume('}'))
{
return result;
}

while (true)
{
this.SkipWhitespace();
if (this.position >= this.text.Length || this.text[this.position] != '"')
{
throw this.Error("Expected property name");
}

string name = this.ReadString();
this.SkipWhitespace();
if (!this.TryConsume(':'))
{
throw this.Error("Expected ':'");
}

this.SkipWhitespace();
result[name] = this.ReadValue();
this.SkipWhitespace();

if (this.TryConsume(','))
{
continue;
}

if (this.TryConsume('}'))
{
return result;
}

throw this.Error("Expected ',' or '}'");
}
}

private List<object> ReadArray()
{
var result = new List<object>();
this.position++;
this.SkipWhitespace();
if (this.TryConsume(']'))
{
return result;
}

while (true)
{
this.SkipWhitespace();
result.Add(this.ReadValue());
this.SkipWhitespace();

if (this.TryConsume(','))
{
continue;
}

if (this.TryConsume(']'))
{
return result;
}

throw this.Error("Expected ',' or ']'");
}
}

private string ReadString()
{
this.position++;
var sb = new StringBuilder();
while (this.position < this.text.Length)
{
char c = this.text[this.position++];
if (c == '"')
{
return sb.ToString();
}

if (c != '\\')
{
sb.Append(c);
continue;
}

if (this.position >= this.text.Length)
{
break;
}

char escaped = this.text[this.position++];
switch (escaped)
{
case '"': sb.Append('"'); break;
case '\\': sb.Append('\\'); break;
case '/': sb.Append('/'); break;
case 'b': sb.Append('\b'); break;
case 'f': sb.Append('\f'); break;
case 'n': sb.Append('\n'); break;
case 'r': sb.Append('\r'); break;
case 't': sb.Append('\t'); break;
case 'u':
if (this.position + 4 > this.text.Length ||
!int.TryParse(this.text.Substring(this.position, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int codeUnit))
{
throw this.Error("Invalid unicode escape");
}

sb.Append((char)codeUnit);
this.position += 4;
break;
default:
throw this.Error($"Invalid escape character '{escaped}'");
}
}

throw this.Error("Unterminated string");
}

private Ctf2JsonNumber ReadNumber()
{
int start = this.position;
while (this.position < this.text.Length)
{
char c = this.text[this.position];
if ((c >= '0' && c <= '9') || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E')
{
this.position++;
continue;
}

break;
}

return new Ctf2JsonNumber(this.text.Substring(start, this.position - start));
}

private void Expect(string literal)
{
if (string.CompareOrdinal(this.text, this.position, literal, 0, literal.Length) != 0)
{
throw this.Error($"Expected '{literal}'");
}

this.position += literal.Length;
}

private bool TryConsume(char c)
{
if (this.position < this.text.Length && this.text[this.position] == c)
{
this.position++;
return true;
}

return false;
}

private void SkipWhitespace()
{
while (this.position < this.text.Length && char.IsWhiteSpace(this.text[this.position]))
{
this.position++;
}
}

private CtfMetadataException Error(string message)
{
return new CtfMetadataException($"Invalid CTF 2 metadata JSON: {message} at offset {this.position}.");
}
}
}
Loading
Loading