Class: ASMREPL::ThreadState

Inherits:
Object
  • Object
show all
Defined in:
lib/asmrepl/thread_state.rb

Constant Summary collapse

FLAGS =
[
  ['CF', 'Carry Flag'],
  [nil, 'Reserved'],
  ['PF', 'Parity Flag'],
  [nil, 'Reserved'],
  ['AF', 'Adjust Flag'],
  [nil, 'Reserved'],
  ['ZF', 'Zero Flag'],
  ['SF', 'Sign Flag'],
  ['TF', 'Trap Flag'],
  ['IF', 'Interrupt Enable Flag'],
  ['DF', 'Direction Flag'],
  ['OF', 'Overflow Flag'],
  ['IOPL_H', 'I/O privilege level High bit'],
  ['IOPL_L', 'I/O privilege level Low bit'],
  ['NT', 'Nested Task Flag'],
  [nil, 'Reserved'],
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ ThreadState

Returns a new instance of ThreadState.



34
35
36
# File 'lib/asmrepl/thread_state.rb', line 34

def initialize buffer
  @to_ptr = buffer
end

Instance Attribute Details

#to_ptrObject (readonly)

Returns the value of attribute to_ptr.



32
33
34
# File 'lib/asmrepl/thread_state.rb', line 32

def to_ptr
  @to_ptr
end

Class Method Details

.build(fields) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/asmrepl/thread_state.rb', line 86

def self.build fields
  Class.new(ThreadState) do
    define_method(:fields) do
      fields
    end

    define_singleton_method(:fields) do
      fields
    end

    fields.each_with_index do |field, i|
      define_method(field) do
        to_ptr[Fiddle::SIZEOF_INT64_T * i, Fiddle::SIZEOF_INT64_T].unpack1("l!")
      end

      define_method("#{field}=") do |v|
        to_ptr[Fiddle::SIZEOF_INT64_T * i, Fiddle::SIZEOF_INT64_T] = [v].pack("l!")
      end
    end
  end
end

.mallocObject



9
10
11
# File 'lib/asmrepl/thread_state.rb', line 9

def self.malloc
  new Fiddle::Pointer.malloc sizeof
end

.sizeofObject



5
6
7
# File 'lib/asmrepl/thread_state.rb', line 5

def self.sizeof
  fields.length * Fiddle::SIZEOF_INT64_T
end

Instance Method Details

#[](name) ⇒ Object



38
39
40
41
42
# File 'lib/asmrepl/thread_state.rb', line 38

def [] name
  idx = fields.index(name)
  return unless idx
  to_ptr[Fiddle::SIZEOF_INT64_T * idx, Fiddle::SIZEOF_INT64_T].unpack1("l!")
end

#[]=(name, val) ⇒ Object



44
45
46
47
48
# File 'lib/asmrepl/thread_state.rb', line 44

def []= name, val
  idx = fields.index(name)
  return unless idx
  to_ptr[Fiddle::SIZEOF_INT64_T * idx, Fiddle::SIZEOF_INT64_T] = [val].pack("l!")
end

#display_registersObject



78
79
80
# File 'lib/asmrepl/thread_state.rb', line 78

def display_registers
  %w{ rax rbx rcx rdx rdi rsi rbp rsp r8 r9 r10 r11 r12 r13 r14 r15 }
end

#flagsObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/asmrepl/thread_state.rb', line 50

def flags
  flags = read_flags
  f = []
  FLAGS.each do |abbrv, _|
    if abbrv && flags & 1 == 1
      f << abbrv
    end
    flags >>= 1
  end
  f
end

#other_registersObject



82
83
84
# File 'lib/asmrepl/thread_state.rb', line 82

def other_registers
  fields - display_registers
end

#to_sObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/asmrepl/thread_state.rb', line 62

def to_s
  buf = ""
  display_registers.first(8).zip(display_registers.drop(8)).each do |l, r|
    buf << "#{l.ljust(3)}  #{sprintf("%#018x", self[l] & MAXINT)}"
    buf << "  "
    buf << "#{r.ljust(3)}  #{sprintf("%#018x", self[r] & MAXINT)}\n"
  end

  buf << "\n"

  other_registers.each do |reg|
    buf << "#{reg.ljust(7)}  #{sprintf("%#018x", self[reg] & MAXINT)}\n"
  end
  buf
end