暂无描述

options.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 driver
  15. import (
  16. "bufio"
  17. "flag"
  18. "fmt"
  19. "io"
  20. "os"
  21. "strings"
  22. "github.com/google/pprof/internal/binutils"
  23. "github.com/google/pprof/internal/plugin"
  24. "github.com/google/pprof/internal/symbolizer"
  25. )
  26. // setDefaults returns a new plugin.Options with zero fields sets to
  27. // sensible defaults.
  28. func setDefaults(o *plugin.Options) *plugin.Options {
  29. d := &plugin.Options{}
  30. if o != nil {
  31. *d = *o
  32. }
  33. if d.Writer == nil {
  34. d.Writer = oswriter{}
  35. }
  36. if d.Flagset == nil {
  37. d.Flagset = goFlags{}
  38. }
  39. if d.Obj == nil {
  40. d.Obj = &binutils.Binutils{}
  41. }
  42. if d.UI == nil {
  43. d.UI = &stdUI{r: bufio.NewReader(os.Stdin)}
  44. }
  45. if d.Sym == nil {
  46. d.Sym = &symbolizer.Symbolizer{Obj: d.Obj, UI: d.UI}
  47. }
  48. return d
  49. }
  50. // goFlags returns a flagset implementation based on the standard flag
  51. // package from the Go distribution. It implements the plugin.FlagSet
  52. // interface.
  53. type goFlags struct{}
  54. func (goFlags) Bool(o string, d bool, c string) *bool {
  55. return flag.Bool(o, d, c)
  56. }
  57. func (goFlags) Int(o string, d int, c string) *int {
  58. return flag.Int(o, d, c)
  59. }
  60. func (goFlags) Float64(o string, d float64, c string) *float64 {
  61. return flag.Float64(o, d, c)
  62. }
  63. func (goFlags) String(o, d, c string) *string {
  64. return flag.String(o, d, c)
  65. }
  66. func (goFlags) BoolVar(b *bool, o string, d bool, c string) {
  67. flag.BoolVar(b, o, d, c)
  68. }
  69. func (goFlags) IntVar(i *int, o string, d int, c string) {
  70. flag.IntVar(i, o, d, c)
  71. }
  72. func (goFlags) Float64Var(f *float64, o string, d float64, c string) {
  73. flag.Float64Var(f, o, d, c)
  74. }
  75. func (goFlags) StringVar(s *string, o, d, c string) {
  76. flag.StringVar(s, o, d, c)
  77. }
  78. func (goFlags) StringList(o, d, c string) *[]*string {
  79. return &[]*string{flag.String(o, d, c)}
  80. }
  81. func (goFlags) ExtraUsage() string {
  82. return ""
  83. }
  84. func (goFlags) Parse(usage func()) []string {
  85. flag.Usage = usage
  86. flag.Parse()
  87. args := flag.Args()
  88. if len(args) == 0 {
  89. usage()
  90. }
  91. return args
  92. }
  93. type stdUI struct {
  94. r *bufio.Reader
  95. }
  96. func (ui *stdUI) ReadLine(prompt string) (string, error) {
  97. os.Stdout.WriteString(prompt)
  98. return ui.r.ReadString('\n')
  99. }
  100. func (ui *stdUI) Print(args ...interface{}) {
  101. ui.fprint(os.Stderr, args)
  102. }
  103. func (ui *stdUI) PrintErr(args ...interface{}) {
  104. ui.fprint(os.Stderr, args)
  105. }
  106. func (ui *stdUI) IsTerminal() bool {
  107. return false
  108. }
  109. func (ui *stdUI) SetAutoComplete(func(string) string) {
  110. }
  111. func (ui *stdUI) fprint(f *os.File, args []interface{}) {
  112. text := fmt.Sprint(args...)
  113. if !strings.HasSuffix(text, "\n") {
  114. text += "\n"
  115. }
  116. f.WriteString(text)
  117. }
  118. // oswriter implements the Writer interface using a regular file.
  119. type oswriter struct{}
  120. func (oswriter) Open(name string) (io.WriteCloser, error) {
  121. f, err := os.Create(name)
  122. return f, err
  123. }