KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > TrailInfo


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 import gov.nasa.jpf.*;
22 import gov.nasa.jpf.Path;
23 import gov.nasa.jpf.util.*;
24 import gov.nasa.jpf.util.Printable;
25
26 import java.io.PrintWriter JavaDoc;
27
28 /**
29  * concrete type to store execution paths. TrailInfo corresponds to Transition,
30  * i.e. all instructions executed in the context of a vm.forward() leading
31  * into a new state
32  */

33 public class TrailInfo implements Transition, Printable { // <2do> cloneable
34

35   private int thread;
36   private int random;
37   private Step first, last;
38   int nSteps;
39
40   private Object JavaDoc annotation;
41   String JavaDoc output;
42   
43   static boolean showSteps; // do we report steps?
44

45   static boolean init (Config config) {
46     showSteps = config.getBoolean("vm.report.show_steps");
47     return true;
48   }
49   
50   public TrailInfo (int t, int r) {
51     thread = t;
52     random = r;
53   }
54
55   public String JavaDoc getLabel () {
56     if (last != null) {
57       return last.getLineString();
58     } else {
59       return "?";
60     }
61   }
62
63   public void setOutput (String JavaDoc s) {
64     output = s;
65   }
66
67   public void setAnnotation (Object JavaDoc o) {
68     annotation = o;
69   }
70   
71   public Object JavaDoc getAnnotation () {
72     return annotation;
73   }
74   
75   public String JavaDoc getOutput () {
76     return output;
77   }
78   
79   public void setRandom (int r) {
80     random = r;
81   }
82
83   public int getRandom () {
84     return this.random;
85   }
86
87   public Step getStep (int index) {
88     Step s;
89     int i;
90     for (i=0, s=first; (s!= null) && (i<index); i++, s=s.next);
91     return s;
92   }
93
94   public Step getLastStep () {
95     return last;
96   }
97   
98   public int getStepCount () {
99     return nSteps;
100   }
101
102   public int getThread () {
103     return this.thread;
104   }
105
106   /**
107    * @see Transition
108    */

109   public TransitionStep getTransitionStep (int index) {
110     return getStep(index);
111   }
112
113   /**
114    * @see Transition
115    */

116   public TransitionStep getLastTransitionStep () {
117     return last;
118   }
119   
120
121   void addStep (Step step) {
122     if (first == null) {
123       first = step;
124       last = step;
125     } else {
126       last.next = step;
127       last = step;
128     }
129     nSteps++;
130   }
131     
132   public void printOn (PrintWriter JavaDoc pw) {
133     SourceRef sr = new SourceRef();
134
135     pw.print("Thread #");
136     pw.print(thread);
137
138     if (random != -1) {
139       pw.print(" Random #");
140       pw.print(random);
141     }
142     pw.println();
143
144     if (showSteps) {
145       for (Step s=first; s != null; s = s.next) {
146         s.printStepOn(pw, sr, null);
147       }
148     }
149   }
150
151   public void toXML (PrintWriter JavaDoc pw) {
152     if (random == -1) {
153       pw.println("\t<Step Thread=\"" + getThread() + "\">");
154     } else {
155       pw.println("\t<Step Thread=\"" + getThread() + "\" Random=\"" +
156                 getRandom() + "\">");
157     }
158
159     for (Step s=first; s!= null; s=s.next) {
160       s.toXML(pw);
161     }
162     
163     pw.println("\t</Step>");
164   }
165
166   public static void toXML (PrintWriter JavaDoc s, Path path) {
167     s.println("<?xml version=\"1.0\"?>");
168     s.println("<!DOCTYPE Trace [");
169
170     s.println("\t<!ELEMENT Trace (Step+)>");
171     s.println("\t<!ATTLIST Trace");
172     s.println("\t\tHandler CDATA #REQUIRED");
173     s.println("\t\tApplication CDATA #REQUIRED");
174     s.println("\t\tMessage CDATA #IMPLIED>");
175
176     s.println("\t<!ELEMENT Step (Instruction+,Comment*)>");
177     s.println("\t<!ATTLIST Step");
178     s.println("\t\tThread CDATA #REQUIRED ");
179     s.println("\t\tRandom CDATA \"-1\">");
180
181     s.println("\t<!ELEMENT Instruction (EMPTY)>");
182     s.println("\t<!ATTLIST Instruction");
183     s.println("\t\tFile CDATA #REQUIRED");
184     s.println("\t\tLine CDATA #REQUIRED>");
185
186     s.println("\t<!ELEMENT Comment (#PCDATA)>");
187     s.println("]>");
188     s.println();
189
190     s.print("<Trace ");
191     s.print(" Handler=\"gov.nasa.jpf.jvm.JVMXMLTraceHandler\"");
192     s.print(" Application=\"");
193     s.print(JVM.getVM().getMainClassName());
194     s.println("\">");
195
196     for (int i = 0; i < path.length(); i++) {
197       TrailInfo ti = (TrailInfo) path.get(i);
198       ti.toXML(s);
199     }
200
201     s.println("</Trace>");
202   }
203 }
204
Popular Tags