From fb54abe9ce3cbec6d464c238406b05502cb34eeb Mon Sep 17 00:00:00 2001 From: Hiroshi Ioka Date: Tue, 19 Sep 2017 18:18:09 +0900 Subject: all: correct location of go tool In general, there are no guarantee that `go` command exist on $PATH. This CL tries to get `go` command from $GOROOT/bin instead. There are three kinds of code we should handle: For normal code, the CL implements goCmd() or goCmdName(). For unit tests, the CL uses testenv.GoTool() or testenv.GoToolPath(). For integration tests, the CL sets PATH=$GOROOT/bin:$PATH in cmd/dist. Note that make.bash sets PATH=$GOROOT/bin:$PATH in the build process. So this change is only useful when we use toolchain manually. Updates #21875 Change-Id: I963b9f22ea732dd735363ececde4cf94a5db5ca2 Reviewed-on: https://go-review.googlesource.com/64650 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/trace/pprof.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/cmd/trace') diff --git a/src/cmd/trace/pprof.go b/src/cmd/trace/pprof.go index 40803ac5f9..47be2a6d1c 100644 --- a/src/cmd/trace/pprof.go +++ b/src/cmd/trace/pprof.go @@ -15,10 +15,24 @@ import ( "net/http" "os" "os/exec" + "path/filepath" + "runtime" "github.com/google/pprof/profile" ) +func goCmd() string { + var exeSuffix string + if runtime.GOOS == "windows" { + exeSuffix = ".exe" + } + path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix) + if _, err := os.Stat(path); err == nil { + return path + } + return "go" +} + func init() { http.HandleFunc("/io", serveSVGProfile(pprofIO)) http.HandleFunc("/block", serveSVGProfile(pprofBlock)) @@ -147,7 +161,7 @@ func serveSVGProfile(prof func(w io.Writer) error) http.HandlerFunc { return } svgFilename := blockf.Name() + ".svg" - if output, err := exec.Command("go", "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil { + if output, err := exec.Command(goCmd(), "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil { http.Error(w, fmt.Sprintf("failed to execute go tool pprof: %v\n%s", err, output), http.StatusInternalServerError) return } -- cgit v1.3-6-g1900