2015-06-12 19:49:23 +00:00
|
|
|
package proc
|
2015-01-14 02:37:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sys "golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2016-01-15 05:26:54 +00:00
|
|
|
type WaitStatus sys.WaitStatus
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// OSSpecificDetails hold Linux specific
|
|
|
|
// process details.
|
2015-03-28 01:12:07 +00:00
|
|
|
type OSSpecificDetails struct {
|
|
|
|
registers sys.PtraceRegs
|
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
|
2015-08-20 15:06:33 +00:00
|
|
|
func (t *Thread) halt() (err error) {
|
2016-01-10 08:57:52 +00:00
|
|
|
err = sys.Tgkill(t.dbp.Pid, t.ID, sys.SIGSTOP)
|
2015-01-14 02:37:10 +00:00
|
|
|
if err != nil {
|
2016-01-10 08:57:52 +00:00
|
|
|
err = fmt.Errorf("halt err %s on thread %d", err, t.ID)
|
2015-08-20 14:28:11 +00:00
|
|
|
return
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
2016-01-10 08:57:52 +00:00
|
|
|
_, _, err = t.dbp.wait(t.ID, 0)
|
2015-01-14 02:37:10 +00:00
|
|
|
if err != nil {
|
2016-01-10 08:57:52 +00:00
|
|
|
err = fmt.Errorf("wait err %s on thread %d", err, t.ID)
|
2015-08-20 14:28:11 +00:00
|
|
|
return
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
2015-08-20 14:28:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) stopped() bool {
|
|
|
|
state := status(t.ID, t.dbp.os.comm)
|
|
|
|
return state == StatusTraceStop
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 15:14:07 +00:00
|
|
|
func (t *Thread) resume() error {
|
|
|
|
return t.resumeWithSig(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Thread) resumeWithSig(sig int) (err error) {
|
2015-06-24 14:29:16 +00:00
|
|
|
t.running = true
|
2016-02-15 15:14:07 +00:00
|
|
|
t.dbp.execPtraceFunc(func() { err = PtraceCont(t.ID, sig) })
|
2015-06-13 04:47:30 +00:00
|
|
|
return
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 04:47:30 +00:00
|
|
|
func (t *Thread) singleStep() (err error) {
|
2015-11-19 15:19:42 +00:00
|
|
|
for {
|
2016-01-10 08:57:52 +00:00
|
|
|
t.dbp.execPtraceFunc(func() { err = sys.PtraceSingleStep(t.ID) })
|
2015-11-19 15:19:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-10 08:57:52 +00:00
|
|
|
wpid, status, err := t.dbp.wait(t.ID, 0)
|
2015-11-19 15:19:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-14 21:26:06 +00:00
|
|
|
if (status == nil || status.Exited()) && wpid == t.dbp.Pid {
|
|
|
|
t.dbp.postExit()
|
|
|
|
rs := 0
|
|
|
|
if status != nil {
|
|
|
|
rs = status.ExitStatus()
|
|
|
|
}
|
|
|
|
return ProcessExitedError{Pid: t.dbp.Pid, Status: rs}
|
|
|
|
}
|
2016-01-10 08:57:52 +00:00
|
|
|
if wpid == t.ID && status.StopSignal() == sys.SIGTRAP {
|
2015-11-19 15:19:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:51:23 +00:00
|
|
|
func (t *Thread) blocked() bool {
|
2015-04-23 15:40:33 +00:00
|
|
|
pc, _ := t.PC()
|
2015-05-27 17:16:45 +00:00
|
|
|
fn := t.dbp.goSymTable.PCToFunc(pc)
|
2015-02-28 14:05:37 +00:00
|
|
|
if fn != nil && ((fn.Name == "runtime.futex") || (fn.Name == "runtime.usleep") || (fn.Name == "runtime.clone")) {
|
2015-02-27 23:11:13 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) saveRegisters() (Registers, error) {
|
2015-06-13 04:47:30 +00:00
|
|
|
var err error
|
2016-01-10 08:57:52 +00:00
|
|
|
t.dbp.execPtraceFunc(func() { err = sys.PtraceGetRegs(t.ID, &t.os.registers) })
|
2015-06-13 04:47:30 +00:00
|
|
|
if err != nil {
|
2015-05-04 22:31:13 +00:00
|
|
|
return nil, fmt.Errorf("could not save register contents")
|
2015-05-01 13:34:09 +00:00
|
|
|
}
|
2016-01-10 08:57:52 +00:00
|
|
|
return &Regs{&t.os.registers}, nil
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) restoreRegisters() (err error) {
|
|
|
|
t.dbp.execPtraceFunc(func() { err = sys.PtraceSetRegs(t.ID, &t.os.registers) })
|
2015-06-13 04:47:30 +00:00
|
|
|
return
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
2015-03-28 01:12:07 +00:00
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) writeMemory(addr uintptr, data []byte) (written int, err error) {
|
2015-06-11 20:17:56 +00:00
|
|
|
if len(data) == 0 {
|
2015-06-13 04:47:30 +00:00
|
|
|
return
|
2015-06-11 20:17:56 +00:00
|
|
|
}
|
2016-01-10 08:57:52 +00:00
|
|
|
t.dbp.execPtraceFunc(func() { written, err = sys.PtracePokeData(t.ID, addr, data) })
|
2015-06-13 04:47:30 +00:00
|
|
|
return
|
2015-03-28 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
func (t *Thread) readMemory(addr uintptr, size int) (data []byte, err error) {
|
2015-08-02 02:43:03 +00:00
|
|
|
if size == 0 {
|
2015-08-02 04:14:06 +00:00
|
|
|
return
|
2015-06-11 20:17:56 +00:00
|
|
|
}
|
2015-08-02 02:43:03 +00:00
|
|
|
data = make([]byte, size)
|
2016-01-10 08:57:52 +00:00
|
|
|
t.dbp.execPtraceFunc(func() { _, err = sys.PtracePeekData(t.ID, addr, data) })
|
2015-06-13 04:47:30 +00:00
|
|
|
return
|
2015-03-28 01:12:07 +00:00
|
|
|
}
|