View Javadoc

1   // Debug.java, created Sat Feb 22 13:35:27 2003 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Runtime;
5   
6   import joeq.Main.jq;
7   import joeq.Memory.Address;
8   import joeq.UTF.Utf8;
9   import jwutil.util.Assert;
10  
11  /***
12   * @author  John Whaley <jwhaley@alum.mit.edu>
13   * @version $Id: Debug.java 2170 2005-02-17 22:17:09Z livshits $
14   */
15  public abstract class Debug {
16  
17      private static byte[] buffer = new byte[16];
18      private static int bufferIndex;
19  
20      private static void writeDecimalToBuffer(int i) {
21          boolean nonzero_found = false;
22          bufferIndex = -1;
23          if (i < 0) {
24              i = -i;
25              buffer[++bufferIndex] = (byte)'-';
26          }
27          for (int j=1000000000; j > 1; j /= 10) {
28              int k = i / j;
29              i = i % j;
30              if (nonzero_found || k != 0) {
31                  buffer[++bufferIndex] = (byte)(k + '0');
32                  nonzero_found = true;
33              }
34          }
35          buffer[++bufferIndex] = (byte)(i + '0');
36          buffer[++bufferIndex] = (byte)0;
37      }
38  
39      private static void writeHexToBuffer(int i) {
40          bufferIndex = -1;
41          buffer[++bufferIndex] = (byte)'0';
42          buffer[++bufferIndex] = (byte)'x';
43          for (int j=0; j < 8; ++j) {
44              int v = (i & 0xF0000000) >>> 28;
45              buffer[++bufferIndex] = (v < 0xa)?((byte)(v+'0')):((byte)(v+'a'-0xa));
46              i <<= 4;
47          }
48          buffer[++bufferIndex] = (byte)0;
49          Assert._assert(bufferIndex == 10);
50      }
51  
52      public static void write(String s) {
53          _delegate.write(s);
54      }
55      
56      public static void write(byte[] msg, int size) {
57          _delegate.write(msg, size);
58      }
59  
60      public static void write(Utf8 u) {
61          u.debugWrite();
62      }
63      
64      public static void write(char x) {
65          buffer[0] = (byte) x;
66          buffer[1] = (byte) 0;
67          bufferIndex = 1;
68          _delegate.write(buffer, bufferIndex);
69      }
70      
71      public static void write(int x) {
72          writeDecimalToBuffer(x);
73          _delegate.write(buffer, bufferIndex);
74      }
75  
76      public static void writeHex(int x) {
77          writeHexToBuffer(x);
78          _delegate.write(buffer, bufferIndex);
79      }
80      
81      public static void write(Address x) {
82          writeHex(x.to32BitValue());
83      }
84      
85      public static void write(int x, String s) {
86          write(x); write(s);
87      }
88  
89      public static void write(String s, int x) {
90          write(s); write(x);
91      }
92      
93      public static void write(String s, Address x) {
94          write(s); write(x);
95      }
96      
97      public static void write(String s1, int x, String s2) {
98          write(s1); write(x); write(s2);
99      }
100     
101     public static void write(int x1, String s, int x2) {
102         write(x1); write(s); write(x2);
103     }
104     
105     public static void writeln() {
106         writeln("");
107     }
108     
109     public static void writeln(String s) {
110         _delegate.writeln(s);
111     }
112     
113     public static void writeln(Utf8 u) {
114         u.debugWrite();
115         writeln();
116     }
117     
118     public static void writeln(int x) {
119         writeDecimalToBuffer(x);
120         _delegate.writeln(buffer, bufferIndex);
121     }
122     
123     public static void writelnHex(int x) {
124         writeHexToBuffer(x);
125         _delegate.writeln(buffer, bufferIndex);
126     }
127     
128     public static void writeln(Address x) {
129         writelnHex(x.to32BitValue());
130     }
131     
132     public static void writeln(int x, String s) {
133         write(x); writeln(s);
134     }
135     
136     public static void writeln(String s, int x) {
137         write(s); writeln(x);
138     }
139     
140     public static void writeln(String s, Address x) {
141         write(s); writeln(x);
142     }
143     
144     public static void writeln(String s1, int x, String s2) {
145         write(s1); write(x); writeln(s2);
146     }
147 
148     public static void die(int code) {
149         _delegate.die(code);
150     }
151 
152     static interface Delegate {
153         void write(byte[] msg, int size);
154         void write(String msg);
155         void writeln(byte[] msg, int size);
156         void writeln(String msg);
157         void die(int code);
158     }
159 
160     private static Delegate _delegate;
161     static {
162         /* Set up delegates. */
163         _delegate = null;
164         boolean nullVM = jq.nullVM;
165         if (!nullVM) {
166             _delegate = attemptDelegate("joeq.Runtime.DebugImpl");
167         }
168         if (_delegate == null) {
169             if(System.getProperty("silentdebug", "no").equals("yes")){
170                 _delegate = new joeq.Runtime.SilentDebugImpl();
171             }else{
172                 _delegate = new joeq.Runtime.BasicDebugImpl();
173             }
174         }
175     }
176 
177     private static Delegate attemptDelegate(String s) {
178         String type = "debug delegate";
179         try {
180             Class c = Class.forName(s);
181             return (Delegate)c.newInstance();
182         } catch (java.lang.ClassNotFoundException x) {
183             //System.err.println("Cannot find "+type+" "+s+": "+x);
184         } catch (java.lang.InstantiationException x) {
185             System.err.println("Cannot instantiate "+type+" "+s+": "+x);
186         } catch (java.lang.IllegalAccessException x) {
187             System.err.println("Cannot access "+type+" "+s+": "+x);
188         }
189         return null;
190     }
191 }