process.go 4.2 KB

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