Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rbconfig'
- require 'fileutils'
- module PryExceptionExplorer
- CompileError = Class.new(StandardError)
- module ShimBuilder
- class << self
- attr_reader :dir, :file
- end
- @dir = File.expand_path("~/.pry-exception_explorer/#{RUBY_VERSION}")
- @file = File.join(@dir, "raise_shim.c")
- if RUBY_PLATFORM =~ /darwin/
- Dyname = "dylib"
- else
- Dyname = "so"
- end
- ShimCode = <<-EOF
- #include <ruby.h>
- #include <stdio.h>
- #include <dlfcn.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdarg.h>
- void
- rb_raise(VALUE exc, const char *fmt, ...)
- {
- va_list args;
- VALUE mesg;
- va_start(args, fmt);
- mesg = rb_vsprintf(fmt, args);
- va_end(args);
- rb_funcall(rb_cObject, rb_intern("raise"), 2, exc, mesg);
- }
- void
- rb_name_error(ID id, const char *fmt, ...)
- {
- rb_funcall(rb_cObject, rb_intern("raise"), 2, rb_eNameError, rb_str_new2("hooked exception (pry)"));
- }
- EOF
- def self.create_directory_and_source_file
- FileUtils.mkdir_p(@dir)
- File.open(@file, 'w') do |f|
- f.puts(ShimCode)
- end
- end
- def self.compile
- create_directory_and_source_file
- # -L
- ldflags = [ ]
- ldflags << "-L#{RbConfig::CONFIG['libdir']}"
- ldflags << "-L#{RbConfig::CONFIG['archdir']}"
- ldflags << RbConfig::CONFIG['LDFLAGS']
- ldflags << RbConfig::CONFIG['LIBS']
- ldflags << RbConfig::CONFIG['DLDLIBS']
- ldflags << RbConfig::CONFIG['LIBRUBYARG']
- ldflags = ldflags.join(' ')
- # -I
- includes = [ ]
- if RbConfig::CONFIG['rubyhdrdir'] then
- includes << "#{RbConfig::CONFIG['rubyhdrdir']}"
- includes << "#{RbConfig::CONFIG['rubyhdrdir']}/#{RbConfig::CONFIG['arch']}"
- includes << "#{RbConfig::CONFIG['rubyhdrdir']}/ruby/backward"
- else
- includes << "#{RbConfig::CONFIG['archdir']}"
- end
- includes = includes.map { |inc| "-I #{inc}" }.join(' ')
- if RUBY_PLATFORM =~ /darwin/
- compile_line = "gcc -Wall #{ldflags} #{includes} -o lib_overrides.dylib -dynamiclib #{@file}"
- else
- compile_line = "gcc -Wall -O2 -g #{ldflags} -shared #{includes} -o lib_overrides.so #{@file}"
- end
- FileUtils.chdir @dir do
- puts compile_line
- if !system(compile_line)
- raise CompileError, "There was a problem building the shim, aborted!"
- end
- end
- end
- end
- end
- PryExceptionExplorer::ShimBuilder.compile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement