gops

gops is a command to list and diagnose Go processes currently running on your system.

It is possible to use gops tool both in local and remote mode.

Local mode requires that you start the target binary as the same user that runs gops binary. To use gops in a remote mode you need to know target’s agent address.

In Local mode use process’s PID as a target; in Remote mode target is a host:port combination.

Using

❯ gops
128   1    com.docker.vmnetd  go1.13.10 /Library/PrivilegedHelperTools/com.docker.vmnetd
1233  1    updater            go1.13.7  /Applications/Keybase.app/Contents/SharedSupport/bin/updater
11653 1479 gops               go1.15.2  /Users/pepa/go/bin/gops
1279  1    kbfs               go1.13.7  /Applications/Keybase.app/Contents/SharedSupport/bin/kbfs
1253  1    keybase            go1.13.7  /Applications/Keybase.app/Contents/SharedSupport/bin/keybase
❯ gops 1253
parent PID:	1
threads:	24
memory usage:	0.646%
cpu usage:	0.140%
username:	pepa
cmd+args:	/Applications/Keybase.app/Contents/SharedSupport/bin/keybase -d --log-file=/Users/pepa/Library/Logs/keybase.service.log service
elapsed time:	03-02:55:27
local/remote:	127.0.0.1:16873 <-> :0 (LISTEN)
local/remote:	192.168.0.227:52010 <-> 50.16.218.172:443 (CLOSED)
local/remote:	192.168.0.227:64850 <-> 50.16.218.172:443 (ESTABLISHED)
local/remote:	192.168.0.227:60994 <-> 52.202.29.121:443 (CLOSED)
local/remote:	127.0.0.1:16873 <-> 127.0.0.1:64871 (ESTABLISHED)
local/remote:	127.0.0.1:16873 <-> 127.0.0.1:64872 (ESTABLISHED)
local/remote:	127.0.0.1:16873 <-> 127.0.0.1:64873 (ESTABLISHED)
local/remote:	127.0.0.1:16873 <-> 127.0.0.1:64874 (ESTABLISHED)
local/remote:	127.0.0.1:16873 <-> 127.0.0.1:64875 (ESTABLISHED)
local/remote:	127.0.0.1:16873 <-> 127.0.0.1:64876 (ESTABLISHED)

Diagnostic

For processes that starts the diagnostics agent, gops can report additional information such as the current stack trace, Go version, memory stats, etc.

package main

import (
	"log"
	"time"

	"github.com/google/gops/agent"
)

func main() {
	if err := agent.Listen(agent.Options{}); err != nil {
		log.Fatal(err)
	}
	time.Sleep(time.Hour)
}
go run .

And than in another terminal:

❯ gops
128   1     com.docker.vmnetd  go1.13.10 /Library/PrivilegedHelperTools/com.docker.vmnetd
12107 12092 goagent          * go1.15.2  /private/var/folders/v3/b9nghy8j1_956wvhnhd3x9yh0000gn/T/go-build151901398/b001/exe/goagent
12108 11958 gops               go1.15.2  /Users/pepa/go/bin/gops
12092 1479  go                 go1.15.2  /usr/local/Cellar/go/1.15.2/libexec/bin/go
1233  1     updater            go1.13.7  /Applications/Keybase.app/Contents/SharedSupport/bin/updater
1279  1     kbfs               go1.13.7  /Applications/Keybase.app/Contents/SharedSupport/bin/kbfs
1253  1     keybase            go1.13.7  /Applications/Keybase.app/Contents/SharedSupport/bin/keybase

❯ gops stack 12107
goroutine 6 [running]:
runtime/pprof.writeGoroutineStacks(0x1191080, 0xc000094000, 0x0, 0x0)
	/usr/local/Cellar/go/1.15.2/libexec/src/runtime/pprof/pprof.go:693 +0x9f
runtime/pprof.writeGoroutine(0x1191080, 0xc000094000, 0x2, 0x0, 0x0)
	/usr/local/Cellar/go/1.15.2/libexec/src/runtime/pprof/pprof.go:682 +0x45
runtime/pprof.(*Profile).WriteTo(0x12520c0, 0x1191080, 0xc000094000, 0x2, 0xc000094000, 0x0)
	/usr/local/Cellar/go/1.15.2/libexec/src/runtime/pprof/pprof.go:331 +0x3f2
github.com/google/gops/agent.handle(0x17b5ff8, 0xc000094000, 0xc000018328, 0x1, 0x1, 0x0, 0x0)
	/Users/pepa/go/pkg/mod/github.com/google/gops@v0.3.12/agent/agent.go:189 +0x1b8
github.com/google/gops/agent.listen()
	/Users/pepa/go/pkg/mod/github.com/google/gops@v0.3.12/agent/agent.go:133 +0x2bd
created by github.com/google/gops/agent.Listen
	/Users/pepa/go/pkg/mod/github.com/google/gops@v0.3.12/agent/agent.go:111 +0x365

goroutine 1 [sleep]:
time.Sleep(0x34630b8a000)
	/usr/local/Cellar/go/1.15.2/libexec/src/runtime/time.go:188 +0xbf
main.main()
	/Users/pepa/test/go/goagent/main.go:14 +0x98

Publikováno v Go