mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 18:20:27 +00:00
Since https://github.com/weaveworks/tcptracer-bpf/pull/39, tcptracer-bpf can generate "fd_install" events when a process installs a new file descriptor in its fd table. Those events must be requested explicitely on a per-pid basis with tracer.AddFdInstallWatcher(pid). This is useful to know about "accept" events that would otherwise be missed because kretprobes are not triggered for functions that were called before the installation of the kretprobe. This patch find all the processes that are currently blocked on an accept() syscall during the EbpfTracker initialization. feedInitialConnections() will use tracer.AddFdInstallWatcher() to subscribe to fd_install events. When a fd_install event is received, synthesise an accept event with the connection tuple and the network namespace (from /proc).
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package process_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/weaveworks/common/test"
|
|
"github.com/weaveworks/scope/probe/process"
|
|
)
|
|
|
|
func TestBasicWalk(t *testing.T) {
|
|
var (
|
|
procRoot = "/proc"
|
|
procFunc = func(process.Process, process.Process) {}
|
|
)
|
|
if err := process.NewWalker(procRoot, false).Walk(procFunc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestCache(t *testing.T) {
|
|
processes := []process.Process{
|
|
{PID: 1, PPID: 0, Name: "init", Cmdline: "init"},
|
|
{PID: 2, PPID: 1, Name: "bash", Cmdline: "bash"},
|
|
{PID: 3, PPID: 1, Name: "apache", Threads: 2, Cmdline: "apache"},
|
|
{PID: 4, PPID: 2, Name: "ping", Cmdline: "ping foo.bar.local"},
|
|
}
|
|
walker := &mockWalker{
|
|
processes: processes,
|
|
}
|
|
cachingWalker := process.NewCachingWalker(walker)
|
|
err := cachingWalker.Tick()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
want, err := all(walker)
|
|
have, err := all(cachingWalker)
|
|
if err != nil || !reflect.DeepEqual(want, have) {
|
|
t.Errorf("%v (%v)", test.Diff(want, have), err)
|
|
}
|
|
|
|
walker.processes = []process.Process{}
|
|
have, err = all(cachingWalker)
|
|
if err != nil || !reflect.DeepEqual(want, have) {
|
|
t.Errorf("%v (%v)", test.Diff(want, have), err)
|
|
}
|
|
|
|
err = cachingWalker.Tick()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
have, err = all(cachingWalker)
|
|
want = map[process.Process]struct{}{}
|
|
if err != nil || !reflect.DeepEqual(want, have) {
|
|
t.Errorf("%v (%v)", test.Diff(want, have), err)
|
|
}
|
|
}
|
|
|
|
func all(w process.Walker) (map[process.Process]struct{}, error) {
|
|
all := map[process.Process]struct{}{}
|
|
err := w.Walk(func(p, _ process.Process) {
|
|
all[p] = struct{}{}
|
|
})
|
|
return all, err
|
|
}
|