process.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package containers
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/coroot/coroot-node-agent/ebpftracer/tracer/jattach"
  6. "github.com/coroot/coroot-node-agent/utils"
  7. . "github.com/coroot/coroot-node-agent/utils/modelse"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/jpillora/backoff"
  13. "github.com/cilium/ebpf/link"
  14. "github.com/coroot/coroot-node-agent/proc"
  15. "github.com/mdlayher/taskstats"
  16. )
  17. type Process struct {
  18. Pid uint32
  19. StartedAt time.Time
  20. NetNsId string
  21. ctx context.Context
  22. cancelFunc context.CancelFunc
  23. dotNetMonitor *DotNetMonitor
  24. uprobes []link.Link
  25. goTlsUprobesChecked bool
  26. openSslUprobesChecked bool
  27. jvmUprobesChecked bool
  28. stackUprobesChecked bool
  29. codeType CodeType
  30. cmdline string
  31. }
  32. func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
  33. ns, err := proc.GetNetNs(pid)
  34. if err != nil {
  35. return nil
  36. }
  37. defer ns.Close()
  38. p := &Process{Pid: pid, StartedAt: stats.BeginTime, NetNsId: ns.UniqueId()}
  39. p.ctx, p.cancelFunc = context.WithCancel(context.Background())
  40. go p.instrument()
  41. return p
  42. }
  43. func (p *Process) isHostNs() bool {
  44. return p.NetNsId == hostNetNsId
  45. }
  46. func (p *Process) instrument() {
  47. b := backoff.Backoff{Factor: 2, Min: time.Second, Max: time.Minute}
  48. for {
  49. select {
  50. case <-p.ctx.Done():
  51. return
  52. default:
  53. dest, err := os.Readlink(proc.Path(p.Pid, "exe"))
  54. if err != nil {
  55. return
  56. }
  57. if dest != "/" {
  58. if dotNetAppName, err := dotNetApp(p.Pid); err == nil {
  59. if dotNetAppName != "" {
  60. p.dotNetMonitor = NewDotNetMonitor(p.ctx, p.Pid, dotNetAppName)
  61. }
  62. return
  63. }
  64. }
  65. time.Sleep(b.Duration())
  66. }
  67. }
  68. }
  69. func (p *Process) Close() {
  70. p.cancelFunc()
  71. for _, u := range p.uprobes {
  72. _ = u.Close()
  73. }
  74. }
  75. func (p *Process) DynamicClose() {
  76. for _, u := range p.uprobes {
  77. _ = u.Close()
  78. }
  79. p.goTlsUprobesChecked = false
  80. p.openSslUprobesChecked = false
  81. p.jvmUprobesChecked = false
  82. if p.codeType.IsJvmCode() && p.stackUprobesChecked {
  83. nativeBasePath := utils.GetDefaultAgentsPath("NativeAgent")
  84. kvPairs := []string{
  85. fmt.Sprintf("%s=%s", filepath.Join(nativeBasePath, "lib", "apmAgent.jar"), nativeBasePath),
  86. "UNINSTALL",
  87. }
  88. argkv := strings.Join(kvPairs, ",")
  89. args := []string{"load", "instrument", "false", argkv}
  90. jattacher := jattach.JvmJattacher{
  91. Pid: p.Pid,
  92. Args: args,
  93. PrintOutput: 1,
  94. }
  95. _, err := jattacher.JDetach()
  96. if err == nil {
  97. p.stackUprobesChecked = false
  98. }
  99. }
  100. p.uprobes = []link.Link{}
  101. }
  102. func (p *Process) GetCmdline() string {
  103. return p.cmdline
  104. }