Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

221 řádky
8.0 KiB

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