-
Notifications
You must be signed in to change notification settings - Fork 3
/
debug.go
67 lines (61 loc) · 1.42 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"net"
"net/http"
"os"
"runtime/pprof"
"runtime/trace"
_ "net/http/pprof"
)
func startDebug() {
port := os.Getenv("LMHTTPPROF")
if port == "" {
port = "6060"
}
go func() {
err := http.ListenAndServe("localhost:"+port, nil)
if err != nil {
if oerr, ok := err.(*net.OpError); ok && oerr.Op == "listen" {
if ierr, ok := oerr.Err.(*os.SyscallError); ok && ierr.Err.Error() == "address already in use" {
err := http.ListenAndServe("localhost:0", nil)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to http.ListenAndServe: %s\n", err)
}
}
} else {
fmt.Fprintf(os.Stderr, "failed to http.ListenAndServe: %s\n", err)
}
}
}()
if path := os.Getenv("LMTRACE"); path != "" {
fh, err := os.Create(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't open LMTRACE (%s): %s\n", path, err)
os.Exit(1)
}
err = trace.Start(fh)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to trace.Start: %s\n", err)
}
}
if path := os.Getenv("LMPROF"); path != "" {
fh, err := os.Create(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't open LMPROF (%s): %s\n", path, err)
os.Exit(1)
}
err = pprof.StartCPUProfile(fh)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to pprof.StartCPUProfile: %s\n", err)
}
}
}
func stopDebug() {
if os.Getenv("LMTRACE") != "" {
trace.Stop()
}
if os.Getenv("LMPROF") != "" {
pprof.StopCPUProfile()
}
}