2015-06-12 19:49:23 +00:00
|
|
|
package proc
|
2015-01-14 02:37:10 +00:00
|
|
|
|
2016-01-12 08:01:42 +00:00
|
|
|
import (
|
2021-07-31 15:16:26 +00:00
|
|
|
"debug/dwarf"
|
2016-01-12 08:01:42 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/constant"
|
2021-05-06 17:33:56 +00:00
|
|
|
"go/parser"
|
2021-05-28 18:21:53 +00:00
|
|
|
"go/token"
|
2016-01-12 08:01:42 +00:00
|
|
|
"reflect"
|
2021-07-31 15:16:26 +00:00
|
|
|
|
2021-08-24 12:53:27 +00:00
|
|
|
"github.com/go-delve/delve/pkg/dwarf/godwarf"
|
2021-07-31 15:16:26 +00:00
|
|
|
"github.com/go-delve/delve/pkg/dwarf/op"
|
|
|
|
"github.com/go-delve/delve/pkg/dwarf/reader"
|
|
|
|
"github.com/go-delve/delve/pkg/goversion"
|
|
|
|
"github.com/go-delve/delve/pkg/proc/internal/ebpf"
|
2016-01-12 08:01:42 +00:00
|
|
|
)
|
2015-01-14 02:37:10 +00:00
|
|
|
|
2020-03-23 17:57:01 +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"
|
|
|
|
|
2022-02-22 17:57:37 +00:00
|
|
|
// HardcodedBreakpoint is the name given to hardcoded breakpoints (for
|
|
|
|
// example: calls to runtime.Breakpoint)
|
|
|
|
HardcodedBreakpoint = "hardcoded-breakpoint"
|
|
|
|
|
|
|
|
unrecoveredPanicID = -1
|
|
|
|
fatalThrowID = -2
|
|
|
|
hardcodedBreakpointID = -3
|
2021-09-29 10:01:37 +00:00
|
|
|
|
|
|
|
NoLogicalID = -1000 // Logical breakpoint ID for breakpoints internal breakpoints.
|
2020-03-23 17:57:01 +00:00
|
|
|
)
|
|
|
|
|
2019-11-01 19:41:06 +00:00
|
|
|
// Breakpoint represents a physical breakpoint. Stores information on the break
|
2015-01-14 02:37:10 +00:00
|
|
|
// point including the byte of data that originally was stored at that
|
|
|
|
// address.
|
2015-06-12 19:32:32 +00:00
|
|
|
type Breakpoint struct {
|
2015-04-22 13:18:25 +00:00
|
|
|
// File & line information for printing.
|
2015-01-14 02:37:10 +00:00
|
|
|
FunctionName string
|
|
|
|
File string
|
|
|
|
Line int
|
2015-04-22 13:18:25 +00:00
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
Addr uint64 // Address breakpoint is set for.
|
|
|
|
OriginalData []byte // If software breakpoint, the data we replace with breakpoint instruction.
|
|
|
|
|
2021-08-09 17:41:25 +00:00
|
|
|
WatchExpr string
|
|
|
|
WatchType WatchType
|
|
|
|
HWBreakIndex uint8 // hardware breakpoint index
|
|
|
|
watchStackOff int64 // for watchpoints of stack variables, offset of the address from top of the stack
|
2021-05-06 17:33:56 +00:00
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
// Breaklets is the list of overlapping breakpoints on this physical breakpoint.
|
|
|
|
// There can be at most one UserBreakpoint in this list but multiple internal breakpoints are allowed.
|
|
|
|
Breaklets []*Breaklet
|
|
|
|
|
|
|
|
// Breakpoint information
|
2022-05-25 20:58:26 +00:00
|
|
|
Logical *LogicalBreakpoint
|
2021-07-21 15:24:19 +00:00
|
|
|
|
|
|
|
// ReturnInfo describes how to collect return variables when this
|
|
|
|
// breakpoint is hit as a return breakpoint.
|
|
|
|
returnInfo *returnBreakpointInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Breaklet represents one of multiple breakpoints that can overlap on a
|
|
|
|
// single physical breakpoint.
|
|
|
|
type Breaklet struct {
|
|
|
|
// Kind describes whether this is a stepping breakpoint (for next'ing or
|
2017-09-25 06:29:13 +00:00
|
|
|
// stepping).
|
|
|
|
Kind BreakpointKind
|
2015-07-01 03:16:52 +00:00
|
|
|
|
2021-09-29 10:01:37 +00:00
|
|
|
LogicalID int // ID of the logical breakpoint that owns this physical breakpoint
|
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
// Cond: if not nil the breakpoint will be triggered only if evaluating Cond returns true
|
|
|
|
Cond ast.Expr
|
|
|
|
|
2016-07-26 13:34:31 +00:00
|
|
|
// DeferReturns: when kind == NextDeferBreakpoint this breakpoint
|
|
|
|
// will also check if the caller is runtime.gopanic or if the return
|
|
|
|
// address is in the DeferReturns array.
|
|
|
|
// Next uses NextDeferBreakpoints for the breakpoint it sets on the
|
2016-07-14 07:43:39 +00:00
|
|
|
// deferred function, DeferReturns is populated with the
|
|
|
|
// addresses of calls to runtime.deferreturn in the current
|
2018-10-16 15:49:20 +00:00
|
|
|
// function. This ensures that the breakpoint on the deferred
|
2016-07-14 07:43:39 +00:00
|
|
|
// function only triggers on panic or on the defer call to
|
|
|
|
// the function, not when the function is called directly
|
|
|
|
DeferReturns []uint64
|
2021-07-21 15:24:19 +00:00
|
|
|
|
2021-08-09 17:41:25 +00:00
|
|
|
// checkPanicCall checks that the breakpoint happened while the function was
|
|
|
|
// called by a panic. It is only checked for WatchOutOfScopeBreakpoint Kind.
|
|
|
|
checkPanicCall bool
|
|
|
|
|
|
|
|
// callback is called if every other condition for this breaklet is met,
|
|
|
|
// the return value will determine if the breaklet should be considered
|
|
|
|
// active.
|
|
|
|
// The callback can have side-effects.
|
2023-04-24 19:12:54 +00:00
|
|
|
callback func(th Thread, p *Target) (bool, error)
|
2021-08-09 17:41:25 +00:00
|
|
|
|
|
|
|
// For WatchOutOfScopeBreakpoints and StackResizeBreakpoints the watchpoint
|
|
|
|
// field contains the watchpoint related to this out of scope sentinel.
|
|
|
|
watchpoint *Breakpoint
|
2015-03-06 14:01:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
// BreakpointKind determines the behavior of delve when the
|
2016-07-26 13:34:31 +00:00
|
|
|
// breakpoint is reached.
|
2017-09-25 06:29:13 +00:00
|
|
|
type BreakpointKind uint16
|
2016-07-26 13:34:31 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
// UserBreakpoint is a user set breakpoint
|
2017-09-25 06:29:13 +00:00
|
|
|
UserBreakpoint BreakpointKind = (1 << iota)
|
2016-07-26 13:34:31 +00:00
|
|
|
// NextBreakpoint is a breakpoint set by Next, Continue
|
|
|
|
// will stop on it and delete it
|
|
|
|
NextBreakpoint
|
|
|
|
// NextDeferBreakpoint is a breakpoint set by Next on the
|
|
|
|
// first deferred function. In addition to checking their condition
|
|
|
|
// breakpoints of this kind will also check that the function has been
|
|
|
|
// called by runtime.gopanic or through runtime.deferreturn.
|
|
|
|
NextDeferBreakpoint
|
|
|
|
// StepBreakpoint is a breakpoint set by Step on a CALL instruction,
|
|
|
|
// Continue will set a new breakpoint (of NextBreakpoint kind) on the
|
|
|
|
// destination of CALL, delete this breakpoint and then continue again
|
|
|
|
StepBreakpoint
|
2021-07-21 15:24:19 +00:00
|
|
|
|
2021-08-09 17:41:25 +00:00
|
|
|
// WatchOutOfScopeBreakpoint is a breakpoint used to detect when a watched
|
|
|
|
// stack variable goes out of scope.
|
|
|
|
WatchOutOfScopeBreakpoint
|
|
|
|
|
|
|
|
// StackResizeBreakpoint is a breakpoint used to detect stack resizes to
|
|
|
|
// adjust the watchpoint of stack variables.
|
|
|
|
StackResizeBreakpoint
|
|
|
|
|
2022-10-04 15:07:05 +00:00
|
|
|
// PluginOpenBreakpoint is a breakpoint used to detect that a plugin has
|
|
|
|
// been loaded and we should try to enable suspended breakpoints.
|
|
|
|
PluginOpenBreakpoint
|
|
|
|
|
2024-04-09 13:53:23 +00:00
|
|
|
// StepIntoNewProc is a breakpoint used to step into a newly created
|
|
|
|
// goroutine.
|
|
|
|
StepIntoNewProcBreakpoint
|
|
|
|
|
|
|
|
steppingMask = NextBreakpoint | NextDeferBreakpoint | StepBreakpoint | StepIntoNewProcBreakpoint
|
2016-07-26 13:34:31 +00:00
|
|
|
)
|
|
|
|
|
2021-05-06 17:33:56 +00:00
|
|
|
// WatchType is the watchpoint type
|
|
|
|
type WatchType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
WatchRead WatchType = 1 << iota
|
|
|
|
WatchWrite
|
|
|
|
)
|
|
|
|
|
|
|
|
// Read returns true if the hardware breakpoint should trigger on memory reads.
|
|
|
|
func (wtype WatchType) Read() bool {
|
|
|
|
return wtype&WatchRead != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write returns true if the hardware breakpoint should trigger on memory writes.
|
|
|
|
func (wtype WatchType) Write() bool {
|
|
|
|
return wtype&WatchWrite != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size in bytes of the hardware breakpoint.
|
|
|
|
func (wtype WatchType) Size() int {
|
|
|
|
return int(wtype >> 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
// withSize returns a new HWBreakType with the size set to the specified value
|
|
|
|
func (wtype WatchType) withSize(sz uint8) WatchType {
|
|
|
|
return WatchType((sz << 4) | uint8(wtype&0xf))
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrHWBreakUnsupported = errors.New("hardware breakpoints not implemented")
|
|
|
|
|
2015-06-12 19:32:32 +00:00
|
|
|
func (bp *Breakpoint) String() string {
|
2021-09-29 10:01:37 +00:00
|
|
|
return fmt.Sprintf("Breakpoint %d at %#v %s:%d", bp.LogicalID(), bp.Addr, bp.File, bp.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *Breakpoint) LogicalID() int {
|
|
|
|
for _, breaklet := range bp.Breaklets {
|
|
|
|
if breaklet.Kind == UserBreakpoint {
|
|
|
|
return breaklet.LogicalID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoLogicalID
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 17:41:25 +00:00
|
|
|
// VerboseDescr returns a string describing parts of the breakpoint struct
|
|
|
|
// that aren't otherwise user visible, for debugging purposes.
|
|
|
|
func (bp *Breakpoint) VerboseDescr() []string {
|
|
|
|
r := []string{}
|
|
|
|
|
|
|
|
r = append(r, fmt.Sprintf("OriginalData=%#x", bp.OriginalData))
|
|
|
|
|
|
|
|
if bp.WatchType != 0 {
|
|
|
|
r = append(r, fmt.Sprintf("HWBreakIndex=%#x watchStackOff=%#x", bp.HWBreakIndex, bp.watchStackOff))
|
|
|
|
}
|
|
|
|
|
2022-05-25 20:58:26 +00:00
|
|
|
lbp := bp.Logical
|
|
|
|
|
2021-08-09 17:41:25 +00:00
|
|
|
for _, breaklet := range bp.Breaklets {
|
|
|
|
switch breaklet.Kind {
|
|
|
|
case UserBreakpoint:
|
2022-05-25 20:58:26 +00:00
|
|
|
r = append(r, fmt.Sprintf("User Cond=%q HitCond=%v", exprToString(breaklet.Cond), lbp.HitCond))
|
2021-08-09 17:41:25 +00:00
|
|
|
case NextBreakpoint:
|
|
|
|
r = append(r, fmt.Sprintf("Next Cond=%q", exprToString(breaklet.Cond)))
|
|
|
|
case NextDeferBreakpoint:
|
|
|
|
r = append(r, fmt.Sprintf("NextDefer Cond=%q DeferReturns=%#x", exprToString(breaklet.Cond), breaklet.DeferReturns))
|
|
|
|
case StepBreakpoint:
|
|
|
|
r = append(r, fmt.Sprintf("Step Cond=%q", exprToString(breaklet.Cond)))
|
|
|
|
case WatchOutOfScopeBreakpoint:
|
|
|
|
r = append(r, fmt.Sprintf("WatchOutOfScope Cond=%q checkPanicCall=%v", exprToString(breaklet.Cond), breaklet.checkPanicCall))
|
|
|
|
case StackResizeBreakpoint:
|
|
|
|
r = append(r, fmt.Sprintf("StackResizeBreakpoint Cond=%q", exprToString(breaklet.Cond)))
|
2022-10-04 15:07:05 +00:00
|
|
|
case PluginOpenBreakpoint:
|
|
|
|
r = append(r, "PluginOpenBreakpoint")
|
2024-04-09 13:53:23 +00:00
|
|
|
case StepIntoNewProcBreakpoint:
|
|
|
|
r = append(r, "StepIntoNewProcBreakpoint")
|
2021-08-09 17:41:25 +00:00
|
|
|
default:
|
|
|
|
r = append(r, fmt.Sprintf("Unknown %d", breaklet.Kind))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// BreakpointExistsError is returned when trying to set a breakpoint at
|
2015-01-14 02:37:10 +00:00
|
|
|
// an address that already has a breakpoint set for it.
|
2015-06-12 19:32:32 +00:00
|
|
|
type BreakpointExistsError struct {
|
2017-04-21 06:55:53 +00:00
|
|
|
File string
|
|
|
|
Line int
|
|
|
|
Addr uint64
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 19:32:32 +00:00
|
|
|
func (bpe BreakpointExistsError) Error() string {
|
2017-04-21 06:55:53 +00:00
|
|
|
return fmt.Sprintf("Breakpoint exists at %s:%d at %x", bpe.File, bpe.Line, bpe.Addr)
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidAddressError represents the result of
|
|
|
|
// attempting to set a breakpoint at an invalid address.
|
|
|
|
type InvalidAddressError struct {
|
2017-04-21 06:55:53 +00:00
|
|
|
Address uint64
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (iae InvalidAddressError) Error() string {
|
2017-04-21 06:55:53 +00:00
|
|
|
return fmt.Sprintf("Invalid address %#v\n", iae.Address)
|
2015-06-26 13:59:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 12:51:15 +00:00
|
|
|
type returnBreakpointInfo struct {
|
|
|
|
retFrameCond ast.Expr
|
|
|
|
fn *Function
|
|
|
|
frameOffset int64
|
|
|
|
spOffset int64
|
|
|
|
}
|
|
|
|
|
2017-04-21 06:55:53 +00:00
|
|
|
// CheckCondition evaluates bp's condition on thread.
|
2021-08-09 17:16:24 +00:00
|
|
|
func (bp *Breakpoint) checkCondition(tgt *Target, thread Thread, bpstate *BreakpointState) {
|
|
|
|
*bpstate = BreakpointState{Breakpoint: bp, Active: false, Stepping: false, SteppingInto: false, CondError: nil}
|
2021-07-21 15:24:19 +00:00
|
|
|
for _, breaklet := range bp.Breaklets {
|
2021-08-09 17:16:24 +00:00
|
|
|
bpstate.checkCond(tgt, breaklet, thread)
|
2021-05-28 18:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 17:16:24 +00:00
|
|
|
func (bpstate *BreakpointState) checkCond(tgt *Target, breaklet *Breaklet, thread Thread) {
|
2021-07-21 15:24:19 +00:00
|
|
|
var condErr error
|
|
|
|
active := true
|
|
|
|
if breaklet.Cond != nil {
|
2021-08-09 17:16:24 +00:00
|
|
|
active, condErr = evalBreakpointCondition(tgt, thread, breaklet.Cond)
|
2021-07-21 15:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if condErr != nil && bpstate.CondError == nil {
|
|
|
|
bpstate.CondError = condErr
|
|
|
|
}
|
|
|
|
if !active {
|
2021-05-28 18:21:53 +00:00
|
|
|
return
|
2015-11-18 09:07:08 +00:00
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
|
|
|
|
switch breaklet.Kind {
|
|
|
|
case UserBreakpoint:
|
2022-08-16 16:31:11 +00:00
|
|
|
var goroutineID int64
|
2022-05-25 20:58:26 +00:00
|
|
|
lbp := bpstate.Breakpoint.Logical
|
|
|
|
if lbp != nil {
|
|
|
|
if g, err := GetG(thread); err == nil {
|
2022-07-12 08:31:34 +00:00
|
|
|
goroutineID = g.ID
|
|
|
|
lbp.HitCount[goroutineID]++
|
2022-05-25 20:58:26 +00:00
|
|
|
}
|
|
|
|
lbp.TotalHitCount++
|
2021-07-21 15:24:19 +00:00
|
|
|
}
|
2022-07-12 08:31:34 +00:00
|
|
|
active = checkHitCond(lbp, goroutineID)
|
2021-07-21 15:24:19 +00:00
|
|
|
|
|
|
|
case StepBreakpoint, NextBreakpoint, NextDeferBreakpoint:
|
|
|
|
nextDeferOk := true
|
|
|
|
if breaklet.Kind&NextDeferBreakpoint != 0 {
|
|
|
|
var err error
|
2023-06-27 16:33:07 +00:00
|
|
|
frames, err := ThreadStacktrace(tgt, thread, 2)
|
2021-07-21 15:24:19 +00:00
|
|
|
if err == nil {
|
|
|
|
nextDeferOk, _ = isPanicCall(frames)
|
|
|
|
if !nextDeferOk {
|
|
|
|
nextDeferOk, _ = isDeferReturnCall(frames, breaklet.DeferReturns)
|
|
|
|
}
|
2016-07-14 07:43:39 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
active = active && nextDeferOk
|
|
|
|
|
2021-08-09 17:41:25 +00:00
|
|
|
case WatchOutOfScopeBreakpoint:
|
|
|
|
if breaklet.checkPanicCall {
|
2023-06-27 16:33:07 +00:00
|
|
|
frames, err := ThreadStacktrace(tgt, thread, 2)
|
2021-08-09 17:41:25 +00:00
|
|
|
if err == nil {
|
|
|
|
ipc, _ := isPanicCall(frames)
|
|
|
|
active = active && ipc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:53:23 +00:00
|
|
|
case StackResizeBreakpoint, PluginOpenBreakpoint, StepIntoNewProcBreakpoint:
|
2021-08-09 17:41:25 +00:00
|
|
|
// no further checks
|
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
default:
|
|
|
|
bpstate.CondError = fmt.Errorf("internal error unknown breakpoint kind %v", breaklet.Kind)
|
2016-07-14 07:43:39 +00:00
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
|
|
|
|
if active {
|
2021-08-09 17:41:25 +00:00
|
|
|
if breaklet.callback != nil {
|
2023-04-24 19:12:54 +00:00
|
|
|
var err error
|
|
|
|
active, err = breaklet.callback(thread, tgt)
|
|
|
|
if err != nil && bpstate.CondError == nil {
|
|
|
|
bpstate.CondError = err
|
|
|
|
}
|
2021-08-09 17:41:25 +00:00
|
|
|
}
|
|
|
|
bpstate.Active = active
|
2021-05-28 18:21:53 +00:00
|
|
|
}
|
2023-04-24 19:12:54 +00:00
|
|
|
|
|
|
|
if bpstate.Active {
|
|
|
|
switch breaklet.Kind {
|
|
|
|
case NextBreakpoint, NextDeferBreakpoint:
|
|
|
|
bpstate.Stepping = true
|
|
|
|
case StepBreakpoint:
|
|
|
|
bpstate.Stepping = true
|
|
|
|
bpstate.SteppingInto = true
|
|
|
|
}
|
|
|
|
}
|
2021-05-28 18:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkHitCond evaluates bp's hit condition on thread.
|
2022-08-16 16:31:11 +00:00
|
|
|
func checkHitCond(lbp *LogicalBreakpoint, goroutineID int64) bool {
|
2022-05-25 20:58:26 +00:00
|
|
|
if lbp == nil || lbp.HitCond == nil {
|
2021-07-21 15:24:19 +00:00
|
|
|
return true
|
2021-05-28 18:21:53 +00:00
|
|
|
}
|
2022-07-12 08:31:34 +00:00
|
|
|
hitCount := int(lbp.TotalHitCount)
|
|
|
|
if lbp.HitCondPerG && goroutineID > 0 {
|
|
|
|
hitCount = int(lbp.HitCount[goroutineID])
|
|
|
|
}
|
2021-05-28 18:21:53 +00:00
|
|
|
// Evaluate the breakpoint condition.
|
2022-05-25 20:58:26 +00:00
|
|
|
switch lbp.HitCond.Op {
|
2021-05-28 18:21:53 +00:00
|
|
|
case token.EQL:
|
2022-07-12 08:31:34 +00:00
|
|
|
return hitCount == lbp.HitCond.Val
|
2021-05-28 18:21:53 +00:00
|
|
|
case token.NEQ:
|
2022-07-12 08:31:34 +00:00
|
|
|
return hitCount != lbp.HitCond.Val
|
2021-05-28 18:21:53 +00:00
|
|
|
case token.GTR:
|
2022-07-12 08:31:34 +00:00
|
|
|
return hitCount > lbp.HitCond.Val
|
2021-05-28 18:21:53 +00:00
|
|
|
case token.LSS:
|
2022-07-12 08:31:34 +00:00
|
|
|
return hitCount < lbp.HitCond.Val
|
2021-05-28 18:21:53 +00:00
|
|
|
case token.GEQ:
|
2022-07-12 08:31:34 +00:00
|
|
|
return hitCount >= lbp.HitCond.Val
|
2021-05-28 18:21:53 +00:00
|
|
|
case token.LEQ:
|
2022-07-12 08:31:34 +00:00
|
|
|
return hitCount <= lbp.HitCond.Val
|
2021-05-28 18:21:53 +00:00
|
|
|
case token.REM:
|
2022-07-12 08:31:34 +00:00
|
|
|
return hitCount%lbp.HitCond.Val == 0
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
return false
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 15:47:53 +00:00
|
|
|
func isPanicCall(frames []Stackframe) (bool, int) {
|
|
|
|
// In Go prior to 1.17 the call stack for a panic is:
|
|
|
|
// 0. deferred function call
|
|
|
|
// 1. runtime.callN
|
|
|
|
// 2. runtime.gopanic
|
|
|
|
// in Go after 1.17 it is either:
|
|
|
|
// 0. deferred function call
|
|
|
|
// 1. deferred call wrapper
|
|
|
|
// 2. runtime.gopanic
|
|
|
|
// or:
|
|
|
|
// 0. deferred function call
|
|
|
|
// 1. runtime.gopanic
|
|
|
|
if len(frames) >= 3 && frames[2].Current.Fn != nil && frames[2].Current.Fn.Name == "runtime.gopanic" {
|
|
|
|
return true, 2
|
|
|
|
}
|
|
|
|
if len(frames) >= 2 && frames[1].Current.Fn != nil && frames[1].Current.Fn.Name == "runtime.gopanic" {
|
|
|
|
return true, 1
|
|
|
|
}
|
|
|
|
return false, 0
|
proc,terminal: Implement reverse step, next and stepout (#1785)
* proc: move defer breakpoint code into a function
Moves the code that sets a breakpoint on the first deferred function,
used by both next and StepOut, to its function.
* proc: implement reverse step/next/stepout
When the direction of execution is reversed (on a recording) Step, Next and
StepOut will behave similarly to their forward version. However there are
some subtle interactions between their behavior, prologue skipping, deferred
calls and normal calls. Specifically:
- when stepping backwards we need to set a breakpoint on the first
instruction after each CALL instruction, once this breakpoint is reached we
need to execute a single StepInstruction operation to reverse step into the
CALL.
- to insure that the prologue is skipped reverse next needs to check if it
is on the first instruction after the prologue, and if it is behave like
reverse stepout.
- there is no reason to set breakpoints on deferred calls when reverse
nexting or reverse stepping out, they will never be hit.
- reverse step out should generally place its breakpoint on the CALL
instruction that created the current stack frame (which will be the CALL
instruction immediately preceding the instruction at the return address).
- reverse step out needs to treat panic calls and deferreturn calls
specially.
* service,terminal: implement reverse step, next, stepout
2020-03-11 22:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isDeferReturnCall(frames []Stackframe, deferReturns []uint64) (bool, uint64) {
|
2021-08-22 14:46:00 +00:00
|
|
|
if len(frames) >= 2 && (len(deferReturns) > 0) {
|
|
|
|
// On Go 1.18 and later runtime.deferreturn doesn't use jmpdefer anymore,
|
|
|
|
// it's a normal function making normal calls to deferred functions.
|
|
|
|
if frames[1].Current.Fn != nil && frames[1].Current.Fn.Name == "runtime.deferreturn" {
|
|
|
|
return true, 0
|
|
|
|
}
|
|
|
|
}
|
proc,terminal: Implement reverse step, next and stepout (#1785)
* proc: move defer breakpoint code into a function
Moves the code that sets a breakpoint on the first deferred function,
used by both next and StepOut, to its function.
* proc: implement reverse step/next/stepout
When the direction of execution is reversed (on a recording) Step, Next and
StepOut will behave similarly to their forward version. However there are
some subtle interactions between their behavior, prologue skipping, deferred
calls and normal calls. Specifically:
- when stepping backwards we need to set a breakpoint on the first
instruction after each CALL instruction, once this breakpoint is reached we
need to execute a single StepInstruction operation to reverse step into the
CALL.
- to insure that the prologue is skipped reverse next needs to check if it
is on the first instruction after the prologue, and if it is behave like
reverse stepout.
- there is no reason to set breakpoints on deferred calls when reverse
nexting or reverse stepping out, they will never be hit.
- reverse step out should generally place its breakpoint on the CALL
instruction that created the current stack frame (which will be the CALL
instruction immediately preceding the instruction at the return address).
- reverse step out needs to treat panic calls and deferreturn calls
specially.
* service,terminal: implement reverse step, next, stepout
2020-03-11 22:40:41 +00:00
|
|
|
if len(frames) >= 1 {
|
|
|
|
for _, pc := range deferReturns {
|
|
|
|
if frames[0].Ret == pc {
|
|
|
|
return true, pc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
// IsStepping returns true if bp is an stepping breakpoint.
|
|
|
|
// User-set breakpoints can overlap with stepping breakpoints, in that case
|
|
|
|
// both IsUser and IsStepping will be true.
|
|
|
|
func (bp *Breakpoint) IsStepping() bool {
|
|
|
|
for _, breaklet := range bp.Breaklets {
|
|
|
|
if breaklet.Kind&steppingMask != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsUser returns true if bp is a user-set breakpoint.
|
2021-07-21 15:24:19 +00:00
|
|
|
// User-set breakpoints can overlap with stepping breakpoints, in that case
|
|
|
|
// both IsUser and IsStepping will be true.
|
2017-09-25 06:29:13 +00:00
|
|
|
func (bp *Breakpoint) IsUser() bool {
|
2021-07-21 15:24:19 +00:00
|
|
|
for _, breaklet := range bp.Breaklets {
|
|
|
|
if breaklet.Kind == UserBreakpoint {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserBreaklet returns the user breaklet for this breakpoint, or nil if
|
|
|
|
// none exist.
|
|
|
|
func (bp *Breakpoint) UserBreaklet() *Breaklet {
|
|
|
|
for _, breaklet := range bp.Breaklets {
|
|
|
|
if breaklet.Kind == UserBreakpoint {
|
|
|
|
return breaklet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-05-16 18:23:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 17:16:24 +00:00
|
|
|
func evalBreakpointCondition(tgt *Target, thread Thread, cond ast.Expr) (bool, error) {
|
2017-09-25 06:29:13 +00:00
|
|
|
if cond == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-08-09 17:16:24 +00:00
|
|
|
scope, err := GoroutineScope(tgt, thread)
|
2015-11-18 09:07:08 +00:00
|
|
|
if err != nil {
|
2021-08-09 17:16:24 +00:00
|
|
|
scope, err = ThreadScope(tgt, thread)
|
2020-08-08 13:06:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
2015-11-18 09:07:08 +00:00
|
|
|
}
|
2017-05-16 18:23:33 +00:00
|
|
|
v, err := scope.evalAST(cond)
|
2016-01-12 08:01:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, fmt.Errorf("error evaluating expression: %v", err)
|
|
|
|
}
|
|
|
|
if v.Kind != reflect.Bool {
|
|
|
|
return true, errors.New("condition expression not boolean")
|
|
|
|
}
|
2018-07-07 08:31:56 +00:00
|
|
|
v.loadValue(loadFullValue)
|
|
|
|
if v.Unreadable != nil {
|
|
|
|
return true, fmt.Errorf("condition expression unreadable: %v", v.Unreadable)
|
|
|
|
}
|
2016-01-12 08:01:42 +00:00
|
|
|
return constant.BoolVal(v.Value), nil
|
2015-11-18 09:07:08 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// NoBreakpointError is returned when trying to
|
|
|
|
// clear a breakpoint that does not exist.
|
2015-06-12 19:32:32 +00:00
|
|
|
type NoBreakpointError struct {
|
2017-04-21 06:55:53 +00:00
|
|
|
Addr uint64
|
2015-04-20 18:03:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 19:32:32 +00:00
|
|
|
func (nbp NoBreakpointError) Error() string {
|
2017-04-21 06:55:53 +00:00
|
|
|
return fmt.Sprintf("no breakpoint at %#v", nbp.Addr)
|
2015-04-20 18:03:22 +00:00
|
|
|
}
|
2017-09-24 13:00:55 +00:00
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
// BreakpointMap represents an (address, breakpoint) map.
|
2017-09-24 13:00:55 +00:00
|
|
|
type BreakpointMap struct {
|
|
|
|
M map[uint64]*Breakpoint
|
|
|
|
|
2022-05-25 20:58:26 +00:00
|
|
|
// Logical is a map of logical breakpoints.
|
|
|
|
Logical map[int]*LogicalBreakpoint
|
|
|
|
|
2021-08-09 17:41:25 +00:00
|
|
|
// WatchOutOfScope is the list of watchpoints that went out of scope during
|
|
|
|
// the last resume operation
|
|
|
|
WatchOutOfScope []*Breakpoint
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
// NewBreakpointMap creates a new BreakpointMap.
|
2017-09-24 13:00:55 +00:00
|
|
|
func NewBreakpointMap() BreakpointMap {
|
|
|
|
return BreakpointMap{
|
|
|
|
M: make(map[uint64]*Breakpoint),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:14:14 +00:00
|
|
|
// SetBreakpoint sets a breakpoint at addr, and stores it in the process wide
|
|
|
|
// break point table.
|
2022-02-25 09:09:53 +00:00
|
|
|
func (t *Target) SetBreakpoint(logicalID int, addr uint64, kind BreakpointKind, cond ast.Expr) (*Breakpoint, error) {
|
|
|
|
return t.setBreakpointInternal(logicalID, addr, kind, 0, cond)
|
2021-05-06 17:33:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 12:53:27 +00:00
|
|
|
// SetEBPFTracepoint will attach a uprobe to the function
|
|
|
|
// specified by 'fnName'.
|
2021-07-31 15:16:26 +00:00
|
|
|
func (t *Target) SetEBPFTracepoint(fnName string) error {
|
2021-08-24 12:53:27 +00:00
|
|
|
// Not every OS/arch that we support has support for eBPF,
|
|
|
|
// so check early and return an error if this is called on an
|
|
|
|
// unsupported system.
|
2021-07-31 15:16:26 +00:00
|
|
|
if !t.proc.SupportsBPF() {
|
|
|
|
return errors.New("eBPF is not supported")
|
|
|
|
}
|
2021-10-30 18:52:26 +00:00
|
|
|
fns, err := t.BinInfo().FindFunction(fnName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-31 15:16:26 +00:00
|
|
|
}
|
2021-08-24 12:53:27 +00:00
|
|
|
|
|
|
|
// Get information on the Goroutine so we can tell the
|
|
|
|
// eBPF program where to find it in order to get the
|
|
|
|
// goroutine ID.
|
|
|
|
rdr := t.BinInfo().Images[0].DwarfReader()
|
|
|
|
rdr.SeekToTypeNamed("runtime.g")
|
|
|
|
typ, err := t.BinInfo().findType("runtime.g")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("could not find type for runtime.g")
|
|
|
|
}
|
|
|
|
var goidOffset int64
|
|
|
|
switch t := typ.(type) {
|
|
|
|
case *godwarf.StructType:
|
|
|
|
for _, field := range t.Field {
|
|
|
|
if field.Name == "goid" {
|
|
|
|
goidOffset = field.ByteOffset
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 18:52:26 +00:00
|
|
|
for _, fn := range fns {
|
|
|
|
err := t.setEBPFTracepointOnFunc(fn, goidOffset)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) setEBPFTracepointOnFunc(fn *Function, goidOffset int64) error {
|
|
|
|
// Start putting together the argument map. This will tell the eBPF program
|
|
|
|
// all of the arguments we want to trace and how to find them.
|
|
|
|
|
2021-08-24 12:53:27 +00:00
|
|
|
// Start looping through each argument / return parameter for the function we
|
|
|
|
// are setting the uprobe on. Parse location information so that we can pass it
|
|
|
|
// along to the eBPF program.
|
2021-07-31 15:16:26 +00:00
|
|
|
dwarfTree, err := fn.cu.image.getDwarfTree(fn.offset)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
variablesFlags := reader.VariablesOnlyVisible
|
|
|
|
if t.BinInfo().Producer() != "" && goversion.ProducerAfterOrEqual(t.BinInfo().Producer(), 1, 15) {
|
|
|
|
variablesFlags |= reader.VariablesTrustDeclLine
|
|
|
|
}
|
2023-06-14 11:23:46 +00:00
|
|
|
_, l := t.BinInfo().EntryLineForFunc(fn)
|
2021-07-31 15:16:26 +00:00
|
|
|
|
2021-10-25 19:37:36 +00:00
|
|
|
var args []ebpf.UProbeArgMap
|
2021-07-31 15:16:26 +00:00
|
|
|
varEntries := reader.Variables(dwarfTree, fn.Entry, l, variablesFlags)
|
|
|
|
for _, entry := range varEntries {
|
|
|
|
_, dt, err := readVarEntry(entry.Tree, fn.cu.image)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-25 19:37:36 +00:00
|
|
|
|
2021-08-04 08:00:32 +00:00
|
|
|
offset, pieces, _, err := t.BinInfo().Location(entry, dwarf.AttrLocation, fn.Entry, op.DwarfRegisters{}, nil)
|
2021-07-31 15:16:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
paramPieces := make([]int, 0, len(pieces))
|
|
|
|
for _, piece := range pieces {
|
|
|
|
if piece.Kind == op.RegPiece {
|
|
|
|
paramPieces = append(paramPieces, int(piece.Val))
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 19:37:36 +00:00
|
|
|
isret, _ := entry.Val(dwarf.AttrVarParam).(bool)
|
2021-07-31 15:16:26 +00:00
|
|
|
offset += int64(t.BinInfo().Arch.PtrSize())
|
2021-10-25 19:37:36 +00:00
|
|
|
args = append(args, ebpf.UProbeArgMap{
|
|
|
|
Offset: offset,
|
|
|
|
Size: dt.Size(),
|
|
|
|
Kind: dt.Common().ReflectKind,
|
|
|
|
Pieces: paramPieces,
|
|
|
|
InReg: len(pieces) > 0,
|
|
|
|
Ret: isret,
|
|
|
|
})
|
2021-07-31 15:16:26 +00:00
|
|
|
}
|
2021-08-24 12:53:27 +00:00
|
|
|
|
2021-10-30 18:52:26 +00:00
|
|
|
//TODO(aarzilli): inlined calls?
|
|
|
|
|
2021-08-24 12:53:27 +00:00
|
|
|
// Finally, set the uprobe on the function.
|
2022-07-22 15:57:57 +00:00
|
|
|
return t.proc.SetUProbe(fn.Name, goidOffset, args)
|
2021-07-31 15:16:26 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 17:33:56 +00:00
|
|
|
// SetWatchpoint sets a data breakpoint at addr and stores it in the
|
|
|
|
// process wide break point table.
|
2022-02-25 09:09:53 +00:00
|
|
|
func (t *Target) SetWatchpoint(logicalID int, scope *EvalScope, expr string, wtype WatchType, cond ast.Expr) (*Breakpoint, error) {
|
2021-05-06 17:33:56 +00:00
|
|
|
if (wtype&WatchWrite == 0) && (wtype&WatchRead == 0) {
|
|
|
|
return nil, errors.New("at least one of read and write must be set for watchpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := parser.ParseExpr(expr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
xv, err := scope.evalAST(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if xv.Addr == 0 || xv.Flags&VariableFakeAddress != 0 || xv.DwarfType == nil {
|
|
|
|
return nil, fmt.Errorf("can not watch %q", expr)
|
|
|
|
}
|
|
|
|
if xv.Unreadable != nil {
|
|
|
|
return nil, fmt.Errorf("expression %q is unreadable: %v", expr, xv.Unreadable)
|
|
|
|
}
|
|
|
|
if xv.Kind == reflect.UnsafePointer || xv.Kind == reflect.Invalid {
|
|
|
|
return nil, fmt.Errorf("can not watch variable of type %s", xv.Kind.String())
|
|
|
|
}
|
|
|
|
sz := xv.DwarfType.Size()
|
|
|
|
if sz <= 0 || sz > int64(t.BinInfo().Arch.PtrSize()) {
|
|
|
|
//TODO(aarzilli): it is reasonable to expect to be able to watch string
|
|
|
|
//and interface variables and we could support it by watching certain
|
|
|
|
//member fields here.
|
|
|
|
return nil, fmt.Errorf("can not watch variable of type %s", xv.DwarfType.String())
|
|
|
|
}
|
2021-08-09 17:41:25 +00:00
|
|
|
|
|
|
|
stackWatch := scope.g != nil && !scope.g.SystemStack && xv.Addr >= scope.g.stack.lo && xv.Addr < scope.g.stack.hi
|
|
|
|
|
|
|
|
if stackWatch && wtype&WatchRead != 0 {
|
|
|
|
// In theory this would work except for the fact that the runtime will
|
|
|
|
// read them randomly to resize stacks so it doesn't make sense to do
|
|
|
|
// this.
|
|
|
|
return nil, errors.New("can not watch stack allocated variable for reads")
|
2021-05-06 17:33:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:09:53 +00:00
|
|
|
bp, err := t.setBreakpointInternal(logicalID, xv.Addr, UserBreakpoint, wtype.withSize(uint8(sz)), cond)
|
2021-08-09 17:41:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return bp, err
|
2021-05-20 17:04:02 +00:00
|
|
|
}
|
2021-08-09 17:41:25 +00:00
|
|
|
bp.WatchExpr = expr
|
|
|
|
|
|
|
|
if stackWatch {
|
|
|
|
bp.watchStackOff = int64(bp.Addr) - int64(scope.g.stack.hi)
|
|
|
|
err := t.setStackWatchBreakpoints(scope, bp)
|
|
|
|
if err != nil {
|
|
|
|
return bp, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bp, nil
|
2021-05-06 17:33:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:09:53 +00:00
|
|
|
func (t *Target) setBreakpointInternal(logicalID int, addr uint64, kind BreakpointKind, wtype WatchType, cond ast.Expr) (*Breakpoint, error) {
|
2020-06-03 18:14:14 +00:00
|
|
|
if valid, err := t.Valid(); !valid {
|
2022-07-14 21:14:45 +00:00
|
|
|
recorded, _ := t.recman.Recorded()
|
2021-12-13 19:39:20 +00:00
|
|
|
if !recorded {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-03 18:14:14 +00:00
|
|
|
}
|
|
|
|
bpmap := t.Breakpoints()
|
2021-07-21 15:24:19 +00:00
|
|
|
newBreaklet := &Breaklet{Kind: kind, Cond: cond}
|
|
|
|
if kind == UserBreakpoint {
|
2022-02-25 09:09:53 +00:00
|
|
|
newBreaklet.LogicalID = logicalID
|
2021-07-21 15:24:19 +00:00
|
|
|
}
|
2022-05-25 20:58:26 +00:00
|
|
|
|
|
|
|
setLogicalBreakpoint := func(bp *Breakpoint) {
|
|
|
|
if kind != UserBreakpoint || bp.Logical != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bpmap.Logical == nil {
|
|
|
|
bpmap.Logical = make(map[int]*LogicalBreakpoint)
|
|
|
|
}
|
|
|
|
lbp := bpmap.Logical[logicalID]
|
|
|
|
if lbp == nil {
|
|
|
|
lbp = &LogicalBreakpoint{LogicalID: logicalID}
|
2022-08-16 16:31:11 +00:00
|
|
|
lbp.HitCount = make(map[int64]uint64)
|
2022-05-25 20:58:26 +00:00
|
|
|
lbp.Enabled = true
|
|
|
|
bpmap.Logical[logicalID] = lbp
|
|
|
|
}
|
|
|
|
bp.Logical = lbp
|
|
|
|
breaklet := bp.UserBreaklet()
|
|
|
|
if breaklet != nil && breaklet.Cond == nil {
|
|
|
|
breaklet.Cond = lbp.Cond
|
|
|
|
}
|
2023-03-22 18:38:09 +00:00
|
|
|
if lbp.File == "" && lbp.Line == 0 {
|
|
|
|
lbp.File = bp.File
|
|
|
|
lbp.Line = bp.Line
|
|
|
|
} else if bp.File != lbp.File || bp.Line != lbp.Line {
|
|
|
|
lbp.File = "<multiple locations>"
|
|
|
|
lbp.Line = 0
|
|
|
|
}
|
2022-05-25 20:58:26 +00:00
|
|
|
fn := t.BinInfo().PCToFunc(bp.Addr)
|
|
|
|
if fn != nil {
|
|
|
|
lbp.FunctionName = fn.NameWithoutTypeParams()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-24 13:00:55 +00:00
|
|
|
if bp, ok := bpmap.M[addr]; ok {
|
2021-07-21 15:24:19 +00:00
|
|
|
if !bp.canOverlap(kind) {
|
2017-09-25 06:29:13 +00:00
|
|
|
return bp, BreakpointExistsError{bp.File, bp.Line, bp.Addr}
|
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
bp.Breaklets = append(bp.Breaklets, newBreaklet)
|
2022-05-25 20:58:26 +00:00
|
|
|
setLogicalBreakpoint(bp)
|
2017-09-25 06:29:13 +00:00
|
|
|
return bp, nil
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 15:36:55 +00:00
|
|
|
f, l, fn := t.BinInfo().PCToLine(addr)
|
2017-09-24 13:00:55 +00:00
|
|
|
|
2018-11-12 12:06:24 +00:00
|
|
|
fnName := ""
|
|
|
|
if fn != nil {
|
|
|
|
fnName = fn.Name
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:33:56 +00:00
|
|
|
hwidx := uint8(0)
|
|
|
|
if wtype != 0 {
|
|
|
|
m := make(map[uint8]bool)
|
|
|
|
for _, bp := range bpmap.M {
|
|
|
|
if bp.WatchType != 0 {
|
|
|
|
m[bp.HWBreakIndex] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for hwidx = 0; true; hwidx++ {
|
|
|
|
if !m[hwidx] {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-24 13:00:55 +00:00
|
|
|
newBreakpoint := &Breakpoint{
|
2018-11-12 12:06:24 +00:00
|
|
|
FunctionName: fnName,
|
2021-05-06 17:33:56 +00:00
|
|
|
WatchType: wtype,
|
|
|
|
HWBreakIndex: hwidx,
|
2017-09-24 13:00:55 +00:00
|
|
|
File: f,
|
|
|
|
Line: l,
|
|
|
|
Addr: addr,
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:27:54 +00:00
|
|
|
err := t.proc.WriteBreakpoint(newBreakpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
newBreakpoint.Breaklets = append(newBreakpoint.Breaklets, newBreaklet)
|
2022-05-25 20:58:26 +00:00
|
|
|
setLogicalBreakpoint(newBreakpoint)
|
2021-07-21 15:24:19 +00:00
|
|
|
|
2017-09-24 13:00:55 +00:00
|
|
|
bpmap.M[addr] = newBreakpoint
|
|
|
|
|
|
|
|
return newBreakpoint, nil
|
|
|
|
}
|
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
// canOverlap returns true if a breakpoint of kind can be overlapped to the
|
|
|
|
// already existing breaklets in bp.
|
|
|
|
// At most one user breakpoint can be set but multiple internal breakpoints are allowed.
|
|
|
|
// All other internal breakpoints are allowed to overlap freely.
|
|
|
|
func (bp *Breakpoint) canOverlap(kind BreakpointKind) bool {
|
|
|
|
if kind == UserBreakpoint {
|
|
|
|
return !bp.IsUser()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:14:14 +00:00
|
|
|
// ClearBreakpoint clears the breakpoint at addr.
|
2021-09-29 10:01:37 +00:00
|
|
|
func (t *Target) ClearBreakpoint(addr uint64) error {
|
2020-06-03 18:14:14 +00:00
|
|
|
if valid, err := t.Valid(); !valid {
|
2022-07-14 21:14:45 +00:00
|
|
|
recorded, _ := t.recman.Recorded()
|
2021-12-13 19:39:20 +00:00
|
|
|
if !recorded {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-03 18:14:14 +00:00
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
bp, ok := t.Breakpoints().M[addr]
|
2017-09-24 13:00:55 +00:00
|
|
|
if !ok {
|
2021-09-29 10:01:37 +00:00
|
|
|
return NoBreakpointError{Addr: addr}
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
for i := range bp.Breaklets {
|
|
|
|
if bp.Breaklets[i].Kind == UserBreakpoint {
|
|
|
|
bp.Breaklets[i] = nil
|
2022-05-25 20:58:26 +00:00
|
|
|
if bp.WatchExpr == "" {
|
|
|
|
bp.Logical = nil
|
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
_, err := t.finishClearBreakpoint(bp)
|
|
|
|
if err != nil {
|
2021-09-29 10:01:37 +00:00
|
|
|
return err
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
2021-08-09 17:41:25 +00:00
|
|
|
|
|
|
|
if bp.WatchExpr != "" && bp.watchStackOff != 0 {
|
|
|
|
// stack watchpoint, must remove all its WatchOutOfScopeBreakpoints/StackResizeBreakpoints
|
|
|
|
err := t.clearStackWatchBreakpoints(bp)
|
|
|
|
if err != nil {
|
2021-09-29 10:01:37 +00:00
|
|
|
return err
|
2021-08-09 17:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:01:37 +00:00
|
|
|
return nil
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 19:07:42 +00:00
|
|
|
// ClearSteppingBreakpoints removes all stepping breakpoints from the map,
|
2017-09-24 13:00:55 +00:00
|
|
|
// calling clearBreakpoint on each one.
|
2021-07-21 15:24:19 +00:00
|
|
|
func (t *Target) ClearSteppingBreakpoints() error {
|
2020-06-03 18:14:14 +00:00
|
|
|
bpmap := t.Breakpoints()
|
|
|
|
threads := t.ThreadList()
|
2021-07-21 15:24:19 +00:00
|
|
|
for _, bp := range bpmap.M {
|
|
|
|
for i := range bp.Breaklets {
|
|
|
|
if bp.Breaklets[i].Kind&steppingMask != 0 {
|
|
|
|
bp.Breaklets[i] = nil
|
|
|
|
}
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
cleared, err := t.finishClearBreakpoint(bp)
|
|
|
|
if err != nil {
|
2017-09-24 13:00:55 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
if cleared {
|
|
|
|
for _, thread := range threads {
|
|
|
|
if thread.Breakpoint().Breakpoint == bp {
|
|
|
|
thread.Breakpoint().Clear()
|
|
|
|
}
|
2020-06-03 18:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
|
2021-07-21 15:24:19 +00:00
|
|
|
// finishClearBreakpoint clears nil breaklets from the breaklet list of bp
|
|
|
|
// and if it is empty erases the breakpoint.
|
|
|
|
// Returns true if the breakpoint was deleted
|
|
|
|
func (t *Target) finishClearBreakpoint(bp *Breakpoint) (bool, error) {
|
|
|
|
oldBreaklets := bp.Breaklets
|
|
|
|
bp.Breaklets = bp.Breaklets[:0]
|
|
|
|
for _, breaklet := range oldBreaklets {
|
|
|
|
if breaklet != nil {
|
|
|
|
bp.Breaklets = append(bp.Breaklets, breaklet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(bp.Breaklets) > 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err := t.proc.EraseBreakpoint(bp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(t.Breakpoints().M, bp.Addr)
|
2022-05-25 20:58:26 +00:00
|
|
|
if bp.WatchExpr != "" && bp.Logical != nil {
|
|
|
|
delete(t.Breakpoints().Logical, bp.Logical.LogicalID)
|
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasSteppingBreakpoints returns true if bpmap has at least one stepping
|
2017-09-25 06:29:13 +00:00
|
|
|
// breakpoint set.
|
2021-07-21 15:24:19 +00:00
|
|
|
func (bpmap *BreakpointMap) HasSteppingBreakpoints() bool {
|
2017-09-25 06:29:13 +00:00
|
|
|
for _, bp := range bpmap.M {
|
2021-07-21 15:24:19 +00:00
|
|
|
if bp.IsStepping() {
|
2017-09-25 06:29:13 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:33:56 +00:00
|
|
|
// HasHWBreakpoints returns true if there are hardware breakpoints.
|
|
|
|
func (bpmap *BreakpointMap) HasHWBreakpoints() bool {
|
|
|
|
for _, bp := range bpmap.M {
|
|
|
|
if bp.WatchType != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
// BreakpointState describes the state of a breakpoint in a thread.
|
|
|
|
type BreakpointState struct {
|
|
|
|
*Breakpoint
|
2021-07-21 15:24:19 +00:00
|
|
|
// Active is true if the condition of any breaklet is met.
|
2017-09-25 06:29:13 +00:00
|
|
|
Active bool
|
2021-07-21 15:24:19 +00:00
|
|
|
// Stepping is true if one of the active breaklets is a stepping
|
2017-09-25 06:29:13 +00:00
|
|
|
// breakpoint.
|
2021-07-21 15:24:19 +00:00
|
|
|
Stepping bool
|
|
|
|
// SteppingInto is true if one of the active stepping breaklets has Kind ==
|
|
|
|
// StepBreakpoint.
|
|
|
|
SteppingInto bool
|
2017-09-25 06:29:13 +00:00
|
|
|
// CondError contains any error encountered while evaluating the
|
|
|
|
// breakpoint's condition.
|
|
|
|
CondError error
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
// Clear zeros the struct.
|
2017-09-25 06:29:13 +00:00
|
|
|
func (bpstate *BreakpointState) Clear() {
|
|
|
|
bpstate.Breakpoint = nil
|
|
|
|
bpstate.Active = false
|
2021-07-21 15:24:19 +00:00
|
|
|
bpstate.Stepping = false
|
|
|
|
bpstate.SteppingInto = false
|
2017-09-25 06:29:13 +00:00
|
|
|
bpstate.CondError = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bpstate *BreakpointState) String() string {
|
|
|
|
s := bpstate.Breakpoint.String()
|
|
|
|
if bpstate.Active {
|
|
|
|
s += " active"
|
|
|
|
}
|
2021-07-21 15:24:19 +00:00
|
|
|
if bpstate.Stepping {
|
|
|
|
s += " stepping"
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2018-05-11 12:51:15 +00:00
|
|
|
|
|
|
|
func configureReturnBreakpoint(bi *BinaryInfo, bp *Breakpoint, topframe *Stackframe, retFrameCond ast.Expr) {
|
|
|
|
if topframe.Current.Fn == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bp.returnInfo = &returnBreakpointInfo{
|
|
|
|
retFrameCond: retFrameCond,
|
|
|
|
fn: topframe.Current.Fn,
|
|
|
|
frameOffset: topframe.FrameOffset(),
|
|
|
|
spOffset: topframe.FrameOffset() - int64(bi.Arch.PtrSize()), // must be the value that SP had at the entry point of the function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 16:37:55 +00:00
|
|
|
func (rbpi *returnBreakpointInfo) Collect(t *Target, thread Thread) []*Variable {
|
2018-05-11 12:51:15 +00:00
|
|
|
if rbpi == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := GetG(thread)
|
|
|
|
if err != nil {
|
2020-11-09 19:28:40 +00:00
|
|
|
return returnInfoError("could not get g", err, thread.ProcessMemory())
|
2018-05-11 12:51:15 +00:00
|
|
|
}
|
2021-07-02 16:37:55 +00:00
|
|
|
scope, err := GoroutineScope(t, thread)
|
2018-05-11 12:51:15 +00:00
|
|
|
if err != nil {
|
2020-11-09 19:28:40 +00:00
|
|
|
return returnInfoError("could not get scope", err, thread.ProcessMemory())
|
2018-05-11 12:51:15 +00:00
|
|
|
}
|
|
|
|
v, err := scope.evalAST(rbpi.retFrameCond)
|
|
|
|
if err != nil || v.Unreadable != nil || v.Kind != reflect.Bool {
|
|
|
|
// This condition was evaluated as part of the breakpoint condition
|
|
|
|
// evaluation, if the errors happen they will be reported as part of the
|
|
|
|
// condition errors.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !constant.BoolVal(v.Value) {
|
|
|
|
// Breakpoint not hit as a return breakpoint.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:39:06 +00:00
|
|
|
oldFrameOffset := rbpi.frameOffset + int64(g.stack.hi)
|
|
|
|
oldSP := uint64(rbpi.spOffset + int64(g.stack.hi))
|
2018-05-04 17:31:45 +00:00
|
|
|
err = fakeFunctionEntryScope(scope, rbpi.fn, oldFrameOffset, oldSP)
|
2018-05-11 12:51:15 +00:00
|
|
|
if err != nil {
|
2020-11-09 19:28:40 +00:00
|
|
|
return returnInfoError("could not read function entry", err, thread.ProcessMemory())
|
2018-05-11 12:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 08:03:54 +00:00
|
|
|
vars, err := scope.Locals(0)
|
2018-05-11 12:51:15 +00:00
|
|
|
if err != nil {
|
2020-11-09 19:28:40 +00:00
|
|
|
return returnInfoError("could not evaluate return variables", err, thread.ProcessMemory())
|
2018-05-11 12:51:15 +00:00
|
|
|
}
|
|
|
|
vars = filterVariables(vars, func(v *Variable) bool {
|
|
|
|
return (v.Flags & VariableReturnArgument) != 0
|
|
|
|
})
|
|
|
|
|
|
|
|
return vars
|
|
|
|
}
|
|
|
|
|
|
|
|
func returnInfoError(descr string, err error, mem MemoryReadWriter) []*Variable {
|
|
|
|
v := newConstant(constant.MakeString(fmt.Sprintf("%s: %v", descr, err.Error())), mem)
|
|
|
|
v.Name = "return value read error"
|
|
|
|
return []*Variable{v}
|
|
|
|
}
|
2022-05-25 20:58:26 +00:00
|
|
|
|
|
|
|
// LogicalBreakpoint represents a breakpoint set by a user.
|
|
|
|
// A logical breakpoint can be associated with zero or many physical
|
|
|
|
// breakpoints.
|
|
|
|
// Where a physical breakpoint is associated with a specific instruction
|
|
|
|
// address a logical breakpoint is associated with a source code location.
|
|
|
|
// Therefore a logical breakpoint can be associated with zero or many
|
|
|
|
// physical breakpoints.
|
|
|
|
// It will have one or more physical breakpoints when source code has been
|
|
|
|
// inlined (or in the case of type parametric code).
|
|
|
|
// It will have zero physical breakpoints when it represents a deferred
|
|
|
|
// breakpoint for code that will be loaded in the future.
|
|
|
|
type LogicalBreakpoint struct {
|
|
|
|
LogicalID int
|
|
|
|
Name string
|
|
|
|
FunctionName string
|
|
|
|
File string
|
|
|
|
Line int
|
|
|
|
Enabled bool
|
|
|
|
|
2022-09-28 18:35:07 +00:00
|
|
|
Set SetBreakpoint
|
|
|
|
|
2022-05-25 20:58:26 +00:00
|
|
|
Tracepoint bool // Tracepoint flag
|
|
|
|
TraceReturn bool
|
|
|
|
Goroutine bool // Retrieve goroutine information
|
|
|
|
Stacktrace int // Number of stack frames to retrieve
|
|
|
|
Variables []string // Variables to evaluate
|
|
|
|
LoadArgs *LoadConfig
|
|
|
|
LoadLocals *LoadConfig
|
|
|
|
|
2022-08-16 16:31:11 +00:00
|
|
|
HitCount map[int64]uint64 // Number of times a breakpoint has been reached in a certain goroutine
|
|
|
|
TotalHitCount uint64 // Number of times a breakpoint has been reached
|
|
|
|
HitCondPerG bool // Use per goroutine hitcount as HitCond operand, instead of total hitcount
|
2022-05-25 20:58:26 +00:00
|
|
|
|
|
|
|
// HitCond: if not nil the breakpoint will be triggered only if the evaluated HitCond returns
|
|
|
|
// true with the TotalHitCount.
|
|
|
|
HitCond *struct {
|
|
|
|
Op token.Token
|
|
|
|
Val int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cond: if not nil the breakpoint will be triggered only if evaluating Cond returns true
|
|
|
|
Cond ast.Expr
|
|
|
|
|
|
|
|
UserData interface{} // Any additional information about the breakpoint
|
|
|
|
}
|
2022-09-28 18:35:07 +00:00
|
|
|
|
|
|
|
// SetBreakpoint describes how a breakpoint should be set.
|
|
|
|
type SetBreakpoint struct {
|
|
|
|
FunctionName string
|
|
|
|
File string
|
|
|
|
Line int
|
|
|
|
Expr func(*Target) []uint64
|
2023-07-07 17:33:40 +00:00
|
|
|
ExprString string
|
2022-09-28 18:35:07 +00:00
|
|
|
PidAddrs []PidAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
type PidAddr struct {
|
|
|
|
Pid int
|
|
|
|
Addr uint64
|
|
|
|
}
|