github.com/undoio/delve@v1.9.0/pkg/proc/eval.go (about)

     1  package proc
     2  
     3  import (
     4  	"bytes"
     5  	"debug/dwarf"
     6  	"errors"
     7  	"fmt"
     8  	"go/ast"
     9  	"go/constant"
    10  	"go/parser"
    11  	"go/printer"
    12  	"go/scanner"
    13  	"go/token"
    14  	"reflect"
    15  	"sort"
    16  	"strconv"
    17  	"strings"
    18  
    19  	"github.com/undoio/delve/pkg/dwarf/godwarf"
    20  	"github.com/undoio/delve/pkg/dwarf/op"
    21  	"github.com/undoio/delve/pkg/dwarf/reader"
    22  	"github.com/undoio/delve/pkg/goversion"
    23  	"github.com/undoio/delve/pkg/logflags"
    24  )
    25  
    26  var errOperationOnSpecialFloat = errors.New("operations on non-finite floats not implemented")
    27  
    28  const goDictionaryName = ".dict"
    29  
    30  // EvalScope is the scope for variable evaluation. Contains the thread,
    31  // current location (PC), and canonical frame address.
    32  type EvalScope struct {
    33  	Location
    34  	Regs    op.DwarfRegisters
    35  	Mem     MemoryReadWriter // Target's memory
    36  	g       *G
    37  	BinInfo *BinaryInfo
    38  	target  *Target
    39  
    40  	frameOffset int64
    41  
    42  	// When the following pointer is not nil this EvalScope was created
    43  	// by CallFunction and the expression evaluation is executing on a
    44  	// different goroutine from the debugger's main goroutine.
    45  	// Under this circumstance the expression evaluator can make function
    46  	// calls by setting up the runtime.debugCallV1 call and then writing a
    47  	// value to the continueRequest channel.
    48  	// When a value is written to continueRequest the debugger's main goroutine
    49  	// will call Continue, when the runtime in the target process sends us a
    50  	// request in the function call protocol the debugger's main goroutine will
    51  	// write a value to the continueCompleted channel.
    52  	// The goroutine executing the expression evaluation shall signal that the
    53  	// evaluation is complete by closing the continueRequest channel.
    54  	callCtx *callContext
    55  
    56  	dictAddr uint64 // dictionary address for instantiated generic functions
    57  }
    58  
    59  type localsFlags uint8
    60  
    61  const (
    62  	// If localsTrustArgOrder is set function arguments that don't have an
    63  	// address will have one assigned by looking at their position in the argument
    64  	// list.
    65  	localsTrustArgOrder localsFlags = 1 << iota
    66  
    67  	// If localsNoDeclLineCheck the declaration line isn't checked at
    68  	// all to determine if the variable is in scope.
    69  	localsNoDeclLineCheck
    70  )
    71  
    72  // ConvertEvalScope returns a new EvalScope in the context of the
    73  // specified goroutine ID and stack frame.
    74  // If deferCall is > 0 the eval scope will be relative to the specified deferred call.
    75  func ConvertEvalScope(dbp *Target, gid, frame, deferCall int) (*EvalScope, error) {
    76  	if _, err := dbp.Valid(); err != nil {
    77  		return nil, err
    78  	}
    79  	ct := dbp.CurrentThread()
    80  	g, err := FindGoroutine(dbp, gid)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	var opts StacktraceOptions
    86  	if deferCall > 0 {
    87  		opts = StacktraceReadDefers
    88  	}
    89  
    90  	var locs []Stackframe
    91  	if g != nil {
    92  		locs, err = g.Stacktrace(frame+1, opts)
    93  	} else {
    94  		locs, err = ThreadStacktrace(ct, frame+1)
    95  	}
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	if frame >= len(locs) {
   101  		return nil, fmt.Errorf("Frame %d does not exist in goroutine %d", frame, gid)
   102  	}
   103  
   104  	if deferCall > 0 {
   105  		if deferCall-1 >= len(locs[frame].Defers) {
   106  			return nil, fmt.Errorf("Frame %d only has %d deferred calls", frame, len(locs[frame].Defers))
   107  		}
   108  
   109  		d := locs[frame].Defers[deferCall-1]
   110  		if d.Unreadable != nil {
   111  			return nil, d.Unreadable
   112  		}
   113  
   114  		return d.EvalScope(dbp, ct)
   115  	}
   116  
   117  	return FrameToScope(dbp, dbp.Memory(), g, locs[frame:]...), nil
   118  }
   119  
   120  // FrameToScope returns a new EvalScope for frames[0].
   121  // If frames has at least two elements all memory between
   122  // frames[0].Regs.SP() and frames[1].Regs.CFA will be cached.
   123  // Otherwise all memory between frames[0].Regs.SP() and frames[0].Regs.CFA
   124  // will be cached.
   125  func FrameToScope(t *Target, thread MemoryReadWriter, g *G, frames ...Stackframe) *EvalScope {
   126  	// Creates a cacheMem that will preload the entire stack frame the first
   127  	// time any local variable is read.
   128  	// Remember that the stack grows downward in memory.
   129  	minaddr := frames[0].Regs.SP()
   130  	var maxaddr uint64
   131  	if len(frames) > 1 && frames[0].SystemStack == frames[1].SystemStack {
   132  		maxaddr = uint64(frames[1].Regs.CFA)
   133  	} else {
   134  		maxaddr = uint64(frames[0].Regs.CFA)
   135  	}
   136  	if maxaddr > minaddr && maxaddr-minaddr < maxFramePrefetchSize {
   137  		thread = cacheMemory(thread, minaddr, int(maxaddr-minaddr))
   138  	}
   139  
   140  	s := &EvalScope{Location: frames[0].Call, Regs: frames[0].Regs, Mem: thread, g: g, BinInfo: t.BinInfo(), target: t, frameOffset: frames[0].FrameOffset()}
   141  	s.PC = frames[0].lastpc
   142  	return s
   143  }
   144  
   145  // ThreadScope returns an EvalScope for the given thread.
   146  func ThreadScope(t *Target, thread Thread) (*EvalScope, error) {
   147  	locations, err := ThreadStacktrace(thread, 1)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	if len(locations) < 1 {
   152  		return nil, errors.New("could not decode first frame")
   153  	}
   154  	return FrameToScope(t, thread.ProcessMemory(), nil, locations...), nil
   155  }
   156  
   157  // GoroutineScope returns an EvalScope for the goroutine running on the given thread.
   158  func GoroutineScope(t *Target, thread Thread) (*EvalScope, error) {
   159  	locations, err := ThreadStacktrace(thread, 1)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  	if len(locations) < 1 {
   164  		return nil, errors.New("could not decode first frame")
   165  	}
   166  	g, err := GetG(thread)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	return FrameToScope(t, thread.ProcessMemory(), g, locations...), nil
   171  }
   172  
   173  // EvalExpression returns the value of the given expression.
   174  func (scope *EvalScope) EvalExpression(expr string, cfg LoadConfig) (*Variable, error) {
   175  	if scope.callCtx != nil {
   176  		// makes sure that the other goroutine won't wait forever if we make a mistake
   177  		defer close(scope.callCtx.continueRequest)
   178  	}
   179  	t, err := parser.ParseExpr(expr)
   180  	if eqOff, isAs := isAssignment(err); scope.callCtx != nil && isAs {
   181  		lexpr := expr[:eqOff]
   182  		rexpr := expr[eqOff+1:]
   183  		err := scope.SetVariable(lexpr, rexpr)
   184  		scope.callCtx.doReturn(nil, err)
   185  		return nil, err
   186  	}
   187  	if err != nil {
   188  		scope.callCtx.doReturn(nil, err)
   189  		return nil, err
   190  	}
   191  
   192  	ev, err := scope.evalToplevelTypeCast(t, cfg)
   193  	if ev == nil && err == nil {
   194  		ev, err = scope.evalAST(t)
   195  	}
   196  	if err != nil {
   197  		scope.callCtx.doReturn(nil, err)
   198  		return nil, err
   199  	}
   200  	ev.loadValue(cfg)
   201  	if ev.Name == "" {
   202  		ev.Name = expr
   203  	}
   204  	scope.callCtx.doReturn(ev, nil)
   205  	return ev, nil
   206  }
   207  
   208  func isAssignment(err error) (int, bool) {
   209  	el, isScannerErr := err.(scanner.ErrorList)
   210  	if isScannerErr && el[0].Msg == "expected '==', found '='" {
   211  		return el[0].Pos.Offset, true
   212  	}
   213  	return 0, false
   214  }
   215  
   216  // Locals returns all variables in 'scope'.
   217  func (scope *EvalScope) Locals(flags localsFlags) ([]*Variable, error) {
   218  	if scope.Fn == nil {
   219  		return nil, errors.New("unable to find function context")
   220  	}
   221  
   222  	trustArgOrder := (flags&localsTrustArgOrder != 0) && scope.BinInfo.Producer() != "" && goversion.ProducerAfterOrEqual(scope.BinInfo.Producer(), 1, 12) && scope.Fn != nil && (scope.PC == scope.Fn.Entry)
   223  
   224  	dwarfTree, err := scope.image().getDwarfTree(scope.Fn.offset)
   225  	if err != nil {
   226  		return nil, err
   227  	}
   228  
   229  	variablesFlags := reader.VariablesOnlyVisible
   230  	if flags&localsNoDeclLineCheck != 0 {
   231  		variablesFlags = reader.VariablesNoDeclLineCheck
   232  	}
   233  	if scope.BinInfo.Producer() != "" && goversion.ProducerAfterOrEqual(scope.BinInfo.Producer(), 1, 15) {
   234  		variablesFlags |= reader.VariablesTrustDeclLine
   235  	}
   236  
   237  	varEntries := reader.Variables(dwarfTree, scope.PC, scope.Line, variablesFlags)
   238  
   239  	// look for dictionary entry
   240  	if scope.dictAddr == 0 {
   241  		for _, entry := range varEntries {
   242  			name, _ := entry.Val(dwarf.AttrName).(string)
   243  			if name == goDictionaryName {
   244  				dictVar, err := extractVarInfoFromEntry(scope.target, scope.BinInfo, scope.image(), scope.Regs, scope.Mem, entry.Tree, 0)
   245  				if err != nil {
   246  					logflags.DebuggerLogger().Errorf("could not load %s variable: %v", name, err)
   247  				} else if dictVar.Unreadable != nil {
   248  					logflags.DebuggerLogger().Errorf("could not load %s variable: %v", name, dictVar.Unreadable)
   249  				} else {
   250  					scope.dictAddr, err = readUintRaw(dictVar.mem, dictVar.Addr, int64(scope.BinInfo.Arch.PtrSize()))
   251  					if err != nil {
   252  						logflags.DebuggerLogger().Errorf("could not load %s variable: %v", name, err)
   253  					}
   254  				}
   255  				break
   256  			}
   257  		}
   258  	}
   259  
   260  	vars := make([]*Variable, 0, len(varEntries))
   261  	depths := make([]int, 0, len(varEntries))
   262  	for _, entry := range varEntries {
   263  		if name, _ := entry.Val(dwarf.AttrName).(string); name == goDictionaryName {
   264  			continue
   265  		}
   266  		val, err := extractVarInfoFromEntry(scope.target, scope.BinInfo, scope.image(), scope.Regs, scope.Mem, entry.Tree, scope.dictAddr)
   267  		if err != nil {
   268  			// skip variables that we can't parse yet
   269  			continue
   270  		}
   271  		if trustArgOrder && ((val.Unreadable != nil && val.Addr == 0) || val.Flags&VariableFakeAddress != 0) && entry.Tag == dwarf.TagFormalParameter {
   272  			addr := afterLastArgAddr(vars)
   273  			if addr == 0 {
   274  				addr = uint64(scope.Regs.CFA)
   275  			}
   276  			addr = uint64(alignAddr(int64(addr), val.DwarfType.Align()))
   277  			val = newVariable(val.Name, addr, val.DwarfType, scope.BinInfo, scope.Mem)
   278  		}
   279  		vars = append(vars, val)
   280  		depth := entry.Depth
   281  		if entry.Tag == dwarf.TagFormalParameter {
   282  			if depth <= 1 {
   283  				depth = 0
   284  			}
   285  			isret, _ := entry.Val(dwarf.AttrVarParam).(bool)
   286  			if isret {
   287  				val.Flags |= VariableReturnArgument
   288  			} else {
   289  				val.Flags |= VariableArgument
   290  			}
   291  		}
   292  		depths = append(depths, depth)
   293  	}
   294  
   295  	if len(vars) <= 0 {
   296  		return vars, nil
   297  	}
   298  
   299  	sort.Stable(&variablesByDepthAndDeclLine{vars, depths})
   300  
   301  	lvn := map[string]*Variable{} // lvn[n] is the last variable we saw named n
   302  
   303  	for i, v := range vars {
   304  		if name := v.Name; len(name) > 1 && name[0] == '&' {
   305  			locationExpr := v.LocationExpr
   306  			declLine := v.DeclLine
   307  			v = v.maybeDereference()
   308  			if v.Addr == 0 && v.Unreadable == nil {
   309  				v.Unreadable = fmt.Errorf("no address for escaped variable")
   310  			}
   311  			v.Name = name[1:]
   312  			v.Flags |= VariableEscaped
   313  			// See https://github.com/go-delve/delve/issues/2049 for details
   314  			if locationExpr != nil {
   315  				locationExpr.isEscaped = true
   316  				v.LocationExpr = locationExpr
   317  			}
   318  			v.DeclLine = declLine
   319  			vars[i] = v
   320  		}
   321  		if otherv := lvn[v.Name]; otherv != nil {
   322  			otherv.Flags |= VariableShadowed
   323  		}
   324  		lvn[v.Name] = v
   325  	}
   326  
   327  	return vars, nil
   328  }
   329  
   330  func afterLastArgAddr(vars []*Variable) uint64 {
   331  	for i := len(vars) - 1; i >= 0; i-- {
   332  		v := vars[i]
   333  		if (v.Flags&VariableArgument != 0) || (v.Flags&VariableReturnArgument != 0) {
   334  			return v.Addr + uint64(v.DwarfType.Size())
   335  		}
   336  	}
   337  	return 0
   338  }
   339  
   340  // setValue writes the value of srcv to dstv.
   341  //   - If srcv is a numerical literal constant and srcv is of a compatible type
   342  //     the necessary type conversion is performed.
   343  //   - If srcv is nil and dstv is of a nil'able type then dstv is nilled.
   344  //   - If srcv is the empty string and dstv is a string then dstv is set to the
   345  //     empty string.
   346  //   - If dstv is an "interface {}" and srcv is either an interface (possibly
   347  //     non-empty) or a pointer shaped type (map, channel, pointer or struct
   348  //     containing a single pointer field) the type conversion to "interface {}"
   349  //     is performed.
   350  //   - If srcv and dstv have the same type and are both addressable then the
   351  //     contents of srcv are copied byte-by-byte into dstv
   352  func (scope *EvalScope) setValue(dstv, srcv *Variable, srcExpr string) error {
   353  	srcv.loadValue(loadSingleValue)
   354  
   355  	typerr := srcv.isType(dstv.RealType, dstv.Kind)
   356  	if _, isTypeConvErr := typerr.(*typeConvErr); isTypeConvErr {
   357  		// attempt iface -> eface and ptr-shaped -> eface conversions.
   358  		return convertToEface(srcv, dstv)
   359  	}
   360  	if typerr != nil {
   361  		return typerr
   362  	}
   363  
   364  	if srcv.Unreadable != nil {
   365  		//lint:ignore ST1005 backwards compatibility
   366  		return fmt.Errorf("Expression \"%s\" is unreadable: %v", srcExpr, srcv.Unreadable)
   367  	}
   368  
   369  	// Numerical types
   370  	switch dstv.Kind {
   371  	case reflect.Float32, reflect.Float64:
   372  		f, _ := constant.Float64Val(srcv.Value)
   373  		return dstv.writeFloatRaw(f, dstv.RealType.Size())
   374  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   375  		n, _ := constant.Int64Val(srcv.Value)
   376  		return dstv.writeUint(uint64(n), dstv.RealType.Size())
   377  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   378  		n, _ := constant.Uint64Val(srcv.Value)
   379  		return dstv.writeUint(n, dstv.RealType.Size())
   380  	case reflect.Bool:
   381  		return dstv.writeBool(constant.BoolVal(srcv.Value))
   382  	case reflect.Complex64, reflect.Complex128:
   383  		real, _ := constant.Float64Val(constant.Real(srcv.Value))
   384  		imag, _ := constant.Float64Val(constant.Imag(srcv.Value))
   385  		return dstv.writeComplex(real, imag, dstv.RealType.Size())
   386  	case reflect.Func:
   387  		if dstv.RealType.Size() == 0 {
   388  			if dstv.Name != "" {
   389  				return fmt.Errorf("can not assign to %s", dstv.Name)
   390  			}
   391  			return errors.New("can not assign to function expression")
   392  		}
   393  	}
   394  
   395  	// nilling nillable variables
   396  	if srcv == nilVariable {
   397  		return dstv.writeZero()
   398  	}
   399  
   400  	if srcv.Kind == reflect.String {
   401  		if err := allocString(scope, srcv); err != nil {
   402  			return err
   403  		}
   404  		return dstv.writeString(uint64(srcv.Len), uint64(srcv.Base))
   405  	}
   406  
   407  	// slice assignment (this is not handled by the writeCopy below so that
   408  	// results of a reslice operation can be used here).
   409  	if srcv.Kind == reflect.Slice {
   410  		return dstv.writeSlice(srcv.Len, srcv.Cap, srcv.Base)
   411  	}
   412  
   413  	// allow any integer to be converted to any pointer
   414  	if t, isptr := dstv.RealType.(*godwarf.PtrType); isptr {
   415  		return dstv.writeUint(uint64(srcv.Children[0].Addr), int64(t.ByteSize))
   416  	}
   417  
   418  	// byte-by-byte copying for everything else, but the source must be addressable
   419  	if srcv.Addr != 0 {
   420  		return dstv.writeCopy(srcv)
   421  	}
   422  
   423  	return fmt.Errorf("can not set variables of type %s (not implemented)", dstv.Kind.String())
   424  }
   425  
   426  // SetVariable sets the value of the named variable
   427  func (scope *EvalScope) SetVariable(name, value string) error {
   428  	t, err := parser.ParseExpr(name)
   429  	if err != nil {
   430  		return err
   431  	}
   432  
   433  	xv, err := scope.evalAST(t)
   434  	if err != nil {
   435  		return err
   436  	}
   437  
   438  	if xv.Addr == 0 {
   439  		//lint:ignore ST1005 backwards compatibility
   440  		return fmt.Errorf("Can not assign to \"%s\"", name)
   441  	}
   442  
   443  	if xv.Unreadable != nil {
   444  		//lint:ignore ST1005 backwards compatibility
   445  		return fmt.Errorf("Expression \"%s\" is unreadable: %v", name, xv.Unreadable)
   446  	}
   447  
   448  	t, err = parser.ParseExpr(value)
   449  	if err != nil {
   450  		return err
   451  	}
   452  
   453  	yv, err := scope.evalAST(t)
   454  	if err != nil {
   455  		return err
   456  	}
   457  
   458  	return scope.setValue(xv, yv, value)
   459  }
   460  
   461  // LocalVariables returns all local variables from the current function scope.
   462  func (scope *EvalScope) LocalVariables(cfg LoadConfig) ([]*Variable, error) {
   463  	vars, err := scope.Locals(0)
   464  	if err != nil {
   465  		return nil, err
   466  	}
   467  	vars = filterVariables(vars, func(v *Variable) bool {
   468  		return (v.Flags & (VariableArgument | VariableReturnArgument)) == 0
   469  	})
   470  	cfg.MaxMapBuckets = maxMapBucketsFactor * cfg.MaxArrayValues
   471  	loadValues(vars, cfg)
   472  	return vars, nil
   473  }
   474  
   475  // FunctionArguments returns the name, value, and type of all current function arguments.
   476  func (scope *EvalScope) FunctionArguments(cfg LoadConfig) ([]*Variable, error) {
   477  	vars, err := scope.Locals(0)
   478  	if err != nil {
   479  		return nil, err
   480  	}
   481  	vars = filterVariables(vars, func(v *Variable) bool {
   482  		return (v.Flags & (VariableArgument | VariableReturnArgument)) != 0
   483  	})
   484  	cfg.MaxMapBuckets = maxMapBucketsFactor * cfg.MaxArrayValues
   485  	loadValues(vars, cfg)
   486  	return vars, nil
   487  }
   488  
   489  func filterVariables(vars []*Variable, pred func(v *Variable) bool) []*Variable {
   490  	r := make([]*Variable, 0, len(vars))
   491  	for i := range vars {
   492  		if pred(vars[i]) {
   493  			r = append(r, vars[i])
   494  		}
   495  	}
   496  	return r
   497  }
   498  
   499  func regsReplaceStaticBase(regs op.DwarfRegisters, image *Image) op.DwarfRegisters {
   500  	regs.StaticBase = image.StaticBase
   501  	return regs
   502  }
   503  
   504  // PackageVariables returns the name, value, and type of all package variables in the application.
   505  func (scope *EvalScope) PackageVariables(cfg LoadConfig) ([]*Variable, error) {
   506  	pkgvars := make([]packageVar, len(scope.BinInfo.packageVars))
   507  	copy(pkgvars, scope.BinInfo.packageVars)
   508  	sort.Slice(pkgvars, func(i, j int) bool {
   509  		if pkgvars[i].cu.image.addr == pkgvars[j].cu.image.addr {
   510  			return pkgvars[i].offset < pkgvars[j].offset
   511  		}
   512  		return pkgvars[i].cu.image.addr < pkgvars[j].cu.image.addr
   513  	})
   514  	vars := make([]*Variable, 0, len(scope.BinInfo.packageVars))
   515  	for _, pkgvar := range pkgvars {
   516  		reader := pkgvar.cu.image.dwarfReader
   517  		reader.Seek(pkgvar.offset)
   518  		entry, err := reader.Next()
   519  		if err != nil {
   520  			return nil, err
   521  		}
   522  
   523  		// Ignore errors trying to extract values
   524  		val, err := extractVarInfoFromEntry(scope.target, scope.BinInfo, pkgvar.cu.image, regsReplaceStaticBase(scope.Regs, pkgvar.cu.image), scope.Mem, godwarf.EntryToTree(entry), 0)
   525  		if val != nil && val.Kind == reflect.Invalid {
   526  			continue
   527  		}
   528  		if err != nil {
   529  			continue
   530  		}
   531  		val.loadValue(cfg)
   532  		vars = append(vars, val)
   533  	}
   534  
   535  	return vars, nil
   536  }
   537  
   538  func (scope *EvalScope) findGlobal(pkgName, varName string) (*Variable, error) {
   539  	for _, pkgPath := range scope.BinInfo.PackageMap[pkgName] {
   540  		v, err := scope.findGlobalInternal(pkgPath + "." + varName)
   541  		if err != nil || v != nil {
   542  			return v, err
   543  		}
   544  	}
   545  	v, err := scope.findGlobalInternal(pkgName + "." + varName)
   546  	if err != nil || v != nil {
   547  		return v, err
   548  	}
   549  	return nil, fmt.Errorf("could not find symbol value for %s.%s", pkgName, varName)
   550  }
   551  
   552  func (scope *EvalScope) findGlobalInternal(name string) (*Variable, error) {
   553  	for _, pkgvar := range scope.BinInfo.packageVars {
   554  		if pkgvar.name == name || strings.HasSuffix(pkgvar.name, "/"+name) {
   555  			reader := pkgvar.cu.image.dwarfReader
   556  			reader.Seek(pkgvar.offset)
   557  			entry, err := reader.Next()
   558  			if err != nil {
   559  				return nil, err
   560  			}
   561  			return extractVarInfoFromEntry(scope.target, scope.BinInfo, pkgvar.cu.image, regsReplaceStaticBase(scope.Regs, pkgvar.cu.image), scope.Mem, godwarf.EntryToTree(entry), 0)
   562  		}
   563  	}
   564  	for _, fn := range scope.BinInfo.Functions {
   565  		if fn.Name == name || strings.HasSuffix(fn.Name, "/"+name) {
   566  			//TODO(aarzilli): convert function entry into a function type?
   567  			r := newVariable(fn.Name, fn.Entry, &godwarf.FuncType{}, scope.BinInfo, scope.Mem)
   568  			r.Value = constant.MakeString(fn.Name)
   569  			r.Base = fn.Entry
   570  			r.loaded = true
   571  			if fn.Entry == 0 {
   572  				r.Unreadable = fmt.Errorf("function %s is inlined", fn.Name)
   573  			}
   574  			return r, nil
   575  		}
   576  	}
   577  	for dwref, ctyp := range scope.BinInfo.consts {
   578  		for _, cval := range ctyp.values {
   579  			if cval.fullName == name || strings.HasSuffix(cval.fullName, "/"+name) {
   580  				t, err := scope.BinInfo.Images[dwref.imageIndex].Type(dwref.offset)
   581  				if err != nil {
   582  					return nil, err
   583  				}
   584  				v := newVariable(name, 0x0, t, scope.BinInfo, scope.Mem)
   585  				switch v.Kind {
   586  				case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   587  					v.Value = constant.MakeInt64(cval.value)
   588  				case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   589  					v.Value = constant.MakeUint64(uint64(cval.value))
   590  				default:
   591  					return nil, fmt.Errorf("unsupported constant kind %v", v.Kind)
   592  				}
   593  				v.Flags |= VariableConstant
   594  				v.loaded = true
   595  				return v, nil
   596  			}
   597  		}
   598  	}
   599  	return nil, nil
   600  }
   601  
   602  // image returns the image containing the current function.
   603  func (scope *EvalScope) image() *Image {
   604  	return scope.BinInfo.funcToImage(scope.Fn)
   605  }
   606  
   607  // DwarfReader returns the DwarfReader containing the
   608  // Dwarf information for the target process.
   609  func (scope *EvalScope) DwarfReader() *reader.Reader {
   610  	return scope.image().DwarfReader()
   611  }
   612  
   613  // PtrSize returns the size of a pointer.
   614  func (scope *EvalScope) PtrSize() int {
   615  	return scope.BinInfo.Arch.PtrSize()
   616  }
   617  
   618  // evalToplevelTypeCast implements certain type casts that we only support
   619  // at the outermost levels of an expression.
   620  func (scope *EvalScope) evalToplevelTypeCast(t ast.Expr, cfg LoadConfig) (*Variable, error) {
   621  	call, _ := t.(*ast.CallExpr)
   622  	if call == nil || len(call.Args) != 1 {
   623  		return nil, nil
   624  	}
   625  	targetTypeStr := exprToString(removeParen(call.Fun))
   626  	var targetType godwarf.Type
   627  	switch targetTypeStr {
   628  	case "[]byte", "[]uint8":
   629  		targetType = fakeSliceType(&godwarf.IntType{BasicType: godwarf.BasicType{CommonType: godwarf.CommonType{ByteSize: 1, Name: "uint8"}, BitSize: 8, BitOffset: 0}})
   630  	case "[]int32", "[]rune":
   631  		targetType = fakeSliceType(&godwarf.IntType{BasicType: godwarf.BasicType{CommonType: godwarf.CommonType{ByteSize: 1, Name: "int32"}, BitSize: 32, BitOffset: 0}})
   632  	case "string":
   633  		var err error
   634  		targetType, err = scope.BinInfo.findType("string")
   635  		if err != nil {
   636  			return nil, err
   637  		}
   638  	default:
   639  		return nil, nil
   640  	}
   641  
   642  	argv, err := scope.evalToplevelTypeCast(call.Args[0], cfg)
   643  	if argv == nil && err == nil {
   644  		argv, err = scope.evalAST(call.Args[0])
   645  	}
   646  	if err != nil {
   647  		return nil, err
   648  	}
   649  	argv.loadValue(cfg)
   650  	if argv.Unreadable != nil {
   651  		return nil, argv.Unreadable
   652  	}
   653  
   654  	v := newVariable("", 0, targetType, scope.BinInfo, scope.Mem)
   655  	v.loaded = true
   656  
   657  	converr := fmt.Errorf("can not convert %q to %s", exprToString(call.Args[0]), targetTypeStr)
   658  
   659  	switch targetTypeStr {
   660  	case "[]byte", "[]uint8":
   661  		if argv.Kind != reflect.String {
   662  			return nil, converr
   663  		}
   664  		for i, ch := range []byte(constant.StringVal(argv.Value)) {
   665  			e := newVariable("", argv.Addr+uint64(i), targetType.(*godwarf.SliceType).ElemType, scope.BinInfo, argv.mem)
   666  			e.loaded = true
   667  			e.Value = constant.MakeInt64(int64(ch))
   668  			v.Children = append(v.Children, *e)
   669  		}
   670  		v.Len = int64(len(v.Children))
   671  		v.Cap = v.Len
   672  		return v, nil
   673  
   674  	case "[]int32", "[]rune":
   675  		if argv.Kind != reflect.String {
   676  			return nil, converr
   677  		}
   678  		for i, ch := range constant.StringVal(argv.Value) {
   679  			e := newVariable("", argv.Addr+uint64(i), targetType.(*godwarf.SliceType).ElemType, scope.BinInfo, argv.mem)
   680  			e.loaded = true
   681  			e.Value = constant.MakeInt64(int64(ch))
   682  			v.Children = append(v.Children, *e)
   683  		}
   684  		v.Len = int64(len(v.Children))
   685  		v.Cap = v.Len
   686  		return v, nil
   687  
   688  	case "string":
   689  		switch argv.Kind {
   690  		case reflect.String:
   691  			s := constant.StringVal(argv.Value)
   692  			v.Value = constant.MakeString(s)
   693  			v.Len = int64(len(s))
   694  			return v, nil
   695  		case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr:
   696  			b, _ := constant.Int64Val(argv.Value)
   697  			s := string(rune(b))
   698  			v.Value = constant.MakeString(s)
   699  			v.Len = int64(len(s))
   700  			return v, nil
   701  		case reflect.Slice, reflect.Array:
   702  			var elem godwarf.Type
   703  			if argv.Kind == reflect.Slice {
   704  				elem = argv.RealType.(*godwarf.SliceType).ElemType
   705  			} else {
   706  				elem = argv.RealType.(*godwarf.ArrayType).Type
   707  			}
   708  			switch elemType := elem.(type) {
   709  			case *godwarf.UintType:
   710  				if elemType.Name != "uint8" && elemType.Name != "byte" {
   711  					return nil, nil
   712  				}
   713  				bytes := make([]byte, len(argv.Children))
   714  				for i := range argv.Children {
   715  					n, _ := constant.Int64Val(argv.Children[i].Value)
   716  					bytes[i] = byte(n)
   717  				}
   718  				v.Value = constant.MakeString(string(bytes))
   719  
   720  			case *godwarf.IntType:
   721  				if elemType.Name != "int32" && elemType.Name != "rune" {
   722  					return nil, nil
   723  				}
   724  				runes := make([]rune, len(argv.Children))
   725  				for i := range argv.Children {
   726  					n, _ := constant.Int64Val(argv.Children[i].Value)
   727  					runes[i] = rune(n)
   728  				}
   729  				v.Value = constant.MakeString(string(runes))
   730  
   731  			default:
   732  				return nil, nil
   733  			}
   734  			v.Len = int64(len(constant.StringVal(v.Value)))
   735  			return v, nil
   736  
   737  		default:
   738  			return nil, nil
   739  		}
   740  	}
   741  
   742  	return nil, nil
   743  }
   744  
   745  func (scope *EvalScope) evalAST(t ast.Expr) (*Variable, error) {
   746  	switch node := t.(type) {
   747  	case *ast.CallExpr:
   748  		return scope.evalTypeCastOrFuncCall(node)
   749  
   750  	case *ast.Ident:
   751  		return scope.evalIdent(node)
   752  
   753  	case *ast.ParenExpr:
   754  		// otherwise just eval recursively
   755  		return scope.evalAST(node.X)
   756  
   757  	case *ast.SelectorExpr: // <expression>.<identifier>
   758  		// try to interpret the selector as a package variable
   759  		if maybePkg, ok := node.X.(*ast.Ident); ok {
   760  			if maybePkg.Name == "runtime" && node.Sel.Name == "curg" {
   761  				if scope.g == nil {
   762  					typ, err := scope.BinInfo.findType("runtime.g")
   763  					if err != nil {
   764  						return nil, fmt.Errorf("blah: %v", err)
   765  					}
   766  					gvar := newVariable("curg", fakeAddressUnresolv, typ, scope.BinInfo, scope.Mem)
   767  					gvar.loaded = true
   768  					gvar.Flags = VariableFakeAddress
   769  					gvar.Children = append(gvar.Children, *newConstant(constant.MakeInt64(0), scope.Mem))
   770  					gvar.Children[0].Name = "goid"
   771  					return gvar, nil
   772  				}
   773  				return scope.g.variable.clone(), nil
   774  			} else if maybePkg.Name == "runtime" && node.Sel.Name == "frameoff" {
   775  				return newConstant(constant.MakeInt64(scope.frameOffset), scope.Mem), nil
   776  			} else if v, err := scope.findGlobal(maybePkg.Name, node.Sel.Name); err == nil {
   777  				return v, nil
   778  			}
   779  		}
   780  		// try to accept "package/path".varname syntax for package variables
   781  		if maybePkg, ok := node.X.(*ast.BasicLit); ok && maybePkg.Kind == token.STRING {
   782  			pkgpath, err := strconv.Unquote(maybePkg.Value)
   783  			if err == nil {
   784  				if v, err := scope.findGlobal(pkgpath, node.Sel.Name); err == nil {
   785  					return v, nil
   786  				}
   787  			}
   788  		}
   789  		// if it's not a package variable then it must be a struct member access
   790  		return scope.evalStructSelector(node)
   791  
   792  	case *ast.TypeAssertExpr: // <expression>.(<type>)
   793  		return scope.evalTypeAssert(node)
   794  
   795  	case *ast.IndexExpr:
   796  		return scope.evalIndex(node)
   797  
   798  	case *ast.SliceExpr:
   799  		if node.Slice3 {
   800  			return nil, fmt.Errorf("3-index slice expressions not supported")
   801  		}
   802  		return scope.evalReslice(node)
   803  
   804  	case *ast.StarExpr:
   805  		// pointer dereferencing *<expression>
   806  		return scope.evalPointerDeref(node)
   807  
   808  	case *ast.UnaryExpr:
   809  		// The unary operators we support are +, - and & (note that unary * is parsed as ast.StarExpr)
   810  		switch node.Op {
   811  		case token.AND:
   812  			return scope.evalAddrOf(node)
   813  
   814  		default:
   815  			return scope.evalUnary(node)
   816  		}
   817  
   818  	case *ast.BinaryExpr:
   819  		return scope.evalBinary(node)
   820  
   821  	case *ast.BasicLit:
   822  		return newConstant(constant.MakeFromLiteral(node.Value, node.Kind, 0), scope.Mem), nil
   823  
   824  	default:
   825  		return nil, fmt.Errorf("expression %T not implemented", t)
   826  
   827  	}
   828  }
   829  
   830  func exprToString(t ast.Expr) string {
   831  	var buf bytes.Buffer
   832  	printer.Fprint(&buf, token.NewFileSet(), t)
   833  	return buf.String()
   834  }
   835  
   836  func removeParen(n ast.Expr) ast.Expr {
   837  	for {
   838  		p, ok := n.(*ast.ParenExpr)
   839  		if !ok {
   840  			break
   841  		}
   842  		n = p.X
   843  	}
   844  	return n
   845  }
   846  
   847  // evalTypeCastOrFuncCall evaluates a type cast or a function call
   848  func (scope *EvalScope) evalTypeCastOrFuncCall(node *ast.CallExpr) (*Variable, error) {
   849  	if len(node.Args) != 1 {
   850  		// Things that have more or less than one argument are always function calls.
   851  		return evalFunctionCall(scope, node)
   852  	}
   853  
   854  	ambiguous := func() (*Variable, error) {
   855  		// Ambiguous, could be a function call or a type cast, if node.Fun can be
   856  		// evaluated then try to treat it as a function call, otherwise try the
   857  		// type cast.
   858  		_, err0 := scope.evalAST(node.Fun)
   859  		if err0 == nil {
   860  			return evalFunctionCall(scope, node)
   861  		}
   862  		v, err := scope.evalTypeCast(node)
   863  		if err == reader.ErrTypeNotFound {
   864  			return nil, fmt.Errorf("could not evaluate function or type %s: %v", exprToString(node.Fun), err0)
   865  		}
   866  		return v, err
   867  	}
   868  
   869  	fnnode := removeParen(node.Fun)
   870  	if n, _ := fnnode.(*ast.StarExpr); n != nil {
   871  		fnnode = removeParen(n.X)
   872  	}
   873  
   874  	switch n := fnnode.(type) {
   875  	case *ast.BasicLit:
   876  		// It can only be a ("type string")(x) type cast
   877  		return scope.evalTypeCast(node)
   878  	case *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.ChanType:
   879  		return scope.evalTypeCast(node)
   880  	case *ast.SelectorExpr:
   881  		if _, isident := n.X.(*ast.Ident); isident {
   882  			return ambiguous()
   883  		}
   884  		return evalFunctionCall(scope, node)
   885  	case *ast.Ident:
   886  		if supportedBuiltins[n.Name] {
   887  			return evalFunctionCall(scope, node)
   888  		}
   889  		return ambiguous()
   890  	case *ast.IndexExpr:
   891  		// Ambiguous, could be a parametric type
   892  		switch n.X.(type) {
   893  		case *ast.Ident, *ast.SelectorExpr:
   894  			// Do the type-cast first since evaluating node.Fun could be expensive.
   895  			v, err := scope.evalTypeCast(node)
   896  			if err == nil || err != reader.ErrTypeNotFound {
   897  				return v, err
   898  			}
   899  			return evalFunctionCall(scope, node)
   900  		default:
   901  			return evalFunctionCall(scope, node)
   902  		}
   903  	case *astIndexListExpr:
   904  		return scope.evalTypeCast(node)
   905  	default:
   906  		// All other expressions must be function calls
   907  		return evalFunctionCall(scope, node)
   908  	}
   909  }
   910  
   911  // Eval type cast expressions
   912  func (scope *EvalScope) evalTypeCast(node *ast.CallExpr) (*Variable, error) {
   913  	argv, err := scope.evalAST(node.Args[0])
   914  	if err != nil {
   915  		return nil, err
   916  	}
   917  	argv.loadValue(loadSingleValue)
   918  	if argv.Unreadable != nil {
   919  		return nil, argv.Unreadable
   920  	}
   921  
   922  	fnnode := node.Fun
   923  
   924  	// remove all enclosing parenthesis from the type name
   925  	fnnode = removeParen(fnnode)
   926  
   927  	styp, err := scope.BinInfo.findTypeExpr(fnnode)
   928  	if err != nil {
   929  		return nil, err
   930  	}
   931  	typ := resolveTypedef(styp)
   932  
   933  	converr := fmt.Errorf("can not convert %q to %s", exprToString(node.Args[0]), typ.String())
   934  
   935  	v := newVariable("", 0, styp, scope.BinInfo, scope.Mem)
   936  	v.loaded = true
   937  
   938  	switch ttyp := typ.(type) {
   939  	case *godwarf.PtrType:
   940  		switch argv.Kind {
   941  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   942  			// ok
   943  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   944  			// ok
   945  		default:
   946  			return nil, converr
   947  		}
   948  
   949  		n, _ := constant.Int64Val(argv.Value)
   950  
   951  		mem := scope.Mem
   952  		if scope.target != nil {
   953  			if mem2 := scope.target.findFakeMemory(uint64(n)); mem2 != nil {
   954  				mem = mem2
   955  			}
   956  		}
   957  
   958  		v.Children = []Variable{*(newVariable("", uint64(n), ttyp.Type, scope.BinInfo, mem))}
   959  		v.Children[0].OnlyAddr = true
   960  		return v, nil
   961  
   962  	case *godwarf.UintType:
   963  		switch argv.Kind {
   964  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   965  			n, _ := constant.Int64Val(argv.Value)
   966  			v.Value = constant.MakeUint64(convertInt(uint64(n), false, ttyp.Size()))
   967  			return v, nil
   968  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   969  			n, _ := constant.Uint64Val(argv.Value)
   970  			v.Value = constant.MakeUint64(convertInt(n, false, ttyp.Size()))
   971  			return v, nil
   972  		case reflect.Float32, reflect.Float64:
   973  			x, _ := constant.Float64Val(argv.Value)
   974  			v.Value = constant.MakeUint64(uint64(x))
   975  			return v, nil
   976  		case reflect.Ptr:
   977  			v.Value = constant.MakeUint64(uint64(argv.Children[0].Addr))
   978  			return v, nil
   979  		}
   980  	case *godwarf.IntType:
   981  		switch argv.Kind {
   982  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   983  			n, _ := constant.Int64Val(argv.Value)
   984  			v.Value = constant.MakeInt64(int64(convertInt(uint64(n), true, ttyp.Size())))
   985  			return v, nil
   986  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   987  			n, _ := constant.Uint64Val(argv.Value)
   988  			v.Value = constant.MakeInt64(int64(convertInt(n, true, ttyp.Size())))
   989  			return v, nil
   990  		case reflect.Float32, reflect.Float64:
   991  			x, _ := constant.Float64Val(argv.Value)
   992  			v.Value = constant.MakeInt64(int64(x))
   993  			return v, nil
   994  		}
   995  	case *godwarf.FloatType:
   996  		switch argv.Kind {
   997  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   998  			fallthrough
   999  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  1000  			fallthrough
  1001  		case reflect.Float32, reflect.Float64:
  1002  			v.Value = argv.Value
  1003  			return v, nil
  1004  		}
  1005  	case *godwarf.ComplexType:
  1006  		switch argv.Kind {
  1007  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1008  			fallthrough
  1009  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  1010  			fallthrough
  1011  		case reflect.Float32, reflect.Float64:
  1012  			v.Value = argv.Value
  1013  			return v, nil
  1014  		}
  1015  	}
  1016  
  1017  	return nil, converr
  1018  }
  1019  
  1020  func convertInt(n uint64, signed bool, size int64) uint64 {
  1021  	bits := uint64(size) * 8
  1022  	mask := uint64((1 << bits) - 1)
  1023  	r := n & mask
  1024  	if signed && (r>>(bits-1)) != 0 {
  1025  		// sign extension
  1026  		r |= ^uint64(0) &^ mask
  1027  	}
  1028  	return r
  1029  }
  1030  
  1031  var supportedBuiltins = map[string]bool{"cap": true, "len": true, "complex": true, "imag": true, "real": true}
  1032  
  1033  func (scope *EvalScope) evalBuiltinCall(node *ast.CallExpr) (*Variable, error) {
  1034  	fnnode, ok := node.Fun.(*ast.Ident)
  1035  	if !ok {
  1036  		return nil, nil
  1037  	}
  1038  
  1039  	callBuiltinWithArgs := func(builtin func([]*Variable, []ast.Expr) (*Variable, error)) (*Variable, error) {
  1040  		args := make([]*Variable, len(node.Args))
  1041  
  1042  		for i := range node.Args {
  1043  			v, err := scope.evalAST(node.Args[i])
  1044  			if err != nil {
  1045  				return nil, err
  1046  			}
  1047  			args[i] = v
  1048  		}
  1049  
  1050  		return builtin(args, node.Args)
  1051  	}
  1052  
  1053  	switch fnnode.Name {
  1054  	case "cap":
  1055  		return callBuiltinWithArgs(capBuiltin)
  1056  	case "len":
  1057  		return callBuiltinWithArgs(lenBuiltin)
  1058  	case "complex":
  1059  		return callBuiltinWithArgs(complexBuiltin)
  1060  	case "imag":
  1061  		return callBuiltinWithArgs(imagBuiltin)
  1062  	case "real":
  1063  		return callBuiltinWithArgs(realBuiltin)
  1064  	}
  1065  
  1066  	return nil, nil
  1067  }
  1068  
  1069  func capBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
  1070  	if len(args) != 1 {
  1071  		return nil, fmt.Errorf("wrong number of arguments to cap: %d", len(args))
  1072  	}
  1073  
  1074  	arg := args[0]
  1075  	invalidArgErr := fmt.Errorf("invalid argument %s (type %s) for cap", exprToString(nodeargs[0]), arg.TypeString())
  1076  
  1077  	switch arg.Kind {
  1078  	case reflect.Ptr:
  1079  		arg = arg.maybeDereference()
  1080  		if arg.Kind != reflect.Array {
  1081  			return nil, invalidArgErr
  1082  		}
  1083  		fallthrough
  1084  	case reflect.Array:
  1085  		return newConstant(constant.MakeInt64(arg.Len), arg.mem), nil
  1086  	case reflect.Slice:
  1087  		return newConstant(constant.MakeInt64(arg.Cap), arg.mem), nil
  1088  	case reflect.Chan:
  1089  		arg.loadValue(loadFullValue)
  1090  		if arg.Unreadable != nil {
  1091  			return nil, arg.Unreadable
  1092  		}
  1093  		if arg.Base == 0 {
  1094  			return newConstant(constant.MakeInt64(0), arg.mem), nil
  1095  		}
  1096  		return newConstant(arg.Children[1].Value, arg.mem), nil
  1097  	default:
  1098  		return nil, invalidArgErr
  1099  	}
  1100  }
  1101  
  1102  func lenBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
  1103  	if len(args) != 1 {
  1104  		return nil, fmt.Errorf("wrong number of arguments to len: %d", len(args))
  1105  	}
  1106  	arg := args[0]
  1107  	invalidArgErr := fmt.Errorf("invalid argument %s (type %s) for len", exprToString(nodeargs[0]), arg.TypeString())
  1108  
  1109  	switch arg.Kind {
  1110  	case reflect.Ptr:
  1111  		arg = arg.maybeDereference()
  1112  		if arg.Kind != reflect.Array {
  1113  			return nil, invalidArgErr
  1114  		}
  1115  		fallthrough
  1116  	case reflect.Array, reflect.Slice, reflect.String:
  1117  		if arg.Unreadable != nil {
  1118  			return nil, arg.Unreadable
  1119  		}
  1120  		return newConstant(constant.MakeInt64(arg.Len), arg.mem), nil
  1121  	case reflect.Chan:
  1122  		arg.loadValue(loadFullValue)
  1123  		if arg.Unreadable != nil {
  1124  			return nil, arg.Unreadable
  1125  		}
  1126  		if arg.Base == 0 {
  1127  			return newConstant(constant.MakeInt64(0), arg.mem), nil
  1128  		}
  1129  		return newConstant(arg.Children[0].Value, arg.mem), nil
  1130  	case reflect.Map:
  1131  		it := arg.mapIterator()
  1132  		if arg.Unreadable != nil {
  1133  			return nil, arg.Unreadable
  1134  		}
  1135  		if it == nil {
  1136  			return newConstant(constant.MakeInt64(0), arg.mem), nil
  1137  		}
  1138  		return newConstant(constant.MakeInt64(arg.Len), arg.mem), nil
  1139  	default:
  1140  		return nil, invalidArgErr
  1141  	}
  1142  }
  1143  
  1144  func complexBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
  1145  	if len(args) != 2 {
  1146  		return nil, fmt.Errorf("wrong number of arguments to complex: %d", len(args))
  1147  	}
  1148  
  1149  	realev := args[0]
  1150  	imagev := args[1]
  1151  
  1152  	realev.loadValue(loadSingleValue)
  1153  	imagev.loadValue(loadSingleValue)
  1154  
  1155  	if realev.Unreadable != nil {
  1156  		return nil, realev.Unreadable
  1157  	}
  1158  
  1159  	if imagev.Unreadable != nil {
  1160  		return nil, imagev.Unreadable
  1161  	}
  1162  
  1163  	if realev.Value == nil || ((realev.Value.Kind() != constant.Int) && (realev.Value.Kind() != constant.Float)) {
  1164  		return nil, fmt.Errorf("invalid argument 1 %s (type %s) to complex", exprToString(nodeargs[0]), realev.TypeString())
  1165  	}
  1166  
  1167  	if imagev.Value == nil || ((imagev.Value.Kind() != constant.Int) && (imagev.Value.Kind() != constant.Float)) {
  1168  		return nil, fmt.Errorf("invalid argument 2 %s (type %s) to complex", exprToString(nodeargs[1]), imagev.TypeString())
  1169  	}
  1170  
  1171  	sz := int64(0)
  1172  	if realev.RealType != nil {
  1173  		sz = realev.RealType.(*godwarf.FloatType).Size()
  1174  	}
  1175  	if imagev.RealType != nil {
  1176  		isz := imagev.RealType.(*godwarf.FloatType).Size()
  1177  		if isz > sz {
  1178  			sz = isz
  1179  		}
  1180  	}
  1181  
  1182  	if sz == 0 {
  1183  		sz = 128
  1184  	}
  1185  
  1186  	typ := &godwarf.ComplexType{BasicType: godwarf.BasicType{CommonType: godwarf.CommonType{ByteSize: int64(sz / 8), Name: fmt.Sprintf("complex%d", sz)}, BitSize: sz, BitOffset: 0}}
  1187  
  1188  	r := realev.newVariable("", 0, typ, nil)
  1189  	r.Value = constant.BinaryOp(realev.Value, token.ADD, constant.MakeImag(imagev.Value))
  1190  	return r, nil
  1191  }
  1192  
  1193  func imagBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
  1194  	if len(args) != 1 {
  1195  		return nil, fmt.Errorf("wrong number of arguments to imag: %d", len(args))
  1196  	}
  1197  
  1198  	arg := args[0]
  1199  	arg.loadValue(loadSingleValue)
  1200  
  1201  	if arg.Unreadable != nil {
  1202  		return nil, arg.Unreadable
  1203  	}
  1204  
  1205  	if arg.Kind != reflect.Complex64 && arg.Kind != reflect.Complex128 {
  1206  		return nil, fmt.Errorf("invalid argument %s (type %s) to imag", exprToString(nodeargs[0]), arg.TypeString())
  1207  	}
  1208  
  1209  	return newConstant(constant.Imag(arg.Value), arg.mem), nil
  1210  }
  1211  
  1212  func realBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
  1213  	if len(args) != 1 {
  1214  		return nil, fmt.Errorf("wrong number of arguments to real: %d", len(args))
  1215  	}
  1216  
  1217  	arg := args[0]
  1218  	arg.loadValue(loadSingleValue)
  1219  
  1220  	if arg.Unreadable != nil {
  1221  		return nil, arg.Unreadable
  1222  	}
  1223  
  1224  	if arg.Value == nil || ((arg.Value.Kind() != constant.Int) && (arg.Value.Kind() != constant.Float) && (arg.Value.Kind() != constant.Complex)) {
  1225  		return nil, fmt.Errorf("invalid argument %s (type %s) to real", exprToString(nodeargs[0]), arg.TypeString())
  1226  	}
  1227  
  1228  	return newConstant(constant.Real(arg.Value), arg.mem), nil
  1229  }
  1230  
  1231  // Evaluates identifier expressions
  1232  func (scope *EvalScope) evalIdent(node *ast.Ident) (*Variable, error) {
  1233  	switch node.Name {
  1234  	case "true", "false":
  1235  		return newConstant(constant.MakeBool(node.Name == "true"), scope.Mem), nil
  1236  	case "nil":
  1237  		return nilVariable, nil
  1238  	}
  1239  
  1240  	vars, err := scope.Locals(0)
  1241  	if err != nil {
  1242  		return nil, err
  1243  	}
  1244  	for i := range vars {
  1245  		if vars[i].Name == node.Name && vars[i].Flags&VariableShadowed == 0 {
  1246  			return vars[i], nil
  1247  		}
  1248  	}
  1249  
  1250  	// if it's not a local variable then it could be a package variable w/o explicit package name
  1251  	if scope.Fn != nil {
  1252  		if v, err := scope.findGlobal(scope.Fn.PackageName(), node.Name); err == nil {
  1253  			v.Name = node.Name
  1254  			return v, nil
  1255  		}
  1256  	}
  1257  
  1258  	// not a local variable, nor a global variable, try a CPU register
  1259  	if s := validRegisterName(node.Name); s != "" {
  1260  		if regnum, ok := scope.BinInfo.Arch.RegisterNameToDwarf(s); ok {
  1261  			if reg := scope.Regs.Reg(uint64(regnum)); reg != nil {
  1262  				reg.FillBytes()
  1263  
  1264  				var typ godwarf.Type
  1265  				if len(reg.Bytes) <= 8 {
  1266  					typ = &godwarf.UintType{BasicType: godwarf.BasicType{CommonType: godwarf.CommonType{ByteSize: 8, Name: "uint64"}, BitSize: 64, BitOffset: 0}}
  1267  				} else {
  1268  					typ, err = scope.BinInfo.findType("string")
  1269  					if err != nil {
  1270  						return nil, err
  1271  					}
  1272  				}
  1273  
  1274  				v := newVariable(node.Name, 0, typ, scope.BinInfo, scope.Mem)
  1275  				if v.Kind == reflect.String {
  1276  					v.Len = int64(len(reg.Bytes) * 2)
  1277  					v.Base = fakeAddressUnresolv
  1278  				}
  1279  				v.Addr = fakeAddressUnresolv
  1280  				v.Flags = VariableCPURegister
  1281  				v.reg = reg
  1282  				return v, nil
  1283  			}
  1284  		}
  1285  	}
  1286  
  1287  	return nil, fmt.Errorf("could not find symbol value for %s", node.Name)
  1288  }
  1289  
  1290  // Evaluates expressions <subexpr>.<field name> where subexpr is not a package name
  1291  func (scope *EvalScope) evalStructSelector(node *ast.SelectorExpr) (*Variable, error) {
  1292  	xv, err := scope.evalAST(node.X)
  1293  	if err != nil {
  1294  		return nil, err
  1295  	}
  1296  
  1297  	// Prevent abuse, attempting to call "nil.member" directly.
  1298  	if xv.Addr == 0 && xv.Name == "nil" {
  1299  		return nil, fmt.Errorf("%s (type %s) is not a struct", xv.Name, xv.TypeString())
  1300  	}
  1301  	// Prevent abuse, attempting to call "\"fake\".member" directly.
  1302  	if xv.Addr == 0 && xv.Name == "" && xv.DwarfType == nil && xv.RealType == nil {
  1303  		return nil, fmt.Errorf("%s (type %s) is not a struct", xv.Value, xv.TypeString())
  1304  	}
  1305  	// Special type conversions for CPU register variables (REGNAME.int8, etc)
  1306  	if xv.Flags&VariableCPURegister != 0 && !xv.loaded {
  1307  		return xv.registerVariableTypeConv(node.Sel.Name)
  1308  	}
  1309  
  1310  	rv, err := xv.findMethod(node.Sel.Name)
  1311  	if err != nil {
  1312  		return nil, err
  1313  	}
  1314  	if rv != nil {
  1315  		return rv, nil
  1316  	}
  1317  	return xv.structMember(node.Sel.Name)
  1318  }
  1319  
  1320  // Evaluates expressions <subexpr>.(<type>)
  1321  func (scope *EvalScope) evalTypeAssert(node *ast.TypeAssertExpr) (*Variable, error) {
  1322  	xv, err := scope.evalAST(node.X)
  1323  	if err != nil {
  1324  		return nil, err
  1325  	}
  1326  	if xv.Kind != reflect.Interface {
  1327  		return nil, fmt.Errorf("expression \"%s\" not an interface", exprToString(node.X))
  1328  	}
  1329  	xv.loadInterface(0, false, loadFullValue)
  1330  	if xv.Unreadable != nil {
  1331  		return nil, xv.Unreadable
  1332  	}
  1333  	if xv.Children[0].Unreadable != nil {
  1334  		return nil, xv.Children[0].Unreadable
  1335  	}
  1336  	if xv.Children[0].Addr == 0 {
  1337  		return nil, fmt.Errorf("interface conversion: %s is nil, not %s", xv.DwarfType.String(), exprToString(node.Type))
  1338  	}
  1339  	// Accept .(data) as a type assertion that always succeeds, so that users
  1340  	// can access the data field of an interface without actually having to
  1341  	// type the concrete type.
  1342  	if idtyp, isident := node.Type.(*ast.Ident); !isident || idtyp.Name != "data" {
  1343  		typ, err := scope.BinInfo.findTypeExpr(node.Type)
  1344  		if err != nil {
  1345  			return nil, err
  1346  		}
  1347  		if xv.Children[0].DwarfType.Common().Name != typ.Common().Name {
  1348  			return nil, fmt.Errorf("interface conversion: %s is %s, not %s", xv.DwarfType.Common().Name, xv.Children[0].TypeString(), typ.Common().Name)
  1349  		}
  1350  	}
  1351  	// loadInterface will set OnlyAddr for the data member since here we are
  1352  	// passing false to loadData, however returning the variable with OnlyAddr
  1353  	// set here would be wrong since, once the expression evaluation
  1354  	// terminates, the value of this variable will be loaded.
  1355  	xv.Children[0].OnlyAddr = false
  1356  	return &xv.Children[0], nil
  1357  }
  1358  
  1359  // Evaluates expressions <subexpr>[<subexpr>] (subscript access to arrays, slices and maps)
  1360  func (scope *EvalScope) evalIndex(node *ast.IndexExpr) (*Variable, error) {
  1361  	xev, err := scope.evalAST(node.X)
  1362  	if err != nil {
  1363  		return nil, err
  1364  	}
  1365  	if xev.Unreadable != nil {
  1366  		return nil, xev.Unreadable
  1367  	}
  1368  
  1369  	if xev.Flags&VariableCPtr == 0 {
  1370  		xev = xev.maybeDereference()
  1371  	}
  1372  
  1373  	idxev, err := scope.evalAST(node.Index)
  1374  	if err != nil {
  1375  		return nil, err
  1376  	}
  1377  
  1378  	cantindex := fmt.Errorf("expression \"%s\" (%s) does not support indexing", exprToString(node.X), xev.TypeString())
  1379  
  1380  	switch xev.Kind {
  1381  	case reflect.Ptr:
  1382  		if xev == nilVariable {
  1383  			return nil, cantindex
  1384  		}
  1385  		if xev.Flags&VariableCPtr == 0 {
  1386  			_, isarrptr := xev.RealType.(*godwarf.PtrType).Type.(*godwarf.ArrayType)
  1387  			if !isarrptr {
  1388  				return nil, cantindex
  1389  			}
  1390  			xev = xev.maybeDereference()
  1391  		}
  1392  		fallthrough
  1393  
  1394  	case reflect.Slice, reflect.Array, reflect.String:
  1395  		if xev.Base == 0 {
  1396  			return nil, fmt.Errorf("can not index \"%s\"", exprToString(node.X))
  1397  		}
  1398  		n, err := idxev.asInt()
  1399  		if err != nil {
  1400  			return nil, err
  1401  		}
  1402  		return xev.sliceAccess(int(n))
  1403  
  1404  	case reflect.Map:
  1405  		idxev.loadValue(loadFullValue)
  1406  		if idxev.Unreadable != nil {
  1407  			return nil, idxev.Unreadable
  1408  		}
  1409  		return xev.mapAccess(idxev)
  1410  	default:
  1411  		return nil, cantindex
  1412  	}
  1413  }
  1414  
  1415  // Evaluates expressions <subexpr>[<subexpr>:<subexpr>]
  1416  // HACK: slicing a map expression with [0:0] will return the whole map
  1417  func (scope *EvalScope) evalReslice(node *ast.SliceExpr) (*Variable, error) {
  1418  	xev, err := scope.evalAST(node.X)
  1419  	if err != nil {
  1420  		return nil, err
  1421  	}
  1422  	if xev.Unreadable != nil {
  1423  		return nil, xev.Unreadable
  1424  	}
  1425  
  1426  	var low, high int64
  1427  
  1428  	if node.Low != nil {
  1429  		lowv, err := scope.evalAST(node.Low)
  1430  		if err != nil {
  1431  			return nil, err
  1432  		}
  1433  		low, err = lowv.asInt()
  1434  		if err != nil {
  1435  			return nil, fmt.Errorf("can not convert \"%s\" to int: %v", exprToString(node.Low), err)
  1436  		}
  1437  	}
  1438  
  1439  	if node.High == nil {
  1440  		high = xev.Len
  1441  	} else {
  1442  		highv, err := scope.evalAST(node.High)
  1443  		if err != nil {
  1444  			return nil, err
  1445  		}
  1446  		high, err = highv.asInt()
  1447  		if err != nil {
  1448  			return nil, fmt.Errorf("can not convert \"%s\" to int: %v", exprToString(node.High), err)
  1449  		}
  1450  	}
  1451  
  1452  	switch xev.Kind {
  1453  	case reflect.Slice, reflect.Array, reflect.String:
  1454  		if xev.Base == 0 {
  1455  			return nil, fmt.Errorf("can not slice \"%s\"", exprToString(node.X))
  1456  		}
  1457  		return xev.reslice(low, high)
  1458  	case reflect.Map:
  1459  		if node.High != nil {
  1460  			return nil, fmt.Errorf("second slice argument must be empty for maps")
  1461  		}
  1462  		xev.mapSkip += int(low)
  1463  		xev.mapIterator() // reads map length
  1464  		if int64(xev.mapSkip) >= xev.Len {
  1465  			return nil, fmt.Errorf("map index out of bounds")
  1466  		}
  1467  		return xev, nil
  1468  	case reflect.Ptr:
  1469  		if xev.Flags&VariableCPtr != 0 {
  1470  			return xev.reslice(low, high)
  1471  		}
  1472  		fallthrough
  1473  	default:
  1474  		return nil, fmt.Errorf("can not slice \"%s\" (type %s)", exprToString(node.X), xev.TypeString())
  1475  	}
  1476  }
  1477  
  1478  // Evaluates a pointer dereference expression: *<subexpr>
  1479  func (scope *EvalScope) evalPointerDeref(node *ast.StarExpr) (*Variable, error) {
  1480  	xev, err := scope.evalAST(node.X)
  1481  	if err != nil {
  1482  		return nil, err
  1483  	}
  1484  
  1485  	if xev.Kind != reflect.Ptr {
  1486  		return nil, fmt.Errorf("expression \"%s\" (%s) can not be dereferenced", exprToString(node.X), xev.TypeString())
  1487  	}
  1488  
  1489  	if xev == nilVariable {
  1490  		return nil, fmt.Errorf("nil can not be dereferenced")
  1491  	}
  1492  
  1493  	if len(xev.Children) == 1 {
  1494  		// this branch is here to support pointers constructed with typecasts from ints
  1495  		xev.Children[0].OnlyAddr = false
  1496  		return &(xev.Children[0]), nil
  1497  	}
  1498  	rv := xev.maybeDereference()
  1499  	if rv.Addr == 0 {
  1500  		return nil, fmt.Errorf("nil pointer dereference")
  1501  	}
  1502  	return rv, nil
  1503  }
  1504  
  1505  // Evaluates expressions &<subexpr>
  1506  func (scope *EvalScope) evalAddrOf(node *ast.UnaryExpr) (*Variable, error) {
  1507  	xev, err := scope.evalAST(node.X)
  1508  	if err != nil {
  1509  		return nil, err
  1510  	}
  1511  	if xev.Addr == 0 || xev.DwarfType == nil {
  1512  		return nil, fmt.Errorf("can not take address of \"%s\"", exprToString(node.X))
  1513  	}
  1514  
  1515  	return xev.pointerToVariable(), nil
  1516  }
  1517  
  1518  func (v *Variable) pointerToVariable() *Variable {
  1519  	v.OnlyAddr = true
  1520  
  1521  	typename := "*" + v.DwarfType.Common().Name
  1522  	rv := v.newVariable("", 0, &godwarf.PtrType{CommonType: godwarf.CommonType{ByteSize: int64(v.bi.Arch.PtrSize()), Name: typename}, Type: v.DwarfType}, v.mem)
  1523  	rv.Children = []Variable{*v}
  1524  	rv.loaded = true
  1525  
  1526  	return rv
  1527  }
  1528  
  1529  func constantUnaryOp(op token.Token, y constant.Value) (r constant.Value, err error) {
  1530  	defer func() {
  1531  		if ierr := recover(); ierr != nil {
  1532  			err = fmt.Errorf("%v", ierr)
  1533  		}
  1534  	}()
  1535  	r = constant.UnaryOp(op, y, 0)
  1536  	return
  1537  }
  1538  
  1539  func constantBinaryOp(op token.Token, x, y constant.Value) (r constant.Value, err error) {
  1540  	defer func() {
  1541  		if ierr := recover(); ierr != nil {
  1542  			err = fmt.Errorf("%v", ierr)
  1543  		}
  1544  	}()
  1545  	switch op {
  1546  	case token.SHL, token.SHR:
  1547  		n, _ := constant.Uint64Val(y)
  1548  		r = constant.Shift(x, op, uint(n))
  1549  	default:
  1550  		r = constant.BinaryOp(x, op, y)
  1551  	}
  1552  	return
  1553  }
  1554  
  1555  func constantCompare(op token.Token, x, y constant.Value) (r bool, err error) {
  1556  	defer func() {
  1557  		if ierr := recover(); ierr != nil {
  1558  			err = fmt.Errorf("%v", ierr)
  1559  		}
  1560  	}()
  1561  	r = constant.Compare(x, op, y)
  1562  	return
  1563  }
  1564  
  1565  // Evaluates expressions: -<subexpr> and +<subexpr>
  1566  func (scope *EvalScope) evalUnary(node *ast.UnaryExpr) (*Variable, error) {
  1567  	xv, err := scope.evalAST(node.X)
  1568  	if err != nil {
  1569  		return nil, err
  1570  	}
  1571  
  1572  	xv.loadValue(loadSingleValue)
  1573  	if xv.Unreadable != nil {
  1574  		return nil, xv.Unreadable
  1575  	}
  1576  	if xv.FloatSpecial != 0 {
  1577  		return nil, errOperationOnSpecialFloat
  1578  	}
  1579  	if xv.Value == nil {
  1580  		return nil, fmt.Errorf("operator %s can not be applied to \"%s\"", node.Op.String(), exprToString(node.X))
  1581  	}
  1582  	rc, err := constantUnaryOp(node.Op, xv.Value)
  1583  	if err != nil {
  1584  		return nil, err
  1585  	}
  1586  	if xv.DwarfType != nil {
  1587  		r := xv.newVariable("", 0, xv.DwarfType, scope.Mem)
  1588  		r.Value = rc
  1589  		return r, nil
  1590  	}
  1591  	return newConstant(rc, xv.mem), nil
  1592  }
  1593  
  1594  func negotiateType(op token.Token, xv, yv *Variable) (godwarf.Type, error) {
  1595  	if xv == nilVariable {
  1596  		return nil, negotiateTypeNil(op, yv)
  1597  	}
  1598  
  1599  	if yv == nilVariable {
  1600  		return nil, negotiateTypeNil(op, xv)
  1601  	}
  1602  
  1603  	if op == token.SHR || op == token.SHL {
  1604  		if xv.Value == nil || xv.Value.Kind() != constant.Int {
  1605  			return nil, fmt.Errorf("shift of type %s", xv.Kind)
  1606  		}
  1607  
  1608  		switch yv.Kind {
  1609  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1610  			// ok
  1611  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1612  			if constant.Sign(yv.Value) < 0 {
  1613  				return nil, fmt.Errorf("shift count must not be negative")
  1614  			}
  1615  		default:
  1616  			return nil, fmt.Errorf("shift count type %s, must be unsigned integer", yv.Kind.String())
  1617  		}
  1618  
  1619  		return xv.DwarfType, nil
  1620  	}
  1621  
  1622  	if xv.DwarfType == nil && yv.DwarfType == nil {
  1623  		return nil, nil
  1624  	}
  1625  
  1626  	if xv.DwarfType != nil && yv.DwarfType != nil {
  1627  		if xv.DwarfType.String() != yv.DwarfType.String() {
  1628  			return nil, fmt.Errorf("mismatched types \"%s\" and \"%s\"", xv.DwarfType.String(), yv.DwarfType.String())
  1629  		}
  1630  		return xv.DwarfType, nil
  1631  	} else if xv.DwarfType != nil && yv.DwarfType == nil {
  1632  		if err := yv.isType(xv.DwarfType, xv.Kind); err != nil {
  1633  			return nil, err
  1634  		}
  1635  		return xv.DwarfType, nil
  1636  	} else if xv.DwarfType == nil && yv.DwarfType != nil {
  1637  		if err := xv.isType(yv.DwarfType, yv.Kind); err != nil {
  1638  			return nil, err
  1639  		}
  1640  		return yv.DwarfType, nil
  1641  	}
  1642  
  1643  	panic("unreachable")
  1644  }
  1645  
  1646  func negotiateTypeNil(op token.Token, v *Variable) error {
  1647  	if op != token.EQL && op != token.NEQ {
  1648  		return fmt.Errorf("operator %s can not be applied to \"nil\"", op.String())
  1649  	}
  1650  	switch v.Kind {
  1651  	case reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:
  1652  		return nil
  1653  	default:
  1654  		return fmt.Errorf("can not compare %s to nil", v.Kind.String())
  1655  	}
  1656  }
  1657  
  1658  func (scope *EvalScope) evalBinary(node *ast.BinaryExpr) (*Variable, error) {
  1659  	switch node.Op {
  1660  	case token.INC, token.DEC, token.ARROW:
  1661  		return nil, fmt.Errorf("operator %s not supported", node.Op.String())
  1662  	}
  1663  
  1664  	xv, err := scope.evalAST(node.X)
  1665  	if err != nil {
  1666  		return nil, err
  1667  	}
  1668  	if xv.Kind != reflect.String { // delay loading strings until we use them
  1669  		xv.loadValue(loadFullValue)
  1670  	}
  1671  	if xv.Unreadable != nil {
  1672  		return nil, xv.Unreadable
  1673  	}
  1674  
  1675  	// short circuits logical operators
  1676  	switch node.Op {
  1677  	case token.LAND:
  1678  		if !constant.BoolVal(xv.Value) {
  1679  			return newConstant(xv.Value, xv.mem), nil
  1680  		}
  1681  	case token.LOR:
  1682  		if constant.BoolVal(xv.Value) {
  1683  			return newConstant(xv.Value, xv.mem), nil
  1684  		}
  1685  	}
  1686  
  1687  	yv, err := scope.evalAST(node.Y)
  1688  	if err != nil {
  1689  		return nil, err
  1690  	}
  1691  	if yv.Kind != reflect.String { // delay loading strings until we use them
  1692  		yv.loadValue(loadFullValue)
  1693  	}
  1694  	if yv.Unreadable != nil {
  1695  		return nil, yv.Unreadable
  1696  	}
  1697  
  1698  	if xv.FloatSpecial != 0 || yv.FloatSpecial != 0 {
  1699  		return nil, errOperationOnSpecialFloat
  1700  	}
  1701  
  1702  	typ, err := negotiateType(node.Op, xv, yv)
  1703  	if err != nil {
  1704  		return nil, err
  1705  	}
  1706  
  1707  	op := node.Op
  1708  	if typ != nil && (op == token.QUO) {
  1709  		_, isint := typ.(*godwarf.IntType)
  1710  		_, isuint := typ.(*godwarf.UintType)
  1711  		if isint || isuint {
  1712  			// forces integer division if the result type is integer
  1713  			op = token.QUO_ASSIGN
  1714  		}
  1715  	}
  1716  
  1717  	switch op {
  1718  	case token.EQL, token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ:
  1719  		v, err := compareOp(op, xv, yv)
  1720  		if err != nil {
  1721  			return nil, err
  1722  		}
  1723  		return newConstant(constant.MakeBool(v), xv.mem), nil
  1724  
  1725  	default:
  1726  		if xv.Kind == reflect.String {
  1727  			xv.loadValue(loadFullValueLongerStrings)
  1728  		}
  1729  		if yv.Kind == reflect.String {
  1730  			yv.loadValue(loadFullValueLongerStrings)
  1731  		}
  1732  		if xv.Value == nil {
  1733  			return nil, fmt.Errorf("operator %s can not be applied to \"%s\"", node.Op.String(), exprToString(node.X))
  1734  		}
  1735  
  1736  		if yv.Value == nil {
  1737  			return nil, fmt.Errorf("operator %s can not be applied to \"%s\"", node.Op.String(), exprToString(node.Y))
  1738  		}
  1739  
  1740  		rc, err := constantBinaryOp(op, xv.Value, yv.Value)
  1741  		if err != nil {
  1742  			return nil, err
  1743  		}
  1744  
  1745  		if typ == nil {
  1746  			return newConstant(rc, xv.mem), nil
  1747  		}
  1748  
  1749  		r := xv.newVariable("", 0, typ, scope.Mem)
  1750  		r.Value = rc
  1751  		switch r.Kind {
  1752  		case reflect.String:
  1753  			r.Len = xv.Len + yv.Len
  1754  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1755  			n, _ := constant.Int64Val(r.Value)
  1756  			r.Value = constant.MakeInt64(int64(convertInt(uint64(n), true, typ.Size())))
  1757  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  1758  			n, _ := constant.Uint64Val(r.Value)
  1759  			r.Value = constant.MakeUint64(convertInt(n, false, typ.Size()))
  1760  		}
  1761  		return r, nil
  1762  	}
  1763  }
  1764  
  1765  // Compares xv to yv using operator op
  1766  // Both xv and yv must be loaded and have a compatible type (as determined by negotiateType)
  1767  func compareOp(op token.Token, xv *Variable, yv *Variable) (bool, error) {
  1768  	switch xv.Kind {
  1769  	case reflect.Bool:
  1770  		fallthrough
  1771  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1772  		fallthrough
  1773  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1774  		fallthrough
  1775  	case reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
  1776  		return constantCompare(op, xv.Value, yv.Value)
  1777  	case reflect.String:
  1778  		if xv.Len != yv.Len {
  1779  			switch op {
  1780  			case token.EQL:
  1781  				return false, nil
  1782  			case token.NEQ:
  1783  				return true, nil
  1784  			}
  1785  		}
  1786  		if xv.Kind == reflect.String {
  1787  			xv.loadValue(loadFullValueLongerStrings)
  1788  		}
  1789  		if yv.Kind == reflect.String {
  1790  			yv.loadValue(loadFullValueLongerStrings)
  1791  		}
  1792  		if int64(len(constant.StringVal(xv.Value))) != xv.Len || int64(len(constant.StringVal(yv.Value))) != yv.Len {
  1793  			return false, fmt.Errorf("string too long for comparison")
  1794  		}
  1795  		return constantCompare(op, xv.Value, yv.Value)
  1796  	}
  1797  
  1798  	if op != token.EQL && op != token.NEQ {
  1799  		return false, fmt.Errorf("operator %s not defined on %s", op.String(), xv.Kind.String())
  1800  	}
  1801  
  1802  	var eql bool
  1803  	var err error
  1804  
  1805  	if xv == nilVariable {
  1806  		switch op {
  1807  		case token.EQL:
  1808  			return yv.isNil(), nil
  1809  		case token.NEQ:
  1810  			return !yv.isNil(), nil
  1811  		}
  1812  	}
  1813  
  1814  	if yv == nilVariable {
  1815  		switch op {
  1816  		case token.EQL:
  1817  			return xv.isNil(), nil
  1818  		case token.NEQ:
  1819  			return !xv.isNil(), nil
  1820  		}
  1821  	}
  1822  
  1823  	switch xv.Kind {
  1824  	case reflect.Ptr:
  1825  		eql = xv.Children[0].Addr == yv.Children[0].Addr
  1826  	case reflect.Array:
  1827  		if int64(len(xv.Children)) != xv.Len || int64(len(yv.Children)) != yv.Len {
  1828  			return false, fmt.Errorf("array too long for comparison")
  1829  		}
  1830  		eql, err = equalChildren(xv, yv, true)
  1831  	case reflect.Struct:
  1832  		if len(xv.Children) != len(yv.Children) {
  1833  			return false, nil
  1834  		}
  1835  		if int64(len(xv.Children)) != xv.Len || int64(len(yv.Children)) != yv.Len {
  1836  			return false, fmt.Errorf("structure too deep for comparison")
  1837  		}
  1838  		eql, err = equalChildren(xv, yv, false)
  1839  	case reflect.Slice, reflect.Map, reflect.Func, reflect.Chan:
  1840  		return false, fmt.Errorf("can not compare %s variables", xv.Kind.String())
  1841  	case reflect.Interface:
  1842  		if xv.Children[0].RealType.String() != yv.Children[0].RealType.String() {
  1843  			eql = false
  1844  		} else {
  1845  			eql, err = compareOp(token.EQL, &xv.Children[0], &yv.Children[0])
  1846  		}
  1847  	default:
  1848  		return false, fmt.Errorf("unimplemented comparison of %s variables", xv.Kind.String())
  1849  	}
  1850  
  1851  	if op == token.NEQ {
  1852  		return !eql, err
  1853  	}
  1854  	return eql, err
  1855  }
  1856  
  1857  func (v *Variable) isNil() bool {
  1858  	switch v.Kind {
  1859  	case reflect.Ptr:
  1860  		return v.Children[0].Addr == 0
  1861  	case reflect.Interface:
  1862  		return v.Children[0].Addr == 0 && v.Children[0].Kind == reflect.Invalid
  1863  	case reflect.Slice, reflect.Map, reflect.Func, reflect.Chan:
  1864  		return v.Base == 0
  1865  	}
  1866  	return false
  1867  }
  1868  
  1869  func equalChildren(xv, yv *Variable, shortcircuit bool) (bool, error) {
  1870  	r := true
  1871  	for i := range xv.Children {
  1872  		eql, err := compareOp(token.EQL, &xv.Children[i], &yv.Children[i])
  1873  		if err != nil {
  1874  			return false, err
  1875  		}
  1876  		r = r && eql
  1877  		if !r && shortcircuit {
  1878  			return false, nil
  1879  		}
  1880  	}
  1881  	return r, nil
  1882  }
  1883  
  1884  func (v *Variable) asInt() (int64, error) {
  1885  	if v.DwarfType == nil {
  1886  		if v.Value.Kind() != constant.Int {
  1887  			return 0, fmt.Errorf("can not convert constant %s to int", v.Value)
  1888  		}
  1889  	} else {
  1890  		v.loadValue(loadSingleValue)
  1891  		if v.Unreadable != nil {
  1892  			return 0, v.Unreadable
  1893  		}
  1894  		if _, ok := v.DwarfType.(*godwarf.IntType); !ok {
  1895  			return 0, fmt.Errorf("can not convert value of type %s to int", v.DwarfType.String())
  1896  		}
  1897  	}
  1898  	n, _ := constant.Int64Val(v.Value)
  1899  	return n, nil
  1900  }
  1901  
  1902  func (v *Variable) asUint() (uint64, error) {
  1903  	if v.DwarfType == nil {
  1904  		if v.Value.Kind() != constant.Int {
  1905  			return 0, fmt.Errorf("can not convert constant %s to uint", v.Value)
  1906  		}
  1907  	} else {
  1908  		v.loadValue(loadSingleValue)
  1909  		if v.Unreadable != nil {
  1910  			return 0, v.Unreadable
  1911  		}
  1912  		if _, ok := v.DwarfType.(*godwarf.UintType); !ok {
  1913  			return 0, fmt.Errorf("can not convert value of type %s to uint", v.DwarfType.String())
  1914  		}
  1915  	}
  1916  	n, _ := constant.Uint64Val(v.Value)
  1917  	return n, nil
  1918  }
  1919  
  1920  type typeConvErr struct {
  1921  	srcType, dstType godwarf.Type
  1922  }
  1923  
  1924  func (err *typeConvErr) Error() string {
  1925  	return fmt.Sprintf("can not convert value of type %s to %s", err.srcType.String(), err.dstType.String())
  1926  }
  1927  
  1928  func (v *Variable) isType(typ godwarf.Type, kind reflect.Kind) error {
  1929  	if v.DwarfType != nil {
  1930  		if typ == nil || !sameType(typ, v.RealType) {
  1931  			return &typeConvErr{v.DwarfType, typ}
  1932  		}
  1933  		return nil
  1934  	}
  1935  
  1936  	if typ == nil {
  1937  		return nil
  1938  	}
  1939  
  1940  	if v == nilVariable {
  1941  		switch kind {
  1942  		case reflect.Slice, reflect.Map, reflect.Func, reflect.Ptr, reflect.Chan, reflect.Interface:
  1943  			return nil
  1944  		default:
  1945  			return fmt.Errorf("mismatched types nil and %s", typ.String())
  1946  		}
  1947  	}
  1948  
  1949  	converr := fmt.Errorf("can not convert %s constant to %s", v.Value, typ.String())
  1950  
  1951  	if v.Value == nil {
  1952  		return converr
  1953  	}
  1954  
  1955  	switch typ.(type) {
  1956  	case *godwarf.IntType:
  1957  		if v.Value.Kind() != constant.Int {
  1958  			return converr
  1959  		}
  1960  	case *godwarf.UintType:
  1961  		if v.Value.Kind() != constant.Int {
  1962  			return converr
  1963  		}
  1964  	case *godwarf.FloatType:
  1965  		if (v.Value.Kind() != constant.Int) && (v.Value.Kind() != constant.Float) {
  1966  			return converr
  1967  		}
  1968  	case *godwarf.BoolType:
  1969  		if v.Value.Kind() != constant.Bool {
  1970  			return converr
  1971  		}
  1972  	case *godwarf.StringType:
  1973  		if v.Value.Kind() != constant.String {
  1974  			return converr
  1975  		}
  1976  	case *godwarf.ComplexType:
  1977  		if v.Value.Kind() != constant.Complex && v.Value.Kind() != constant.Float && v.Value.Kind() != constant.Int {
  1978  			return converr
  1979  		}
  1980  	default:
  1981  		return converr
  1982  	}
  1983  
  1984  	return nil
  1985  }
  1986  
  1987  func sameType(t1, t2 godwarf.Type) bool {
  1988  	// Because of a bug in the go linker a type that refers to another type
  1989  	// (for example a pointer type) will usually use the typedef but rarely use
  1990  	// the non-typedef entry directly.
  1991  	// For types that we read directly from go this is fine because it's
  1992  	// consistent, however we also synthesize some types ourselves
  1993  	// (specifically pointers and slices) and we always use a reference through
  1994  	// a typedef.
  1995  	t1 = resolveTypedef(t1)
  1996  	t2 = resolveTypedef(t2)
  1997  
  1998  	if tt1, isptr1 := t1.(*godwarf.PtrType); isptr1 {
  1999  		tt2, isptr2 := t2.(*godwarf.PtrType)
  2000  		if !isptr2 {
  2001  			return false
  2002  		}
  2003  		return sameType(tt1.Type, tt2.Type)
  2004  	}
  2005  	if tt1, isslice1 := t1.(*godwarf.SliceType); isslice1 {
  2006  		tt2, isslice2 := t2.(*godwarf.SliceType)
  2007  		if !isslice2 {
  2008  			return false
  2009  		}
  2010  		return sameType(tt1.ElemType, tt2.ElemType)
  2011  	}
  2012  	return t1.String() == t2.String()
  2013  }
  2014  
  2015  func (v *Variable) sliceAccess(idx int) (*Variable, error) {
  2016  	wrong := false
  2017  	if v.Flags&VariableCPtr == 0 {
  2018  		wrong = idx < 0 || int64(idx) >= v.Len
  2019  	} else {
  2020  		wrong = idx < 0
  2021  	}
  2022  	if wrong {
  2023  		return nil, fmt.Errorf("index out of bounds")
  2024  	}
  2025  	if v.loaded {
  2026  		return &v.Children[idx], nil
  2027  	}
  2028  	mem := v.mem
  2029  	if v.Kind != reflect.Array {
  2030  		mem = DereferenceMemory(mem)
  2031  	}
  2032  	return v.newVariable("", v.Base+uint64(int64(idx)*v.stride), v.fieldType, mem), nil
  2033  }
  2034  
  2035  func (v *Variable) mapAccess(idx *Variable) (*Variable, error) {
  2036  	it := v.mapIterator()
  2037  	if it == nil {
  2038  		return nil, fmt.Errorf("can not access unreadable map: %v", v.Unreadable)
  2039  	}
  2040  
  2041  	lcfg := loadFullValue
  2042  	if idx.Kind == reflect.String && int64(len(constant.StringVal(idx.Value))) == idx.Len && idx.Len > int64(lcfg.MaxStringLen) {
  2043  		// If the index is a string load as much of the keys to at least match the length of the index.
  2044  		//TODO(aarzilli): when struct literals are implemented this needs to be
  2045  		//done recursively for literal struct fields.
  2046  		lcfg.MaxStringLen = int(idx.Len)
  2047  	}
  2048  
  2049  	first := true
  2050  	for it.next() {
  2051  		key := it.key()
  2052  		key.loadValue(lcfg)
  2053  		if key.Unreadable != nil {
  2054  			return nil, fmt.Errorf("can not access unreadable map: %v", key.Unreadable)
  2055  		}
  2056  		if first {
  2057  			first = false
  2058  			if err := idx.isType(key.RealType, key.Kind); err != nil {
  2059  				return nil, err
  2060  			}
  2061  		}
  2062  		eql, err := compareOp(token.EQL, key, idx)
  2063  		if err != nil {
  2064  			return nil, err
  2065  		}
  2066  		if eql {
  2067  			return it.value(), nil
  2068  		}
  2069  	}
  2070  	if v.Unreadable != nil {
  2071  		return nil, v.Unreadable
  2072  	}
  2073  	// go would return zero for the map value type here, we do not have the ability to create zeroes
  2074  	return nil, fmt.Errorf("key not found")
  2075  }
  2076  
  2077  // LoadResliced returns a new array, slice or map that starts at index start and contains
  2078  // up to cfg.MaxArrayValues children.
  2079  func (v *Variable) LoadResliced(start int, cfg LoadConfig) (newV *Variable, err error) {
  2080  	switch v.Kind {
  2081  	case reflect.Array, reflect.Slice:
  2082  		low, high := int64(start), int64(start+cfg.MaxArrayValues)
  2083  		if high > v.Len {
  2084  			high = v.Len
  2085  		}
  2086  		newV, err = v.reslice(low, high)
  2087  		if err != nil {
  2088  			return nil, err
  2089  		}
  2090  	case reflect.Map:
  2091  		newV = v.clone()
  2092  		newV.Children = nil
  2093  		newV.loaded = false
  2094  		newV.mapSkip = start
  2095  	default:
  2096  		return nil, fmt.Errorf("variable to reslice is not an array, slice, or map")
  2097  	}
  2098  	newV.loadValue(cfg)
  2099  	return newV, nil
  2100  }
  2101  
  2102  func (v *Variable) reslice(low int64, high int64) (*Variable, error) {
  2103  	wrong := false
  2104  	cptrNeedsFakeSlice := false
  2105  	if v.Flags&VariableCPtr == 0 {
  2106  		wrong = low < 0 || low > v.Len || high < 0 || high > v.Len
  2107  	} else {
  2108  		wrong = low < 0 || high < 0
  2109  		if high == 0 {
  2110  			high = low
  2111  		}
  2112  		cptrNeedsFakeSlice = v.Kind != reflect.String
  2113  	}
  2114  	if wrong {
  2115  		return nil, fmt.Errorf("index out of bounds")
  2116  	}
  2117  
  2118  	base := v.Base + uint64(int64(low)*v.stride)
  2119  	len := high - low
  2120  
  2121  	if high-low < 0 {
  2122  		return nil, fmt.Errorf("index out of bounds")
  2123  	}
  2124  
  2125  	typ := v.DwarfType
  2126  	if _, isarr := v.DwarfType.(*godwarf.ArrayType); isarr || cptrNeedsFakeSlice {
  2127  		typ = fakeSliceType(v.fieldType)
  2128  	}
  2129  
  2130  	mem := v.mem
  2131  	if v.Kind != reflect.Array {
  2132  		mem = DereferenceMemory(mem)
  2133  	}
  2134  
  2135  	r := v.newVariable("", 0, typ, mem)
  2136  	r.Cap = len
  2137  	r.Len = len
  2138  	r.Base = base
  2139  	r.stride = v.stride
  2140  	r.fieldType = v.fieldType
  2141  	r.Flags = v.Flags
  2142  	r.reg = v.reg
  2143  
  2144  	return r, nil
  2145  }
  2146  
  2147  // findMethod finds method mname in the type of variable v
  2148  func (v *Variable) findMethod(mname string) (*Variable, error) {
  2149  	if _, isiface := v.RealType.(*godwarf.InterfaceType); isiface {
  2150  		v.loadInterface(0, false, loadFullValue)
  2151  		if v.Unreadable != nil {
  2152  			return nil, v.Unreadable
  2153  		}
  2154  		return v.Children[0].findMethod(mname)
  2155  	}
  2156  
  2157  	queue := []*Variable{v}
  2158  	seen := map[string]struct{}{}
  2159  
  2160  	for len(queue) > 0 {
  2161  		v := queue[0]
  2162  		queue = append(queue[:0], queue[1:]...)
  2163  		if _, isseen := seen[v.RealType.String()]; isseen {
  2164  			continue
  2165  		}
  2166  		seen[v.RealType.String()] = struct{}{}
  2167  
  2168  		typ := v.DwarfType
  2169  		ptyp, isptr := typ.(*godwarf.PtrType)
  2170  		if isptr {
  2171  			typ = ptyp.Type
  2172  		}
  2173  
  2174  		typePath := typ.Common().Name
  2175  		dot := strings.LastIndex(typePath, ".")
  2176  		if dot < 0 {
  2177  			// probably just a C type
  2178  			continue
  2179  		}
  2180  
  2181  		pkg := typePath[:dot]
  2182  		receiver := typePath[dot+1:]
  2183  
  2184  		//TODO(aarzilli): support generic functions?
  2185  
  2186  		if fn, ok := v.bi.LookupFunc[fmt.Sprintf("%s.%s.%s", pkg, receiver, mname)]; ok {
  2187  			r, err := functionToVariable(fn, v.bi, v.mem)
  2188  			if err != nil {
  2189  				return nil, err
  2190  			}
  2191  			if isptr {
  2192  				r.Children = append(r.Children, *(v.maybeDereference()))
  2193  			} else {
  2194  				r.Children = append(r.Children, *v)
  2195  			}
  2196  			return r, nil
  2197  		}
  2198  
  2199  		if fn, ok := v.bi.LookupFunc[fmt.Sprintf("%s.(*%s).%s", pkg, receiver, mname)]; ok {
  2200  			r, err := functionToVariable(fn, v.bi, v.mem)
  2201  			if err != nil {
  2202  				return nil, err
  2203  			}
  2204  			if isptr {
  2205  				r.Children = append(r.Children, *v)
  2206  			} else {
  2207  				r.Children = append(r.Children, *(v.pointerToVariable()))
  2208  			}
  2209  			return r, nil
  2210  		}
  2211  
  2212  		// queue embedded fields for search
  2213  		structVar := v.maybeDereference()
  2214  		structVar.Name = v.Name
  2215  		if structVar.Unreadable != nil {
  2216  			return structVar, nil
  2217  		}
  2218  		switch t := structVar.RealType.(type) {
  2219  		case *godwarf.StructType:
  2220  			for _, field := range t.Field {
  2221  				if field.Embedded {
  2222  					embeddedVar, err := structVar.toField(field)
  2223  					if err != nil {
  2224  						return nil, err
  2225  					}
  2226  					queue = append(queue, embeddedVar)
  2227  				}
  2228  			}
  2229  		}
  2230  	}
  2231  
  2232  	return nil, nil
  2233  }
  2234  
  2235  func functionToVariable(fn *Function, bi *BinaryInfo, mem MemoryReadWriter) (*Variable, error) {
  2236  	typ, err := fn.fakeType(bi, true)
  2237  	if err != nil {
  2238  		return nil, err
  2239  	}
  2240  	v := newVariable(fn.Name, 0, typ, bi, mem)
  2241  	v.Value = constant.MakeString(fn.Name)
  2242  	v.loaded = true
  2243  	v.Base = fn.Entry
  2244  	return v, nil
  2245  }
  2246  
  2247  func fakeSliceType(fieldType godwarf.Type) godwarf.Type {
  2248  	return &godwarf.SliceType{
  2249  		StructType: godwarf.StructType{
  2250  			CommonType: godwarf.CommonType{
  2251  				ByteSize: 24,
  2252  				Name:     "",
  2253  			},
  2254  			StructName: fmt.Sprintf("[]%s", fieldType.Common().Name),
  2255  			Kind:       "struct",
  2256  			Field:      nil,
  2257  		},
  2258  		ElemType: fieldType,
  2259  	}
  2260  }
  2261  
  2262  func fakeArrayType(n uint64, fieldType godwarf.Type) godwarf.Type {
  2263  	stride := alignAddr(fieldType.Common().ByteSize, fieldType.Align())
  2264  	return &godwarf.ArrayType{
  2265  		CommonType: godwarf.CommonType{
  2266  			ReflectKind: reflect.Array,
  2267  			ByteSize:    int64(n) * stride,
  2268  			Name:        fmt.Sprintf("[%d]%s", n, fieldType.String())},
  2269  		Type:          fieldType,
  2270  		StrideBitSize: stride * 8,
  2271  		Count:         int64(n)}
  2272  }
  2273  
  2274  var errMethodEvalUnsupported = errors.New("evaluating methods not supported on this version of Go")
  2275  
  2276  func (fn *Function) fakeType(bi *BinaryInfo, removeReceiver bool) (*godwarf.FuncType, error) {
  2277  	if producer := bi.Producer(); producer == "" || !goversion.ProducerAfterOrEqual(producer, 1, 10) {
  2278  		// versions of Go prior to 1.10 do not distinguish between parameters and
  2279  		// return values, therefore we can't use a subprogram DIE to derive a
  2280  		// function type.
  2281  		return nil, errMethodEvalUnsupported
  2282  	}
  2283  	_, formalArgs, err := funcCallArgs(fn, bi, true)
  2284  	if err != nil {
  2285  		return nil, err
  2286  	}
  2287  
  2288  	// Only try and remove the receiver if it is actually being passed in as a formal argument.
  2289  	// In the case of:
  2290  	//
  2291  	// func (_ X) Method() { ... }
  2292  	//
  2293  	// that would not be true, the receiver is not used and thus
  2294  	// not being passed in as a formal argument.
  2295  	//
  2296  	// TODO(derekparker) This, I think, creates a new bug where
  2297  	// if the receiver is not passed in as a formal argument but
  2298  	// there are other arguments, such as:
  2299  	//
  2300  	// func (_ X) Method(i int) { ... }
  2301  	//
  2302  	// The first argument 'i int' will be removed. We must actually detect
  2303  	// here if the receiver is being used. While this is a bug, it's not a
  2304  	// functional bug, it only affects the string representation of the fake
  2305  	// function type we create. It's not really easy to tell here if we use
  2306  	// the receiver or not. Perhaps we should not perform this manipulation at all?
  2307  	if removeReceiver && len(formalArgs) > 0 {
  2308  		formalArgs = formalArgs[1:]
  2309  	}
  2310  
  2311  	args := make([]string, 0, len(formalArgs))
  2312  	rets := make([]string, 0, len(formalArgs))
  2313  
  2314  	for _, formalArg := range formalArgs {
  2315  		var s string
  2316  		if strings.HasPrefix(formalArg.name, "~") {
  2317  			s = formalArg.typ.String()
  2318  		} else {
  2319  			s = fmt.Sprintf("%s %s", formalArg.name, formalArg.typ.String())
  2320  		}
  2321  		if formalArg.isret {
  2322  			rets = append(rets, s)
  2323  		} else {
  2324  			args = append(args, s)
  2325  		}
  2326  	}
  2327  
  2328  	argstr := strings.Join(args, ", ")
  2329  	var retstr string
  2330  	switch len(rets) {
  2331  	case 0:
  2332  		retstr = ""
  2333  	case 1:
  2334  		retstr = " " + rets[0]
  2335  	default:
  2336  		retstr = " (" + strings.Join(rets, ", ") + ")"
  2337  	}
  2338  	return &godwarf.FuncType{
  2339  		CommonType: godwarf.CommonType{
  2340  			Name:        "func(" + argstr + ")" + retstr,
  2341  			ReflectKind: reflect.Func,
  2342  		},
  2343  		//TODO(aarzilli): at the moment we aren't using the ParamType and
  2344  		// ReturnType fields of FuncType anywhere (when this is returned to the
  2345  		// client it's first converted to a string and the function calling code
  2346  		// reads the subroutine entry because it needs to know the stack offsets).
  2347  		// If we start using them they should be filled here.
  2348  	}, nil
  2349  }
  2350  
  2351  func validRegisterName(s string) string {
  2352  	for len(s) > 0 && s[0] == '_' {
  2353  		s = s[1:]
  2354  	}
  2355  	for i := range s {
  2356  		if (s[i] < '0' || s[i] > '9') && (s[i] < 'A' || s[i] > 'Z') {
  2357  			return ""
  2358  		}
  2359  	}
  2360  	return s
  2361  }