-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathWhere.java
More file actions
375 lines (334 loc) · 11 KB
/
Copy pathWhere.java
File metadata and controls
375 lines (334 loc) · 11 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package com.datadog.debugger.probe;
import static java.util.Arrays.stream;
import com.datadog.debugger.agent.Generated;
import com.datadog.debugger.instrumentation.Types;
import com.datadog.debugger.util.ClassFileLines;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.MethodNode;
/** Stores probe location definition */
public class Where {
private static final String UNKNOWN_RETURN_TYPE = "$com.datadog.debugger.UNKNOWN$";
private static final Pattern JVM_CLASS_PATTERN = Pattern.compile("L([^;]+);");
private final String typeName;
private final String methodName;
private final String sourceFile;
private final String signature;
private final SourceLine[] lines;
// used as cache for matching signature
private String probeMethodDescriptor;
Where(
String typeName, String methodName, String signature, SourceLine[] lines, String sourceFile) {
this.typeName = typeName;
this.methodName = methodName;
this.sourceFile = sourceFile;
this.signature = signature;
this.lines = lines;
}
Where(String typeName, String methodName, String signature, String[] lines, String sourceFile) {
this(typeName, methodName, signature, sourceLines(lines), sourceFile);
}
public static Where of(String typeName, String methodName, String signature, String... lines) {
return new Where(typeName, methodName, signature, lines, null);
}
public static Where of(Method method) {
return of(
method.getDeclaringClass().getName(),
method.getName(),
stream(method.getParameterTypes())
.map(Class::getTypeName)
.collect(Collectors.joining(", ", "(", ")")));
}
protected static SourceLine[] sourceLines(String[] defs) {
if (defs == null || defs.length == 0) {
return null;
}
SourceLine[] lines = new SourceLine[defs.length];
for (int i = 0; i < lines.length; i++) {
lines[i] = SourceLine.fromString(defs[i]);
}
return lines;
}
public static Where convertLineToMethod(Where lineWhere, ClassFileLines classFileLines) {
if (lineWhere.methodName != null && lineWhere.lines != null) {
List<MethodNode> methodsByLine =
classFileLines.getMethodsByLine(lineWhere.lines[0].getFrom());
if (methodsByLine != null && !methodsByLine.isEmpty()) {
// pick the first method, as we can have multiple methods (lambdas) on the same line
MethodNode method = methodsByLine.get(0);
return new Where(
lineWhere.typeName,
method.name,
Types.descriptorToSignature(method.desc),
(SourceLine[]) null,
null);
}
}
return lineWhere;
}
public String getTypeName() {
return typeName;
}
public String getMethodName() {
return methodName;
}
public String getSourceFile() {
return sourceFile;
}
public String getSignature() {
return signature;
}
public String[] getLines() {
if (lines == null) {
return null;
}
String[] defs = new String[lines.length];
for (int i = 0; i < defs.length; i++) {
defs[i] = lines[i].toString();
}
return defs;
}
public SourceLine[] getSourceLines() {
return lines;
}
public boolean isTypeMatching(String targetType) {
return typeName == null || typeName.equals("*") || typeName.equals(targetType);
}
public boolean isMethodNameMatching(String targetMethod) {
return methodName == null || methodName.equals("*") || methodName.equals(targetMethod);
}
public boolean isMethodMatching(MethodNode methodNode, ClassFileLines classFileLines) {
String targetName = methodNode.name;
String targetMethodDescriptor = methodNode.desc;
// try exact matching: name + FQN signature
if (!isMethodNameMatching(targetName)
|| ((methodNode.access & Opcodes.ACC_BRIDGE) == Opcodes.ACC_BRIDGE)) {
return false;
}
if (signature == null) {
if (lines == null || lines.length == 0) {
return true;
}
// try matching by line
List<MethodNode> methodsByLine = classFileLines.getMethodsByLine(lines[0].getFrom());
if (methodsByLine == null || methodsByLine.isEmpty()) {
return false;
}
return methodsByLine.stream().anyMatch(m -> m == methodNode);
}
if (signature.equals("*") || signature.equals(targetMethodDescriptor)) {
return true;
}
// try full JVM signature: "(Ljava.lang.String;Ljava.util.Map;I)V"
if (probeMethodDescriptor == null) {
probeMethodDescriptor = signature.trim();
if (!probeMethodDescriptor.isEmpty()) {
if (isMissingReturnType(probeMethodDescriptor)) {
probeMethodDescriptor = UNKNOWN_RETURN_TYPE + " " + probeMethodDescriptor;
}
probeMethodDescriptor = Types.descriptorFromSignature(probeMethodDescriptor);
}
}
if (probeMethodDescriptor.isEmpty()) {
return true;
}
if (probeMethodDescriptor.equals(targetMethodDescriptor)) {
return true;
}
// fallback to signature without return type: "Ljava.lang.String;Ljava.util.Map;I"
String noRetTypeDescriptor = removeReturnType(probeMethodDescriptor);
targetMethodDescriptor = removeReturnType(targetMethodDescriptor);
if (noRetTypeDescriptor.equals(targetMethodDescriptor)) {
return true;
}
// Fallback to signature without Fully Qualified Name: "LString;LMap;I"
String simplifiedSignature = removeFQN(targetMethodDescriptor);
return noRetTypeDescriptor.equals(simplifiedSignature);
}
private static boolean isMissingReturnType(String probeMethodDescriptor) {
return probeMethodDescriptor.startsWith("(");
}
private static String removeFQN(String targetSignature) {
Matcher matcher = JVM_CLASS_PATTERN.matcher(targetSignature);
List<String> classes = new ArrayList<>();
while (matcher.find()) {
String className = matcher.group(1);
classes.add(className);
}
String result = targetSignature;
for (String className : classes) {
Pattern classNamePattern = Pattern.compile("L" + escapeClassName(className) + ";");
result =
classNamePattern
.matcher(result)
.replaceAll("L" + escapeClassName(simplify(className)) + ";");
}
return result;
}
private static String escapeClassName(String className) {
return className.replace("$", "\\$");
}
private static String simplify(String fqnClass) {
if (fqnClass == null) {
return null;
}
int index = fqnClass.lastIndexOf('.');
if (index == -1) {
index = fqnClass.lastIndexOf('/');
if (index == -1) {
return fqnClass;
}
}
return fqnClass.substring(index + 1);
}
private static String removeReturnType(String javaSignature) {
if (javaSignature == null) {
return null;
}
int leftParen = javaSignature.indexOf('(');
int rightParen = javaSignature.indexOf(')');
if (leftParen == -1 || rightParen == -1 || leftParen > rightParen) {
return javaSignature;
}
return javaSignature.substring(leftParen + 1, rightParen);
}
public boolean isSignatureMatching(String targetSignature) {
if (signature == null || signature.equals("*")) {
return true;
}
if (signature.equals(targetSignature)) {
return true;
}
return Types.descriptorFromSignature(signature).equals(targetSignature);
}
@Generated
@Override
public String toString() {
return "Where{"
+ "typeName='"
+ typeName
+ '\''
+ ", methodName='"
+ methodName
+ '\''
+ ", sourceFile='"
+ sourceFile
+ '\''
+ ", signature='"
+ signature
+ '\''
+ ", lines="
+ Arrays.toString(lines)
+ '}';
}
@Generated
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Where where = (Where) o;
return Objects.equals(typeName, where.typeName)
&& Objects.equals(methodName, where.methodName)
&& Objects.equals(sourceFile, where.sourceFile)
&& Objects.equals(signature, where.signature)
&& Arrays.equals(lines, where.lines);
}
@Generated
@Override
public int hashCode() {
int result = Objects.hash(typeName, methodName, sourceFile, signature);
result = 31 * result + Arrays.hashCode(lines);
return result;
}
public static class SourceLine {
private final int from, till;
public SourceLine(int line) {
this(line, line);
}
public SourceLine(int from, int till) {
this.from = from;
this.till = till;
}
public boolean isSingleLine() {
return from == till;
}
public int getFrom() {
return from;
}
public int getTill() {
return till;
}
@Override
public String toString() {
if (isSingleLine()) {
return String.valueOf(from);
} else {
return from + "-" + till;
}
}
public static SourceLine fromString(String lineDef) {
int delimIndex = lineDef.indexOf('-');
if (delimIndex == -1) {
int line = Integer.parseInt(lineDef);
return new SourceLine(line, line);
} else {
int lineFrom = Integer.parseInt(lineDef.substring(0, delimIndex));
int lineTill = Integer.parseInt(lineDef.substring(delimIndex + 1));
return new SourceLine(lineFrom, lineTill);
}
}
@Generated
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SourceLine that = (SourceLine) o;
return from == that.from && till == that.till;
}
@Generated
@Override
public int hashCode() {
return Objects.hash(from, till);
}
}
public static class SourceLineAdapter extends JsonAdapter<SourceLine[]> {
private static String[] STRING_ARRAY = new String[0];
@Override
public Where.SourceLine[] fromJson(JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonReader.Token.NULL) {
jsonReader.nextNull();
return null;
}
List<String> lines = new ArrayList<>();
jsonReader.beginArray();
while (jsonReader.hasNext()) {
lines.add(jsonReader.nextString());
}
jsonReader.endArray();
return sourceLines(lines.toArray(STRING_ARRAY));
}
@Override
public void toJson(JsonWriter jsonWriter, Where.SourceLine[] sourceLines) throws IOException {
if (sourceLines == null) {
jsonWriter.nullValue();
return;
}
jsonWriter.beginArray();
for (SourceLine sourceLine : sourceLines) {
jsonWriter.value(sourceLine.toString());
}
jsonWriter.endArray();
}
}
}