Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TraceParserGen.cs fixes to generate compiling code #1547

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 36 additions & 14 deletions src/TraceParserGen/TraceParserGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public string ClassNamePrefix
public bool NeedsParserState;

/// <summary>
/// Once you have set all the properties you wish, you can actually geneate a TraceEventParser to a
/// Once you have set all the properties you wish, you can actually generate a TraceEventParser to a
/// particular output file by calling this routine.
/// </summary>
public void GenerateTraceEventParserFile(string outputFileName)
Expand All @@ -102,6 +102,7 @@ public void GenerateTraceEventParserFile(string outputFileName)
output.WriteLine("using System.Text;");
output.WriteLine("using Microsoft.Diagnostics.Tracing;");
output.WriteLine("using Address = System.UInt64;");
output.WriteLine("using FastSerialization;");
output.WriteLine();
output.WriteLine("#pragma warning disable 1591 // disable warnings on XML comments not being present");
output.WriteLine("");
Expand Down Expand Up @@ -144,6 +145,8 @@ private void GenerateTraceEventParserClass(TextWriter output)

if (NeedsParserState)
{
output.WriteLine(" private {0} ParserState;", stateClassName);
output.WriteLine();
output.WriteLine(" private " + stateClassName + " State");
output.WriteLine(" {");
output.WriteLine(" get");
Expand Down Expand Up @@ -255,8 +258,7 @@ private static string FixKeywordName(string keywordName)
/// <param name="output"></param>
private void GenerateTemplateDefs(TextWriter output)
{

// Geneate all the helper functions that initialize a single template for an event.
// Generate all the helper functions that initialize a single template for an event.
foreach (var keyValue in m_eventsByName)
{
var evntName = keyValue.Key;
Expand All @@ -268,15 +270,15 @@ private void GenerateTemplateDefs(TextWriter output)
stateArg = ", " + templateClassName + "State state=null";
}

output.WriteLine(" static private {0} {1}Template(Action<{0}> action{2})", templateClassName, TraceParserGen.ToCSharpName(evntName), stateArg);
output.WriteLine(" static private {0} {1}Template(Action<{0}> action, {2}State state)", templateClassName, TraceParserGen.ToCSharpName(evntName), ClassNamePrefix);
output.WriteLine(" { // action, eventid, taskid, taskName, taskGuid, opcode, opcodeName, providerGuid, providerName");
var state = "";
if (NeedsParserState && templateClassName != "EmptyTraceData")
{
state = ", state";
}

output.WriteLine(" return new {0}(action, {1}, {2}, \"{3}\", Guid.Empty, {4}, \"{5}\", ProviderGuid, ProviderName {6});",
output.WriteLine(" return new {0}(action, {1}, {2}, \"{3}\", {3}TaskGuid, {4}, \"{5}\", ProviderGuid, ProviderName{6});",
templateClassName, evnt.Id, evnt.Task, TraceParserGen.ToCSharpName(evnt.TaskName), evnt.Opcode, TraceParserGen.ToCSharpName(evnt.OpcodeName), state);
output.WriteLine(" }");
}
Expand All @@ -298,12 +300,14 @@ private void GenerateTemplateDefs(TextWriter output)
{
var evnt = m_provider.Events[i];

string templateClassName = GetTemplateNameForEvent(evnt, evnt.EventName);

// 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, null);",
i, templateClassName,
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
// as of today, the task GUID defaults to Guid.Empty and declarations are written in the following code
// TODO: define the xxxTaskGuid based on eventGUID attribute of <task> elements of the .man file
}

Expand All @@ -314,6 +318,26 @@ private void GenerateTemplateDefs(TextWriter output)
output.WriteLine(" callback(template);");
output.WriteLine(" }");
output.WriteLine();

output.WriteLine(" // These default to Guid.Empty. Modify as needed to assign unique GUIDs");

var eventsGenerated = new HashSet<string>();

for (int i = 0; i < m_provider.Events.Count; i++)
{
var evnt = m_provider.Events[i];
var csharpName = TraceParserGen.ToCSharpName(evnt.TaskName);

if (eventsGenerated.Contains(csharpName))
{
continue;
}

output.WriteLine(" private static readonly Guid {0}TaskGuid = Guid.Empty;", csharpName);

eventsGenerated.Add(csharpName);
}
output.WriteLine();
}

/// <summary>
Expand Down Expand Up @@ -346,22 +370,19 @@ 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));",
templateClassName, evnt.Id, evnt.Task, taskName, taskGuid,
evnt.Opcode, TraceParserGen.ToCSharpName(evnt.OpcodeName)
);
output.WriteLine(" source.RegisterEventTemplate(" + evntName + "Template(value,state));");
output.WriteLine(" }");
output.WriteLine(" remove");
output.WriteLine(" {");
output.WriteLine(" source.UnregisterEventTemplate(value, " + evnt.Id + ", " + taskGuid + ");");
output.WriteLine(" source.UnregisterEventTemplate(value, " + evnt.Id + ", ProviderGuid);");
output.WriteLine(" }");
output.WriteLine(" }");
}
}

private void GenerateEventPayloadClass(TextWriter output, string stateClassName)
{
// Severla distinct events might have the same payload class, this Dictionary keeps track
// Several distinct events might have the same payload class, this Dictionary keeps track
// of which we have emitted so we don't emit it twice.
var classesEmitted = new Dictionary<string, string>();

Expand Down Expand Up @@ -1111,6 +1132,7 @@ public FieldInfo(string name, string typeName, FieldInfo prevField, int count, s
ByteSize = 16;
break;
case "trace:WBEMSid":
case "win:SID":
SkipMethod = "SkipSID(" + FieldInfo.Skip(prevField) + ")";
break;
case "win:GUID":
Expand Down