1 package gov.nasa.jpf; 20 21 import gov.nasa.jpf.util.CoverageManager; 22 import gov.nasa.jpf.util.Debug; 23 import gov.nasa.jpf.util.LogManager; 24 25 import java.io.PrintWriter ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 30 34 public class JPF implements Runnable { 35 36 37 static String VERSION = "3.?"; 38 39 static Logger logger; 40 41 public Config config; 42 43 46 static JPF jpf; 47 48 51 public VM vm; 52 53 56 public Search search; 57 58 62 Path path; 63 64 65 69 public static Logger getLogger (String name) { 70 return LogManager.getLogger( name); 71 } 72 73 79 public JPF(Config conf) { 80 config = conf; 81 82 try { 83 vm = (VM) config.getEssentialInstance("vm.class", VM.class); 84 85 Class [] argTypes = { Config.class, VM.class }; 86 Object [] args = { config, vm }; 87 search = (Search) config.getEssentialInstance("search.class", Search.class, argTypes, args); 88 89 addCombinedListeners(); 90 } catch (Config.Exception cx) { 91 Debug.println(Debug.ERROR, cx); 92 } 93 } 94 95 public boolean isRunnable () { 96 return ((vm != null) && (search != null)); 97 } 98 99 public void addSearchListener (SearchListener l) { 100 if (search != null) { 101 search.addListener(l); 102 } 103 } 104 105 public void addVMListener (VMListener l) { 106 if (vm != null) { 107 vm.addListener(l); 108 } 109 } 110 111 public void addSearchProperty (Property p) { 112 if (search != null) { 113 search.addProperty(p); 114 } 115 } 116 117 void addCombinedListeners () throws Config.Exception { 119 Object [] listeners = config.getInstances("jpf.listener", JPFListener.class); 120 if (listeners != null) { 121 for (int i=0; i<listeners.length; i++) { 122 if (listeners[i] instanceof VMListener) { 123 if (vm != null) { 124 vm.addListener( (VMListener) listeners[i]); 125 } 126 } 127 if (listeners[i] instanceof SearchListener) { 128 if (search != null) { 129 search.addListener( (SearchListener) listeners[i]); 130 } 131 } 132 } 133 } 134 } 135 136 139 public Search getSearch() { 140 return search; 141 } 142 143 146 public VM getVM() { 147 return vm; 148 } 149 150 public static void exit() { 151 throw new ExitException(); 154 } 155 156 public boolean foundErrors() { 157 return !(search.getErrors().isEmpty()); 158 } 159 160 161 static boolean isHelpRequest (String [] args) { 162 if (args == null) return true; 163 if (args.length == 0) return true; 164 165 for (int i=0; i<args.length; i++) { 166 if ("-help".equals(args[i])) { 167 args[i] = null; 168 return true; 169 } 170 } 171 172 return false; 173 } 174 175 static boolean isPrintConfigRequest(String [] args) { 176 if (args == null) return false; 177 178 for (int i=0; i<args.length; i++) { 179 if ("-show".equals(args[i])) { 180 args[i] = null; 181 return true; 182 } 183 } 184 return false; 185 } 186 187 192 static void checkUnknownArgs (String [] args) { 193 for ( int i=0; i<args.length; i++) { 194 if (args[i] != null) { 195 if (args[i].charAt(0) == '-') { 196 logger.warning("unknown command line option: " + args[i]); 197 } 198 else { 199 break; 202 } 203 } 204 } 205 } 206 207 public static void printBanner (Config config) { 208 if (logger.isLoggable(Level.INFO)) { 209 logger.info("Java Pathfinder Model Checker v" + 210 config.getString("jpf.version", "3.1x") + 211 " - (C) 1999-2005 RIACS/NASA Ames Research Center"); 212 } 213 } 214 215 216 public static void main(String [] args) { 217 Config conf = createConfig(args); 218 LogManager.init(conf); 219 logger = getLogger("gov.nasa.jpf"); 220 221 printBanner(conf); 222 223 LogManager.printStatus(logger); 224 conf.printStatus(logger); 225 226 if (isHelpRequest(args)) { 227 showUsage(); 228 } 229 230 if (isPrintConfigRequest(args)) { 231 conf.print(new PrintWriter (System.out)); 232 } 233 234 if (conf.getTargetArg() != null) { 235 checkUnknownArgs(args); 236 237 JPF jpf = new JPF(conf); 238 jpf.run(); 239 } 240 } 241 242 247 static String getArg(String [] args, String pattern, String defValue, boolean consume) { 248 String s = defValue; 249 250 for (int i = 0; i < args.length; i++) { 251 String arg = args[i]; 252 253 if (arg != null) { 254 if (arg.matches(pattern)) { 255 int idx=arg.indexOf('='); 256 if (idx > 0) { 257 s = arg.substring(idx+1); 258 if (consume) { 259 args[i]=null; 260 } 261 } else if (i < args.length-1) { 262 s = args[i+1]; 263 if (consume) { 264 args[i] = null; 265 args[i+1] = null; 266 } 267 } 268 break; 269 } 270 } 271 } 272 return s; 273 } 274 275 278 static String getConfigFileName (String [] args) { 279 return getArg(args, "-c(onfig)?(=.+)?", "jpf.properties", true); 280 } 281 282 285 static String getRootDirName (String [] args) { 286 return getArg(args, "[+]jpf[.]basedir(=.+)?", null, false); } 288 289 294 public static Config createConfig (String [] args) { 295 String pf = getConfigFileName(args); 296 String rd = getRootDirName(args); 297 298 return new Config(args, pf, rd, JPF.class); 299 } 300 301 304 public void run() { 305 if (isRunnable()) { 306 try { 307 if (vm.initialize()) { 308 search.search(); 309 printResults(); 310 } 311 } catch (ExitException ex) { 312 Debug.println(Debug.WARNING, "JPF terminated"); 313 } catch (JPFException jx) { 314 Debug.println(Debug.ERROR, "JPF exception, terminating: " + jx.getMessage()); 315 if (config.getBoolean("jpf.print_exception_stack")) { 316 Debug.println(Debug.ERROR); 317 jx.printStackTrace(); 318 } 319 } catch (Throwable t) { 321 t.printStackTrace(); 323 } 324 } 325 } 326 327 public ErrorList getSearchErrors () { 328 if (search != null) { 329 return search.getErrors(); 330 } 331 332 return null; 333 } 334 335 static void showUsage() { 336 System.out 337 .println("Usage: \"java [<vm-option>..] gov.nasa.jpf.JPF [<jpf-option>..] [<app> [<app-arg>..]]"); 338 System.out.println(" <jpf-option> : -c <config-file> : name of config properties file (default \"jpf.properties\")"); 339 System.out.println(" | -help : print usage information"); 340 System.out.println(" | -show : print configuration dictionary contents"); 341 System.out.println(" | +<key>=<value> : add or override key/value pair to config dictionary"); 342 System.out.println(" <app> : application class or *.xml error trace file"); 343 System.out.println(" <app-arg> : arguments passed into main(String[]) if application class"); 344 } 345 346 352 private void printResults() { 353 ErrorList errors = search.getErrors(); 354 355 vm.printResults(new PrintWriter (System.out, true)); 357 358 Debug.println(Debug.ERROR); 359 Debug.println(Debug.ERROR); 360 Debug.println(Debug.ERROR, "==================================="); 361 362 int nerrors = errors.size(); 363 364 if (nerrors == 0) { 365 Debug.println(Debug.ERROR, " No Errors Found"); 366 } else if (nerrors == 1) { 367 Debug.println(Debug.ERROR, " 1 Error Found: " 368 + errors.getError(0).getMessage()); 369 } else { 370 Debug.println(Debug.ERROR, " " + nerrors + " Errors Found"); 371 } 372 373 Debug.println(Debug.ERROR, "==================================="); 374 Debug.println(Debug.ERROR); 375 376 379 CoverageManager.printResults(); 380 } 381 382 public static void handleException(JPFException e) { 383 Debug.println(Debug.ERROR, "jpf: " + e.getMessage()); 384 385 if (e instanceof JPFException) { 386 Debug.println(Debug.ERROR); 387 e.printStackTrace(); 388 } 389 390 exit(); 391 } 392 393 401 static class ExitException extends RuntimeException { 402 ExitException() { 403 } 404 405 ExitException(String msg) { 406 super(msg); 407 } 408 } 409 } | Popular Tags |