1
0
mirror of https://github.com/google/gops.git synced 2024-11-27 08:31:17 +02:00
gops/gops.go
2016-11-03 17:52:29 -07:00

67 lines
1.2 KiB
Go

// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Program gops is a tool to list currently running Go processes.
package main
import (
"fmt"
"log"
"hello/gops/internal/objfile"
ps "github.com/keybase/go-ps"
)
func main() {
pss, err := ps.Processes()
if err != nil {
log.Fatal(err)
}
var undetermined int
for _, pr := range pss {
name, err := pr.Path()
if err != nil {
undetermined++
continue
}
ok, err := isGo(name)
if err != nil {
continue
}
if ok {
fmt.Printf("%d\t%v\n", pr.Pid(), pr.Executable())
}
}
if undetermined > 0 {
fmt.Printf("\n%d processes left undetermined\n", undetermined)
}
}
func isGo(filename string) (ok bool, err error) {
obj, err := objfile.Open(filename)
if err != nil {
return false, err
}
defer obj.Close()
symbols, err := obj.Symbols()
if err != nil {
return false, err
}
// TODO(jbd): find a faster way to determine Go programs
// looping over the symbols is a joke.
for _, s := range symbols {
if s.Name == "runtime.buildVersion" {
return true, nil
}
}
return false, nil
}