Sin descripción

plugin.go 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 plugin defines the plugin implementations that the main pprof driver requires.
  15. package plugin
  16. import (
  17. "io"
  18. "net/http"
  19. "regexp"
  20. "time"
  21. "github.com/google/pprof/profile"
  22. )
  23. // Options groups all the optional plugins into pprof.
  24. type Options struct {
  25. Writer Writer
  26. Flagset FlagSet
  27. Fetch Fetcher
  28. Sym Symbolizer
  29. Obj ObjTool
  30. UI UI
  31. // HTTPServer is a function that should block serving http requests,
  32. // including the handlers specfied in args. If non-nil, pprof will
  33. // invoke this function if necessary to provide a web interface.
  34. //
  35. // If HTTPServer is nil, pprof will use its own internal HTTP server.
  36. //
  37. // A common use for a custom HTTPServer is to provide custom
  38. // authentication checks.
  39. HTTPServer func(args *HTTPServerArgs) error
  40. }
  41. // Writer provides a mechanism to write data under a certain name,
  42. // typically a filename.
  43. type Writer interface {
  44. Open(name string) (io.WriteCloser, error)
  45. }
  46. // A FlagSet creates and parses command-line flags.
  47. // It is similar to the standard flag.FlagSet.
  48. type FlagSet interface {
  49. // Bool, Int, Float64, and String define new flags,
  50. // like the functions of the same name in package flag.
  51. Bool(name string, def bool, usage string) *bool
  52. Int(name string, def int, usage string) *int
  53. Float64(name string, def float64, usage string) *float64
  54. String(name string, def string, usage string) *string
  55. // BoolVar, IntVar, Float64Var, and StringVar define new flags referencing
  56. // a given pointer, like the functions of the same name in package flag.
  57. BoolVar(pointer *bool, name string, def bool, usage string)
  58. IntVar(pointer *int, name string, def int, usage string)
  59. Float64Var(pointer *float64, name string, def float64, usage string)
  60. StringVar(pointer *string, name string, def string, usage string)
  61. // StringList is similar to String but allows multiple values for a
  62. // single flag
  63. StringList(name string, def string, usage string) *[]*string
  64. // ExtraUsage returns any additional text that should be
  65. // printed after the standard usage message.
  66. // The typical use of ExtraUsage is to show any custom flags
  67. // defined by the specific pprof plugins being used.
  68. ExtraUsage() string
  69. // Parse initializes the flags with their values for this run
  70. // and returns the non-flag command line arguments.
  71. // If an unknown flag is encountered or there are no arguments,
  72. // Parse should call usage and return nil.
  73. Parse(usage func()) []string
  74. }
  75. // A Fetcher reads and returns the profile named by src. src can be a
  76. // local file path or a URL. duration and timeout are units specified
  77. // by the end user, or 0 by default. duration refers to the length of
  78. // the profile collection, if applicable, and timeout is the amount of
  79. // time to wait for a profile before returning an error. Returns the
  80. // fetched profile, the URL of the actual source of the profile, or an
  81. // error.
  82. type Fetcher interface {
  83. Fetch(src string, duration, timeout time.Duration) (*profile.Profile, string, error)
  84. }
  85. // A Symbolizer introduces symbol information into a profile.
  86. type Symbolizer interface {
  87. Symbolize(mode string, srcs MappingSources, prof *profile.Profile) error
  88. }
  89. // MappingSources map each profile.Mapping to the source of the profile.
  90. // The key is either Mapping.File or Mapping.BuildId.
  91. type MappingSources map[string][]struct {
  92. Source string // URL of the source the mapping was collected from
  93. Start uint64 // delta applied to addresses from this source (to represent Merge adjustments)
  94. }
  95. // An ObjTool inspects shared libraries and executable files.
  96. type ObjTool interface {
  97. // Open opens the named object file. If the object is a shared
  98. // library, start/limit/offset are the addresses where it is mapped
  99. // into memory in the address space being inspected.
  100. Open(file string, start, limit, offset uint64) (ObjFile, error)
  101. // Disasm disassembles the named object file, starting at
  102. // the start address and stopping at (before) the end address.
  103. Disasm(file string, start, end uint64) ([]Inst, error)
  104. }
  105. // An Inst is a single instruction in an assembly listing.
  106. type Inst struct {
  107. Addr uint64 // virtual address of instruction
  108. Text string // instruction text
  109. Function string // function name
  110. File string // source file
  111. Line int // source line
  112. }
  113. // An ObjFile is a single object file: a shared library or executable.
  114. type ObjFile interface {
  115. // Name returns the underlyinf file name, if available
  116. Name() string
  117. // Base returns the base address to use when looking up symbols in the file.
  118. Base() uint64
  119. // BuildID returns the GNU build ID of the file, or an empty string.
  120. BuildID() string
  121. // SourceLine reports the source line information for a given
  122. // address in the file. Due to inlining, the source line information
  123. // is in general a list of positions representing a call stack,
  124. // with the leaf function first.
  125. SourceLine(addr uint64) ([]Frame, error)
  126. // Symbols returns a list of symbols in the object file.
  127. // If r is not nil, Symbols restricts the list to symbols
  128. // with names matching the regular expression.
  129. // If addr is not zero, Symbols restricts the list to symbols
  130. // containing that address.
  131. Symbols(r *regexp.Regexp, addr uint64) ([]*Sym, error)
  132. // Close closes the file, releasing associated resources.
  133. Close() error
  134. }
  135. // A Frame describes a single line in a source file.
  136. type Frame struct {
  137. Func string // name of function
  138. File string // source file name
  139. Line int // line in file
  140. }
  141. // A Sym describes a single symbol in an object file.
  142. type Sym struct {
  143. Name []string // names of symbol (many if symbol was dedup'ed)
  144. File string // object file containing symbol
  145. Start uint64 // start virtual address
  146. End uint64 // virtual address of last byte in sym (Start+size-1)
  147. }
  148. // A UI manages user interactions.
  149. type UI interface {
  150. // Read returns a line of text (a command) read from the user.
  151. // prompt is printed before reading the command.
  152. ReadLine(prompt string) (string, error)
  153. // Print shows a message to the user.
  154. // It formats the text as fmt.Print would and adds a final \n if not already present.
  155. // For line-based UI, Print writes to standard error.
  156. // (Standard output is reserved for report data.)
  157. Print(...interface{})
  158. // PrintErr shows an error message to the user.
  159. // It formats the text as fmt.Print would and adds a final \n if not already present.
  160. // For line-based UI, PrintErr writes to standard error.
  161. PrintErr(...interface{})
  162. // IsTerminal returns whether the UI is known to be tied to an
  163. // interactive terminal (as opposed to being redirected to a file).
  164. IsTerminal() bool
  165. // SetAutoComplete instructs the UI to call complete(cmd) to obtain
  166. // the auto-completion of cmd, if the UI supports auto-completion at all.
  167. SetAutoComplete(complete func(string) string)
  168. }
  169. // HTTPServerArgs contains arguments needed by an HTTP server that
  170. // is exporting a pprof web interface.
  171. type HTTPServerArgs struct {
  172. // Hostport contains the http server address (derived from flags).
  173. Hostport string
  174. Host string // Host portion of Hostport
  175. Port int // Port portion of Hostport
  176. // Handlers maps from URL paths to the handler to invoke to
  177. // serve that path.
  178. Handlers map[string]http.Handler
  179. }