1// Copyright 2018 Google Inc. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Evaluates Go expressions, using the current values of variables in a program 16// being debugged. 17// 18// TODOs: 19// More overflow checking. 20// Stricter type checking. 21// More expression types. 22 23// +build linux 24 25package server 26 27import ( 28 "errors" 29 "fmt" 30 "go/ast" 31 "go/parser" 32 "go/token" 33 "math" 34 "math/big" 35 36 "cloud.google.com/go/cmd/go-cloud-debug-agent/internal/debug" 37 "cloud.google.com/go/cmd/go-cloud-debug-agent/internal/debug/dwarf" 38) 39 40const prec = 256 // precision for untyped float and complex constants. 41 42var ( 43 // Some big.Ints to use in overflow checks. 44 bigIntMaxInt32 = big.NewInt(math.MaxInt32) 45 bigIntMinInt32 = big.NewInt(math.MinInt32) 46 bigIntMaxInt64 = big.NewInt(math.MaxInt64) 47 bigIntMinInt64 = big.NewInt(math.MinInt64) 48 bigIntMaxUint64 = new(big.Int).SetUint64(math.MaxUint64) 49) 50 51// result stores an intermediate value produced during evaluation of an expression. 52// 53// d contains the DWARF type of the value. For untyped values, d will be nil. 54// 55// v contains the value itself. For numeric and bool types, v will have the 56// corresponding predeclared Go type. 57// For untyped integer, rune, float, complex, string, and bool constants, v will 58// have type untInt, untRune, untFloat, untComplex, untString, or bool, 59// respectively. 60// For values of type int, uint and uintptr, v will be an int32, int64, uint32 61// or uint64 as appropriate. 62// For address operations, v will have type pointerToValue. 63// For the operands of address operations, v will have type addressableValue. 64// Other types are represented using the corresponding implementation of 65// debug.Value in program.go. 66// 67// If an evaluation results in an error, the zero value of result is used. 68type result struct { 69 d dwarf.Type 70 v interface{} 71} 72 73// untInt is an untyped integer constant 74type untInt struct { 75 *big.Int 76} 77 78// untRune is an untyped rune constant 79type untRune struct { 80 *big.Int 81} 82 83// untFloat is an untyped floating-point constant 84type untFloat struct { 85 *big.Float 86} 87 88// untComplex is an untyped complex constant 89type untComplex struct { 90 r *big.Float 91 i *big.Float 92} 93 94// untString is an untyped string constant 95type untString string 96 97// pointerToValue is a pointer to a value in memory. 98// The evaluator constructs these as the result of address operations like "&x". 99// Unlike debug.Pointer, the DWARF type stored alongside values of this type 100// is the type of the variable, not the type of the pointer. 101type pointerToValue struct { 102 a uint64 103} 104 105// addressableValue is the memory location of a value. 106// The evaluator constructs these while evaluating the operands of address 107// operations like "&x", instead of computing the value of x itself. 108type addressableValue struct { 109 a uint64 110} 111 112// A sliceOf is a slice created by slicing an array. 113// Unlike debug.Slice, the DWARF type stored alongside a value of this type is 114// the type of the slice's elements, not the type of the slice. 115type sliceOf debug.Slice 116 117// ident is a value for representing a special identifier. 118type ident string 119 120// identLookup is a built-in function of the expression evaluator which gets the 121// value of a global symbol. 122var identLookup ident = "lookup" 123 124// evalExpression evaluates a Go expression. 125// If the program counter and stack pointer are nonzero, they are used to determine 126// what local variables are available and where in memory they are. 127func (s *Server) evalExpression(expression string, pc, sp uint64) (debug.Value, error) { 128 e := evaluator{server: s, expression: expression, pc: pc, sp: sp} 129 node, err := parser.ParseExpr(expression) 130 if err != nil { 131 return nil, err 132 } 133 val := e.evalNode(node, false) 134 if e.evalError != nil { 135 return nil, e.evalError 136 } 137 138 // Convert untyped constants to their default types. 139 switch v := val.v.(type) { 140 case untInt: 141 return e.intFromInteger(v) 142 case untRune: 143 if v.Cmp(bigIntMaxInt32) == +1 { 144 return nil, errors.New("constant overflows rune") 145 } 146 if v.Cmp(bigIntMinInt32) == -1 { 147 return nil, errors.New("constant overflows rune") 148 } 149 return int32(v.Int64()), nil 150 case untFloat: 151 f, _ := v.Float64() 152 if math.IsInf(f, 0) { 153 return nil, errors.New("constant overflows float64") 154 } 155 if math.IsNaN(f) { 156 return nil, errors.New("constant is NaN") 157 } 158 return f, nil 159 case untComplex: 160 r, _ := v.r.Float64() 161 i, _ := v.i.Float64() 162 if math.IsInf(r, 0) || math.IsInf(i, 0) { 163 return nil, errors.New("constant overflows complex128") 164 } 165 if math.IsNaN(r) || math.IsNaN(i) { 166 return nil, errors.New("constant is NaN") 167 } 168 return complex(r, i), nil 169 case untString: 170 return debug.String{Length: uint64(len(v)), String: string(v)}, nil 171 case pointerToValue: 172 return debug.Pointer{TypeID: uint64(val.d.Common().Offset), Address: v.a}, nil 173 case sliceOf: 174 return debug.Slice(v), nil 175 case nil, addressableValue: 176 // This case should not be reachable. 177 return nil, errors.New("unknown error") 178 } 179 return val.v, nil 180} 181 182type evaluator struct { 183 // expression is the expression being evaluated. 184 expression string 185 // server interacts with the program being debugged. 186 server *Server 187 // curNode is the current parse tree node. This is set so that error messages 188 // can quote the part of the expression that caused an error. 189 curNode ast.Node 190 // evalError is the first error that occurred while evaluating the expression, 191 // or nil if no error has occurred. 192 evalError error 193 // pc and sp are the current program counter and stack pointer, used for 194 // finding local variables. If either are zero, the expression is evaluated 195 // without using local variables. 196 pc uint64 197 sp uint64 198} 199 200// setNode sets curNode, and returns curNode's previous value. 201func (e *evaluator) setNode(node ast.Node) (old ast.Node) { 202 old, e.curNode = e.curNode, node 203 return old 204} 205 206// err saves an error that occurred during evaluation. 207// It returns a zero result, so that functions can exit and set an error with 208// return e.err(...) 209func (e *evaluator) err(s string) result { 210 if e.evalError != nil { 211 return result{} 212 } 213 // Append the substring of the expression that corresponds to the current AST node. 214 start := int(e.curNode.Pos() - 1) 215 end := int(e.curNode.End() - 1) 216 if start < 0 { 217 start = 0 218 } 219 if end > len(e.expression) { 220 end = len(e.expression) 221 } 222 if start > end { 223 start, end = 0, 0 224 } 225 e.evalError = errors.New(s + `: "` + e.expression[start:end] + `"`) 226 return result{} 227} 228 229// evalNode computes the value of a node in the expression tree. 230// If getAddress is true, the node is the argument of an & operator, so evalNode 231// will return a result with a value of type addressableValue if possible. 232func (e *evaluator) evalNode(node ast.Node, getAddress bool) result { 233 // Set the current node in the evaluator, so that error messages can refer to 234 // it. Defer a function call that changes it back. 235 defer e.setNode(e.setNode(node)) 236 237 switch n := node.(type) { 238 case *ast.Ident: 239 if e.pc != 0 && e.sp != 0 { 240 a, t := e.server.findLocalVar(n.Name, e.pc, e.sp) 241 if t != nil { 242 return e.resultFrom(a, t, getAddress) 243 } 244 } 245 a, t := e.server.findGlobalVar(n.Name) 246 if t != nil { 247 return e.resultFrom(a, t, getAddress) 248 } 249 switch n.Name { 250 // Note: these could have been redefined as constants in the code, but we 251 // don't have a way to detect that. 252 case "true": 253 return result{nil, true} 254 case "false": 255 return result{nil, false} 256 case "lookup": 257 return result{nil, identLookup} 258 } 259 return e.err("unknown identifier") 260 261 case *ast.BasicLit: 262 switch n.Kind { 263 case token.INT: 264 i := new(big.Int) 265 if _, ok := i.SetString(n.Value, 0); !ok { 266 return e.err("invalid integer constant") 267 } 268 return result{nil, untInt{i}} 269 case token.FLOAT: 270 r, _, err := big.ParseFloat(n.Value, 10, prec, big.ToNearestEven) 271 if err != nil { 272 return e.err(err.Error()) 273 } 274 return result{nil, untFloat{r}} 275 case token.IMAG: 276 if len(n.Value) <= 1 || n.Value[len(n.Value)-1] != 'i' { 277 return e.err("invalid imaginary constant") 278 } 279 r, _, err := big.ParseFloat(n.Value[:len(n.Value)-1], 10, prec, big.ToNearestEven) 280 if err != nil { 281 return e.err(err.Error()) 282 } 283 return result{nil, untComplex{new(big.Float), r}} 284 case token.CHAR: 285 // TODO: unescaping 286 return result{nil, untRune{new(big.Int).SetInt64(int64(n.Value[1]))}} 287 case token.STRING: 288 // TODO: unescaping 289 if len(n.Value) <= 1 { 290 return e.err("invalid string constant") 291 } 292 return result{nil, untString(n.Value[1 : len(n.Value)-1])} 293 } 294 295 case *ast.ParenExpr: 296 return e.evalNode(n.X, getAddress) 297 298 case *ast.StarExpr: 299 x := e.evalNode(n.X, false) 300 switch v := x.v.(type) { 301 case debug.Pointer: 302 // x.d may be a typedef pointing to a pointer type (or a typedef pointing 303 // to a typedef pointing to a pointer type, etc.), so remove typedefs 304 // until we get the underlying pointer type. 305 t := followTypedefs(x.d) 306 if pt, ok := t.(*dwarf.PtrType); ok { 307 return e.resultFrom(v.Address, pt.Type, getAddress) 308 } else { 309 return e.err("invalid DWARF type for pointer") 310 } 311 case pointerToValue: 312 return e.resultFrom(v.a, x.d, getAddress) 313 case nil: 314 return x 315 } 316 return e.err("invalid indirect") 317 318 case *ast.SelectorExpr: 319 x := e.evalNode(n.X, false) 320 sel := n.Sel.Name 321 switch v := x.v.(type) { 322 case debug.Struct: 323 for _, f := range v.Fields { 324 if f.Name == sel { 325 t, err := e.server.dwarfData.Type(dwarf.Offset(f.Var.TypeID)) 326 if err != nil { 327 return e.err(err.Error()) 328 } 329 return e.resultFrom(f.Var.Address, t, getAddress) 330 } 331 } 332 return e.err("struct field not found") 333 case debug.Pointer: 334 pt, ok := followTypedefs(x.d).(*dwarf.PtrType) // x.d should be a pointer to struct. 335 if !ok { 336 return e.err("invalid DWARF information for pointer") 337 } 338 st, ok := followTypedefs(pt.Type).(*dwarf.StructType) 339 if !ok { 340 break 341 } 342 for _, f := range st.Field { 343 if f.Name == sel { 344 return e.resultFrom(v.Address+uint64(f.ByteOffset), f.Type, getAddress) 345 } 346 } 347 return e.err("struct field not found") 348 case pointerToValue: 349 st, ok := followTypedefs(x.d).(*dwarf.StructType) // x.d should be a struct. 350 if !ok { 351 break 352 } 353 for _, f := range st.Field { 354 if f.Name == sel { 355 return e.resultFrom(v.a+uint64(f.ByteOffset), f.Type, getAddress) 356 } 357 } 358 return e.err("struct field not found") 359 } 360 return e.err("invalid selector expression") 361 362 case *ast.IndexExpr: 363 x, index := e.evalNode(n.X, false), e.evalNode(n.Index, false) 364 if x.v == nil || index.v == nil { 365 return result{} 366 } 367 // The expression is x[index] 368 if m, ok := x.v.(debug.Map); ok { 369 if getAddress { 370 return e.err("can't take address of map value") 371 } 372 mt, ok := followTypedefs(x.d).(*dwarf.MapType) 373 if !ok { 374 return e.err("invalid DWARF type for map") 375 } 376 var ( 377 found bool // true if the key was found 378 value result // the map value for the key 379 abort bool // true if an error occurred while searching 380 // fn is a function that checks if one (key, value) pair corresponds 381 // to the index in the expression. 382 fn = func(keyAddr, valAddr uint64, keyType, valType dwarf.Type) bool { 383 key := e.resultFrom(keyAddr, keyType, false) 384 if key.v == nil { 385 abort = true 386 return false // stop searching map 387 } 388 equal, ok := e.evalBinaryOp(token.EQL, index, key).v.(bool) 389 if !ok { 390 abort = true 391 return false // stop searching map 392 } 393 if equal { 394 found = true 395 value = e.resultFrom(valAddr, valType, false) 396 return false // stop searching map 397 } 398 return true // continue searching map 399 } 400 ) 401 if err := e.server.peekMapValues(mt, m.Address, fn); err != nil { 402 return e.err(err.Error()) 403 } 404 if abort { 405 // Some operation on individual map keys failed. 406 return result{} 407 } 408 if found { 409 return value 410 } 411 // The key wasn't in the map; return the zero value. 412 return e.zero(mt.ElemType) 413 } 414 415 // The index should be a non-negative integer for the remaining cases. 416 u, err := uint64FromResult(index) 417 if err != nil { 418 return e.err("invalid index: " + err.Error()) 419 } 420 switch v := x.v.(type) { 421 case debug.Array: 422 if u >= v.Length { 423 return e.err("array index out of bounds") 424 } 425 elemType, err := e.server.dwarfData.Type(dwarf.Offset(v.ElementTypeID)) 426 if err != nil { 427 return e.err(err.Error()) 428 } 429 return e.resultFrom(v.Element(u).Address, elemType, getAddress) 430 case debug.Slice: 431 if u >= v.Length { 432 return e.err("slice index out of bounds") 433 } 434 elemType, err := e.server.dwarfData.Type(dwarf.Offset(v.ElementTypeID)) 435 if err != nil { 436 return e.err(err.Error()) 437 } 438 return e.resultFrom(v.Element(u).Address, elemType, getAddress) 439 case sliceOf: 440 if u >= v.Length { 441 return e.err("slice index out of bounds") 442 } 443 return e.resultFrom(v.Element(u).Address, x.d, getAddress) 444 case debug.String: 445 if getAddress { 446 return e.err("can't take address of string element") 447 } 448 if u >= v.Length { 449 return e.err("string index out of bounds") 450 } 451 if u >= uint64(len(v.String)) { 452 return e.err("string element unavailable") 453 } 454 return e.uint8Result(v.String[u]) 455 case untString: 456 if getAddress { 457 return e.err("can't take address of string element") 458 } 459 if u >= uint64(len(v)) { 460 return e.err("string index out of bounds") 461 } 462 return e.uint8Result(v[u]) 463 } 464 return e.err("invalid index expression") 465 466 case *ast.SliceExpr: 467 if n.Slice3 && n.High == nil { 468 return e.err("middle index required in full slice") 469 } 470 if n.Slice3 && n.Max == nil { 471 return e.err("final index required in full slice") 472 } 473 var ( 474 low, high, max uint64 475 err error 476 ) 477 if n.Low != nil { 478 low, err = uint64FromResult(e.evalNode(n.Low, false)) 479 if err != nil { 480 return e.err("invalid slice lower bound: " + err.Error()) 481 } 482 } 483 if n.High != nil { 484 high, err = uint64FromResult(e.evalNode(n.High, false)) 485 if err != nil { 486 return e.err("invalid slice upper bound: " + err.Error()) 487 } 488 } 489 if n.Max != nil { 490 max, err = uint64FromResult(e.evalNode(n.Max, false)) 491 if err != nil { 492 return e.err("invalid slice capacity: " + err.Error()) 493 } 494 } 495 x := e.evalNode(n.X, false) 496 switch v := x.v.(type) { 497 case debug.Array, debug.Pointer, pointerToValue: 498 // This case handles the slicing of arrays and pointers to arrays. 499 var arr debug.Array 500 switch v := x.v.(type) { 501 case debug.Array: 502 arr = v 503 case debug.Pointer: 504 pt, ok := followTypedefs(x.d).(*dwarf.PtrType) 505 if !ok { 506 return e.err("invalid DWARF type for pointer") 507 } 508 a := e.resultFrom(v.Address, pt.Type, false) 509 arr, ok = a.v.(debug.Array) 510 if !ok { 511 // v is a pointer to something other than an array. 512 return e.err("cannot slice pointer") 513 } 514 case pointerToValue: 515 a := e.resultFrom(v.a, x.d, false) 516 var ok bool 517 arr, ok = a.v.(debug.Array) 518 if !ok { 519 // v is a pointer to something other than an array. 520 return e.err("cannot slice pointer") 521 } 522 } 523 elemType, err := e.server.dwarfData.Type(dwarf.Offset(arr.ElementTypeID)) 524 if err != nil { 525 return e.err(err.Error()) 526 } 527 if n.High == nil { 528 high = arr.Length 529 } else if high > arr.Length { 530 return e.err("slice upper bound is too large") 531 } 532 if n.Max == nil { 533 max = arr.Length 534 } else if max > arr.Length { 535 return e.err("slice capacity is too large") 536 } 537 if low > high || high > max { 538 return e.err("invalid slice index") 539 } 540 return result{ 541 d: elemType, 542 v: sliceOf{ 543 Array: debug.Array{ 544 ElementTypeID: arr.ElementTypeID, 545 Address: arr.Element(low).Address, 546 Length: high - low, 547 StrideBits: uint64(elemType.Common().ByteSize) * 8, 548 }, 549 Capacity: max - low, 550 }, 551 } 552 case debug.Slice: 553 if n.High == nil { 554 high = v.Length 555 } else if high > v.Capacity { 556 return e.err("slice upper bound is too large") 557 } 558 if n.Max == nil { 559 max = v.Capacity 560 } else if max > v.Capacity { 561 return e.err("slice capacity is too large") 562 } 563 if low > high || high > max { 564 return e.err("invalid slice index") 565 } 566 v.Address += low * (v.StrideBits / 8) 567 v.Length = high - low 568 v.Capacity = max - low 569 return result{x.d, v} 570 case sliceOf: 571 if n.High == nil { 572 high = v.Length 573 } else if high > v.Capacity { 574 return e.err("slice upper bound is too large") 575 } 576 if n.Max == nil { 577 max = v.Capacity 578 } else if max > v.Capacity { 579 return e.err("slice capacity is too large") 580 } 581 if low > high || high > max { 582 return e.err("invalid slice index") 583 } 584 v.Address += low * (v.StrideBits / 8) 585 v.Length = high - low 586 v.Capacity = max - low 587 return result{x.d, v} 588 case debug.String: 589 if n.Max != nil { 590 return e.err("full slice of string") 591 } 592 if n.High == nil { 593 high = v.Length 594 } 595 if low > high || high > v.Length { 596 return e.err("invalid slice index") 597 } 598 v.Length = high - low 599 if low > uint64(len(v.String)) { 600 // v.String was truncated before the point where this slice starts. 601 v.String = "" 602 } else { 603 if high > uint64(len(v.String)) { 604 // v.String was truncated before the point where this slice ends. 605 high = uint64(len(v.String)) 606 } 607 v.String = v.String[low:high] 608 } 609 return result{x.d, v} 610 case untString: 611 if n.Max != nil { 612 return e.err("full slice of string") 613 } 614 if n.High == nil { 615 high = uint64(len(v)) 616 } 617 if low > high { 618 return e.err("invalid slice expression") 619 } 620 if high > uint64(len(v)) { 621 return e.err("slice upper bound is too large") 622 } 623 return e.stringResult(string(v[low:high])) 624 default: 625 return e.err("invalid slice expression") 626 } 627 628 case *ast.CallExpr: 629 // Only supports lookup("x"), which gets the value of a global symbol x. 630 fun := e.evalNode(n.Fun, false) 631 var args []result 632 for _, a := range n.Args { 633 args = append(args, e.evalNode(a, false)) 634 } 635 if fun.v == identLookup { 636 if len(args) != 1 { 637 return e.err("lookup should have one argument") 638 } 639 ident, ok := args[0].v.(untString) 640 if !ok { 641 return e.err("argument for lookup should be a string constant") 642 } 643 if a, t := e.server.findGlobalVar(string(ident)); t == nil { 644 return e.err("symbol not found") 645 } else { 646 return e.resultFrom(a, t, getAddress) 647 } 648 } 649 return e.err("function calls not implemented") 650 651 case *ast.UnaryExpr: 652 if n.Op == token.AND { 653 x := e.evalNode(n.X, true) 654 switch v := x.v.(type) { 655 case addressableValue: 656 return result{x.d, pointerToValue{v.a}} 657 case nil: 658 return x 659 } 660 return e.err("can't take address") 661 } 662 663 x := e.evalNode(n.X, false) 664 if x.v == nil { 665 return x 666 } 667 switch v := x.v.(type) { 668 669 case int8: 670 switch n.Op { 671 case token.ADD: 672 case token.SUB: 673 v = -v 674 case token.XOR: 675 v = ^v 676 default: 677 return e.err("invalid operation") 678 } 679 return result{x.d, v} 680 681 case int16: 682 switch n.Op { 683 case token.ADD: 684 case token.SUB: 685 v = -v 686 case token.XOR: 687 v = ^v 688 default: 689 return e.err("invalid operation") 690 } 691 return result{x.d, v} 692 693 case int32: 694 switch n.Op { 695 case token.ADD: 696 case token.SUB: 697 v = -v 698 case token.XOR: 699 v = ^v 700 default: 701 return e.err("invalid operation") 702 } 703 return result{x.d, v} 704 705 case int64: 706 switch n.Op { 707 case token.ADD: 708 case token.SUB: 709 v = -v 710 case token.XOR: 711 v = ^v 712 default: 713 return e.err("invalid operation") 714 } 715 return result{x.d, v} 716 717 case uint8: 718 switch n.Op { 719 case token.ADD: 720 case token.SUB: 721 v = -v 722 case token.XOR: 723 v = ^v 724 default: 725 return e.err("invalid operation") 726 } 727 return result{x.d, v} 728 729 case uint16: 730 switch n.Op { 731 case token.ADD: 732 case token.SUB: 733 v = -v 734 case token.XOR: 735 v = ^v 736 default: 737 return e.err("invalid operation") 738 } 739 return result{x.d, v} 740 741 case uint32: 742 switch n.Op { 743 case token.ADD: 744 case token.SUB: 745 v = -v 746 case token.XOR: 747 v = ^v 748 default: 749 return e.err("invalid operation") 750 } 751 return result{x.d, v} 752 753 case uint64: 754 switch n.Op { 755 case token.ADD: 756 case token.SUB: 757 v = -v 758 case token.XOR: 759 v = ^v 760 default: 761 return e.err("invalid operation") 762 } 763 return result{x.d, v} 764 765 case float32: 766 switch n.Op { 767 case token.ADD: 768 case token.SUB: 769 v = -v 770 default: 771 return e.err("invalid operation") 772 } 773 return result{x.d, v} 774 775 case float64: 776 switch n.Op { 777 case token.ADD: 778 case token.SUB: 779 v = -v 780 default: 781 return e.err("invalid operation") 782 } 783 return result{x.d, v} 784 785 case complex64: 786 switch n.Op { 787 case token.ADD: 788 case token.SUB: 789 v = -v 790 default: 791 return e.err("invalid operation") 792 } 793 return result{x.d, v} 794 795 case complex128: 796 switch n.Op { 797 case token.ADD: 798 case token.SUB: 799 v = -v 800 default: 801 return e.err("invalid operation") 802 } 803 return result{x.d, v} 804 805 case untInt: 806 switch n.Op { 807 case token.ADD: 808 case token.SUB: 809 v.Int.Neg(v.Int) 810 case token.XOR: 811 v.Int.Not(v.Int) 812 default: 813 return e.err("invalid operation") 814 } 815 return result{x.d, v} 816 817 case untRune: 818 switch n.Op { 819 case token.ADD: 820 case token.SUB: 821 v.Int.Neg(v.Int) 822 case token.XOR: 823 v.Int.Not(v.Int) 824 default: 825 return e.err("invalid operation") 826 } 827 return result{x.d, v} 828 829 case untFloat: 830 switch n.Op { 831 case token.ADD: 832 case token.SUB: 833 v.Float.Neg(v.Float) 834 default: 835 return e.err("invalid operation") 836 } 837 return result{x.d, v} 838 839 case untComplex: 840 switch n.Op { 841 case token.ADD: 842 case token.SUB: 843 v.r.Neg(v.r) 844 v.i.Neg(v.i) 845 default: 846 return e.err("invalid operation") 847 } 848 return result{x.d, v} 849 850 case bool: 851 switch n.Op { 852 case token.NOT: 853 v = !v 854 default: 855 return e.err("invalid operation") 856 } 857 return result{x.d, v} 858 } 859 860 case *ast.BinaryExpr: 861 x := e.evalNode(n.X, false) 862 if x.v == nil { 863 return x 864 } 865 y := e.evalNode(n.Y, false) 866 if y.v == nil { 867 return y 868 } 869 return e.evalBinaryOp(n.Op, x, y) 870 } 871 return e.err("invalid expression") 872} 873 874// evalBinaryOp evaluates a binary operator op applied to x and y. 875func (e *evaluator) evalBinaryOp(op token.Token, x, y result) result { 876 if op == token.NEQ { 877 tmp := e.evalBinaryOp(token.EQL, x, y) 878 b, ok := tmp.v.(bool) 879 if !ok { 880 return tmp 881 } 882 return result{nil, !b} 883 } 884 if op == token.GTR { 885 return e.evalBinaryOp(token.LSS, y, x) 886 } 887 if op == token.GEQ { 888 return e.evalBinaryOp(token.LEQ, x, y) 889 } 890 891 x = convertUntyped(x, y) 892 y = convertUntyped(y, x) 893 894 switch a := x.v.(type) { 895 896 case int8: 897 b, ok := y.v.(int8) 898 if !ok { 899 return e.err("type mismatch") 900 } 901 var c int8 902 switch op { 903 case token.EQL: 904 return result{nil, a == b} 905 case token.LSS: 906 return result{nil, a < b} 907 case token.LEQ: 908 return result{nil, a <= b} 909 case token.ADD: 910 c = a + b 911 case token.SUB: 912 c = a - b 913 case token.OR: 914 c = a | b 915 case token.XOR: 916 c = a ^ b 917 case token.MUL: 918 c = a * b 919 case token.QUO: 920 if b == 0 { 921 return e.err("integer divide by zero") 922 } 923 c = a / b 924 case token.REM: 925 if b == 0 { 926 return e.err("integer divide by zero") 927 } 928 c = a % b 929 case token.AND: 930 c = a & b 931 case token.AND_NOT: 932 c = a &^ b 933 default: 934 return e.err("invalid operation") 935 } 936 return result{x.d, c} 937 938 case int16: 939 b, ok := y.v.(int16) 940 if !ok { 941 return e.err("type mismatch") 942 } 943 var c int16 944 switch op { 945 case token.EQL: 946 return result{nil, a == b} 947 case token.LSS: 948 return result{nil, a < b} 949 case token.LEQ: 950 return result{nil, a <= b} 951 case token.ADD: 952 c = a + b 953 case token.SUB: 954 c = a - b 955 case token.OR: 956 c = a | b 957 case token.XOR: 958 c = a ^ b 959 case token.MUL: 960 c = a * b 961 case token.QUO: 962 if b == 0 { 963 return e.err("integer divide by zero") 964 } 965 c = a / b 966 case token.REM: 967 if b == 0 { 968 return e.err("integer divide by zero") 969 } 970 c = a % b 971 case token.AND: 972 c = a & b 973 case token.AND_NOT: 974 c = a &^ b 975 default: 976 return e.err("invalid operation") 977 } 978 return result{x.d, c} 979 980 case int32: 981 b, ok := y.v.(int32) 982 if !ok { 983 return e.err("type mismatch") 984 } 985 var c int32 986 switch op { 987 case token.EQL: 988 return result{nil, a == b} 989 case token.LSS: 990 return result{nil, a < b} 991 case token.LEQ: 992 return result{nil, a <= b} 993 case token.ADD: 994 c = a + b 995 case token.SUB: 996 c = a - b 997 case token.OR: 998 c = a | b 999 case token.XOR: 1000 c = a ^ b 1001 case token.MUL: 1002 c = a * b 1003 case token.QUO: 1004 if b == 0 { 1005 return e.err("integer divide by zero") 1006 } 1007 c = a / b 1008 case token.REM: 1009 if b == 0 { 1010 return e.err("integer divide by zero") 1011 } 1012 c = a % b 1013 case token.AND: 1014 c = a & b 1015 case token.AND_NOT: 1016 c = a &^ b 1017 default: 1018 return e.err("invalid operation") 1019 } 1020 return result{x.d, c} 1021 1022 case int64: 1023 b, ok := y.v.(int64) 1024 if !ok { 1025 return e.err("type mismatch") 1026 } 1027 var c int64 1028 switch op { 1029 case token.EQL: 1030 return result{nil, a == b} 1031 case token.LSS: 1032 return result{nil, a < b} 1033 case token.LEQ: 1034 return result{nil, a <= b} 1035 case token.ADD: 1036 c = a + b 1037 case token.SUB: 1038 c = a - b 1039 case token.OR: 1040 c = a | b 1041 case token.XOR: 1042 c = a ^ b 1043 case token.MUL: 1044 c = a * b 1045 case token.QUO: 1046 if b == 0 { 1047 return e.err("integer divide by zero") 1048 } 1049 c = a / b 1050 case token.REM: 1051 if b == 0 { 1052 return e.err("integer divide by zero") 1053 } 1054 c = a % b 1055 case token.AND: 1056 c = a & b 1057 case token.AND_NOT: 1058 c = a &^ b 1059 default: 1060 return e.err("invalid operation") 1061 } 1062 return result{x.d, c} 1063 1064 case uint8: 1065 b, ok := y.v.(uint8) 1066 if !ok { 1067 return e.err("type mismatch") 1068 } 1069 var c uint8 1070 switch op { 1071 case token.EQL: 1072 return result{nil, a == b} 1073 case token.LSS: 1074 return result{nil, a < b} 1075 case token.LEQ: 1076 return result{nil, a <= b} 1077 case token.ADD: 1078 c = a + b 1079 case token.SUB: 1080 c = a - b 1081 case token.OR: 1082 c = a | b 1083 case token.XOR: 1084 c = a ^ b 1085 case token.MUL: 1086 c = a * b 1087 case token.QUO: 1088 if b == 0 { 1089 return e.err("integer divide by zero") 1090 } 1091 c = a / b 1092 case token.REM: 1093 if b == 0 { 1094 return e.err("integer divide by zero") 1095 } 1096 c = a % b 1097 case token.AND: 1098 c = a & b 1099 case token.AND_NOT: 1100 c = a &^ b 1101 default: 1102 return e.err("invalid operation") 1103 } 1104 return result{x.d, c} 1105 1106 case uint16: 1107 b, ok := y.v.(uint16) 1108 if !ok { 1109 return e.err("type mismatch") 1110 } 1111 var c uint16 1112 switch op { 1113 case token.EQL: 1114 return result{nil, a == b} 1115 case token.LSS: 1116 return result{nil, a < b} 1117 case token.LEQ: 1118 return result{nil, a <= b} 1119 case token.ADD: 1120 c = a + b 1121 case token.SUB: 1122 c = a - b 1123 case token.OR: 1124 c = a | b 1125 case token.XOR: 1126 c = a ^ b 1127 case token.MUL: 1128 c = a * b 1129 case token.QUO: 1130 if b == 0 { 1131 return e.err("integer divide by zero") 1132 } 1133 c = a / b 1134 case token.REM: 1135 if b == 0 { 1136 return e.err("integer divide by zero") 1137 } 1138 c = a % b 1139 case token.AND: 1140 c = a & b 1141 case token.AND_NOT: 1142 c = a &^ b 1143 default: 1144 return e.err("invalid operation") 1145 } 1146 return result{x.d, c} 1147 1148 case uint32: 1149 b, ok := y.v.(uint32) 1150 if !ok { 1151 return e.err("type mismatch") 1152 } 1153 var c uint32 1154 switch op { 1155 case token.EQL: 1156 return result{nil, a == b} 1157 case token.LSS: 1158 return result{nil, a < b} 1159 case token.LEQ: 1160 return result{nil, a <= b} 1161 case token.ADD: 1162 c = a + b 1163 case token.SUB: 1164 c = a - b 1165 case token.OR: 1166 c = a | b 1167 case token.XOR: 1168 c = a ^ b 1169 case token.MUL: 1170 c = a * b 1171 case token.QUO: 1172 if b == 0 { 1173 return e.err("integer divide by zero") 1174 } 1175 c = a / b 1176 case token.REM: 1177 if b == 0 { 1178 return e.err("integer divide by zero") 1179 } 1180 c = a % b 1181 case token.AND: 1182 c = a & b 1183 case token.AND_NOT: 1184 c = a &^ b 1185 default: 1186 return e.err("invalid operation") 1187 } 1188 return result{x.d, c} 1189 1190 case uint64: 1191 b, ok := y.v.(uint64) 1192 if !ok { 1193 return e.err("type mismatch") 1194 } 1195 var c uint64 1196 switch op { 1197 case token.EQL: 1198 return result{nil, a == b} 1199 case token.LSS: 1200 return result{nil, a < b} 1201 case token.LEQ: 1202 return result{nil, a <= b} 1203 case token.ADD: 1204 c = a + b 1205 case token.SUB: 1206 c = a - b 1207 case token.OR: 1208 c = a | b 1209 case token.XOR: 1210 c = a ^ b 1211 case token.MUL: 1212 c = a * b 1213 case token.QUO: 1214 if b == 0 { 1215 return e.err("integer divide by zero") 1216 } 1217 c = a / b 1218 case token.REM: 1219 if b == 0 { 1220 return e.err("integer divide by zero") 1221 } 1222 c = a % b 1223 case token.AND: 1224 c = a & b 1225 case token.AND_NOT: 1226 c = a &^ b 1227 default: 1228 return e.err("invalid operation") 1229 } 1230 return result{x.d, c} 1231 1232 case float32: 1233 b, ok := y.v.(float32) 1234 if !ok { 1235 return e.err("type mismatch") 1236 } 1237 var c float32 1238 switch op { 1239 case token.EQL: 1240 return result{nil, a == b} 1241 case token.LSS: 1242 return result{nil, a < b} 1243 case token.LEQ: 1244 return result{nil, a <= b} 1245 case token.ADD: 1246 c = a + b 1247 case token.SUB: 1248 c = a - b 1249 case token.MUL: 1250 c = a * b 1251 case token.QUO: 1252 c = a / b 1253 default: 1254 return e.err("invalid operation") 1255 } 1256 return result{x.d, c} 1257 1258 case float64: 1259 b, ok := y.v.(float64) 1260 if !ok { 1261 return e.err("type mismatch") 1262 } 1263 var c float64 1264 switch op { 1265 case token.EQL: 1266 return result{nil, a == b} 1267 case token.LSS: 1268 return result{nil, a < b} 1269 case token.LEQ: 1270 return result{nil, a <= b} 1271 case token.ADD: 1272 c = a + b 1273 case token.SUB: 1274 c = a - b 1275 case token.MUL: 1276 c = a * b 1277 case token.QUO: 1278 c = a / b 1279 default: 1280 return e.err("invalid operation") 1281 } 1282 return result{x.d, c} 1283 1284 case complex64: 1285 b, ok := y.v.(complex64) 1286 if !ok { 1287 return e.err("type mismatch") 1288 } 1289 var c complex64 1290 switch op { 1291 case token.EQL: 1292 return result{nil, a == b} 1293 case token.ADD: 1294 c = a + b 1295 case token.SUB: 1296 c = a - b 1297 case token.MUL: 1298 c = a * b 1299 case token.QUO: 1300 c = a / b 1301 default: 1302 return e.err("invalid operation") 1303 } 1304 return result{x.d, c} 1305 1306 case complex128: 1307 b, ok := y.v.(complex128) 1308 if !ok { 1309 return e.err("type mismatch") 1310 } 1311 var c complex128 1312 switch op { 1313 case token.EQL: 1314 return result{nil, a == b} 1315 case token.ADD: 1316 c = a + b 1317 case token.SUB: 1318 c = a - b 1319 case token.MUL: 1320 c = a * b 1321 case token.QUO: 1322 c = a / b 1323 default: 1324 return e.err("invalid operation") 1325 } 1326 return result{x.d, c} 1327 1328 case bool: 1329 b, ok := y.v.(bool) 1330 if !ok { 1331 return e.err("type mismatch") 1332 } 1333 var c bool 1334 switch op { 1335 case token.LOR: 1336 c = a || b 1337 case token.LAND: 1338 c = a && b 1339 case token.EQL: 1340 c = a == b 1341 default: 1342 return e.err("invalid operation") 1343 } 1344 return result{x.d, c} 1345 1346 case debug.String: 1347 b, ok := y.v.(debug.String) 1348 if !ok { 1349 return e.err("type mismatch") 1350 } 1351 var c debug.String 1352 switch op { 1353 // TODO: these comparison operators only use the part of the string that 1354 // was read. Very large strings do not have their entire contents read by 1355 // server.value. 1356 case token.EQL: 1357 return result{nil, a.Length == b.Length && a.String == b.String} 1358 case token.LSS: 1359 return result{nil, a.String < b.String} 1360 case token.LEQ: 1361 return result{nil, a.String <= b.String} 1362 case token.ADD: 1363 c.Length = a.Length + b.Length 1364 if a.Length == uint64(len(a.String)) { 1365 c.String = a.String + b.String 1366 } else { 1367 // The first string was truncated at a.Length characters, so the sum 1368 // must be truncated there too. 1369 c.String = a.String 1370 } 1371 default: 1372 return e.err("invalid operation") 1373 } 1374 return result{x.d, c} 1375 1376 case untString: 1377 b, ok := y.v.(untString) 1378 if !ok { 1379 return e.err("type mismatch") 1380 } 1381 var c untString 1382 switch op { 1383 case token.EQL: 1384 return result{nil, a == b} 1385 case token.LSS: 1386 return result{nil, a < b} 1387 case token.LEQ: 1388 return result{nil, a <= b} 1389 case token.ADD: 1390 c = a + b 1391 default: 1392 return e.err("invalid operation") 1393 } 1394 return result{x.d, c} 1395 1396 case untInt: 1397 i := a.Int 1398 b, ok := y.v.(untInt) 1399 if !ok { 1400 return e.err("type mismatch") 1401 } 1402 switch op { 1403 case token.EQL: 1404 return result{nil, i.Cmp(b.Int) == 0} 1405 case token.LSS: 1406 return result{nil, i.Cmp(b.Int) < 0} 1407 case token.LEQ: 1408 return result{nil, i.Cmp(b.Int) <= 0} 1409 } 1410 c := new(big.Int) 1411 switch op { 1412 case token.ADD: 1413 c.Add(i, b.Int) 1414 case token.SUB: 1415 c.Sub(i, b.Int) 1416 case token.OR: 1417 c.Or(i, b.Int) 1418 case token.XOR: 1419 c.Xor(i, b.Int) 1420 case token.MUL: 1421 c.Mul(i, b.Int) 1422 case token.QUO: 1423 if b.Sign() == 0 { 1424 return e.err("integer divide by zero") 1425 } 1426 c.Quo(i, b.Int) 1427 case token.REM: 1428 if b.Sign() == 0 { 1429 return e.err("integer divide by zero") 1430 } 1431 c.Mod(i, b.Int) 1432 case token.AND: 1433 c.And(i, b.Int) 1434 case token.AND_NOT: 1435 c.AndNot(i, b.Int) 1436 default: 1437 return e.err("invalid operation") 1438 } 1439 return result{nil, untInt{c}} 1440 1441 case untRune: 1442 i := a.Int 1443 b, ok := y.v.(untRune) 1444 if !ok { 1445 return e.err("type mismatch") 1446 } 1447 switch op { 1448 case token.EQL: 1449 return result{nil, i.Cmp(b.Int) == 0} 1450 case token.LSS: 1451 return result{nil, i.Cmp(b.Int) < 0} 1452 case token.LEQ: 1453 return result{nil, i.Cmp(b.Int) <= 0} 1454 } 1455 c := new(big.Int) 1456 switch op { 1457 case token.ADD: 1458 c.Add(i, b.Int) 1459 case token.SUB: 1460 c.Sub(i, b.Int) 1461 case token.OR: 1462 c.Or(i, b.Int) 1463 case token.XOR: 1464 c.Xor(i, b.Int) 1465 case token.MUL: 1466 c.Mul(i, b.Int) 1467 case token.QUO: 1468 if b.Sign() == 0 { 1469 return e.err("integer divide by zero") 1470 } 1471 c.Quo(i, b.Int) 1472 case token.REM: 1473 if b.Sign() == 0 { 1474 return e.err("integer divide by zero") 1475 } 1476 c.Mod(i, b.Int) 1477 case token.AND: 1478 c.And(i, b.Int) 1479 case token.AND_NOT: 1480 c.AndNot(i, b.Int) 1481 default: 1482 return e.err("invalid operation") 1483 } 1484 return result{nil, untRune{c}} 1485 1486 case untFloat: 1487 r := a.Float 1488 b, ok := y.v.(untFloat) 1489 if !ok { 1490 return e.err("type mismatch") 1491 } 1492 switch op { 1493 case token.EQL: 1494 return result{nil, r.Cmp(b.Float) == 0} 1495 case token.LSS: 1496 return result{nil, r.Cmp(b.Float) < 0} 1497 case token.LEQ: 1498 return result{nil, r.Cmp(b.Float) <= 0} 1499 } 1500 c := new(big.Float) 1501 switch op { 1502 case token.ADD: 1503 c.Add(r, b.Float) 1504 case token.SUB: 1505 c.Sub(r, b.Float) 1506 case token.MUL: 1507 c.Mul(r, b.Float) 1508 case token.QUO: 1509 if b.Sign() == 0 { 1510 return e.err("divide by zero") 1511 } 1512 c.Quo(r, b.Float) 1513 default: 1514 return e.err("invalid operation") 1515 } 1516 return result{nil, untFloat{c}} 1517 1518 case untComplex: 1519 b, ok := y.v.(untComplex) 1520 if !ok { 1521 return e.err("type mismatch") 1522 } 1523 var ( 1524 ar = a.r 1525 br = b.r 1526 ai = a.i 1527 bi = b.i 1528 ) 1529 if op == token.EQL { 1530 return result{nil, ar.Cmp(br) == 0 && ai.Cmp(bi) == 0} 1531 } 1532 var ( 1533 cr = new(big.Float) 1534 ci = new(big.Float) 1535 ) 1536 switch op { 1537 case token.ADD: 1538 cr.Add(ar, br) 1539 ci.Add(ai, bi) 1540 case token.SUB: 1541 cr.Sub(ar, br) 1542 ci.Sub(ai, bi) 1543 case token.MUL: 1544 var t0, t1 big.Float 1545 t0.Mul(ar, br) 1546 t1.Mul(ai, bi) 1547 cr.Sub(&t0, &t1) 1548 t0.Mul(ar, bi) 1549 t1.Mul(ai, br) 1550 ci.Add(&t0, &t1) 1551 case token.QUO: 1552 // a/b = a*conj(b)/|b|^2 1553 var t0, t1 big.Float 1554 cr.Mul(ar, br) 1555 t0.Mul(ai, bi) 1556 cr.Add(cr, &t0) // cr = Re(a*conj(b)) 1557 ci.Mul(ai, br) 1558 t0.Mul(ar, bi) 1559 ci.Sub(ci, &t0) // ci = Im(a*conj(b)) 1560 t0.Mul(br, br) 1561 t1.Mul(bi, bi) 1562 t0.Add(&t0, &t1) // t0 = |b|^2 1563 if t0.Sign() == 0 { 1564 return e.err("divide by zero") 1565 } 1566 cr.Quo(cr, &t0) // cr = Re(a*conj(b))/|b|^2 = Re(a/b) 1567 ci.Quo(ci, &t0) // ci = Im(a*conj(b))/|b|^2 = Im(a/b) 1568 } 1569 return result{nil, untComplex{cr, ci}} 1570 } 1571 1572 return e.err("invalid operation") 1573} 1574 1575// findLocalVar finds a local variable (or function parameter) by name, and 1576// returns its address and DWARF type. It returns a nil type on failure. 1577// The PC and SP are used to determine the current function and stack frame. 1578func (s *Server) findLocalVar(name string, pc, sp uint64) (uint64, dwarf.Type) { 1579 // Find the DWARF entry for the function at pc. 1580 funcEntry, _, err := s.dwarfData.PCToFunction(uint64(pc)) 1581 if err != nil { 1582 return 0, nil 1583 } 1584 1585 // Compute the stack frame pointer. 1586 fpOffset, err := s.dwarfData.PCToSPOffset(uint64(pc)) 1587 if err != nil { 1588 return 0, nil 1589 } 1590 framePointer := sp + uint64(fpOffset) 1591 1592 // Check each child of the function's DWARF entry to see if it is a parameter 1593 // or local variable with the right name. If so, return its address and type. 1594 r := s.dwarfData.Reader() 1595 r.Seek(funcEntry.Offset) 1596 for { 1597 varEntry, err := r.Next() 1598 if err != nil { 1599 break 1600 } 1601 if varEntry.Tag == 0 { 1602 // This tag marks the end of the function's DWARF entry's children. 1603 break 1604 } 1605 1606 // Check this entry corresponds to a local variable or function parameter, 1607 // that it has the correct name, and that we can get its type and location. 1608 // If so, return them. 1609 if varEntry.Tag != dwarf.TagFormalParameter && varEntry.Tag != dwarf.TagVariable { 1610 continue 1611 } 1612 varName, ok := varEntry.Val(dwarf.AttrName).(string) 1613 if !ok { 1614 continue 1615 } 1616 if varName != name { 1617 continue 1618 } 1619 varTypeOffset, ok := varEntry.Val(dwarf.AttrType).(dwarf.Offset) 1620 if !ok { 1621 continue 1622 } 1623 varType, err := s.dwarfData.Type(varTypeOffset) 1624 if err != nil { 1625 continue 1626 } 1627 locationAttribute := varEntry.Val(dwarf.AttrLocation) 1628 if locationAttribute == nil { 1629 continue 1630 } 1631 locationDescription, ok := locationAttribute.([]uint8) 1632 if !ok { 1633 continue 1634 } 1635 frameOffset, err := evalLocation(locationDescription) 1636 if err != nil { 1637 continue 1638 } 1639 return framePointer + uint64(frameOffset), varType 1640 } 1641 1642 return 0, nil 1643} 1644 1645// findGlobalVar finds a global variable by name, and returns its address and 1646// DWARF type. It returns a nil type on failure. 1647func (s *Server) findGlobalVar(name string) (uint64, dwarf.Type) { 1648 entry, err := s.dwarfData.LookupVariable(name) 1649 if err != nil { 1650 return 0, nil 1651 } 1652 loc, err := s.dwarfData.EntryLocation(entry) 1653 if err != nil { 1654 return 0, nil 1655 } 1656 ofs, err := s.dwarfData.EntryTypeOffset(entry) 1657 if err != nil { 1658 return 0, nil 1659 } 1660 typ, err := s.dwarfData.Type(ofs) 1661 if err != nil { 1662 return 0, nil 1663 } 1664 return loc, typ 1665} 1666 1667// intFromInteger converts an untyped integer constant to an int32 or int64, 1668// depending on the int size of the debugged program. 1669// It returns an error on overflow, or if it can't determine the int size. 1670func (e *evaluator) intFromInteger(v untInt) (interface{}, error) { 1671 t, ok := e.getBaseType("int") 1672 if !ok { 1673 return nil, errors.New("couldn't get int size from DWARF info") 1674 } 1675 switch t.Common().ByteSize { 1676 case 4: 1677 if v.Cmp(bigIntMaxInt32) == +1 || v.Cmp(bigIntMinInt32) == -1 { 1678 return nil, errors.New("constant overflows int") 1679 } 1680 return int32(v.Int64()), nil 1681 case 8: 1682 if v.Cmp(bigIntMaxInt64) == +1 || v.Cmp(bigIntMinInt64) == -1 { 1683 return nil, errors.New("constant overflows int") 1684 } 1685 return v.Int64(), nil 1686 } 1687 return nil, errors.New("invalid int size in DWARF info") 1688} 1689 1690// uint8Result constructs a result for a uint8 value. 1691func (e *evaluator) uint8Result(v uint8) result { 1692 t, ok := e.getBaseType("uint8") 1693 if !ok { 1694 e.err("couldn't construct uint8") 1695 } 1696 return result{t, uint8(v)} 1697} 1698 1699// stringResult constructs a result for a string value. 1700func (e *evaluator) stringResult(s string) result { 1701 t, ok := e.getBaseType("string") 1702 if !ok { 1703 e.err("couldn't construct string") 1704 } 1705 return result{t, debug.String{Length: uint64(len(s)), String: s}} 1706} 1707 1708// getBaseType returns the *dwarf.Type with a given name. 1709// TODO: cache this. 1710func (e *evaluator) getBaseType(name string) (dwarf.Type, bool) { 1711 entry, err := e.server.dwarfData.LookupEntry(name) 1712 if err != nil { 1713 return nil, false 1714 } 1715 t, err := e.server.dwarfData.Type(entry.Offset) 1716 if err != nil { 1717 return nil, false 1718 } 1719 return t, true 1720} 1721 1722// resultFrom constructs a result corresponding to a value in the program with 1723// the given address and DWARF type. 1724// If getAddress is true, the result will be the operand of an address expression, 1725// so resultFrom returns a result containing a value of type addressableValue. 1726func (e *evaluator) resultFrom(a uint64, t dwarf.Type, getAddress bool) result { 1727 if a == 0 { 1728 return e.err("nil pointer dereference") 1729 } 1730 if getAddress { 1731 return result{t, addressableValue{a}} 1732 } 1733 v, err := e.server.value(t, a) 1734 if err != nil { 1735 return e.err(err.Error()) 1736 } 1737 return result{t, v} 1738} 1739 1740// zero returns the zero value of type t. 1741// TODO: implement for array and struct. 1742func (e *evaluator) zero(t dwarf.Type) result { 1743 var v interface{} 1744 switch typ := followTypedefs(t).(type) { 1745 case *dwarf.CharType, *dwarf.IntType, *dwarf.EnumType: 1746 switch typ.Common().ByteSize { 1747 case 1: 1748 v = int8(0) 1749 case 2: 1750 v = int16(0) 1751 case 4: 1752 v = int32(0) 1753 case 8: 1754 v = int64(0) 1755 default: 1756 return e.err("invalid integer size " + fmt.Sprint(typ.Common().ByteSize)) 1757 } 1758 case *dwarf.UcharType, *dwarf.UintType: 1759 switch typ.Common().ByteSize { 1760 case 1: 1761 v = uint8(0) 1762 case 2: 1763 v = uint16(0) 1764 case 4: 1765 v = uint32(0) 1766 case 8: 1767 v = uint64(0) 1768 default: 1769 return e.err("invalid unsigned integer size " + fmt.Sprint(typ.Common().ByteSize)) 1770 } 1771 case *dwarf.FloatType: 1772 switch typ.Common().ByteSize { 1773 case 4: 1774 v = float32(0) 1775 case 8: 1776 v = float64(0) 1777 default: 1778 return e.err("invalid float size " + fmt.Sprint(typ.Common().ByteSize)) 1779 } 1780 case *dwarf.ComplexType: 1781 switch typ.Common().ByteSize { 1782 case 8: 1783 v = complex64(0) 1784 case 16: 1785 v = complex128(0) 1786 default: 1787 return e.err("invalid complex size " + fmt.Sprint(typ.Common().ByteSize)) 1788 } 1789 case *dwarf.BoolType: 1790 v = false 1791 case *dwarf.PtrType: 1792 v = debug.Pointer{TypeID: uint64(t.Common().Offset)} 1793 case *dwarf.SliceType: 1794 v = debug.Slice{ 1795 Array: debug.Array{ 1796 ElementTypeID: uint64(typ.ElemType.Common().Offset), 1797 StrideBits: uint64(typ.ElemType.Common().ByteSize) * 8, 1798 }, 1799 } 1800 case *dwarf.StringType: 1801 v = debug.String{} 1802 case *dwarf.InterfaceType: 1803 v = debug.Interface{} 1804 case *dwarf.FuncType: 1805 v = debug.Func{} 1806 case *dwarf.MapType: 1807 v = debug.Map{TypeID: uint64(t.Common().Offset)} 1808 case *dwarf.ChanType: 1809 v = debug.Channel{ 1810 ElementTypeID: uint64(typ.ElemType.Common().Offset), 1811 Stride: uint64(typ.ElemType.Common().ByteSize), 1812 } 1813 default: 1814 return e.err("can't get zero value of this type") 1815 } 1816 return result{t, v} 1817} 1818 1819// convertUntyped converts x to be the same type as y, if x is untyped and the 1820// conversion is possible. 1821// 1822// An untyped bool can be converted to a boolean type. 1823// An untyped string can be converted to a string type. 1824// An untyped integer, rune, float or complex value can be converted to a 1825// numeric type, or to an untyped value later in that list. 1826// 1827// x is returned unchanged if none of these cases apply. 1828func convertUntyped(x, y result) result { 1829 switch a := x.v.(type) { 1830 case untInt: 1831 i := a.Int 1832 switch y.v.(type) { 1833 case int8: 1834 return result{y.d, int8(i.Int64())} 1835 case int16: 1836 return result{y.d, int16(i.Int64())} 1837 case int32: 1838 return result{y.d, int32(i.Int64())} 1839 case int64: 1840 return result{y.d, int64(i.Int64())} 1841 case uint8: 1842 return result{y.d, uint8(i.Uint64())} 1843 case uint16: 1844 return result{y.d, uint16(i.Uint64())} 1845 case uint32: 1846 return result{y.d, uint32(i.Uint64())} 1847 case uint64: 1848 return result{y.d, uint64(i.Uint64())} 1849 case float32: 1850 f, _ := new(big.Float).SetInt(i).Float32() 1851 return result{y.d, f} 1852 case float64: 1853 f, _ := new(big.Float).SetInt(i).Float64() 1854 return result{y.d, f} 1855 case complex64: 1856 f, _ := new(big.Float).SetInt(i).Float32() 1857 return result{y.d, complex(f, 0)} 1858 case complex128: 1859 f, _ := new(big.Float).SetInt(i).Float64() 1860 return result{y.d, complex(f, 0)} 1861 case untRune: 1862 return result{nil, untRune{i}} 1863 case untFloat: 1864 return result{nil, untFloat{new(big.Float).SetPrec(prec).SetInt(i)}} 1865 case untComplex: 1866 return result{nil, untComplex{new(big.Float).SetPrec(prec).SetInt(i), new(big.Float)}} 1867 } 1868 case untRune: 1869 i := a.Int 1870 switch y.v.(type) { 1871 case int8: 1872 return result{y.d, int8(i.Int64())} 1873 case int16: 1874 return result{y.d, int16(i.Int64())} 1875 case int32: 1876 return result{y.d, int32(i.Int64())} 1877 case int64: 1878 return result{y.d, int64(i.Int64())} 1879 case uint8: 1880 return result{y.d, uint8(i.Uint64())} 1881 case uint16: 1882 return result{y.d, uint16(i.Uint64())} 1883 case uint32: 1884 return result{y.d, uint32(i.Uint64())} 1885 case uint64: 1886 return result{y.d, uint64(i.Uint64())} 1887 case float32: 1888 f, _ := new(big.Float).SetInt(i).Float32() 1889 return result{y.d, f} 1890 case float64: 1891 f, _ := new(big.Float).SetInt(i).Float64() 1892 return result{y.d, f} 1893 case complex64: 1894 f, _ := new(big.Float).SetInt(i).Float32() 1895 return result{y.d, complex(f, 0)} 1896 case complex128: 1897 f, _ := new(big.Float).SetInt(i).Float64() 1898 return result{y.d, complex(f, 0)} 1899 case untRune: 1900 return result{nil, untRune{i}} 1901 case untFloat: 1902 return result{nil, untFloat{new(big.Float).SetPrec(prec).SetInt(i)}} 1903 case untComplex: 1904 return result{nil, untComplex{new(big.Float).SetPrec(prec).SetInt(i), new(big.Float)}} 1905 } 1906 case untFloat: 1907 if a.IsInt() { 1908 i, _ := a.Int(nil) 1909 switch y.v.(type) { 1910 case int8: 1911 return result{y.d, int8(i.Int64())} 1912 case int16: 1913 return result{y.d, int16(i.Int64())} 1914 case int32: 1915 return result{y.d, int32(i.Int64())} 1916 case int64: 1917 return result{y.d, int64(i.Int64())} 1918 case uint8: 1919 return result{y.d, uint8(i.Uint64())} 1920 case uint16: 1921 return result{y.d, uint16(i.Uint64())} 1922 case uint32: 1923 return result{y.d, uint32(i.Uint64())} 1924 case uint64: 1925 return result{y.d, uint64(i.Uint64())} 1926 } 1927 } 1928 switch y.v.(type) { 1929 case float32: 1930 f, _ := a.Float32() 1931 return result{y.d, float32(f)} 1932 case float64: 1933 f, _ := a.Float64() 1934 return result{y.d, float64(f)} 1935 case complex64: 1936 f, _ := a.Float32() 1937 return result{y.d, complex(f, 0)} 1938 case complex128: 1939 f, _ := a.Float64() 1940 return result{y.d, complex(f, 0)} 1941 case untComplex: 1942 return result{nil, untComplex{a.Float, new(big.Float)}} 1943 } 1944 case untComplex: 1945 if a.i.Sign() == 0 { 1946 // a is a real number. 1947 if a.r.IsInt() { 1948 // a is an integer. 1949 i, _ := a.r.Int(nil) 1950 switch y.v.(type) { 1951 case int8: 1952 return result{y.d, int8(i.Int64())} 1953 case int16: 1954 return result{y.d, int16(i.Int64())} 1955 case int32: 1956 return result{y.d, int32(i.Int64())} 1957 case int64: 1958 return result{y.d, int64(i.Int64())} 1959 case uint8: 1960 return result{y.d, uint8(i.Uint64())} 1961 case uint16: 1962 return result{y.d, uint16(i.Uint64())} 1963 case uint32: 1964 return result{y.d, uint32(i.Uint64())} 1965 case uint64: 1966 return result{y.d, uint64(i.Uint64())} 1967 } 1968 } 1969 switch y.v.(type) { 1970 case float32: 1971 f, _ := a.r.Float32() 1972 return result{y.d, float32(f)} 1973 case float64: 1974 f, _ := a.r.Float64() 1975 return result{y.d, float64(f)} 1976 } 1977 } 1978 switch y.v.(type) { 1979 case complex64: 1980 r, _ := a.r.Float32() 1981 i, _ := a.i.Float32() 1982 return result{y.d, complex(r, i)} 1983 case complex128: 1984 r, _ := a.r.Float64() 1985 i, _ := a.i.Float64() 1986 return result{y.d, complex(r, i)} 1987 } 1988 case bool: 1989 if x.d != nil { 1990 // x is a typed bool, not an untyped bool. 1991 break 1992 } 1993 switch y.v.(type) { 1994 case bool: 1995 return result{y.d, bool(a)} 1996 } 1997 case untString: 1998 switch y.v.(type) { 1999 case debug.String: 2000 return result{y.d, debug.String{Length: uint64(len(a)), String: string(a)}} 2001 } 2002 } 2003 return x 2004} 2005 2006// uint64FromResult converts a result into a uint64 for slice or index expressions. 2007// It returns an error if the conversion cannot be done. 2008func uint64FromResult(x result) (uint64, error) { 2009 switch v := x.v.(type) { 2010 case int8: 2011 if v < 0 { 2012 return 0, errors.New("value is negative") 2013 } 2014 return uint64(v), nil 2015 case int16: 2016 if v < 0 { 2017 return 0, errors.New("value is negative") 2018 } 2019 return uint64(v), nil 2020 case int32: 2021 if v < 0 { 2022 return 0, errors.New("value is negative") 2023 } 2024 return uint64(v), nil 2025 case int64: 2026 if v < 0 { 2027 return 0, errors.New("value is negative") 2028 } 2029 return uint64(v), nil 2030 case uint8: 2031 return uint64(v), nil 2032 case uint16: 2033 return uint64(v), nil 2034 case uint32: 2035 return uint64(v), nil 2036 case uint64: 2037 return v, nil 2038 case untInt: 2039 if v.Int.Sign() == -1 { 2040 return 0, errors.New("value is negative") 2041 } 2042 if v.Int.Cmp(bigIntMaxUint64) == +1 { 2043 return 0, errors.New("value is too large") 2044 } 2045 return v.Int.Uint64(), nil 2046 case untRune: 2047 if v.Sign() == -1 { 2048 return 0, errors.New("value is negative") 2049 } 2050 if v.Cmp(bigIntMaxUint64) == +1 { 2051 return 0, errors.New("value is too large") 2052 } 2053 return v.Uint64(), nil 2054 case untFloat: 2055 if !v.IsInt() { 2056 return 0, errors.New("value is not an integer") 2057 } 2058 if v.Sign() == -1 { 2059 return 0, errors.New("value is negative") 2060 } 2061 i, _ := v.Int(nil) 2062 if i.Cmp(bigIntMaxUint64) == +1 { 2063 return 0, errors.New("value is too large") 2064 } 2065 return i.Uint64(), nil 2066 case untComplex: 2067 if v.i.Sign() != 0 { 2068 return 0, errors.New("value is complex") 2069 } 2070 if !v.r.IsInt() { 2071 return 0, errors.New("value is not an integer") 2072 } 2073 if v.r.Sign() == -1 { 2074 return 0, errors.New("value is negative") 2075 } 2076 i, _ := v.r.Int(nil) 2077 if i.Cmp(bigIntMaxUint64) == +1 { 2078 return 0, errors.New("value is too large") 2079 } 2080 return i.Uint64(), nil 2081 } 2082 return 0, fmt.Errorf("cannot convert to unsigned integer") 2083} 2084 2085// followTypedefs returns the underlying type of t, removing any typedefs. 2086// If t leads to a cycle of typedefs, followTypedefs returns nil. 2087func followTypedefs(t dwarf.Type) dwarf.Type { 2088 // If t is a *dwarf.TypedefType, next returns t.Type, otherwise it returns t. 2089 // The bool returned is true when the argument was a typedef. 2090 next := func(t dwarf.Type) (dwarf.Type, bool) { 2091 tt, ok := t.(*dwarf.TypedefType) 2092 if !ok { 2093 return t, false 2094 } 2095 return tt.Type, true 2096 } 2097 // Advance two pointers, one at twice the speed, so we can detect if we get 2098 // stuck in a cycle. 2099 slow, fast := t, t 2100 for { 2101 var wasTypedef bool 2102 fast, wasTypedef = next(fast) 2103 if !wasTypedef { 2104 return fast 2105 } 2106 fast, wasTypedef = next(fast) 2107 if !wasTypedef { 2108 return fast 2109 } 2110 slow, _ = next(slow) 2111 if slow == fast { 2112 return nil 2113 } 2114 } 2115} 2116