1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package driver
16
17 import (
18 "bufio"
19 "fmt"
20 "io"
21 "os"
22 "strings"
23
24 "github.com/google/pprof/internal/binutils"
25 "github.com/google/pprof/internal/plugin"
26 "github.com/google/pprof/internal/symbolizer"
27 "github.com/google/pprof/internal/transport"
28 )
29
30
31
32 func setDefaults(o *plugin.Options) *plugin.Options {
33 d := &plugin.Options{}
34 if o != nil {
35 *d = *o
36 }
37 if d.Writer == nil {
38 d.Writer = oswriter{}
39 }
40 if d.Flagset == nil {
41 d.Flagset = &GoFlags{}
42 }
43 if d.Obj == nil {
44 d.Obj = &binutils.Binutils{}
45 }
46 if d.UI == nil {
47 d.UI = &stdUI{r: bufio.NewReader(os.Stdin)}
48 }
49 if d.HTTPTransport == nil {
50 d.HTTPTransport = transport.New(d.Flagset)
51 }
52 if d.Sym == nil {
53 d.Sym = &symbolizer.Symbolizer{Obj: d.Obj, UI: d.UI, Transport: d.HTTPTransport}
54 }
55 return d
56 }
57
58 type stdUI struct {
59 r *bufio.Reader
60 }
61
62 func (ui *stdUI) ReadLine(prompt string) (string, error) {
63 os.Stdout.WriteString(prompt)
64 return ui.r.ReadString('\n')
65 }
66
67 func (ui *stdUI) Print(args ...interface{}) {
68 ui.fprint(os.Stderr, args)
69 }
70
71 func (ui *stdUI) PrintErr(args ...interface{}) {
72 ui.fprint(os.Stderr, args)
73 }
74
75 func (ui *stdUI) IsTerminal() bool {
76 return false
77 }
78
79 func (ui *stdUI) WantBrowser() bool {
80 return true
81 }
82
83 func (ui *stdUI) SetAutoComplete(func(string) string) {
84 }
85
86 func (ui *stdUI) fprint(f *os.File, args []interface{}) {
87 text := fmt.Sprint(args...)
88 if !strings.HasSuffix(text, "\n") {
89 text += "\n"
90 }
91 f.WriteString(text)
92 }
93
94
95 type oswriter struct{}
96
97 func (oswriter) Open(name string) (io.WriteCloser, error) {
98 f, err := os.Create(name)
99 return f, err
100 }
101
View as plain text