No Description

driver.go 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. o.Writer,
  41. o.Flagset,
  42. o.Fetch,
  43. sym,
  44. obj,
  45. 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. File string // source file
  123. Line int // source line
  124. }
  125. // An ObjFile is a single object file: a shared library or executable.
  126. type ObjFile interface {
  127. // Name returns the underlying file name, if available.
  128. Name() string
  129. // Base returns the base address to use when looking up symbols in the file.
  130. Base() uint64
  131. // BuildID returns the GNU build ID of the file, or an empty string.
  132. BuildID() string
  133. // SourceLine reports the source line information for a given
  134. // address in the file. Due to inlining, the source line information
  135. // is in general a list of positions representing a call stack,
  136. // with the leaf function first.
  137. SourceLine(addr uint64) ([]Frame, error)
  138. // Symbols returns a list of symbols in the object file.
  139. // If r is not nil, Symbols restricts the list to symbols
  140. // with names matching the regular expression.
  141. // If addr is not zero, Symbols restricts the list to symbols
  142. // containing that address.
  143. Symbols(r *regexp.Regexp, addr uint64) ([]*Sym, error)
  144. // Close closes the file, releasing associated resources.
  145. Close() error
  146. }
  147. // A Frame describes a single line in a source file.
  148. type Frame struct {
  149. Func string // name of function
  150. File string // source file name
  151. Line int // line in file
  152. }
  153. // A Sym describes a single symbol in an object file.
  154. type Sym struct {
  155. Name []string // names of symbol (many if symbol was dedup'ed)
  156. File string // object file containing symbol
  157. Start uint64 // start virtual address
  158. End uint64 // virtual address of last byte in sym (Start+size-1)
  159. }
  160. // A UI manages user interactions.
  161. type UI interface {
  162. // Read returns a line of text (a command) read from the user.
  163. // prompt is printed before reading the command.
  164. ReadLine(prompt string) (string, error)
  165. // Print shows a message to the user.
  166. // It formats the text as fmt.Print would and adds a final \n if not already present.
  167. // For line-based UI, Print writes to standard error.
  168. // (Standard output is reserved for report data.)
  169. Print(...interface{})
  170. // PrintErr shows an error message to the user.
  171. // It formats the text as fmt.Print would and adds a final \n if not already present.
  172. // For line-based UI, PrintErr writes to standard error.
  173. PrintErr(...interface{})
  174. // IsTerminal returns whether the UI is known to be tied to an
  175. // interactive terminal (as opposed to being redirected to a file).
  176. IsTerminal() bool
  177. // SetAutoComplete instructs the UI to call complete(cmd) to obtain
  178. // the auto-completion of cmd, if the UI supports auto-completion at all.
  179. SetAutoComplete(complete func(string) string)
  180. }
  181. // internalObjTool is a wrapper to map from the pprof external
  182. // interface to the internal interface.
  183. type internalObjTool struct {
  184. ObjTool
  185. }
  186. func (o *internalObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  187. f, err := o.ObjTool.Open(file, start, limit, offset)
  188. if err != nil {
  189. return nil, err
  190. }
  191. return &internalObjFile{f}, err
  192. }
  193. type internalObjFile struct {
  194. ObjFile
  195. }
  196. func (f *internalObjFile) SourceLine(frame uint64) ([]plugin.Frame, error) {
  197. frames, err := f.ObjFile.SourceLine(frame)
  198. if err != nil {
  199. return nil, err
  200. }
  201. var pluginFrames []plugin.Frame
  202. for _, f := range frames {
  203. pluginFrames = append(pluginFrames, plugin.Frame(f))
  204. }
  205. return pluginFrames, nil
  206. }
  207. func (f *internalObjFile) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  208. syms, err := f.ObjFile.Symbols(r, addr)
  209. if err != nil {
  210. return nil, err
  211. }
  212. var pluginSyms []*plugin.Sym
  213. for _, s := range syms {
  214. ps := plugin.Sym(*s)
  215. pluginSyms = append(pluginSyms, &ps)
  216. }
  217. return pluginSyms, nil
  218. }
  219. func (o *internalObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  220. insts, err := o.ObjTool.Disasm(file, start, end)
  221. if err != nil {
  222. return nil, err
  223. }
  224. var pluginInst []plugin.Inst
  225. for _, inst := range insts {
  226. pluginInst = append(pluginInst, plugin.Inst(inst))
  227. }
  228. return pluginInst, nil
  229. }
  230. // internalSymbolizer is a wrapper to map from the pprof external
  231. // interface to the internal interface.
  232. type internalSymbolizer struct {
  233. Symbolizer
  234. }
  235. func (s *internalSymbolizer) Symbolize(mode string, srcs plugin.MappingSources, prof *profile.Profile) error {
  236. isrcs := plugin.MappingSources{}
  237. for m, s := range srcs {
  238. isrcs[m] = s
  239. }
  240. return s.Symbolize(mode, isrcs, prof)
  241. }