18 using System.Collections.Generic;
22 using TraceLab.Components.Types.Preprocessors.ExecutionTraces;
24 namespace TraceLab.Components.DevelopmentKit.Preprocessors.ExecutionTraces
38 TextReader brTrace =
new StreamReader(filename);
42 Dictionary<string, string> idToClass =
new Dictionary<string,string>();
43 Dictionary<string, string> idToMethod =
new Dictionary<string,string>();
45 Dictionary<string, Stack<string>> threadToMethodsStackTranslator =
new Dictionary<string, Stack<string>>();
48 while ((currentLine = brTrace.ReadLine()) != null)
51 string[] traceLineSplit;
53 if (currentLine.StartsWith(
"<classDef "))
55 traceLineSplit = currentLine.Split(
' ');
56 if (traceLineSplit[1].StartsWith(
"name=") ==
false)
57 throw new Exception();
58 if (traceLineSplit[2].StartsWith(
"sourceName=") ==
false)
59 throw new Exception();
60 if (traceLineSplit[3].StartsWith(
"classId=") ==
false)
61 throw new Exception();
62 string name = ExtractValueFromQuotes(traceLineSplit[1]);
63 string classID = ExtractValueFromQuotes(traceLineSplit[3]);
65 idToClass.Add(classID, name);
69 if (currentLine.StartsWith(
"<methodDef "))
71 traceLineSplit = currentLine.Split(
' ');
72 if (traceLineSplit[1].StartsWith(
"name=") ==
false)
73 throw new Exception();
74 if (traceLineSplit[2].StartsWith(
"signature=") ==
false)
75 throw new Exception();
76 if (traceLineSplit[3].StartsWith(
"startLineNumber=") ==
false)
77 throw new Exception();
78 if (traceLineSplit[4].StartsWith(
"endLineNumber=") ==
false)
79 throw new Exception();
80 if (traceLineSplit[5].StartsWith(
"methodId=") ==
false)
81 throw new Exception();
82 if (traceLineSplit[6].StartsWith(
"classIdRef=") ==
false)
83 throw new Exception();
84 string name = ExtractValueFromQuotes(traceLineSplit[1]);
85 string signature = ExtractValueFromQuotes(traceLineSplit[2]);
86 string methodID = ExtractValueFromQuotes(traceLineSplit[5]);
87 string classID = ExtractValueFromQuotes(traceLineSplit[6]);
90 if (name.Equals(
"-init-"))
92 string fullClassName = idToClass[classID];
94 string className = fullClassName.Substring(fullClassName.LastIndexOf(
"/") + 1);
96 if (className.IndexOf(
"$") > 0)
99 className = className.Substring(className.LastIndexOf(
"$") + 1);
105 if (name.EndsWith(
"$"))
107 name = name.Substring(0, name.LastIndexOf(
"$"));
109 string fullMethodName = idToClass[classID] +
"#" + name;
110 fullMethodName = fullMethodName.Replace(
'/',
'.');
111 fullMethodName = fullMethodName.Replace(
'#',
'.');
112 fullMethodName = fullMethodName.Replace(
'$',
'.');
114 idToMethod.Add(methodID, fullMethodName);
118 if (currentLine.StartsWith(
"<methodEntry "))
120 traceLineSplit = currentLine.Split(
' ');
121 if (traceLineSplit[1].StartsWith(
"threadIdRef=") ==
false)
122 throw new Exception();
123 if (traceLineSplit[2].StartsWith(
"time=") ==
false)
124 throw new Exception();
125 if (traceLineSplit[3].StartsWith(
"methodIdRef=") ==
false)
126 throw new Exception();
127 if (traceLineSplit[4].StartsWith(
"classIdRef=") ==
false)
128 throw new Exception();
129 if (traceLineSplit[5].StartsWith(
"ticket=") ==
false)
130 throw new Exception();
131 if (traceLineSplit[6].StartsWith(
"stackDepth=") ==
false)
132 throw new Exception();
133 string threadID = ExtractValueFromQuotes(traceLineSplit[1]);
134 string methodIDTrace = ExtractValueFromQuotes(traceLineSplit[3]);
136 if (!threadToMethodsStackTranslator.ContainsKey(threadID))
138 threadToMethodsStackTranslator.Add(threadID,
new Stack<string>());
145 string parentMethodID = null;
148 parentMethodID = threadToMethodsStackTranslator[threadID].Peek();
150 catch (InvalidOperationException)
158 string childMethodID = methodIDTrace;
159 threadToMethodsStackTranslator[threadID].Push(childMethodID);
161 if (threadToMethodsStackTranslator[threadID].Count == 1)
168 if (String.IsNullOrWhiteSpace(parentMethodID) || String.IsNullOrWhiteSpace(childMethodID))
171 if (idToMethod[parentMethodID].EndsWith(
".class")
172 || idToMethod[parentMethodID].EndsWith(
".-clinit-")
173 || idToMethod[parentMethodID].EndsWith(
".1")
174 || idToMethod[parentMethodID].Contains(
".1.")
178 if (idToMethod[childMethodID].EndsWith(
".class")
179 || idToMethod[childMethodID].EndsWith(
".-clinit-")
180 || idToMethod[childMethodID].EndsWith(
".1")
181 || idToMethod[childMethodID].Contains(
".1.")
186 if (parentMethodID.Equals(childMethodID) || idToMethod[parentMethodID].Equals(idToMethod[childMethodID]))
189 bigrams.Add(
new BiGram(idToMethod[parentMethodID], idToMethod[childMethodID]));
193 if (currentLine.StartsWith(
"<methodExit "))
195 traceLineSplit = currentLine.Split(
' ');
196 if (traceLineSplit[1].StartsWith(
"threadIdRef=") ==
false)
197 throw new Exception();
198 if (traceLineSplit[2].StartsWith(
"methodIdRef=") ==
false)
199 throw new Exception();
200 if (traceLineSplit[3].StartsWith(
"classIdRef=") ==
false)
201 throw new Exception();
202 if (traceLineSplit[4].StartsWith(
"ticket=") ==
false)
203 throw new Exception();
204 if (traceLineSplit[5].StartsWith(
"time=") ==
false)
205 throw new Exception();
207 string threadID = ExtractValueFromQuotes(traceLineSplit[1]);
208 string methodIDTrace = ExtractValueFromQuotes(traceLineSplit[2]);
211 string topOfStack = threadToMethodsStackTranslator[threadID].Peek();
212 if (topOfStack.Equals(methodIDTrace) ==
false)
213 throw new Exception(currentLine +
"\r\n" + threadToMethodsStackTranslator[threadID]);
214 threadToMethodsStackTranslator[threadID].Pop();
230 TextReader brTrace =
new StreamReader(filename);
233 HashSet<string> setOfUniqueMethodsTrace =
new HashSet<string>();
235 Dictionary<string, string> idToClass =
new Dictionary<string,string>();
236 Dictionary<string, string> idToMethod =
new Dictionary<string, string>();
239 while ((currentLine = brTrace.ReadLine()) !=null)
242 string[] traceLineSplit;
244 if (currentLine.StartsWith(
"<classDef "))
246 traceLineSplit = currentLine.Split(
' ');
247 if (traceLineSplit[1].StartsWith(
"name=") ==
false)
248 throw new Exception();
249 if (traceLineSplit[2].StartsWith(
"sourceName=") ==
false)
250 throw new Exception();
251 if (traceLineSplit[3].StartsWith(
"classId=") ==
false)
252 throw new Exception();
254 string name = ExtractValueFromQuotes(traceLineSplit[1]);
255 string classID = ExtractValueFromQuotes(traceLineSplit[3]);
257 idToClass.Add(classID, name);
261 if (currentLine.StartsWith(
"<methodDef "))
263 traceLineSplit = currentLine.Split(
' ');
264 if (traceLineSplit[1].StartsWith(
"name=") ==
false)
265 throw new Exception();
266 if (traceLineSplit[2].StartsWith(
"signature=") ==
false)
267 throw new Exception();
268 if (traceLineSplit[3].StartsWith(
"startLineNumber=") ==
false)
269 throw new Exception();
270 if (traceLineSplit[4].StartsWith(
"endLineNumber=") ==
false)
271 throw new Exception();
272 if (traceLineSplit[5].StartsWith(
"methodId=") ==
false)
273 throw new Exception();
274 if (traceLineSplit[6].StartsWith(
"classIdRef=") ==
false)
275 throw new Exception();
277 string name = ExtractValueFromQuotes(traceLineSplit[1]);
278 string signature = ExtractValueFromQuotes(traceLineSplit[2]);
279 string methodID = ExtractValueFromQuotes(traceLineSplit[5]);
280 string classID = ExtractValueFromQuotes(traceLineSplit[6]);
284 if (name.IndexOf(
"$") > 0)
285 name = name.Substring(0, name.IndexOf(
"$"));
288 if (name.Equals(
"-clinit-"))
293 if (traceLineSplit[1].Equals(
"name=\"class$\""))
298 if (name.Equals(
"-init-"))
300 string fullClassName = idToClass[classID];
302 string className = fullClassName.Substring(fullClassName.LastIndexOf(
"/") + 1);
305 if (className.IndexOf(
"$") > 0)
308 className = className.Substring(className.LastIndexOf(
"$") + 1);
316 string fullMethodName = idToClass[classID] +
"." + name;
317 fullMethodName = fullMethodName.Replace(
'/',
'.');
318 fullMethodName = fullMethodName.Replace(
'#',
'.');
319 fullMethodName = fullMethodName.Replace(
'$',
'.');
321 idToMethod.Add(methodID, fullMethodName);
322 setOfUniqueMethodsTrace.Add(fullMethodName);
327 return setOfUniqueMethodsTrace;
331 private static string ExtractValueFromQuotes(
string entry)
333 int startPos = entry.IndexOf(
"\"") + 1;
334 int length = entry.LastIndexOf(
"\"") - startPos;
335 return entry.Substring(startPos, length);