暂无描述

plugin.go 6.8KB

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