暂无描述

driver.go 9.0KB

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