mwrap.git  about / heads / tags
LD_PRELOAD malloc wrapper + line stats for Ruby
blob 054b3a3428b4ed6002bc46ac3a1a55a381ad9196 1331 bytes (raw)
$ git show HEAD:bin/mwrap	# shows this blob on the CLI

 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
 
#!/usr/bin/ruby
# frozen_string_literal: true
# Copyright (C) mwrap hackers <mwrap-public@80x24.org>
# License: GPL-2.0+ <https://www.gnu.org/licenses/gpl-2.0.txt>
help = <<EOM
usage: mwrap COMMAND [ARGS]
see https://80x24.org/mwrap/README.html for more info
EOM
ARGV.empty? and abort help
ARGV.each do |x|
  case x
  when '--version', '-v'
    require 'mwrap/version'
    puts "mwrap #{Mwrap::VERSION} - #{RUBY_DESCRIPTION}"
    exit 0
  when '--help', '-h'
    puts help
    exit 0
  else # don't intercept --version/--help intended for commands we wrap
    break
  end
end

require 'mwrap'
mwrap_so = $".grep(%r{/mwrap\.so\z})[0] or abort "mwrap.so not loaded"
cur = ENV['LD_PRELOAD']
if cur
  cur = cur.split(/[:\s]+/)
  if !cur.include?(mwrap_so)
    # drop old versions
    cur.delete_if { |path| path.end_with?('/mwrap.so') }
    cur.unshift(mwrap_so)
    ENV['LD_PRELOAD'] = cur.join(':')
  end
else
  ENV['LD_PRELOAD'] = mwrap_so
end

# work around close-on-exec by default behavior in Ruby:
opts = {}
if ENV['MWRAP'] =~ /dump_fd:(\d+)/
  dump_fd = $1.to_i
  if dump_fd > 2
    dump_io = IO.new(dump_fd)
    opts[dump_fd] = dump_io
  end
end

# allow inheriting FDs from systemd
n = ENV['LISTEN_FDS']
if n && ENV['LISTEN_PID'].to_i == $$
  n = 3 + n.to_i
  (3...n).each { |fd| opts[fd] = IO.new(fd) }
end
exec *ARGV, opts

git clone https://80x24.org/mwrap.git