Mercurial > hg > Members > kono > jpf-core
comparison src/main/gov/nasa/jpf/vm/DebugJenkinsStateSet.java @ 0:61d41facf527
initial v8 import (history reset)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 23 Jan 2015 10:14:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61d41facf527 |
---|---|
1 /* | |
2 * Copyright (C) 2014, United States Government, as represented by the | |
3 * Administrator of the National Aeronautics and Space Administration. | |
4 * All rights reserved. | |
5 * | |
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 | |
9 * | |
10 * http://www.apache.org/licenses/LICENSE-2.0. | |
11 * | |
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. | |
17 */ | |
18 | |
19 package gov.nasa.jpf.vm; | |
20 | |
21 import java.io.File; | |
22 import java.io.FileNotFoundException; | |
23 import java.io.FileOutputStream; | |
24 import java.io.IOException; | |
25 | |
26 import gov.nasa.jpf.Config; | |
27 import gov.nasa.jpf.JPFConfigException; | |
28 import gov.nasa.jpf.JPFException; | |
29 | |
30 /** | |
31 * a JenkinsStateSet that stores program state information in a readable | |
32 * and diffable format. | |
33 * | |
34 * Storing states as readable text is enabled by setting vm.storage.class to this class | |
35 * | |
36 * Note: this automatically sets/overrides the serializer to Debug<serializer-class> | |
37 */ | |
38 public class DebugJenkinsStateSet extends JenkinsStateSet { | |
39 | |
40 static final String LOGFILE = "state"; | |
41 | |
42 File outputDir; | |
43 File outputFile; | |
44 | |
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); | |
50 | |
51 if (!cname.startsWith("Debug")){ | |
52 if (idx > 0){ | |
53 serializerCls = serializerCls.substring(0,idx) + "Debug" + cname; | |
54 } else { | |
55 serializerCls = "Debug" + cname; | |
56 } | |
57 } | |
58 | |
59 serializer = conf.getInstance(null, serializerCls, DebugStateSerializer.class); | |
60 if (serializer == null){ | |
61 throw new JPFConfigException("Debug StateSet cannot instantiate serializer: " + serializerCls); | |
62 } | |
63 } | |
64 | |
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()); | |
70 } | |
71 } | |
72 | |
73 outputFile = new File( outputDir, LOGFILE); | |
74 } | |
75 | |
76 @Override | |
77 public void attach(VM vm){ | |
78 // we use our own serializer | |
79 vm.setSerializer( serializer); | |
80 | |
81 // <2do> this is a bit hack'ish - why does the VM keep the serializer anyways, | |
82 // if it is only used here | |
83 super.attach(vm); | |
84 } | |
85 | |
86 @Override | |
87 public int addCurrent () { | |
88 int maxId = lastStateId; | |
89 FileOutputStream fos = null; | |
90 | |
91 try { | |
92 fos = new FileOutputStream( outputFile); | |
93 } catch (FileNotFoundException fnfx){ | |
94 throw new JPFException("cannot create Debug state set output file: " + outputFile.getAbsolutePath()); | |
95 } | |
96 | |
97 ((DebugStateSerializer)serializer).setOutputStream(fos); | |
98 | |
99 int stateId = super.addCurrent(); | |
100 | |
101 try { | |
102 fos.flush(); | |
103 fos.close(); | |
104 } catch (IOException iox){ | |
105 throw new JPFException("cannot write Debug state set output file: " + outputFile.getAbsolutePath()); | |
106 } | |
107 | |
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)); | |
112 } else { | |
113 if (outputFile.isFile()){ | |
114 outputFile.delete(); | |
115 } | |
116 } | |
117 | |
118 return stateId; | |
119 } | |
120 } |