jvm.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package ebpftracer
  2. import (
  3. "debug/elf"
  4. "errors"
  5. "fmt"
  6. "github.com/cilium/ebpf/link"
  7. "github.com/coroot/coroot-node-agent/ebpftracer/tracer/inject"
  8. "github.com/coroot/coroot-node-agent/proc"
  9. "github.com/coroot/coroot-node-agent/utils"
  10. . "github.com/coroot/coroot-node-agent/utils/modelse"
  11. klog "github.com/sirupsen/logrus"
  12. "golang.org/x/arch/arm64/arm64asm"
  13. "golang.org/x/arch/x86/x86asm"
  14. "runtime"
  15. )
  16. const (
  17. // goServeHTTP = "net/http.serverHandler.ServeHTTP"
  18. // binPath = "/root/code/jdk8u/build/linux-x86_64-normal-server-release/jdk/lib/amd64/libnio.so"
  19. symbolsocketRead0 = "Java_sun_nio_ch_FileDispatcherImpl_read0"
  20. symbolsocketWrite0 = "Java_java_net_SocketOutputStream_socketWrite0"
  21. )
  22. func (t *Tracer) AttachJavaNioReadUprobes(pid uint32, codeType CodeType, rootfs string) ([]link.Link, error) {
  23. if t.DisableL7Tracing() {
  24. return nil, nil
  25. }
  26. var links []link.Link
  27. var bpath string
  28. // JavaAOT 逻辑
  29. if codeType.IsJavaAotCode() {
  30. bpath = proc.Path(pid, "exe")
  31. } else {
  32. //version := UsePIDToGetJDKVersion(pid)
  33. //klog.Infof("[attach] java version is %s", version)
  34. var err error
  35. bpath, err = utils.GetSoPath(pid, "libnio.so", rootfs)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if bpath == "" {
  40. return nil, errors.New("can not find nio.so")
  41. }
  42. }
  43. //bpath = rootfs + bpath
  44. klog.Infof("[attach] find the nio.so path is %s", bpath)
  45. ex, err := link.OpenExecutable(bpath)
  46. if err != nil {
  47. klog.Errorf("[attach] open executable: %v", err)
  48. return nil, err
  49. }
  50. ef, err := elf.Open(bpath)
  51. if err != nil {
  52. klog.Errorf("[attach] open elf: %v", err)
  53. return nil, err
  54. }
  55. defer ef.Close()
  56. symbols, err := ef.DynamicSymbols()
  57. if err != nil {
  58. if errors.Is(err, elf.ErrNoSymbols) {
  59. return nil, err
  60. }
  61. return nil, err
  62. }
  63. textSection := ef.Section(".text")
  64. if textSection == nil {
  65. return nil, errors.New("can not find .text section")
  66. }
  67. textSectionData, err := textSection.Data()
  68. if err != nil {
  69. return nil, err
  70. }
  71. textSectionLen := uint64(len(textSectionData) - 1)
  72. // opt := link.UprobeOptions{
  73. // Offset: 61,
  74. // }
  75. // upread02, err := ex.Uprobe(symbolsocketRead0, t.uprobes["uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0"], &opt)
  76. // if err != nil {
  77. // return nil
  78. // }
  79. // links = append(links, upread01)
  80. for _, s := range symbols {
  81. if elf.ST_TYPE(s.Info) != elf.STT_FUNC || s.Size == 0 {
  82. continue
  83. }
  84. switch s.Name {
  85. case symbolsocketRead0:
  86. default:
  87. continue
  88. }
  89. address := s.Value
  90. for _, p := range ef.Progs {
  91. if p.Type != elf.PT_LOAD || (p.Flags&elf.PF_X) == 0 {
  92. continue
  93. }
  94. if p.Vaddr <= s.Value && s.Value < (p.Vaddr+p.Memsz) {
  95. address = s.Value - p.Vaddr + p.Off
  96. break
  97. }
  98. }
  99. klog.Infof("[attach] java symbol name is %s", s.Name)
  100. switch s.Name {
  101. case symbolsocketRead0:
  102. sStart := s.Value - textSection.Addr
  103. sEnd := sStart + s.Size
  104. if sEnd > textSectionLen {
  105. continue
  106. }
  107. sBytes := textSectionData[sStart:sEnd]
  108. returnOffsets := getCallNextMoveOffsets(ef.Machine, sBytes)
  109. if len(returnOffsets) == 0 {
  110. return nil, fmt.Errorf("failed to attach uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0 uprobe")
  111. }
  112. for _, offset := range returnOffsets {
  113. 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)})
  114. if err != nil {
  115. return nil, fmt.Errorf("failed to attach uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0 uprobe")
  116. }
  117. klog.Infof("[attach] java symbol offset is %d", offset)
  118. links = append(links, l)
  119. }
  120. }
  121. }
  122. if len(links) == 0 {
  123. return nil, fmt.Errorf("no links found for %s", bpath)
  124. }
  125. klog.WithField("pid", pid).Infof("[attach] libnio attached!")
  126. return links, nil
  127. }
  128. func (t *Tracer) AttachJavaNetWriteUprobes(pid uint32, rootfs string) ([]link.Link, error) {
  129. if t.DisableL7Tracing() {
  130. return nil, nil
  131. }
  132. // 关闭横向串联
  133. if t.DisableE2ETracing() {
  134. return nil, nil
  135. }
  136. var err error
  137. libnetso := "libnet.so"
  138. elfFileName := "cwlibnet.so"
  139. //inject
  140. originFunc := symbolsocketWrite0
  141. uProbeData := inject.UprobeData{
  142. Offset: 53,
  143. Func: originFunc,
  144. //ELFPath: physicalPath,
  145. }
  146. if runtime.GOARCH == "arm64" {
  147. uProbeData = inject.UprobeData{
  148. Offset: 8,
  149. Func: "CW_" + originFunc,
  150. //ELFPath: physicalPath,
  151. }
  152. }
  153. klog.Infof("[jvm] ProbeData %s", utils.ToString(uProbeData))
  154. jvmInjector := &inject.JvmInjector{
  155. Pid: int(pid),
  156. ReleaseLibNetInfo: inject.LibNetInfo{
  157. LibName: libnetso,
  158. //LibPath: mapsPath,
  159. //FileDeleted: baseDeleted,
  160. FuncSymbol: inject.InstInfo{
  161. SymName: originFunc,
  162. },
  163. },
  164. DebugLibNetInfo: inject.LibNetInfo{
  165. // TODO 根据版本设置
  166. LibName: elfFileName,
  167. //LibPath: uProbeData.ELFPath,
  168. //FileDeleted: deleted,
  169. FuncSymbol: inject.InstInfo{
  170. SymName: uProbeData.Func,
  171. },
  172. //ProcLoadPath: procMapsLoadPath,
  173. },
  174. RecodeInfo: inject.LibNetInfo{FuncSymbol: inject.InstInfo{SymName: "CW_RECODE_" + originFunc}},
  175. Uprobe: uProbeData,
  176. Rootfs: rootfs,
  177. }
  178. err = inject.JvmInject(jvmInjector)
  179. klog.Infof("[jvm] ReleaseLibNetInfo :[%s]", utils.ToString(jvmInjector.ReleaseLibNetInfo))
  180. klog.Infof("[jvm] DebugLibNetInfo :[%s]", utils.ToString(jvmInjector.DebugLibNetInfo))
  181. if err != nil {
  182. klog.WithError(err).Errorf("[jvm] inject.JvmInject error.")
  183. return nil, err
  184. }
  185. var links []link.Link
  186. ex, err := link.OpenExecutable(jvmInjector.Uprobe.ELFPath)
  187. if err != nil {
  188. return nil, err
  189. }
  190. opt := link.UprobeOptions{
  191. Offset: uint64(jvmInjector.Uprobe.Offset),
  192. PID: int(pid),
  193. }
  194. upread02, err := ex.Uprobe(jvmInjector.Uprobe.Func, t.uprobes["uprobe_Java_java_net_SocketOutputStream_socketWrite0"], &opt)
  195. if err != nil {
  196. return nil, err
  197. }
  198. links = append(links, upread02)
  199. if len(links) == 0 {
  200. return nil, errors.New("can not find uprobe_Java_net_SocketOutputStream_socketWrite0")
  201. }
  202. klog.WithField("pid", pid).Infoln("[jvm] libnet has been successfully injected.")
  203. return links, nil
  204. }
  205. func getCallNextMoveOffsets(machine elf.Machine, instructions []byte) []int {
  206. var res []int
  207. firstCall := 0
  208. switch machine {
  209. case elf.EM_X86_64:
  210. for i := 0; i < len(instructions); {
  211. ins, err := x86asm.Decode(instructions[i:], 64)
  212. if err == nil && ins.Op == x86asm.CALL {
  213. if firstCall == 0 {
  214. firstCall = 1
  215. } else {
  216. i += ins.Len
  217. res = append(res, i)
  218. return res
  219. }
  220. }
  221. i += ins.Len
  222. }
  223. case elf.EM_AARCH64:
  224. for i := 0; i < len(instructions); {
  225. ins, err := arm64asm.Decode(instructions[i:])
  226. if err == nil && ins.Op == arm64asm.BL {
  227. if firstCall == 0 {
  228. firstCall = 1
  229. } else {
  230. i += 4
  231. res = append(res, i)
  232. return res
  233. }
  234. }
  235. i += 4
  236. }
  237. }
  238. return res
  239. }