-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugifyingClassLoader.java
More file actions
225 lines (195 loc) · 7.65 KB
/
Copy pathDebugifyingClassLoader.java
File metadata and controls
225 lines (195 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* DebugifyingClassLoader.java
Copyright 2003, Bil Lewis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* DebugifyingClassLoader.java
*
*/
package com.lambda.Debugger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.jar.Manifest;
import org.apache.bcel.Repository;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
public class DebugifyingClassLoader extends java.lang.ClassLoader {
static private PrintStream out = System.out;
static private VectorD dontInstrument = new VectorD();
static {
dontInstrument.add("java.");
dontInstrument.add("sun.");
dontInstrument.add("apple.");
dontInstrument.add("com.apple.");
dontInstrument.add("com.sun.");
dontInstrument.add("jdk.");
dontInstrument.add("javax.");
dontInstrument.add("JAVAX.");
dontInstrument.add("org.apache.bcel");
dontInstrument.add("com.lambda.Debugger");
dontInstrument.add("edu.insa.LSD");
dontInstrument.add("lambda.Debugger");
dontInstrument.add("insa.LSD");
}
static private int depth = 0;
static private String[] spaces = { "", " ", " ", " ", " ",
" ", " ", " ", " ", " ",
" " };
private static void println(String s) {
System.out.println(s);
}
private static String spaces(int i) {
if (i < 0)
return spaces[0];
if (i < 10)
return spaces[i];
return spaces[10];
}
private static String spacesPlus() {
depth++;
return spaces(depth - 1);
}
private static String spacesMinus() {
depth--;
return spaces(depth);
}
private static String spacesExact() {
return spaces(depth - 1);
}
private boolean dontInstrument(String className) {
for (int i = 0; i < dontInstrument.size(); i++) {
if (className.startsWith((String) dontInstrument.elementAt(i)))
return true;
}
return false;
}
protected Class loadClass(String className, boolean resolve)
throws java.lang.ClassNotFoundException {
Class clazz;
boolean instrument = true;
clazz = findLoadedClass(className);
if (clazz != null)
return clazz;
if (shouldDelegateToParent(className)) {
clazz = getParent().loadClass(className);
if (Debugger.TRACE_LOADER)
println("loaded via parent: " + getParent() + " " + className);
return clazz;
}
if (Debugger.TRACE_LOADER)
println(spacesPlus() + "DebugifyingClassLoader loading: "
+ className);
try {
clazz = findClass(className, instrument);
} catch (VerifyError ve) {
println(spacesMinus() + "The ODB cannot instrument: " + className
+ ". Please report bug.\n" + ve);
if (Debugger.TRACE_LOADER)
ve.printStackTrace();
clazz = getParent().loadClass(className);
} catch (IllegalStateException ise) {
println(spacesMinus() + "The ODB cannot instrument: " + className
+ ". Please report bug.\n" + ise);
if (Debugger.TRACE_LOADER)
ise.printStackTrace();
clazz = getParent().loadClass(className);
}
if (clazz == null)
throw new java.lang.ClassNotFoundException(className); // return
// null;
if (resolve)
resolveClass(clazz);
return clazz;
}
private boolean shouldDelegateToParent(String className) {
if (className.startsWith("javax.xml."))
return false;
return dontInstrument(className) || (!Debugger.INSTRUMENT)
|| Defaults.instrumentOnlyExcludes(className, true);
}
public static byte[] debugify(String className, byte[] bytes) {
if (!Debugger.INSTRUMENT)
return bytes;
if (Debugger.TRACE_LOADER)
println("debugifying " + className);
ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes),
"<generated>");
JavaClass javaClass;
try {
javaClass = parser.parse();
} catch (IOException e) {
System.out.println("IMPOSSIBLE");
e.printStackTrace();
return bytes;
}
if (javaClass == null)
return null;
if (Debugger.TRACE_LOADER)
println(spacesExact() + "DebugifyingClassLoader debugifying: "
+ className);
if (Debugger.TRACE_LOADER_STACK)
(new Exception("Just used to get stack trace")).printStackTrace();
long start = System.currentTimeMillis();
javaClass = Debugify.debugifyClass(javaClass, className);
long end = System.currentTimeMillis();
Debugger.timeDebugifying += (end - start);
return InstrumentedClassFixer.toVerifiedClassBytes(javaClass);
}
protected Class findClass(String className, boolean instrument) throws ClassNotFoundException {
JavaClass javaClass = null;
try {
javaClass = Repository.lookupClass(className);
} catch (ClassNotFoundException e) {
if (Debugger.TRACE_LOADER) e.printStackTrace();
throw e;//new RuntimeException(e);
}
Class clazz;
if (javaClass == null)
return null;
if (!Debugger.USE_BOOTCLASSLOADER) {
if (Debugger.TRACE_LOADER)
println(spacesExact() + "DebugifyingClassLoader debugifying: "
+ className);
if (Debugger.TRACE_LOADER_STACK)
(new Exception("Just used to get stack trace"))
.printStackTrace();
long start = System.currentTimeMillis();
javaClass = Debugify.debugifyClass(javaClass, className);
long end = System.currentTimeMillis();
Debugger.timeDebugifying += (end - start);
}
byte[] b = InstrumentedClassFixer.toVerifiedClassBytes(javaClass);
int i = className.lastIndexOf('.');
if (i != -1) {
String pkgname = className.substring(0, i);
if (getPackage(pkgname) == null) {
definePackage(pkgname, null, null, null, null, null, null, null);
}
}
// int i = className.lastIndexOf('.');
// URL url = res.getCodeSourceURL();
// if (i != -1) {
// String pkgname = name.substring(0, i);
// // Check if package already loaded.
// Manifest man = res.getManifest();
// definePackageInternal(pkgname, man, url);
// }
clazz = defineClass(className, b, 0, b.length);
if (Debugger.TRACE_LOADER)
println(spacesMinus() + "DebugifyingClassLoader loaded: "
+ className);
return clazz;
}
}