
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
160 lines
3.5 KiB
Go
160 lines
3.5 KiB
Go
package native
|
|
|
|
// #include "threads_darwin.h"
|
|
// #include "proc_darwin.h"
|
|
import "C"
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
|
|
sys "golang.org/x/sys/unix"
|
|
|
|
"github.com/derekparker/delve/pkg/proc"
|
|
)
|
|
|
|
// 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")
|
|
|
|
// 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) {
|
|
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 *Thread) 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 *Thread) resume() error {
|
|
t.running = true
|
|
// 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
|
|
}
|
|
|
|
func (t *Thread) Blocked() bool {
|
|
// 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)
|
|
if fn == nil {
|
|
return false
|
|
}
|
|
switch fn.Name {
|
|
case "runtime.kevent", "runtime.mach_semaphore_wait", "runtime.usleep", "runtime.mach_semaphore_timedwait":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
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 *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
|
|
}
|
|
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
|
|
}
|