process.go 1.5 KB

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