Skip to content

Commit

Permalink
Add $CRYSTAL_INTERPRETER_LOADER_INFO to show loaded libraries (#12221)
Browse files Browse the repository at this point in the history
With this change, you can ask the interpreter to print a list of loaded libraries, similar to what you would get with `ldd` for a compiled progam.
This feature is opt-in and can be enabled by setting the environment variable `CRYSTAL_INTERPRETER_LOADER_INFO` to a non-empty value.
  • Loading branch information
straight-shoota authored Jul 15, 2022
1 parent fd12842 commit e10841c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/compiler/crystal/interpreter/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,24 @@ class Crystal::Repl::Context
args.delete("-lgc")

Crystal::Loader.parse(args).tap do |loader|
if ENV["CRYSTAL_INTERPRETER_LOADER_INFO"]?.presence
STDERR.puts "Crystal::Loader loaded libraries:"
loader.loaded_libraries.each do |path|
STDERR.puts " #{path}"
end
end

# FIXME: Part 2: This is a workaround for initial integration of the interpreter:
# We append a handle to the current executable (i.e. the compiler program)
# to the loader's handle list. This gives the loader access to all the symbols in the compiler program,
# including those from statically linked libraries like libgc.
# This probably won't work for a fully statically linked compiler.
# But `Crystal::Loader` currently doesn't support that anyways.
loader.load_current_program_handle

if ENV["CRYSTAL_INTERPRETER_LOADER_INFO"]?.presence
STDERR.puts " current program handle"
end
end
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/loader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Crystal::Loader
end

getter search_paths : Array(String)
getter loaded_libraries = [] of String

def initialize(@search_paths : Array(String))
@handles = [] of Handle
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/loader/msvc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class Crystal::Loader
# (https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order)
handle = open_library(dll)
return false unless handle

@handles << handle
@loaded_libraries << dll
end

true
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/loader/unix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Crystal::Loader
return false unless handle

@handles << handle
@loaded_libraries << path.to_s
true
end

Expand Down

0 comments on commit e10841c

Please sign in to comment.