Skip to content

Commit

Permalink
Fix TraceParserGen to generate meaningful parser for WPF
Browse files Browse the repository at this point in the history
  • Loading branch information
oatkins committed Apr 6, 2021
1 parent 6b4771e commit 72116c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/TraceParserGen/ETWManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ internal Provider(XmlReader reader, string fileName)
{
m_taskNames = new Dictionary<int, string>();
m_taskValues = new Dictionary<string, int>();
m_taskGuids = new Dictionary<string, Guid>();
}
string name = reader.GetAttribute("name");
int value = (int)ParseNumber(reader.GetAttribute("value"));
Expand All @@ -290,7 +291,12 @@ internal Provider(XmlReader reader, string fileName)
m_taskNames.Add(value, message);
m_taskValues.Add(name, value);

// Remember enuough to resolve opcodes nested inside this task.
if (Guid.TryParse(reader.GetAttribute("eventGUID"), out var guid))
{
m_taskGuids.Add(name, guid);
}

// Remember enough to resolve opcodes nested inside this task.
curTask = value;
curTaskDepth = reader.Depth;
reader.Read();
Expand Down Expand Up @@ -556,6 +562,7 @@ private void ReadTemplate(XmlReader reader)
private List<Event> m_events = new List<Event>();
internal string[] m_keywordNames;
internal Dictionary<int, string> m_taskNames;
internal Dictionary<string, Guid> m_taskGuids;
// Note that the key is task << 8 + opcode to allow for private opcode names
internal Dictionary<int, string> m_opcodeNames;

Expand Down Expand Up @@ -760,7 +767,7 @@ internal void ResolveIdsInEvent(string fileName = null)
if (!m_provider.m_opcodeValues.TryGetValue(Task + ":" + id, out opcode))
opcode = m_provider.m_opcodeValues[id];

Opcode = (byte) opcode;
Opcode = (byte)opcode;
}

id = m_keywordsId;
Expand Down Expand Up @@ -794,7 +801,7 @@ internal void ResolveIdsInEvent(string fileName = null)
}
}
}
catch(Exception)
catch (Exception)
{
if (fileName == null)
{
Expand Down
9 changes: 9 additions & 0 deletions src/TraceParserGen/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"profiles": {
"TraceParserGen": {
"commandName": "Project",
"commandLineArgs": "\"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\WPF\\wpf-etw.man\" \"D:\\src\\WpfTraceAnalyzer\\WpfTraceAnalyzer\\WpfTraceParser.cs\"",
"workingDirectory": "D:\\src\\WpfTraceAnalyzer\\WpfTraceAnalyzer"
}
}
}
33 changes: 27 additions & 6 deletions src/TraceParserGen/TraceParserGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ private void GenerateTraceEventParserClass(TextWriter output)

// *********** GENERATE EVENTS ********** //
GenerateEvents(output);

GenerateTaskGuids(output);

output.WriteLine("");
output.WriteLine(" #region private");
output.WriteLine(" protected override string GetProviderName() { return ProviderName; }");
Expand Down Expand Up @@ -220,8 +223,12 @@ private static string FixKeywordName(string keywordName)
keywordName = keywordName.Substring(keywordIdx + 8);
}

// Convert it to CamelCase.
if (keywordName.IndexOf('_') < 0)
{
return keywordName;
}

// Convert it to CamelCase.
var sb = new StringBuilder();
bool capitalizeNext = true;
for (int i = 0; i < keywordName.Length; i++)
Expand Down Expand Up @@ -249,6 +256,21 @@ private static string FixKeywordName(string keywordName)
return sb.ToString();
}

private void GenerateTaskGuids(TextWriter output)
{
var dictionary = m_provider?.m_taskGuids;
if (dictionary == null)
{
return;
}

foreach (var pair in dictionary)
{
var fieldName = $"{pair.Key}TaskGuid";
output.WriteLine($" public static readonly Guid {fieldName} = Guid.Parse(\"{pair.Value}\");");
}
}

/// <summary>
/// Generate the *Template helper functions as well as the EnumerateTemplates operation.
/// </summary>
Expand Down Expand Up @@ -299,12 +321,11 @@ private void GenerateTemplateDefs(TextWriter output)
var evnt = m_provider.Events[i];

// check if the same event template has not been already defined (different versions of the same event)
output.WriteLine(" templates[{0}] = new {1}TraceData(null, {2}, {3}, \"{4}\", {4}TaskGuid, {5}, \"{6}\", ProviderGuid, ProviderName);",
i, TraceParserGen.ToCSharpName(evnt.EventName),
output.WriteLine(" templates[{0}] = new {1}(null, {2}, {3}, \"{4}\", {4}TaskGuid, {5}, \"{6}\", ProviderGuid, ProviderName);",
i,
TraceParserGen.GetTemplateNameForEvent(evnt, evnt.TemplateName),
evnt.Id, evnt.Task, TraceParserGen.ToCSharpName(evnt.TaskName), evnt.Opcode, TraceParserGen.ToCSharpName(evnt.OpcodeName)
);
// as of today, the generated code won't compile because the task GUID is not defined
// TODO: define the xxxTaskGuid based on eventGUID attribute of <task> elements of the .man file
}

output.WriteLine(" s_templates = templates;");
Expand Down Expand Up @@ -346,7 +367,7 @@ private void GenerateEvents(TextWriter output)
var taskName = TraceParserGen.ToCSharpName(evnt.TaskName);
if (string.IsNullOrEmpty(taskName)) taskName = evntName;
// Call the *Template() function that does the work
output.WriteLine(" RegisterTemplate(new {0}(value, {1}, {2}, \"{3}\", {4}, {5}, \"{6}\", ProviderGuid, ProviderName));",
output.WriteLine(" source.RegisterEventTemplate(new {0}(value, {1}, {2}, \"{3}\", {4}, {5}, \"{6}\", ProviderGuid, ProviderName));",
templateClassName, evnt.Id, evnt.Task, taskName, taskGuid,
evnt.Opcode, TraceParserGen.ToCSharpName(evnt.OpcodeName)
);
Expand Down

0 comments on commit 72116c4

Please sign in to comment.