Nenhuma descrição

plugin.go 7.2KB

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