process.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. stackUprobes []link.Link
  29. goTlsUprobesChecked bool
  30. openSslUprobesChecked bool
  31. jvmAttachOnce bool
  32. stackAttachOnce bool
  33. stackStatus StackStatus
  34. versionFailed bool
  35. codeType CodeType
  36. cmdline string
  37. pythonGilChecked bool
  38. }
  39. func NewProcess(pid uint32, stats *taskstats.Stats, tracer *ebpftracer.Tracer) *Process {
  40. p := &Process{Pid: pid, StartedAt: stats.BeginTime}
  41. p.ctx, p.cancelFunc = context.WithCancel(context.Background())
  42. go p.instrument(tracer)
  43. return p
  44. }
  45. func (p *Process) NetNsId() string {
  46. if p.netNsId == "" {
  47. ns, err := proc.GetNetNs(p.Pid)
  48. if err != nil {
  49. return ""
  50. }
  51. p.netNsId = ns.UniqueId()
  52. _ = ns.Close()
  53. }
  54. return p.netNsId
  55. }
  56. func (p *Process) isHostNs() bool {
  57. return p.NetNsId() == hostNetNsId
  58. }
  59. func (p *Process) instrument(tracer *ebpftracer.Tracer) {
  60. b := backoff.Backoff{Factor: 2, Min: time.Second, Max: time.Minute}
  61. for {
  62. select {
  63. case <-p.ctx.Done():
  64. return
  65. default:
  66. dest, err := os.Readlink(proc.Path(p.Pid, "exe"))
  67. if err != nil {
  68. return
  69. }
  70. if dest != "/" {
  71. p.instrumentPython(tracer)
  72. if dotNetAppName, err := dotNetApp(p.Pid); err == nil {
  73. if dotNetAppName != "" {
  74. p.dotNetMonitor = NewDotNetMonitor(p.ctx, p.Pid, dotNetAppName)
  75. }
  76. }
  77. return
  78. }
  79. time.Sleep(b.Duration())
  80. }
  81. }
  82. }
  83. func (p *Process) instrumentPython(tracer *ebpftracer.Tracer) {
  84. if p.pythonGilChecked {
  85. return
  86. }
  87. p.pythonGilChecked = true
  88. cmdline := proc.GetCmdline(p.Pid)
  89. if len(cmdline) == 0 {
  90. return
  91. }
  92. parts := bytes.Split(cmdline, []byte{0})
  93. cmd := parts[0]
  94. if len(cmd) == 0 {
  95. return
  96. }
  97. cmd = bytes.TrimSuffix(bytes.Fields(cmd)[0], []byte{':'})
  98. if !pythonCmd.Match(cmd) {
  99. return
  100. }
  101. p.uprobes = append(p.uprobes, tracer.AttachPythonThreadLockProbes(p.Pid)...)
  102. }
  103. func (p *Process) Close() {
  104. p.cancelFunc()
  105. for _, u := range p.uprobes {
  106. _ = u.Close()
  107. }
  108. }
  109. func (p *Process) closeStackUprobes() error {
  110. for _, u := range p.stackUprobes {
  111. err := u.Close()
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. p.stackUprobes = []link.Link{}
  117. p.stackStatus.StackUprobesClose()
  118. return nil
  119. }
  120. func (p *Process) DynamicClose(closeType int) {
  121. for _, u := range p.uprobes {
  122. _ = u.Close()
  123. }
  124. //p.goTlsUprobesChecked = false
  125. //p.openSslUprobesChecked = false
  126. //p.jvmUprobesChecked = false
  127. //if p.codeType.IsJvmCode() && p.stackUprobesChecked {
  128. // nativeBasePath := utils.GetDefaultAgentsPath("NativeAgent")
  129. // kvPairs := []string{
  130. // fmt.Sprintf("%s=%s", filepath.Join(nativeBasePath, "lib", "apmAgent.jar"), nativeBasePath),
  131. // "UNINSTALL",
  132. // }
  133. // argkv := strings.Join(kvPairs, ",")
  134. // args := []string{"load", "instrument", "false", argkv}
  135. // jattacher := jattach.JvmJattacher{
  136. // Pid: p.Pid,
  137. // Args: args,
  138. // PrintOutput: 1,
  139. // }
  140. // _, err := jattacher.JDetach()
  141. // if err == nil {
  142. // //p.stackUprobesChecked = false
  143. // }
  144. //}
  145. p.uprobes = []link.Link{}
  146. }
  147. func (p *Process) uninstallJavaAgent() error {
  148. if p.codeType.IsJvmCode() && p.stackAttachOnce {
  149. nativeBasePath := utils.GetDefaultAgentsPath("NativeAgent")
  150. kvPairs := []string{
  151. fmt.Sprintf("%s=%s", filepath.Join(nativeBasePath, "lib", "apmAgent.jar"), nativeBasePath),
  152. "UNINSTALL",
  153. }
  154. argkv := strings.Join(kvPairs, ",")
  155. args := []string{"load", "instrument", "false", argkv}
  156. jattacher := jattach.JvmJattacher{
  157. Pid: p.Pid,
  158. Args: args,
  159. PrintOutput: 1,
  160. }
  161. _, err := jattacher.JDetach()
  162. if err != nil {
  163. return err
  164. }
  165. p.stackStatus.JattachClose()
  166. return nil
  167. }
  168. return nil
  169. }
  170. func (p *Process) GetCmdline() string {
  171. return p.cmdline
  172. }