2015-06-12 19:49:23 +00:00
|
|
|
package proc
|
2014-12-05 22:17:10 +00:00
|
|
|
|
|
|
|
import (
|
2015-04-09 14:53:02 +00:00
|
|
|
"encoding/binary"
|
2016-01-20 20:22:39 +00:00
|
|
|
"errors"
|
2014-12-05 22:17:10 +00:00
|
|
|
"fmt"
|
2016-01-12 08:01:42 +00:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2016-04-14 09:58:59 +00:00
|
|
|
"path/filepath"
|
2016-01-12 08:01:42 +00:00
|
|
|
"strconv"
|
2014-12-05 22:17:10 +00:00
|
|
|
)
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
// ErrNotExecutable is returned after attempting to execute a non-executable file
|
|
|
|
// to begin a debug session.
|
|
|
|
var ErrNotExecutable = errors.New("not an executable file")
|
2016-04-21 10:19:21 +00:00
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
// ErrNotRecorded is returned when an action is requested that is
|
|
|
|
// only possible on recorded (traced) programs.
|
|
|
|
var ErrNotRecorded = errors.New("not a recording")
|
|
|
|
|
2019-02-27 22:28:25 +00:00
|
|
|
const (
|
|
|
|
// UnrecoveredPanic is the name given to the unrecovered panic breakpoint.
|
|
|
|
UnrecoveredPanic = "unrecovered-panic"
|
|
|
|
|
|
|
|
// FatalThrow is the name given to the breakpoint triggered when the target process dies because of a fatal runtime error
|
|
|
|
FatalThrow = "runtime-fatal-throw"
|
|
|
|
|
|
|
|
unrecoveredPanicID = -1
|
|
|
|
fatalThrowID = -2
|
|
|
|
)
|
2017-09-20 12:19:59 +00:00
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
// ErrProcessExited indicates that the process has exited and contains both
|
2015-03-05 22:59:51 +00:00
|
|
|
// process id and exit status.
|
2018-08-31 18:08:18 +00:00
|
|
|
type ErrProcessExited struct {
|
2015-03-05 22:59:51 +00:00
|
|
|
Pid int
|
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
func (pe ErrProcessExited) Error() string {
|
2015-06-21 18:08:14 +00:00
|
|
|
return fmt.Sprintf("Process %d has exited with status %d", pe.Pid, pe.Status)
|
2015-03-05 22:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 11:07:37 +00:00
|
|
|
// ProcessDetachedError indicates that we detached from the target process.
|
|
|
|
type ProcessDetachedError struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pe ProcessDetachedError) Error() string {
|
|
|
|
return "detached from the process"
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:52:13 +00:00
|
|
|
// PostInitializationSetup handles all of the initialization procedures
|
|
|
|
// that must happen after Delve creates or attaches to a process.
|
|
|
|
func PostInitializationSetup(p Process, path string, debugInfoDirs []string, writeBreakpoint WriteBreakpointFn) error {
|
|
|
|
entryPoint, err := p.EntryPoint()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.BinInfo().LoadBinaryInfo(path, entryPoint, debugInfoDirs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-08 21:06:38 +00:00
|
|
|
for _, image := range p.BinInfo().Images {
|
|
|
|
if image.loadErr != nil {
|
|
|
|
return image.loadErr
|
|
|
|
}
|
|
|
|
}
|
2018-11-12 22:52:13 +00:00
|
|
|
|
|
|
|
g, _ := GetG(p.CurrentThread())
|
|
|
|
p.SetSelectedGoroutine(g)
|
|
|
|
|
|
|
|
createUnrecoveredPanicBreakpoint(p, writeBreakpoint)
|
2019-02-27 22:28:25 +00:00
|
|
|
createFatalThrowBreakpoint(p, writeBreakpoint)
|
2018-11-12 22:52:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// FindFileLocation returns the PC for a given file:line.
|
2018-03-20 10:05:35 +00:00
|
|
|
// Assumes that `file` is normalized to lower case and '/' on Windows.
|
2019-11-01 19:41:06 +00:00
|
|
|
func FindFileLocation(p Process, fileName string, lineno int) ([]uint64, error) {
|
|
|
|
pcs, err := p.BinInfo().LineToPC(fileName, lineno)
|
2015-08-07 16:50:14 +00:00
|
|
|
if err != nil {
|
2019-11-01 19:41:06 +00:00
|
|
|
return nil, err
|
2015-08-07 16:50:14 +00:00
|
|
|
}
|
2019-11-01 19:41:06 +00:00
|
|
|
var fn *Function
|
|
|
|
for i := range pcs {
|
|
|
|
if fn == nil || pcs[i] < fn.Entry || pcs[i] >= fn.End {
|
|
|
|
fn = p.BinInfo().PCToFunc(pcs[i])
|
|
|
|
}
|
|
|
|
if fn != nil && fn.Entry == pcs[i] {
|
|
|
|
pcs[i], _ = FirstPCAfterPrologue(p, fn, true)
|
|
|
|
}
|
2016-02-11 07:18:39 +00:00
|
|
|
}
|
2019-11-01 19:41:06 +00:00
|
|
|
return pcs, nil
|
2015-08-07 16:50:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
// ErrFunctionNotFound is returned when failing to find the
|
|
|
|
// function named 'FuncName' within the binary.
|
|
|
|
type ErrFunctionNotFound struct {
|
2018-03-20 11:34:02 +00:00
|
|
|
FuncName string
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
func (err *ErrFunctionNotFound) Error() string {
|
2018-03-20 11:34:02 +00:00
|
|
|
return fmt.Sprintf("Could not find function %s\n", err.FuncName)
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// FindFunctionLocation finds address of a function's line
|
2015-08-07 16:50:14 +00:00
|
|
|
// If lineOffset is passed FindFunctionLocation will return the address of that line
|
2019-11-01 19:41:06 +00:00
|
|
|
func FindFunctionLocation(p Process, funcName string, lineOffset int) ([]uint64, error) {
|
2017-04-28 17:15:39 +00:00
|
|
|
bi := p.BinInfo()
|
2017-09-01 13:30:45 +00:00
|
|
|
origfn := bi.LookupFunc[funcName]
|
2015-10-22 17:07:24 +00:00
|
|
|
if origfn == nil {
|
2019-11-01 19:41:06 +00:00
|
|
|
return nil, &ErrFunctionNotFound{funcName}
|
2015-08-07 16:50:14 +00:00
|
|
|
}
|
2015-01-01 13:23:55 +00:00
|
|
|
|
2019-06-25 20:50:05 +00:00
|
|
|
if lineOffset <= 0 {
|
2019-11-01 19:41:06 +00:00
|
|
|
r := make([]uint64, 0, len(origfn.InlinedCalls)+1)
|
|
|
|
if origfn.Entry > 0 {
|
|
|
|
// add concrete implementation of the function
|
|
|
|
pc, err := FirstPCAfterPrologue(p, origfn, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r = append(r, pc)
|
|
|
|
}
|
|
|
|
// add inlined calls to the function
|
|
|
|
for _, call := range origfn.InlinedCalls {
|
|
|
|
r = append(r, call.LowPC)
|
|
|
|
}
|
|
|
|
if len(r) == 0 {
|
|
|
|
return nil, &ErrFunctionNotFound{funcName}
|
|
|
|
}
|
|
|
|
return r, nil
|
2015-03-24 13:31:56 +00:00
|
|
|
}
|
2019-06-25 20:50:05 +00:00
|
|
|
filename, lineno := origfn.cu.lineInfo.PCToLine(origfn.Entry, origfn.Entry)
|
2019-11-01 19:41:06 +00:00
|
|
|
return bi.LineToPC(filename, lineno+lineOffset)
|
2015-01-01 13:23:55 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// Next continues execution until the next source line.
|
2017-04-21 07:50:38 +00:00
|
|
|
func Next(dbp Process) (err error) {
|
2018-06-21 11:07:37 +00:00
|
|
|
if _, err := dbp.Valid(); err != nil {
|
|
|
|
return err
|
2016-01-30 06:23:14 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
if dbp.Breakpoints().HasInternalBreakpoints() {
|
|
|
|
return fmt.Errorf("next while nexting")
|
2015-04-25 02:56:57 +00:00
|
|
|
}
|
|
|
|
|
proc: support inlining
Go 1.10 added inlined calls to debug_info, this commit adds support
for DW_TAG_inlined_call to delve, both for stack traces (where
inlined calls will appear as normal stack frames) and to correct
the behavior of next, step and stepout.
The calls to Next and Frame of stackIterator continue to work
unchanged and only return real stack frames, after reading each line
appendInlinedCalls is called to unpacked all the inlined calls that
involve the current PC.
The fake stack frames produced by appendInlinedCalls are
distinguished from real stack frames by having the Inlined attribute
set to true. Also their Current and Call locations are treated
differently. The Call location will be changed to represent the
position inside the inlined call, while the Current location will
always reference the real stack frame. This is done because:
* next, step and stepout need to access the debug_info entry of
the real function they are stepping through
* we are already manipulating Call in different ways while Current
is just what we read from the call stack
The strategy remains mostly the same, we disassemble the function
and we set a breakpoint on each instruction corresponding to a
different file:line. The function in question will be the one
corresponding to the first real (i.e. non-inlined) stack frame.
* If the current function contains inlined calls, 'next' will not
set any breakpoints on instructions that belong to inlined calls. We
do not do this for 'step'.
* If we are inside an inlined call that makes other inlined
functions, 'next' will not set any breakpoints that belong to
inlined calls that are children of the current inlined call.
* If the current function is inlined the breakpoint on the return
address won't be set, because inlined frames don't have a return
address.
* The code we use for stepout doesn't work at all if we are inside
an inlined call, instead we call 'next' but instruct it to remove
all PCs belonging to the current inlined call.
2017-11-13 15:54:08 +00:00
|
|
|
if err = next(dbp, false, false); err != nil {
|
2017-07-24 08:58:12 +00:00
|
|
|
dbp.ClearInternalBreakpoints()
|
|
|
|
return
|
2015-08-22 03:13:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
return Continue(dbp)
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// Continue continues execution of the debugged
|
|
|
|
// process. It will continue until it hits a breakpoint
|
|
|
|
// or is otherwise stopped.
|
2017-04-21 07:50:38 +00:00
|
|
|
func Continue(dbp Process) error {
|
2018-06-21 11:07:37 +00:00
|
|
|
if _, err := dbp.Valid(); err != nil {
|
|
|
|
return err
|
2017-07-26 22:57:47 +00:00
|
|
|
}
|
2018-05-11 12:51:15 +00:00
|
|
|
for _, thread := range dbp.ThreadList() {
|
|
|
|
thread.Common().returnValues = nil
|
|
|
|
}
|
2018-02-09 19:42:45 +00:00
|
|
|
dbp.CheckAndClearManualStopRequest()
|
2018-03-07 10:14:34 +00:00
|
|
|
defer func() {
|
|
|
|
// Make sure we clear internal breakpoints if we simultaneously receive a
|
|
|
|
// manual stop request and hit a breakpoint.
|
|
|
|
if dbp.CheckAndClearManualStopRequest() {
|
|
|
|
dbp.ClearInternalBreakpoints()
|
|
|
|
}
|
|
|
|
}()
|
2015-11-18 09:07:08 +00:00
|
|
|
for {
|
2018-02-09 19:42:45 +00:00
|
|
|
if dbp.CheckAndClearManualStopRequest() {
|
2018-03-07 10:14:34 +00:00
|
|
|
dbp.ClearInternalBreakpoints()
|
2017-07-07 23:29:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
trapthread, err := dbp.ContinueOnce()
|
|
|
|
if err != nil {
|
2015-11-18 09:07:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
threads := dbp.ThreadList()
|
2015-11-18 09:07:08 +00:00
|
|
|
|
2019-06-30 17:44:30 +00:00
|
|
|
callInjectionDone, err := callInjectionProtocol(dbp, threads)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
if err := pickCurrentThread(dbp, trapthread, threads); err != nil {
|
2015-07-10 19:48:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-16 08:13:15 +00:00
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
curthread := dbp.CurrentThread()
|
2017-09-25 06:29:13 +00:00
|
|
|
curbp := curthread.Breakpoint()
|
2017-02-22 08:35:21 +00:00
|
|
|
|
2015-11-24 21:48:28 +00:00
|
|
|
switch {
|
2017-09-25 06:29:13 +00:00
|
|
|
case curbp.Breakpoint == nil:
|
2018-05-04 17:31:45 +00:00
|
|
|
// runtime.Breakpoint, manual stop or debugCallV1-related stop
|
|
|
|
recorded, _ := dbp.Recorded()
|
|
|
|
if recorded {
|
|
|
|
return conditionErrors(threads)
|
|
|
|
}
|
|
|
|
|
|
|
|
loc, err := curthread.Location()
|
|
|
|
if err != nil || loc.Fn == nil {
|
|
|
|
return conditionErrors(threads)
|
|
|
|
}
|
2019-06-30 17:44:30 +00:00
|
|
|
g, _ := GetG(curthread)
|
2018-05-04 17:31:45 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case loc.Fn.Name == "runtime.breakpoint":
|
2017-02-07 21:07:18 +00:00
|
|
|
// Single-step current thread until we exit runtime.breakpoint and
|
|
|
|
// runtime.Breakpoint.
|
|
|
|
// On go < 1.8 it was sufficient to single-step twice on go1.8 a change
|
|
|
|
// to the compiler requires 4 steps.
|
2018-05-04 17:31:45 +00:00
|
|
|
if err := stepInstructionOut(dbp, curthread, "runtime.breakpoint", "runtime.Breakpoint"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conditionErrors(threads)
|
2019-06-30 17:44:30 +00:00
|
|
|
case g == nil || dbp.Common().fncallForG[g.ID] == nil:
|
|
|
|
// a hardcoded breakpoint somewhere else in the code (probably cgo)
|
2018-05-04 17:31:45 +00:00
|
|
|
return conditionErrors(threads)
|
2015-03-01 04:03:26 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
case curbp.Active && curbp.Internal:
|
2018-10-16 15:49:20 +00:00
|
|
|
switch curbp.Kind {
|
|
|
|
case StepBreakpoint:
|
2016-07-26 13:34:31 +00:00
|
|
|
// See description of proc.(*Process).next for the meaning of StepBreakpoints
|
2017-02-22 08:35:21 +00:00
|
|
|
if err := conditionErrors(threads); err != nil {
|
2016-07-26 13:34:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
regs, err := curthread.Registers(false)
|
2017-04-13 23:19:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
pc := regs.PC()
|
2017-11-06 17:34:59 +00:00
|
|
|
text, err := disassemble(curthread, regs, dbp.Breakpoints(), dbp.BinInfo(), pc, pc+maxInstructionLength, true)
|
2016-07-26 13:34:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// here we either set a breakpoint into the destination of the CALL
|
|
|
|
// instruction or we determined that the called function is hidden,
|
|
|
|
// either way we need to resume execution
|
2017-04-21 06:55:53 +00:00
|
|
|
if err = setStepIntoBreakpoint(dbp, text, SameGoroutineCondition(dbp.SelectedGoroutine())); err != nil {
|
2016-07-26 13:34:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-16 15:49:20 +00:00
|
|
|
default:
|
2018-05-11 12:51:15 +00:00
|
|
|
curthread.Common().returnValues = curbp.Breakpoint.returnInfo.Collect(curthread)
|
2016-09-30 06:35:29 +00:00
|
|
|
if err := dbp.ClearInternalBreakpoints(); err != nil {
|
2016-07-26 13:34:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
return conditionErrors(threads)
|
2016-01-12 08:01:42 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
case curbp.Active:
|
2017-02-22 08:35:21 +00:00
|
|
|
onNextGoroutine, err := onNextGoroutine(curthread, dbp.Breakpoints())
|
2016-01-12 08:01:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-16 08:13:15 +00:00
|
|
|
}
|
2016-01-12 08:01:42 +00:00
|
|
|
if onNextGoroutine {
|
2016-09-30 06:35:29 +00:00
|
|
|
err := dbp.ClearInternalBreakpoints()
|
2016-01-12 08:01:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 12:19:59 +00:00
|
|
|
if curbp.Name == UnrecoveredPanic {
|
|
|
|
dbp.ClearInternalBreakpoints()
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
return conditionErrors(threads)
|
2015-11-24 21:48:28 +00:00
|
|
|
default:
|
|
|
|
// not a manual stop, not on runtime.Breakpoint, not on a breakpoint, just repeat
|
2015-03-01 04:03:26 +00:00
|
|
|
}
|
2019-06-30 17:44:30 +00:00
|
|
|
if callInjectionDone {
|
|
|
|
// a call injection was finished, don't let a breakpoint with a failed
|
|
|
|
// condition or a step breakpoint shadow this.
|
|
|
|
return conditionErrors(threads)
|
|
|
|
}
|
2015-11-24 21:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:50:38 +00:00
|
|
|
func conditionErrors(threads []Thread) error {
|
2016-01-12 08:01:42 +00:00
|
|
|
var condErr error
|
2017-02-22 08:35:21 +00:00
|
|
|
for _, th := range threads {
|
2017-09-25 06:29:13 +00:00
|
|
|
if bp := th.Breakpoint(); bp.Breakpoint != nil && bp.CondError != nil {
|
2016-01-12 08:01:42 +00:00
|
|
|
if condErr == nil {
|
2017-09-25 06:29:13 +00:00
|
|
|
condErr = bp.CondError
|
2016-01-12 08:01:42 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("multiple errors evaluating conditions")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return condErr
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:23:47 +00:00
|
|
|
// pick a new dbp.currentThread, with the following priority:
|
2016-09-30 06:35:29 +00:00
|
|
|
// - a thread with onTriggeredInternalBreakpoint() == true
|
2015-11-24 21:48:28 +00:00
|
|
|
// - a thread with onTriggeredBreakpoint() == true (prioritizing trapthread)
|
|
|
|
// - trapthread
|
2017-04-21 07:50:38 +00:00
|
|
|
func pickCurrentThread(dbp Process, trapthread Thread, threads []Thread) error {
|
2017-02-22 08:35:21 +00:00
|
|
|
for _, th := range threads {
|
2017-09-25 06:29:13 +00:00
|
|
|
if bp := th.Breakpoint(); bp.Active && bp.Internal {
|
2017-02-22 08:35:21 +00:00
|
|
|
return dbp.SwitchThread(th.ThreadID())
|
2015-11-24 21:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
if bp := trapthread.Breakpoint(); bp.Active {
|
2017-02-22 08:35:21 +00:00
|
|
|
return dbp.SwitchThread(trapthread.ThreadID())
|
2015-11-24 21:48:28 +00:00
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
for _, th := range threads {
|
2017-09-25 06:29:13 +00:00
|
|
|
if bp := th.Breakpoint(); bp.Active {
|
2017-02-22 08:35:21 +00:00
|
|
|
return dbp.SwitchThread(th.ThreadID())
|
2015-11-24 21:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
return dbp.SwitchThread(trapthread.ThreadID())
|
2014-12-05 22:17:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 17:31:45 +00:00
|
|
|
// stepInstructionOut repeatedly calls StepInstruction until the current
|
|
|
|
// function is neither fnname1 or fnname2.
|
|
|
|
// This function is used to step out of runtime.Breakpoint as well as
|
|
|
|
// runtime.debugCallV1.
|
|
|
|
func stepInstructionOut(dbp Process, curthread Thread, fnname1, fnname2 string) error {
|
|
|
|
for {
|
|
|
|
if err := curthread.StepInstruction(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
loc, err := curthread.Location()
|
|
|
|
if err != nil || loc.Fn == nil || (loc.Fn.Name != fnname1 && loc.Fn.Name != fnname2) {
|
2019-06-30 17:44:30 +00:00
|
|
|
g, _ := GetG(curthread)
|
|
|
|
selg := dbp.SelectedGoroutine()
|
|
|
|
if g != nil && selg != nil && g.ID == selg.ID {
|
|
|
|
selg.CurrentLoc = *loc
|
2018-05-04 17:31:45 +00:00
|
|
|
}
|
2019-08-12 22:11:19 +00:00
|
|
|
return curthread.SetCurrentBreakpoint(true)
|
2018-05-04 17:31:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 05:25:46 +00:00
|
|
|
// Step will continue until another source line is reached.
|
|
|
|
// Will step into functions.
|
2017-04-21 07:50:38 +00:00
|
|
|
func Step(dbp Process) (err error) {
|
2018-06-21 11:07:37 +00:00
|
|
|
if _, err := dbp.Valid(); err != nil {
|
|
|
|
return err
|
2016-01-24 05:25:46 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
if dbp.Breakpoints().HasInternalBreakpoints() {
|
|
|
|
return fmt.Errorf("next while nexting")
|
2016-04-24 23:20:02 +00:00
|
|
|
}
|
2016-04-13 13:25:23 +00:00
|
|
|
|
proc: support inlining
Go 1.10 added inlined calls to debug_info, this commit adds support
for DW_TAG_inlined_call to delve, both for stack traces (where
inlined calls will appear as normal stack frames) and to correct
the behavior of next, step and stepout.
The calls to Next and Frame of stackIterator continue to work
unchanged and only return real stack frames, after reading each line
appendInlinedCalls is called to unpacked all the inlined calls that
involve the current PC.
The fake stack frames produced by appendInlinedCalls are
distinguished from real stack frames by having the Inlined attribute
set to true. Also their Current and Call locations are treated
differently. The Call location will be changed to represent the
position inside the inlined call, while the Current location will
always reference the real stack frame. This is done because:
* next, step and stepout need to access the debug_info entry of
the real function they are stepping through
* we are already manipulating Call in different ways while Current
is just what we read from the call stack
The strategy remains mostly the same, we disassemble the function
and we set a breakpoint on each instruction corresponding to a
different file:line. The function in question will be the one
corresponding to the first real (i.e. non-inlined) stack frame.
* If the current function contains inlined calls, 'next' will not
set any breakpoints on instructions that belong to inlined calls. We
do not do this for 'step'.
* If we are inside an inlined call that makes other inlined
functions, 'next' will not set any breakpoints that belong to
inlined calls that are children of the current inlined call.
* If the current function is inlined the breakpoint on the return
address won't be set, because inlined frames don't have a return
address.
* The code we use for stepout doesn't work at all if we are inside
an inlined call, instead we call 'next' but instruct it to remove
all PCs belonging to the current inlined call.
2017-11-13 15:54:08 +00:00
|
|
|
if err = next(dbp, true, false); err != nil {
|
2016-07-26 13:34:31 +00:00
|
|
|
switch err.(type) {
|
2018-08-31 18:08:18 +00:00
|
|
|
case ErrThreadBlocked: // Noop
|
2016-07-26 13:34:31 +00:00
|
|
|
default:
|
2016-09-30 06:35:29 +00:00
|
|
|
dbp.ClearInternalBreakpoints()
|
2016-07-26 13:34:31 +00:00
|
|
|
return
|
|
|
|
}
|
2016-02-12 07:43:22 +00:00
|
|
|
}
|
2016-07-26 13:34:31 +00:00
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
return Continue(dbp)
|
2016-02-12 07:43:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 06:55:53 +00:00
|
|
|
// SameGoroutineCondition returns an expression that evaluates to true when
|
|
|
|
// the current goroutine is g.
|
|
|
|
func SameGoroutineCondition(g *G) ast.Expr {
|
2016-04-11 11:50:01 +00:00
|
|
|
if g == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &ast.BinaryExpr{
|
|
|
|
Op: token.EQL,
|
|
|
|
X: &ast.SelectorExpr{
|
|
|
|
X: &ast.SelectorExpr{
|
|
|
|
X: &ast.Ident{Name: "runtime"},
|
|
|
|
Sel: &ast.Ident{Name: "curg"},
|
|
|
|
},
|
|
|
|
Sel: &ast.Ident{Name: "goid"},
|
|
|
|
},
|
|
|
|
Y: &ast.BasicLit{Kind: token.INT, Value: strconv.Itoa(g.ID)},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:23:33 +00:00
|
|
|
func frameoffCondition(frameoff int64) ast.Expr {
|
|
|
|
return &ast.BinaryExpr{
|
|
|
|
Op: token.EQL,
|
|
|
|
X: &ast.SelectorExpr{
|
|
|
|
X: &ast.Ident{Name: "runtime"},
|
|
|
|
Sel: &ast.Ident{Name: "frameoff"},
|
|
|
|
},
|
|
|
|
Y: &ast.BasicLit{Kind: token.INT, Value: strconv.FormatInt(frameoff, 10)},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func andFrameoffCondition(cond ast.Expr, frameoff int64) ast.Expr {
|
|
|
|
if cond == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &ast.BinaryExpr{
|
|
|
|
Op: token.LAND,
|
|
|
|
X: cond,
|
|
|
|
Y: frameoffCondition(frameoff),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 09:58:59 +00:00
|
|
|
// StepOut will continue until the current goroutine exits the
|
|
|
|
// function currently being executed or a deferred function is executed
|
2017-04-21 07:50:38 +00:00
|
|
|
func StepOut(dbp Process) error {
|
2018-06-21 11:07:37 +00:00
|
|
|
if _, err := dbp.Valid(); err != nil {
|
|
|
|
return err
|
2017-07-26 22:57:47 +00:00
|
|
|
}
|
2018-05-04 17:31:45 +00:00
|
|
|
if dbp.Breakpoints().HasInternalBreakpoints() {
|
|
|
|
return fmt.Errorf("next while nexting")
|
|
|
|
}
|
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
selg := dbp.SelectedGoroutine()
|
|
|
|
curthread := dbp.CurrentThread()
|
2016-04-14 09:58:59 +00:00
|
|
|
|
2017-05-16 18:23:33 +00:00
|
|
|
topframe, retframe, err := topframe(selg, curthread)
|
2016-04-14 09:58:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
proc: support inlining
Go 1.10 added inlined calls to debug_info, this commit adds support
for DW_TAG_inlined_call to delve, both for stack traces (where
inlined calls will appear as normal stack frames) and to correct
the behavior of next, step and stepout.
The calls to Next and Frame of stackIterator continue to work
unchanged and only return real stack frames, after reading each line
appendInlinedCalls is called to unpacked all the inlined calls that
involve the current PC.
The fake stack frames produced by appendInlinedCalls are
distinguished from real stack frames by having the Inlined attribute
set to true. Also their Current and Call locations are treated
differently. The Call location will be changed to represent the
position inside the inlined call, while the Current location will
always reference the real stack frame. This is done because:
* next, step and stepout need to access the debug_info entry of
the real function they are stepping through
* we are already manipulating Call in different ways while Current
is just what we read from the call stack
The strategy remains mostly the same, we disassemble the function
and we set a breakpoint on each instruction corresponding to a
different file:line. The function in question will be the one
corresponding to the first real (i.e. non-inlined) stack frame.
* If the current function contains inlined calls, 'next' will not
set any breakpoints on instructions that belong to inlined calls. We
do not do this for 'step'.
* If we are inside an inlined call that makes other inlined
functions, 'next' will not set any breakpoints that belong to
inlined calls that are children of the current inlined call.
* If the current function is inlined the breakpoint on the return
address won't be set, because inlined frames don't have a return
address.
* The code we use for stepout doesn't work at all if we are inside
an inlined call, instead we call 'next' but instruct it to remove
all PCs belonging to the current inlined call.
2017-11-13 15:54:08 +00:00
|
|
|
success := false
|
|
|
|
defer func() {
|
|
|
|
if !success {
|
|
|
|
dbp.ClearInternalBreakpoints()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if topframe.Inlined {
|
|
|
|
if err := next(dbp, false, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
success = true
|
|
|
|
return Continue(dbp)
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:23:33 +00:00
|
|
|
sameGCond := SameGoroutineCondition(selg)
|
2017-09-01 13:34:13 +00:00
|
|
|
retFrameCond := andFrameoffCondition(sameGCond, retframe.FrameOffset())
|
2016-04-14 09:58:59 +00:00
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
var deferpc uint64
|
2016-04-14 09:58:59 +00:00
|
|
|
if filepath.Ext(topframe.Current.File) == ".go" {
|
2018-07-06 07:37:31 +00:00
|
|
|
if topframe.TopmostDefer != nil && topframe.TopmostDefer.DeferredPC != 0 {
|
|
|
|
deferfn := dbp.BinInfo().PCToFunc(topframe.TopmostDefer.DeferredPC)
|
|
|
|
deferpc, err = FirstPCAfterPrologue(dbp, deferfn, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-14 09:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if deferpc != 0 && deferpc != topframe.Current.PC {
|
2017-05-16 18:23:33 +00:00
|
|
|
bp, err := dbp.SetBreakpoint(deferpc, NextDeferBreakpoint, sameGCond)
|
2016-04-14 09:58:59 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(BreakpointExistsError); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bp != nil {
|
|
|
|
// For StepOut we do not want to step into the deferred function
|
|
|
|
// when it's called by runtime.deferreturn so we do not populate
|
|
|
|
// DeferReturns.
|
|
|
|
bp.DeferReturns = []uint64{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
proc: support inlining
Go 1.10 added inlined calls to debug_info, this commit adds support
for DW_TAG_inlined_call to delve, both for stack traces (where
inlined calls will appear as normal stack frames) and to correct
the behavior of next, step and stepout.
The calls to Next and Frame of stackIterator continue to work
unchanged and only return real stack frames, after reading each line
appendInlinedCalls is called to unpacked all the inlined calls that
involve the current PC.
The fake stack frames produced by appendInlinedCalls are
distinguished from real stack frames by having the Inlined attribute
set to true. Also their Current and Call locations are treated
differently. The Call location will be changed to represent the
position inside the inlined call, while the Current location will
always reference the real stack frame. This is done because:
* next, step and stepout need to access the debug_info entry of
the real function they are stepping through
* we are already manipulating Call in different ways while Current
is just what we read from the call stack
The strategy remains mostly the same, we disassemble the function
and we set a breakpoint on each instruction corresponding to a
different file:line. The function in question will be the one
corresponding to the first real (i.e. non-inlined) stack frame.
* If the current function contains inlined calls, 'next' will not
set any breakpoints on instructions that belong to inlined calls. We
do not do this for 'step'.
* If we are inside an inlined call that makes other inlined
functions, 'next' will not set any breakpoints that belong to
inlined calls that are children of the current inlined call.
* If the current function is inlined the breakpoint on the return
address won't be set, because inlined frames don't have a return
address.
* The code we use for stepout doesn't work at all if we are inside
an inlined call, instead we call 'next' but instruct it to remove
all PCs belonging to the current inlined call.
2017-11-13 15:54:08 +00:00
|
|
|
if topframe.Ret == 0 && deferpc == 0 {
|
|
|
|
return errors.New("nothing to stepout to")
|
|
|
|
}
|
|
|
|
|
2016-04-14 09:58:59 +00:00
|
|
|
if topframe.Ret != 0 {
|
2018-05-11 12:51:15 +00:00
|
|
|
bp, err := dbp.SetBreakpoint(topframe.Ret, NextBreakpoint, retFrameCond)
|
2017-05-16 18:23:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, isexists := err.(BreakpointExistsError); !isexists {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-14 09:58:59 +00:00
|
|
|
}
|
2018-05-11 12:51:15 +00:00
|
|
|
if bp != nil {
|
|
|
|
configureReturnBreakpoint(dbp.BinInfo(), bp, &topframe, retFrameCond)
|
|
|
|
}
|
2016-04-14 09:58:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
if bp := curthread.Breakpoint(); bp.Breakpoint == nil {
|
2019-08-12 22:11:19 +00:00
|
|
|
curthread.SetCurrentBreakpoint(false)
|
2017-05-16 18:23:33 +00:00
|
|
|
}
|
|
|
|
|
proc: support inlining
Go 1.10 added inlined calls to debug_info, this commit adds support
for DW_TAG_inlined_call to delve, both for stack traces (where
inlined calls will appear as normal stack frames) and to correct
the behavior of next, step and stepout.
The calls to Next and Frame of stackIterator continue to work
unchanged and only return real stack frames, after reading each line
appendInlinedCalls is called to unpacked all the inlined calls that
involve the current PC.
The fake stack frames produced by appendInlinedCalls are
distinguished from real stack frames by having the Inlined attribute
set to true. Also their Current and Call locations are treated
differently. The Call location will be changed to represent the
position inside the inlined call, while the Current location will
always reference the real stack frame. This is done because:
* next, step and stepout need to access the debug_info entry of
the real function they are stepping through
* we are already manipulating Call in different ways while Current
is just what we read from the call stack
The strategy remains mostly the same, we disassemble the function
and we set a breakpoint on each instruction corresponding to a
different file:line. The function in question will be the one
corresponding to the first real (i.e. non-inlined) stack frame.
* If the current function contains inlined calls, 'next' will not
set any breakpoints on instructions that belong to inlined calls. We
do not do this for 'step'.
* If we are inside an inlined call that makes other inlined
functions, 'next' will not set any breakpoints that belong to
inlined calls that are children of the current inlined call.
* If the current function is inlined the breakpoint on the return
address won't be set, because inlined frames don't have a return
address.
* The code we use for stepout doesn't work at all if we are inside
an inlined call, instead we call 'next' but instruct it to remove
all PCs belonging to the current inlined call.
2017-11-13 15:54:08 +00:00
|
|
|
success = true
|
2017-02-22 08:35:21 +00:00
|
|
|
return Continue(dbp)
|
2016-04-14 09:58:59 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 14:18:10 +00:00
|
|
|
// GoroutinesInfo searches for goroutines starting at index 'start', and
|
|
|
|
// returns an array of up to 'count' (or all found elements, if 'count' is 0)
|
|
|
|
// G structures representing the information Delve care about from the internal
|
|
|
|
// runtime G structure.
|
|
|
|
// GoroutinesInfo also returns the next index to be used as 'start' argument
|
|
|
|
// while scanning for all available goroutines, or -1 if there was an error
|
|
|
|
// or if the index already reached the last possible value.
|
|
|
|
func GoroutinesInfo(dbp Process, start, count int) ([]*G, int, error) {
|
2018-06-21 11:07:37 +00:00
|
|
|
if _, err := dbp.Valid(); err != nil {
|
2018-11-19 14:18:10 +00:00
|
|
|
return nil, -1, err
|
2016-01-30 06:23:14 +00:00
|
|
|
}
|
2018-05-10 19:25:59 +00:00
|
|
|
if dbp.Common().allGCache != nil {
|
2018-11-19 14:18:10 +00:00
|
|
|
// We can't use the cached array to fulfill a subrange request
|
|
|
|
if start == 0 && (count == 0 || count >= len(dbp.Common().allGCache)) {
|
|
|
|
return dbp.Common().allGCache, -1, nil
|
|
|
|
}
|
2015-09-17 17:33:42 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 21:06:38 +00:00
|
|
|
exeimage := dbp.BinInfo().Images[0] // Image corresponding to the executable file
|
|
|
|
|
2015-04-09 14:53:02 +00:00
|
|
|
var (
|
2017-09-01 13:34:13 +00:00
|
|
|
threadg = map[int]*G{}
|
2015-06-17 17:11:57 +00:00
|
|
|
allg []*G
|
2019-05-08 21:06:38 +00:00
|
|
|
rdr = exeimage.DwarfReader()
|
2015-04-09 14:53:02 +00:00
|
|
|
)
|
|
|
|
|
2017-02-22 08:35:21 +00:00
|
|
|
threads := dbp.ThreadList()
|
|
|
|
for _, th := range threads {
|
2017-04-21 06:55:53 +00:00
|
|
|
if th.Blocked() {
|
2015-06-17 17:11:57 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
g, _ := GetG(th)
|
2015-06-17 17:11:57 +00:00
|
|
|
if g != nil {
|
2017-09-01 13:34:13 +00:00
|
|
|
threadg[g.ID] = g
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 21:06:38 +00:00
|
|
|
addr, err := rdr.AddrFor("runtime.allglen", exeimage.StaticBase)
|
2015-04-09 14:53:02 +00:00
|
|
|
if err != nil {
|
2018-11-19 14:18:10 +00:00
|
|
|
return nil, -1, err
|
2015-04-09 14:53:02 +00:00
|
|
|
}
|
2017-04-18 14:24:45 +00:00
|
|
|
allglenBytes := make([]byte, 8)
|
|
|
|
_, err = dbp.CurrentThread().ReadMemory(allglenBytes, uintptr(addr))
|
2015-05-09 16:25:26 +00:00
|
|
|
if err != nil {
|
2018-11-19 14:18:10 +00:00
|
|
|
return nil, -1, err
|
2015-05-09 16:25:26 +00:00
|
|
|
}
|
|
|
|
allglen := binary.LittleEndian.Uint64(allglenBytes)
|
|
|
|
|
|
|
|
rdr.Seek(0)
|
2019-05-08 21:06:38 +00:00
|
|
|
allgentryaddr, err := rdr.AddrFor("runtime.allgs", exeimage.StaticBase)
|
2015-04-09 14:53:02 +00:00
|
|
|
if err != nil {
|
2015-10-28 12:17:37 +00:00
|
|
|
// try old name (pre Go 1.6)
|
2019-05-08 21:06:38 +00:00
|
|
|
allgentryaddr, err = rdr.AddrFor("runtime.allg", exeimage.StaticBase)
|
2015-10-28 12:17:37 +00:00
|
|
|
if err != nil {
|
2018-11-19 14:18:10 +00:00
|
|
|
return nil, -1, err
|
2015-10-28 12:17:37 +00:00
|
|
|
}
|
2015-04-09 14:53:02 +00:00
|
|
|
}
|
2017-04-21 06:55:53 +00:00
|
|
|
faddr := make([]byte, dbp.BinInfo().Arch.PtrSize())
|
2017-04-18 14:24:45 +00:00
|
|
|
_, err = dbp.CurrentThread().ReadMemory(faddr, uintptr(allgentryaddr))
|
2017-06-29 18:15:59 +00:00
|
|
|
if err != nil {
|
2018-11-19 14:18:10 +00:00
|
|
|
return nil, -1, err
|
2017-06-29 18:15:59 +00:00
|
|
|
}
|
2015-04-09 14:53:02 +00:00
|
|
|
allgptr := binary.LittleEndian.Uint64(faddr)
|
|
|
|
|
2018-11-19 14:18:10 +00:00
|
|
|
for i := uint64(start); i < allglen; i++ {
|
|
|
|
if count != 0 && len(allg) >= count {
|
|
|
|
return allg, int(i), nil
|
|
|
|
}
|
2017-04-21 06:55:53 +00:00
|
|
|
gvar, err := newGVariable(dbp.CurrentThread(), uintptr(allgptr+(i*uint64(dbp.BinInfo().Arch.PtrSize()))), true)
|
2016-01-10 17:29:14 +00:00
|
|
|
if err != nil {
|
2018-09-25 12:53:44 +00:00
|
|
|
allg = append(allg, &G{Unreadable: err})
|
|
|
|
continue
|
2016-01-10 17:29:14 +00:00
|
|
|
}
|
|
|
|
g, err := gvar.parseG()
|
2015-04-09 14:53:02 +00:00
|
|
|
if err != nil {
|
2018-09-25 12:53:44 +00:00
|
|
|
allg = append(allg, &G{Unreadable: err})
|
|
|
|
continue
|
2015-04-09 14:53:02 +00:00
|
|
|
}
|
2017-09-01 13:34:13 +00:00
|
|
|
if thg, allocated := threadg[g.ID]; allocated {
|
|
|
|
loc, err := thg.Thread.Location()
|
2015-07-08 02:21:47 +00:00
|
|
|
if err != nil {
|
2018-11-19 14:18:10 +00:00
|
|
|
return nil, -1, err
|
2015-07-08 02:21:47 +00:00
|
|
|
}
|
2017-09-01 13:34:13 +00:00
|
|
|
g.Thread = thg.Thread
|
2015-07-08 02:21:47 +00:00
|
|
|
// Prefer actual thread location information.
|
2015-10-18 22:02:14 +00:00
|
|
|
g.CurrentLoc = *loc
|
2017-09-01 13:34:13 +00:00
|
|
|
g.SystemStack = thg.SystemStack
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
2015-09-17 17:42:05 +00:00
|
|
|
if g.Status != Gdead {
|
|
|
|
allg = append(allg, g)
|
|
|
|
}
|
2015-04-09 14:53:02 +00:00
|
|
|
}
|
2018-12-06 08:28:38 +00:00
|
|
|
if start == 0 {
|
|
|
|
dbp.Common().allGCache = allg
|
|
|
|
}
|
2017-02-22 08:35:21 +00:00
|
|
|
|
2018-11-19 14:18:10 +00:00
|
|
|
return allg, -1, nil
|
2015-04-09 14:53:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// FindGoroutine returns a G struct representing the goroutine
|
|
|
|
// specified by `gid`.
|
2017-04-21 07:50:38 +00:00
|
|
|
func FindGoroutine(dbp Process, gid int) (*G, error) {
|
2019-02-26 17:22:33 +00:00
|
|
|
if selg := dbp.SelectedGoroutine(); (gid == -1) || (selg != nil && selg.ID == gid) || (selg == nil && gid == 0) {
|
|
|
|
// Return the currently selected goroutine in the following circumstances:
|
|
|
|
//
|
|
|
|
// 1. if the caller asks for gid == -1 (because that's what a goroutine ID of -1 means in our API).
|
|
|
|
// 2. if gid == selg.ID.
|
|
|
|
// this serves two purposes: (a) it's an optimizations that allows us
|
|
|
|
// to avoid reading any other goroutine and, more importantly, (b) we
|
|
|
|
// could be reading an incorrect value for the goroutine ID of a thread.
|
|
|
|
// This condition usually happens when a goroutine calls runtime.clone
|
|
|
|
// and for a short period of time two threads will appear to be running
|
|
|
|
// the same goroutine.
|
|
|
|
// 3. if the caller asks for gid == 0 and the selected goroutine is
|
|
|
|
// either 0 or nil.
|
|
|
|
// Goroutine 0 is special, it either means we have no current goroutine
|
|
|
|
// (for example, running C code), or that we are running on a speical
|
|
|
|
// stack (system stack, signal handling stack) and we didn't properly
|
|
|
|
// detect it.
|
|
|
|
// Since there could be multiple goroutines '0' running simultaneously
|
|
|
|
// if the user requests it return the one that's already selected or
|
|
|
|
// nil if there isn't a selected goroutine.
|
|
|
|
return selg, nil
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 15:13:48 +00:00
|
|
|
if gid == 0 {
|
2018-12-06 08:32:20 +00:00
|
|
|
return nil, fmt.Errorf("Unknown goroutine %d", gid)
|
2018-11-28 15:13:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 08:32:20 +00:00
|
|
|
// Calling GoroutinesInfo could be slow if there are many goroutines
|
|
|
|
// running, check if a running goroutine has been requested first.
|
|
|
|
for _, thread := range dbp.ThreadList() {
|
|
|
|
g, _ := GetG(thread)
|
|
|
|
if g != nil && g.ID == gid {
|
|
|
|
return g, nil
|
|
|
|
}
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
2018-12-06 08:32:20 +00:00
|
|
|
|
|
|
|
const goroutinesInfoLimit = 10
|
|
|
|
nextg := 0
|
|
|
|
for nextg >= 0 {
|
|
|
|
var gs []*G
|
|
|
|
var err error
|
|
|
|
gs, nextg, err = GoroutinesInfo(dbp, nextg, goroutinesInfoLimit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := range gs {
|
|
|
|
if gs[i].ID == gid {
|
|
|
|
if gs[i].Unreadable != nil {
|
|
|
|
return nil, gs[i].Unreadable
|
|
|
|
}
|
|
|
|
return gs[i], nil
|
2018-09-25 12:53:44 +00:00
|
|
|
}
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 08:32:20 +00:00
|
|
|
|
2015-08-28 20:06:29 +00:00
|
|
|
return nil, fmt.Errorf("Unknown goroutine %d", gid)
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// ConvertEvalScope returns a new EvalScope in the context of the
|
|
|
|
// specified goroutine ID and stack frame.
|
2018-07-10 10:15:11 +00:00
|
|
|
// If deferCall is > 0 the eval scope will be relative to the specified deferred call.
|
|
|
|
func ConvertEvalScope(dbp Process, gid, frame, deferCall int) (*EvalScope, error) {
|
2018-06-21 11:07:37 +00:00
|
|
|
if _, err := dbp.Valid(); err != nil {
|
|
|
|
return nil, err
|
2017-07-26 22:57:47 +00:00
|
|
|
}
|
2017-02-15 13:41:03 +00:00
|
|
|
ct := dbp.CurrentThread()
|
|
|
|
g, err := FindGoroutine(dbp, gid)
|
2015-08-28 20:06:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if g == nil {
|
2017-02-15 13:41:03 +00:00
|
|
|
return ThreadScope(ct)
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 06:55:53 +00:00
|
|
|
var thread MemoryReadWriter
|
|
|
|
if g.Thread == nil {
|
2017-02-15 13:41:03 +00:00
|
|
|
thread = ct
|
2015-08-28 20:06:29 +00:00
|
|
|
} else {
|
2017-04-21 06:55:53 +00:00
|
|
|
thread = g.Thread
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 17:21:20 +00:00
|
|
|
var opts StacktraceOptions
|
|
|
|
if deferCall > 0 {
|
|
|
|
opts = StacktraceReadDefers
|
|
|
|
}
|
|
|
|
|
|
|
|
locs, err := g.Stacktrace(frame+1, opts)
|
2015-08-28 20:06:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if frame >= len(locs) {
|
|
|
|
return nil, fmt.Errorf("Frame %d does not exist in goroutine %d", frame, gid)
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:15:11 +00:00
|
|
|
if deferCall > 0 {
|
|
|
|
if deferCall-1 >= len(locs[frame].Defers) {
|
|
|
|
return nil, fmt.Errorf("Frame %d only has %d deferred calls", frame, len(locs[frame].Defers))
|
|
|
|
}
|
|
|
|
|
|
|
|
d := locs[frame].Defers[deferCall-1]
|
|
|
|
if d.Unreadable != nil {
|
|
|
|
return nil, d.Unreadable
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.EvalScope(ct)
|
|
|
|
}
|
|
|
|
|
2018-04-14 13:44:39 +00:00
|
|
|
return FrameToScope(dbp.BinInfo(), thread, g, locs[frame:]...), nil
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
2015-10-09 23:30:28 +00:00
|
|
|
|
2018-04-14 12:57:35 +00:00
|
|
|
// FrameToScope returns a new EvalScope for frames[0].
|
|
|
|
// If frames has at least two elements all memory between
|
|
|
|
// frames[0].Regs.SP() and frames[1].Regs.CFA will be cached.
|
|
|
|
// Otherwise all memory between frames[0].Regs.SP() and frames[0].Regs.CFA
|
|
|
|
// will be cached.
|
2018-04-14 13:44:39 +00:00
|
|
|
func FrameToScope(bi *BinaryInfo, thread MemoryReadWriter, g *G, frames ...Stackframe) *EvalScope {
|
|
|
|
// Creates a cacheMem that will preload the entire stack frame the first
|
|
|
|
// time any local variable is read.
|
2018-04-15 12:11:27 +00:00
|
|
|
// Remember that the stack grows downward in memory.
|
2018-04-14 13:44:39 +00:00
|
|
|
minaddr := frames[0].Regs.SP()
|
|
|
|
var maxaddr uint64
|
|
|
|
if len(frames) > 1 && frames[0].SystemStack == frames[1].SystemStack {
|
|
|
|
maxaddr = uint64(frames[1].Regs.CFA)
|
|
|
|
} else {
|
|
|
|
maxaddr = uint64(frames[0].Regs.CFA)
|
|
|
|
}
|
|
|
|
if maxaddr > minaddr && maxaddr-minaddr < maxFramePrefetchSize {
|
|
|
|
thread = cacheMemory(thread, uintptr(minaddr), int(maxaddr-minaddr))
|
|
|
|
}
|
|
|
|
|
2019-06-30 17:44:30 +00:00
|
|
|
s := &EvalScope{Location: frames[0].Call, Regs: frames[0].Regs, Mem: thread, g: g, BinInfo: bi, frameOffset: frames[0].FrameOffset()}
|
2018-04-14 13:44:39 +00:00
|
|
|
s.PC = frames[0].lastpc
|
2018-02-13 17:20:45 +00:00
|
|
|
return s
|
2017-02-15 13:41:03 +00:00
|
|
|
}
|
2018-03-20 11:34:02 +00:00
|
|
|
|
2018-11-12 22:52:13 +00:00
|
|
|
// createUnrecoveredPanicBreakpoint creates the unrecoverable-panic breakpoint.
|
2018-03-20 11:34:02 +00:00
|
|
|
// This function is meant to be called by implementations of the Process interface.
|
2018-11-12 22:52:13 +00:00
|
|
|
func createUnrecoveredPanicBreakpoint(p Process, writeBreakpoint WriteBreakpointFn) {
|
2019-11-01 19:41:06 +00:00
|
|
|
panicpcs, err := FindFunctionLocation(p, "runtime.startpanic", 0)
|
2018-08-31 18:08:18 +00:00
|
|
|
if _, isFnNotFound := err.(*ErrFunctionNotFound); isFnNotFound {
|
2019-11-01 19:41:06 +00:00
|
|
|
panicpcs, err = FindFunctionLocation(p, "runtime.fatalpanic", 0)
|
2018-03-20 11:34:02 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2019-11-01 19:41:06 +00:00
|
|
|
bp, err := p.Breakpoints().SetWithID(unrecoveredPanicID, panicpcs[0], writeBreakpoint)
|
2018-03-20 11:34:02 +00:00
|
|
|
if err == nil {
|
|
|
|
bp.Name = UnrecoveredPanic
|
|
|
|
bp.Variables = []string{"runtime.curg._panic.arg"}
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 22:28:25 +00:00
|
|
|
}
|
2018-03-20 11:34:02 +00:00
|
|
|
|
2019-02-27 22:28:25 +00:00
|
|
|
func createFatalThrowBreakpoint(p Process, writeBreakpoint WriteBreakpointFn) {
|
2019-11-01 19:41:06 +00:00
|
|
|
fatalpcs, err := FindFunctionLocation(p, "runtime.fatalthrow", 0)
|
2019-02-27 22:28:25 +00:00
|
|
|
if err == nil {
|
2019-11-01 19:41:06 +00:00
|
|
|
bp, err := p.Breakpoints().SetWithID(fatalThrowID, fatalpcs[0], writeBreakpoint)
|
2019-02-27 22:28:25 +00:00
|
|
|
if err == nil {
|
|
|
|
bp.Name = FatalThrow
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 11:34:02 +00:00
|
|
|
}
|
2018-03-25 08:04:32 +00:00
|
|
|
|
|
|
|
// FirstPCAfterPrologue returns the address of the first
|
|
|
|
// instruction after the prologue for function fn.
|
|
|
|
// If sameline is set FirstPCAfterPrologue will always return an
|
|
|
|
// address associated with the same line as fn.Entry.
|
|
|
|
func FirstPCAfterPrologue(p Process, fn *Function, sameline bool) (uint64, error) {
|
|
|
|
pc, _, line, ok := fn.cu.lineInfo.PrologueEndPC(fn.Entry, fn.End)
|
|
|
|
if ok {
|
|
|
|
if !sameline {
|
|
|
|
return pc, nil
|
2018-08-31 18:08:18 +00:00
|
|
|
}
|
|
|
|
_, entryLine := fn.cu.lineInfo.PCToLine(fn.Entry, fn.Entry)
|
|
|
|
if entryLine == line {
|
|
|
|
return pc, nil
|
2018-03-25 08:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, err := firstPCAfterPrologueDisassembly(p, fn, sameline)
|
|
|
|
if err != nil {
|
|
|
|
return fn.Entry, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pc == fn.Entry {
|
|
|
|
// Look for the first instruction with the stmt flag set, so that setting a
|
|
|
|
// breakpoint with file:line and with the function name always result on
|
|
|
|
// the same instruction being selected.
|
2019-11-01 19:41:06 +00:00
|
|
|
if pc2, _, _, ok := fn.cu.lineInfo.FirstStmtForLine(fn.Entry, fn.End); ok {
|
|
|
|
return pc2, nil
|
2018-03-25 08:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pc, nil
|
|
|
|
}
|