-
Notifications
You must be signed in to change notification settings - Fork 0
/
investigator.rb
executable file
·65 lines (53 loc) · 1.41 KB
/
investigator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
require 'fiddle'
require 'fiddle/import'
require 'json'
$is_windows = Gem.win_platform?
def linked_libruby
$is_windows ? linked_libruby_windows : linked_libruby_unix
end
def executable
File.join(
RbConfig::CONFIG['bindir'],
"#{RbConfig::CONFIG['ruby_install_name']}#{RbConfig::CONFIG['EXEEXT']}"
)
end
Dl_info = Fiddle::Importer.struct [
'char* dli_fname',
'void* dli_fbase',
'char* dli_sname',
'void* dli_saddr',
]
private
def linked_libruby_unix
curr_process = Fiddle.dlopen(nil)
dladdr = Fiddle::Function.new(
curr_process['dladdr'],
[Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP],
Fiddle::TYPE_INT
)
dlinfo = Dl_info.malloc(Fiddle::RUBY_FREE)
retcode = dladdr.call(
Fiddle::Pointer.new(curr_process['ruby_show_version']),
dlinfo
)
return nil if retcode == 0 # means error
fname = dlinfo.dli_fname.to_s
# If it's not a shared library
# it can be 'ruby' or 'irb' or whatever you use to call ruby from the shell
# https://stackoverflow.com/a/41627181/13500870
return nil unless File.exist?(fname)
path = File.realpath(fname)
path unless path == File.realpath(executable)
end
def linked_libruby_windows
raise NotImplementedError, 'Windows is not supported'
end
begin
puts JSON.pretty_generate(
executable: executable,
linked_libruby: linked_libruby,
)
rescue Exception => exception
abort "#{exception.class}: #{exception.message}"
end