Skip to content

Commit

Permalink
[Truffle] Option for logging feature location.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Jan 9, 2017
1 parent 6b92884 commit 81bdd91
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
2 changes: 2 additions & 0 deletions tool/truffle/options.yml
Expand Up @@ -111,3 +111,5 @@ CEXTS_LOG_LOAD: [cexts.log.load, boolean, false, Log loading of cexts]
LOG_DYNAMIC_CONSTANT_LOOKUP: [constant.dynamic_lookup.log, boolean, false, Log source code positions where dynamic constant lookup is performed]

OPTIONS_LOG: [options.log, boolean, false, Log the final value of all options]

LOG_FEATURE_LOCATION: [log.feature_location, boolean, false, Log the process of finding features]
13 changes: 9 additions & 4 deletions truffle/src/main/java/org/jruby/truffle/RubyLanguage.java
Expand Up @@ -62,11 +62,16 @@ private RubyLanguage() {

@CompilerDirectives.TruffleBoundary
public static String fileLine(SourceSection section) {
Source source = section.getSource();
if (section.isAvailable()) {
return String.format("%s:%d", source.getName(), section.getStartLine());
if (section == null) {
return "unknown";
} else {
return source.getName();
final Source source = section.getSource();

if (section.isAvailable()) {
return String.format("%s:%d", source.getName(), section.getStartLine());
} else {
return source.getName();
}
}
}

Expand Down
Expand Up @@ -13,11 +13,15 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.IndirectCallNode;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.Log;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.RubyLanguage;
import org.jruby.truffle.core.array.ArrayOperations;
import org.jruby.truffle.language.CallStackManager;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.language.control.JavaException;
import org.jruby.truffle.language.control.RaiseException;

Expand All @@ -43,32 +47,73 @@ public ReentrantLockFreeingMap<String> getFileLocks() {

@TruffleBoundary
public String findFeature(String feature) {
if (context.getOptions().LOG_FEATURE_LOCATION) {
final String originalFeature = feature;

Log.LOGGER.info(() -> {
final Node callerNode = context.getCallStack().getTopMostUserCallNode();

final SourceSection sourceSection;

if (callerNode == null) {
sourceSection = null;
} else {
sourceSection = callerNode.getEncapsulatingSourceSection();
}

return String.format("starting search from %s for feature %s...", RubyLanguage.fileLine(sourceSection), originalFeature);
});
}

final String currentDirectory = context.getNativePlatform().getPosix().getcwd();

if (context.getOptions().LOG_FEATURE_LOCATION) {
Log.LOGGER.info(String.format("current directory: %s", currentDirectory));
}

if (feature.startsWith("./")) {
feature = currentDirectory + "/" + feature.substring(2);

Log.LOGGER.info(String.format("feature adjusted to %s", feature));
} else if (feature.startsWith("../")) {
feature = currentDirectory.substring(
0,
currentDirectory.lastIndexOf('/')) + "/" + feature.substring(3);

Log.LOGGER.info(String.format("feature adjusted to %s", feature));
}

String found = null;

if (feature.startsWith(SourceLoader.TRUFFLE_SCHEME)
|| feature.startsWith(SourceLoader.JRUBY_SCHEME)
|| new File(feature).isAbsolute()) {
return findFeatureWithAndWithoutExtension(feature);
found = findFeatureWithAndWithoutExtension(feature);
} else {
for (Object pathObject : ArrayOperations.toIterable(context.getCoreLibrary().getLoadPath())) {
if (context.getOptions().LOG_FEATURE_LOCATION) {
Log.LOGGER.info(String.format("from load path %s...", pathObject.toString()));
}

final String fileWithinPath = new File(pathObject.toString(), feature).getPath();
final String result = findFeatureWithAndWithoutExtension(fileWithinPath);

if (result != null) {
found = result;
break;
}
}
}

for (Object pathObject : ArrayOperations.toIterable(context.getCoreLibrary().getLoadPath())) {
final String fileWithinPath = new File(pathObject.toString(), feature).getPath();
final String result = findFeatureWithAndWithoutExtension(fileWithinPath);

if (result != null) {
return result;
if (context.getOptions().LOG_FEATURE_LOCATION) {
if (found == null) {
Log.LOGGER.info("not found");
} else {
Log.LOGGER.info(String.format("found in %s", found));
}
}

return null;
return found;
}

private String findFeatureWithAndWithoutExtension(String path) {
Expand Down Expand Up @@ -104,6 +149,10 @@ private String findFeatureWithAndWithoutExtension(String path) {
}

private String findFeatureWithExactPath(String path) {
if (context.getOptions().LOG_FEATURE_LOCATION) {
Log.LOGGER.info(String.format("trying %s...", path));
}

if (path.startsWith(SourceLoader.TRUFFLE_SCHEME) || path.startsWith(SourceLoader.JRUBY_SCHEME)) {
return path;
}
Expand Down
4 changes: 4 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/options/Options.java
Expand Up @@ -112,6 +112,7 @@ public class Options {
public final boolean CEXTS_LOG_LOAD;
public final boolean LOG_DYNAMIC_CONSTANT_LOOKUP;
public final boolean OPTIONS_LOG;
public final boolean LOG_FEATURE_LOCATION;

Options(OptionsBuilder builder) {
HOME = builder.getOrDefault(OptionsCatalog.HOME);
Expand Down Expand Up @@ -208,6 +209,7 @@ public class Options {
CEXTS_LOG_LOAD = builder.getOrDefault(OptionsCatalog.CEXTS_LOG_LOAD);
LOG_DYNAMIC_CONSTANT_LOOKUP = builder.getOrDefault(OptionsCatalog.LOG_DYNAMIC_CONSTANT_LOOKUP);
OPTIONS_LOG = builder.getOrDefault(OptionsCatalog.OPTIONS_LOG);
LOG_FEATURE_LOCATION = builder.getOrDefault(OptionsCatalog.LOG_FEATURE_LOCATION);
}

public Object fromDescription(OptionDescription description) {
Expand Down Expand Up @@ -400,6 +402,8 @@ public Object fromDescription(OptionDescription description) {
return LOG_DYNAMIC_CONSTANT_LOOKUP;
case "options.log":
return OPTIONS_LOG;
case "log.feature_location":
return LOG_FEATURE_LOCATION;
default:
return null;
}
Expand Down
Expand Up @@ -110,6 +110,7 @@ public class OptionsCatalog {
public static final OptionDescription CEXTS_LOG_LOAD = new BooleanOptionDescription("cexts.log.load", "Log loading of cexts", false);
public static final OptionDescription LOG_DYNAMIC_CONSTANT_LOOKUP = new BooleanOptionDescription("constant.dynamic_lookup.log", "Log source code positions where dynamic constant lookup is performed", false);
public static final OptionDescription OPTIONS_LOG = new BooleanOptionDescription("options.log", "Log the final value of all options", false);
public static final OptionDescription LOG_FEATURE_LOCATION = new BooleanOptionDescription("log.feature_location", "Log the process of finding features", false);

public static OptionDescription fromName(String name) {
switch (name) {
Expand Down Expand Up @@ -301,6 +302,8 @@ public static OptionDescription fromName(String name) {
return LOG_DYNAMIC_CONSTANT_LOOKUP;
case "options.log":
return OPTIONS_LOG;
case "log.feature_location":
return LOG_FEATURE_LOCATION;
default:
return null;
}
Expand Down Expand Up @@ -402,6 +405,7 @@ public static OptionDescription[] allDescriptions() {
CEXTS_LOG_LOAD,
LOG_DYNAMIC_CONSTANT_LOOKUP,
OPTIONS_LOG,
LOG_FEATURE_LOCATION,
};
}

Expand Down

0 comments on commit 81bdd91

Please sign in to comment.