Нема описа

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 specified 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. HTTPTransport http.RoundTripper
  41. }
  42. // Writer provides a mechanism to write data under a certain name,
  43. // typically a filename.
  44. type Writer interface {
  45. Open(name string) (io.WriteCloser, error)
  46. }
  47. // A FlagSet creates and parses command-line flags.
  48. // It is similar to the standard flag.FlagSet.
  49. type FlagSet interface {
  50. // Bool, Int, Float64, and String define new flags,
  51. // like the functions of the same name in package flag.
  52. Bool(name string, def bool, usage string) *bool
  53. Int(name string, def int, usage string) *int
  54. Float64(name string, def float64, usage string) *float64
  55. String(name string, def string, usage string) *string
  56. // StringList is similar to String but allows multiple values for a
  57. // single flag
  58. StringList(name string, def string, usage string) *[]*string
  59. // ExtraUsage returns any additional text that should be printed after the
  60. // standard usage message. The extra usage message returned includes all text
  61. // added with AddExtraUsage().
  62. // The typical use of ExtraUsage is to show any custom flags defined by the
  63. // specific pprof plugins being used.
  64. ExtraUsage() string
  65. // AddExtraUsage appends additional text to the end of the extra usage message.
  66. AddExtraUsage(eu string)
  67. // Parse initializes the flags with their values for this run
  68. // and returns the non-flag command line arguments.
  69. // If an unknown flag is encountered or there are no arguments,
  70. // Parse should call usage and return nil.
  71. Parse(usage func()) []string
  72. }
  73. // A Fetcher reads and returns the profile named by src. src can be a
  74. // local file path or a URL. duration and timeout are units specified
  75. // by the end user, or 0 by default. duration refers to the length of
  76. // the profile collection, if applicable, and timeout is the amount of
  77. // time to wait for a profile before returning an error. Returns the
  78. // fetched profile, the URL of the actual source of the profile, or an
  79. // error.
  80. type Fetcher interface {
  81. Fetch(src string, duration, timeout time.Duration) (*profile.Profile, string, error)
  82. }
  83. // A Symbolizer introduces symbol information into a profile.
  84. type Symbolizer interface {
  85. Symbolize(mode string, srcs MappingSources, prof *profile.Profile) error
  86. }
  87. // MappingSources map each profile.Mapping to the source of the profile.
  88. // The key is either Mapping.File or Mapping.BuildId.
  89. type MappingSources map[string][]struct {
  90. Source string // URL of the source the mapping was collected from
  91. Start uint64 // delta applied to addresses from this source (to represent Merge adjustments)
  92. }
  93. // An ObjTool inspects shared libraries and executable files.
  94. type ObjTool interface {
  95. // Open opens the named object file. If the object is a shared
  96. // library, start/limit/offset are the addresses where it is mapped
  97. // into memory in the address space being inspected.
  98. Open(file string, start, limit, offset uint64) (ObjFile, error)
  99. // Disasm disassembles the named object file, starting at
  100. // the start address and stopping at (before) the end address.
  101. Disasm(file string, start, end uint64) ([]Inst, error)
  102. }
  103. // An Inst is a single instruction in an assembly listing.
  104. type Inst struct {
  105. Addr uint64 // virtual address of instruction
  106. Text string // instruction text
  107. Function string // function name
  108. File string // source file
  109. Line int // source line
  110. }
  111. // An ObjFile is a single object file: a shared library or executable.
  112. type ObjFile interface {
  113. // Name returns the underlyinf file name, if available
  114. Name() string
  115. // Base returns the base address to use when looking up symbols in the file.
  116. Base() uint64
  117. // BuildID returns the GNU build ID of the file, or an empty string.
  118. BuildID() string
  119. // SourceLine reports the source line information for a given
  120. // address in the file. Due to inlining, the source line information
  121. // is in general a list of positions representing a call stack,
  122. // with the leaf function first.
  123. SourceLine(addr uint64) ([]Frame, error)
  124. // Symbols returns a list of symbols in the object file.
  125. // If r is not nil, Symbols restricts the list to symbols
  126. // with names matching the regular expression.
  127. // If addr is not zero, Symbols restricts the list to symbols
  128. // containing that address.
  129. Symbols(r *regexp.Regexp, addr uint64) ([]*Sym, error)
  130. // Close closes the file, releasing associated resources.
  131. Close() error
  132. }
  133. // A Frame describes a single line in a source file.
  134. type Frame struct {
  135. Func string // name of function
  136. File string // source file name
  137. Line int // line in file
  138. }
  139. // A Sym describes a single symbol in an object file.
  140. type Sym struct {
  141. Name []string // names of symbol (many if symbol was dedup'ed)
  142. File string // object file containing symbol
  143. Start uint64 // start virtual address
  144. End uint64 // virtual address of last byte in sym (Start+size-1)
  145. }
  146. // A UI manages user interactions.
  147. type UI interface {
  148. // Read returns a line of text (a command) read from the user.
  149. // prompt is printed before reading the command.
  150. ReadLine(prompt string) (string, error)
  151. // Print shows a message to the user.
  152. // It formats the text as fmt.Print would and adds a final \n if not already present.
  153. // For line-based UI, Print writes to standard error.
  154. // (Standard output is reserved for report data.)
  155. Print(...interface{})
  156. // PrintErr shows an error message to the user.
  157. // It formats the text as fmt.Print would and adds a final \n if not already present.
  158. // For line-based UI, PrintErr writes to standard error.
  159. PrintErr(...interface{})
  160. // IsTerminal returns whether the UI is known to be tied to an
  161. // interactive terminal (as opposed to being redirected to a file).
  162. IsTerminal() bool
  163. // WantBrowser indicates whether a browser should be opened with the -http option.
  164. WantBrowser() 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. }