2015-06-12 19:49:23 +00:00
|
|
|
package proc
|
2015-01-14 02:37:10 +00:00
|
|
|
|
|
|
|
// #include "threads_darwin.h"
|
2015-11-10 09:49:47 +00:00
|
|
|
// #include "proc_darwin.h"
|
2015-01-14 02:37:10 +00:00
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-24 16:30:23 +00:00
|
|
|
"unsafe"
|
2017-02-09 08:13:12 +00:00
|
|
|
|
|
|
|
sys "golang.org/x/sys/unix"
|
2015-01-14 02:37:10 +00:00
|
|
|
)
|
|
|
|
|
2016-01-15 05:26:54 +00:00
|
|
|
// WaitStatus is a synonym for the platform-specific WaitStatus
|
|
|
|
type WaitStatus sys.WaitStatus
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// OSSpecificDetails holds information specific to the OSX/Darwin
|
|
|
|
// operating system / kernel.
|
2015-01-14 02:37:10 +00:00
|
|
|
type OSSpecificDetails struct {
|
2016-01-10 08:57:52 +00:00
|
|
|
threadAct C.thread_act_t
|
|
|
|
registers C.x86_thread_state64_t
|
2016-11-10 10:20:08 +00:00
|
|
|
exists bool
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// ErrContinueThread is the error returned when a thread could not
|
|
|
|
// be continued.
|
2015-11-10 09:49:47 +00:00
|
|
|
var ErrContinueThread = fmt.Errorf("could not continue thread")
|
|
|
|
|
2015-08-20 15:06:33 +00:00
|
|
|
func (t *Thread) halt() (err error) {
|
2016-01-10 08:57:52 +00:00
|
|
|
kret := C.thread_suspend(t.os.threadAct)
|
2015-01-14 02:37:10 +00:00
|
|
|
if kret != C.KERN_SUCCESS {
|
2015-08-20 14:28:11 +00:00
|
|
|
errStr := C.GoString(C.mach_error_string(C.mach_error_t(kret)))
|
2016-11-10 10:20:08 +00:00
|
|
|
// check that the thread still exists before complaining
|
|
|
|
err2 := t.dbp.updateThreadList()
|
|
|
|
if err2 != nil {
|
|
|
|
err = fmt.Errorf("could not suspend thread %d %s (additionally could not update thread list: %v)", t.ID, errStr, err2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-09 08:13:12 +00:00
|
|
|
if _, ok := t.dbp.threads[t.ID]; ok {
|
2016-11-10 10:20:08 +00:00
|
|
|
err = fmt.Errorf("could not suspend thread %d %s", t.ID, errStr)
|
|
|
|
return
|
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
2015-08-20 14:28:11 +00:00
|
|
|
return
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 19:51:23 +00:00
|
|
|
func (t *Thread) singleStep() error {
|
2016-01-10 08:57:52 +00:00
|
|
|
kret := C.single_step(t.os.threadAct)
|
2015-02-28 16:44:36 +00:00
|
|
|
if kret != C.KERN_SUCCESS {
|
2015-05-04 22:31:13 +00:00
|
|
|
return fmt.Errorf("could not single step")
|
2015-02-28 16:44:36 +00:00
|
|
|
}
|
2015-11-10 09:49:47 +00:00
|
|
|
for {
|
2017-02-09 08:13:12 +00:00
|
|
|
twthread, err := t.dbp.trapWait(t.dbp.pid)
|
2016-02-14 21:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if twthread.ID == t.ID {
|
2015-11-10 09:49:47 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
kret = C.clear_trap_flag(t.os.threadAct)
|
2015-02-28 16:44:36 +00:00
|
|
|
if kret != C.KERN_SUCCESS {
|
2015-05-04 22:31:13 +00:00
|
|
|
return fmt.Errorf("could not clear CPU trap flag")
|
2015-02-28 16:44:36 +00:00
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:51:23 +00:00
|
|
|
func (t *Thread) resume() error {
|
2015-06-24 14:29:16 +00:00
|
|
|
t.running = true
|
2015-01-14 02:37:10 +00:00
|
|
|
// TODO(dp) set flag for ptrace stops
|
2015-06-13 04:47:30 +00:00
|
|
|
var err error
|
2017-02-09 08:13:12 +00:00
|
|
|
t.dbp.execPtraceFunc(func() { err = PtraceCont(t.dbp.pid, 0) })
|
2015-06-13 04:47:30 +00:00
|
|
|
if err == nil {
|
2015-01-14 02:37:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-01-10 08:57:52 +00:00
|
|
|
kret := C.resume_thread(t.os.threadAct)
|
2015-03-08 00:21:10 +00:00
|
|
|
if kret != C.KERN_SUCCESS {
|
2015-11-10 09:49:47 +00:00
|
|
|
return ErrContinueThread
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
func threadBlocked(t IThread) bool {
|
2015-01-14 02:37:10 +00:00
|
|
|
// TODO(dp) cache the func pc to remove this lookup
|
2017-02-22 08:35:21 +00:00
|
|
|
regs, err := t.Registers(false)
|
2015-08-20 14:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
pc := regs.PC()
|
|
|
|
fn := t.BinInfo().goSymTable.PCToFunc(pc)
|
2015-07-14 14:51:52 +00:00
|
|
|
if fn == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch fn.Name {
|
2017-03-24 10:10:21 +00:00
|
|
|
case "runtime.kevent", "runtime.mach_semaphore_wait", "runtime.usleep", "runtime.mach_semaphore_timedwait":
|
2015-01-14 02:37:10 +00:00
|
|
|
return true
|
2015-07-14 14:51:52 +00:00
|
|
|
default:
|
|
|
|
return false
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) stopped() bool {
|
|
|
|
return C.thread_blocked(t.os.threadAct) > C.int(0)
|
2015-08-20 14:28:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) writeMemory(addr uintptr, data []byte) (int, error) {
|
2015-06-11 20:17:56 +00:00
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
var (
|
2016-01-10 08:57:52 +00:00
|
|
|
vmData = unsafe.Pointer(&data[0])
|
|
|
|
vmAddr = C.mach_vm_address_t(addr)
|
|
|
|
length = C.mach_msg_type_number_t(len(data))
|
2015-01-14 02:37:10 +00:00
|
|
|
)
|
2016-01-10 08:57:52 +00:00
|
|
|
if ret := C.write_memory(t.dbp.os.task, vmAddr, vmData, length); ret < 0 {
|
2015-05-04 22:31:13 +00:00
|
|
|
return 0, fmt.Errorf("could not write memory")
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) readMemory(addr uintptr, size int) ([]byte, error) {
|
2015-08-02 02:43:03 +00:00
|
|
|
if size == 0 {
|
|
|
|
return nil, nil
|
2015-06-11 20:17:56 +00:00
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
var (
|
2016-01-10 08:57:52 +00:00
|
|
|
buf = make([]byte, size)
|
|
|
|
vmData = unsafe.Pointer(&buf[0])
|
|
|
|
vmAddr = C.mach_vm_address_t(addr)
|
|
|
|
length = C.mach_msg_type_number_t(size)
|
2015-01-14 02:37:10 +00:00
|
|
|
)
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
ret := C.read_memory(t.dbp.os.task, vmAddr, vmData, length)
|
2015-01-14 02:37:10 +00:00
|
|
|
if ret < 0 {
|
2015-08-02 02:43:03 +00:00
|
|
|
return nil, fmt.Errorf("could not read memory")
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
2015-08-02 02:43:03 +00:00
|
|
|
return buf, nil
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|