001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm;
020:
021: import gov.nasa.jpf.*;
022: import gov.nasa.jpf.jvm.bytecode.Instruction;
023: import gov.nasa.jpf.util.Left;
024:
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027: import gov.nasa.jpf.util.SourceRef;
028: import gov.nasa.jpf.util.Source;
029:
030: /**
031: * this corresponds to an executed instruction. Note that we can have a
032: * potentially huge number of Steps, hence we want to save objects here
033: * (e.g. Collection overhead)
034: */
035: public class Step extends SourceRef implements TransitionStep {
036:
037: Instruction insn;
038: String comment;
039: Step next;
040:
041: // report options - statics are suboptimal, but we don't have a good path
042: // to the VM here
043: static boolean showBytecode;
044: static boolean showMissingLines;
045:
046: public String description; // on demand (cache)
047:
048: public Step(String f, int l) {
049: super (f, l);
050: }
051:
052: public Step(String file, int line, Instruction insn) {
053: this (file, line);
054: this .insn = insn;
055: }
056:
057: static boolean init(Config config) {
058: showBytecode = config.getBoolean("vm.report.show_bytecode");
059: showMissingLines = config
060: .getBoolean("vm.report.show_missing_lines");
061: return true;
062: }
063:
064: public Step getNext() {
065: return next;
066: }
067:
068: public SourceRef getSourceRef() {
069: return this ;
070: }
071:
072: public String getDescription() {
073: if (description != null) {
074: StringWriter sw = new StringWriter();
075: PrintWriter pw = new PrintWriter(sw);
076:
077: printStepOn(pw, null, null);
078: description = sw.toString();
079: }
080:
081: return description;
082: }
083:
084: public void setComment(String s) {
085: comment = s;
086: }
087:
088: public String getComment() {
089: return comment;
090: }
091:
092: public void printStepOn(PrintWriter pw, SourceRef lastPrinted,
093: String prefix) {
094: Source source = Source.getSource(fileName);
095: boolean isLineMissing = source.isLineMissing(line);
096:
097: if (!this .equals(lastPrinted)) {
098: if (!isLineMissing) {
099: pw.print((prefix != null) ? prefix : " ");
100: pw.print(Left.format(fileName + ":" + line, 20));
101: pw.print(' ');
102: pw.println(source.getLine(line));
103: } else {
104: if (!fileName.equals(lastPrinted.getFileName())
105: && !showMissingLines) {
106: pw.print((prefix != null) ? prefix : " ");
107: pw.print("[no source for: ");
108: pw.print(fileName);
109: pw.println(']');
110: }
111: }
112:
113: lastPrinted.set(this );
114: }
115:
116: if (showBytecode || (showMissingLines && isLineMissing)) {
117: if (insn != null) {
118: MethodInfo mi = insn.getMethod();
119: pw.print('\t');
120: pw.print(Left.format(mi.getClassInfo().getName() + "."
121: + mi.getUniqueName() + "." + insn.getPosition()
122: + ":", 40));
123: pw.println(insn);
124: }
125: }
126:
127: if (comment != null) {
128: pw.print(" // ");
129: pw.println(comment);
130: }
131: }
132:
133: public void toXML(PrintWriter pw) {
134: pw.print("\t<Instruction File=\"");
135: pw.print(fileName);
136: pw.print("\" Line=\"");
137: pw.print(line);
138: pw.println("\"/>");
139:
140: if (comment != null) {
141: pw.print("\t<Comment>");
142: pw.print(comment);
143: pw.println("</Comment>");
144: }
145: }
146: }
|