You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

101 lines
2.3 KiB

  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. "fmt"
  18. "io"
  19. "os"
  20. "strings"
  21. "github.com/google/pprof/internal/binutils"
  22. "github.com/google/pprof/internal/plugin"
  23. "github.com/google/pprof/internal/symbolizer"
  24. "github.com/google/pprof/internal/transport"
  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.HTTPTransport == nil {
  46. d.HTTPTransport = transport.New(d.Flagset)
  47. }
  48. if d.Sym == nil {
  49. d.Sym = &symbolizer.Symbolizer{Obj: d.Obj, UI: d.UI, Transport: d.HTTPTransport}
  50. }
  51. return d
  52. }
  53. type stdUI struct {
  54. r *bufio.Reader
  55. }
  56. func (ui *stdUI) ReadLine(prompt string) (string, error) {
  57. os.Stdout.WriteString(prompt)
  58. return ui.r.ReadString('\n')
  59. }
  60. func (ui *stdUI) Print(args ...interface{}) {
  61. ui.fprint(os.Stderr, args)
  62. }
  63. func (ui *stdUI) PrintErr(args ...interface{}) {
  64. ui.fprint(os.Stderr, args)
  65. }
  66. func (ui *stdUI) IsTerminal() bool {
  67. return false
  68. }
  69. func (ui *stdUI) WantBrowser() bool {
  70. return true
  71. }
  72. func (ui *stdUI) SetAutoComplete(func(string) string) {
  73. }
  74. func (ui *stdUI) fprint(f *os.File, args []interface{}) {
  75. text := fmt.Sprint(args...)
  76. if !strings.HasSuffix(text, "\n") {
  77. text += "\n"
  78. }
  79. f.WriteString(text)
  80. }
  81. // oswriter implements the Writer interface using a regular file.
  82. type oswriter struct{}
  83. func (oswriter) Open(name string) (io.WriteCloser, error) {
  84. f, err := os.Create(name)
  85. return f, err
  86. }