| 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 | // pprof is a tool for collection, manipulation and visualization |
| 16 | // of performance profiles. |
| 17 | package mainscip-go gomod github.com/google/pprof . `github.com/google/pprof`/ |
| 18 | |
| 19 | import ( |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 21 | "osscip-go gomod github.com/golang/go/src go1.24.0 os/" |
| 22 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 23 | "syscallscip-go gomod github.com/golang/go/src go1.24.0 syscall/" |
| 24 | |
| 25 | "github.com/chzyer/readlinescip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/" |
| 26 | "github.com/google/pprof/driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/" |
| 27 | ) |
| 28 | |
| 29 | func mainscip-go gomod github.com/google/pprof . `github.com/google/pprof`/main().() { |
| 30 | if err := driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/.PProfscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/PProf().(&driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/.Optionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/Options#{UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/Options#UI.: newUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/newUI().()}); err != nil { |
| 31 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.24.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.24.0 os/Stderr., "pprof: %v\n", err) |
| 32 | osscip-go gomod github.com/golang/go/src go1.24.0 os/.Exitscip-go gomod github.com/golang/go/src go1.24.0 os/Exit().(2) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // readlineUI implements the driver.UI interface using the |
| 37 | // github.com/chzyer/readline library. |
| 38 | // This is contained in pprof.go to avoid adding the readline |
| 39 | // dependency in the vendored copy of pprof in the Go distribution, |
| 40 | // which does not use this file. |
| 41 | type readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI# struct { |
| 42 | rlscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#rl. *readlinescip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/.Instancescip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/Instance# |
| 43 | } |
| 44 | |
| 45 | func newUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/newUI().() driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/.UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/driver`/UI# { |
| 46 | rl, err := readlinescip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/.Newscip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/New().("") |
| 47 | if err != nil { |
| 48 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.24.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.24.0 os/Stderr., "readline: %v", err) |
| 49 | return nil |
| 50 | } |
| 51 | return &readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#{ |
| 52 | rlscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#rl.: rl, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // ReadLine returns a line of text (a command) read from the user. |
| 57 | // prompt is printed before reading the command. |
| 58 | func (r *readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#) ReadLinescip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#ReadLine().(prompt string) (string, error) { |
| 59 | r.rlscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#rl..SetPromptscip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/Instance#SetPrompt().(prompt) |
| 60 | return r.rlscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#rl..Readlinescip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/Instance#Readline().() |
| 61 | } |
| 62 | |
| 63 | // Print shows a message to the user. |
| 64 | // It is printed over stderr as stdout is reserved for regular output. |
| 65 | func (r *readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#) Printscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#Print().(args ...interface{}) { |
| 66 | text := fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprint().(args...) |
| 67 | if !stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasSuffix().(text, "\n") { |
| 68 | text += "\n" |
| 69 | } |
| 70 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprint().(r.rlscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#rl..Stderrscip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/Instance#Stderr().(), text) |
| 71 | } |
| 72 | |
| 73 | // PrintErr shows a message to the user, colored in red for emphasis. |
| 74 | // It is printed over stderr as stdout is reserved for regular output. |
| 75 | func (r *readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#) PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#PrintErr().(args ...interface{}) { |
| 76 | text := fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprint().(args...) |
| 77 | if !stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasSuffix().(text, "\n") { |
| 78 | text += "\n" |
| 79 | } |
| 80 | if readlinescip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/.IsTerminalscip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/IsTerminal().(int(syscallscip-go gomod github.com/golang/go/src go1.24.0 syscall/.Stderrscip-go gomod github.com/golang/go/src go1.24.0 syscall/Stderr.)) { |
| 81 | text = colorizescip-go gomod github.com/google/pprof . `github.com/google/pprof`/colorize().(text) |
| 82 | } |
| 83 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprint().(r.rlscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#rl..Stderrscip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/Instance#Stderr().(), text) |
| 84 | } |
| 85 | |
| 86 | // colorize the msg using ANSI color escapes. |
| 87 | func colorizescip-go gomod github.com/google/pprof . `github.com/google/pprof`/colorize().(msg string) string { |
| 88 | var red = 31 |
| 89 | var colorEscape = fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\033[0;%dm", red) |
| 90 | var colorResetEscape = "\033[0m" |
| 91 | return colorEscape + msg + colorResetEscape |
| 92 | } |
| 93 | |
| 94 | // IsTerminal returns whether the UI is known to be tied to an |
| 95 | // interactive terminal (as opposed to being redirected to a file). |
| 96 | func (r *readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#) IsTerminalscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#IsTerminal().() bool { |
| 97 | return readlinescip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/.IsTerminalscip-go gomod github.com/chzyer/readline v1.5.1 `github.com/chzyer/readline`/IsTerminal().(int(syscallscip-go gomod github.com/golang/go/src go1.24.0 syscall/.Stdoutscip-go gomod github.com/golang/go/src go1.24.0 syscall/Stdout.)) |
| 98 | } |
| 99 | |
| 100 | // WantBrowser starts a browser on interactive mode. |
| 101 | func (r *readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#) WantBrowserscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#WantBrowser().() bool { |
| 102 | return r.IsTerminalscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#IsTerminal().() |
| 103 | } |
| 104 | |
| 105 | // SetAutoComplete instructs the UI to call complete(cmd) to obtain |
| 106 | // the auto-completion of cmd, if the UI supports auto-completion at all. |
| 107 | func (r *readlineUIscip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#) SetAutoCompletescip-go gomod github.com/google/pprof . `github.com/google/pprof`/readlineUI#SetAutoComplete().(complete func(string) string) { |
| 108 | // TODO: Implement auto-completion support. |
| 109 | } |