浏览代码

Merge pull request #95 from hirochachacha/home

internal/driver: avoid using $HOME directly
Alexey Alexandrov 8 年前
父节点
当前提交
57b03fd3eb
共有 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
 
84
 
85
 * **-text:** Prints the location entries, one per line, including the flat and cum
85
 * **-text:** Prints the location entries, one per line, including the flat and cum
86
   values.
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
 * **-peek= _regex_:** Print the location entry with all its predecessors and
88
 * **-peek= _regex_:** Print the location entry with all its predecessors and
89
   successors, without trimming any entries.
89
   successors, without trimming any entries.
90
 * **-traces:** Prints each sample with a location per line.
90
 * **-traces:** Prints each sample with a location per line.
120
 
120
 
121
 pprof will look for source files on its current working directory and all its
121
 pprof will look for source files on its current working directory and all its
122
 ancestors. pprof will look for binaries on the directories specified in the
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
 pprof uses the binutils tools to examine and disassemble the binaries. By
128
 pprof uses the binutils tools to examine and disassemble the binaries. By
128
 default it will search for those tools in the current path, but it can also
129
 default it will search for those tools in the current path, but it can also

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

268
 	"   PPROF_TOOLS        Search path for object-level tools\n" +
268
 	"   PPROF_TOOLS        Search path for object-level tools\n" +
269
 	"   PPROF_BINARY_PATH  Search path for local binary files\n" +
269
 	"   PPROF_BINARY_PATH  Search path for local binary files\n" +
270
 	"                      default: $HOME/pprof/binaries\n" +
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
 	"os"
25
 	"os"
26
 	"os/exec"
26
 	"os/exec"
27
 	"path/filepath"
27
 	"path/filepath"
28
+	"runtime"
28
 	"strconv"
29
 	"strconv"
29
 	"strings"
30
 	"strings"
30
 	"sync"
31
 	"sync"
214
 	err    error
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
 // setTmpDir prepares the directory to use to save profiles retrieved
229
 // setTmpDir prepares the directory to use to save profiles retrieved
218
 // remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof.
230
 // remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof.
219
 func setTmpDir(ui plugin.UI) (string, error) {
231
 func setTmpDir(ui plugin.UI) (string, error) {
220
 	if profileDir := os.Getenv("PPROF_TMPDIR"); profileDir != "" {
232
 	if profileDir := os.Getenv("PPROF_TMPDIR"); profileDir != "" {
221
 		return profileDir, nil
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
 		if err := os.MkdirAll(tmpDir, 0755); err != nil {
236
 		if err := os.MkdirAll(tmpDir, 0755); err != nil {
225
 			ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
237
 			ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
226
 			continue
238
 			continue
315
 	searchPath := os.Getenv("PPROF_BINARY_PATH")
327
 	searchPath := os.Getenv("PPROF_BINARY_PATH")
316
 	if searchPath == "" {
328
 	if searchPath == "" {
317
 		// Use $HOME/pprof/binaries as default directory for local symbolization binaries
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
 mapping:
332
 mapping:
321
 	for _, m := range p.Mapping {
333
 	for _, m := range p.Mapping {