delve/pkg/proc/native/threads_darwin.go

160 lines
3.5 KiB
Go
Raw Normal View History

package native
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"
2016-01-24 16:30:23 +00:00
"unsafe"
sys "golang.org/x/sys/unix"
"github.com/derekparker/delve/pkg/proc"
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
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.
var ErrContinueThread = fmt.Errorf("could not continue thread")
proc/native: fix race condition between Halt and process death (linux) If a breakpoint is hit close to process death on a thread that isn't the group leader the process could die while we are trying to stop it. This can be easily reproduced by having the goroutine that's executing main.main (which will almost always run on the thread group leader) wait for a second goroutine before exiting, then setting a breakpoint on the second goroutine and stepping through it (see TestIssue1101 in proc_test.go). When stepping over the return instruction of main.f the deferred wg.Done() call will be executed which will cause the main goroutine to resume and proceed to exit. Both the temporary breakpoint on wg.Done and the temporary breakpoint on the return address of main.f will be in close proximity to main.main calling os.Exit() and causing the death of the thread group leader. Under these circumstances the call to native.(*Thread).waitFast in native.(*Thread).halt can hang forever due to a bug similar to https://sourceware.org/bugzilla/show_bug.cgi?id=12702 (see comment in native.(*Thread).wait for an explanation). Replacing waitFast with a normal wait work in most circumstances, however, besides the performance hit, it looks like in this circumstances trapWait sometimes receives a spurious SIGTRAP on the dying group leader which would cause the subsequent call to wait in halt to accidentally reap the process without noting that it did exit. Instead this patch removes the call to wait from halt and instead calls trapWait in a loop in setCurrentBreakpoints until all threads are set to running=false. This is also a better fix than the workaround to ESRCH error while setting current breakpoints implemented in 94b50d. Fixes #1101
2018-01-29 10:07:38 +00:00
// Halt stops this thread from executing.
func (thread *Thread) Halt() (err error) {
defer func() {
if err == nil {
thread.running = false
}
}()
if thread.Stopped() {
return
}
err = thread.halt()
return
}
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)))
// 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
}
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 {
twthread, err := t.dbp.trapWait(t.dbp.pid)
if err != nil {
return err
}
if twthread.ID == 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
}
func (t *Thread) Blocked() bool {
2015-01-14 02:37:10 +00:00
// TODO(dp) cache the func pc to remove this lookup
regs, err := t.Registers(false)
if err != nil {
return false
}
pc := regs.PC()
fn := t.BinInfo().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)
}
func (t *Thread) WriteMemory(addr uintptr, data []byte) (int, error) {
if t.dbp.exited {
return 0, proc.ProcessExitedError{Pid: t.dbp.pid}
}
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
}
func (t *Thread) ReadMemory(buf []byte, addr uintptr) (int, error) {
if t.dbp.exited {
return 0, proc.ProcessExitedError{Pid: t.dbp.pid}
}
if len(buf) == 0 {
return 0, nil
}
2015-01-14 02:37:10 +00:00
var (
2016-01-10 08:57:52 +00:00
vmData = unsafe.Pointer(&buf[0])
vmAddr = C.mach_vm_address_t(addr)
length = C.mach_msg_type_number_t(len(buf))
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 {
return 0, fmt.Errorf("could not read memory")
2015-01-14 02:37:10 +00:00
}
return len(buf), nil
2015-01-14 02:37:10 +00:00
}