Skip to content

Commit

Permalink
gops: Stat pidfile to determine if agent is working
Browse files Browse the repository at this point in the history
Fixes #27.
  • Loading branch information
rakyll committed Jan 20, 2017
1 parent 290a9a1 commit 218c61a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 9 additions & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ func guessUnixHomeDir() string {
return os.Getenv("HOME")
}

func GetPort(pid int) (string, error) {
func PIDFile(pid int) (string, error) {
gopsdir, err := ConfigDir()
if err != nil {
return "", err
}
portfile := fmt.Sprintf("%s/%d", gopsdir, pid)
return fmt.Sprintf("%s/%d", gopsdir, pid), nil
}

func GetPort(pid int) (string, error) {
portfile, err := PIDFile(pid)
if err != nil {
return "", err
}
b, err := ioutil.ReadFile(portfile)
if err != nil {
return "", err
Expand Down
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"log"
"os"
"strconv"
"strings"
"sync"

"github.com/google/gops/internal"
"github.com/google/gops/internal/objfile"
ps "github.com/keybase/go-ps"
)
Expand Down Expand Up @@ -113,17 +113,19 @@ func printIfGo(pr ps.Process) {
}

var ok bool
var agent bool
// TODO(jbd): find a faster way to determine Go programs.
for _, s := range symbols {
if s.Name == "runtime.buildVersion" {
ok = true
}
// TODO(jbd): Stat the pid file to determine if the agent is still working.
if strings.HasPrefix(s.Name, "github.com/google/gops/agent") {
agent = true
}
}

var agent bool
pidfile, err := internal.PIDFile(pr.Pid())
if err == nil {
_, err := os.Stat(pidfile)
agent = err == nil
}

if ok {
buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "%d", pr.Pid())
Expand Down

0 comments on commit 218c61a

Please sign in to comment.