浏览代码

internal/driver: avoid using $HOME directly

Not all OSes have $HOME variable.
Use appropriate variables per OS.

Fixes https://github.com/golang/go/issues/18864
Hiroshi Ioka 8 年前
父节点
当前提交
f90721db3d
共有 3 个文件被更改,包括 21 次插入7 次删除
  1. 5
    4
      doc/pprof.md
  2. 2
    1
      internal/driver/cli.go
  3. 14
    2
      internal/driver/fetch.go

+ 5
- 4
doc/pprof.md 查看文件

@@ -84,7 +84,7 @@ pprof text reports show the location hierarchy in text format.
84 84
 
85 85
 * **-text:** Prints the location entries, one per line, including the flat and cum
86 86
   values.
87
-* **-tree:** Prints each location entry with its predecessors and successors. 
87
+* **-tree:** Prints each location entry with its predecessors and successors.
88 88
 * **-peek= _regex_:** Print the location entry with all its predecessors and
89 89
   successors, without trimming any entries.
90 90
 * **-traces:** Prints each sample with a location per line.
@@ -120,9 +120,10 @@ profile must contain data with the appropriate level of detail.
120 120
 
121 121
 pprof will look for source files on its current working directory and all its
122 122
 ancestors. pprof will look for binaries on the directories specified in the
123
-`$PPROF_BINARY_PATH` environment variable, by default `$HOME/pprof/binaries`. It
124
-will look binaries up by name, and if the profile includes linker build ids, it
125
-will also search for them in a directory named as the build id.
123
+`$PPROF_BINARY_PATH` environment variable, by default `$HOME/pprof/binaries`
124
+(`%USERPROFILE%\pprof\binaries` on Windows). It will look binaries up by name,
125
+and if the profile includes linker build ids, it will also search for them in
126
+a directory named as the build id.
126 127
 
127 128
 pprof uses the binutils tools to examine and disassemble the binaries. By
128 129
 default it will search for those tools in the current path, but it can also

+ 2
- 1
internal/driver/cli.go 查看文件

@@ -268,4 +268,5 @@ var usageMsgVars = "\n\n" +
268 268
 	"   PPROF_TOOLS        Search path for object-level tools\n" +
269 269
 	"   PPROF_BINARY_PATH  Search path for local binary files\n" +
270 270
 	"                      default: $HOME/pprof/binaries\n" +
271
-	"                      finds binaries by $name and $buildid/$name\n"
271
+	"                      finds binaries by $name and $buildid/$name\n" +
272
+	"   * On Windows, %USERPROFILE% is used instead of $HOME"

+ 14
- 2
internal/driver/fetch.go 查看文件

@@ -25,6 +25,7 @@ import (
25 25
 	"os"
26 26
 	"os/exec"
27 27
 	"path/filepath"
28
+	"runtime"
28 29
 	"strconv"
29 30
 	"strings"
30 31
 	"sync"
@@ -214,13 +215,24 @@ type profileSource struct {
214 215
 	err    error
215 216
 }
216 217
 
218
+func homeEnv() string {
219
+	switch runtime.GOOS {
220
+	case "windows":
221
+		return "USERPROFILE"
222
+	case "plan9":
223
+		return "home"
224
+	default:
225
+		return "HOME"
226
+	}
227
+}
228
+
217 229
 // setTmpDir prepares the directory to use to save profiles retrieved
218 230
 // remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof.
219 231
 func setTmpDir(ui plugin.UI) (string, error) {
220 232
 	if profileDir := os.Getenv("PPROF_TMPDIR"); profileDir != "" {
221 233
 		return profileDir, nil
222 234
 	}
223
-	for _, tmpDir := range []string{os.Getenv("HOME") + "/pprof", os.TempDir()} {
235
+	for _, tmpDir := range []string{os.Getenv(homeEnv()) + "/pprof", os.TempDir()} {
224 236
 		if err := os.MkdirAll(tmpDir, 0755); err != nil {
225 237
 			ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
226 238
 			continue
@@ -315,7 +327,7 @@ func locateBinaries(p *profile.Profile, s *source, obj plugin.ObjTool, ui plugin
315 327
 	searchPath := os.Getenv("PPROF_BINARY_PATH")
316 328
 	if searchPath == "" {
317 329
 		// Use $HOME/pprof/binaries as default directory for local symbolization binaries
318
-		searchPath = filepath.Join(os.Getenv("HOME"), "pprof", "binaries")
330
+		searchPath = filepath.Join(os.Getenv(homeEnv()), "pprof", "binaries")
319 331
 	}
320 332
 mapping:
321 333
 	for _, m := range p.Mapping {