Nenhuma descrição

binutils.go 9.0KB

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