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 (
|
|
|
|
"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"
|
|
|
|
)
|
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"
|
|
|
|
|
|
|
|
unrecoveredPanicID = -1
|
|
|
|
fatalThrowID = -2
|
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
Name string // User defined name of the breakpoint
|
2019-11-01 19:41:06 +00:00
|
|
|
LogicalID int // ID of the logical breakpoint that owns this physical breakpoint
|
2017-09-25 06:29:13 +00:00
|
|
|
|
2021-05-20 17:04:02 +00:00
|
|
|
WatchExpr string
|
2021-05-06 17:33:56 +00:00
|
|
|
WatchType WatchType
|
|
|
|
HWBreakIndex uint8 // hardware breakpoint index
|
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
// Kind describes whether this is an internal breakpoint (for next'ing or
|
|
|
|
// stepping).
|
|
|
|
// A single breakpoint can be both a UserBreakpoint and some kind of
|
|
|
|
// internal breakpoint, but it can not be two different kinds of internal
|
|
|
|
// breakpoint.
|
|
|
|
Kind BreakpointKind
|
2015-07-01 03:16:52 +00:00
|
|
|
|
|
|
|
// Breakpoint information
|
2018-10-16 15:49:20 +00:00
|
|
|
Tracepoint bool // Tracepoint flag
|
|
|
|
TraceReturn bool
|
2016-05-29 19:20:09 +00:00
|
|
|
Goroutine bool // Retrieve goroutine information
|
|
|
|
Stacktrace int // Number of stack frames to retrieve
|
|
|
|
Variables []string // Variables to evaluate
|
2016-04-24 17:15:39 +00:00
|
|
|
LoadArgs *LoadConfig
|
|
|
|
LoadLocals *LoadConfig
|
2015-09-26 00:04:39 +00:00
|
|
|
HitCount map[int]uint64 // Number of times a breakpoint has been reached in a certain goroutine
|
|
|
|
TotalHitCount uint64 // Number of times a breakpoint has been reached
|
2015-11-18 09:07:08 +00:00
|
|
|
|
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
|
2016-07-26 13:34:31 +00:00
|
|
|
// Cond: if not nil the breakpoint will be triggered only if evaluating Cond returns true
|
|
|
|
Cond ast.Expr
|
2017-09-25 06:29:13 +00:00
|
|
|
// internalCond is the same as Cond but used for the condition of internal breakpoints
|
|
|
|
internalCond ast.Expr
|
2021-05-28 18:21:53 +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
|
|
|
|
}
|
2018-05-11 12:51:15 +00:00
|
|
|
|
|
|
|
// ReturnInfo describes how to collect return variables when this
|
|
|
|
// breakpoint is hit as a return breakpoint.
|
|
|
|
returnInfo *returnBreakpointInfo
|
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-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 {
|
2019-11-01 19:41:06 +00:00
|
|
|
return fmt.Sprintf("Breakpoint %d at %#v %s:%d (%d)", bp.LogicalID, bp.Addr, bp.File, bp.Line, bp.TotalHitCount)
|
2015-01-14 02:37:10 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2017-09-25 06:29:13 +00:00
|
|
|
func (bp *Breakpoint) CheckCondition(thread Thread) BreakpointState {
|
|
|
|
bpstate := BreakpointState{Breakpoint: bp, Active: false, Internal: false, CondError: nil}
|
2021-05-28 18:21:53 +00:00
|
|
|
bpstate.checkCond(thread)
|
|
|
|
// Update the breakpoint hit counts.
|
|
|
|
if bpstate.Breakpoint != nil && bpstate.Active {
|
|
|
|
if g, err := GetG(thread); err == nil {
|
|
|
|
bpstate.HitCount[g.ID]++
|
|
|
|
}
|
|
|
|
bpstate.TotalHitCount++
|
|
|
|
}
|
|
|
|
bpstate.checkHitCond(thread)
|
|
|
|
return bpstate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bpstate *BreakpointState) checkCond(thread Thread) {
|
|
|
|
if bpstate.Cond == nil && bpstate.internalCond == nil {
|
2017-09-25 06:29:13 +00:00
|
|
|
bpstate.Active = true
|
2021-05-28 18:21:53 +00:00
|
|
|
bpstate.Internal = bpstate.IsInternal()
|
|
|
|
return
|
2015-11-18 09:07:08 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
nextDeferOk := true
|
2021-05-28 18:21:53 +00:00
|
|
|
if bpstate.Kind&NextDeferBreakpoint != 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
|
|
|
var err error
|
2017-02-15 13:41:03 +00:00
|
|
|
frames, err := ThreadStacktrace(thread, 2)
|
2016-07-14 07:43:39 +00:00
|
|
|
if err == nil {
|
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
|
|
|
nextDeferOk = isPanicCall(frames)
|
|
|
|
if !nextDeferOk {
|
2021-05-28 18:21:53 +00:00
|
|
|
nextDeferOk, _ = isDeferReturnCall(frames, bpstate.DeferReturns)
|
2016-07-14 07:43:39 +00:00
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 18:21:53 +00:00
|
|
|
if bpstate.IsInternal() {
|
2017-09-25 06:29:13 +00:00
|
|
|
// Check internalCondition if this is also an internal breakpoint
|
2021-05-28 18:21:53 +00:00
|
|
|
bpstate.Active, bpstate.CondError = evalBreakpointCondition(thread, bpstate.internalCond)
|
2017-09-25 06:29:13 +00:00
|
|
|
bpstate.Active = bpstate.Active && nextDeferOk
|
|
|
|
if bpstate.Active || bpstate.CondError != nil {
|
|
|
|
bpstate.Internal = true
|
2021-05-28 18:21:53 +00:00
|
|
|
return
|
2016-07-14 07:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 18:21:53 +00:00
|
|
|
if bpstate.IsUser() {
|
2017-09-25 06:29:13 +00:00
|
|
|
// Check normal condition if this is also a user breakpoint
|
2021-05-28 18:21:53 +00:00
|
|
|
bpstate.Active, bpstate.CondError = evalBreakpointCondition(thread, bpstate.Cond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkHitCond evaluates bp's hit condition on thread.
|
|
|
|
func (bpstate *BreakpointState) checkHitCond(thread Thread) {
|
|
|
|
if bpstate.HitCond == nil || !bpstate.Active || bpstate.Internal {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Evaluate the breakpoint condition.
|
|
|
|
switch bpstate.HitCond.Op {
|
|
|
|
case token.EQL:
|
|
|
|
bpstate.Active = int(bpstate.TotalHitCount) == bpstate.HitCond.Val
|
|
|
|
case token.NEQ:
|
|
|
|
bpstate.Active = int(bpstate.TotalHitCount) != bpstate.HitCond.Val
|
|
|
|
case token.GTR:
|
|
|
|
bpstate.Active = int(bpstate.TotalHitCount) > bpstate.HitCond.Val
|
|
|
|
case token.LSS:
|
|
|
|
bpstate.Active = int(bpstate.TotalHitCount) < bpstate.HitCond.Val
|
|
|
|
case token.GEQ:
|
|
|
|
bpstate.Active = int(bpstate.TotalHitCount) >= bpstate.HitCond.Val
|
|
|
|
case token.LEQ:
|
|
|
|
bpstate.Active = int(bpstate.TotalHitCount) <= bpstate.HitCond.Val
|
|
|
|
case token.REM:
|
|
|
|
bpstate.Active = int(bpstate.TotalHitCount)%bpstate.HitCond.Val == 0
|
2017-09-25 06:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 isPanicCall(frames []Stackframe) bool {
|
|
|
|
return len(frames) >= 3 && frames[2].Current.Fn != nil && frames[2].Current.Fn.Name == "runtime.gopanic"
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDeferReturnCall(frames []Stackframe, deferReturns []uint64) (bool, uint64) {
|
|
|
|
if len(frames) >= 1 {
|
|
|
|
for _, pc := range deferReturns {
|
|
|
|
if frames[0].Ret == pc {
|
|
|
|
return true, pc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
// IsInternal returns true if bp is an internal breakpoint.
|
|
|
|
// User-set breakpoints can overlap with internal breakpoints, in that case
|
|
|
|
// both IsUser and IsInternal will be true.
|
|
|
|
func (bp *Breakpoint) IsInternal() bool {
|
|
|
|
return bp.Kind != UserBreakpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsUser returns true if bp is a user-set breakpoint.
|
|
|
|
// User-set breakpoints can overlap with internal breakpoints, in that case
|
|
|
|
// both IsUser and IsInternal will be true.
|
|
|
|
func (bp *Breakpoint) IsUser() bool {
|
|
|
|
return bp.Kind&UserBreakpoint != 0
|
2017-05-16 18:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func evalBreakpointCondition(thread Thread, cond ast.Expr) (bool, error) {
|
2017-09-25 06:29:13 +00:00
|
|
|
if cond == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
2017-02-15 13:41:03 +00:00
|
|
|
scope, err := GoroutineScope(thread)
|
2015-11-18 09:07:08 +00:00
|
|
|
if err != nil {
|
2020-08-08 13:06:14 +00:00
|
|
|
scope, err = ThreadScope(thread)
|
|
|
|
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
|
|
|
|
|
|
|
|
breakpointIDCounter int
|
|
|
|
internalBreakpointIDCounter int
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (t *Target) SetBreakpoint(addr uint64, kind BreakpointKind, cond ast.Expr) (*Breakpoint, error) {
|
2021-05-06 17:33:56 +00:00
|
|
|
return t.setBreakpointInternal(addr, kind, 0, cond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWatchpoint sets a data breakpoint at addr and stores it in the
|
|
|
|
// process wide break point table.
|
|
|
|
func (t *Target) SetWatchpoint(scope *EvalScope, expr string, wtype WatchType, cond ast.Expr) (*Breakpoint, error) {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
if xv.Addr >= scope.g.stack.lo && xv.Addr < scope.g.stack.hi {
|
|
|
|
//TODO(aarzilli): support watching stack variables
|
|
|
|
return nil, errors.New("can not watch stack allocated variable")
|
|
|
|
}
|
|
|
|
|
2021-05-20 17:04:02 +00:00
|
|
|
bp, err := t.setBreakpointInternal(xv.Addr, UserBreakpoint, wtype.withSize(uint8(sz)), cond)
|
|
|
|
if bp != nil {
|
|
|
|
bp.WatchExpr = expr
|
|
|
|
}
|
|
|
|
return bp, err
|
2021-05-06 17:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) setBreakpointInternal(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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bpmap := t.Breakpoints()
|
2017-09-24 13:00:55 +00:00
|
|
|
if bp, ok := bpmap.M[addr]; ok {
|
2017-09-25 06:29:13 +00:00
|
|
|
// We can overlap one internal breakpoint with one user breakpoint, we
|
|
|
|
// need to support this otherwise a conditional breakpoint can mask a
|
|
|
|
// breakpoint set by next or step.
|
2018-09-26 05:22:44 +00:00
|
|
|
if (kind != UserBreakpoint && bp.Kind != UserBreakpoint) || (kind == UserBreakpoint && bp.IsUser()) {
|
2017-09-25 06:29:13 +00:00
|
|
|
return bp, BreakpointExistsError{bp.File, bp.Line, bp.Addr}
|
|
|
|
}
|
|
|
|
bp.Kind |= kind
|
|
|
|
if kind != UserBreakpoint {
|
|
|
|
bp.internalCond = cond
|
|
|
|
} else {
|
|
|
|
bp.Cond = cond
|
|
|
|
}
|
|
|
|
return bp, nil
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 15:27:54 +00:00
|
|
|
f, l, fn := t.BinInfo().PCToLine(uint64(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,
|
|
|
|
Kind: kind,
|
|
|
|
HitCount: map[int]uint64{},
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:27:54 +00:00
|
|
|
err := t.proc.WriteBreakpoint(newBreakpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-24 13:00:55 +00:00
|
|
|
if kind != UserBreakpoint {
|
|
|
|
bpmap.internalBreakpointIDCounter++
|
2019-11-01 19:41:06 +00:00
|
|
|
newBreakpoint.LogicalID = bpmap.internalBreakpointIDCounter
|
2017-09-25 06:29:13 +00:00
|
|
|
newBreakpoint.internalCond = cond
|
2017-09-24 13:00:55 +00:00
|
|
|
} else {
|
|
|
|
bpmap.breakpointIDCounter++
|
2019-11-01 19:41:06 +00:00
|
|
|
newBreakpoint.LogicalID = bpmap.breakpointIDCounter
|
2017-09-25 06:29:13 +00:00
|
|
|
newBreakpoint.Cond = cond
|
2017-09-24 13:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bpmap.M[addr] = newBreakpoint
|
|
|
|
|
|
|
|
return newBreakpoint, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:02:23 +00:00
|
|
|
// SetBreakpointWithID creates a breakpoint at addr, with the specified logical ID.
|
|
|
|
func (t *Target) SetBreakpointWithID(id int, addr uint64) (*Breakpoint, error) {
|
2020-06-03 18:14:14 +00:00
|
|
|
bpmap := t.Breakpoints()
|
|
|
|
bp, err := t.SetBreakpoint(addr, UserBreakpoint, nil)
|
2017-09-24 13:00:55 +00:00
|
|
|
if err == nil {
|
2019-11-01 19:41:06 +00:00
|
|
|
bp.LogicalID = id
|
2017-09-24 13:00:55 +00:00
|
|
|
bpmap.breakpointIDCounter--
|
|
|
|
}
|
|
|
|
return bp, err
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:14:14 +00:00
|
|
|
// ClearBreakpoint clears the breakpoint at addr.
|
|
|
|
func (t *Target) ClearBreakpoint(addr uint64) (*Breakpoint, error) {
|
|
|
|
if valid, err := t.Valid(); !valid {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bpmap := t.Breakpoints()
|
2017-09-24 13:00:55 +00:00
|
|
|
bp, ok := bpmap.M[addr]
|
|
|
|
if !ok {
|
|
|
|
return nil, NoBreakpointError{Addr: addr}
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:29:13 +00:00
|
|
|
bp.Kind &= ^UserBreakpoint
|
|
|
|
bp.Cond = nil
|
|
|
|
if bp.Kind != 0 {
|
|
|
|
return bp, nil
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:14:14 +00:00
|
|
|
if err := t.proc.EraseBreakpoint(bp); err != nil {
|
2017-09-24 13:00:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(bpmap.M, addr)
|
|
|
|
|
|
|
|
return bp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearInternalBreakpoints removes all internal breakpoints from the map,
|
|
|
|
// calling clearBreakpoint on each one.
|
2020-06-03 18:14:14 +00:00
|
|
|
func (t *Target) ClearInternalBreakpoints() error {
|
|
|
|
bpmap := t.Breakpoints()
|
|
|
|
threads := t.ThreadList()
|
2017-09-24 13:00:55 +00:00
|
|
|
for addr, bp := range bpmap.M {
|
2017-09-25 06:29:13 +00:00
|
|
|
bp.Kind = bp.Kind & UserBreakpoint
|
|
|
|
bp.internalCond = nil
|
2018-05-11 12:51:15 +00:00
|
|
|
bp.returnInfo = nil
|
2017-09-25 06:29:13 +00:00
|
|
|
if bp.Kind != 0 {
|
2017-09-24 13:00:55 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-06-03 18:14:14 +00:00
|
|
|
if err := t.proc.EraseBreakpoint(bp); err != nil {
|
2017-09-24 13:00:55 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-03 18:14:14 +00:00
|
|
|
for _, thread := range threads {
|
|
|
|
if thread.Breakpoint().Breakpoint == bp {
|
|
|
|
thread.Breakpoint().Clear()
|
|
|
|
}
|
|
|
|
}
|
2017-09-24 13:00:55 +00:00
|
|
|
delete(bpmap.M, addr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-25 06:29:13 +00:00
|
|
|
|
|
|
|
// HasInternalBreakpoints returns true if bpmap has at least one internal
|
|
|
|
// breakpoint set.
|
|
|
|
func (bpmap *BreakpointMap) HasInternalBreakpoints() bool {
|
|
|
|
for _, bp := range bpmap.M {
|
2018-09-26 05:22:44 +00:00
|
|
|
if bp.IsInternal() {
|
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
|
|
|
|
// Active is true if the breakpoint condition was met.
|
|
|
|
Active bool
|
|
|
|
// Internal is true if the breakpoint was matched as an internal
|
|
|
|
// breakpoint.
|
|
|
|
Internal bool
|
|
|
|
// 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
|
|
|
|
bpstate.Internal = false
|
|
|
|
bpstate.CondError = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bpstate *BreakpointState) String() string {
|
|
|
|
s := bpstate.Breakpoint.String()
|
|
|
|
if bpstate.Active {
|
|
|
|
s += " active"
|
|
|
|
}
|
|
|
|
if bpstate.Internal {
|
|
|
|
s += " internal"
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rbpi *returnBreakpointInfo) Collect(thread Thread) []*Variable {
|
|
|
|
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
|
|
|
}
|
|
|
|
scope, err := GoroutineScope(thread)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
vars, err := scope.Locals()
|
|
|
|
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}
|
|
|
|
}
|