暫無描述

driver.go 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 driver provides an external entry point to the pprof driver.
  15. package driver
  16. import (
  17. "io"
  18. "regexp"
  19. "time"
  20. internaldriver "github.com/google/pprof/internal/driver"
  21. "github.com/google/pprof/internal/plugin"
  22. "github.com/google/pprof/profile"
  23. )
  24. // PProf acquires a profile, and symbolizes it using a profile
  25. // manager. Then it generates a report formatted according to the
  26. // options selected through the flags package.
  27. func PProf(o *Options) error {
  28. return internaldriver.PProf(o.internalOptions())
  29. }
  30. func (o *Options) internalOptions() *plugin.Options {
  31. var obj plugin.ObjTool
  32. if o.Obj != nil {
  33. obj = &internalObjTool{o.Obj}
  34. }
  35. var sym plugin.Symbolizer
  36. if o.Sym != nil {
  37. sym = &internalSymbolizer{o.Sym}
  38. }
  39. var httpServer func(args *plugin.HTTPServerArgs) error
  40. if o.HTTPServer != nil {
  41. httpServer = func(args *plugin.HTTPServerArgs) error {
  42. return o.HTTPServer(((*HTTPServerArgs)(args)))
  43. }
  44. }
  45. return &plugin.Options{
  46. Writer: o.Writer,
  47. Flagset: o.Flagset,
  48. Fetch: o.Fetch,
  49. Sym: sym,
  50. Obj: obj,
  51. UI: o.UI,
  52. HTTPServer: httpServer,
  53. }
  54. }
  55. // HTTPServerArgs contains arguments needed by an HTTP server that
  56. // is exporting a pprof web interface.
  57. type HTTPServerArgs plugin.HTTPServerArgs
  58. // Options groups all the optional plugins into pprof.
  59. type Options struct {
  60. Writer Writer
  61. Flagset FlagSet
  62. Fetch Fetcher
  63. Sym Symbolizer
  64. Obj ObjTool
  65. UI UI
  66. HTTPServer func(*HTTPServerArgs) error
  67. }
  68. // Writer provides a mechanism to write data under a certain name,
  69. // typically a filename.
  70. type Writer interface {
  71. Open(name string) (io.WriteCloser, error)
  72. }
  73. // A FlagSet creates and parses command-line flags.
  74. // It is similar to the standard flag.FlagSet.
  75. type FlagSet interface {
  76. // Bool, Int, Float64, and String define new flags,
  77. // like the functions of the same name in package flag.
  78. Bool(name string, def bool, usage string) *bool
  79. Int(name string, def int, usage string) *int
  80. Float64(name string, def float64, usage string) *float64
  81. String(name string, def string, usage string) *string
  82. // BoolVar, IntVar, Float64Var, and StringVar define new flags referencing
  83. // a given pointer, like the functions of the same name in package flag.
  84. BoolVar(pointer *bool, name string, def bool, usage string)
  85. IntVar(pointer *int, name string, def int, usage string)
  86. Float64Var(pointer *float64, name string, def float64, usage string)
  87. StringVar(pointer *string, name string, def string, usage string)
  88. // StringList is similar to String but allows multiple values for a
  89. // single flag
  90. StringList(name string, def string, usage string) *[]*string
  91. // ExtraUsage returns any additional text that should be
  92. // printed after the standard usage message.
  93. // The typical use of ExtraUsage is to show any custom flags
  94. // defined by the specific pprof plugins being used.
  95. ExtraUsage() string
  96. // Parse initializes the flags with their values for this run
  97. // and returns the non-flag command line arguments.
  98. // If an unknown flag is encountered or there are no arguments,
  99. // Parse should call usage and return nil.
  100. Parse(usage func()) []string
  101. }
  102. // A Fetcher reads and returns the profile named by src, using
  103. // the specified duration and timeout. It returns the fetched
  104. // profile and a string indicating a URL from where the profile
  105. // was fetched, which may be different than src.
  106. type Fetcher interface {
  107. Fetch(src string, duration, timeout time.Duration) (*profile.Profile, string, error)
  108. }
  109. // A Symbolizer introduces symbol information into a profile.
  110. type Symbolizer interface {
  111. Symbolize(mode string, srcs MappingSources, prof *profile.Profile) error
  112. }
  113. // MappingSources map each profile.Mapping to the source of the profile.
  114. // The key is either Mapping.File or Mapping.BuildId.
  115. type MappingSources map[string][]struct {
  116. Source string // URL of the source the mapping was collected from
  117. Start uint64 // delta applied to addresses from this source (to represent Merge adjustments)
  118. }
  119. // An ObjTool inspects shared libraries and executable files.
  120. type ObjTool interface {
  121. // Open opens the named object file. If the object is a shared
  122. // library, start/limit/offset are the addresses where it is mapped
  123. // into memory in the address space being inspected.
  124. Open(file string, start, limit, offset uint64) (ObjFile, error)
  125. // Disasm disassembles the named object file, starting at
  126. // the start address and stopping at (before) the end address.
  127. Disasm(file string, start, end uint64) ([]Inst, error)
  128. }
  129. // An Inst is a single instruction in an assembly listing.
  130. type Inst struct {
  131. Addr uint64 // virtual address of instruction
  132. Text string // instruction text
  133. Function string // function name
  134. File string // source file
  135. Line int // source line
  136. }
  137. // An ObjFile is a single object file: a shared library or executable.
  138. type ObjFile interface {
  139. // Name returns the underlying file name, if available.
  140. Name() string
  141. // Base returns the base address to use when looking up symbols in the file.
  142. Base() uint64
  143. // BuildID returns the GNU build ID of the file, or an empty string.
  144. BuildID() string
  145. // SourceLine reports the source line information for a given
  146. // address in the file. Due to inlining, the source line information
  147. // is in general a list of positions representing a call stack,
  148. // with the leaf function first.
  149. SourceLine(addr uint64) ([]Frame, error)
  150. // Symbols returns a list of symbols in the object file.
  151. // If r is not nil, Symbols restricts the list to symbols
  152. // with names matching the regular expression.
  153. // If addr is not zero, Symbols restricts the list to symbols
  154. // containing that address.
  155. Symbols(r *regexp.Regexp, addr uint64) ([]*Sym, error)
  156. // Close closes the file, releasing associated resources.
  157. Close() error
  158. }
  159. // A Frame describes a single line in a source file.
  160. type Frame struct {
  161. Func string // name of function
  162. File string // source file name
  163. Line int // line in file
  164. }
  165. // A Sym describes a single symbol in an object file.
  166. type Sym struct {
  167. Name []string // names of symbol (many if symbol was dedup'ed)
  168. File string // object file containing symbol
  169. Start uint64 // start virtual address
  170. End uint64 // virtual address of last byte in sym (Start+size-1)
  171. }
  172. // A UI manages user interactions.
  173. type UI interface {
  174. // Read returns a line of text (a command) read from the user.
  175. // prompt is printed before reading the command.
  176. ReadLine(prompt string) (string, error)
  177. // Print shows a message to the user.
  178. // It formats the text as fmt.Print would and adds a final \n if not already present.
  179. // For line-based UI, Print writes to standard error.
  180. // (Standard output is reserved for report data.)
  181. Print(...interface{})
  182. // PrintErr shows an error message to the user.
  183. // It formats the text as fmt.Print would and adds a final \n if not already present.
  184. // For line-based UI, PrintErr writes to standard error.
  185. PrintErr(...interface{})
  186. // IsTerminal returns whether the UI is known to be tied to an
  187. // interactive terminal (as opposed to being redirected to a file).
  188. IsTerminal() bool
  189. // WantBrowser indicates whether browser should be opened with the -http option.
  190. WantBrowser() bool
  191. // SetAutoComplete instructs the UI to call complete(cmd) to obtain
  192. // the auto-completion of cmd, if the UI supports auto-completion at all.
  193. SetAutoComplete(complete func(string) string)
  194. }
  195. // internalObjTool is a wrapper to map from the pprof external
  196. // interface to the internal interface.
  197. type internalObjTool struct {
  198. ObjTool
  199. }
  200. func (o *internalObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  201. f, err := o.ObjTool.Open(file, start, limit, offset)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return &internalObjFile{f}, err
  206. }
  207. type internalObjFile struct {
  208. ObjFile
  209. }
  210. func (f *internalObjFile) SourceLine(frame uint64) ([]plugin.Frame, error) {
  211. frames, err := f.ObjFile.SourceLine(frame)
  212. if err != nil {
  213. return nil, err
  214. }
  215. var pluginFrames []plugin.Frame
  216. for _, f := range frames {
  217. pluginFrames = append(pluginFrames, plugin.Frame(f))
  218. }
  219. return pluginFrames, nil
  220. }
  221. func (f *internalObjFile) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  222. syms, err := f.ObjFile.Symbols(r, addr)
  223. if err != nil {
  224. return nil, err
  225. }
  226. var pluginSyms []*plugin.Sym
  227. for _, s := range syms {
  228. ps := plugin.Sym(*s)
  229. pluginSyms = append(pluginSyms, &ps)
  230. }
  231. return pluginSyms, nil
  232. }
  233. func (o *internalObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  234. insts, err := o.ObjTool.Disasm(file, start, end)
  235. if err != nil {
  236. return nil, err
  237. }
  238. var pluginInst []plugin.Inst
  239. for _, inst := range insts {
  240. pluginInst = append(pluginInst, plugin.Inst(inst))
  241. }
  242. return pluginInst, nil
  243. }
  244. // internalSymbolizer is a wrapper to map from the pprof external
  245. // interface to the internal interface.
  246. type internalSymbolizer struct {
  247. Symbolizer
  248. }
  249. func (s *internalSymbolizer) Symbolize(mode string, srcs plugin.MappingSources, prof *profile.Profile) error {
  250. isrcs := MappingSources{}
  251. for m, s := range srcs {
  252. isrcs[m] = s
  253. }
  254. return s.Symbolizer.Symbolize(mode, isrcs, prof)
  255. }