2016-11-08 03:29:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-14 06:38:17 +02:00
|
|
|
"errors"
|
2016-11-08 03:29:14 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
2016-11-14 06:04:40 +02:00
|
|
|
"os"
|
2016-11-14 06:38:17 +02:00
|
|
|
"os/exec"
|
2016-11-14 06:04:40 +02:00
|
|
|
|
2016-11-15 09:07:31 +02:00
|
|
|
"github.com/google/gops/internal"
|
2016-11-08 03:29:14 +02:00
|
|
|
"github.com/google/gops/signal"
|
2016-11-14 07:46:10 +02:00
|
|
|
ps "github.com/keybase/go-ps"
|
2016-11-08 03:29:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmds = map[string](func(pid int) error){
|
2016-11-14 07:50:30 +02:00
|
|
|
"stack": stackTrace,
|
|
|
|
"gc": gc,
|
|
|
|
"memstats": memStats,
|
|
|
|
"version": version,
|
|
|
|
"pprof-heap": pprofHeap,
|
|
|
|
"pprof-cpu": pprofCPU,
|
2016-11-14 21:07:06 +02:00
|
|
|
"vitals": vitals,
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func stackTrace(pid int) error {
|
2016-11-28 20:45:02 +02:00
|
|
|
return cmdWithPrint(pid, signal.StackTrace)
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func gc(pid int) error {
|
|
|
|
_, err := cmd(pid, signal.GC)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func memStats(pid int) error {
|
2016-11-28 20:45:02 +02:00
|
|
|
return cmdWithPrint(pid, signal.MemStats)
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func version(pid int) error {
|
2016-11-28 20:45:02 +02:00
|
|
|
return cmdWithPrint(pid, signal.Version)
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-11-14 07:50:30 +02:00
|
|
|
func pprofHeap(pid int) error {
|
|
|
|
return pprof(pid, signal.HeapProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pprofCPU(pid int) error {
|
|
|
|
fmt.Println("Profiling CPU now, will take 30 secs...")
|
|
|
|
return pprof(pid, signal.CPUProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pprof(pid int, p byte) error {
|
|
|
|
out, err := cmd(pid, p)
|
2016-11-14 06:04:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 10:56:20 +02:00
|
|
|
if len(out) == 0 {
|
2016-11-14 06:38:17 +02:00
|
|
|
return errors.New("failed to read the profile")
|
|
|
|
}
|
2017-01-03 10:56:20 +02:00
|
|
|
tmpfile, err := ioutil.TempFile("", "profile")
|
2016-11-14 06:04:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-14 06:38:17 +02:00
|
|
|
defer os.Remove(tmpfile.Name())
|
2017-01-03 10:56:20 +02:00
|
|
|
if err := ioutil.WriteFile(tmpfile.Name(), out, 0); err != nil {
|
2016-11-14 06:04:40 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-11-14 07:46:10 +02:00
|
|
|
process, err := ps.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(jbd): add context to the error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
binary, err := process.Path()
|
|
|
|
if err != nil {
|
2016-11-27 04:43:04 +02:00
|
|
|
return fmt.Errorf("cannot the binary for the PID: %v", err)
|
2016-11-14 07:46:10 +02:00
|
|
|
}
|
|
|
|
cmd := exec.Command("go", "tool", "pprof", binary, tmpfile.Name())
|
2016-11-14 06:04:40 +02:00
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:07:06 +02:00
|
|
|
func vitals(pid int) error {
|
2016-11-28 20:45:02 +02:00
|
|
|
return cmdWithPrint(pid, signal.Vitals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdWithPrint(pid int, c byte) error {
|
|
|
|
out, err := cmd(pid, c)
|
2016-11-14 21:07:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 10:56:20 +02:00
|
|
|
fmt.Printf("%s", out)
|
2016-11-14 21:07:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:56:20 +02:00
|
|
|
func cmd(pid int, c byte) ([]byte, error) {
|
2016-11-15 09:07:31 +02:00
|
|
|
port, err := internal.GetPort(pid)
|
2016-11-15 08:59:09 +02:00
|
|
|
if err != nil {
|
2017-01-03 10:56:20 +02:00
|
|
|
return nil, err
|
2016-11-15 08:59:09 +02:00
|
|
|
}
|
|
|
|
conn, err := net.Dial("tcp", "127.0.0.1:"+port)
|
2016-11-08 03:29:14 +02:00
|
|
|
if err != nil {
|
2017-01-03 10:56:20 +02:00
|
|
|
return nil, err
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|
|
|
|
if _, err := conn.Write([]byte{c}); err != nil {
|
2017-01-03 10:56:20 +02:00
|
|
|
return nil, err
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|
|
|
|
all, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
2017-01-03 10:56:20 +02:00
|
|
|
return nil, err
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|
2017-01-03 10:56:20 +02:00
|
|
|
return all, nil
|
2016-11-08 03:29:14 +02:00
|
|
|
}
|