[msvc] Update csproj files (#4074)
[mono.git] / mcs / tools / mono-symbolicate / StackFrameData.cs
1 using System;
2 using System.Text.RegularExpressions;
3 using System.Globalization;
4
5 namespace Mono
6 {
7         class StackFrameData
8         {
9                 static Regex regex = new Regex (@"\w*at (?<Method>.+) *(\[0x(?<IL>.+)\]|<0x.+ \+ 0x(?<NativeOffset>.+)>( (?<MethodIndex>\d+)|)) in <(?<MVID>[^>#]+)(#(?<AOTID>[^>]+)|)>:0");
10
11                 public readonly string TypeFullName;
12                 public readonly string MethodSignature;
13                 public readonly int Offset;
14                 public readonly bool IsILOffset;
15                 public readonly uint MethodIndex;
16                 public readonly string Line;
17                 public readonly string Mvid;
18                 public readonly string Aotid;
19
20                 public string File { get; private set; }
21                 public int LineNumber { get; private set; }
22
23                 private StackFrameData (string line, string typeFullName, string methodSig, int offset, bool isILOffset, uint methodIndex, string mvid, string aotid)
24                 {
25                         LineNumber = -1;
26
27                         Line = line;
28                         TypeFullName = typeFullName;
29                         MethodSignature = methodSig;
30                         Offset = offset;
31                         IsILOffset = isILOffset;
32                         MethodIndex = methodIndex;
33                         Mvid = mvid;
34                         Aotid = aotid;
35                 }
36
37                 public static bool TryParse (string line, out StackFrameData stackFrame)
38                 {
39                         stackFrame = null;
40
41                         var match = regex.Match (line);
42                         if (!match.Success)
43                                 return false;
44
45                         string typeFullName, methodSignature;
46                         var methodStr = match.Groups ["Method"].Value.Trim ();
47                         if (!ExtractSignatures (methodStr, out typeFullName, out methodSignature))
48                                 return false;
49
50                         var isILOffset = !string.IsNullOrEmpty (match.Groups ["IL"].Value);
51                         var offsetVarName = (isILOffset)? "IL" : "NativeOffset";
52                         var offset = int.Parse (match.Groups [offsetVarName].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
53
54                         uint methodIndex = 0xffffff;
55                         if (!string.IsNullOrEmpty (match.Groups ["MethodIndex"].Value))
56                                 methodIndex = uint.Parse (match.Groups ["MethodIndex"].Value, CultureInfo.InvariantCulture);
57
58                         var mvid = match.Groups ["MVID"].Value;
59                         var aotid = match.Groups ["AOTID"].Value;
60
61                         stackFrame = new StackFrameData (line, typeFullName, methodSignature, offset, isILOffset, methodIndex, mvid, aotid);
62
63                         return true;
64                 }
65
66                 static bool ExtractSignatures (string str, out string typeFullName, out string methodSignature)
67                 {
68                         var methodNameEnd = str.IndexOf ('(');
69                         if (methodNameEnd == -1) {
70                                 typeFullName = methodSignature = null;
71                                 return false;
72                         }
73
74                         var typeNameEnd = str.LastIndexOf ('.', methodNameEnd);
75                         if (typeNameEnd == -1) {
76                                 typeFullName = methodSignature = null;
77                                 return false;
78                         }
79
80                         // Adjustment for Type..ctor ()
81                         if (typeNameEnd > 0 && str [typeNameEnd - 1] == '.') {
82                                 --typeNameEnd;
83                         }
84
85                         typeFullName = str.Substring (0, typeNameEnd);
86                         // Remove generic parameters
87                         typeFullName = Regex.Replace (typeFullName, @"\[[^\[\]]*\]", "");
88
89                         methodSignature = str.Substring (typeNameEnd + 1);
90
91                         return true;
92                 }
93
94                 internal void SetLocation (string file, int lineNumber)
95                 {
96                         File = file;
97                         LineNumber = lineNumber;
98                 }
99
100                 public override string ToString ()
101                 {
102                         return string.Format ("{0} in {1}:{2} ", Line.Substring (0, Line.IndexOf(" in <", StringComparison.Ordinal)), File, LineNumber);
103                 }
104         }
105 }