jvm.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package ebpftracer
  2. import (
  3. "errors"
  4. "github.com/coroot/coroot-node-agent/common"
  5. "io/ioutil"
  6. "runtime"
  7. "strings"
  8. "debug/elf"
  9. "fmt"
  10. "path/filepath"
  11. "github.com/coroot/coroot-node-agent/proc"
  12. "github.com/coroot/coroot-node-agent/utils"
  13. "github.com/cilium/ebpf/link"
  14. "golang.org/x/arch/arm64/arm64asm"
  15. "golang.org/x/arch/x86/x86asm"
  16. )
  17. const (
  18. // goServeHTTP = "net/http.serverHandler.ServeHTTP"
  19. // binPath = "/root/code/jdk8u/build/linux-x86_64-normal-server-release/jdk/lib/amd64/libnio.so"
  20. symbolsocketRead0 = "Java_sun_nio_ch_FileDispatcherImpl_read0"
  21. )
  22. func (t *Tracer) AttachJavaNioReadUprobes(pid uint32, insID utils.ID, codeType common.CodeType) []link.Link {
  23. if t.disableL7Tracing {
  24. return nil
  25. }
  26. var links []link.Link
  27. var bpath = ""
  28. // JavaAOT 逻辑
  29. if codeType.IsJavaAotCode() {
  30. bpath = proc.Path(uint32(pid), "exe")
  31. } else {
  32. version := UsePIDToGetJDKVersion(pid)
  33. fmt.Println("java version is ", version)
  34. bpath = getSoPath(pid, "libnio.so")
  35. if bpath == "" {
  36. fmt.Println("can,t find the nio.so")
  37. return nil
  38. }
  39. }
  40. fmt.Println("find the nio.so path is ", bpath)
  41. ex, err := link.OpenExecutable(bpath)
  42. if err != nil {
  43. return nil
  44. }
  45. ef, err := elf.Open(bpath)
  46. if err != nil {
  47. return nil
  48. //PID: int(pid),
  49. }
  50. defer ef.Close()
  51. symbols, err := ef.Symbols()
  52. if err != nil {
  53. if errors.Is(err, elf.ErrNoSymbols) {
  54. return nil
  55. }
  56. return nil
  57. }
  58. textSection := ef.Section(".text")
  59. if textSection == nil {
  60. return nil
  61. }
  62. textSectionData, err := textSection.Data()
  63. if err != nil {
  64. return nil
  65. }
  66. textSectionLen := uint64(len(textSectionData) - 1)
  67. // opt := link.UprobeOptions{
  68. // Offset: 61,
  69. // }
  70. // upread02, err := ex.Uprobe(symbolsocketRead0, t.uprobes["uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0"], &opt)
  71. // if err != nil {
  72. // return nil
  73. // }
  74. // links = append(links, upread01)
  75. for _, s := range symbols {
  76. if elf.ST_TYPE(s.Info) != elf.STT_FUNC || s.Size == 0 {
  77. continue
  78. }
  79. switch s.Name {
  80. case symbolsocketRead0:
  81. default:
  82. continue
  83. }
  84. address := s.Value
  85. for _, p := range ef.Progs {
  86. if p.Type != elf.PT_LOAD || (p.Flags&elf.PF_X) == 0 {
  87. continue
  88. }
  89. if p.Vaddr <= s.Value && s.Value < (p.Vaddr+p.Memsz) {
  90. address = s.Value - p.Vaddr + p.Off
  91. break
  92. }
  93. }
  94. fmt.Println("s.Name-----:", s.Name)
  95. switch s.Name {
  96. case symbolsocketRead0:
  97. sStart := s.Value - textSection.Addr
  98. sEnd := sStart + s.Size
  99. if sEnd > textSectionLen {
  100. continue
  101. }
  102. sBytes := textSectionData[sStart:sEnd]
  103. returnOffsets := getCallNextMoveOffsets(ef.Machine, sBytes)
  104. if len(returnOffsets) == 0 {
  105. return nil
  106. }
  107. for _, offset := range returnOffsets {
  108. l, err := ex.Uprobe(s.Name, t.uprobes["uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0"], &link.UprobeOptions{Address: address, Offset: uint64(offset), PID: int(pid)})
  109. if err != nil {
  110. fmt.Println("failed to attach uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0 uprobe")
  111. return nil
  112. }
  113. links = append(links, l)
  114. }
  115. }
  116. }
  117. if len(links) == 0 {
  118. return nil
  119. }
  120. fmt.Println("jvm uprobes attached, pid is ", pid)
  121. return links
  122. }
  123. func (t *Tracer) AttachJavaNetWriteUprobes(pid uint32, insID utils.ID) []link.Link {
  124. if t.disableL7Tracing {
  125. return nil
  126. }
  127. var libnetSo = "/data/roger/graalvm/lib/libnet.so"
  128. var sys = "Java_java_net_SocketOutputStream_socketWrite0"
  129. var offset = 53
  130. if runtime.GOARCH == "arm64" {
  131. libnetSo = "/root/cwlibnet.so"
  132. sys = "CW_Java_java_net_SocketOutputStream_socketWrite0"
  133. offset = 0
  134. }
  135. var links []link.Link
  136. ex, err := link.OpenExecutable(libnetSo)
  137. if err != nil {
  138. return nil
  139. }
  140. opt := link.UprobeOptions{
  141. Offset: uint64(offset),
  142. PID: int(pid),
  143. }
  144. upread02, err := ex.Uprobe(sys, t.uprobes["uprobe_Java_java_net_SocketOutputStream_socketWrite0"], &opt)
  145. if err != nil {
  146. return nil
  147. }
  148. links = append(links, upread02)
  149. if len(links) == 0 {
  150. return nil
  151. }
  152. fmt.Println("jvm client uprobes attached", pid)
  153. return links
  154. }
  155. func getCallNextMoveOffsets(machine elf.Machine, instructions []byte) []int {
  156. var res []int
  157. firstCall := 0
  158. switch machine {
  159. case elf.EM_X86_64:
  160. for i := 0; i < len(instructions); {
  161. ins, err := x86asm.Decode(instructions[i:], 64)
  162. if err == nil && ins.Op == x86asm.CALL {
  163. if firstCall == 0 {
  164. firstCall = 1
  165. } else {
  166. i += ins.Len
  167. res = append(res, i)
  168. return res
  169. }
  170. }
  171. i += ins.Len
  172. }
  173. case elf.EM_AARCH64:
  174. for i := 0; i < len(instructions); {
  175. ins, err := arm64asm.Decode(instructions[i:])
  176. if err == nil && ins.Op == arm64asm.BL {
  177. if firstCall == 0 {
  178. firstCall = 1
  179. } else {
  180. i += 4
  181. res = append(res, i)
  182. return res
  183. }
  184. }
  185. i += 4
  186. }
  187. }
  188. return res
  189. }
  190. func getSoPath(pid uint32, soname string) string {
  191. // 获取进程的maps文件路径
  192. mapsPath := fmt.Sprintf("/proc/%d/maps", pid)
  193. // 读取maps文件内容
  194. mapsData, err := ioutil.ReadFile(mapsPath)
  195. if err != nil {
  196. fmt.Println("无法读取maps文件")
  197. return ""
  198. }
  199. lines := strings.Split(string(mapsData), "\n")
  200. for _, line := range lines {
  201. fields := strings.Fields(line)
  202. if len(fields) >= 6 {
  203. perms := fields[1]
  204. path := fields[len(fields)-1]
  205. if strings.Contains(perms, "x") && filepath.Ext(path) == ".so" {
  206. if strings.Contains(path, soname) {
  207. return path
  208. }
  209. }
  210. }
  211. }
  212. return ""
  213. }