2015-06-12 19:49:23 +00:00
|
|
|
package proc
|
2015-01-14 02:37:10 +00:00
|
|
|
|
|
|
|
// #include "threads_darwin.h"
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OSSpecificDetails struct {
|
|
|
|
thread_act C.thread_act_t
|
2015-03-28 01:12:07 +00:00
|
|
|
registers C.x86_thread_state64_t
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 19:51:23 +00:00
|
|
|
func (t *Thread) Halt() error {
|
2015-01-14 02:37:10 +00:00
|
|
|
var kret C.kern_return_t
|
|
|
|
kret = C.thread_suspend(t.os.thread_act)
|
|
|
|
if kret != C.KERN_SUCCESS {
|
2015-04-19 22:11:33 +00:00
|
|
|
return fmt.Errorf("could not suspend thread %d", t.Id)
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
2015-06-24 14:29:16 +00:00
|
|
|
t.running = false
|
2015-01-14 02:37:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:51:23 +00:00
|
|
|
func (t *Thread) singleStep() error {
|
2015-02-28 16:44:36 +00:00
|
|
|
kret := C.single_step(t.os.thread_act)
|
|
|
|
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-05-27 17:16:45 +00:00
|
|
|
t.dbp.trapWait(0)
|
2015-02-28 16:44:36 +00:00
|
|
|
kret = C.clear_trap_flag(t.os.thread_act)
|
|
|
|
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
|
|
|
|
t.dbp.execPtraceFunc(func() { err = PtraceCont(t.dbp.Pid, 0) })
|
|
|
|
if err == nil {
|
2015-01-14 02:37:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-08 00:21:10 +00:00
|
|
|
kret := C.resume_thread(t.os.thread_act)
|
|
|
|
if kret != C.KERN_SUCCESS {
|
2015-05-04 22:31:13 +00:00
|
|
|
return fmt.Errorf("could not continue thread")
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:51:23 +00:00
|
|
|
func (t *Thread) blocked() bool {
|
2015-01-14 02:37:10 +00:00
|
|
|
// TODO(dp) cache the func pc to remove this lookup
|
2015-04-23 15:40:33 +00:00
|
|
|
pc, _ := t.PC()
|
2015-05-27 17:16:45 +00:00
|
|
|
fn := t.dbp.goSymTable.PCToFunc(pc)
|
2015-07-14 14:51:52 +00:00
|
|
|
if fn == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch fn.Name {
|
|
|
|
case "runtime.kevent", "runtime.mach_semaphore_wait", "runtime.usleep":
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:43:03 +00:00
|
|
|
func (thread *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 (
|
|
|
|
vm_data = unsafe.Pointer(&data[0])
|
|
|
|
vm_addr = C.mach_vm_address_t(addr)
|
|
|
|
length = C.mach_msg_type_number_t(len(data))
|
|
|
|
)
|
2015-05-27 17:16:45 +00:00
|
|
|
if ret := C.write_memory(thread.dbp.os.task, vm_addr, vm_data, 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
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:43:03 +00:00
|
|
|
func (thread *Thread) readMemory(addr uintptr, size int) ([]byte, error) {
|
|
|
|
if size == 0 {
|
|
|
|
return nil, nil
|
2015-06-11 20:17:56 +00:00
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
var (
|
2015-08-02 02:43:03 +00:00
|
|
|
buf = make([]byte, size)
|
|
|
|
vm_data = unsafe.Pointer(&buf[0])
|
2015-01-14 02:37:10 +00:00
|
|
|
vm_addr = C.mach_vm_address_t(addr)
|
2015-08-02 02:43:03 +00:00
|
|
|
length = C.mach_msg_type_number_t(size)
|
2015-01-14 02:37:10 +00:00
|
|
|
)
|
|
|
|
|
2015-05-27 17:16:45 +00:00
|
|
|
ret := C.read_memory(thread.dbp.os.task, vm_addr, vm_data, 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
|
|
|
}
|