Skip to content

Commit 1cf2d34

Browse files
committed
WIP
1 parent f89c828 commit 1cf2d34

File tree

8 files changed

+62
-129
lines changed

8 files changed

+62
-129
lines changed

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/agent/DebuggerTransformer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public class DebuggerTransformer implements ClassFileTransformer {
8787
SpanProbe.class);
8888
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
8989

90+
public static Path DUMP_PATH = Paths.get(System.getProperty(JAVA_IO_TMPDIR), "debugger");
91+
9092
private final Config config;
9193
private final TransformerDefinitionMatcher definitionMatcher;
9294
private final AllowListHelper allowListHelper;
@@ -517,7 +519,6 @@ private boolean performInstrumentation(
517519
private void handleInstrumentationResult(
518520
List<ProbeDefinition> definitions, InstrumentationResult result) {
519521
for (ProbeDefinition definition : definitions) {
520-
definition.buildLocation(result);
521522
if (listener != null) {
522523
listener.instrumentationResult(definition, result);
523524
}
@@ -576,9 +577,18 @@ private void notifyBlockedDefinitions(
576577

577578
private InstrumentationResult applyInstrumentation(
578579
MethodInfo methodInfo, List<ProbeDefinition> definitions) {
580+
System.out.println(
581+
"****** DebuggerTransformer.applyInstrumentation "
582+
+ "methodInfo = "
583+
+ methodInfo
584+
+ ", definitions = "
585+
+ definitions);
579586
Map<ProbeId, List<DiagnosticMessage>> diagnostics = new HashMap<>();
580587
definitions.forEach(
581-
probeDefinition -> diagnostics.put(probeDefinition.getProbeId(), new ArrayList<>()));
588+
probeDefinition -> {
589+
probeDefinition.buildLocation(methodInfo);
590+
diagnostics.put(probeDefinition.getProbeId(), new ArrayList<>());
591+
});
582592
InstrumentationResult.Status status = preCheckInstrumentation(diagnostics, methodInfo);
583593
if (status != InstrumentationResult.Status.ERROR) {
584594
try {
@@ -842,8 +852,7 @@ private void dumpOriginalClassFile(String className, byte[] classfileBuffer) {
842852

843853
private static Path dumpClassFile(String className, byte[] classfileBuffer) {
844854
try {
845-
Path classFilePath =
846-
Paths.get(System.getProperty(JAVA_IO_TMPDIR), "debugger", className + ".class");
855+
Path classFilePath = Paths.get(DUMP_PATH.toString(), className + ".class");
847856
Files.createDirectories(classFilePath.getParent());
848857
Files.write(classFilePath, classfileBuffer, StandardOpenOption.CREATE);
849858
return classFilePath;

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/instrumentation/InstrumentationResult.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.HashMap;
88
import java.util.List;
99
import java.util.Map;
10-
import org.objectweb.asm.tree.MethodNode;
1110

1211
/** Stores status instrumentation results */
1312
public class InstrumentationResult {
@@ -66,12 +65,11 @@ public InstrumentationResult(
6665
Status status, Map<ProbeId, List<DiagnosticMessage>> diagnostics, MethodInfo methodInfo) {
6766
this.status = status;
6867
this.diagnostics = diagnostics;
69-
this.sourceFileName = methodInfo.getClassNode().sourceFile;
70-
this.typeName = methodInfo.getClassNode().name.replace('/', '.');
71-
MethodNode methodNode = methodInfo.getMethodNode();
72-
this.methodName = methodNode.name;
68+
this.sourceFileName = methodInfo.getSourceFileName();
69+
this.typeName = methodInfo.getTypeName();
70+
this.methodName = methodInfo.getMethodName();
7371
this.methodStart = methodInfo.getMethodStart();
74-
this.signature = methodNode.desc != null ? Types.descriptorToSignature(methodNode.desc) : null;
72+
this.signature = methodInfo.getSignature();
7573
}
7674

7775
public boolean isError() {

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/instrumentation/MethodInfo.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public ClassNode getClassNode() {
3030
return classNode;
3131
}
3232

33+
public String getMethodName() {
34+
return methodNode.name;
35+
}
36+
3337
public MethodNode getMethodNode() {
3438
return methodNode;
3539
}
@@ -41,4 +45,16 @@ public ClassFileLines getClassFileLines() {
4145
public int getMethodStart() {
4246
return classFileLines != null ? classFileLines.getMethodStart(methodNode) : -1;
4347
}
48+
49+
public String getSignature() {
50+
return methodNode.desc != null ? Types.descriptorToSignature(methodNode.desc) : null;
51+
}
52+
53+
public String getSourceFileName() {
54+
return classNode.sourceFile;
55+
}
56+
57+
public String getTypeName() {
58+
return classNode.name.replace('/', '.');
59+
}
4460
}

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/CodeOriginProbe.java

Lines changed: 13 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import com.datadog.debugger.instrumentation.BasicProbeInstrumentor;
1010
import com.datadog.debugger.instrumentation.DiagnosticMessage;
11-
import com.datadog.debugger.instrumentation.InstrumentationResult;
1211
import com.datadog.debugger.instrumentation.InstrumentationResult.Status;
1312
import com.datadog.debugger.instrumentation.MethodInfo;
1413
import datadog.trace.bootstrap.debugger.CapturedContext;
@@ -40,114 +39,53 @@ public Status instrument(
4039
return new BasicProbeInstrumentor(this, methodInfo, diagnostics, probeIds).instrument();
4140
}
4241

43-
@Override
44-
public boolean isLineProbe() {
45-
// these are always method probes even if there is a line number
46-
return false;
47-
}
48-
49-
@Override
50-
public void evaluate(
51-
CapturedContext context, CapturedContext.Status status, MethodLocation methodLocation) {
52-
if (!MethodLocation.isSame(methodLocation, getEvaluateAt())) {
53-
return;
54-
}
55-
56-
super.evaluate(context, status, methodLocation);
57-
AgentSpan span = findSpan(AgentTracer.activeSpan());
58-
59-
if (span == null) {
60-
LOGGER.debug("Could not find the span for probeId {}", id);
61-
return;
62-
}
63-
applyCodeOriginTags(span);
64-
}
65-
6642
@Override
6743
public void commit(
6844
CapturedContext entryContext,
6945
CapturedContext exitContext,
7046
List<CapturedThrowable> caughtExceptions) {
71-
recordCodeOrigin();
72-
}
73-
74-
private void recordCodeOrigin() {
7547
AgentSpan span = AgentTracer.activeSpan();
7648
if (span == null) {
7749
LOGGER.debug("Could not find the span for probeId {}", id);
7850
return;
7951
}
80-
DebuggerSink sink = DebuggerAgent.getSink();
81-
if (isDebuggerEnabled(span) && sink != null) {
82-
Snapshot snapshot = createSnapshot();
83-
if (fillSnapshot(entryContext, exitContext, caughtExceptions, snapshot)) {
84-
String snapshotId = snapshot.getId();
85-
LOGGER.debug("committing code origin probe id={}, snapshot id={}", id, snapshotId);
86-
commitSnapshot(snapshot, sink);
87-
88-
List<AgentSpan> agentSpans =
89-
entrySpanProbe ? asList(span, span.getLocalRootSpan()) : singletonList(span);
90-
for (AgentSpan agentSpan : agentSpans) {
91-
if (agentSpan.getTag(format(DD_CODE_ORIGIN_FRAME, 0, "snapshot_id")) == null) {
92-
agentSpan.setTag(format(DD_CODE_ORIGIN_FRAME, 0, "snapshot_id"), snapshotId);
93-
}
94-
}
95-
}
96-
}
97-
if (sink != null) {
98-
sink.getProbeStatusSink().addEmitting(probeId);
99-
}
100-
span.getLocalRootSpan().setTag(getId(), (String) null); // clear possible span reference
101-
}
102-
103-
private List<AgentSpan> applyCodeOriginTags(AgentSpan span) {
104-
List<StackTraceElement> entries =
105-
stackTraceElements != null ? stackTraceElements : getUserStackFrames();
10652
List<AgentSpan> agentSpans =
10753
entrySpanProbe ? asList(span, span.getLocalRootSpan()) : singletonList(span);
10854

10955
for (AgentSpan s : agentSpans) {
11056
if (s.getTag(DD_CODE_ORIGIN_TYPE) == null) {
11157
s.setTag(DD_CODE_ORIGIN_TYPE, entrySpanProbe ? "entry" : "exit");
112-
113-
for (int i = 0; i < entries.size(); i++) {
114-
StackTraceElement info = entries.get(i);
115-
String fileName = info.getFileName();
116-
s.setTag(format(DD_CODE_ORIGIN_FRAME, i, "file"), fileName);
117-
s.setTag(format(DD_CODE_ORIGIN_FRAME, i, "method"), info.getMethodName());
118-
s.setTag(format(DD_CODE_ORIGIN_FRAME, i, "line"), info.getLineNumber());
119-
s.setTag(format(DD_CODE_ORIGIN_FRAME, i, "type"), info.getClassName());
120-
if (i == 0 && entrySpanProbe) {
121-
s.setTag(format(DD_CODE_ORIGIN_FRAME, i, "signature"), where.getSignature());
122-
}
123-
}
58+
s.setTag(format(DD_CODE_ORIGIN_FRAME, 0, "file"), location.getFile());
59+
s.setTag(format(DD_CODE_ORIGIN_FRAME, 0, "method"), location.getMethod());
60+
s.setTag(format(DD_CODE_ORIGIN_FRAME, 0, "line"), location.getLines().get(0));
61+
s.setTag(format(DD_CODE_ORIGIN_FRAME, 0, "type"), location.getType());
62+
s.setTag(format(DD_CODE_ORIGIN_FRAME, 0, "signature"), signature);
12463
}
12564
}
126-
return agentSpans;
12765
}
12866

12967
public boolean entrySpanProbe() {
13068
return entrySpanProbe;
13169
}
13270

13371
@Override
134-
public void buildLocation(InstrumentationResult result) {
72+
public void buildLocation(MethodInfo methodInfo) {
13573
String type = where.getTypeName();
13674
String method = where.getMethodName();
13775
List<String> lines = null;
13876

13977
String file = where.getSourceFile();
14078

141-
if (result != null) {
142-
type = result.getTypeName();
143-
method = result.getMethodName();
144-
if (result.getMethodStart() != -1) {
145-
lines = singletonList(String.valueOf(result.getMethodStart()));
79+
if (methodInfo != null) {
80+
type = methodInfo.getTypeName();
81+
method = methodInfo.getMethodName();
82+
if (methodInfo.getMethodStart() != -1) {
83+
lines = singletonList(String.valueOf(methodInfo.getMethodStart()));
14684
}
14785
if (file == null) {
148-
file = result.getSourceFileName();
86+
file = methodInfo.getSourceFileName();
14987
}
150-
signature = result.getMethodSignature();
88+
signature = methodInfo.getSignature();
15189
}
15290
this.location = new ProbeLocation(type, method, file, lines);
15391
}

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/ExceptionProbe.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.datadog.debugger.el.ProbeCondition;
88
import com.datadog.debugger.exception.ExceptionProbeManager;
99
import com.datadog.debugger.exception.Fingerprinter;
10-
import com.datadog.debugger.instrumentation.InstrumentationResult;
10+
import com.datadog.debugger.instrumentation.MethodInfo;
1111
import com.datadog.debugger.sink.Snapshot;
1212
import datadog.trace.bootstrap.debugger.CapturedContext;
1313
import datadog.trace.bootstrap.debugger.MethodLocation;
@@ -136,12 +136,12 @@ private void clearExceptionRefs(Snapshot snapshot) {
136136
}
137137

138138
@Override
139-
public void buildLocation(InstrumentationResult result) {
139+
public void buildLocation(MethodInfo methodInfo) {
140140
String type = where.getTypeName();
141141
String method = where.getMethodName();
142-
if (result != null) {
143-
type = result.getTypeName();
144-
method = result.getMethodName();
142+
if (methodInfo != null) {
143+
type = methodInfo.getTypeName();
144+
method = methodInfo.getMethodName();
145145
}
146146
// drop line number for exception probe
147147
this.location = new ProbeLocation(type, method, where.getSourceFile(), emptyList());

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/ProbeDefinition.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ public MethodLocation getEvaluateAt() {
104104
return evaluateAt;
105105
}
106106

107-
public void buildLocation(InstrumentationResult result) {
107+
public void buildLocation(MethodInfo methodInfo) {
108108
String type = where.getTypeName();
109109
String method = where.getMethodName();
110-
if (result != null) {
111-
type = result.getTypeName();
112-
method = result.getMethodName();
110+
if (methodInfo != null) {
111+
type = methodInfo.getTypeName();
112+
method = methodInfo.getMethodName();
113113
}
114114
List<String> lines = where.getLines() != null ? Arrays.asList(where.getLines()) : null;
115115
this.location = new ProbeLocation(type, method, where.getSourceFile(), lines);

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturingTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public static LogProbe.Builder createProbeBuilder(
344344

345345
protected TestSnapshotListener installProbes(Configuration configuration) {
346346
config = Config.get();
347-
setFieldInConfig(config, "debuggerEnabled", true);
347+
// setFieldInConfig(config, "debuggerEnabled", true);
348348
setFieldInConfig(config, "debuggerClassFileDumpEnabled", true);
349349
setFieldInConfig(config, "debuggerVerifyByteCode", false);
350350
setFieldInConfig(config, "debuggerCodeOriginMaxUserFrames", 20);

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/origin/CodeOriginTest.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import static datadog.trace.api.DDTags.DD_CODE_ORIGIN_TYPE;
77
import static datadog.trace.util.AgentThreadFactory.AgentThread.TASK_SCHEDULER;
88
import static java.lang.String.format;
9-
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
109
import static java.util.Arrays.asList;
1110
import static org.junit.jupiter.api.Assertions.assertEquals;
1211
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -17,6 +16,7 @@
1716
import static utils.InstrumentationTestHelper.compileAndLoadClass;
1817

1918
import com.datadog.debugger.agent.CapturingTestBase;
19+
import com.datadog.debugger.agent.DebuggerTransformer;
2020
import com.datadog.debugger.codeorigin.DefaultCodeOriginRecorder;
2121
import com.datadog.debugger.probe.CodeOriginProbe;
2222
import com.datadog.debugger.probe.LogProbe;
@@ -35,10 +35,8 @@
3535
import datadog.trace.bootstrap.debugger.ProbeId;
3636
import datadog.trace.core.CoreTracer;
3737
import datadog.trace.util.AgentTaskScheduler;
38-
import java.io.File;
3938
import java.io.IOException;
4039
import java.net.URISyntaxException;
41-
import java.nio.file.Files;
4240
import java.nio.file.Paths;
4341
import java.util.ArrayList;
4442
import java.util.HashSet;
@@ -49,7 +47,6 @@
4947
import java.util.stream.Collectors;
5048
import org.jetbrains.annotations.NotNull;
5149
import org.joor.Reflect;
52-
import org.junit.jupiter.api.AfterAll;
5350
import org.junit.jupiter.api.BeforeEach;
5451
import org.junit.jupiter.api.Test;
5552

@@ -98,27 +95,6 @@ public void before() {
9895
setFieldInConfig(InstrumenterConfig.get(), "codeOriginEnabled", true);
9996
}
10097

101-
@AfterAll
102-
public static void collectClasses() throws IOException {
103-
File[] files =
104-
Paths.get(System.getProperty("java.io.tmpdir"), "debugger/com/datadog/debugger")
105-
.toFile()
106-
.listFiles(
107-
file -> {
108-
String name = file.getName();
109-
return name.startsWith("CodeOrigin") && name.endsWith(".class");
110-
});
111-
112-
if (new File("build").exists()) {
113-
File buildDir = new File("build/debugger");
114-
buildDir.mkdirs();
115-
for (File file : files) {
116-
Files.copy(
117-
file.toPath(), Paths.get(buildDir.getAbsolutePath(), file.getName()), REPLACE_EXISTING);
118-
}
119-
}
120-
}
121-
12298
@Test
12399
public void basicInstrumentation() throws Exception {
124100
final String className = "com.datadog.debugger.CodeOrigin01";
@@ -130,6 +106,7 @@ public void basicInstrumentation() throws Exception {
130106

131107
@Test
132108
public void withDebug1() throws Exception {
109+
DebuggerTransformer.DUMP_PATH = Paths.get("build/debugger");
133110
final String className = "com.datadog.debugger.CodeOrigin02";
134111
installProbes();
135112
final Class<?> testClass = compileAndLoadClass(className);
@@ -160,17 +137,12 @@ public void withLogProbe() throws IOException, URISyntaxException {
160137
public void doubleEntry() throws IOException, URISyntaxException {
161138
final String className = "com.datadog.debugger.CodeOrigin05";
162139

163-
List<LogProbe> probes =
140+
List<ProbeDefinition> probes =
164141
asList(
142+
new CodeOriginProbe(CODE_ORIGIN_ID1, true, Where.of(className, "entry", "()", "53")),
143+
new CodeOriginProbe(CODE_ORIGIN_ID2, false, Where.of(className, "exit", "()", "62")),
165144
new CodeOriginProbe(
166-
CODE_ORIGIN_ID1, true, Where.of(className, "entry", "()", "53"), MAX_FRAMES),
167-
new CodeOriginProbe(
168-
CODE_ORIGIN_ID2, false, Where.of(className, "exit", "()", "62"), MAX_FRAMES),
169-
new CodeOriginProbe(
170-
CODE_ORIGIN_DOUBLE_ENTRY_ID,
171-
true,
172-
Where.of(className, "doubleEntry", "()", "66"),
173-
MAX_FRAMES));
145+
CODE_ORIGIN_DOUBLE_ENTRY_ID, true, Where.of(className, "doubleEntry", "()", "66")));
174146
installProbes(probes);
175147
final Class<?> testClass = compileAndLoadClass(className);
176148
checkResults(testClass, "fullTrace", false);

0 commit comments

Comments
 (0)