Açıklama Yok

disasm.go 3.9KB

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