KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > tracing > Debugger


1 /*
2   Copyright (C) 2001 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.tracing;
19
20 import org.objectweb.jac.core.*;
21 import java.util.*;
22 import java.io.*;
23
24 /**
25  * This class is a simple debugger that can by used by a program to
26  * step the called methods and print some informations.
27  *
28  * <p>It is used by the debugging wrapper.
29  *
30  * @see DebuggingWrapper
31  * @see DebuggingWrapper#step(Interaction) */

32
33 public class Debugger {
34
35    /** Constant for stepping. */
36    public static final int STEP = 0;
37    /** Constant for stepping into */
38    public static final int STEP_INTO = 1;
39
40    /** Store the current debugging mode. */
41    public int mode = STEP;
42    /** Store if the debugger must step or not. */
43    public boolean stepping = true;
44    /** A stack that allows step into to stop. */
45    public transient Stack stepIntoStack;
46    
47    /** The debugger constructor. */
48    
49    public Debugger() {
50       stepIntoStack = new Stack();
51    }
52
53    /**
54     * Set the debugging mode of the debugger. Can be "step",
55     * "step_into", or "run".
56     *
57     * @param mode the new mode
58     *
59     * @see #getDebuggingMode() */

60
61    public void setDebuggingMode( int mode ) {
62       this.mode = mode;
63    }
64
65    /**
66     * The getter for the debugging mode.
67     *
68     * @return the current debugging mode
69     *
70     * @see #setDebuggingMode(int)
71     */

72
73    public int getDebuggingMode() {
74       return mode;
75    }
76
77    /**
78     * Disable stepping.
79     *
80     * <p>If this method is called, the debugger enters a run mode but
81     * is still active (a stepping mode can be recovered).
82     *
83     * @see #isStepping()
84     * @see #enableStepping() */

85    
86    public void disableStepping() {
87       stepping = false;
88    }
89
90    /**
91     * Enable stepping
92     *
93     * <p>If this method is called and that the stepping was disabled,
94     * the debugger enters a stepping mode.
95     *
96     * @see #isStepping()
97     * @see #disableStepping() */

98
99    
100    public void enableStepping() {
101       stepping = true;
102    }
103
104    /**
105     * Tell if in stepping mode.
106     *
107     * @return true if stepping
108     *
109     * @see #enableStepping()
110     * @see #disableStepping() */

111    
112    public boolean isStepping() {
113       return stepping;
114    }
115
116    /**
117     * Must be called when a new method is called.
118     *
119     * <p>If the debugger is in step mode, then, the program stops and
120     * the user is asked to press a key to continue.
121     *
122     * @param container the name of the container that runs the method
123     * @param objectName the name of the called object
124     * @param method the name of the called method
125     * @param args the arguments of the called method */

126
127    public void startOfMethod( String JavaDoc container,
128                               String JavaDoc objectName,
129                               String JavaDoc method,
130                               Object JavaDoc[] args ) {
131
132       if( isStepping() && getDebuggingMode() == STEP_INTO ) {
133          if( stepIntoStack.isEmpty() ) {
134             setDebuggingMode( STEP );
135          } else {
136             stepIntoStack.push( "" );
137          }
138       }
139
140       if( isStepping() && getDebuggingMode() == STEP &&
141           Collaboration.get().getAttribute( "step_into" ) == null ) {
142
143          System.out.println( "Debugging is calling " + method + " (on container " + container + "):" );
144          System.out.println( "wrappee = " + objectName );
145          System.out.println( "args = " + Arrays.asList( args ) );
146
147          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
148          boolean ok = false;
149          String JavaDoc answer = "";
150          
151          while (!ok) {
152             
153             System.out.println(" - [s]tep");
154             System.out.println(" - step [i]nto");
155             System.out.println(" - [r]un");
156             System.out.println(" - [q]uit");
157             System.out.print("> ");
158             
159             try {
160                answer = in.readLine();
161
162                if (answer.equals("q")) {
163                   System.out.println("Ciao!");
164                   System.exit(0);
165                } else if (answer.equals("i")) {
166                   stepIntoStack.push( "" );
167                   setDebuggingMode( STEP_INTO );
168                   ok = true;
169                } else if (answer.equals("s")) {
170                   setDebuggingMode( STEP );
171                   ok = true;
172                } else if (answer.equals("r")) {
173                   disableStepping();
174                   ok = true;
175                }
176                
177             } catch (Exception JavaDoc e) {
178                e.printStackTrace();
179             }
180             
181          }
182       }
183    }
184
185    /**
186     * This must be called at the end of a stepped method to print the
187     * execution informations of the method.
188     *
189     * @param container the name of the container that runs the method
190     * @param objectName the name of the called object
191     * @param method the name of the called method
192     * @param args the arguments of the called method
193     * @param ret the value returned by the called method
194     * @param executionTime the method call duration */

195
196    public void endOfMethod( String JavaDoc container,
197                             String JavaDoc objectName,
198                             String JavaDoc method,
199                             Object JavaDoc[] args,
200                             Object JavaDoc ret,
201                             long executionTime ) {
202
203       if( isStepping() && getDebuggingMode() == STEP_INTO ) {
204          if( stepIntoStack.isEmpty() ) {
205             setDebuggingMode( STEP );
206          } else {
207             stepIntoStack.pop();
208          }
209       }
210
211       if( isStepping() && getDebuggingMode() == STEP &&
212           Collaboration.get().getAttribute( "step_into" ) == null ) {
213
214          System.out.println( "Debugging is returning from " + method + " (on container " + container + "):" );
215          System.out.println( "wrappee = " + objectName );
216          System.out.println( "args =" + Arrays.asList( args ) );
217          System.out.println( "return =" + ret );
218          System.out.println( "duration = " + executionTime + " ms"
219          );
220       }
221    }
222 }
223
224
225    
226
227
228
229
230
231
Popular Tags