TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
JPDA.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Linq;
21 using System.Text;
22 using TraceLab.Components.Types.Preprocessors.ExecutionTraces;
23 
24 namespace TraceLab.Components.DevelopmentKit.Preprocessors.ExecutionTraces
25 {
29  public static class JPDA
30  {
36  public static BiGramCollection GenerateBiGrams(string filename)
37  {
38  return GenerateBiGrams(filename, null);
39  }
40 
48  public static BiGramCollection GenerateBiGrams(string filename, ISet<string> includeOnly)
49  {
50  TextReader traceFile = new StreamReader(filename);
51  BiGramCollection bigrams = new BiGramCollection();
52  Dictionary<string, List<string>> threadToMethodsStackTranslator = new Dictionary<string, List<string>>();
53  int lineNumber = 0;
54  string line;
55  while ((line = traceFile.ReadLine()) != null)
56  {
57  lineNumber++;
58 
59  if (line.Equals("-- VM Started --"))
60  continue;
61  if (line.Equals("m-- VM Started --"))
62  continue;
63  if (line.Equals("-- The application exited --"))
64  continue;
65  if (line.Length == 1)
66  continue;
67 
68  string[] traceLineSplit = line.Split('\t');
69 
70  if (traceLineSplit.Length >= 3)
71  {
72  throw new DevelopmentKitException("Error at line " + lineNumber + ": " + line);
73  }
74 
75  if (traceLineSplit[1][0] == '=')
76  {
77  // do nothing. This is a report line like ===== main =====
78  continue;
79  }
80 
81  String threadName = traceLineSplit[0].Substring(0, traceLineSplit[0].IndexOf(':'));
82 // if (threadName.equals("org.eclipse.jdt.internal.ui.text.JavaReconciler"))
83 // {
84 // continue;
85 // }
86 
87  if (!threadToMethodsStackTranslator.ContainsKey(threadName))
88  {
89  threadToMethodsStackTranslator.Add(threadName, new List<string>());
90  }
91 
92  int numberOfPipes = 0;
93  // count number of pipes "|"
94  for (int i = 0; i < traceLineSplit[0].Length; i++)
95  {
96  if (traceLineSplit[0][i] == '|')
97  {
98  numberOfPipes++;
99  }
100  }
101 
102  string[] buf = traceLineSplit[1].Split(new string[] {" -- "}, StringSplitOptions.None);
103  string methodName = buf[0];
104  string methodPath = buf[1];
105 
106  //leave method names like method14, but eliminate everything after a dollar (e.g., method$1)
107  int indexOfDollar = methodName.IndexOf('$');
108  if (indexOfDollar >= 0)
109  {
110 // System.out.print(methodName+"->");
111  methodName = methodName.Substring(0, indexOfDollar);
112 // System.out.println(methodName);
113  }
114 
115  indexOfDollar = methodPath.IndexOf('$');
116  if (indexOfDollar >= 0)
117  {
118 // System.out.print(methodPath+"->");
119  methodPath = methodPath.Replace('$','.');
120 // methodPath=methodPath.substring(0,indexOfDollar);
121 // System.out.println(methodPath);
122  }
123 
124  if (methodName.Equals("<init>"))
125  {
126 // System.out.print("<init>"+"->");
127  methodName=methodPath.Substring(methodPath.LastIndexOf(".") + 1);
128 // System.out.println(methodName);
129  }
130 
131  if (methodName.Equals("<clinit>"))
132  {
133 // System.out.print("<cinit>"+"->");
134  methodName=methodPath.Substring(methodPath.LastIndexOf(".") + 1);
135 // System.out.println(methodName);
136  }
137 
138  if (numberOfPipes == 0)
139  {
140  threadToMethodsStackTranslator[threadName].Clear();
141  string fullMethodName = methodPath + "." + methodName;
142  //string IDFullMethodName = inputOutput.getPositionOfMethodMappingInCorpus(fullMethodName);
143 
144  threadToMethodsStackTranslator[threadName].Insert(numberOfPipes, fullMethodName);
145  //if its 0, it couldn't have been called by anything
146  continue;
147  }
148 
149  while (threadToMethodsStackTranslator[threadName].Count > numberOfPipes)
150  {
151  threadToMethodsStackTranslator[threadName].RemoveAt(numberOfPipes);
152  }
153 
154 // if (threadName.equals("org.eclipse.jdt.internal.ui.text.JavaReconciler"))
155 // {
156 // //code to deal with the Reconciler thread which sometimes has methods that start with 20-30 pipes and the size of the stack traces is less than that
157 // //solution: add an unknownMethod
158 //
159 // if (currentThreadMethodsStack.size()<numberOfPipes)
160 // {
161 // int numberOfUnknownMethodsToAdd=numberOfPipes-currentThreadMethodsStack.size();
162 // for (int i=0;i<numberOfUnknownMethodsToAdd;i++)
163 // currentThreadMethodsStack.add("orgeclipsejdtinternaluitextjavareconciler#unknownMethod");
164 // }
165 // }
166 
167  if (threadToMethodsStackTranslator[threadName].Count < numberOfPipes)
168  {
169 // numberOfInconsistencies++;
170 // System.out.println(currentLine);
171  continue;
172  }
173  string fullMethodName2 = methodPath + "." + methodName;
174  //String IDFullMethodName=inputOutput.getPositionOfMethodMappingInCorpus(fullMethodName);
175  threadToMethodsStackTranslator[threadName].Insert(numberOfPipes, fullMethodName2);
176 
177  string parentMethod = threadToMethodsStackTranslator[threadName][numberOfPipes - 1];
178  string childMethod = threadToMethodsStackTranslator[threadName][numberOfPipes];
179  //if (parentMethod.id.equals("-1")||childMethod.id.equals("-1"))
180  // continue;
181 
182  if (includeOnly != null)
183  {
184  if (!includeOnly.Contains(parentMethod))
185  {
186  //Console.WriteLine(String.Format("includeOnly.Contains({0}): {1}", parentMethod, includeOnly.Contains(parentMethod)));
187  //Console.WriteLine("Ignoring parent: " + parentMethod);
188  continue;
189  }
190  if (!includeOnly.Contains(childMethod))
191  {
192  //Console.WriteLine("Ignoring child: " + childMethod);
193  continue;
194  }
195  }
196 
197  bigrams.Add(new BiGram(parentMethod, childMethod));
198  }
199 
200  traceFile.Close();
201  return bigrams;
202  }
203 
209  public static ISet<string> GenerateUniqueMethods(string filename)
210  {
211  TextReader traceFile = new StreamReader(filename);
212  HashSet<string> uniqueMethods = new HashSet<string>();
213  int lineNumber = 0;
214  string line;
215  while ((line = traceFile.ReadLine()) != null)
216  {
217  lineNumber++;
218 
219  if (line.Equals("-- VM Started --"))
220  continue;
221  if (line.Equals("m-- VM Started --"))
222  continue;
223  if (line.Equals("-- The application exited --"))
224  continue;
225  if (line.Length == 1)
226  continue;
227 
228  string[] traceLineSplit = line.Split('\t');
229 
230  if (traceLineSplit.Length >= 3)
231  {
232  throw new DevelopmentKitException("Error at line " + lineNumber + ": " + line);
233  }
234 
235  if (traceLineSplit[1][0] == '=')
236  {
237  // do nothing. This is a report line like ===== main =====
238  continue;
239  }
240 
241  string[] buf = traceLineSplit[1].Split(new string[] { " -- " }, StringSplitOptions.None);
242  string methodName = buf[0];
243  string methodPath = buf[1];
244 
245  //leave method names like method14, but eliminate everything after a dollar (e.g., method$1)
246  int indexOfDollar = methodName.IndexOf('$');
247  if (indexOfDollar >= 0)
248  {
249  // System.out.print(methodName+"->");
250  methodName = methodName.Substring(0, indexOfDollar);
251  // System.out.println(methodName);
252  }
253 
254  indexOfDollar = methodPath.IndexOf('$');
255  if (indexOfDollar >= 0)
256  {
257  // System.out.print(methodPath+"->");
258  methodPath = methodPath.Replace('$', '.');
259  // methodPath=methodPath.substring(0,indexOfDollar);
260  // System.out.println(methodPath);
261  }
262 
263  if (methodName.Equals("<init>"))
264  {
265  // System.out.print("<init>"+"->");
266  methodName = methodPath.Substring(methodPath.LastIndexOf(".") + 1);
267  // System.out.println(methodName);
268  }
269 
270  if (methodName.Equals("<clinit>"))
271  {
272  // System.out.print("<cinit>"+"->");
273  methodName = methodPath.Substring(methodPath.LastIndexOf(".") + 1);
274  // System.out.println(methodName);
275  }
276 
277  string fullMethodName = methodPath + "." + methodName;
278  uniqueMethods.Add(fullMethodName);
279  }
280 
281  traceFile.Close();
282  return uniqueMethods;
283  }
284  }
285 }