Skip to content

Commit

Permalink
Stop target process immediately after execve.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebfe committed Nov 13, 2014
1 parent 2ea8ff1 commit 478b7a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
11 changes: 8 additions & 3 deletions cmd/dlv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,19 @@ func main() {
start := func(name string) *proctl.DebuggedProcess {
proc := exec.Command(name)
proc.Stdout = os.Stdout
proc.SysProcAttr = &syscall.SysProcAttr{Ptrace: true}

err = proc.Start()
if err != nil {
die(1, "Could not start process:", err)
}
syscall.Kill(proc.Process.Pid, syscall.SIGSTOP)
var status syscall.WaitStatus
_, err := syscall.Wait4(proc.Process.Pid, &status, syscall.WALL, nil)
if err != nil {
die(1, "Waiting for target execve failed:", err)
}

dbgproc, err = proctl.NewDebugProcess(proc.Process.Pid)
dbgproc, err = proctl.NewDebugProcess(proc.Process.Pid, false)
if err != nil {
die(1, "Could not start debugging process:", err)
}
Expand All @@ -87,7 +92,7 @@ func main() {

dbgproc = start("./" + debugname)
case pid != 0:
dbgproc, err = proctl.NewDebugProcess(pid)
dbgproc, err = proctl.NewDebugProcess(pid, true)
if err != nil {
die(1, "Could not start debugging process:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func WithTestProcess(name string, t *testing.T, fn testfunc) {
}
defer os.Remove("./" + base)

p, err := proctl.AttachBinary("./" + base)
p, err := proctl.AttachBinary("./" + base, true)
if err != nil {
t.Fatal("NewDebugProcess():", err)
}
Expand Down
20 changes: 14 additions & 6 deletions proctl/proctl_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func AttachBinary(name string) (*DebuggedProcess, error) {
return nil, err
}

dbgproc, err := NewDebugProcess(proc.Process.Pid)
dbgproc, err := NewDebugProcess(proc.Process.Pid, true)
if err != nil {
return nil, err
}
Expand All @@ -98,18 +98,26 @@ func AttachBinary(name string) (*DebuggedProcess, error) {
}

// Returns a new DebuggedProcess struct with sensible defaults.
func NewDebugProcess(pid int) (*DebuggedProcess, error) {
func NewDebugProcess(pid int, attach bool) (*DebuggedProcess, error) {
dbp := DebuggedProcess{
Pid: pid,
Threads: make(map[int]*ThreadContext),
BreakPoints: make(map[uint64]*BreakPoint),
}

thread, err := dbp.AttachThread(pid)
if err != nil {
return nil, err
if attach {
thread, err := dbp.AttachThread(pid)
if err != nil {
return nil, err
}
dbp.CurrentThread = thread
} else {
thread, err := dbp.addThread(pid)
if err != nil {
return nil, err
}
dbp.CurrentThread = thread
}
dbp.CurrentThread = thread

proc, err := os.FindProcess(pid)
if err != nil {
Expand Down

0 comments on commit 478b7a8

Please sign in to comment.