暂无描述

disasm.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/pkg/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(`^;?\s?(.*):([0-9]+)`)
  27. objdumpOutputFunction = regexp.MustCompile(`^;?\s?(\S.*)\(\):`)
  28. objdumpOutputFunctionLLVM = regexp.MustCompile(`^([[:xdigit:]]+)?\s?(.*):`)
  29. )
  30. func findSymbols(syms []byte, file string, r *regexp.Regexp, address uint64) ([]*plugin.Sym, error) {
  31. // Collect all symbols from the nm output, grouping names mapped to
  32. // the same address into a single symbol.
  33. // The symbols to return.
  34. var symbols []*plugin.Sym
  35. // The current group of symbol names, and the address they are all at.
  36. names, start := []string{}, uint64(0)
  37. buf := bytes.NewBuffer(syms)
  38. for {
  39. symAddr, name, err := nextSymbol(buf)
  40. if err == io.EOF {
  41. // Done. If there was an unfinished group, append it.
  42. if len(names) != 0 {
  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. }
  47. // And return the symbols.
  48. return symbols, nil
  49. }
  50. if err != nil {
  51. // There was some kind of serious error reading nm's output.
  52. return nil, err
  53. }
  54. // If this symbol is at the same address as the current group, add it to the group.
  55. if symAddr == start {
  56. names = append(names, name)
  57. continue
  58. }
  59. // Otherwise append the current group to the list of symbols.
  60. if match := matchSymbol(names, start, symAddr-1, r, address); match != nil {
  61. symbols = append(symbols, &plugin.Sym{Name: match, File: file, Start: start, End: symAddr - 1})
  62. }
  63. // And start a new group.
  64. names, start = []string{name}, symAddr
  65. }
  66. }
  67. // matchSymbol checks if a symbol is to be selected by checking its
  68. // name to the regexp and optionally its address. It returns the name(s)
  69. // to be used for the matched symbol, or nil if no match
  70. func matchSymbol(names []string, start, end uint64, r *regexp.Regexp, address uint64) []string {
  71. if address != 0 && address >= start && address <= end {
  72. return names
  73. }
  74. for _, name := range names {
  75. if r == nil || r.MatchString(name) {
  76. return []string{name}
  77. }
  78. // Match all possible demangled versions of the name.
  79. for _, o := range [][]demangle.Option{
  80. {demangle.NoClones},
  81. {demangle.NoParams},
  82. {demangle.NoParams, demangle.NoTemplateParams},
  83. } {
  84. if demangled, err := demangle.ToString(name, o...); err == nil && r.MatchString(demangled) {
  85. return []string{demangled}
  86. }
  87. }
  88. }
  89. return nil
  90. }
  91. // disassemble parses the output of the objdump command and returns
  92. // the assembly instructions in a slice.
  93. func disassemble(asm []byte) ([]plugin.Inst, error) {
  94. buf := bytes.NewBuffer(asm)
  95. function, file, line := "", "", 0
  96. var assembly []plugin.Inst
  97. for {
  98. input, err := buf.ReadString('\n')
  99. if err != nil {
  100. if err != io.EOF {
  101. return nil, err
  102. }
  103. if input == "" {
  104. break
  105. }
  106. }
  107. if fields := objdumpAsmOutputRE.FindStringSubmatch(input); len(fields) == 3 {
  108. if address, err := strconv.ParseUint(fields[1], 16, 64); err == nil {
  109. assembly = append(assembly,
  110. plugin.Inst{
  111. Addr: address,
  112. Text: fields[2],
  113. Function: function,
  114. File: file,
  115. Line: line,
  116. })
  117. continue
  118. }
  119. }
  120. if fields := objdumpOutputFileLine.FindStringSubmatch(input); len(fields) == 3 {
  121. if l, err := strconv.ParseUint(fields[2], 10, 32); err == nil {
  122. file, line = fields[1], int(l)
  123. }
  124. continue
  125. }
  126. if fields := objdumpOutputFunction.FindStringSubmatch(input); len(fields) == 2 {
  127. function = fields[1]
  128. continue
  129. } else {
  130. if fields := objdumpOutputFunctionLLVM.FindStringSubmatch(input); len(fields) == 3 {
  131. function = fields[2]
  132. continue
  133. }
  134. }
  135. // Reset on unrecognized lines.
  136. function, file, line = "", "", 0
  137. }
  138. return assembly, nil
  139. }
  140. // nextSymbol parses the nm output to find the next symbol listed.
  141. // Skips over any output it cannot recognize.
  142. func nextSymbol(buf *bytes.Buffer) (uint64, string, error) {
  143. for {
  144. line, err := buf.ReadString('\n')
  145. if err != nil {
  146. if err != io.EOF || line == "" {
  147. return 0, "", err
  148. }
  149. }
  150. if fields := nmOutputRE.FindStringSubmatch(line); len(fields) == 4 {
  151. if address, err := strconv.ParseUint(fields[1], 16, 64); err == nil {
  152. return address, fields[3], nil
  153. }
  154. }
  155. }
  156. }