Bez popisu

binutils.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 provides access to the GNU binutils.
  15. package binutils
  16. import (
  17. "debug/elf"
  18. "fmt"
  19. "os"
  20. "os/exec"
  21. "path/filepath"
  22. "regexp"
  23. "strings"
  24. "github.com/google/pprof/internal/elfexec"
  25. "github.com/google/pprof/internal/plugin"
  26. )
  27. // A Binutils implements plugin.ObjTool by invoking the GNU binutils.
  28. // SetConfig must be called before any of the other methods.
  29. type Binutils struct {
  30. // Commands to invoke.
  31. addr2line string
  32. nm string
  33. objdump string
  34. // if fast, perform symbolization using nm (symbol names only),
  35. // instead of file-line detail from the slower addr2line.
  36. fast bool
  37. }
  38. // SetFastSymbolization sets a toggle that makes binutils use fast
  39. // symbolization (using nm), which is much faster than addr2line but
  40. // provides only symbol name information (no file/line).
  41. func (b *Binutils) SetFastSymbolization(fast bool) {
  42. b.fast = fast
  43. }
  44. // SetTools processes the contents of the tools option. It
  45. // expects a set of entries separated by commas; each entry is a pair
  46. // of the form t:path, where cmd will be used to look only for the
  47. // tool named t. If t is not specified, the path is searched for all
  48. // tools.
  49. func (b *Binutils) SetTools(config string) {
  50. // paths collect paths per tool; Key "" contains the default.
  51. paths := make(map[string][]string)
  52. for _, t := range strings.Split(config, ",") {
  53. name, path := "", t
  54. if ct := strings.SplitN(t, ":", 2); len(ct) == 2 {
  55. name, path = ct[0], ct[1]
  56. }
  57. paths[name] = append(paths[name], path)
  58. }
  59. defaultPath := paths[""]
  60. b.addr2line = findExe("addr2line", append(paths["addr2line"], defaultPath...))
  61. b.nm = findExe("nm", append(paths["nm"], defaultPath...))
  62. b.objdump = findExe("objdump", append(paths["objdump"], defaultPath...))
  63. }
  64. // findExe looks for an executable command on a set of paths.
  65. // If it cannot find it, returns cmd.
  66. func findExe(cmd string, paths []string) string {
  67. for _, p := range paths {
  68. cp := filepath.Join(p, cmd)
  69. if c, err := exec.LookPath(cp); err == nil {
  70. return c
  71. }
  72. }
  73. return cmd
  74. }
  75. // Disasm returns the assembly instructions for the specified address range
  76. // of a binary.
  77. func (b *Binutils) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  78. if b.addr2line == "" {
  79. // Update the command invocations if not initialized.
  80. b.SetTools("")
  81. }
  82. cmd := exec.Command(b.objdump, "-d", "-C", "--no-show-raw-insn", "-l",
  83. fmt.Sprintf("--start-address=%#x", start),
  84. fmt.Sprintf("--stop-address=%#x", end),
  85. file)
  86. out, err := cmd.Output()
  87. if err != nil {
  88. return nil, fmt.Errorf("%v: %v", cmd.Args, err)
  89. }
  90. return disassemble(out)
  91. }
  92. // Open satisfies the plugin.ObjTool interface.
  93. func (b *Binutils) Open(name string, start, limit, offset uint64) (plugin.ObjFile, error) {
  94. if b.addr2line == "" {
  95. // Update the command invocations if not initialized.
  96. b.SetTools("")
  97. }
  98. // Make sure file is a supported executable.
  99. // The pprof driver uses Open to sniff the difference
  100. // between an executable and a profile.
  101. // For now, only ELF is supported.
  102. // Could read the first few bytes of the file and
  103. // use a table of prefixes if we need to support other
  104. // systems at some point.
  105. f, err := os.Open(name)
  106. if err != nil {
  107. // For testing, do not require file name to exist.
  108. if strings.Contains(b.addr2line, "testdata/") {
  109. return &fileAddr2Line{file: file{b: b, name: name}}, nil
  110. }
  111. return nil, err
  112. }
  113. defer f.Close()
  114. ef, err := elf.NewFile(f)
  115. if err != nil {
  116. return nil, fmt.Errorf("Parsing %s: %v", name, err)
  117. }
  118. var stextOffset *uint64
  119. var pageAligned = func(addr uint64) bool { return addr%4096 == 0 }
  120. if strings.Contains(name, "vmlinux") || !pageAligned(start) || !pageAligned(limit) || !pageAligned(offset) {
  121. // Reading all Symbols is expensive, and we only rarely need it so
  122. // we don't want to do it every time. But if _stext happens to be
  123. // page-aligned but isn't the same as Vaddr, we would symbolize
  124. // wrong. So if the name the addresses aren't page aligned, or if
  125. // the name is "vmlinux" we read _stext. We can be wrong if: (1)
  126. // someone passes a kernel path that doesn't contain "vmlinux" AND
  127. // (2) _stext is page-aligned AND (3) _stext is not at Vaddr
  128. symbols, err := ef.Symbols()
  129. if err != nil {
  130. return nil, err
  131. }
  132. for _, s := range symbols {
  133. if s.Name == "_stext" {
  134. // The kernel may use _stext as the mapping start address.
  135. stextOffset = &s.Value
  136. break
  137. }
  138. }
  139. }
  140. base, err := elfexec.GetBase(&ef.FileHeader, nil, stextOffset, start, limit, offset)
  141. if err != nil {
  142. return nil, fmt.Errorf("Could not identify base for %s: %v", name, err)
  143. }
  144. // Find build ID, while we have the file open.
  145. buildID := ""
  146. if id, err := elfexec.GetBuildID(f); err == nil {
  147. buildID = fmt.Sprintf("%x", id)
  148. }
  149. if b.fast {
  150. return &fileNM{file: file{b, name, base, buildID}}, nil
  151. }
  152. return &fileAddr2Line{file: file{b, name, base, buildID}}, nil
  153. }
  154. // file implements the binutils.ObjFile interface.
  155. type file struct {
  156. b *Binutils
  157. name string
  158. base uint64
  159. buildID string
  160. }
  161. func (f *file) Name() string {
  162. return f.name
  163. }
  164. func (f *file) Base() uint64 {
  165. return f.base
  166. }
  167. func (f *file) BuildID() string {
  168. return f.buildID
  169. }
  170. func (f *file) SourceLine(addr uint64) ([]plugin.Frame, error) {
  171. return []plugin.Frame{}, nil
  172. }
  173. func (f *file) Close() error {
  174. return nil
  175. }
  176. func (f *file) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  177. // Get from nm a list of symbols sorted by address.
  178. cmd := exec.Command(f.b.nm, "-n", f.name)
  179. out, err := cmd.Output()
  180. if err != nil {
  181. return nil, fmt.Errorf("%v: %v", cmd.Args, err)
  182. }
  183. return findSymbols(out, f.name, r, addr)
  184. }
  185. // fileNM implements the binutils.ObjFile interface, using 'nm' to map
  186. // addresses to symbols (without file/line number information). It is
  187. // faster than fileAddr2Line.
  188. type fileNM struct {
  189. file
  190. addr2linernm *addr2LinerNM
  191. }
  192. func (f *fileNM) SourceLine(addr uint64) ([]plugin.Frame, error) {
  193. if f.addr2linernm == nil {
  194. addr2liner, err := newAddr2LinerNM(f.b.nm, f.name, f.base)
  195. if err != nil {
  196. return nil, err
  197. }
  198. f.addr2linernm = addr2liner
  199. }
  200. return f.addr2linernm.addrInfo(addr)
  201. }
  202. // fileAddr2Line implements the binutils.ObjFile interface, using
  203. // 'addr2line' to map addresses to symbols (with file/line number
  204. // information). It can be slow for large binaries with debug
  205. // information.
  206. type fileAddr2Line struct {
  207. file
  208. addr2liner *addr2Liner
  209. }
  210. func (f *fileAddr2Line) SourceLine(addr uint64) ([]plugin.Frame, error) {
  211. if f.addr2liner == nil {
  212. addr2liner, err := newAddr2Liner(f.b.addr2line, f.name, f.base)
  213. if err != nil {
  214. return nil, err
  215. }
  216. f.addr2liner = addr2liner
  217. // When addr2line encounters some gcc compiled binaries, it
  218. // drops interesting parts of names in anonymous namespaces.
  219. // Fallback to NM for better function names.
  220. if nm, err := newAddr2LinerNM(f.b.nm, f.name, f.base); err == nil {
  221. f.addr2liner.nm = nm
  222. }
  223. }
  224. return f.addr2liner.addrInfo(addr)
  225. }
  226. func (f *fileAddr2Line) Close() error {
  227. if f.addr2liner != nil {
  228. f.addr2liner.rw.close()
  229. f.addr2liner = nil
  230. }
  231. return nil
  232. }