/
breakpoints.go
986 lines (865 loc) · 28.1 KB
/
breakpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
package proc
import (
"debug/dwarf"
"errors"
"fmt"
"go/ast"
"go/constant"
"go/parser"
"go/token"
"reflect"
"github.com/undoio/delve/pkg/dwarf/godwarf"
"github.com/undoio/delve/pkg/dwarf/op"
"github.com/undoio/delve/pkg/dwarf/reader"
"github.com/undoio/delve/pkg/goversion"
"github.com/undoio/delve/pkg/proc/internal/ebpf"
)
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"
// HardcodedBreakpoint is the name given to hardcoded breakpoints (for
// example: calls to runtime.Breakpoint)
HardcodedBreakpoint = "hardcoded-breakpoint"
unrecoveredPanicID = -1
fatalThrowID = -2
hardcodedBreakpointID = -3
NoLogicalID = -1000 // Logical breakpoint ID for breakpoints internal breakpoints.
)
// Breakpoint represents a physical breakpoint. Stores information on the break
// point including the byte of data that originally was stored at that
// address.
type Breakpoint struct {
// File & line information for printing.
FunctionName string
File string
Line int
Addr uint64 // Address breakpoint is set for.
OriginalData []byte // If software breakpoint, the data we replace with breakpoint instruction.
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
// 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
Logical *LogicalBreakpoint
// 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
// stepping).
Kind BreakpointKind
LogicalID int // ID of the logical breakpoint that owns this physical breakpoint
// Cond: if not nil the breakpoint will be triggered only if evaluating Cond returns true
Cond ast.Expr
// 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
// deferred function, DeferReturns is populated with the
// addresses of calls to runtime.deferreturn in the current
// function. This ensures that the breakpoint on the deferred
// function only triggers on panic or on the defer call to
// the function, not when the function is called directly
DeferReturns []uint64
// 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.
callback func(th Thread) bool
// For WatchOutOfScopeBreakpoints and StackResizeBreakpoints the watchpoint
// field contains the watchpoint related to this out of scope sentinel.
watchpoint *Breakpoint
}
// BreakpointKind determines the behavior of delve when the
// breakpoint is reached.
type BreakpointKind uint16
const (
// UserBreakpoint is a user set breakpoint
UserBreakpoint BreakpointKind = (1 << iota)
// 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
// 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
steppingMask = NextBreakpoint | NextDeferBreakpoint | StepBreakpoint
)
// 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")
func (bp *Breakpoint) String() string {
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
}
// 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))
}
lbp := bp.Logical
for _, breaklet := range bp.Breaklets {
switch breaklet.Kind {
case UserBreakpoint:
r = append(r, fmt.Sprintf("User Cond=%q HitCond=%v", exprToString(breaklet.Cond), lbp.HitCond))
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)))
default:
r = append(r, fmt.Sprintf("Unknown %d", breaklet.Kind))
}
}
return r
}
// BreakpointExistsError is returned when trying to set a breakpoint at
// an address that already has a breakpoint set for it.
type BreakpointExistsError struct {
File string
Line int
Addr uint64
}
func (bpe BreakpointExistsError) Error() string {
return fmt.Sprintf("Breakpoint exists at %s:%d at %x", bpe.File, bpe.Line, bpe.Addr)
}
// InvalidAddressError represents the result of
// attempting to set a breakpoint at an invalid address.
type InvalidAddressError struct {
Address uint64
}
func (iae InvalidAddressError) Error() string {
return fmt.Sprintf("Invalid address %#v\n", iae.Address)
}
type returnBreakpointInfo struct {
retFrameCond ast.Expr
fn *Function
frameOffset int64
spOffset int64
}
// CheckCondition evaluates bp's condition on thread.
func (bp *Breakpoint) checkCondition(tgt *Target, thread Thread, bpstate *BreakpointState) {
*bpstate = BreakpointState{Breakpoint: bp, Active: false, Stepping: false, SteppingInto: false, CondError: nil}
for _, breaklet := range bp.Breaklets {
bpstate.checkCond(tgt, breaklet, thread)
}
}
func (bpstate *BreakpointState) checkCond(tgt *Target, breaklet *Breaklet, thread Thread) {
var condErr error
active := true
if breaklet.Cond != nil {
active, condErr = evalBreakpointCondition(tgt, thread, breaklet.Cond)
}
if condErr != nil && bpstate.CondError == nil {
bpstate.CondError = condErr
}
if !active {
return
}
switch breaklet.Kind {
case UserBreakpoint:
lbp := bpstate.Breakpoint.Logical
if lbp != nil {
if g, err := GetG(thread); err == nil {
lbp.HitCount[g.ID]++
}
lbp.TotalHitCount++
}
active = checkHitCond(lbp)
case StepBreakpoint, NextBreakpoint, NextDeferBreakpoint:
nextDeferOk := true
if breaklet.Kind&NextDeferBreakpoint != 0 {
var err error
frames, err := ThreadStacktrace(thread, 2)
if err == nil {
nextDeferOk, _ = isPanicCall(frames)
if !nextDeferOk {
nextDeferOk, _ = isDeferReturnCall(frames, breaklet.DeferReturns)
}
}
}
active = active && nextDeferOk
if active {
bpstate.Stepping = true
if breaklet.Kind == StepBreakpoint {
bpstate.SteppingInto = true
}
}
case WatchOutOfScopeBreakpoint:
if breaklet.checkPanicCall {
frames, err := ThreadStacktrace(thread, 2)
if err == nil {
ipc, _ := isPanicCall(frames)
active = active && ipc
}
}
case StackResizeBreakpoint:
// no further checks
default:
bpstate.CondError = fmt.Errorf("internal error unknown breakpoint kind %v", breaklet.Kind)
}
if active {
if breaklet.callback != nil {
active = breaklet.callback(thread)
}
bpstate.Active = active
}
}
// checkHitCond evaluates bp's hit condition on thread.
func checkHitCond(lbp *LogicalBreakpoint) bool {
if lbp == nil || lbp.HitCond == nil {
return true
}
// Evaluate the breakpoint condition.
switch lbp.HitCond.Op {
case token.EQL:
return int(lbp.TotalHitCount) == lbp.HitCond.Val
case token.NEQ:
return int(lbp.TotalHitCount) != lbp.HitCond.Val
case token.GTR:
return int(lbp.TotalHitCount) > lbp.HitCond.Val
case token.LSS:
return int(lbp.TotalHitCount) < lbp.HitCond.Val
case token.GEQ:
return int(lbp.TotalHitCount) >= lbp.HitCond.Val
case token.LEQ:
return int(lbp.TotalHitCount) <= lbp.HitCond.Val
case token.REM:
return int(lbp.TotalHitCount)%lbp.HitCond.Val == 0
}
return false
}
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
}
func isDeferReturnCall(frames []Stackframe, deferReturns []uint64) (bool, uint64) {
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
}
}
if len(frames) >= 1 {
for _, pc := range deferReturns {
if frames[0].Ret == pc {
return true, pc
}
}
}
return false, 0
}
// 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
}
// IsUser returns true if bp is a user-set breakpoint.
// User-set breakpoints can overlap with stepping breakpoints, in that case
// both IsUser and IsStepping will be true.
func (bp *Breakpoint) IsUser() bool {
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
}
func evalBreakpointCondition(tgt *Target, thread Thread, cond ast.Expr) (bool, error) {
if cond == nil {
return true, nil
}
scope, err := GoroutineScope(tgt, thread)
if err != nil {
scope, err = ThreadScope(tgt, thread)
if err != nil {
return true, err
}
}
v, err := scope.evalAST(cond)
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")
}
v.loadValue(loadFullValue)
if v.Unreadable != nil {
return true, fmt.Errorf("condition expression unreadable: %v", v.Unreadable)
}
return constant.BoolVal(v.Value), nil
}
// NoBreakpointError is returned when trying to
// clear a breakpoint that does not exist.
type NoBreakpointError struct {
Addr uint64
}
func (nbp NoBreakpointError) Error() string {
return fmt.Sprintf("no breakpoint at %#v", nbp.Addr)
}
// BreakpointMap represents an (address, breakpoint) map.
type BreakpointMap struct {
M map[uint64]*Breakpoint
// Logical is a map of logical breakpoints.
Logical map[int]*LogicalBreakpoint
// WatchOutOfScope is the list of watchpoints that went out of scope during
// the last resume operation
WatchOutOfScope []*Breakpoint
}
// NewBreakpointMap creates a new BreakpointMap.
func NewBreakpointMap() BreakpointMap {
return BreakpointMap{
M: make(map[uint64]*Breakpoint),
}
}
// SetBreakpoint sets a breakpoint at addr, and stores it in the process wide
// break point table.
func (t *Target) SetBreakpoint(logicalID int, addr uint64, kind BreakpointKind, cond ast.Expr) (*Breakpoint, error) {
return t.setBreakpointInternal(logicalID, addr, kind, 0, cond)
}
// SetEBPFTracepoint will attach a uprobe to the function
// specified by 'fnName'.
func (t *Target) SetEBPFTracepoint(fnName string) error {
// 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.
if !t.proc.SupportsBPF() {
return errors.New("eBPF is not supported")
}
fns, err := t.BinInfo().FindFunction(fnName)
if err != nil {
return err
}
// 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
}
}
}
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.
// 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.
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
}
_, l, _ := t.BinInfo().PCToLine(fn.Entry)
var args []ebpf.UProbeArgMap
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
}
offset, pieces, _, err := t.BinInfo().Location(entry, dwarf.AttrLocation, fn.Entry, op.DwarfRegisters{}, nil)
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))
}
}
isret, _ := entry.Val(dwarf.AttrVarParam).(bool)
offset += int64(t.BinInfo().Arch.PtrSize())
args = append(args, ebpf.UProbeArgMap{
Offset: offset,
Size: dt.Size(),
Kind: dt.Common().ReflectKind,
Pieces: paramPieces,
InReg: len(pieces) > 0,
Ret: isret,
})
}
//TODO(aarzilli): inlined calls?
// Finally, set the uprobe on the function.
t.proc.SetUProbe(fn.Name, goidOffset, args)
return nil
}
// SetWatchpoint sets a data breakpoint at addr and stores it in the
// process wide break point table.
func (t *Target) SetWatchpoint(logicalID int, 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())
}
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")
}
bp, err := t.setBreakpointInternal(logicalID, xv.Addr, UserBreakpoint, wtype.withSize(uint8(sz)), cond)
if err != nil {
return bp, err
}
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
}
func (t *Target) setBreakpointInternal(logicalID int, addr uint64, kind BreakpointKind, wtype WatchType, cond ast.Expr) (*Breakpoint, error) {
if valid, err := t.Valid(); !valid {
recorded, _ := t.Recorded()
if !recorded {
return nil, err
}
}
bpmap := t.Breakpoints()
newBreaklet := &Breaklet{Kind: kind, Cond: cond}
if kind == UserBreakpoint {
newBreaklet.LogicalID = logicalID
}
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}
lbp.HitCount = make(map[int]uint64)
lbp.Enabled = true
bpmap.Logical[logicalID] = lbp
}
bp.Logical = lbp
breaklet := bp.UserBreaklet()
if breaklet != nil && breaklet.Cond == nil {
breaklet.Cond = lbp.Cond
}
lbp.File = bp.File
lbp.Line = bp.Line
fn := t.BinInfo().PCToFunc(bp.Addr)
if fn != nil {
lbp.FunctionName = fn.NameWithoutTypeParams()
}
}
if bp, ok := bpmap.M[addr]; ok {
if !bp.canOverlap(kind) {
return bp, BreakpointExistsError{bp.File, bp.Line, bp.Addr}
}
bp.Breaklets = append(bp.Breaklets, newBreaklet)
setLogicalBreakpoint(bp)
return bp, nil
}
f, l, fn := t.BinInfo().PCToLine(uint64(addr))
fnName := ""
if fn != nil {
fnName = fn.Name
}
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
}
}
}
newBreakpoint := &Breakpoint{
FunctionName: fnName,
WatchType: wtype,
HWBreakIndex: hwidx,
File: f,
Line: l,
Addr: addr,
}
err := t.proc.WriteBreakpoint(newBreakpoint)
if err != nil {
return nil, err
}
newBreakpoint.Breaklets = append(newBreakpoint.Breaklets, newBreaklet)
setLogicalBreakpoint(newBreakpoint)
bpmap.M[addr] = newBreakpoint
return newBreakpoint, nil
}
// 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
}
// ClearBreakpoint clears the breakpoint at addr.
func (t *Target) ClearBreakpoint(addr uint64) error {
if valid, err := t.Valid(); !valid {
recorded, _ := t.Recorded()
if !recorded {
return err
}
}
bp, ok := t.Breakpoints().M[addr]
if !ok {
return NoBreakpointError{Addr: addr}
}
for i := range bp.Breaklets {
if bp.Breaklets[i].Kind == UserBreakpoint {
bp.Breaklets[i] = nil
if bp.WatchExpr == "" {
bp.Logical = nil
}
}
}
_, err := t.finishClearBreakpoint(bp)
if err != nil {
return err
}
if bp.WatchExpr != "" && bp.watchStackOff != 0 {
// stack watchpoint, must remove all its WatchOutOfScopeBreakpoints/StackResizeBreakpoints
err := t.clearStackWatchBreakpoints(bp)
if err != nil {
return err
}
}
return nil
}
// ClearSteppingBreakpoints removes all stepping breakpoints from the map,
// calling clearBreakpoint on each one.
func (t *Target) ClearSteppingBreakpoints() error {
bpmap := t.Breakpoints()
threads := t.ThreadList()
for _, bp := range bpmap.M {
for i := range bp.Breaklets {
if bp.Breaklets[i].Kind&steppingMask != 0 {
bp.Breaklets[i] = nil
}
}
cleared, err := t.finishClearBreakpoint(bp)
if err != nil {
return err
}
if cleared {
for _, thread := range threads {
if thread.Breakpoint().Breakpoint == bp {
thread.Breakpoint().Clear()
}
}
}
}
return nil
}
// 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)
if bp.WatchExpr != "" && bp.Logical != nil {
delete(t.Breakpoints().Logical, bp.Logical.LogicalID)
}
return true, nil
}
// HasSteppingBreakpoints returns true if bpmap has at least one stepping
// breakpoint set.
func (bpmap *BreakpointMap) HasSteppingBreakpoints() bool {
for _, bp := range bpmap.M {
if bp.IsStepping() {
return true
}
}
return false
}
// 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
}
// BreakpointState describes the state of a breakpoint in a thread.
type BreakpointState struct {
*Breakpoint
// Active is true if the condition of any breaklet is met.
Active bool
// Stepping is true if one of the active breaklets is a stepping
// breakpoint.
Stepping bool
// SteppingInto is true if one of the active stepping breaklets has Kind ==
// StepBreakpoint.
SteppingInto bool
// CondError contains any error encountered while evaluating the
// breakpoint's condition.
CondError error
}
// Clear zeros the struct.
func (bpstate *BreakpointState) Clear() {
bpstate.Breakpoint = nil
bpstate.Active = false
bpstate.Stepping = false
bpstate.SteppingInto = false
bpstate.CondError = nil
}
func (bpstate *BreakpointState) String() string {
s := bpstate.Breakpoint.String()
if bpstate.Active {
s += " active"
}
if bpstate.Stepping {
s += " stepping"
}
return s
}
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(t *Target, thread Thread) []*Variable {
if rbpi == nil {
return nil
}
g, err := GetG(thread)
if err != nil {
return returnInfoError("could not get g", err, thread.ProcessMemory())
}
scope, err := GoroutineScope(t, thread)
if err != nil {
return returnInfoError("could not get scope", err, thread.ProcessMemory())
}
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
}
oldFrameOffset := rbpi.frameOffset + int64(g.stack.hi)
oldSP := uint64(rbpi.spOffset + int64(g.stack.hi))
err = fakeFunctionEntryScope(scope, rbpi.fn, oldFrameOffset, oldSP)
if err != nil {
return returnInfoError("could not read function entry", err, thread.ProcessMemory())
}
vars, err := scope.Locals(0)
if err != nil {
return returnInfoError("could not evaluate return variables", err, thread.ProcessMemory())
}
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}
}
// 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
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
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
// 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
}