OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [gnu/] [gcj/] [util/] [Debug.java] - Blame information for rev 772

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 756 jeremybenn
/*  Copyright (C) 2004  Free Software Foundation
2
 
3
This file is part of libgcj.
4
 
5
This software is copyrighted work licensed under the terms of the
6
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7
details.  */
8
 
9
/* Utility methods that allow an object to be converted to a textual
10
   representation on an OutputStream.  The intention here is that this
11
   class be used for debugging, so we provide information about all
12
   fields, public or otherwise. */
13
 
14
package gnu.gcj.util;
15
 
16
import java.lang.reflect.*;
17
import java.io.*;
18
import java.util.*;
19
 
20
class Debug
21
{
22
  private final PrintStream p;
23
  private final int maxdepth;
24
  private final int maxArrayLength;
25
  private final boolean printStaticFields;
26
  private int depth;
27
 
28
  Debug(PrintStream writer, int maxdepth, int maxArrayLength, boolean printStaticFields)
29
  {
30
    p = writer;
31
    this.maxdepth = maxdepth;
32
    this.maxArrayLength = maxArrayLength;
33
    this.printStaticFields = printStaticFields;
34
  }
35
 
36
  Debug(PrintStream writer)
37
  {
38
    this(writer, 0, 10, false);
39
  }
40
 
41
  Debug(int maxdepth, boolean printStaticFields)
42
  {
43
    this(new PrintStream
44
         (new FileOutputStream(FileDescriptor.err), true),
45
         maxdepth,
46
         maxdepth > 0 ? 1000 : 10, printStaticFields);
47
  }
48
 
49
  Debug(int maxdepth)
50
  {
51
    this(maxdepth, false);
52
  }
53
 
54
  Debug()
55
  {
56
    this(0, false);
57
  }
58
 
59
  private final void indent()
60
  {
61
    for (int i = 0; i < depth; i++)
62
      p.print("  ");
63
  }
64
 
65
  private final java.util.IdentityHashMap h =
66
  new java.util.IdentityHashMap();
67
 
68
  private static native Field[] getDeclaredFields(Class c);
69
  private static native Object getField(Object o, Field f);
70
  private static native long getAddr(Object o);
71
 
72
  // Return an array containing all the fields of a class and its
73
  // superclasses.
74
  private Field[] internalGetFields(Class c)
75
  {
76
    HashSet set = new HashSet();
77
    set.addAll(Arrays.asList(getDeclaredFields(c)));
78
    Class[] interfaces = c.getInterfaces();
79
    for (int i = 0; i < interfaces.length; i++)
80
      set.addAll(Arrays.asList(internalGetFields(interfaces[i])));
81
    Class superClass = c.getSuperclass();
82
    if (superClass != null)
83
      set.addAll(Arrays.asList(internalGetFields(superClass)));
84
    return (Field[])set.toArray(new Field[set.size()]);
85
  }
86
 
87
  // FIXME: We could just use getClass() here, but this is a
88
  // workaround for a C++ bug that is causing getClass() to be
89
  // miscompiled.
90
  static private Class getItsClass(Object O)
91
  {
92
    return O.getClass();
93
  }
94
 
95
  // Print a reasonably readable textual representation of an object
96
  // on our OutputStream.  Objects are only printed once, no matter
97
  // how many references point to them.
98
  private void print(Object O)
99
  {
100
    int savedDepth = depth;
101
    h.put(O, O);
102
    try
103
      {
104
        Class C = getItsClass(O);
105
        p.print(C.getName() + "@");
106
        p.println(Long.toHexString(getAddr(O)));
107
 
108
        if (C.isArray())
109
          {
110
            indent(); p.println("{");
111
            depth++;
112
            indent();
113
            C = C.getComponentType();
114
 
115
            int len = Array.getLength(O);
116
            for (int i = 0; i < len; i++)
117
              {
118
                Object thing = Array.get(O, i);
119
                print0(thing, C);
120
                p.print(", ");
121
                if (i > maxArrayLength)
122
                  {
123
                    p.print("...");
124
                    break;
125
                  }
126
              }
127
            depth--;
128
            p.println();
129
            indent(); p.print("}");
130
            return;
131
          }
132
 
133
        indent(); p.println("{");
134
        depth++;
135
        if (C == java.lang.Class.class)
136
          {
137
            indent();
138
            p.println ("class = " + O.toString() + ",");
139
          }
140
        else if (C == java.lang.reflect.Field.class)
141
          {
142
            indent();
143
            p.println ("<field> = \"" + O.toString() + "\",");
144
          }
145
        else if (C == java.lang.String.class)
146
          {
147
            indent();
148
            p.println ("<string> = \"" + O.toString() + "\",");
149
          }
150
        Field[] f = internalGetFields(C);
151
        for (int i = 0; i < f.length; i++)
152
          {
153
            Class type = f[i].getType();
154
            boolean isStatic = (f[i].getModifiers() & Modifier.STATIC) != 0;
155
 
156
            if (isStatic && ! printStaticFields)
157
              continue;
158
 
159
            indent();
160
            if (isStatic)
161
              p.print("static ");
162
            p.print(type.getName() +" " +f[i].getName() + " = ");
163
            Object thing = getField(O, f[i]);
164
            print0(thing, type);
165
            p.println(",");
166
          }
167
        depth--;
168
        indent(); p.print("}");
169
      }
170
    catch (Throwable t)
171
      {
172
        p.print("error: 0x" + Long.toHexString(getAddr(O)) + ";");
173
        depth = savedDepth;
174
      }
175
  }
176
 
177
  private void print0(Object thing, Class C)
178
  {
179
    try
180
      {
181
        if (thing == null)
182
          {
183
            p.print("null");
184
            return;
185
          }
186
        else if (C == gnu.gcj.RawData.class ||
187
                 C == gnu.gcj.RawDataManaged.class)
188
          {
189
          }
190
        else if (C.isPrimitive())
191
          {
192
            if (getItsClass(thing) == Character.class)
193
              p.print("'" + thing + "'");
194
            else
195
              p.print(thing);
196
            return;
197
          }
198
        else if (getItsClass(thing) == String.class)
199
          {
200
            p.print("\"" + thing + "\"");
201
            return;
202
          }
203
        else if (depth < maxdepth && h.get(thing) == null)
204
          {
205
            depth++;
206
            print(thing);
207
            depth--;
208
            return;
209
          }
210
      }
211
    catch (Throwable t)
212
      {
213
      }
214
 
215
    // The default action: just print the address.
216
    p.print("0x"+ Long.toHexString(getAddr(thing)));
217
  }
218
 
219
  // Print the textual representation of an object on System.err.
220
  public void write(Object O)
221
  {
222
    depth = 0;
223
    print(O);
224
    p.flush();
225
  }
226
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.