1
0
mirror of https://github.com/google/gops.git synced 2024-11-24 08:22:25 +02:00
gops/cmd.go

65 lines
1.0 KiB
Go
Raw Normal View History

2016-11-08 03:29:14 +02:00
package main
import (
"fmt"
"io/ioutil"
"net"
"github.com/google/gops/signal"
)
var cmds = map[string](func(pid int) error){
"stack": stackTrace,
"gc": gc,
"memstats": memStats,
"version": version,
}
func stackTrace(pid int) error {
out, err := cmd(pid, signal.StackTrace)
if err != nil {
return err
}
fmt.Println(out)
return nil
}
func gc(pid int) error {
_, err := cmd(pid, signal.GC)
return err
}
func memStats(pid int) error {
out, err := cmd(pid, signal.MemStats)
if err != nil {
return err
}
fmt.Printf(out)
return nil
}
func version(pid int) error {
out, err := cmd(pid, signal.Version)
if err != nil {
return err
}
fmt.Printf(out)
return nil
}
func cmd(pid int, c byte) (string, error) {
sock := fmt.Sprintf("/tmp/gops%d.sock", pid)
conn, err := net.Dial("unix", sock)
if err != nil {
return "", err
}
if _, err := conn.Write([]byte{c}); err != nil {
return "", err
}
all, err := ioutil.ReadAll(conn)
if err != nil {
return "", err
}
return string(all), nil
}