delve/proc/threads_darwin.go

124 lines
2.7 KiB
Go
Raw Normal View History

2015-06-12 19:49:23 +00:00
package proc
2015-01-14 02:37:10 +00:00
// #include "threads_darwin.h"
// #include "proc_darwin.h"
2015-01-14 02:37:10 +00:00
import "C"
import (
"fmt"
"unsafe"
2016-01-15 05:26:54 +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
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.
var ErrContinueThread = fmt.Errorf("could not continue thread")
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 {
errStr := C.GoString(C.mach_error_string(C.mach_error_t(kret)))
2016-01-10 08:57:52 +00:00
err = fmt.Errorf("could not suspend thread %d %s", t.ID, errStr)
return
2015-01-14 02:37:10 +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)
if kret != C.KERN_SUCCESS {
2015-05-04 22:31:13 +00:00
return fmt.Errorf("could not single step")
}
for {
port := C.mach_port_wait(t.dbp.os.portSet, C.int(0))
2016-01-10 08:57:52 +00:00
if port == C.mach_port_t(t.ID) {
break
}
}
2016-01-10 08:57:52 +00:00
kret = C.clear_trap_flag(t.os.threadAct)
if kret != C.KERN_SUCCESS {
2015-05-04 22:31:13 +00:00
return fmt.Errorf("could not clear CPU trap flag")
}
2015-01-14 02:37:10 +00:00
return nil
}
2015-06-12 19:51:23 +00:00
func (t *Thread) resume() error {
t.running = true
2015-01-14 02:37:10 +00:00
// TODO(dp) set flag for ptrace stops
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
}
2016-01-10 08:57:52 +00:00
kret := C.resume_thread(t.os.threadAct)
if kret != C.KERN_SUCCESS {
return ErrContinueThread
2015-01-14 02:37:10 +00:00
}
return nil
}
2016-01-10 08:57:52 +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
2016-01-10 08:57:52 +00:00
pc, err := t.PC()
if err != nil {
return false
}
2016-01-10 08:57:52 +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
}
}
2016-01-10 08:57:52 +00:00
func (t *Thread) stopped() bool {
return C.thread_blocked(t.os.threadAct) > C.int(0)
}
2016-01-10 08:57:52 +00:00
func (t *Thread) writeMemory(addr uintptr, data []byte) (int, error) {
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-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
}