00001 package edu.ksu.cis.bandera.birc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import edu.ksu.cis.bandera.bir.BirTrace;
00036 import edu.ksu.cis.bandera.bir.BirState;
00037 import edu.ksu.cis.bandera.bir.TransSystem;
00038 import edu.ksu.cis.bandera.bir.Transformation;
00039 import edu.ksu.cis.bandera.bir.TransVector;
00040 import edu.ksu.cis.bandera.bir.Action;
00041 import edu.ksu.cis.bandera.bir.ActionVector;
00042
00043 import ca.mcgill.sable.util.*;
00044 import ca.mcgill.sable.soot.*;
00045 import ca.mcgill.sable.soot.jimple.*;
00046
00047 import java.io.*;
00048 import java.util.*;
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 public class JimpleTrace {
00063
00064 boolean verified;
00065 boolean completed;
00066 boolean limitviolation;
00067 boolean outofmemory;
00068 boolean depthexceeded;
00069 boolean vectorexceeded;
00070 Stmt [] stmts;
00071 String trapName;
00072 Stmt trapStmt;
00073 TransSystem system;
00074 BirTrace birTrace;
00075 Hashtable threadOfStmt = new Hashtable();
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 int [] stateIndex;
00099
00100
00101
00102
00103
00104
00105 public JimpleTrace(BirTrace birTrace) {
00106 this.birTrace = birTrace;
00107 Transformation trapTrans = birTrace.getTrapTrans();
00108 Action trapAction = birTrace.getTrapAction();
00109 system = birTrace.getTransSystem();
00110 verified = birTrace.isVerified();
00111 completed = birTrace.isComplete();
00112 limitviolation = birTrace.isLimitViolation();
00113 outofmemory = birTrace.isOutOfMemory();
00114 depthexceeded = birTrace.isDepthExceeded();
00115 vectorexceeded = birTrace.isVectorExceeded();
00116
00117 if (limitviolation) trapName = birTrace.getTrapName();
00118
00119
00120
00121
00122 if (completed && !limitviolation && !verified && !outofmemory && !depthexceeded && !vectorexceeded) {
00123 TransVector transVector = birTrace.getTransVector();
00124 Vector steps = new Vector(transVector.size());
00125 stateIndex = new int[birTrace.getNumActions() + 1];
00126 stateIndex[0] = 0;
00127 int actionCount = 0;
00128
00129 for (int i = 0; i < transVector.size(); i++) {
00130 Transformation trans = transVector.elementAt(i);
00131 ActionVector actions = trans.getActions();
00132 String threadName = trans.getFromLoc().getThread().getName();
00133
00134 for(int j = 0; j < actions.size(); j++) {
00135 Action action = actions.elementAt(j);
00136 actionCount++;
00137
00138
00139 Object source = system.getSource(action);
00140 if (source != null) {
00141 steps.addElement(source);
00142 threadOfStmt.put(source,threadName);
00143 stateIndex[steps.size()] = actionCount;
00144 }
00145 if (trapTrans == trans && trapAction == action)
00146 break;
00147 }
00148 }
00149
00150
00151 stmts = new Stmt[steps.size()];
00152 for(int i = 0; i < steps.size(); i++)
00153 stmts[i] = (Stmt) steps.elementAt(i);
00154
00155
00156 if (birTrace.getTrapName() != null) {
00157 trapName = birTrace.getTrapName();
00158 trapStmt = (Stmt) system.getSource(trapAction);
00159 String threadName =
00160 birTrace.getTrapTrans().getFromLoc().getThread().getName();
00161 threadOfStmt.put(trapStmt,threadName);
00162 }
00163 }
00164 }
00165
00166 String evalExpr(Expr expr, int stmtIndex) {
00167 ExprExtractor extractor =
00168 new ExprExtractor(system, null, null, null,
00169 null, new PredicateSet());
00170 expr.apply(extractor);
00171 edu.ksu.cis.bandera.bir.Expr birExpr =
00172 (edu.ksu.cis.bandera.bir.Expr) extractor.getResult();
00173 return birTrace.evalExpr(birExpr,stateIndex[stmtIndex]);
00174 }
00175 public JimpleStore getLastStore() {
00176 return getStore(stmts.length);
00177 }
00178
00179
00180
00181
00182
00183
00184 public Stmt [] getStatements() { return stmts; }
00185
00186
00187
00188
00189
00190
00191
00192
00193 public JimpleStore getStore(int stmtIndex) {
00194 BirState state = birTrace.getState(stateIndex[stmtIndex]);
00195 JimpleStore store = new JimpleStore(state);
00196 return store;
00197 }
00198 public TransSystem getTransSystem() { return system; }
00199
00200
00201
00202 public String getTrapName() { return trapName; }
00203
00204
00205
00206 public Stmt getTrapStmt() { return trapStmt; }
00207
00208
00209
00210
00211 public boolean isComplete() { return completed; }
00212
00213
00214
00215 public boolean isDepthExceeded() { return depthexceeded; }
00216
00217
00218
00219
00220 public boolean isLimitViolation() { return limitviolation; }
00221
00222
00223
00224 public boolean isOutOfMemory() { return outofmemory; }
00225
00226
00227
00228 public boolean isVectorExceeded() { return vectorexceeded; }
00229
00230
00231
00232
00233 public boolean isVerified() { return verified; }
00234
00235
00236
00237
00238 static String pad(Object o, int width) {
00239 String padding = " ";
00240 String item = o.toString();
00241 return item + padding.substring(0,width - item.length());
00242 }
00243 public void print() {
00244 print(new PrintWriter(System.out, true),false);
00245 }
00246
00247
00248
00249
00250
00251
00252 public void print(PrintWriter out, boolean showVars) {
00253 int threadNameSize = 0;
00254 Enumeration e = threadOfStmt.elements();
00255 while (e.hasMoreElements()) {
00256 int size = ((String)e.nextElement()).length();
00257 if (size > threadNameSize)
00258 threadNameSize = size;
00259 }
00260 if (verified)
00261 out.println("NO TRACE");
00262 else {
00263 out.println("TRACE OF VIOLATION");
00264 for (int i = 0; i < stmts.length; i++) {
00265 if (showVars)
00266 getStore(i).print();
00267 out.println(pad(threadOfStmt.get(stmts[i]),threadNameSize)
00268 + " " + stmts[i]);
00269 }
00270 if (showVars)
00271 getStore(stmts.length).print();
00272 if (trapName != null)
00273 out.println(pad(threadOfStmt.get(trapStmt),threadNameSize)
00274 + " " + trapName + " at " + trapStmt);
00275 }
00276 }
00277 }