1 package gov.nasa.jpf.jvm; 20 21 import gov.nasa.jpf.Config; 22 import gov.nasa.jpf.JPFException; 23 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 28 import java.util.BitSet ; 29 30 31 34 public class InteractiveScheduler extends Scheduler { 35 private static BufferedReader br = new BufferedReader ( 36 new InputStreamReader (System.in)); 37 private static boolean quit = false; 38 private int thread; 39 private int random; 40 private boolean lastRandom; 41 42 public InteractiveScheduler (Config config) { 43 initialize(); 44 } 45 46 public int getRandom () { 47 return random; 48 } 49 50 public int getThread () { 51 return thread; 52 } 53 54 public void initialize () { 55 thread = 0; 56 random = -1; 57 lastRandom = true; 58 } 59 60 public ThreadInfo locateThread (SystemState ss) { 61 if (quit) { 62 return null; 63 } 64 65 int nthreads = ss.getThreadCount(); 66 int nrunnable = 0; 67 BitSet runnable = new BitSet (); 68 69 for (int i = 0; i < nthreads; i++) { 70 ThreadInfo th = ss.getThreadInfo(i); 71 72 if (th.isRunnable()) { 73 runnable.set(i); 74 nrunnable++; 75 } 76 } 77 78 if (nrunnable == 0) { 79 System.out.println("Deadlock: backing up"); 80 81 return null; 82 } 83 84 System.out.print("Runnable threads [" + nrunnable + "]: "); 85 86 for (int i = 0; i < nthreads; i++) { 87 if (runnable.get(i)) { 88 if (i == thread) { 89 System.out.print("[" + i + "] "); 90 } else { 91 System.out.print(i + " "); 92 } 93 } 94 } 95 96 System.out.println(); 97 98 while (true) { 99 try { 100 System.out.print("> "); 101 102 String s = br.readLine(); 103 104 if (s.equals("quit") || s.equals("q")) { 105 quit = true; 106 107 return null; 108 } else if (s.equals("backtrack") || s.equals("back") || 109 s.equals("b")) { 110 return null; 111 } else if (s.equals("show") || s.equals("s")) { 112 while (true) { 113 System.out.print("show> "); 114 s = br.readLine(); 115 116 if (s.equals("")) { 117 break; 118 } 119 120 try { 121 int l = Integer.parseInt(s); 122 123 if (runnable.get(l)) { 124 ThreadInfo th = (ThreadInfo) ss.getThreadInfo(l); 125 System.out.println(th.getMethod().getCompleteName() + ":" + 126 th.getPC().getPosition() + " " + 127 th.getPC()); 128 129 break; 130 } 131 } catch (NumberFormatException e) { 132 } 133 } 134 } else if (s.equals("random") || s.equals("r")) { 135 while (true) { 136 System.out.println("Current random: " + random); 137 System.out.print("random> "); 138 s = br.readLine(); 139 140 if (s.equals("")) { 141 break; 142 } 143 144 try { 145 random = Integer.parseInt(s); 146 147 break; 148 } catch (NumberFormatException e) { 149 } 150 } 151 } else if (s.equals("help") || s.equals("h") || s.equals("?")) { 152 System.out.println("command:"); 153 System.out.println(" quit"); 154 System.out.println(" random"); 155 System.out.println(" backtrack"); 156 System.out.println(" help"); 157 } else { 158 if (runnable.get(thread) && s.equals("")) { 159 break; 160 } 161 162 try { 163 int i = Integer.parseInt(s); 164 165 if (runnable.get(i)) { 166 thread = i; 167 168 break; 169 } 170 } catch (NumberFormatException e) { 171 } 172 } 173 } catch (IOException e) { 174 throw new JPFException(e.getMessage()); 175 } 176 } 177 178 System.out.println("Running thread " + thread); 179 180 return ss.getThreadInfo(thread); 181 } 182 183 public void next () { 184 if (lastRandom) { 185 random = -1; 186 thread++; 187 } else { 188 random++; 189 } 190 } 191 192 public int random (int max) { 193 if (random == -1) { 194 random = 0; 195 } 196 197 lastRandom = (random == max - 1); 198 199 return random; 200 } 201 } | Popular Tags |