process.go 3.3 KB

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