summaryrefslogtreecommitdiff
path: root/misc/ujit_disasm.rb
blob: 38eb139c33db01230422c94bab148591bb922cae (plain)
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
begin
    require "crabstone"
    require "stringio"
rescue LoadError => e
  puts "Please install crabstone, which is needed by the disassembler:"
  puts "  $ brew install capstone"
  puts "  $ gem install capstone"
  raise e
end

module UJIT
  def self.disasm(iseq)
    blocks = UJIT.blocks_for(iseq)
    return if blocks.empty?

    io = StringIO.new

    cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)

    io.puts iseq.disasm

    blocks.sort_by(&:address).reverse.each do |block|
      io.puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
      cs.disasm(block.code, 0).each do |i|
        io.printf(
          "\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
          address: i.address,
          instruction: i.mnemonic,
          details: i.op_str
        )
      end
    end
    io.string
  end
end