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.
 
 
 

113 lines
2.9 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. // +build linux
  15. package server
  16. import (
  17. "errors"
  18. "fmt"
  19. "cloud.google.com/go/cmd/go-cloud-debug-agent/internal/debug/dwarf"
  20. )
  21. func (s *Server) functionStartAddress(name string) (uint64, error) {
  22. entry, err := s.dwarfData.LookupFunction(name)
  23. if err != nil {
  24. return 0, err
  25. }
  26. addrAttr := entry.Val(dwarf.AttrLowpc)
  27. if addrAttr == nil {
  28. return 0, fmt.Errorf("symbol %q has no LowPC attribute", name)
  29. }
  30. addr, ok := addrAttr.(uint64)
  31. if !ok {
  32. return 0, fmt.Errorf("symbol %q has non-uint64 LowPC attribute", name)
  33. }
  34. return addr, nil
  35. }
  36. // evalLocation parses a DWARF location description encoded in v. It works for
  37. // cases where the variable is stored at an offset from the Canonical Frame
  38. // Address. The return value is this offset.
  39. // TODO: a more general location-description-parsing function.
  40. func evalLocation(v []uint8) (int64, error) {
  41. // Some DWARF constants.
  42. const (
  43. opConsts = 0x11
  44. opPlus = 0x22
  45. opCallFrameCFA = 0x9C
  46. )
  47. if len(v) == 0 {
  48. return 0, errors.New("empty location specifier")
  49. }
  50. if v[0] != opCallFrameCFA {
  51. return 0, errors.New("unsupported location specifier")
  52. }
  53. if len(v) == 1 {
  54. // The location description was just DW_OP_call_frame_cfa, so the location is exactly the CFA.
  55. return 0, nil
  56. }
  57. if v[1] != opConsts {
  58. return 0, errors.New("unsupported location specifier")
  59. }
  60. offset, v, err := sleb128(v[2:])
  61. if err != nil {
  62. return 0, err
  63. }
  64. if len(v) == 1 && v[0] == opPlus {
  65. // The location description was DW_OP_call_frame_cfa, DW_OP_consts <offset>, DW_OP_plus.
  66. // So return the offset.
  67. return offset, nil
  68. }
  69. return 0, errors.New("unsupported location specifier")
  70. }
  71. func uleb128(v []uint8) (u uint64) {
  72. var shift uint
  73. for _, x := range v {
  74. u |= (uint64(x) & 0x7F) << shift
  75. shift += 7
  76. if x&0x80 == 0 {
  77. break
  78. }
  79. }
  80. return u
  81. }
  82. // sleb128 parses a signed integer encoded with sleb128 at the start of v, and
  83. // returns the integer and the remainder of v.
  84. func sleb128(v []uint8) (s int64, rest []uint8, err error) {
  85. var shift uint
  86. var sign int64 = -1
  87. var i int
  88. var x uint8
  89. for i, x = range v {
  90. s |= (int64(x) & 0x7F) << shift
  91. shift += 7
  92. sign <<= 7
  93. if x&0x80 == 0 {
  94. if x&0x40 != 0 {
  95. s |= sign
  96. }
  97. break
  98. }
  99. }
  100. if i == len(v) {
  101. return 0, nil, errors.New("truncated sleb128")
  102. }
  103. return s, v[i+1:], nil
  104. }