delve/proctl/threads.go

256 lines
6.5 KiB
Go
Raw Normal View History

2014-12-08 23:40:59 +00:00
package proctl
import (
"fmt"
"path/filepath"
2015-02-02 21:09:56 +00:00
"github.com/derekparker/delve/dwarf/frame"
sys "golang.org/x/sys/unix"
2014-12-08 23:40:59 +00:00
)
2015-02-02 21:09:56 +00:00
// ThreadContext represents a single thread in the traced process
// Id represents the thread id, Process holds a reference to the
// DebuggedProcess struct that contains info on the process as
// a whole, and Status represents the last result of a `wait` call
// on this thread.
2014-12-08 23:40:59 +00:00
type ThreadContext struct {
Id int
Process *DebuggedProcess
Status *sys.WaitStatus
CurrentBreakpoint *BreakPoint
2015-04-25 19:53:55 +00:00
singleStepping bool
running bool
os *OSSpecificDetails
}
2015-02-02 21:09:56 +00:00
// Continue the execution of this thread. This method takes
// software breakpoints into consideration and ensures that
// we step over any breakpoints. It will restore the instruction,
// step, and then restore the breakpoint and continue.
2014-12-08 23:40:59 +00:00
func (thread *ThreadContext) Continue() error {
2015-04-23 15:40:33 +00:00
pc, err := thread.PC()
2014-12-08 23:40:59 +00:00
if err != nil {
return err
2014-12-08 23:40:59 +00:00
}
// Check whether we are stopped at a breakpoint, and
// if so, single step over it before continuing.
2015-04-23 15:54:26 +00:00
if _, ok := thread.Process.BreakPoints[pc]; ok {
2014-12-08 23:40:59 +00:00
err := thread.Step()
if err != nil {
return fmt.Errorf("could not step %s", err)
}
}
2015-03-01 03:14:22 +00:00
return thread.resume()
2014-12-08 23:40:59 +00:00
}
// Single steps this thread a single instruction, ensuring that
// we correctly handle the likely case that we are at a breakpoint.
func (thread *ThreadContext) Step() (err error) {
2015-04-25 19:53:55 +00:00
thread.singleStepping = true
defer func() { thread.singleStepping = false }()
2015-04-23 15:40:33 +00:00
pc, err := thread.PC()
2014-12-08 23:40:59 +00:00
if err != nil {
return err
}
2015-04-23 15:54:26 +00:00
bp, ok := thread.Process.BreakPoints[pc]
2014-12-08 23:40:59 +00:00
if ok {
// Clear the breakpoint so that we can continue execution.
2015-01-14 02:37:10 +00:00
_, err = thread.Process.Clear(bp.Addr)
2014-12-08 23:40:59 +00:00
if err != nil {
return err
}
// Restore breakpoint now that we have passed it.
defer func() {
var nbp *BreakPoint
nbp, err = thread.Process.Break(bp.Addr)
nbp.Temp = bp.Temp
2014-12-08 23:40:59 +00:00
}()
}
2015-01-14 02:37:10 +00:00
err = thread.singleStep()
2014-12-08 23:40:59 +00:00
if err != nil {
return fmt.Errorf("step failed: %s", err.Error())
}
2015-02-17 17:27:47 +00:00
return err
2014-12-08 23:40:59 +00:00
}
// Call a function named `name`. This is currently _NOT_ safe.
func (thread *ThreadContext) CallFn(name string, fn func() error) error {
f := thread.Process.goSymTable.LookupFunc(name)
if f == nil {
return fmt.Errorf("could not find function %s", name)
}
// Set breakpoint at the end of the function (before it returns).
bp, err := thread.Break(f.End - 2)
2014-12-08 23:40:59 +00:00
if err != nil {
return err
}
defer thread.Process.Clear(bp.Addr)
2014-12-08 23:40:59 +00:00
2015-04-23 15:30:27 +00:00
if err = thread.saveRegisters(); err != nil {
return err
}
if err = thread.SetPC(f.Entry); err != nil {
return err
}
defer thread.restoreRegisters()
2015-04-23 15:30:27 +00:00
if err = thread.Continue(); err != nil {
return err
}
th, err := thread.Process.trapWait(-1)
if err != nil {
return err
2014-12-08 23:40:59 +00:00
}
th.CurrentBreakpoint = nil
return fn()
}
// Set breakpoint using this thread.
func (thread *ThreadContext) Break(addr uint64) (*BreakPoint, error) {
return thread.Process.setBreakpoint(thread.Id, addr, false)
}
// Clear breakpoint using this thread.
func (thread *ThreadContext) Clear(addr uint64) (*BreakPoint, error) {
return thread.Process.clearBreakpoint(thread.Id, addr)
}
2014-12-08 23:40:59 +00:00
// Step to next source line.
//
// Next will step over functions, and will follow through to the
// return address of a function.
//
// This functionality is implemented by finding all possible next lines
// and setting a breakpoint at them. Once we've set a breakpoint at each
// potential line, we continue the thread.
func (thread *ThreadContext) Next() (err error) {
2015-04-23 15:40:33 +00:00
curpc, err := thread.PC()
2014-12-08 23:40:59 +00:00
if err != nil {
return err
}
// Grab info on our current stack frame. Used to determine
// whether we may be stepping outside of the current function.
fde, err := thread.Process.frameEntries.FDEForPC(curpc)
if err != nil {
return err
2014-12-08 23:40:59 +00:00
}
// Get current file/line.
f, l, _ := thread.Process.goSymTable.PCToLine(curpc)
if filepath.Ext(f) == ".go" {
if err = thread.next(curpc, fde, f, l); err != nil {
return err
}
} else {
if err = thread.cnext(curpc, fde); err != nil {
return err
2014-12-08 23:40:59 +00:00
}
}
return thread.Continue()
}
2014-12-08 23:40:59 +00:00
// Go routine is exiting.
type GoroutineExitingError struct {
goid int
}
func (ge GoroutineExitingError) Error() string {
return fmt.Sprintf("goroutine %d is exiting", ge.goid)
}
// This version of next uses the AST from the current source file to figure out all of the potential source lines
// we could end up at.
func (thread *ThreadContext) next(curpc uint64, fde *frame.FrameDescriptionEntry, file string, line int) error {
lines, err := thread.Process.ast.NextLines(file, line)
if err != nil {
return err
}
2014-12-08 23:40:59 +00:00
ret, err := thread.ReturnAddress()
2014-12-08 23:40:59 +00:00
if err != nil {
return err
2014-12-08 23:40:59 +00:00
}
pcs := make([]uint64, 0, len(lines))
for i := range lines {
pcs = append(pcs, thread.Process.lineInfo.AllPCsForFileLine(file, lines[i])...)
}
2014-12-08 23:40:59 +00:00
var covered bool
for i := range pcs {
if fde.Cover(pcs[i]) {
covered = true
break
}
}
if !covered {
fn := thread.Process.goSymTable.PCToFunc(ret)
if fn != nil && fn.Name == "runtime.goexit" {
g, err := thread.curG()
if err != nil {
return err
}
return GoroutineExitingError{goid: g.Id}
}
}
pcs = append(pcs, ret)
return thread.setNextTempBreakpoints(curpc, pcs)
}
// Set a breakpoint at every reachable location, as well as the return address. Without
// the benefit of an AST we can't be sure we're not at a branching statement and thus
// cannot accurately predict where we may end up.
func (thread *ThreadContext) cnext(curpc uint64, fde *frame.FrameDescriptionEntry) error {
pcs := thread.Process.lineInfo.AllPCsBetween(fde.Begin(), fde.End())
ret, err := thread.ReturnAddress()
if err != nil {
return err
}
pcs = append(pcs, ret)
return thread.setNextTempBreakpoints(curpc, pcs)
}
func (thread *ThreadContext) setNextTempBreakpoints(curpc uint64, pcs []uint64) error {
for i := range pcs {
if pcs[i] == curpc || pcs[i] == curpc-1 {
continue
}
if _, err := thread.Process.TempBreak(pcs[i]); err != nil {
if err, ok := err.(BreakPointExistsError); !ok {
return err
}
}
}
return nil
}
2015-04-23 01:00:42 +00:00
// Sets the PC for this thread.
func (thread *ThreadContext) SetPC(pc uint64) error {
regs, err := thread.Registers()
if err != nil {
return err
}
return regs.SetPC(thread, pc)
}
func (thread *ThreadContext) curG() (*G, error) {
var g *G
err := thread.CallFn("runtime.getg", func() error {
regs, err := thread.Registers()
2014-12-08 23:40:59 +00:00
if err != nil {
return err
}
reader := thread.Process.dwarf.Reader()
g, err = parseG(thread, regs.SP()+uint64(ptrsize), reader)
return err
})
return g, err
2014-12-08 23:40:59 +00:00
}