2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.vm;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
26 import gov.nasa.jpf.Config;
27 import gov.nasa.jpf.JPFConfigException;
28 import gov.nasa.jpf.JPFException;
31 * a JenkinsStateSet that stores program state information in a readable
32 * and diffable format.
34 * Storing states as readable text is enabled by setting vm.storage.class to this class
36 * Note: this automatically sets/overrides the serializer to Debug<serializer-class>
38 public class DebugJenkinsStateSet extends JenkinsStateSet {
40 static final String LOGFILE = "state";
45 public DebugJenkinsStateSet (Config conf){
46 String serializerCls = conf.getString("vm.serializer.class");
47 if (serializerCls != null){
48 int idx = serializerCls.lastIndexOf('.') + 1;
49 String cname = serializerCls.substring(idx);
51 if (!cname.startsWith("Debug")){
53 serializerCls = serializerCls.substring(0,idx) + "Debug" + cname;
55 serializerCls = "Debug" + cname;
59 serializer = conf.getInstance(null, serializerCls, DebugStateSerializer.class);
60 if (serializer == null){
61 throw new JPFConfigException("Debug StateSet cannot instantiate serializer: " + serializerCls);
65 String path = conf.getString("vm.serializer.output", "tmp");
66 outputDir = new File(path);
67 if (!outputDir.isDirectory()){
68 if (!outputDir.mkdirs()){
69 throw new JPFConfigException("Debug StateSet cannot create output dir: " + outputDir.getAbsolutePath());
73 outputFile = new File( outputDir, LOGFILE);
77 public void attach(VM vm){
78 // we use our own serializer
79 vm.setSerializer( serializer);
81 // <2do> this is a bit hack'ish - why does the VM keep the serializer anyways,
82 // if it is only used here
87 public int addCurrent () {
88 int maxId = lastStateId;
89 FileOutputStream fos = null;
92 fos = new FileOutputStream( outputFile);
93 } catch (FileNotFoundException fnfx){
94 throw new JPFException("cannot create Debug state set output file: " + outputFile.getAbsolutePath());
97 ((DebugStateSerializer)serializer).setOutputStream(fos);
99 int stateId = super.addCurrent();
104 } catch (IOException iox){
105 throw new JPFException("cannot write Debug state set output file: " + outputFile.getAbsolutePath());
108 // if this is a new state, store it under its id, otherwise throw it away
109 if (stateId > maxId){
110 String fname = "state." + stateId;
111 outputFile.renameTo( new File(outputDir, fname));
113 if (outputFile.isFile()){