00001 package gov.nasa.arc.ase.jpf.jvm;
00002
00003 import gov.nasa.arc.ase.jpf.Engine;
00004 import gov.nasa.arc.ase.jpf.InternalErrorException;
00005 import gov.nasa.arc.ase.jpf.iThreadInfo;
00006 import gov.nasa.arc.ase.util.Debug;
00007 import gov.nasa.arc.ase.util.HashData;
00008 import java.io.*;
00009
00010
00011
00012
00013
00014 import java.util.*;
00015
00016
00017
00018
00019
00020
00021
00022
00023 public class KernelState implements Storable {
00024
00025
00026
00027 public StaticArea sa;
00028
00029
00030
00031
00032 public DynamicArea da;
00033
00034
00035
00036
00037 public ThreadList tl;
00038
00039
00040
00041
00042 public SystemState ss;
00043
00044
00045
00046
00047 public int atomicLevel;
00048
00049
00050
00051
00052 private boolean intermediateStep;
00053
00054
00055
00056
00057 private int intermediateThread;
00058
00059
00060
00061
00062 public int[] data;
00063
00064 private KernelState(KernelState ks) {
00065 sa = (StaticArea)ks.sa.clone();
00066 sa.ks = this;
00067 da = (DynamicArea)ks.da.clone();
00068 da.ks = this;
00069 tl = (ThreadList)ks.tl.clone();
00070 tl.ks = this;
00071 ss = null;
00072 atomicLevel = ks.atomicLevel;
00073 intermediateThread = ks.intermediateThread;
00074 data = ks.data;
00075 }
00076
00077
00078
00079 public KernelState(SystemState system_state) {
00080 sa = new StaticArea(this);
00081 da = new DynamicArea(this);
00082 tl = new ThreadList(this);
00083 ss = system_state;
00084 atomicLevel = 0;
00085 intermediateStep = false;
00086 intermediateThread = -1;
00087 data = null;
00088 }
00089 public void backtrackTo(ArrayOffset storing, Object backtrack) {
00090 sa.backtrackTo(storing, ((Object[])backtrack)[0]);
00091 da.backtrackTo(storing, ((Object[])backtrack)[1]);
00092 tl.backtrackTo(storing, ((Object[])backtrack)[2]);
00093 atomicLevel = ((Integer)((Object[])backtrack)[3]).intValue();
00094 intermediateStep = ((Boolean)((Object[])backtrack)[4]).booleanValue();
00095 intermediateThread = ((Integer)((Object[])backtrack)[5]).intValue();
00096
00097 data = storing.data;
00098 }
00099 public void clearAtomic() {
00100 atomicLevel--;
00101 }
00102 public void clearIntermediate() {
00103 intermediateStep = false;
00104 intermediateThread = -1;
00105 }
00106 public Object clone() {
00107 return new KernelState(this);
00108 }
00109
00110 public void gc() {
00111 Engine.getJPF().status.GCRuns++;
00112 da.gc();
00113 }
00114 public Object getBacktrackData() {
00115 Object[] data = new Object[6];
00116
00117 data[0] = sa.getBacktrackData();
00118 data[1] = da.getBacktrackData();
00119 data[2] = tl.getBacktrackData();
00120 data[3] = new Integer(atomicLevel);
00121 data[4] = new Boolean(intermediateStep);
00122 data[5] = new Integer(intermediateThread);
00123
00124 return data;
00125 }
00126 public int getIntermediateThread() {
00127 return intermediateThread;
00128 }
00129 public int[] getStoringData() {
00130 if(data != null) return data;
00131
00132 int[] saData = sa.getStoringData();
00133 int[] daData = da.getStoringData();
00134 int[] tlData = tl.getStoringData();
00135
00136 int sal = saData.length;
00137 int dal = daData.length;
00138 int tll = tlData.length;
00139
00140 data = new int[sal + dal + tll];
00141
00142 System.arraycopy(saData, 0, data, 0, sal);
00143 System.arraycopy(daData, 0, data, sal, dal);
00144 System.arraycopy(tlData, 0, data, sal + dal, tll);
00145
00146 return data;
00147 }
00148 public int getThreadCount() {
00149 return tl.length();
00150 }
00151 public iThreadInfo getThreadInfo(int index) {
00152 return tl.get(index);
00153 }
00154 public void hash(HashData hd) {
00155 da.hash(hd);
00156 sa.hash(hd);
00157
00158 for(int i = 0, l = tl.length(); i < l; i++)
00159 tl.get(i).hash(hd);
00160 }
00161
00162
00163
00164 private static int hash_data(int[] data) {
00165 HashData hd = new HashData();
00166
00167 for (int i = 0, l = data.length; i < l; i++)
00168 hd.add(data[i]);
00169
00170 return hd.getValue();
00171 }
00172
00173
00174
00175
00176
00177 private static int hash_reverse_data(int[] data) {
00178 HashData hd = new HashData();
00179
00180 for (int i = data.length-1; i >= 0; i--)
00181 hd.add(data[i]);
00182
00183 return hd.getValue();
00184 }
00185 public int hashCode() {
00186 HashData hd = new HashData();
00187
00188 hash(hd);
00189
00190 return hd.getValue();
00191 }
00192 public boolean isIntermediate() {
00193 return intermediateStep;
00194 }
00195
00196
00197
00198 public boolean isTerminated() {
00199 return !tl.anyAliveThread();
00200 }
00201 public void jvmError(Exception e, ThreadInfo th) {
00202 log();
00203 e.printStackTrace();
00204 if(th != null) th.printInternalErrorTrace(e);
00205 Engine.getJPF().vm.getErrorTrail().print();
00206 Engine.getJPF().vm.getErrorTrail_to_file();
00207 System.exit(1);
00208 }
00209
00210
00211 public void log() {
00212 da.log();
00213 sa.log();
00214 for(int i = 0; i < tl.length(); i++)
00215 tl.get(i).log();
00216 Debug.println(Debug.MESSAGE);
00217 }
00218
00219
00220
00221 public ThreadInfo newThread(int objref) {
00222 ThreadInfo th = new ThreadInfo(objref);
00223
00224 tl.add(tl.length(), th);
00225
00226
00227
00228 return th;
00229 }
00230 public void setAtomic() {
00231 atomicLevel++;
00232 }
00233 public void setIntermediate() {
00234 intermediateStep = true;
00235 intermediateThread = ss.sch.getThread();
00236 }
00237 }