
- use PT_SUSPEND/PT_RESUME to control running threads in resume/stop/singleStep - change manual stop signal from SIGTRAP to SIGSTOP to make manual stop handling simpler - change (*nativeProcess).trapWaitInternal to suspend newly created threads when we are stepping a thread - change (*nativeProcess).trapWaitInternal to handle some unhandled stop events - remove misleading (*nativeProcess).waitFast which does not do anything different from the normal wait variant - rewrite (*nativeProcess).stop to only set breakpoints for threads of which we have received SIGTRAP - rewrite (*nativeThread).singleStep to actually execute a single instruction and to properly route signals
166 lines
4.0 KiB
Go
166 lines
4.0 KiB
Go
//go:build darwin && macnative
|
|
// +build darwin,macnative
|
|
|
|
package native
|
|
|
|
// #include "threads_darwin.h"
|
|
// #include "proc_darwin.h"
|
|
import "C"
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"unsafe"
|
|
|
|
sys "golang.org/x/sys/unix"
|
|
|
|
"github.com/go-delve/delve/pkg/proc"
|
|
"github.com/go-delve/delve/pkg/proc/amd64util"
|
|
)
|
|
|
|
// waitStatus is a synonym for the platform-specific WaitStatus
|
|
type waitStatus sys.WaitStatus
|
|
|
|
// osSpecificDetails holds information specific to the OSX/Darwin
|
|
// operating system / kernel.
|
|
type osSpecificDetails struct {
|
|
threadAct C.thread_act_t
|
|
registers C.x86_thread_state64_t
|
|
exists bool
|
|
}
|
|
|
|
// ErrContinueThread is the error returned when a thread could not
|
|
// be continued.
|
|
var ErrContinueThread = fmt.Errorf("could not continue thread")
|
|
|
|
func (t *nativeThread) stop() (err error) {
|
|
kret := C.thread_suspend(t.os.threadAct)
|
|
if kret != C.KERN_SUCCESS {
|
|
errStr := C.GoString(C.mach_error_string(C.mach_error_t(kret)))
|
|
// 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
|
|
}
|
|
|
|
if _, ok := t.dbp.threads[t.ID]; ok {
|
|
err = fmt.Errorf("could not suspend thread %d %s", t.ID, errStr)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (t *nativeThread) singleStep() error {
|
|
kret := C.single_step(t.os.threadAct)
|
|
if kret != C.KERN_SUCCESS {
|
|
return fmt.Errorf("could not single step")
|
|
}
|
|
for {
|
|
twthread, err := t.dbp.trapWait(t.dbp.pid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if twthread.ID == t.ID {
|
|
break
|
|
}
|
|
}
|
|
|
|
kret = C.clear_trap_flag(t.os.threadAct)
|
|
if kret != C.KERN_SUCCESS {
|
|
return fmt.Errorf("could not clear CPU trap flag")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *nativeThread) resume() error {
|
|
// TODO(dp) set flag for ptrace stops
|
|
var err error
|
|
t.dbp.execPtraceFunc(func() { err = ptraceCont(t.dbp.pid, 0) })
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
kret := C.resume_thread(t.os.threadAct)
|
|
if kret != C.KERN_SUCCESS {
|
|
return ErrContinueThread
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Stopped returns whether the thread is stopped at
|
|
// the operating system level.
|
|
func (t *nativeThread) Stopped() bool {
|
|
return C.thread_blocked(t.os.threadAct) > C.int(0)
|
|
}
|
|
|
|
func (t *nativeThread) WriteMemory(addr uint64, data []byte) (int, error) {
|
|
if t.dbp.exited {
|
|
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
|
|
}
|
|
if len(data) == 0 {
|
|
return 0, nil
|
|
}
|
|
var (
|
|
vmData = unsafe.Pointer(&data[0])
|
|
vmAddr = C.mach_vm_address_t(addr)
|
|
length = C.mach_msg_type_number_t(len(data))
|
|
)
|
|
if ret := C.write_memory(t.dbp.os.task, vmAddr, vmData, length); ret < 0 {
|
|
return 0, fmt.Errorf("could not write memory")
|
|
}
|
|
return len(data), nil
|
|
}
|
|
|
|
func (t *nativeThread) ReadMemory(buf []byte, addr uint64) (int, error) {
|
|
if t.dbp.exited {
|
|
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
|
|
}
|
|
if len(buf) == 0 {
|
|
return 0, nil
|
|
}
|
|
var (
|
|
vmData = unsafe.Pointer(&buf[0])
|
|
vmAddr = C.mach_vm_address_t(addr)
|
|
length = C.mach_msg_type_number_t(len(buf))
|
|
)
|
|
|
|
ret := C.read_memory(t.dbp.os.task, vmAddr, vmData, length)
|
|
if ret < 0 {
|
|
return 0, fmt.Errorf("could not read memory")
|
|
}
|
|
return len(buf), nil
|
|
}
|
|
|
|
func (t *nativeThread) restoreRegisters(sr proc.Registers) error {
|
|
return errors.New("not implemented")
|
|
}
|
|
|
|
func (t *nativeThread) withDebugRegisters(f func(*amd64util.DebugRegisters) error) error {
|
|
return proc.ErrHWBreakUnsupported
|
|
}
|
|
|
|
// SoftExc returns true if this thread received a software exception during the last resume.
|
|
func (t *nativeThread) SoftExc() bool {
|
|
return false
|
|
}
|
|
|
|
// Continue the execution of this thread.
|
|
//
|
|
// If we are currently at a breakpoint, we'll clear it
|
|
// first and then resume execution. Thread will continue until
|
|
// it hits a breakpoint or is signaled.
|
|
func (t *nativeThread) Continue() error {
|
|
pc, err := t.PC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Check whether we are stopped at a breakpoint, and
|
|
// if so, single step over it before continuing.
|
|
if _, ok := t.dbp.FindBreakpoint(pc, false); ok {
|
|
if err := t.StepInstruction(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return t.resume()
|
|
}
|