github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/agent/subprocesses.go (about) 1 package agent 2 3 import "github.com/mitchellh/go-ps" 4 5 func findAllSubprocesses(pid int) []int { 6 res := []int{} 7 8 childrenLookup := map[int][]int{} 9 processes, err := ps.Processes() 10 if err != nil { 11 // TODO: handle 12 return res 13 } 14 for _, p := range processes { 15 ppid := p.PPid() 16 if _, ok := childrenLookup[ppid]; !ok { 17 childrenLookup[ppid] = []int{} 18 } 19 childrenLookup[ppid] = append(childrenLookup[ppid], p.Pid()) 20 } 21 22 todo := []int{pid} 23 for len(todo) > 0 { 24 parentPid := todo[0] 25 todo = todo[1:] 26 27 if children, ok := childrenLookup[parentPid]; ok { 28 for _, childPid := range children { 29 res = append(res, childPid) 30 todo = append(todo, childPid) 31 } 32 } 33 } 34 35 return res 36 }