Implement linux amd64 step
This commit is contained in:
parent
5b7f7d92a5
commit
ea0ff20e16
BIN
fixtures/testprog
Executable file
BIN
fixtures/testprog
Executable file
Binary file not shown.
11
fixtures/testprog.go
Normal file
11
fixtures/testprog.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("Hello, World!")
|
||||
}
|
61
proctl/proctl_linux_amd64.go
Normal file
61
proctl/proctl_linux_amd64.go
Normal file
@ -0,0 +1,61 @@
|
||||
package proctl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type DebuggedProcess struct {
|
||||
Pid int
|
||||
Regs *syscall.PtraceRegs
|
||||
Process *os.Process
|
||||
}
|
||||
|
||||
func NewDebugProcess(pid int) (*DebuggedProcess, error) {
|
||||
err := syscall.PtraceAttach(pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
proc, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
debuggedProc := DebuggedProcess{
|
||||
Pid: pid,
|
||||
Regs: &syscall.PtraceRegs{},
|
||||
Process: proc,
|
||||
}
|
||||
|
||||
_, err = proc.Wait()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &debuggedProc, nil
|
||||
}
|
||||
|
||||
func (dbp *DebuggedProcess) Registers() (*syscall.PtraceRegs, error) {
|
||||
err := syscall.PtraceGetRegs(dbp.Pid, dbp.Regs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Registers():", err)
|
||||
}
|
||||
|
||||
return dbp.Regs, nil
|
||||
}
|
||||
|
||||
func (dbp *DebuggedProcess) Step() error {
|
||||
err := syscall.PtraceSingleStep(dbp.Pid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dbp.Process.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
50
proctl/proctl_test.go
Normal file
50
proctl/proctl_test.go
Normal file
@ -0,0 +1,50 @@
|
||||
package proctl
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func StartTestProcess() (int, error) {
|
||||
cmd := exec.Command("../fixtures/testprog")
|
||||
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return cmd.Process.Pid, nil
|
||||
}
|
||||
|
||||
func TestStep(t *testing.T) {
|
||||
pid, err := StartTestProcess()
|
||||
if err != nil {
|
||||
t.Fatal("Starting test process:", err)
|
||||
}
|
||||
|
||||
p, err := NewDebugProcess(pid)
|
||||
if err != nil {
|
||||
t.Fatal("NewDebugProcess():", err)
|
||||
}
|
||||
|
||||
regs, err := p.Registers()
|
||||
if err != nil {
|
||||
t.Fatal("Registers():", err)
|
||||
}
|
||||
|
||||
rip := regs.PC()
|
||||
|
||||
err = p.Step()
|
||||
if err != nil {
|
||||
t.Fatal("Step():", err)
|
||||
}
|
||||
|
||||
regs, err = p.Registers()
|
||||
if err != nil {
|
||||
t.Fatal("Registers():", err)
|
||||
}
|
||||
|
||||
if rip >= regs.PC() {
|
||||
t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user