jvm.go 5.0 KB

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