From 43f39b27fd05947bef77c9bb331671dda33a279c Mon Sep 17 00:00:00 2001 From: "j-h.choi" Date: Thu, 29 Jul 2021 11:06:42 +0900 Subject: [PATCH] Code review feedback --- src/Tools/dotnet-stack/Symbolicate.cs | 120 +++++++++++++++++--------- 1 file changed, 79 insertions(+), 41 deletions(-) diff --git a/src/Tools/dotnet-stack/Symbolicate.cs b/src/Tools/dotnet-stack/Symbolicate.cs index dc3ff4d799..413139a5e2 100644 --- a/src/Tools/dotnet-stack/Symbolicate.cs +++ b/src/Tools/dotnet-stack/Symbolicate.cs @@ -21,6 +21,7 @@ internal static class SymbolicateHandler // Temporary folder to store the files converted from pdb to xml private static readonly string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); private static readonly Regex regex = new Regex(@" at (?[\w+\.?]+)\.(?\w+)\((?.*)\) in (?[\w+\.?]+)(\.dll|\.ni\.dll): token (?0x\d+)\+(?0x\d+)", RegexOptions.Compiled); + private static readonly Regex verifyRegex = new Regex(@"at (?.*)\((?.*?)\) in (?.*)token(?.*)\+(?.*)", RegexOptions.Compiled); delegate void SymbolicateDelegate(IConsole console, FileInfo inputPath, DirectoryInfo[] searchDir, string output); @@ -37,23 +38,19 @@ private static void Symbolicate(IConsole console, FileInfo inputPath, DirectoryI try { List search_paths = new List(); - if (searchDir.Length == 0) - { - search_paths.Add(Directory.GetCurrentDirectory()); - } - else + if (searchDir.Length != 0) { foreach (var path in searchDir) { search_paths.Add(path.FullName); } } - + search_paths.Add(Directory.GetCurrentDirectory()); Symbolicator(console, PdbToXmlConvert(search_paths), inputPath.FullName, output); } catch (Exception e) { - Console.Error.WriteLine(e.Message); + console.Error.WriteLine(e.Message); } } @@ -104,7 +101,10 @@ private static List GrabFiles(List paths, string searchPattern) { foreach (var peFile in Directory.GetFiles(assemDir, searchPattern, SearchOption.AllDirectories)) { - files.Add(peFile); + if (!peFile.Contains(".ni.dll")) + { + files.Add(peFile); + } } } } @@ -162,46 +162,60 @@ private static void GetLineFromStack(IConsole console, List xmlList, str { try { - string output = string.Empty; + StreamWriter fswi = null; + if (!inputPath.EndsWith(".symbolicated")) + { + fswi = new StreamWriter(new FileStream(inputPath + ".symbolicated", FileMode.Create, FileAccess.Write)); + } - StreamWriter fsw = null; + string output = string.Empty; + StreamWriter fswo = null; if (outputPath != null) { - fsw = new StreamWriter(new FileStream(outputPath, FileMode.Create, FileAccess.Write)); + fswo = new StreamWriter(new FileStream(outputPath, FileMode.Create, FileAccess.Write)); output = $"\nOutput: {outputPath}\n"; } - using StreamReader fsr = new StreamReader(new FileStream(inputPath, FileMode.Open, FileAccess.Read)); - while (!fsr.EndOfStream) + using StreamReader fsri = new StreamReader(new FileStream(inputPath, FileMode.Open, FileAccess.Read)); + while (!fsri.EndOfStream) { - string line = fsr.ReadLine(); - if (!line.Contains("at ") || !line.Contains("+")) + string line = fsri.ReadLine(); + // The stacktrace must have "at ... in ... token ..." + if (!line.Contains("at ") || !line.Contains(" in ") || !line.Contains("token")) { - fsw?.WriteLine(line); + fswo?.WriteLine(line); console.Out.WriteLine($"{line}"); continue; } - string ret = GetRegex(line, xmlList); - fsw?.WriteLine(ret); + string ret = GetRegex(console, line, xmlList, fswi); + fswo?.WriteLine(ret); console.Out.WriteLine($"{ret}"); } - fsw?.Close(); + fswo?.Close(); + fswi?.Close(); console.Out.WriteLine($"{output}"); } catch (Exception e) { - Console.Error.WriteLine(e); + console.Error.WriteLine(e.Message); } } - private static string GetRegex(string line, List xmlList) + private static string GetRegex(IConsole console, string line, List xmlList, StreamWriter fsw) { string ret = line; - Match match = regex.Match(ret); - if (!match.Success) + Match match = regex.Match(line); + if (!match.Success && fsw != null) { - return ret; + ret = VerifyStackTraceLine(line); + if (ret == line) + { + fsw.WriteLine(line); + return line; + } + match = regex.Match(ret); } + fsw?.WriteLine(ret); StackTraceInfo stInfo = new StackTraceInfo() { Type = match.Groups["type"].Value, @@ -217,18 +231,42 @@ private static string GetRegex(string line, List xmlList) { if (xmlPath.Contains(xmlStr)) { - GetLineFromXml(xmlPath, stInfo); + GetLineFromXml(console, xmlPath, stInfo); if (stInfo.Filepath != null && stInfo.StartLine != null) { - ret = $" at {stInfo.Type}.{stInfo.Method}({stInfo.Param}) in {stInfo.Filepath}:line {stInfo.StartLine}"; - break; + return $" at {stInfo.Type}.{stInfo.Method}({stInfo.Param}) in {stInfo.Filepath}:line {stInfo.StartLine}"; } } } return ret; } - private static void GetLineFromXml(string xmlPath, StackTraceInfo stInfo) + private static string VerifyStackTraceLine(string line) + { + Match match = verifyRegex.Match(line); + StringBuilder str = new StringBuilder(); + str.Append(" at "); + str.Append(match.Groups["typeMethod"].Value.TrimEnd()); + str.Append("("); + str.Append(match.Groups["params"].Value); + str.Append(") in "); + str.Append(match.Groups["filename"].Value.Replace(":", "").Trim()); + str.Append(": token "); + str.Append(match.Groups["token"].Value.Trim()); + str.Append("+"); + str.Append(match.Groups["offset"].Value.TrimStart().Split(" ")[0]); + + if (regex.Match(str.ToString()).Success) + { + return str.ToString(); + } + else + { + return line; + } + } + + private static void GetLineFromXml(IConsole console, string xmlPath, StackTraceInfo stInfo) { try { @@ -244,22 +282,22 @@ private static void GetLineFromXml(string xmlPath, StackTraceInfo stInfo) XmlNode node = xnList[i]; if (node.Name == "files") { - ParseFile(node.ChildNodes, stInfo); + ParseFile(console, node.ChildNodes, stInfo); } else if (node.Name == "methods") { - ParseMethod(node.ChildNodes, stInfo); + ParseMethod(console, node.ChildNodes, stInfo); } } } } catch (ArgumentException e) { - Console.Error.WriteLine(e); + console.Error.WriteLine(e.Message); } } - private static void ParseFile(XmlNodeList xn, StackTraceInfo stInfo) + private static void ParseFile(IConsole console, XmlNodeList xn, StackTraceInfo stInfo) { try { @@ -274,11 +312,11 @@ private static void ParseFile(XmlNodeList xn, StackTraceInfo stInfo) } catch (ArgumentException e) { - Console.Error.WriteLine(e); + console.Error.WriteLine(e.Message); } } - private static void ParseMethod(XmlNodeList xn, StackTraceInfo stInfo) + private static void ParseMethod(IConsole console, XmlNodeList xn, StackTraceInfo stInfo) { try { @@ -292,17 +330,17 @@ private static void ParseMethod(XmlNodeList xn, StackTraceInfo stInfo) { stInfo.Param = node.Attributes["parameterNames"].Value; } - ParseSequence(node.ChildNodes, stInfo); + ParseSequence(console, node.ChildNodes, stInfo); } } } catch (ArgumentException e) { - Console.Error.WriteLine(e); + console.Error.WriteLine(e.Message); } } - private static void ParseSequence(XmlNodeList xn, StackTraceInfo stInfo) + private static void ParseSequence(IConsole console, XmlNodeList xn, StackTraceInfo stInfo) { try { @@ -310,17 +348,17 @@ private static void ParseSequence(XmlNodeList xn, StackTraceInfo stInfo) { if (node.Name == "sequencePoints") { - ParseEntry(node.ChildNodes, stInfo); + ParseEntry(console, node.ChildNodes, stInfo); } } } catch (ArgumentException e) { - Console.Error.WriteLine(e); + console.Error.WriteLine(e.Message); } } - private static void ParseEntry(XmlNodeList xn, StackTraceInfo stInfo) + private static void ParseEntry(IConsole console, XmlNodeList xn, StackTraceInfo stInfo) { try { @@ -347,7 +385,7 @@ private static void ParseEntry(XmlNodeList xn, StackTraceInfo stInfo) } catch (ArgumentException e) { - Console.Error.WriteLine(e); + console.Error.WriteLine(e.Message); } }