jvm.go 4.6 KB

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