Class: ASMREPL::MacOS::Tracer

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

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ Tracer

Returns a new instance of Tracer.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/asmrepl/macos.rb', line 88

def initialize pid
  @pid = pid
  @target = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)

  unless MacOS.task_for_pid(MacOS.mach_task_self, pid, @target.ref).zero?
    raise "Couldn't get task pid. Did you run with sudo?"
  end

  @thread_list = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  thread_count = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)

  raise unless MacOS.task_threads(@target, @thread_list.ref, thread_count).zero?

  @thread = Fiddle::Pointer.new(@thread_list[0, Fiddle::SIZEOF_VOIDP].unpack1("l!"))
end

Instance Method Details

#continueObject



143
144
145
146
147
# File 'lib/asmrepl/macos.rb', line 143

def continue
  unless MacOS.ptrace(MacOS::PT_CONTINUE, @pid, 1, 0).zero?
    raise
  end
end

#stateObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/asmrepl/macos.rb', line 108

def state
  3.times do
    # Probably should use this for something
    # count = thread_count[0]

    # I can't remember what header I found this in, but it's from a macOS header
    # :sweat-smile:
    x86_THREAD_STATE64_COUNT = ThreadState.sizeof / Fiddle::SIZEOF_INT

    # Same here
    x86_THREAD_STATE64 = 4

    state_count = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT64_T)
    state_count[0, Fiddle::SIZEOF_INT64_T] = [x86_THREAD_STATE64_COUNT].pack("l!")

    state = ThreadState.malloc
    if MacOS.thread_get_state(@thread, x86_THREAD_STATE64, state, state_count).zero?
      return state
    end
  end

  raise "Couldn't get CPU state"
end

#state=(new_state) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/asmrepl/macos.rb', line 132

def state= new_state
  # I can't remember what header I found this in, but it's from a macOS header
  # :sweat-smile:
  x86_THREAD_STATE64_COUNT = ThreadState.sizeof / Fiddle::SIZEOF_INT

  # Same here
  x86_THREAD_STATE64 = 4

  raise unless MacOS.thread_set_state(@thread, x86_THREAD_STATE64, new_state, x86_THREAD_STATE64_COUNT).zero?
end

#waitObject



104
105
106
# File 'lib/asmrepl/macos.rb', line 104

def wait
  Process.waitpid @pid
end