
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
174 lines
4.5 KiB
Go
174 lines
4.5 KiB
Go
package native
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/derekparker/delve/pkg/proc"
|
|
)
|
|
|
|
// Thread represents a single thread in the traced process
|
|
// ID represents the thread id or port, Process holds a reference to the
|
|
// Process struct that contains info on the process as
|
|
// a whole, and Status represents the last result of a `wait` call
|
|
// on this thread.
|
|
type Thread struct {
|
|
ID int // Thread ID or mach port
|
|
Status *WaitStatus // Status returned from last wait call
|
|
CurrentBreakpoint proc.BreakpointState // Breakpoint thread is currently stopped at
|
|
|
|
dbp *Process
|
|
singleStepping bool
|
|
running bool
|
|
os *OSSpecificDetails
|
|
}
|
|
|
|
// Continue the execution of this thread.
|
|
//
|
|
// If we are currently at a breakpoint, we'll clear it
|
|
// first and then resume execution. Thread will continue until
|
|
// it hits a breakpoint or is signaled.
|
|
func (thread *Thread) Continue() error {
|
|
pc, err := thread.PC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Check whether we are stopped at a breakpoint, and
|
|
// if so, single step over it before continuing.
|
|
if _, ok := thread.dbp.FindBreakpoint(pc); ok {
|
|
if err := thread.StepInstruction(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return thread.resume()
|
|
}
|
|
|
|
// StepInstruction steps a single instruction.
|
|
//
|
|
// Executes exactly one instruction and then returns.
|
|
// If the thread is at a breakpoint, we first clear it,
|
|
// execute the instruction, and then replace the breakpoint.
|
|
// Otherwise we simply execute the next instruction.
|
|
func (thread *Thread) StepInstruction() (err error) {
|
|
thread.running = true
|
|
thread.singleStepping = true
|
|
defer func() {
|
|
thread.singleStepping = false
|
|
thread.running = false
|
|
}()
|
|
pc, err := thread.PC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
bp, ok := thread.dbp.FindBreakpoint(pc)
|
|
if ok {
|
|
// Clear the breakpoint so that we can continue execution.
|
|
err = thread.ClearBreakpoint(bp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Restore breakpoint now that we have passed it.
|
|
defer func() {
|
|
err = thread.dbp.writeSoftwareBreakpoint(thread, bp.Addr)
|
|
}()
|
|
}
|
|
|
|
err = thread.singleStep()
|
|
if err != nil {
|
|
if _, exited := err.(proc.ProcessExitedError); exited {
|
|
return err
|
|
}
|
|
return fmt.Errorf("step failed: %s", err.Error())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Location returns the threads location, including the file:line
|
|
// of the corresponding source code, the function we're in
|
|
// and the current instruction address.
|
|
func (thread *Thread) Location() (*proc.Location, error) {
|
|
pc, err := thread.PC()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
f, l, fn := thread.dbp.bi.PCToLine(pc)
|
|
return &proc.Location{PC: pc, File: f, Line: l, Fn: fn}, nil
|
|
}
|
|
|
|
func (thread *Thread) Arch() proc.Arch {
|
|
return thread.dbp.bi.Arch
|
|
}
|
|
|
|
func (thread *Thread) BinInfo() *proc.BinaryInfo {
|
|
return &thread.dbp.bi
|
|
}
|
|
|
|
// SetPC sets the PC for this thread.
|
|
func (thread *Thread) SetPC(pc uint64) error {
|
|
regs, err := thread.Registers(false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return regs.SetPC(thread, pc)
|
|
}
|
|
|
|
// Stopped returns whether the thread is stopped at
|
|
// the operating system level. Actual implementation
|
|
// is OS dependant, look in OS thread file.
|
|
func (thread *Thread) Stopped() bool {
|
|
return thread.stopped()
|
|
}
|
|
|
|
// SetCurrentBreakpoint sets the current breakpoint that this
|
|
// thread is stopped at as CurrentBreakpoint on the thread struct.
|
|
func (thread *Thread) SetCurrentBreakpoint() error {
|
|
thread.CurrentBreakpoint.Clear()
|
|
pc, err := thread.PC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if bp, ok := thread.dbp.FindBreakpoint(pc); ok {
|
|
if err = thread.SetPC(bp.Addr); err != nil {
|
|
return err
|
|
}
|
|
thread.CurrentBreakpoint = bp.CheckCondition(thread)
|
|
if thread.CurrentBreakpoint.Breakpoint != nil && thread.CurrentBreakpoint.Active {
|
|
if g, err := proc.GetG(thread); err == nil {
|
|
thread.CurrentBreakpoint.HitCount[g.ID]++
|
|
}
|
|
thread.CurrentBreakpoint.TotalHitCount++
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (th *Thread) Breakpoint() proc.BreakpointState {
|
|
return th.CurrentBreakpoint
|
|
}
|
|
|
|
func (th *Thread) ThreadID() int {
|
|
return th.ID
|
|
}
|
|
|
|
// ClearBreakpoint clears the specified breakpoint.
|
|
func (thread *Thread) ClearBreakpoint(bp *proc.Breakpoint) error {
|
|
if _, err := thread.WriteMemory(uintptr(bp.Addr), bp.OriginalData); err != nil {
|
|
return fmt.Errorf("could not clear breakpoint %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Registers obtains register values from the debugged process.
|
|
func (t *Thread) Registers(floatingPoint bool) (proc.Registers, error) {
|
|
return registers(t, floatingPoint)
|
|
}
|
|
|
|
func (t *Thread) PC() (uint64, error) {
|
|
regs, err := t.Registers(false)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return regs.PC(), nil
|
|
}
|