process.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package containers
  2. import (
  3. "context"
  4. "github.com/coroot/coroot-node-agent/common"
  5. "os"
  6. "time"
  7. "github.com/jpillora/backoff"
  8. "github.com/cilium/ebpf/link"
  9. "github.com/coroot/coroot-node-agent/proc"
  10. "github.com/mdlayher/taskstats"
  11. )
  12. type Process struct {
  13. Pid uint32
  14. StartedAt time.Time
  15. NetNsId string
  16. ctx context.Context
  17. cancelFunc context.CancelFunc
  18. dotNetMonitor *DotNetMonitor
  19. uprobes []link.Link
  20. goTlsUprobesChecked bool
  21. openSslUprobesChecked bool
  22. jvmUprobesChecked bool
  23. stackUprobesChecked bool
  24. codeType common.CodeType
  25. }
  26. func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
  27. ns, err := proc.GetNetNs(pid)
  28. if err != nil {
  29. return nil
  30. }
  31. defer ns.Close()
  32. p := &Process{Pid: pid, StartedAt: stats.BeginTime, NetNsId: ns.UniqueId()}
  33. p.ctx, p.cancelFunc = context.WithCancel(context.Background())
  34. go p.instrument()
  35. return p
  36. }
  37. func (p *Process) isHostNs() bool {
  38. return p.NetNsId == hostNetNsId
  39. }
  40. func (p *Process) instrument() {
  41. b := backoff.Backoff{Factor: 2, Min: time.Second, Max: time.Minute}
  42. for {
  43. select {
  44. case <-p.ctx.Done():
  45. return
  46. default:
  47. dest, err := os.Readlink(proc.Path(p.Pid, "exe"))
  48. if err != nil {
  49. return
  50. }
  51. if dest != "/" {
  52. if dotNetAppName, err := dotNetApp(p.Pid); err == nil {
  53. if dotNetAppName != "" {
  54. p.dotNetMonitor = NewDotNetMonitor(p.ctx, p.Pid, dotNetAppName)
  55. }
  56. return
  57. }
  58. }
  59. time.Sleep(b.Duration())
  60. }
  61. }
  62. }
  63. func (p *Process) Close() {
  64. p.cancelFunc()
  65. for _, u := range p.uprobes {
  66. _ = u.Close()
  67. }
  68. }