golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/internal/plugin/plugin.go (about)

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