Fixing bugs and cleaning up: Continuing sub-graph executions when there is no matched...
[jpf-core.git] / src / main / gov / nasa / jpf / listener / CGMonitor.java
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 package gov.nasa.jpf.listener;
19
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.ListenerAdapter;
22 import gov.nasa.jpf.search.Search;
23 import gov.nasa.jpf.vm.ChoiceGenerator;
24 import gov.nasa.jpf.vm.VM;
25 import java.io.PrintStream;
26
27 /**
28  * listener to report out what CGs and choices are processed during the search.
29  * This is a simple tool to find out about the SUT state space
30  */
31 public class CGMonitor extends ListenerAdapter {
32   
33   protected PrintStream out;
34   
35   protected int depth;
36   
37   // display options
38   protected boolean showInsn = false;   // show the insn that caused the CG
39   protected boolean showChoice = false; // show the choice value (-> show each CG.advance())
40   protected boolean showDepth = true;   // show search depth at point of CG set/advance
41   
42   public CGMonitor (Config conf) {
43     showInsn = conf.getBoolean("cgm.show_insn", showInsn);
44     showChoice = conf.getBoolean("cgm.show_choice", showChoice);
45     showDepth = conf.getBoolean("cgm.show_depth", showDepth);
46     
47     out = System.out;
48   }
49   
50   @Override
51   public void stateAdvanced (Search search) {
52     depth++;
53   }
54   
55   @Override
56   public void stateBacktracked (Search search) {
57     depth--;
58   }
59   
60   @Override
61   public void stateRestored (Search search) {
62     depth = search.getDepth();    
63   }
64   
65   void printPrefix(char c) {
66     for (int i=0; i<depth; i++) {
67       System.out.print(c);
68     }
69   }
70   
71   void printCG (ChoiceGenerator<?> cg, boolean printChoice){
72     if (showDepth){
73       printPrefix('.');
74     }
75     
76     out.print(cg);
77
78     if (printChoice){
79       out.print(", ");
80       out.print(cg.getNextChoice());
81     }
82
83     if (showInsn){
84       out.print(", \"");
85       out.print(cg.getInsn());
86       out.print('\"');
87     }
88
89     out.println();    
90   }
91   
92   @Override
93   public void choiceGeneratorSet (VM vm, ChoiceGenerator<?> currentCG) {
94     if (!showChoice){
95       printCG( vm.getChoiceGenerator(), false);
96     }
97   }
98   
99   @Override
100   public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG) {
101     if (showChoice){
102       printCG( vm.getChoiceGenerator(), true);      
103     }
104   }
105
106 }