Browse Source

Fixed #TASK_QT-9810 [arm适配]java-client arm适配

Carl 1 year ago
parent
commit
7c8c01c68a

+ 4 - 0
ebpftracer/ebpf/ebpf.c

@@ -58,6 +58,10 @@
 #include "utrace/java/net/server.probe.bpf.c"
 #include "utrace/java/net/client.probe.bpf.c"
 
+// 支持arm64后打开
+#if defined(__x86_64__)
 #include "utrace/netcore/net/server.probe.bpf.c"
 #include "utrace/netcore/net/client.probe.bpf.c"
+#endif
+
 char _license[] SEC("license") = "GPL";

+ 18 - 0
ebpftracer/ebpf/include/common.h

@@ -59,4 +59,22 @@ struct protocol_message_t {
 //	TASK_COMM_LEN = 16,
 //};
 
+enum code_type {
+	CodeTypeUnknown    = -1,
+	CodeTypeWaitCheck  = 0,
+	CodeTypePHP        = 1001,
+	CodeTypeJava       = 1002,
+	CodeTypeJavaAot    = 1102,
+	CodeTypePython     = 1003,
+	CodeTypeDotNet     = 1004,
+	CodeTypeNode       = 1005,
+	CodeTypeGo         = 1006,
+	CodeTypeC          = 1007,
+	CodeTypeNetCore    = 1008,
+	CodeTypeNetCoreAot = 1108,
+	CodeTypeLua        = 1009,
+	CodeTypeJavaC      = 1010,
+	CodeTypeRuby       = 1012
+};
+
 #endif /* DF_BPF_COMMON_H */

+ 11 - 0
ebpftracer/ebpf/utrace/java/include/java_common.h

@@ -8,8 +8,19 @@
 
 #if defined(__x86_64__)
 #define PT_LEN_REGS(x) (PT_REGS_RBP(x) - 0x10058);
+#define PT_HTTP_RESP_REGS(x) PT_REGS_PARM2(x)
 #elif defined(__aarch64__)
 #define PT_LEN_REGS(x) PT_REGS_SP(x);
+#define PT_HTTP_RESP_REGS(ctx) ({                \
+	unsigned long res = 0;                       \
+    long err = bpf_probe_read(&res, sizeof(res), \
+            (void *) (PT_REGS_SP(ctx) - 48));    \
+    if (err < 0) {                               \
+        bpf_printk("bpf_probe_read failed with return code: %ld\n", err); \
+        res = 0;                                 \
+    }                                            \
+    res;                                         \
+})
 #endif
 
 

+ 2 - 2
ebpftracer/ebpf/utrace/java/net/client.probe.bpf.c

@@ -77,9 +77,9 @@ static __inline struct apm_span_context* build_sc(struct ebpf_proc_info *proc_in
 	struct apm_span_context *cw_psc = cw_get_parent_tracking_span();
 	if(cw_psc){
 		copy_byte_arrays(cw_psc->trace_id, cw_sc->trace_id, APM_TRACE_ID_SIZE);
-		// new spanid
-		generate_random_bytes(cw_sc->span_id, APM_SPAN_ID_SIZE);
 	}
+	// new spanid
+	generate_random_bytes(cw_sc->span_id, APM_SPAN_ID_SIZE);
 
 	// set host_id/appid
 	u32 k0 = 0;

+ 1 - 1
ebpftracer/ebpf/utrace/java/net/server.probe.bpf.c

@@ -157,7 +157,7 @@ int uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0(struct pt_regs *ctx) {
 //	cw_bpf_debug("enter the ret uprobe_Java_sun_nio_ch_FileDispatcherImpl_read0\n");
 	unsigned long jhttpdata_ptr;
 	// jhttpdata_ptr = (ctx)->rsi;
-	jhttpdata_ptr = PT_REGS_PARM2(ctx);
+	jhttpdata_ptr = PT_HTTP_RESP_REGS(ctx);
 //	cw_bpf_debug("enter rsi is %llx.\n", jhttpdata_ptr);
 	
 	//x/s addr

+ 9 - 6
ebpftracer/jvm.go

@@ -58,8 +58,9 @@ func (t *Tracer) AttachJavaNioReadUprobes(pid uint32, insID utils.ID, codeType c
 	}
 	defer ef.Close()
 
-	symbols, err := ef.Symbols()
+	symbols, err := ef.DynamicSymbols()
 	if err != nil {
+		fmt.Println("can not get symbols")
 		if errors.Is(err, elf.ErrNoSymbols) {
 			return nil
 		}
@@ -68,10 +69,12 @@ func (t *Tracer) AttachJavaNioReadUprobes(pid uint32, insID utils.ID, codeType c
 
 	textSection := ef.Section(".text")
 	if textSection == nil {
+		fmt.Println("can not find .text section")
 		return nil
 	}
 	textSectionData, err := textSection.Data()
 	if err != nil {
+		fmt.Println("can not get .text section data", err)
 		return nil
 	}
 	textSectionLen := uint64(len(textSectionData) - 1)
@@ -115,6 +118,7 @@ func (t *Tracer) AttachJavaNioReadUprobes(pid uint32, insID utils.ID, codeType c
 			sBytes := textSectionData[sStart:sEnd]
 			returnOffsets := getCallNextMoveOffsets(ef.Machine, sBytes)
 			if len(returnOffsets) == 0 {
+				fmt.Println("failed to attach uprobe_ret_Java_sun_nio_ch_FileDispatcherImpl_read0 uprobe")
 				return nil
 			}
 			for _, offset := range returnOffsets {
@@ -131,7 +135,7 @@ func (t *Tracer) AttachJavaNioReadUprobes(pid uint32, insID utils.ID, codeType c
 	if len(links) == 0 {
 		return nil
 	}
-	fmt.Println("jvm uprobes attached, pid is ", pid)
+	fmt.Println("[jvm] libnio attached, pid is ", pid)
 	return links
 }
 
@@ -145,7 +149,7 @@ func (t *Tracer) AttachJavaNetWriteUprobes(pid uint32, insID utils.ID) []link.Li
 	if runtime.GOARCH == "arm64" {
 		libnetSo = "/root/cwlibnet.so"
 		sys = "CW_Java_java_net_SocketOutputStream_socketWrite0"
-		offset = 0
+		offset = 8
 	}
 	var links []link.Link
 	ex, err := link.OpenExecutable(libnetSo)
@@ -165,8 +169,7 @@ func (t *Tracer) AttachJavaNetWriteUprobes(pid uint32, insID utils.ID) []link.Li
 	if len(links) == 0 {
 		return nil
 	}
-	fmt.Println("jvm client uprobes attached", pid)
-
+	fmt.Println("[jvm] libnet attached", pid)
 	return links
 }
 
@@ -213,7 +216,7 @@ func getSoPath(pid uint32, soname string) string {
 	// 读取maps文件内容
 	mapsData, err := ioutil.ReadFile(mapsPath)
 	if err != nil {
-		fmt.Println("无法读取maps文件")
+		fmt.Println("lookup so error.")
 		return ""
 	}
 

+ 6 - 6
ebpftracer/tracer.go

@@ -704,15 +704,15 @@ func ipPort(ip [16]byte, port uint16) netaddr.IPPort {
 }
 
 func (t *Tracer) InitKProcInfo(pid uint32, insID utils.ID, codeType uint16) error {
-
+	var err error
 	info := tracer.EbpfProcInfo{}
 	info.InstanceId = insID.HashtVal
 	// 获取内存地址
-	allocDetails, err := tracer.Allocate(int(pid))
-	if err == nil && allocDetails != nil {
-		info.StartAddr = allocDetails.StartAddr
-		info.EndAddr = allocDetails.EndAddr
-	}
+	//allocDetails, err := tracer.Allocate(int(pid))
+	//if err == nil && allocDetails != nil {
+	//	info.StartAddr = allocDetails.StartAddr
+	//	info.EndAddr = allocDetails.EndAddr
+	//}
 
 	info.CodeType = codeType
 	//klog.Infoln("Major:", major)