You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2116 line
49 KiB

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