variables.go 42.8 KB
Newer Older
D
Derek Parker 已提交
1
package proc
2 3 4 5

import (
	"bytes"
	"encoding/binary"
6
	"errors"
7
	"fmt"
8
	"go/constant"
9 10
	"go/parser"
	"go/token"
11
	"math"
12
	"reflect"
13 14 15
	"strings"
	"unsafe"

D
Derek Parker 已提交
16 17
	"github.com/derekparker/delve/pkg/dwarf/op"
	"github.com/derekparker/delve/pkg/dwarf/reader"
18
	"golang.org/x/debug/dwarf"
19 20
)

21
const (
22
	maxErrCount = 3 // Max number of read errors to accept while evaluating slices, arrays and structs
23

24
	maxArrayStridePrefetch = 1024 // Maximum size of array stride for which we will prefetch the array contents
25

D
Derek Parker 已提交
26 27
	chanRecv = "chan receive"
	chanSend = "chan send"
A
aarzilli 已提交
28 29 30

	hashTophashEmpty = 0 // used by map reading code, indicates an empty bucket
	hashMinTopHash   = 4 // used by map reading code, indicates minimum value of tophash that isn't empty or evacuated
31 32
)

33 34 35 36 37 38 39 40 41
type FloatSpecial uint8

const (
	FloatIsNormal FloatSpecial = iota
	FloatIsNaN
	FloatIsPosInf
	FloatIsNegInf
)

D
Derek Parker 已提交
42 43 44 45
// Variable represents a variable. It contains the address, name,
// type and other information parsed from both the Dwarf information
// and the memory of the debugged process.
// If OnlyAddr is true, the variables value has not been loaded.
46
type Variable struct {
47
	Addr      uintptr
A
aarzilli 已提交
48
	OnlyAddr  bool
49
	Name      string
50 51 52
	DwarfType dwarf.Type
	RealType  dwarf.Type
	Kind      reflect.Kind
53
	mem       MemoryReadWriter
54
	bi        *BinaryInfo
55

56 57
	Value        constant.Value
	FloatSpecial FloatSpecial
58 59 60 61

	Len int64
	Cap int64

62 63
	// Base address of arrays, Base address of the backing array for slices (0 for nil slices)
	// Base address of the backing byte array for strings
A
aarzilli 已提交
64
	// address of the struct backing chan and map variables
A
aarzilli 已提交
65
	// address of the function entry point for function variables (0 for nil function pointers)
66
	Base      uintptr
67 68
	stride    int64
	fieldType dwarf.Type
69

A
aarzilli 已提交
70 71 72
	// number of elements to skip when loading a map
	mapSkip int

73 74 75 76
	Children []Variable

	loaded     bool
	Unreadable error
77 78
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
type LoadConfig struct {
	// FollowPointers requests pointers to be automatically dereferenced.
	FollowPointers bool
	// MaxVariableRecurse is how far to recurse when evaluating nested types.
	MaxVariableRecurse int
	// MaxStringLen is the maximum number of bytes read from a string
	MaxStringLen int
	// MaxArrayValues is the maximum number of elements read from an array, a slice or a map.
	MaxArrayValues int
	// MaxStructFields is the maximum number of fields read from a struct, -1 will read all fields.
	MaxStructFields int
}

var loadSingleValue = LoadConfig{false, 0, 64, 0, 0}
var loadFullValue = LoadConfig{true, 1, 64, 64, -1}

D
Derek Parker 已提交
95
// M represents a runtime M (OS thread) structure.
96
type M struct {
97 98 99 100
	procid   int     // Thread ID or port.
	spinning uint8   // Busy looping.
	blocked  uint8   // Waiting on futex / semaphore.
	curg     uintptr // Current G running on this thread.
101 102
}

D
Derek Parker 已提交
103
// G status, from: src/runtime/runtime2.go
104
const (
D
Derek Parker 已提交
105 106 107 108 109 110 111 112 113
	Gidle           uint64 = iota // 0
	Grunnable                     // 1 runnable and on a run queue
	Grunning                      // 2
	Gsyscall                      // 3
	Gwaiting                      // 4
	GmoribundUnused               // 5 currently unused, but hardcoded in gdb scripts
	Gdead                         // 6
	Genqueue                      // 7 Only the Gscanenqueue is used.
	Gcopystack                    // 8 in this state when newstack is moving the stack
114 115
)

D
Derek Parker 已提交
116
// G represents a runtime G (goroutine) structure (at least the
117
// fields that Delve is interested in).
D
Derek Parker 已提交
118
type G struct {
D
Derek Parker 已提交
119
	ID         int    // Goroutine ID
120 121 122 123
	PC         uint64 // PC of goroutine when it was parked.
	SP         uint64 // SP of goroutine when it was parked.
	GoPC       uint64 // PC of 'go' statement that created this goroutine.
	WaitReason string // Reason for goroutine being parked.
124
	Status     uint64
A
Alessandro Arzilli 已提交
125 126
	stkbarVar  *Variable // stkbar field of g struct
	stkbarPos  int       // stkbarPos field of g struct
127

128
	// Information on goroutine location
129
	CurrentLoc Location
130

A
aarzilli 已提交
131
	// Thread that this goroutine is currently allocated to
132
	Thread Thread
133

A
Alessandro Arzilli 已提交
134
	variable *Variable
135 136
}

D
Derek Parker 已提交
137 138
// EvalScope is the scope for variable evaluation. Contains the thread,
// current location (PC), and canonical frame address.
139
type EvalScope struct {
140 141 142 143 144
	PC      uint64           // Current instruction of the evaluation frame
	CFA     int64            // Stack address of the evaluation frame
	Mem     MemoryReadWriter // Target's memory
	Gvar    *Variable
	BinInfo *BinaryInfo
145 146
}

D
Derek Parker 已提交
147
// IsNilErr is returned when a variable is nil.
148 149 150 151 152 153 154 155
type IsNilErr struct {
	name string
}

func (err *IsNilErr) Error() string {
	return fmt.Sprintf("%s is nil", err.name)
}

156
func (scope *EvalScope) newVariable(name string, addr uintptr, dwarfType dwarf.Type) *Variable {
157
	return newVariable(name, addr, dwarfType, scope.BinInfo, scope.Mem)
158 159
}

160
func newVariableFromThread(t Thread, name string, addr uintptr, dwarfType dwarf.Type) *Variable {
161
	return newVariable(name, addr, dwarfType, t.BinInfo(), t)
162 163
}

164
func (v *Variable) newVariable(name string, addr uintptr, dwarfType dwarf.Type) *Variable {
165
	return newVariable(name, addr, dwarfType, v.bi, v.mem)
166 167
}

168
func newVariable(name string, addr uintptr, dwarfType dwarf.Type, bi *BinaryInfo, mem MemoryReadWriter) *Variable {
169 170 171
	v := &Variable{
		Name:      name,
		Addr:      addr,
172
		DwarfType: dwarfType,
173
		mem:       mem,
174
		bi:        bi,
175 176
	}

177
	v.RealType = resolveTypedef(v.DwarfType)
178 179 180

	switch t := v.RealType.(type) {
	case *dwarf.PtrType:
181 182 183
		v.Kind = reflect.Ptr
		if _, isvoid := t.Type.(*dwarf.VoidType); isvoid {
			v.Kind = reflect.UnsafePointer
184
		}
185 186 187 188 189 190 191 192 193
	case *dwarf.ChanType:
		v.Kind = reflect.Chan
	case *dwarf.MapType:
		v.Kind = reflect.Map
	case *dwarf.StringType:
		v.Kind = reflect.String
		v.stride = 1
		v.fieldType = &dwarf.UintType{BasicType: dwarf.BasicType{CommonType: dwarf.CommonType{ByteSize: 1, Name: "byte"}, BitSize: 8, BitOffset: 0}}
		if v.Addr != 0 {
194
			v.Base, v.Len, v.Unreadable = readStringInfo(v.mem, v.bi.Arch, v.Addr)
195 196 197 198 199 200 201 202 203 204
		}
	case *dwarf.SliceType:
		v.Kind = reflect.Slice
		if v.Addr != 0 {
			v.loadSliceInfo(t)
		}
	case *dwarf.InterfaceType:
		v.Kind = reflect.Interface
	case *dwarf.StructType:
		v.Kind = reflect.Struct
205
	case *dwarf.ArrayType:
206
		v.Kind = reflect.Array
207
		v.Base = v.Addr
208 209 210 211 212 213 214 215
		v.Len = t.Count
		v.Cap = -1
		v.fieldType = t.Type
		v.stride = 0

		if t.Count > 0 {
			v.stride = t.ByteSize / t.Count
		}
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
	case *dwarf.ComplexType:
		switch t.ByteSize {
		case 8:
			v.Kind = reflect.Complex64
		case 16:
			v.Kind = reflect.Complex128
		}
	case *dwarf.IntType:
		v.Kind = reflect.Int
	case *dwarf.UintType:
		v.Kind = reflect.Uint
	case *dwarf.FloatType:
		switch t.ByteSize {
		case 4:
			v.Kind = reflect.Float32
		case 8:
			v.Kind = reflect.Float64
		}
	case *dwarf.BoolType:
		v.Kind = reflect.Bool
	case *dwarf.FuncType:
		v.Kind = reflect.Func
	case *dwarf.VoidType:
		v.Kind = reflect.Invalid
	case *dwarf.UnspecifiedType:
		v.Kind = reflect.Invalid
	default:
		v.Unreadable = fmt.Errorf("Unknown type: %T", t)
244 245
	}

246 247 248
	return v
}

249 250 251 252 253 254 255 256 257 258
func resolveTypedef(typ dwarf.Type) dwarf.Type {
	for {
		if tt, ok := typ.(*dwarf.TypedefType); ok {
			typ = tt.Type
		} else {
			return typ
		}
	}
}

259
func newConstant(val constant.Value, mem MemoryReadWriter) *Variable {
260
	v := &Variable{Value: val, mem: mem, loaded: true}
A
aarzilli 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	switch val.Kind() {
	case constant.Int:
		v.Kind = reflect.Int
	case constant.Float:
		v.Kind = reflect.Float64
	case constant.Bool:
		v.Kind = reflect.Bool
	case constant.Complex:
		v.Kind = reflect.Complex128
	case constant.String:
		v.Kind = reflect.String
		v.Len = int64(len(constant.StringVal(val)))
	}
	return v
}

var nilVariable = &Variable{
278
	Name:     "nil",
A
aarzilli 已提交
279
	Addr:     0,
280
	Base:     0,
A
aarzilli 已提交
281 282 283 284
	Kind:     reflect.Ptr,
	Children: []Variable{{Addr: 0, OnlyAddr: true}},
}

285 286 287
func (v *Variable) clone() *Variable {
	r := *v
	return &r
288 289
}

D
Derek Parker 已提交
290 291
// TypeString returns the string representation
// of the type of this variable.
292 293 294 295 296
func (v *Variable) TypeString() string {
	if v == nilVariable {
		return "nil"
	}
	if v.DwarfType != nil {
297
		return v.DwarfType.Common().Name
298 299 300 301
	}
	return v.Kind.String()
}

302
func (v *Variable) toField(field *dwarf.StructField) (*Variable, error) {
303 304 305
	if v.Unreadable != nil {
		return v.clone(), nil
	}
D
Derek Parker 已提交
306
	if v.Addr == 0 {
307
		return nil, &IsNilErr{v.Name}
D
Derek Parker 已提交
308 309
	}

310 311
	name := ""
	if v.Name != "" {
312 313 314 315
		parts := strings.Split(field.Name, ".")
		if len(parts) > 1 {
			name = fmt.Sprintf("%s.%s", v.Name, parts[1])
		} else {
D
Derek Parker 已提交
316
			name = fmt.Sprintf("%s.%s", v.Name, field.Name)
317
		}
318
	}
319
	return v.newVariable(name, uintptr(int64(v.Addr)+field.ByteOffset), field.Type), nil
320 321
}

D
Derek Parker 已提交
322 323
// DwarfReader returns the DwarfReader containing the
// Dwarf information for the target process.
324
func (scope *EvalScope) DwarfReader() *reader.Reader {
325
	return scope.BinInfo.DwarfReader()
326 327
}

D
Derek Parker 已提交
328
// Type returns the Dwarf type entry at `offset`.
329
func (scope *EvalScope) Type(offset dwarf.Offset) (dwarf.Type, error) {
330
	return scope.BinInfo.dwarf.Type(offset)
331 332
}

D
Derek Parker 已提交
333
// PtrSize returns the size of a pointer.
334
func (scope *EvalScope) PtrSize() int {
335
	return scope.BinInfo.Arch.PtrSize()
336 337
}

A
aarzilli 已提交
338
// ChanRecvBlocked returns whether the goroutine is blocked on
339
// a channel read operation.
340
func (g *G) ChanRecvBlocked() bool {
341
	return (g.Thread == nil) && (g.WaitReason == chanRecv)
D
Derek Parker 已提交
342 343
}

D
Derek Parker 已提交
344 345
// NoGError returned when a G could not be found
// for a specific thread.
346 347 348 349 350 351 352 353
type NoGError struct {
	tid int
}

func (ng NoGError) Error() string {
	return fmt.Sprintf("no G executing on thread %d", ng.tid)
}

354 355 356 357 358
func (gvar *Variable) parseG() (*G, error) {
	mem := gvar.mem
	gaddr := uint64(gvar.Addr)
	_, deref := gvar.RealType.(*dwarf.PtrType)

359
	if deref {
360
		gaddrbytes := make([]byte, gvar.bi.Arch.PtrSize())
361
		_, err := mem.ReadMemory(gaddrbytes, uintptr(gaddr))
362 363 364 365
		if err != nil {
			return nil, fmt.Errorf("error derefing *G %s", err)
		}
		gaddr = binary.LittleEndian.Uint64(gaddrbytes)
366
	}
D
Derek Parker 已提交
367
	if gaddr == 0 {
368
		id := 0
369
		if thread, ok := mem.(Thread); ok {
370
			id = thread.ThreadID()
371
		}
A
Alessandro Arzilli 已提交
372
		return nil, NoGError{tid: id}
373
	}
A
Alessandro Arzilli 已提交
374 375 376 377 378 379 380
	for {
		if _, isptr := gvar.RealType.(*dwarf.PtrType); !isptr {
			break
		}
		gvar = gvar.maybeDereference()
	}
	gvar.loadValue(LoadConfig{false, 1, 64, 0, -1})
D
Derek Parker 已提交
381 382
	if gvar.Unreadable != nil {
		return nil, gvar.Unreadable
383
	}
A
Alessandro Arzilli 已提交
384 385 386 387 388 389
	schedVar := gvar.fieldVariable("sched")
	pc, _ := constant.Int64Val(schedVar.fieldVariable("pc").Value)
	sp, _ := constant.Int64Val(schedVar.fieldVariable("sp").Value)
	id, _ := constant.Int64Val(gvar.fieldVariable("goid").Value)
	gopc, _ := constant.Int64Val(gvar.fieldVariable("gopc").Value)
	waitReason := constant.StringVal(gvar.fieldVariable("waitreason").Value)
390

A
Alessandro Arzilli 已提交
391
	stkbarVar, _ := gvar.structMember("stkbar")
392 393 394 395 396 397
	stkbarVarPosFld := gvar.fieldVariable("stkbarPos")
	var stkbarPos int64
	if stkbarVarPosFld != nil { // stack barriers were removed in Go 1.9
		stkbarPos, _ = constant.Int64Val(stkbarVarPosFld.Value)
	}

A
Alessandro Arzilli 已提交
398
	status, _ := constant.Int64Val(gvar.fieldVariable("atomicstatus").Value)
399
	f, l, fn := gvar.bi.PCToLine(uint64(pc))
D
Derek Parker 已提交
400 401 402 403 404 405 406 407
	g := &G{
		ID:         int(id),
		GoPC:       uint64(gopc),
		PC:         uint64(pc),
		SP:         uint64(sp),
		WaitReason: waitReason,
		Status:     uint64(status),
		CurrentLoc: Location{PC: uint64(pc), File: f, Line: l, Fn: fn},
A
Alessandro Arzilli 已提交
408
		variable:   gvar,
A
Alessandro Arzilli 已提交
409 410
		stkbarVar:  stkbarVar,
		stkbarPos:  int(stkbarPos),
411
	}
D
Derek Parker 已提交
412 413
	return g, nil
}
414

A
Alessandro Arzilli 已提交
415
func (v *Variable) loadFieldNamed(name string) *Variable {
D
Derek Parker 已提交
416
	v, err := v.structMember(name)
417
	if err != nil {
D
Derek Parker 已提交
418
		return nil
419
	}
420
	v.loadValue(loadFullValue)
D
Derek Parker 已提交
421 422
	if v.Unreadable != nil {
		return nil
423
	}
D
Derek Parker 已提交
424
	return v
425 426
}

A
Alessandro Arzilli 已提交
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
func (v *Variable) fieldVariable(name string) *Variable {
	for i := range v.Children {
		if child := &v.Children[i]; child.Name == name {
			return child
		}
	}
	return nil
}

// PC of entry to top-most deferred function.
func (g *G) DeferPC() uint64 {
	if g.variable.Unreadable != nil {
		return 0
	}
	d := g.variable.fieldVariable("_defer").maybeDereference()
	if d.Addr == 0 {
		return 0
	}
	d.loadValue(LoadConfig{false, 1, 64, 0, -1})
	if d.Unreadable != nil {
		return 0
	}
	fnvar := d.fieldVariable("fn").maybeDereference()
	if fnvar.Addr == 0 {
		return 0
	}
	fnvar.loadValue(LoadConfig{false, 1, 64, 0, -1})
	if fnvar.Unreadable != nil {
		return 0
	}
	deferPC, _ := constant.Int64Val(fnvar.fieldVariable("fn").Value)
	return uint64(deferPC)
}

461 462 463 464 465 466 467 468
// From $GOROOT/src/runtime/traceback.go:597
// isExportedRuntime reports whether name is an exported runtime function.
// It is only for runtime functions, so ASCII A-Z is fine.
func isExportedRuntime(name string) bool {
	const n = len("runtime.")
	return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
}

D
Derek Parker 已提交
469 470
// UserCurrent returns the location the users code is at,
// or was at before entering a runtime function.
471
func (g *G) UserCurrent() Location {
A
aarzilli 已提交
472 473 474
	it, err := g.stackIterator()
	if err != nil {
		return g.CurrentLoc
475 476 477
	}
	for it.Next() {
		frame := it.Frame()
478 479 480 481 482
		if frame.Call.Fn != nil {
			name := frame.Call.Fn.Name
			if (strings.Index(name, ".") >= 0) && (!strings.HasPrefix(name, "runtime.") || isExportedRuntime(name)) {
				return frame.Call
			}
483 484
		}
	}
485
	return g.CurrentLoc
486 487
}

D
Derek Parker 已提交
488 489
// Go returns the location of the 'go' statement
// that spawned this goroutine.
490
func (g *G) Go() Location {
491
	f, l, fn := g.variable.bi.goSymTable.PCToLine(g.GoPC)
492 493 494
	return Location{PC: g.GoPC, File: f, Line: l, Fn: fn}
}

A
Alessandro Arzilli 已提交
495 496
// Returns the list of saved return addresses used by stack barriers
func (g *G) stkbar() ([]savedLR, error) {
497 498 499
	if g.stkbarVar == nil { // stack barriers were removed in Go 1.9
		return nil, nil
	}
A
Alessandro Arzilli 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	g.stkbarVar.loadValue(LoadConfig{false, 1, 0, int(g.stkbarVar.Len), 3})
	if g.stkbarVar.Unreadable != nil {
		return nil, fmt.Errorf("unreadable stkbar: %v\n", g.stkbarVar.Unreadable)
	}
	r := make([]savedLR, len(g.stkbarVar.Children))
	for i, child := range g.stkbarVar.Children {
		for _, field := range child.Children {
			switch field.Name {
			case "savedLRPtr":
				ptr, _ := constant.Int64Val(field.Value)
				r[i].ptr = uint64(ptr)
			case "savedLRVal":
				val, _ := constant.Int64Val(field.Value)
				r[i].val = uint64(val)
			}
		}
	}
	return r, nil
}

D
Derek Parker 已提交
520
// EvalVariable returns the value of the given expression (backwards compatibility).
521 522
func (scope *EvalScope) EvalVariable(name string, cfg LoadConfig) (*Variable, error) {
	return scope.EvalExpression(name, cfg)
A
aarzilli 已提交
523
}
524

D
Derek Parker 已提交
525
// SetVariable sets the value of the named variable
A
aarzilli 已提交
526 527
func (scope *EvalScope) SetVariable(name, value string) error {
	t, err := parser.ParseExpr(name)
528
	if err != nil {
A
aarzilli 已提交
529
		return err
530 531
	}

A
aarzilli 已提交
532
	xv, err := scope.evalAST(t)
533
	if err != nil {
A
aarzilli 已提交
534
		return err
535
	}
536

A
aarzilli 已提交
537 538 539 540 541 542 543 544 545
	if xv.Addr == 0 {
		return fmt.Errorf("Can not assign to \"%s\"", name)
	}

	if xv.Unreadable != nil {
		return fmt.Errorf("Expression \"%s\" is unreadable: %v", name, xv.Unreadable)
	}

	t, err = parser.ParseExpr(value)
546 547 548
	if err != nil {
		return err
	}
A
aarzilli 已提交
549 550 551 552 553 554

	yv, err := scope.evalAST(t)
	if err != nil {
		return err
	}

555
	yv.loadValue(loadSingleValue)
A
aarzilli 已提交
556 557 558

	if err := yv.isType(xv.RealType, xv.Kind); err != nil {
		return err
559
	}
A
aarzilli 已提交
560 561 562 563 564 565

	if yv.Unreadable != nil {
		return fmt.Errorf("Expression \"%s\" is unreadable: %v", value, yv.Unreadable)
	}

	return xv.setValue(yv)
566 567
}

568
func (scope *EvalScope) extractVariableFromEntry(entry *dwarf.Entry, cfg LoadConfig) (*Variable, error) {
569
	rdr := scope.DwarfReader()
570
	v, err := scope.extractVarInfoFromEntry(entry, rdr)
571
	if err != nil {
D
Derek Parker 已提交
572 573
		return nil, err
	}
574
	return v, nil
575
}
D
Derek Parker 已提交
576

577 578
func (scope *EvalScope) extractVarInfo(varName string) (*Variable, error) {
	reader := scope.DwarfReader()
579
	off, err := scope.BinInfo.findFunctionDebugInfo(scope.PC)
580 581
	if err != nil {
		return nil, err
582
	}
A
Alessandro Arzilli 已提交
583 584
	reader.Seek(off)
	reader.Next()
585

586
	for entry, err := reader.NextScopeVariable(); entry != nil; entry, err = reader.NextScopeVariable() {
D
Derek Parker 已提交
587
		if err != nil {
588
			return nil, err
D
Derek Parker 已提交
589
		}
A
Alessandro Arzilli 已提交
590 591 592
		if entry.Tag == 0 {
			break
		}
D
Derek Parker 已提交
593 594 595 596 597 598

		n, ok := entry.Val(dwarf.AttrName).(string)
		if !ok {
			continue
		}

599
		if n == varName {
600
			return scope.extractVarInfoFromEntry(entry, reader)
D
Derek Parker 已提交
601 602
		}
	}
603
	return nil, fmt.Errorf("could not find symbol value for %s", varName)
D
Derek Parker 已提交
604
}
605

606
// LocalVariables returns all local variables from the current function scope.
607 608
func (scope *EvalScope) LocalVariables(cfg LoadConfig) ([]*Variable, error) {
	return scope.variablesByTag(dwarf.TagVariable, cfg)
609 610 611
}

// FunctionArguments returns the name, value, and type of all current function arguments.
612 613
func (scope *EvalScope) FunctionArguments(cfg LoadConfig) ([]*Variable, error) {
	return scope.variablesByTag(dwarf.TagFormalParameter, cfg)
614 615 616
}

// PackageVariables returns the name, value, and type of all package variables in the application.
617
func (scope *EvalScope) PackageVariables(cfg LoadConfig) ([]*Variable, error) {
D
Derek Parker 已提交
618
	var vars []*Variable
619
	reader := scope.DwarfReader()
620

621 622 623 624 625 626
	var utypoff dwarf.Offset
	utypentry, err := reader.SeekToTypeNamed("<unspecified>")
	if err == nil {
		utypoff = utypentry.Offset
	}

627 628 629 630 631
	for entry, err := reader.NextPackageVariable(); entry != nil; entry, err = reader.NextPackageVariable() {
		if err != nil {
			return nil, err
		}

632 633 634 635
		if typoff, ok := entry.Val(dwarf.AttrType).(dwarf.Offset); !ok || typoff == utypoff {
			continue
		}

636
		// Ignore errors trying to extract values
637
		val, err := scope.extractVariableFromEntry(entry, cfg)
638 639 640
		if err != nil {
			continue
		}
A
Alessandro Arzilli 已提交
641
		val.loadValue(cfg)
642 643 644 645 646 647
		vars = append(vars, val)
	}

	return vars, nil
}

648 649
func (scope *EvalScope) packageVarAddr(name string) (*Variable, error) {
	reader := scope.DwarfReader()
650 651 652 653 654 655 656 657 658 659 660
	for entry, err := reader.NextPackageVariable(); entry != nil; entry, err = reader.NextPackageVariable() {
		if err != nil {
			return nil, err
		}

		n, ok := entry.Val(dwarf.AttrName).(string)
		if !ok {
			continue
		}

		if n == name {
661
			return scope.extractVarInfoFromEntry(entry, reader)
662 663 664 665 666
		}
	}
	return nil, fmt.Errorf("could not find symbol value for %s", name)
}

667
func (v *Variable) structMember(memberName string) (*Variable, error) {
668 669 670 671
	if v.Unreadable != nil {
		return v.clone(), nil
	}
	structVar := v.maybeDereference()
672
	structVar.Name = v.Name
673 674
	if structVar.Unreadable != nil {
		return structVar, nil
675
	}
676 677

	switch t := structVar.RealType.(type) {
678 679 680 681
	case *dwarf.StructType:
		for _, field := range t.Field {
			if field.Name != memberName {
				continue
682
			}
L
Luke Hoban 已提交
683
			return structVar.toField(field)
684 685 686 687 688
		}
		// Check for embedded field only if field was
		// not a regular struct member
		for _, field := range t.Field {
			isEmbeddedStructMember :=
689
				(field.Type.Common().Name == field.Name) ||
D
Derek Parker 已提交
690 691
					(len(field.Name) > 1 &&
						field.Name[0] == '*' &&
692
						field.Type.Common().Name[1:] == field.Name[1:])
693 694 695 696 697 698
			if !isEmbeddedStructMember {
				continue
			}
			// Check for embedded field referenced by type name
			parts := strings.Split(field.Name, ".")
			if len(parts) > 1 && parts[1] == memberName {
L
Luke Hoban 已提交
699
				embeddedVar, err := structVar.toField(field)
700 701 702 703 704 705
				if err != nil {
					return nil, err
				}
				return embeddedVar, nil
			}
			// Recursively check for promoted fields on the embedded field
L
Luke Hoban 已提交
706
			embeddedVar, err := structVar.toField(field)
707 708 709 710 711 712 713
			if err != nil {
				return nil, err
			}
			embeddedVar.Name = structVar.Name
			embeddedField, err := embeddedVar.structMember(memberName)
			if embeddedField != nil {
				return embeddedField, nil
714 715
			}
		}
716 717
		return nil, fmt.Errorf("%s has no member %s", v.Name, memberName)
	default:
718 719 720
		if v.Name == "" {
			return nil, fmt.Errorf("type %s is not a struct", structVar.TypeString())
		}
D
Derek Parker 已提交
721
		return nil, fmt.Errorf("%s (type %s) is not a struct", v.Name, structVar.TypeString())
722
	}
723 724
}

725 726 727
// Extracts the name and type of a variable from a dwarf entry
// then executes the instructions given in the  DW_AT_location attribute to grab the variable's address
func (scope *EvalScope) extractVarInfoFromEntry(entry *dwarf.Entry, rdr *reader.Reader) (*Variable, error) {
E
epipho 已提交
728
	if entry == nil {
D
Derek Parker 已提交
729
		return nil, fmt.Errorf("invalid entry")
E
epipho 已提交
730 731 732 733 734 735 736 737
	}

	if entry.Tag != dwarf.TagFormalParameter && entry.Tag != dwarf.TagVariable {
		return nil, fmt.Errorf("invalid entry tag, only supports FormalParameter and Variable, got %s", entry.Tag.String())
	}

	n, ok := entry.Val(dwarf.AttrName).(string)
	if !ok {
D
Derek Parker 已提交
738
		return nil, fmt.Errorf("type assertion failed")
E
epipho 已提交
739 740 741 742
	}

	offset, ok := entry.Val(dwarf.AttrType).(dwarf.Offset)
	if !ok {
D
Derek Parker 已提交
743
		return nil, fmt.Errorf("type assertion failed")
E
epipho 已提交
744 745
	}

746
	t, err := scope.Type(offset)
E
epipho 已提交
747 748 749 750 751 752
	if err != nil {
		return nil, err
	}

	instructions, ok := entry.Val(dwarf.AttrLocation).([]byte)
	if !ok {
D
Derek Parker 已提交
753
		return nil, fmt.Errorf("type assertion failed")
E
epipho 已提交
754 755
	}

756
	addr, err := op.ExecuteStackProgram(scope.CFA, instructions)
E
epipho 已提交
757 758 759 760
	if err != nil {
		return nil, err
	}

761
	return scope.newVariable(n, uintptr(addr), t), nil
E
epipho 已提交
762 763
}

764
// If v is a pointer a new variable is returned containing the value pointed by v.
765 766 767 768
func (v *Variable) maybeDereference() *Variable {
	if v.Unreadable != nil {
		return v
	}
769

770
	switch t := v.RealType.(type) {
771
	case *dwarf.PtrType:
772 773
		ptrval, err := readUintRaw(v.mem, uintptr(v.Addr), t.ByteSize)
		r := v.newVariable("", uintptr(ptrval), t.Type)
774
		if err != nil {
775
			r.Unreadable = err
776 777
		}

778
		return r
779
	default:
780
		return v
781 782
	}
}
783

784
// Extracts the value of the variable at the given address.
785 786
func (v *Variable) loadValue(cfg LoadConfig) {
	v.loadValueInternal(0, cfg)
787 788
}

789
func (v *Variable) loadValueInternal(recurseLevel int, cfg LoadConfig) {
790
	if v.Unreadable != nil || v.loaded || (v.Addr == 0 && v.Base == 0) {
791 792
		return
	}
793

794 795
	v.loaded = true
	switch v.Kind {
A
aarzilli 已提交
796
	case reflect.Ptr, reflect.UnsafePointer:
797 798
		v.Len = 1
		v.Children = []Variable{*v.maybeDereference()}
799 800
		if cfg.FollowPointers {
			// Don't increase the recursion level when dereferencing pointers
801 802 803 804 805 806
			// unless this is a pointer to interface (which could cause an infinite loop)
			nextLvl := recurseLevel
			if v.Children[0].Kind == reflect.Interface {
				nextLvl++
			}
			v.Children[0].loadValueInternal(nextLvl, cfg)
807 808 809
		} else {
			v.Children[0].OnlyAddr = true
		}
810

A
aarzilli 已提交
811
	case reflect.Chan:
812 813 814
		sv := v.clone()
		sv.RealType = resolveTypedef(&(sv.RealType.(*dwarf.ChanType).TypedefType))
		sv = sv.maybeDereference()
815
		sv.loadValueInternal(0, loadFullValue)
A
aarzilli 已提交
816 817
		v.Children = sv.Children
		v.Len = sv.Len
818
		v.Base = sv.Addr
A
aarzilli 已提交
819

A
aarzilli 已提交
820
	case reflect.Map:
821 822
		if recurseLevel <= cfg.MaxVariableRecurse {
			v.loadMap(recurseLevel, cfg)
A
aarzilli 已提交
823
		}
A
aarzilli 已提交
824

825
	case reflect.String:
826
		var val string
827
		val, v.Unreadable = readStringValue(v.mem, v.Base, v.Len, cfg)
828
		v.Value = constant.MakeString(val)
829 830

	case reflect.Slice, reflect.Array:
831
		v.loadArrayValues(recurseLevel, cfg)
832 833

	case reflect.Struct:
834
		v.mem = cacheMemory(v.mem, v.Addr, int(v.RealType.Size()))
835 836 837 838
		t := v.RealType.(*dwarf.StructType)
		v.Len = int64(len(t.Field))
		// Recursively call extractValue to grab
		// the value of all the members of the struct.
839
		if recurseLevel <= cfg.MaxVariableRecurse {
840 841
			v.Children = make([]Variable, 0, len(t.Field))
			for i, field := range t.Field {
842 843 844
				if cfg.MaxStructFields >= 0 && len(v.Children) >= cfg.MaxStructFields {
					break
				}
845 846 847
				f, _ := v.toField(field)
				v.Children = append(v.Children, *f)
				v.Children[i].Name = field.Name
848
				v.Children[i].loadValueInternal(recurseLevel+1, cfg)
849
			}
850 851
		}

852
	case reflect.Interface:
853
		v.loadInterface(recurseLevel, true, cfg)
854

855 856 857
	case reflect.Complex64, reflect.Complex128:
		v.readComplex(v.RealType.(*dwarf.ComplexType).ByteSize)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
858
		var val int64
859
		val, v.Unreadable = readIntRaw(v.mem, v.Addr, v.RealType.(*dwarf.IntType).ByteSize)
860
		v.Value = constant.MakeInt64(val)
861
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
862
		var val uint64
863
		val, v.Unreadable = readUintRaw(v.mem, v.Addr, v.RealType.(*dwarf.UintType).ByteSize)
864 865
		v.Value = constant.MakeUint64(val)

866
	case reflect.Bool:
867 868
		val := make([]byte, 1)
		_, err := v.mem.ReadMemory(val, v.Addr)
869 870
		v.Unreadable = err
		if err == nil {
871
			v.Value = constant.MakeBool(val[0] != 0)
872
		}
873
	case reflect.Float32, reflect.Float64:
874 875 876
		var val float64
		val, v.Unreadable = v.readFloatRaw(v.RealType.(*dwarf.FloatType).ByteSize)
		v.Value = constant.MakeFloat64(val)
877 878 879 880 881 882 883 884
		switch {
		case math.IsInf(val, +1):
			v.FloatSpecial = FloatIsPosInf
		case math.IsInf(val, -1):
			v.FloatSpecial = FloatIsNegInf
		case math.IsNaN(val):
			v.FloatSpecial = FloatIsNaN
		}
885 886
	case reflect.Func:
		v.readFunctionPtr()
887
	default:
888
		v.Unreadable = fmt.Errorf("unknown or unsupported kind: \"%s\"", v.Kind.String())
889 890 891
	}
}

A
aarzilli 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
func (v *Variable) setValue(y *Variable) error {
	var err error
	switch v.Kind {
	case reflect.Float32, reflect.Float64:
		f, _ := constant.Float64Val(y.Value)
		err = v.writeFloatRaw(f, v.RealType.Size())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		n, _ := constant.Int64Val(y.Value)
		err = v.writeUint(uint64(n), v.RealType.Size())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		n, _ := constant.Uint64Val(y.Value)
		err = v.writeUint(n, v.RealType.Size())
	case reflect.Bool:
		err = v.writeBool(constant.BoolVal(y.Value))
	case reflect.Complex64, reflect.Complex128:
		real, _ := constant.Float64Val(constant.Real(y.Value))
		imag, _ := constant.Float64Val(constant.Imag(y.Value))
		err = v.writeComplex(real, imag, v.RealType.Size())
910
	default:
911 912
		if t, isptr := v.RealType.(*dwarf.PtrType); isptr {
			err = v.writeUint(uint64(y.Children[0].Addr), int64(t.ByteSize))
A
aarzilli 已提交
913 914 915
		} else {
			return fmt.Errorf("can not set variables of type %s (not implemented)", v.Kind.String())
		}
916
	}
A
aarzilli 已提交
917 918

	return err
919 920
}

921
func readStringInfo(mem MemoryReadWriter, arch Arch, addr uintptr) (uintptr, int64, error) {
922 923 924
	// string data structure is always two ptrs in size. Addr, followed by len
	// http://research.swtch.com/godata

925 926
	mem = cacheMemory(mem, addr, arch.PtrSize()*2)

927
	// read len
928 929
	val := make([]byte, arch.PtrSize())
	_, err := mem.ReadMemory(val, addr+uintptr(arch.PtrSize()))
930
	if err != nil {
A
aarzilli 已提交
931
		return 0, 0, fmt.Errorf("could not read string len %s", err)
932
	}
933
	strlen := int64(binary.LittleEndian.Uint64(val))
934
	if strlen < 0 {
A
aarzilli 已提交
935
		return 0, 0, fmt.Errorf("invalid length: %d", strlen)
936
	}
937 938

	// read addr
939
	_, err = mem.ReadMemory(val, addr)
940
	if err != nil {
A
aarzilli 已提交
941
		return 0, 0, fmt.Errorf("could not read string pointer %s", err)
942 943
	}
	addr = uintptr(binary.LittleEndian.Uint64(val))
D
Derek Parker 已提交
944
	if addr == 0 {
A
aarzilli 已提交
945
		return 0, 0, nil
D
Derek Parker 已提交
946
	}
D
Derek Parker 已提交
947

A
aarzilli 已提交
948 949 950
	return addr, strlen, nil
}

951
func readStringValue(mem MemoryReadWriter, addr uintptr, strlen int64, cfg LoadConfig) (string, error) {
A
aarzilli 已提交
952
	count := strlen
953 954
	if count > int64(cfg.MaxStringLen) {
		count = int64(cfg.MaxStringLen)
A
aarzilli 已提交
955 956
	}

957 958
	val := make([]byte, int(count))
	_, err := mem.ReadMemory(val, addr)
959
	if err != nil {
A
aarzilli 已提交
960
		return "", fmt.Errorf("could not read string at %#v due to %s", addr, err)
961 962
	}

963 964
	retstr := *(*string)(unsafe.Pointer(&val))

A
aarzilli 已提交
965 966 967
	return retstr, nil
}

968
func (v *Variable) loadSliceInfo(t *dwarf.SliceType) {
969 970
	v.mem = cacheMemory(v.mem, v.Addr, int(t.Size()))

971
	var err error
E
epipho 已提交
972 973 974
	for _, f := range t.Field {
		switch f.Name {
		case "array":
975
			var base uint64
976
			base, err = readUintRaw(v.mem, uintptr(int64(v.Addr)+f.ByteOffset), f.Type.Size())
977
			if err == nil {
978
				v.Base = uintptr(base)
979 980 981
				// Dereference array type to get value type
				ptrType, ok := f.Type.(*dwarf.PtrType)
				if !ok {
982 983
					v.Unreadable = fmt.Errorf("Invalid type %s in slice array", f.Type)
					return
984 985
				}
				v.fieldType = ptrType.Type
E
epipho 已提交
986 987
			}
		case "len":
988
			lstrAddr, _ := v.toField(f)
989
			lstrAddr.loadValue(loadSingleValue)
990
			err = lstrAddr.Unreadable
991
			if err == nil {
992
				v.Len, _ = constant.Int64Val(lstrAddr.Value)
E
epipho 已提交
993 994
			}
		case "cap":
995
			cstrAddr, _ := v.toField(f)
996
			cstrAddr.loadValue(loadSingleValue)
997
			err = cstrAddr.Unreadable
998
			if err == nil {
999
				v.Cap, _ = constant.Int64Val(cstrAddr.Value)
E
epipho 已提交
1000 1001
			}
		}
1002 1003 1004 1005
		if err != nil {
			v.Unreadable = err
			return
		}
1006 1007
	}

1008
	v.stride = v.fieldType.Size()
1009 1010
	if t, ok := v.fieldType.(*dwarf.PtrType); ok {
		v.stride = t.ByteSize
1011
	}
1012

1013
	return
1014 1015
}

1016
func (v *Variable) loadArrayValues(recurseLevel int, cfg LoadConfig) {
1017 1018 1019
	if v.Unreadable != nil {
		return
	}
1020 1021 1022 1023
	if v.Len < 0 {
		v.Unreadable = errors.New("Negative array length")
		return
	}
1024

1025 1026
	count := v.Len
	// Cap number of elements
1027 1028
	if count > int64(cfg.MaxArrayValues) {
		count = int64(cfg.MaxArrayValues)
1029
	}
1030

1031
	if v.stride < maxArrayStridePrefetch {
1032
		v.mem = cacheMemory(v.mem, v.Base, int(v.stride*count))
1033
	}
1034

1035 1036 1037
	errcount := 0

	for i := int64(0); i < count; i++ {
1038
		fieldvar := v.newVariable("", uintptr(int64(v.Base)+(i*v.stride)), v.fieldType)
1039
		fieldvar.loadValueInternal(recurseLevel+1, cfg)
1040 1041

		if fieldvar.Unreadable != nil {
1042
			errcount++
E
epipho 已提交
1043
		}
1044

1045
		v.Children = append(v.Children, *fieldvar)
1046 1047 1048
		if errcount > maxErrCount {
			break
		}
1049 1050 1051
	}
}

1052
func (v *Variable) readComplex(size int64) {
1053 1054 1055 1056 1057 1058 1059
	var fs int64
	switch size {
	case 8:
		fs = 4
	case 16:
		fs = 8
	default:
1060 1061
		v.Unreadable = fmt.Errorf("invalid size (%d) for complex type", size)
		return
1062
	}
1063 1064 1065

	ftyp := &dwarf.FloatType{BasicType: dwarf.BasicType{CommonType: dwarf.CommonType{ByteSize: fs, Name: fmt.Sprintf("float%d", fs)}, BitSize: fs * 8, BitOffset: 0}}

1066 1067
	realvar := v.newVariable("real", v.Addr, ftyp)
	imagvar := v.newVariable("imaginary", v.Addr+uintptr(fs), ftyp)
1068 1069
	realvar.loadValue(loadSingleValue)
	imagvar.loadValue(loadSingleValue)
A
aarzilli 已提交
1070
	v.Value = constant.BinaryOp(realvar.Value, token.ADD, constant.MakeImag(imagvar.Value))
1071 1072
}

A
aarzilli 已提交
1073 1074
func (v *Variable) writeComplex(real, imag float64, size int64) error {
	err := v.writeFloatRaw(real, int64(size/2))
1075 1076 1077 1078 1079 1080 1081 1082
	if err != nil {
		return err
	}
	imagaddr := *v
	imagaddr.Addr += uintptr(size / 2)
	return imagaddr.writeFloatRaw(imag, int64(size/2))
}

1083
func readIntRaw(mem MemoryReadWriter, addr uintptr, size int64) (int64, error) {
E
epipho 已提交
1084
	var n int64
1085

1086 1087
	val := make([]byte, int(size))
	_, err := mem.ReadMemory(val, addr)
1088
	if err != nil {
D
Derek Parker 已提交
1089
		return 0, err
1090 1091
	}

1092 1093
	switch size {
	case 1:
1094
		n = int64(int8(val[0]))
1095
	case 2:
1096
		n = int64(int16(binary.LittleEndian.Uint16(val)))
1097
	case 4:
1098
		n = int64(int32(binary.LittleEndian.Uint32(val)))
1099
	case 8:
E
epipho 已提交
1100
		n = int64(binary.LittleEndian.Uint64(val))
1101
	}
1102

D
Derek Parker 已提交
1103
	return n, nil
E
epipho 已提交
1104 1105
}

A
aarzilli 已提交
1106
func (v *Variable) writeUint(value uint64, size int64) error {
1107 1108 1109 1110
	val := make([]byte, size)

	switch size {
	case 1:
A
aarzilli 已提交
1111
		val[0] = byte(value)
1112
	case 2:
A
aarzilli 已提交
1113
		binary.LittleEndian.PutUint16(val, uint16(value))
1114
	case 4:
A
aarzilli 已提交
1115
		binary.LittleEndian.PutUint32(val, uint32(value))
1116
	case 8:
A
aarzilli 已提交
1117
		binary.LittleEndian.PutUint64(val, uint64(value))
1118 1119
	}

1120
	_, err := v.mem.WriteMemory(v.Addr, val)
1121 1122 1123
	return err
}

1124
func readUintRaw(mem MemoryReadWriter, addr uintptr, size int64) (uint64, error) {
E
epipho 已提交
1125 1126
	var n uint64

1127 1128
	val := make([]byte, int(size))
	_, err := mem.ReadMemory(val, addr)
E
epipho 已提交
1129
	if err != nil {
D
Derek Parker 已提交
1130
		return 0, err
E
epipho 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	}

	switch size {
	case 1:
		n = uint64(val[0])
	case 2:
		n = uint64(binary.LittleEndian.Uint16(val))
	case 4:
		n = uint64(binary.LittleEndian.Uint32(val))
	case 8:
		n = uint64(binary.LittleEndian.Uint64(val))
	}

D
Derek Parker 已提交
1144
	return n, nil
1145 1146
}

1147
func (v *Variable) readFloatRaw(size int64) (float64, error) {
1148 1149
	val := make([]byte, int(size))
	_, err := v.mem.ReadMemory(val, v.Addr)
1150
	if err != nil {
1151
		return 0.0, err
1152 1153 1154
	}
	buf := bytes.NewBuffer(val)

D
Derek Parker 已提交
1155 1156 1157 1158
	switch size {
	case 4:
		n := float32(0)
		binary.Read(buf, binary.LittleEndian, &n)
1159
		return float64(n), nil
D
Derek Parker 已提交
1160 1161 1162
	case 8:
		n := float64(0)
		binary.Read(buf, binary.LittleEndian, &n)
1163
		return n, nil
D
Derek Parker 已提交
1164 1165
	}

1166
	return 0.0, fmt.Errorf("could not read float")
1167 1168
}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
func (v *Variable) writeFloatRaw(f float64, size int64) error {
	buf := bytes.NewBuffer(make([]byte, 0, size))

	switch size {
	case 4:
		n := float32(f)
		binary.Write(buf, binary.LittleEndian, n)
	case 8:
		n := float64(f)
		binary.Write(buf, binary.LittleEndian, n)
	}

1181
	_, err := v.mem.WriteMemory(v.Addr, buf.Bytes())
1182 1183 1184
	return err
}

A
aarzilli 已提交
1185
func (v *Variable) writeBool(value bool) error {
1186
	val := []byte{0}
A
aarzilli 已提交
1187
	val[0] = *(*byte)(unsafe.Pointer(&value))
1188
	_, err := v.mem.WriteMemory(v.Addr, val)
1189 1190 1191
	return err
}

1192
func (v *Variable) readFunctionPtr() {
1193
	val := make([]byte, v.bi.Arch.PtrSize())
1194
	_, err := v.mem.ReadMemory(val, v.Addr)
1195
	if err != nil {
1196 1197
		v.Unreadable = err
		return
1198 1199 1200
	}

	// dereference pointer to find function pc
1201 1202
	fnaddr := uintptr(binary.LittleEndian.Uint64(val))
	if fnaddr == 0 {
1203
		v.Base = 0
A
aarzilli 已提交
1204
		v.Value = constant.MakeString("")
1205
		return
1206
	}
1207

1208
	_, err = v.mem.ReadMemory(val, fnaddr)
1209
	if err != nil {
1210 1211
		v.Unreadable = err
		return
1212 1213
	}

1214
	v.Base = uintptr(binary.LittleEndian.Uint64(val))
1215
	fn := v.bi.goSymTable.PCToFunc(uint64(v.Base))
1216
	if fn == nil {
1217
		v.Unreadable = fmt.Errorf("could not find function for %#v", v.Base)
1218
		return
1219 1220
	}

1221
	v.Value = constant.MakeString(fn.Name)
1222 1223
}

1224
func (v *Variable) loadMap(recurseLevel int, cfg LoadConfig) {
A
aarzilli 已提交
1225 1226 1227 1228 1229 1230 1231
	it := v.mapIterator()
	if it == nil {
		return
	}

	for skip := 0; skip < v.mapSkip; skip++ {
		if ok := it.next(); !ok {
1232
			v.Unreadable = fmt.Errorf("map index out of bounds")
A
aarzilli 已提交
1233 1234 1235 1236 1237 1238 1239
			return
		}
	}

	count := 0
	errcount := 0
	for it.next() {
1240
		if count >= cfg.MaxArrayValues {
A
aarzilli 已提交
1241 1242 1243 1244
			break
		}
		key := it.key()
		val := it.value()
1245 1246
		key.loadValueInternal(recurseLevel+1, cfg)
		val.loadValueInternal(recurseLevel+1, cfg)
A
aarzilli 已提交
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		if key.Unreadable != nil || val.Unreadable != nil {
			errcount++
		}
		v.Children = append(v.Children, *key)
		v.Children = append(v.Children, *val)
		count++
		if errcount > maxErrCount {
			break
		}
	}
}

type mapIterator struct {
	v          *Variable
	numbuckets uint64
	oldmask    uint64
	buckets    *Variable
	oldbuckets *Variable
	b          *Variable
	bidx       uint64

	tophashes *Variable
	keys      *Variable
	values    *Variable
	overflow  *Variable

	idx int64
}

// Code derived from go/src/runtime/hashmap.go
func (v *Variable) mapIterator() *mapIterator {
1278 1279 1280
	sv := v.clone()
	sv.RealType = resolveTypedef(&(sv.RealType.(*dwarf.MapType).TypedefType))
	sv = sv.maybeDereference()
1281
	v.Base = sv.Addr
A
aarzilli 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

	maptype, ok := sv.RealType.(*dwarf.StructType)
	if !ok {
		v.Unreadable = fmt.Errorf("wrong real type for map")
		return nil
	}

	it := &mapIterator{v: v, bidx: 0, b: nil, idx: 0}

	if sv.Addr == 0 {
		it.numbuckets = 0
		return it
	}

1296
	v.mem = cacheMemory(v.mem, v.Base, int(v.RealType.Size()))
1297

A
aarzilli 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	for _, f := range maptype.Field {
		var err error
		field, _ := sv.toField(f)
		switch f.Name {
		case "count":
			v.Len, err = field.asInt()
		case "B":
			var b uint64
			b, err = field.asUint()
			it.numbuckets = 1 << b
			it.oldmask = (1 << (b - 1)) - 1
		case "buckets":
			it.buckets = field.maybeDereference()
		case "oldbuckets":
			it.oldbuckets = field.maybeDereference()
		}
		if err != nil {
			v.Unreadable = err
			return nil
		}
	}

1320 1321 1322 1323 1324
	if it.buckets.Kind != reflect.Struct || it.oldbuckets.Kind != reflect.Struct {
		v.Unreadable = mapBucketsNotStructErr
		return nil
	}

A
aarzilli 已提交
1325 1326 1327
	return it
}

1328 1329 1330 1331
var mapBucketContentsNotArrayErr = errors.New("malformed map type: keys, values or tophash of a bucket is not an array")
var mapBucketContentsInconsistentLenErr = errors.New("malformed map type: inconsistent array length in bucket")
var mapBucketsNotStructErr = errors.New("malformed map type: buckets, oldbuckets or overflow field not a struct")

A
aarzilli 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
func (it *mapIterator) nextBucket() bool {
	if it.overflow != nil && it.overflow.Addr > 0 {
		it.b = it.overflow
	} else {
		it.b = nil

		for it.bidx < it.numbuckets {
			it.b = it.buckets.clone()
			it.b.Addr += uintptr(uint64(it.buckets.DwarfType.Size()) * it.bidx)

			if it.oldbuckets.Addr <= 0 {
				break
			}

			// if oldbuckets is not nil we are iterating through a map that is in
			// the middle of a grow.
			// if the bucket we are looking at hasn't been filled in we iterate
			// instead through its corresponding "oldbucket" (i.e. the bucket the
			// elements of this bucket are coming from) but only if this is the first
			// of the two buckets being created from the same oldbucket (otherwise we
			// would print some keys twice)

			oldbidx := it.bidx & it.oldmask
			oldb := it.oldbuckets.clone()
			oldb.Addr += uintptr(uint64(it.oldbuckets.DwarfType.Size()) * oldbidx)

			if mapEvacuated(oldb) {
				break
			}

			if oldbidx == it.bidx {
				it.b = oldb
				break
			}

			// oldbucket origin for current bucket has not been evacuated but we have already
			// iterated over it so we should just skip it
			it.b = nil
			it.bidx++
		}

		if it.b == nil {
			return false
		}
		it.bidx++
	}

	if it.b.Addr <= 0 {
		return false
	}

1383 1384
	it.b.mem = cacheMemory(it.b.mem, it.b.Addr, int(it.b.RealType.Size()))

A
aarzilli 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	it.tophashes = nil
	it.keys = nil
	it.values = nil
	it.overflow = nil

	for _, f := range it.b.DwarfType.(*dwarf.StructType).Field {
		field, err := it.b.toField(f)
		if err != nil {
			it.v.Unreadable = err
			return false
		}
		if field.Unreadable != nil {
			it.v.Unreadable = field.Unreadable
			return false
		}

		switch f.Name {
		case "tophash":
			it.tophashes = field
		case "keys":
			it.keys = field
		case "values":
			it.values = field
		case "overflow":
			it.overflow = field.maybeDereference()
		}
	}

	// sanity checks
	if it.tophashes == nil || it.keys == nil || it.values == nil {
		it.v.Unreadable = fmt.Errorf("malformed map type")
		return false
	}

	if it.tophashes.Kind != reflect.Array || it.keys.Kind != reflect.Array || it.values.Kind != reflect.Array {
1420
		it.v.Unreadable = mapBucketContentsNotArrayErr
A
aarzilli 已提交
1421 1422 1423 1424
		return false
	}

	if it.tophashes.Len != it.keys.Len || it.tophashes.Len != it.values.Len {
1425 1426 1427 1428 1429 1430
		it.v.Unreadable = mapBucketContentsInconsistentLenErr
		return false
	}

	if it.overflow.Kind != reflect.Struct {
		it.v.Unreadable = mapBucketsNotStructErr
A
aarzilli 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
		return false
	}

	return true
}

func (it *mapIterator) next() bool {
	for {
		if it.b == nil || it.idx >= it.tophashes.Len {
			r := it.nextBucket()
			if !r {
				return false
			}
			it.idx = 0
		}
		tophash, _ := it.tophashes.sliceAccess(int(it.idx))
		h, err := tophash.asUint()
		if err != nil {
			it.v.Unreadable = fmt.Errorf("unreadable tophash: %v", err)
			return false
		}
		it.idx++
		if h != hashTophashEmpty {
			return true
		}
	}
}

func (it *mapIterator) key() *Variable {
	k, _ := it.keys.sliceAccess(int(it.idx - 1))
	return k
}

func (it *mapIterator) value() *Variable {
	v, _ := it.values.sliceAccess(int(it.idx - 1))
	return v
}

func mapEvacuated(b *Variable) bool {
	if b.Addr == 0 {
		return true
	}
	for _, f := range b.DwarfType.(*dwarf.StructType).Field {
		if f.Name != "tophash" {
			continue
		}
		tophashes, _ := b.toField(f)
		tophash0var, _ := tophashes.sliceAccess(0)
		tophash0, err := tophash0var.asUint()
		if err != nil {
			return true
		}
		return tophash0 > hashTophashEmpty && tophash0 < hashMinTopHash
	}
	return true
}

1488
func (v *Variable) loadInterface(recurseLevel int, loadData bool, cfg LoadConfig) {
1489 1490
	var _type, typestring, data *Variable
	var typ dwarf.Type
A
Alessandro Arzilli 已提交
1491
	var err error
1492 1493
	isnil := false

A
Alessandro Arzilli 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
	// An interface variable is implemented either by a runtime.iface
	// struct or a runtime.eface struct. The difference being that empty
	// interfaces (i.e. "interface {}") are represented by runtime.eface
	// and non-empty interfaces by runtime.iface.
	//
	// For both runtime.ifaces and runtime.efaces the data is stored in v.data
	//
	// The concrete type however is stored in v.tab._type for non-empty
	// interfaces and in v._type for empty interfaces.
	//
	// For nil empty interface variables _type will be nil, for nil
	// non-empty interface variables tab will be nil
	//
	// In either case the _type field is a pointer to a runtime._type struct.
	//
	// Before go1.7 _type used to have a field named 'string' containing
	// the name of the type. Since go1.7 the field has been replaced by a
	// str field that contains an offset in the module data, the concrete
	// type must be calculated using the str address along with the value
	// of v.tab._type (v._type for empty interfaces).
	//
	// The following code works for both runtime.iface and runtime.eface
	// and sets the go17 flag when the 'string' field can not be found
	// but the str field was found

	go17 := false

1521 1522
	v.mem = cacheMemory(v.mem, v.Addr, int(v.RealType.Size()))

1523 1524 1525
	ityp := resolveTypedef(&v.RealType.(*dwarf.InterfaceType).TypedefType).(*dwarf.StructType)

	for _, f := range ityp.Field {
1526 1527 1528
		switch f.Name {
		case "tab": // for runtime.iface
			tab, _ := v.toField(f)
A
Alessandro Arzilli 已提交
1529 1530 1531 1532 1533
			tab = tab.maybeDereference()
			isnil = tab.Addr == 0
			if !isnil {
				_type, err = tab.structMember("_type")
				if err != nil {
1534 1535 1536 1537
					v.Unreadable = fmt.Errorf("invalid interface type: %v", err)
					return
				}
				typestring, err = _type.structMember("_string")
A
Alessandro Arzilli 已提交
1538 1539 1540 1541
				if err == nil {
					typestring = typestring.maybeDereference()
				} else {
					go17 = true
1542 1543 1544
				}
			}
		case "_type": // for runtime.eface
A
Alessandro Arzilli 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553
			_type, _ = v.toField(f)
			_type = _type.maybeDereference()
			isnil = _type.Addr == 0
			if !isnil {
				typestring, err = _type.structMember("_string")
				if err == nil {
					typestring = typestring.maybeDereference()
				} else {
					go17 = true
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
				}
			}
		case "data":
			data, _ = v.toField(f)
		}
	}

	if isnil {
		// interface to nil
		data = data.maybeDereference()
		v.Children = []Variable{*data}
A
aarzilli 已提交
1565
		if loadData {
1566
			v.Children[0].loadValueInternal(recurseLevel, cfg)
A
aarzilli 已提交
1567
		}
1568 1569 1570
		return
	}

A
Alessandro Arzilli 已提交
1571
	if data == nil {
1572 1573 1574
		v.Unreadable = fmt.Errorf("invalid interface type")
		return
	}
A
Alessandro Arzilli 已提交
1575

1576 1577
	var kind int64

A
Alessandro Arzilli 已提交
1578 1579 1580
	if go17 {
		// No 'string' field use 'str' and 'runtime.firstmoduledata' to
		// find out what the concrete type is
1581
		_type = _type.maybeDereference()
A
Alessandro Arzilli 已提交
1582

1583 1584
		var typename string
		typename, kind, err = nameOfRuntimeType(_type)
A
Alessandro Arzilli 已提交
1585 1586 1587 1588 1589
		if err != nil {
			v.Unreadable = fmt.Errorf("invalid interface type: %v", err)
			return
		}

1590
		typ, err = v.bi.findType(typename)
A
Alessandro Arzilli 已提交
1591
		if err != nil {
1592
			v.Unreadable = fmt.Errorf("interface type %q not found for %#x: %v", typename, data.Addr, err)
1593
			return
A
Alessandro Arzilli 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
		}
	} else {
		if typestring == nil || typestring.Addr == 0 || typestring.Kind != reflect.String {
			v.Unreadable = fmt.Errorf("invalid interface type")
			return
		}
		typestring.loadValue(LoadConfig{false, 0, 512, 0, 0})
		if typestring.Unreadable != nil {
			v.Unreadable = fmt.Errorf("invalid interface type: %v", typestring.Unreadable)
			return
		}

1606
		typename := constant.StringVal(typestring.Value)
1607

1608 1609 1610 1611 1612
		t, err := parser.ParseExpr(typename)
		if err != nil {
			v.Unreadable = fmt.Errorf("invalid interface type, unparsable data type: %v", err)
			return
		}
1613

1614
		typ, err = v.bi.findTypeExpr(t)
1615 1616 1617 1618
		if err != nil {
			v.Unreadable = fmt.Errorf("interface type %q not found for %#x: %v", typename, data.Addr, err)
			return
		}
1619 1620
	}

1621 1622 1623
	if kind&kindDirectIface == 0 {
		realtyp := resolveTypedef(typ)
		if _, isptr := realtyp.(*dwarf.PtrType); !isptr {
1624
			typ = pointerTo(typ, v.bi.Arch)
1625
		}
1626 1627
	}

1628
	data = data.newVariable("data", data.Addr, typ)
1629 1630

	v.Children = []Variable{*data}
1631
	if loadData && recurseLevel <= cfg.MaxVariableRecurse {
1632 1633 1634
		v.Children[0].loadValueInternal(recurseLevel, cfg)
	} else {
		v.Children[0].OnlyAddr = true
1635 1636 1637 1638
	}
	return
}

E
epipho 已提交
1639
// Fetches all variables of a specific type in the current function scope
1640
func (scope *EvalScope) variablesByTag(tag dwarf.Tag, cfg LoadConfig) ([]*Variable, error) {
1641
	reader := scope.DwarfReader()
1642
	off, err := scope.BinInfo.findFunctionDebugInfo(scope.PC)
1643
	if err != nil {
E
epipho 已提交
1644 1645
		return nil, err
	}
A
Alessandro Arzilli 已提交
1646 1647
	reader.Seek(off)
	reader.Next()
E
epipho 已提交
1648

D
Derek Parker 已提交
1649
	var vars []*Variable
1650
	for entry, err := reader.NextScopeVariable(); entry != nil; entry, err = reader.NextScopeVariable() {
E
epipho 已提交
1651 1652 1653
		if err != nil {
			return nil, err
		}
A
Alessandro Arzilli 已提交
1654 1655 1656
		if entry.Tag == 0 {
			break
		}
E
epipho 已提交
1657 1658

		if entry.Tag == tag {
1659
			val, err := scope.extractVariableFromEntry(entry, cfg)
E
epipho 已提交
1660
			if err != nil {
E
epipho 已提交
1661 1662
				// skip variables that we can't parse yet
				continue
E
epipho 已提交
1663 1664 1665 1666 1667
			}

			vars = append(vars, val)
		}
	}
A
Alessandro Arzilli 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
	if len(vars) <= 0 {
		return vars, nil
	}

	// prefetch the whole chunk of memory relative to these variables

	minaddr := vars[0].Addr
	var maxaddr uintptr
	var size int64

	for _, v := range vars {
		if v.Addr < minaddr {
			minaddr = v.Addr
		}

		size += v.DwarfType.Size()

		if end := v.Addr + uintptr(v.DwarfType.Size()); end > maxaddr {
			maxaddr = end
		}
	}

	// check that we aren't trying to cache too much memory: we shouldn't
	// exceed the real size of the variables by more than the number of
	// variables times the size of an architecture pointer (to allow for memory
	// alignment).
	if int64(maxaddr-minaddr)-size <= int64(len(vars))*int64(scope.PtrSize()) {
		mem := cacheMemory(vars[0].mem, minaddr, int(maxaddr-minaddr))

		for _, v := range vars {
			v.mem = mem
		}
	}

	for _, v := range vars {
		v.loadValue(cfg)
	}
E
epipho 已提交
1705 1706 1707

	return vars, nil
}