stack.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package ebpftracer
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github.com/cilium/ebpf"
  6. "github.com/cilium/ebpf/link"
  7. "github.com/coroot/coroot-node-agent/ebpftracer/tracer"
  8. "github.com/coroot/coroot-node-agent/ebpftracer/tracer/jattach"
  9. "github.com/coroot/coroot-node-agent/utils"
  10. . "github.com/coroot/coroot-node-agent/utils/modelse"
  11. klog "github.com/sirupsen/logrus"
  12. "os"
  13. "path/filepath"
  14. "strings"
  15. )
  16. // AttachStackUprobes
  17. // default process stack
  18. func (t *Tracer) AttachStackUprobes(path string, uprobes []tracer.Uprobe) []link.Link {
  19. var links []link.Link
  20. ex, err := link.OpenExecutable(path)
  21. if err != nil {
  22. return nil
  23. }
  24. klog.Infoln("[stack] Attach Start", path)
  25. for i, up := range uprobes {
  26. klog.Debugf("[stack] attaching %d -> %d -> %s -> 0x%x -> 0x%x -> 0x%x", i, len(uprobes), up.Funcname, up.AbsOffset, up.Address, up.AbsOffset+up.Address)
  27. var prog *ebpf.Program
  28. switch up.Location {
  29. case tracer.AtEntry:
  30. prog = t.uprobes["ent"]
  31. case tracer.AtRet:
  32. prog = t.uprobes["ret"]
  33. case tracer.AtDotNetEntry:
  34. prog = t.uprobes["dotnetent"]
  35. }
  36. uplink, err := ex.Uprobe(up.Funcname, prog, &link.UprobeOptions{Address: up.Address, Offset: up.AbsOffset})
  37. if err != nil {
  38. klog.Errorf("[stack] attachingERROR:%v, %v, %v", err, up, uplink)
  39. // return nil
  40. } else {
  41. links = append(links, uplink)
  42. }
  43. }
  44. return links
  45. }
  46. // JVM process stack
  47. func (t *Tracer) JattachJvm(pid uint32, appInfo AppInfo, whiteList, blackList string) error {
  48. klog.Infoln("[Jvm stack uprobe] Attach Start AttachJVMStackUprobes", pid)
  49. // TODO tiny Agent 注入
  50. // TODO copy agent到 jre conf/lib/plugins
  51. //argkv := "/opt/github/euspace/dist/package_dir/agents/NativeAgent/lib/apmAgent.jar=/opt/github/euspace/dist/package_dir/agents/NativeAgent"
  52. if whiteList == "" && blackList == "" {
  53. return fmt.Errorf("no stack rule")
  54. }
  55. whiteList = strings.ReplaceAll(whiteList, ",", "")
  56. blackList = strings.ReplaceAll(blackList, ",", "")
  57. var stackRule string
  58. if whiteList == "" {
  59. stackRule = fmt.Sprintf("blackStack=%s", blackList) // 仅保留黑名单
  60. } else if blackList == "" {
  61. stackRule = fmt.Sprintf("whiteStack=%s", whiteList) // 仅保留白名单
  62. } else {
  63. stackRule = fmt.Sprintf("whiteStack=%s,blackStack=%s", whiteList, blackList)
  64. }
  65. nativeBasePath := utils.GetDefaultAgentsPath("NativeAgent")
  66. kvPairs := []string{
  67. fmt.Sprintf("%s=%s", filepath.Join(nativeBasePath, "lib", "apmAgent.jar"), nativeBasePath),
  68. fmt.Sprintf("%s=%d", "appId", appInfo.AppIdHash.IntVal),
  69. fmt.Sprintf("%s=%d", "agentId", appInfo.AgentId),
  70. fmt.Sprintf("%s=%d", "hostId", utils.GetHostID()),
  71. fmt.Sprintf("%s=%d", "accountId", utils.GetAccountID()),
  72. stackRule,
  73. }
  74. argkv := strings.Join(kvPairs, ",")
  75. klog.Infof("[Jvm stack uprobe] params:[%s]", argkv)
  76. args := []string{"load", "instrument", "false", argkv}
  77. jattacher := jattach.JvmJattacher{
  78. Pid: pid,
  79. Args: args,
  80. PrintOutput: 1,
  81. }
  82. res, err := jattacher.JAttach()
  83. klog.Infof("[Jvm stack uprobe] JAttach Result: %d", res)
  84. if err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. func (t *Tracer) AttachJVMStackUprobes(pid uint32, appInfo AppInfo) ([]link.Link, error) {
  90. //path = utils.GetDefaultAgentsPath("NativeAgent", "libnativeAgent.so")
  91. //tmp/NativeAgentSo2297066477572820801.tmp
  92. path, err := FindNativeSoFromMapped(pid, "NativeAgentSo", ".tmp")
  93. if err != nil {
  94. klog.Error(err)
  95. return nil, err
  96. }
  97. setNodeEnter := "Java_com_cloudwise_agent_common_natives_TraceNative_setNodeEnter"
  98. setNodeReturn := "Java_com_cloudwise_agent_common_natives_TraceNative_setNodeReturn"
  99. klog.Infoln("[Jvm stack uprobe] Attach Start AttachJVMStackUprobes", path)
  100. var links []link.Link
  101. ex, err := link.OpenExecutable(path)
  102. if err != nil {
  103. klog.Error(err)
  104. return nil, err
  105. }
  106. klog.Infof("[Jvm stack uprobe] Attach Start [%s] [%s] ", setNodeEnter, setNodeReturn)
  107. uplink, err := ex.Uprobe(setNodeEnter, t.uprobes["setNodeEnter"], &link.UprobeOptions{Offset: 0x0, PID: int(pid)})
  108. if err != nil {
  109. klog.Errorf("[Jvm stack uprobe] Attaching ERROR: %v, %v, %v\n", err, setNodeEnter, uplink)
  110. return nil, err
  111. } else {
  112. links = append(links, uplink)
  113. }
  114. klog.Infoln("Attach Start " + setNodeReturn)
  115. uplink, err = ex.Uprobe(setNodeReturn, t.uprobes["setNodeReturn"], &link.UprobeOptions{Offset: 0x0})
  116. if err != nil {
  117. klog.Errorf("[Jvm stack uprobe] Attaching ERROR: %v, %v, %v\n", err, setNodeReturn, uplink)
  118. return nil, err
  119. } else {
  120. links = append(links, uplink)
  121. }
  122. return links, nil
  123. }
  124. func FindNativeSoFromMapped(pid uint32, prefix, suffix string) (string, error) {
  125. // todo rootfs
  126. mapsFile := fmt.Sprintf("/proc/%d/maps", pid)
  127. tmpFile, err := os.Open(mapsFile)
  128. if err != nil {
  129. return "", fmt.Errorf("error opening maps file: %v", err)
  130. }
  131. defer tmpFile.Close()
  132. // 使用 bufio.Scanner 逐行读取文件内容
  133. scanner := bufio.NewScanner(tmpFile)
  134. for scanner.Scan() {
  135. line := scanner.Text()
  136. // 检查路径部分是否包含 "NativeAgentSo" 且以 ".tmp" 结尾
  137. if strings.Contains(line, prefix) && strings.HasSuffix(line, suffix) {
  138. parts := strings.Fields(line)
  139. if len(parts) > 5 {
  140. return parts[len(parts)-1], nil // 返回路径
  141. }
  142. }
  143. }
  144. if err = scanner.Err(); err != nil {
  145. return "", fmt.Errorf("error reading maps file: %v", err)
  146. }
  147. return "", fmt.Errorf("no matching path found")
  148. }