Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

107 рядки
3.7 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. // Package dwarf provides access to DWARF debugging information loaded from
  15. // executable files, as defined in the DWARF 2.0 Standard at
  16. // http://dwarfstd.org/doc/dwarf-2.0.0.pdf
  17. package dwarf // import "cloud.google.com/go/cmd/go-cloud-debug-agent/internal/debug/dwarf"
  18. import "encoding/binary"
  19. // Data represents the DWARF debugging information
  20. // loaded from an executable file (for example, an ELF or Mach-O executable).
  21. type Data struct {
  22. // raw data
  23. abbrev []byte
  24. aranges []byte
  25. frame []byte
  26. info []byte
  27. line []byte
  28. pubnames []byte
  29. ranges []byte
  30. str []byte
  31. // parsed data
  32. abbrevCache map[uint32]abbrevTable
  33. order binary.ByteOrder
  34. typeCache map[Offset]Type
  35. typeSigs map[uint64]*typeUnit
  36. unit []unit
  37. sourceFiles []string // All the source files listed in .debug_line, from all the compilation units.
  38. nameCache // map from name to top-level entries in .debug_info.
  39. pcToFuncEntries // cache of .debug_info data for function bounds.
  40. pcToLineEntries // cache of .debug_line data, used for efficient PC-to-line mapping.
  41. lineToPCEntries // cache of .debug_line data, used for efficient line-to-[]PC mapping.
  42. }
  43. // New returns a new Data object initialized from the given parameters.
  44. // Rather than calling this function directly, clients should typically use
  45. // the DWARF method of the File type of the appropriate package debug/elf,
  46. // debug/macho, or debug/pe.
  47. //
  48. // The []byte arguments are the data from the corresponding debug section
  49. // in the object file; for example, for an ELF object, abbrev is the contents of
  50. // the ".debug_abbrev" section.
  51. func New(abbrev, aranges, frame, info, line, pubnames, ranges, str []byte) (*Data, error) {
  52. d := &Data{
  53. abbrev: abbrev,
  54. aranges: aranges,
  55. frame: frame,
  56. info: info,
  57. line: line,
  58. pubnames: pubnames,
  59. ranges: ranges,
  60. str: str,
  61. abbrevCache: make(map[uint32]abbrevTable),
  62. typeCache: make(map[Offset]Type),
  63. typeSigs: make(map[uint64]*typeUnit),
  64. }
  65. // Sniff .debug_info to figure out byte order.
  66. // bytes 4:6 are the version, a tiny 16-bit number (1, 2, 3).
  67. if len(d.info) < 6 {
  68. return nil, DecodeError{"info", Offset(len(d.info)), "too short"}
  69. }
  70. x, y := d.info[4], d.info[5]
  71. switch {
  72. case x == 0 && y == 0:
  73. return nil, DecodeError{"info", 4, "unsupported version 0"}
  74. case x == 0:
  75. d.order = binary.BigEndian
  76. case y == 0:
  77. d.order = binary.LittleEndian
  78. default:
  79. return nil, DecodeError{"info", 4, "cannot determine byte order"}
  80. }
  81. u, err := d.parseUnits()
  82. if err != nil {
  83. return nil, err
  84. }
  85. d.unit = u
  86. d.buildInfoCaches()
  87. if err := d.buildLineCaches(); err != nil {
  88. return nil, err
  89. }
  90. return d, nil
  91. }
  92. // AddTypes will add one .debug_types section to the DWARF data. A
  93. // typical object with DWARF version 4 debug info will have multiple
  94. // .debug_types sections. The name is used for error reporting only,
  95. // and serves to distinguish one .debug_types section from another.
  96. func (d *Data) AddTypes(name string, types []byte) error {
  97. return d.parseTypes(name, types)
  98. }