Skip to content

Commit ddae1c4

Browse files
committed
Add --backtraces-to-native to show a backtrace on ValueWrapper#toNative
1 parent 2031318 commit ddae1c4

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ protected long allocateSharedHandleOnKnownThread(ValueWrapper wrapper) {
315315
true);
316316
}
317317

318-
protected static long allocateHandle(ValueWrapper wrapper, RubyContext context, RubyLanguage language,
318+
protected long allocateHandle(ValueWrapper wrapper, RubyContext context, RubyLanguage language,
319319
HandleBlockHolder holder, boolean shared) {
320320
HandleBlock block;
321321
if (shared) {
@@ -328,6 +328,10 @@ protected static long allocateHandle(ValueWrapper wrapper, RubyContext context,
328328
context.getValueWrapperManager().recordHandleAllocation();
329329
}
330330

331+
if (context.getOptions().BACKTRACE_ON_TO_NATIVE) {
332+
context.getDefaultBacktraceFormatter().printBacktraceOnEnvStderr("ValueWrapper#toNative: ", getNode());
333+
}
334+
331335
if (block == null || block.isFull()) {
332336
if (block != null) {
333337
context.getMarkingService().queueForMarking(block);

src/main/java/org/truffleruby/options/Options.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ public class Options {
166166
public final boolean CEXTS_TO_NATIVE_STATS;
167167
/** --cexts-to-native-count=CEXTS_TO_NATIVE_STATS */
168168
public final boolean CEXTS_TO_NATIVE_COUNT;
169+
/** --backtraces-to-native=false */
170+
public final boolean BACKTRACE_ON_TO_NATIVE;
169171
/** --basic-ops-log-rewrite=false */
170172
public final boolean BASICOPS_LOG_REWRITE;
171173
/** --array-small=3 */
@@ -279,6 +281,7 @@ public Options(Env env, OptionValues options, LanguageOptions languageOptions) {
279281
PRINT_INTERNED_TSTRING_STATS = options.get(OptionsCatalog.PRINT_INTERNED_TSTRING_STATS_KEY);
280282
CEXTS_TO_NATIVE_STATS = options.get(OptionsCatalog.CEXTS_TO_NATIVE_STATS_KEY);
281283
CEXTS_TO_NATIVE_COUNT = options.hasBeenSet(OptionsCatalog.CEXTS_TO_NATIVE_COUNT_KEY) ? options.get(OptionsCatalog.CEXTS_TO_NATIVE_COUNT_KEY) : CEXTS_TO_NATIVE_STATS;
284+
BACKTRACE_ON_TO_NATIVE = options.get(OptionsCatalog.BACKTRACE_ON_TO_NATIVE_KEY);
282285
BASICOPS_LOG_REWRITE = options.get(OptionsCatalog.BASICOPS_LOG_REWRITE_KEY);
283286
ARRAY_SMALL = options.get(OptionsCatalog.ARRAY_SMALL_KEY);
284287
CEXTS_MARKING_CACHE = options.get(OptionsCatalog.CEXTS_MARKING_CACHE_KEY);
@@ -445,6 +448,8 @@ public Object fromDescriptor(OptionDescriptor descriptor) {
445448
return CEXTS_TO_NATIVE_STATS;
446449
case "ruby.cexts-to-native-count":
447450
return CEXTS_TO_NATIVE_COUNT;
451+
case "ruby.backtraces-to-native":
452+
return BACKTRACE_ON_TO_NATIVE;
448453
case "ruby.basic-ops-log-rewrite":
449454
return BASICOPS_LOG_REWRITE;
450455
case "ruby.array-small":

src/options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ INTERNAL: # Options for debugging the TruffleRuby implementation
173173
PRINT_INTERNED_TSTRING_STATS: [print-interned-tstring-stats, boolean, false, Print interned tstring stats at application exit]
174174
CEXTS_TO_NATIVE_STATS: [cexts-to-native-stats, boolean, false, Track the number of conversions of VALUEs to native and print the stats at application exit]
175175
CEXTS_TO_NATIVE_COUNT: [cexts-to-native-count, boolean, CEXTS_TO_NATIVE_STATS, Track the number of conversions of VALUEs to native]
176+
BACKTRACE_ON_TO_NATIVE: [backtraces-to-native, boolean, false, Show a backtrace when a ValueWrapper handle is created for a Ruby object]
176177

177178
# Options to debug the implementation
178179
LAZY_BUILTINS: [lazy-builtins, boolean, LAZY_CALLTARGETS, Load builtin classes (core methods & primitives) lazily on first use]

src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class OptionsCatalog {
103103
public static final OptionKey<Boolean> PRINT_INTERNED_TSTRING_STATS_KEY = new OptionKey<>(false);
104104
public static final OptionKey<Boolean> CEXTS_TO_NATIVE_STATS_KEY = new OptionKey<>(false);
105105
public static final OptionKey<Boolean> CEXTS_TO_NATIVE_COUNT_KEY = new OptionKey<>(CEXTS_TO_NATIVE_STATS_KEY.getDefaultValue());
106+
public static final OptionKey<Boolean> BACKTRACE_ON_TO_NATIVE_KEY = new OptionKey<>(false);
106107
public static final OptionKey<Boolean> LAZY_BUILTINS_KEY = new OptionKey<>(LAZY_CALLTARGETS_KEY.getDefaultValue());
107108
public static final OptionKey<Boolean> LAZY_TRANSLATION_CORE_KEY = new OptionKey<>(LAZY_CALLTARGETS_KEY.getDefaultValue());
108109
public static final OptionKey<Boolean> CHAOS_DATA_KEY = new OptionKey<>(false);
@@ -827,6 +828,14 @@ public class OptionsCatalog {
827828
.usageSyntax("")
828829
.build();
829830

831+
public static final OptionDescriptor BACKTRACE_ON_TO_NATIVE = OptionDescriptor
832+
.newBuilder(BACKTRACE_ON_TO_NATIVE_KEY, "ruby.backtraces-to-native")
833+
.help("Show a backtrace when a ValueWrapper handle is created for a Ruby object")
834+
.category(OptionCategory.INTERNAL)
835+
.stability(OptionStability.EXPERIMENTAL)
836+
.usageSyntax("")
837+
.build();
838+
830839
public static final OptionDescriptor LAZY_BUILTINS = OptionDescriptor
831840
.newBuilder(LAZY_BUILTINS_KEY, "ruby.lazy-builtins")
832841
.help("Load builtin classes (core methods & primitives) lazily on first use")
@@ -1467,6 +1476,8 @@ public static OptionDescriptor fromName(String name) {
14671476
return CEXTS_TO_NATIVE_STATS;
14681477
case "ruby.cexts-to-native-count":
14691478
return CEXTS_TO_NATIVE_COUNT;
1479+
case "ruby.backtraces-to-native":
1480+
return BACKTRACE_ON_TO_NATIVE;
14701481
case "ruby.lazy-builtins":
14711482
return LAZY_BUILTINS;
14721483
case "ruby.lazy-translation-core":
@@ -1675,6 +1686,7 @@ public static OptionDescriptor[] allDescriptors() {
16751686
PRINT_INTERNED_TSTRING_STATS,
16761687
CEXTS_TO_NATIVE_STATS,
16771688
CEXTS_TO_NATIVE_COUNT,
1689+
BACKTRACE_ON_TO_NATIVE,
16781690
LAZY_BUILTINS,
16791691
LAZY_TRANSLATION_CORE,
16801692
CHAOS_DATA,

0 commit comments

Comments
 (0)