proctl_darwin.go 4.9 KB
Newer Older
D
Derek Parker 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
package proctl

// #include "proctl_darwin.h"
import "C"
import (
	"debug/gosym"
	"debug/macho"
	"fmt"
	"os"
	"sync"
	"unsafe"

	"github.com/derekparker/delve/dwarf/frame"
D
Derek Parker 已提交
14
	"github.com/derekparker/delve/dwarf/line"
D
Derek Parker 已提交
15 16 17 18
	sys "golang.org/x/sys/unix"
)

type OSProcessDetails struct {
19 20 21 22
	task             C.mach_port_name_t
	portSet          C.mach_port_t
	exceptionPort    C.mach_port_t
	notificationPort C.mach_port_t
D
Derek Parker 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
}

func (dbp *DebuggedProcess) Halt() (err error) {
	for _, th := range dbp.Threads {
		err := th.Halt()
		if err != nil {
			return err
		}
	}
	return nil
}

func (dbp *DebuggedProcess) updateThreadList() error {
	var (
		err   error
		kret  C.kern_return_t
		count = C.thread_count(C.task_t(dbp.os.task))
	)
D
Derek Parker 已提交
41 42 43 44
	if count == -1 {
		return fmt.Errorf("could not get thread count")
	}
	list := make([]uint32, count)
D
Derek Parker 已提交
45

D
Derek Parker 已提交
46
	// TODO(dp) might be better to malloc mem in C and then free it here
D
Derek Parker 已提交
47 48 49 50 51 52 53 54 55 56
	// instead of getting count above and passing in a slice
	kret = C.get_threads(C.task_t(dbp.os.task), unsafe.Pointer(&list[0]))
	if kret != C.KERN_SUCCESS {
		return fmt.Errorf("could not get thread list")
	}
	if count < 0 {
		return fmt.Errorf("could not get thread list")
	}

	for _, port := range list {
D
Derek Parker 已提交
57 58 59 60 61
		if _, ok := dbp.Threads[int(port)]; !ok {
			_, err = dbp.addThread(int(port), false)
			if err != nil {
				return err
			}
D
Derek Parker 已提交
62 63 64 65 66 67 68
		}
	}

	return nil
}

func (dbp *DebuggedProcess) addThread(port int, attach bool) (*ThreadContext, error) {
D
Derek Parker 已提交
69 70 71 72
	if thread, ok := dbp.Threads[port]; ok {
		return thread, nil
	}
	fmt.Println("new thread spawned", port)
D
Derek Parker 已提交
73 74 75 76 77 78 79
	thread := &ThreadContext{
		Id:      port,
		Process: dbp,
		os:      new(OSSpecificDetails),
	}
	dbp.Threads[port] = thread
	thread.os.thread_act = C.thread_act_t(port)
D
Derek Parker 已提交
80 81 82
	if dbp.CurrentThread == nil {
		dbp.CurrentThread = thread
	}
D
Derek Parker 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	return thread, nil
}

func (dbp *DebuggedProcess) parseDebugFrame(exe *macho.File, wg *sync.WaitGroup) {
	defer wg.Done()

	if sec := exe.Section("__debug_frame"); sec != nil {
		debugFrame, err := exe.Section("__debug_frame").Data()
		if err != nil {
			fmt.Println("could not get __debug_frame section", err)
			os.Exit(1)
		}
		dbp.FrameEntries = frame.Parse(debugFrame)
	} else {
		fmt.Println("could not find __debug_frame section in binary")
		os.Exit(1)
	}
}

func (dbp *DebuggedProcess) obtainGoSymbols(exe *macho.File, wg *sync.WaitGroup) {
	defer wg.Done()

	var (
		symdat  []byte
		pclndat []byte
		err     error
	)

	if sec := exe.Section("__gosymtab"); sec != nil {
		symdat, err = sec.Data()
		if err != nil {
			fmt.Println("could not get .gosymtab section", err)
			os.Exit(1)
		}
	}

	if sec := exe.Section("__gopclntab"); sec != nil {
		pclndat, err = sec.Data()
		if err != nil {
			fmt.Println("could not get .gopclntab section", err)
			os.Exit(1)
		}
	}

	pcln := gosym.NewLineTable(pclndat, exe.Section("__text").Addr)
	tab, err := gosym.NewTable(symdat, pcln)
	if err != nil {
		fmt.Println("could not get initialize line table", err)
		os.Exit(1)
	}

	dbp.GoSymTable = tab
}

D
Derek Parker 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
func (dbp *DebuggedProcess) parseDebugLineInfo(exe *macho.File, wg *sync.WaitGroup) {
	defer wg.Done()

	if sec := exe.Section("__debug_line"); sec != nil {
		debugLine, err := exe.Section("__debug_line").Data()
		if err != nil {
			fmt.Println("could not get __debug_line section", err)
			os.Exit(1)
		}
		dbp.LineInfo = line.Parse(debugLine)
	} else {
		fmt.Println("could not find __debug_line section in binary")
		os.Exit(1)
	}
}

D
Derek Parker 已提交
153
func (dbp *DebuggedProcess) findExecutable() (*macho.File, error) {
D
Derek Parker 已提交
154 155 156 157
	ret := C.acquire_mach_task(C.int(dbp.Pid), &dbp.os.task, &dbp.os.portSet, &dbp.os.exceptionPort, &dbp.os.notificationPort)
	if ret != C.KERN_SUCCESS {
		return nil, fmt.Errorf("could not acquire mach task %d", ret)
	}
D
Derek Parker 已提交
158 159 160 161
	pathptr, err := C.find_executable(C.int(dbp.Pid))
	if err != nil {
		return nil, err
	}
D
Derek Parker 已提交
162 163 164 165 166 167 168 169 170 171
	exe, err := macho.Open(C.GoString(pathptr))
	if err != nil {
		return nil, err
	}
	data, err := exe.DWARF()
	if err != nil {
		return nil, err
	}
	dbp.Dwarf = data
	return exe, nil
D
Derek Parker 已提交
172 173
}

174
func trapWait(dbp *DebuggedProcess, pid int) (*ThreadContext, *BreakPoint, error) {
175
	port := C.mach_port_wait(dbp.os.portSet)
176

177 178
	switch port {
	case dbp.os.notificationPort:
179 180
		_, status, err := wait(dbp.Pid, 0)
		if err != nil {
181
			return nil, nil, err
182
		}
183
		dbp.exited = true
184
		return nil, nil, ProcessExitedError{Pid: dbp.Pid, Status: status.ExitStatus()}
185 186 187 188 189 190 191
	case C.MACH_RCV_INTERRUPTED:
		if !dbp.halt {
			// Call trapWait again, it seems
			// MACH_RCV_INTERRUPTED is emitted before
			// process natural death _sometimes_.
			return trapWait(dbp, pid)
		}
192
		return nil, nil, ManualStopError{}
193
	case 0:
194
		return nil, nil, fmt.Errorf("error while waiting for task")
D
Derek Parker 已提交
195 196
	}

197 198
	// Since we cannot be notified of new threads on OS X
	// this is as good a time as any to check for them.
D
Derek Parker 已提交
199
	dbp.updateThreadList()
D
Derek Parker 已提交
200
	return dbp.handleBreakpointOnThread(int(port))
D
Derek Parker 已提交
201 202 203 204 205 206 207
}

func wait(pid, options int) (int, *sys.WaitStatus, error) {
	var status sys.WaitStatus
	wpid, err := sys.Wait4(pid, &status, options, nil)
	return wpid, &status, err
}