Skip to content

Commit f86bd77

Browse files
committed
add config for local var hoisting
enable only hoisting for java language
1 parent e7adeff commit f86bd77

5 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,11 @@ private void instrumentMethodEnter() {
396396
if (methodNode.tryCatchBlocks.size() > 0) {
397397
throwableListVar = declareThrowableList(insnList);
398398
}
399-
unscopedLocalVars = initAndHoistLocalVars(insnList);
399+
unscopedLocalVars = Collections.emptyList();
400+
if (Config.get().isDebuggerHoistLocalVarsEnabled() && language == JvmLanguage.JAVA) {
401+
// for now, only hoist local vars for Java
402+
unscopedLocalVars = initAndHoistLocalVars(insnList);
403+
}
400404
insnList.add(contextInitLabel);
401405
if (definition instanceof SpanDecorationProbe
402406
&& definition.getEvaluateAt() == MethodLocation.EXIT) {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public abstract class Instrumentor {
4646
protected int localVarBaseOffset;
4747
protected int argOffset;
4848
protected final LocalVariableNode[] localVarsBySlotArray;
49+
protected final JvmLanguage language;
4950
protected LabelNode returnHandlerLabel;
5051
protected final List<CapturedContextInstrumentor.FinallyBlock> finallyBlocks = new ArrayList<>();
5152

@@ -69,6 +70,7 @@ public Instrumentor(
6970
argOffset += t.getSize();
7071
}
7172
localVarsBySlotArray = extractLocalVariables(argTypes);
73+
this.language = JvmLanguage.of(classNode);
7274
}
7375

7476
public abstract InstrumentationResult.Status instrument();
@@ -294,4 +296,31 @@ public FinallyBlock(LabelNode startLabel, LabelNode endLabel, LabelNode handlerL
294296
this.handlerLabel = handlerLabel;
295297
}
296298
}
299+
300+
protected enum JvmLanguage {
301+
JAVA,
302+
KOTLIN,
303+
SCALA,
304+
GROOVY,
305+
UNKNOWN;
306+
307+
public static JvmLanguage of(ClassNode classNode) {
308+
if (classNode.sourceFile == null) {
309+
return UNKNOWN;
310+
}
311+
if (classNode.sourceFile.endsWith(".java")) {
312+
return JAVA;
313+
}
314+
if (classNode.sourceFile.endsWith(".kt")) {
315+
return KOTLIN;
316+
}
317+
if (classNode.sourceFile.endsWith(".scala")) {
318+
return SCALA;
319+
}
320+
if (classNode.sourceFile.endsWith(".groovy")) {
321+
return GROOVY;
322+
}
323+
return UNKNOWN;
324+
}
325+
}
297326
}

dd-trace-api/src/main/java/datadog/trace/api/ConfigDefaults.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public final class ConfigDefaults {
179179
static final boolean DEFAULT_DEBUGGER_VERIFY_BYTECODE = true;
180180
static final boolean DEFAULT_DEBUGGER_INSTRUMENT_THE_WORLD = false;
181181
static final int DEFAULT_DEBUGGER_CAPTURE_TIMEOUT = 100; // milliseconds
182+
static final boolean DEFAULT_DEBUGGER_HOIST_LOCALVARS_ENABLED = true;
182183
static final boolean DEFAULT_DEBUGGER_SYMBOL_ENABLED = true;
183184
static final boolean DEFAULT_DEBUGGER_SYMBOL_FORCE_UPLOAD = false;
184185
static final int DEFAULT_DEBUGGER_SYMBOL_FLUSH_THRESHOLD = 100; // nb of classes

dd-trace-api/src/main/java/datadog/trace/api/config/DebuggerConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public final class DebuggerConfig {
2828
public static final String DEBUGGER_REDACTION_EXCLUDED_IDENTIFIERS =
2929
"dynamic.instrumentation.redaction.excluded.identifiers";
3030
public static final String DEBUGGER_REDACTED_TYPES = "dynamic.instrumentation.redacted.types";
31+
public static final String DEBUGGER_HOIST_LOCALVARS_ENABLED =
32+
"dynamic.instrumentation.hoist.localvars.enabled";
3133
public static final String DEBUGGER_SYMBOL_ENABLED = "symbol.database.upload.enabled";
3234
public static final String DEBUGGER_SYMBOL_FORCE_UPLOAD = "internal.force.symbol.database.upload";
3335
public static final String DEBUGGER_SYMBOL_INCLUDES = "symbol.database.includes";

internal-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ public static String getHostName() {
394394
private final String debuggerRedactedIdentifiers;
395395
private final Set<String> debuggerRedactionExcludedIdentifiers;
396396
private final String debuggerRedactedTypes;
397+
private final boolean debuggerHoistLocalVarsEnabled;
397398
private final boolean debuggerSymbolEnabled;
398399
private final boolean debuggerSymbolForceUpload;
399400
private final String debuggerSymbolIncludes;
@@ -1526,6 +1527,9 @@ PROFILING_DATADOG_PROFILER_ENABLED, isDatadogProfilerSafeInCurrentEnvironment())
15261527
debuggerRedactionExcludedIdentifiers =
15271528
tryMakeImmutableSet(configProvider.getList(DEBUGGER_REDACTION_EXCLUDED_IDENTIFIERS));
15281529
debuggerRedactedTypes = configProvider.getString(DEBUGGER_REDACTED_TYPES, null);
1530+
debuggerHoistLocalVarsEnabled =
1531+
configProvider.getBoolean(
1532+
DEBUGGER_HOIST_LOCALVARS_ENABLED, DEFAULT_DEBUGGER_HOIST_LOCALVARS_ENABLED);
15291533
debuggerSymbolEnabled =
15301534
configProvider.getBoolean(DEBUGGER_SYMBOL_ENABLED, DEFAULT_DEBUGGER_SYMBOL_ENABLED);
15311535
debuggerSymbolForceUpload =
@@ -3042,6 +3046,10 @@ public String getDebuggerRedactedTypes() {
30423046
return debuggerRedactedTypes;
30433047
}
30443048

3049+
public boolean isDebuggerHoistLocalVarsEnabled() {
3050+
return debuggerHoistLocalVarsEnabled;
3051+
}
3052+
30453053
public boolean isAwsPropagationEnabled() {
30463054
return awsPropagationEnabled;
30473055
}

0 commit comments

Comments
 (0)