Sin descripción

disasm.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2014 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 binutils
  15. import (
  16. "bytes"
  17. "io"
  18. "regexp"
  19. "strconv"
  20. "github.com/google/pprof/internal/plugin"
  21. "github.com/ianlancetaylor/demangle"
  22. )
  23. var (
  24. nmOutputRE = regexp.MustCompile(`^\s*([[:xdigit:]]+)\s+(.)\s+(.*)`)
  25. objdumpAsmOutputRE = regexp.MustCompile(`^\s*([[:xdigit:]]+):\s+(.*)`)
  26. objdumpOutputFileLine = regexp.MustCompile(`^(.*):([0-9]+)`)
  27. objdumpOutputFunction = regexp.MustCompile(`^(\S.*)\(\):`)
  28. )
  29. func findSymbols(syms []byte, file string, r *regexp.Regexp, address uint64) ([]*plugin.Sym, error) {
  30. // Collect all symbols from the nm output, grouping names mapped to
  31. // the same address into a single symbol.
  32. var symbols []*plugin.Sym
  33. names, start := []string{}, uint64(0)
  34. buf := bytes.NewBuffer(syms)
  35. for symAddr, name, err := nextSymbol(buf); err == nil; symAddr, name, err = nextSymbol(buf) {
  36. if err != nil {
  37. return nil, err
  38. }
  39. if start == symAddr {
  40. names = append(names, name)
  41. continue
  42. }
  43. if match := matchSymbol(names, start, symAddr-1, r, address); match != nil {
  44. symbols = append(symbols, &plugin.Sym{Name: match, File: file, Start: start, End: symAddr - 1})
  45. }
  46. names, start = []string{name}, symAddr
  47. }
  48. return symbols, nil
  49. }
  50. // matchSymbol checks if a symbol is to be selected by checking its
  51. // name to the regexp and optionally its address. It returns the name(s)
  52. // to be used for the matched symbol, or nil if no match
  53. func matchSymbol(names []string, start, end uint64, r *regexp.Regexp, address uint64) []string {
  54. if address != 0 && address >= start && address <= end {
  55. return names
  56. }
  57. for _, name := range names {
  58. if r.MatchString(name) {
  59. return []string{name}
  60. }
  61. // Match all possible demangled versions of the name.
  62. for _, o := range [][]demangle.Option{
  63. {demangle.NoClones},
  64. {demangle.NoParams},
  65. {demangle.NoParams, demangle.NoTemplateParams},
  66. } {
  67. if demangled, err := demangle.ToString(name, o...); err == nil && r.MatchString(demangled) {
  68. return []string{demangled}
  69. }
  70. }
  71. }
  72. return nil
  73. }
  74. // disassemble parses the output of the objdump command and returns
  75. // the assembly instructions in a slice.
  76. func disassemble(asm []byte) ([]plugin.Inst, error) {
  77. buf := bytes.NewBuffer(asm)
  78. function, file, line := "", "", 0
  79. var assembly []plugin.Inst
  80. for {
  81. input, err := buf.ReadString('\n')
  82. if err != nil {
  83. if err != io.EOF {
  84. return nil, err
  85. }
  86. if input == "" {
  87. break
  88. }
  89. }
  90. if fields := objdumpAsmOutputRE.FindStringSubmatch(input); len(fields) == 3 {
  91. if address, err := strconv.ParseUint(fields[1], 16, 64); err == nil {
  92. assembly = append(assembly,
  93. plugin.Inst{
  94. Addr: address,
  95. Text: fields[2],
  96. Function: function,
  97. File: file,
  98. Line: line,
  99. })
  100. continue
  101. }
  102. }
  103. if fields := objdumpOutputFileLine.FindStringSubmatch(input); len(fields) == 3 {
  104. if l, err := strconv.ParseUint(fields[2], 10, 32); err == nil {
  105. file, line = fields[1], int(l)
  106. }
  107. continue
  108. }
  109. if fields := objdumpOutputFunction.FindStringSubmatch(input); len(fields) == 2 {
  110. function = fields[1]
  111. continue
  112. }
  113. // Reset on unrecognized lines.
  114. function, file, line = "", "", 0
  115. }
  116. return assembly, nil
  117. }
  118. // nextSymbol parses the nm output to find the next symbol listed.
  119. // Skips over any output it cannot recognize.
  120. func nextSymbol(buf *bytes.Buffer) (uint64, string, error) {
  121. for {
  122. line, err := buf.ReadString('\n')
  123. if err != nil {
  124. if err != io.EOF || line == "" {
  125. return 0, "", err
  126. }
  127. }
  128. if fields := nmOutputRE.FindStringSubmatch(line); len(fields) == 4 {
  129. if address, err := strconv.ParseUint(fields[1], 16, 64); err == nil {
  130. return address, fields[3], nil
  131. }
  132. }
  133. }
  134. }