Merge pull request #776 from weaveworks/tracer

Tracer!
This commit is contained in:
Tom Wilkie
2015-12-17 13:55:10 +00:00
31 changed files with 2004 additions and 11 deletions
+28
View File
@@ -7,6 +7,7 @@ import (
// Tree represents all processes on the machine.
type Tree interface {
GetParent(pid int) (int, error)
GetChildren(pid int) ([]int, error)
}
type tree struct {
@@ -32,3 +33,30 @@ func (pt *tree) GetParent(pid int) (int, error) {
return proc.PPID, nil
}
// GetChildren
func (pt *tree) GetChildren(pid int) ([]int, error) {
_, ok := pt.processes[pid]
if !ok {
return []int{}, fmt.Errorf("PID %d not found", pid)
}
var isChild func(id int) bool
isChild = func(id int) bool {
p, ok := pt.processes[id]
if !ok || p.PPID == 0 {
return false
} else if p.PPID == pid {
return true
}
return isChild(p.PPID)
}
children := []int{pid}
for id := range pt.processes {
if isChild(id) {
children = append(children, id)
}
}
return children, nil
}