Skip to content

Commit e8f5aae

Browse files
authored
Fix the instrumentation to instrument at the exit line (#8639)
1 parent 0a1776a commit e8f5aae

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import static com.datadog.debugger.instrumentation.ASMHelper.ldc;
55
import static com.datadog.debugger.instrumentation.Types.DEBUGGER_CONTEXT_TYPE;
66
import static com.datadog.debugger.instrumentation.Types.STRING_TYPE;
7+
import static java.lang.Integer.parseInt;
78

89
import com.datadog.debugger.instrumentation.InstrumentationResult.Status;
10+
import com.datadog.debugger.probe.CodeOriginProbe;
911
import com.datadog.debugger.probe.ProbeDefinition;
1012
import datadog.trace.bootstrap.debugger.ProbeId;
1113
import java.util.List;
1214
import org.objectweb.asm.Type;
15+
import org.objectweb.asm.tree.AbstractInsnNode;
1316
import org.objectweb.asm.tree.InsnList;
17+
import org.objectweb.asm.tree.LabelNode;
1418

1519
public class CodeOriginInstrumentor extends Instrumentor {
1620
public CodeOriginInstrumentor(
@@ -26,11 +30,21 @@ public Status instrument() {
2630
InsnList insnList = new InsnList();
2731

2832
ldc(insnList, probeIds.get(0).getEncodedId());
29-
3033
invokeStatic(insnList, DEBUGGER_CONTEXT_TYPE, "codeOrigin", Type.VOID_TYPE, STRING_TYPE);
31-
32-
methodNode.instructions.insert(methodEnterLabel, insnList);
34+
methodNode.instructions.insert(findInsertionPoint(), insnList);
3335

3436
return InstrumentationResult.Status.INSTALLED;
3537
}
38+
39+
private AbstractInsnNode findInsertionPoint() {
40+
CodeOriginProbe probe = (CodeOriginProbe) definition;
41+
List<String> lines = probe.getLocation().getLines();
42+
if (!probe.entrySpanProbe() && lines != null && !lines.isEmpty()) {
43+
LabelNode lineLabel = classFileLines.getLineLabel(parseInt(lines.get(0)));
44+
if (lineLabel != null) {
45+
return lineLabel.getNext();
46+
}
47+
}
48+
return methodEnterLabel;
49+
}
3650
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ public boolean entrySpanProbe() {
8282
public void buildLocation(MethodInfo methodInfo) {
8383
String type = where.getTypeName();
8484
String method = where.getMethodName();
85-
List<String> lines = null;
85+
List<String> lines = where.getLines() != null ? asList(where.getLines()) : null;
8686

8787
String file = where.getSourceFile();
8888

8989
if (methodInfo != null) {
9090
type = methodInfo.getTypeName();
9191
method = methodInfo.getMethodName();
92-
if (methodInfo.getMethodStart() != -1) {
92+
if (entrySpanProbe || where.getLines() == null) {
9393
lines = singletonList(String.valueOf(methodInfo.getMethodStart()));
9494
}
9595
if (file == null) {

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.junit.jupiter.api.Assertions.assertNull;
1515
import static org.junit.jupiter.api.Assertions.assertTrue;
1616
import static utils.InstrumentationTestHelper.compileAndLoadClass;
17+
import static utils.InstrumentationTestHelper.getLineForLineProbe;
1718
import static utils.TestHelper.setFieldInConfig;
1819

1920
import com.datadog.debugger.agent.CapturingTestBase;
@@ -130,12 +131,25 @@ public void doubleEntry() throws IOException, URISyntaxException {
130131
final String className = "com.datadog.debugger.CodeOrigin05";
131132

132133
installProbes(
133-
new CodeOriginProbe(CODE_ORIGIN_ID1, true, Where.of(className, "entry", "()", "53"), true),
134-
new CodeOriginProbe(CODE_ORIGIN_ID2, false, Where.of(className, "exit", "()", "62"), true),
134+
new CodeOriginProbe(
135+
CODE_ORIGIN_ID1,
136+
true,
137+
Where.of(
138+
className, "entry", "()", "" + getLineForLineProbe(className, CODE_ORIGIN_ID1)),
139+
true),
140+
new CodeOriginProbe(
141+
CODE_ORIGIN_ID2,
142+
false,
143+
Where.of(className, "exit", "()", "" + getLineForLineProbe(className, CODE_ORIGIN_ID2)),
144+
true),
135145
new CodeOriginProbe(
136146
CODE_ORIGIN_DOUBLE_ENTRY_ID,
137147
true,
138-
Where.of(className, "doubleEntry", "()", "66"),
148+
Where.of(
149+
className,
150+
"doubleEntry",
151+
"()",
152+
"" + getLineForLineProbe(className, CODE_ORIGIN_DOUBLE_ENTRY_ID)),
139153
true));
140154
final Class<?> testClass = compileAndLoadClass(className);
141155
checkResults(testClass, "fullTrace", 0);

dd-java-agent/agent-debugger/src/test/resources/com/datadog/debugger/CodeOrigin05.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ private static AgentSpan newSpan(String name) {
4949

5050
public static void entry() throws NoSuchMethodException {
5151
// just to fill out the method body
52-
boolean dummyCode = true;
52+
boolean dummyCode = true; // code origin 1
5353
if (!dummyCode) {
5454
dummyCode = false;
5555
}
5656
doubleEntry();
5757
}
5858

5959
private static void exit() {
60-
int x = 47 / 3;
60+
int x = 47 / 3; // code origin 2
6161
}
6262

6363
public static void doubleEntry() throws NoSuchMethodException {
6464
// just to fill out the method body
6565
boolean dummyCode = true;
66-
if (!dummyCode) {
66+
if (!dummyCode) { // double entry code origin
6767
dummyCode = false;
6868
}
6969
}

0 commit comments

Comments
 (0)