Class: FFI::UDis86::UD
- Inherits:
-
Struct
- Object
- Struct
- FFI::UDis86::UD
- Includes:
- Enumerable
- Defined in:
- lib/ffi/udis86/ud.rb
Class Method Summary collapse
-
.create(options = {}) {|ud| ... } ⇒ UD
Creates a new disassembler object.
-
.open(path, options = {}) {|ud| ... } ⇒ Object
Opens a file and disassembles it.
Instance Method Summary collapse
-
#address_prefix ⇒ Integer
The address-size prefix (67h) of the last disassembled instruction.
-
#disassemble {|ud| ... } ⇒ UD
(also: #disas, #each)
Reads each byte, disassembling each instruction.
-
#init ⇒ UD
Initializes the disassembler.
-
#input_buffer ⇒ String
Returns the input buffer used by the disassembler.
-
#input_buffer=(data) ⇒ String
Sets the contents of the input buffer for the disassembler.
-
#input_callback {|ud| ... } ⇒ Object
Sets the input callback for the disassembler.
-
#insn_length ⇒ Integer
Returns the number of bytes that were disassembled.
-
#insn_offset ⇒ Integer
Returns the starting offset of the disassembled instruction relative to the initial value of the Program Counter (PC).
-
#insn_ptr ⇒ FFI::Pointer
Returns the pointer to the buffer holding the disassembled instruction bytes.
-
#lock_prefix ⇒ Integer
The lock prefix of the last disassembled instruction.
-
#mnemonic ⇒ Symbol
The mnemonic string of the last disassembled instruction.
-
#mnemonic_code ⇒ Symbol
The mnemonic code of the last disassembled instruction.
-
#mode ⇒ Integer
Returns the mode the disassembler is running in.
-
#mode=(new_mode) ⇒ Integer
Sets the mode the disassembler will run in.
-
#next_insn ⇒ UD
Disassembles the next instruction in the input stream.
-
#operand_prefix ⇒ Integer
The operand-size prefix (66h) of the last disassembled instruction.
-
#operands ⇒ Array<Operand>
Returns the operands for the last disassembled instruction.
-
#pc ⇒ Integer
Returns the current value of the Program Counter (PC).
-
#pc=(new_pc) ⇒ Integer
Sets the value of the Program Counter (PC).
-
#rep_prefix ⇒ Integer
The rep prefix of the last disassembled instruction.
-
#repe_prefix ⇒ Integer
The repe prefix of the last disassembled instruction.
-
#repne_prefix ⇒ Integer
The repne prefix of the last disassembled instruction.
-
#rex_prefix ⇒ Integer
The 64-bit mode REX prefix of the last disassembled instruction.
-
#segment_prefix ⇒ Integer
The segment register prefix of the last disassembled instruction.
-
#skip(n) ⇒ UD
Causes the disassembler to skip a certain number of bytes in the input stream.
-
#syntax=(new_syntax) ⇒ Symbol
Sets the assembly syntax that the disassembler will emit.
-
#to_asm ⇒ String
(also: #to_s)
Returns the assembly syntax for the last disassembled instruction.
-
#to_hex ⇒ String
Returns the hexadecimal representation of the disassembled instruction.
-
#vendor ⇒ Symbol
The vendor of whose instructions are to be chosen from during disassembly.
-
#vendor=(new_vendor) ⇒ Symbol
Sets the vendor, of whose instructions are to be chosen from during disassembly.
Class Method Details
.create(options = {}) {|ud| ... } ⇒ UD
Creates a new disassembler object.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ffi/udis86/ud.rb', line 89 def self.create(={},&block) ud = self.new ud.init ud.mode = ([:mode] || 32) if [:buffer] ud.input_buffer = [:buffer] end ud.syntax = ([:syntax] || :intel) if [:vendor] ud.vendor = [:vendor] end if [:pc] ud.pc = [:pc] end ud.input_callback(&block) if block return ud end |
.open(path, options = {}) {|ud| ... } ⇒ Object
Opens a file and disassembles it.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ffi/udis86/ud.rb', line 147 def self.open(path,={}) File.open(path,'rb') do |file| ud = self.create() do |ud| if (b = file.getc) b.ord else -1 end end yield ud if block_given? end return nil end |
Instance Method Details
#address_prefix ⇒ Integer
The address-size prefix (67h) of the last disassembled instruction.
409 410 411 |
# File 'lib/ffi/udis86/ud.rb', line 409 def address_prefix self[:pfx_adr] end |
#disassemble {|ud| ... } ⇒ UD Also known as: disas, each
Reads each byte, disassembling each instruction.
543 544 545 546 547 548 549 |
# File 'lib/ffi/udis86/ud.rb', line 543 def disassemble until UDis86.ud_disassemble(self) == 0 yield self if block_given? end return self end |
#init ⇒ UD
Initializes the disassembler.
169 170 171 172 |
# File 'lib/ffi/udis86/ud.rb', line 169 def init UDis86.ud_init(self) return self end |
#input_buffer ⇒ String
Returns the input buffer used by the disassembler.
180 181 182 183 184 185 186 |
# File 'lib/ffi/udis86/ud.rb', line 180 def input_buffer if @input_buffer @input_buffer.get_bytes(0,@input_buffer.total) else '' end end |
#input_buffer=(data) ⇒ String
Sets the contents of the input buffer for the disassembler.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ffi/udis86/ud.rb', line 200 def input_buffer=(data) data = data.to_s @input_buffer = FFI::MemoryPointer.new(data.length) if data.kind_of?(Array) @input_buffer.put_array_of_uint8(0,data) elsif data.kind_of?(String) @input_buffer.put_bytes(0,data) else raise(RuntimeError,"input buffer must be either a String or an Array of bytes",caller) end UDis86.ud_set_input_buffer(self,@input_buffer,@input_buffer.total) return data end |
#input_callback {|ud| ... } ⇒ Object
Sets the input callback for the disassembler.
228 229 230 231 232 233 234 235 236 |
# File 'lib/ffi/udis86/ud.rb', line 228 def input_callback(&block) if block @input_callback = Proc.new { |ptr| block.call(self) } UDis86.ud_set_input_hook(self,@input_callback) end return @input_callback end |
#insn_length ⇒ Integer
Returns the number of bytes that were disassembled.
504 505 506 |
# File 'lib/ffi/udis86/ud.rb', line 504 def insn_length UDis86.ud_insn_len(self) end |
#insn_offset ⇒ Integer
Returns the starting offset of the disassembled instruction relative to the initial value of the Program Counter (PC).
515 516 517 |
# File 'lib/ffi/udis86/ud.rb', line 515 def insn_offset UDis86.ud_insn_off(self) end |
#insn_ptr ⇒ FFI::Pointer
Returns the pointer to the buffer holding the disassembled instruction bytes.
526 527 528 |
# File 'lib/ffi/udis86/ud.rb', line 526 def insn_ptr UDis86.ud_insn_ptr(self) end |
#lock_prefix ⇒ Integer
The lock prefix of the last disassembled instruction.
419 420 421 |
# File 'lib/ffi/udis86/ud.rb', line 419 def lock_prefix self[:pfx_lock] end |
#mnemonic ⇒ Symbol
The mnemonic string of the last disassembled instruction.
369 370 371 |
# File 'lib/ffi/udis86/ud.rb', line 369 def mnemonic UDis86.ud_lookup_mnemonic(self[:mnemonic]).to_sym end |
#mnemonic_code ⇒ Symbol
The mnemonic code of the last disassembled instruction.
359 360 361 |
# File 'lib/ffi/udis86/ud.rb', line 359 def mnemonic_code self[:mnemonic] end |
#mode ⇒ Integer
Returns the mode the disassembler is running in.
244 245 246 |
# File 'lib/ffi/udis86/ud.rb', line 244 def mode self[:dis_mode] end |
#mode=(new_mode) ⇒ Integer
Sets the mode the disassembler will run in.
257 258 259 260 261 262 263 264 |
# File 'lib/ffi/udis86/ud.rb', line 257 def mode=(new_mode) unless MODES.include?(new_mode) raise(RuntimeError,"invalid disassembly mode #{new_mode}",caller) end UDis86.ud_set_mode(self,new_mode) return new_mode end |
#next_insn ⇒ UD
Disassembles the next instruction in the input stream.
494 495 496 |
# File 'lib/ffi/udis86/ud.rb', line 494 def next_insn UDis86.ud_disassemble(self) end |
#operand_prefix ⇒ Integer
The operand-size prefix (66h) of the last disassembled instruction.
399 400 401 |
# File 'lib/ffi/udis86/ud.rb', line 399 def operand_prefix self[:pfx_opr] end |
#operands ⇒ Array<Operand>
Returns the operands for the last disassembled instruction.
482 483 484 485 486 |
# File 'lib/ffi/udis86/ud.rb', line 482 def operands self[:operand].entries.select do |operand| OPERAND_TYPES.include?(operand.type) end end |
#pc ⇒ Integer
Returns the current value of the Program Counter (PC).
320 321 322 |
# File 'lib/ffi/udis86/ud.rb', line 320 def pc self[:pc] end |
#pc=(new_pc) ⇒ Integer
Sets the value of the Program Counter (PC).
333 334 335 336 |
# File 'lib/ffi/udis86/ud.rb', line 333 def pc=(new_pc) UDis86.ud_set_pc(self,new_pc) return new_pc end |
#rep_prefix ⇒ Integer
The rep prefix of the last disassembled instruction.
429 430 431 |
# File 'lib/ffi/udis86/ud.rb', line 429 def rep_prefix self[:pfx_rep] end |
#repe_prefix ⇒ Integer
The repe prefix of the last disassembled instruction.
439 440 441 |
# File 'lib/ffi/udis86/ud.rb', line 439 def repe_prefix self[:pfx_repe] end |
#repne_prefix ⇒ Integer
The repne prefix of the last disassembled instruction.
449 450 451 |
# File 'lib/ffi/udis86/ud.rb', line 449 def repne_prefix self[:pfx_repne] end |
#rex_prefix ⇒ Integer
The 64-bit mode REX prefix of the last disassembled instruction.
379 380 381 |
# File 'lib/ffi/udis86/ud.rb', line 379 def rex_prefix self[:pfx_rex] end |
#segment_prefix ⇒ Integer
The segment register prefix of the last disassembled instruction.
389 390 391 |
# File 'lib/ffi/udis86/ud.rb', line 389 def segment_prefix self[:pfx_seg] end |
#skip(n) ⇒ UD
Causes the disassembler to skip a certain number of bytes in the input stream.
348 349 350 351 |
# File 'lib/ffi/udis86/ud.rb', line 348 def skip(n) UDis86.ud_input_skip(self,n) return self end |
#syntax=(new_syntax) ⇒ Symbol
Sets the assembly syntax that the disassembler will emit.
276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/ffi/udis86/ud.rb', line 276 def syntax=(new_syntax) new_syntax = new_syntax.to_s.downcase.to_sym func_name = UDis86::SYNTAX[new_syntax] unless func_name raise(ArgumentError,"unknown syntax name #{new_syntax}",caller) end UDis86.ud_set_syntax(self,UDis86.method(func_name)) return new_syntax end |
#to_asm ⇒ String Also known as: to_s
Returns the assembly syntax for the last disassembled instruction.
459 460 461 |
# File 'lib/ffi/udis86/ud.rb', line 459 def to_asm UDis86.ud_insn_asm(self) end |
#to_hex ⇒ String
Returns the hexadecimal representation of the disassembled instruction.
470 471 472 |
# File 'lib/ffi/udis86/ud.rb', line 470 def to_hex UDis86.ud_insn_hex(self) end |
#vendor ⇒ Symbol
The vendor of whose instructions are to be chosen from during disassembly.
295 296 297 |
# File 'lib/ffi/udis86/ud.rb', line 295 def vendor VENDORS[self[:vendor]] end |