Pārlūkot izejas kodu

Feature #TASK_QT-31498 华为云kernel-5.4兼容性

Carl 5 mēneši atpakaļ
vecāks
revīzija
8fd6d614b7

+ 4 - 3
ebpftracer/ebpf/include/apm_trace.h

@@ -140,9 +140,10 @@ struct thread_ctx_t {
 };
 
 static __always_inline void cw_copy_byte_arrays(unsigned char *src, unsigned char *dst, __u32 size) {
-	for (int i = 0; i < size; i++) {
-		dst[i] = src[i];
-	}
+//	for (int i = 0; i < size; i++) {
+//		dst[i] = src[i];
+//	}
+	__builtin_memcpy(dst, src, size);
 }
 
 // 设置 trace 开始标记

+ 70 - 0
ebpftracer/ebpf/utrace/go/include/go_types.h

@@ -229,4 +229,74 @@ static __always_inline bool get_go_string_from_user_ptr(void *user_str_ptr, char
 
     return true;
 }
+
+static __always_inline void
+cw_append_item_to_slice(void *new_item, u32 item_size, void *slice_user_ptr) {
+	// read the slice descriptor
+	struct go_slice_ot slice = {0};
+	bpf_probe_read(&slice, sizeof(slice), slice_user_ptr);
+	long res = 0;
+
+	u64 slice_len = slice.len;
+	u64 slice_cap = slice.cap;
+	if (slice_len < slice_cap && slice.array != NULL) {
+		// Room available on current array, append to the underlying array
+		res = bpf_probe_write_user(slice.array + (item_size * slice_len), new_item, item_size);
+	} else {
+		// No room on current array - try to copy new one of size item_size * (len + 1)
+		u32 alloc_size = item_size * slice_len;
+		if (alloc_size >= MAX_SLICE_ARRAY_SIZE) {
+			return;
+		}
+
+		// Get temporary buffer
+		u32 index = 0;
+		struct slice_array_buff *map_buff = bpf_map_lookup_elem(&slice_array_buff_map, &index);
+		if (!map_buff) {
+			return;
+		}
+
+		unsigned char *new_slice_array = map_buff->buff;
+		// help the verifier
+		alloc_size &= (MAX_SLICE_ARRAY_SIZE - 1);
+		if (alloc_size + item_size > MAX_SLICE_ARRAY_SIZE) {
+			// No room for new item
+			return;
+		}
+		// Append to buffer
+		if (slice.array != NULL) {
+			bpf_probe_read_user(new_slice_array, alloc_size, slice.array);
+		}
+		copy_byte_arrays(new_item, new_slice_array + alloc_size, item_size);
+
+		// Copy buffer to userspace
+		u32 new_array_size = alloc_size + item_size;
+
+		__u32 tgid = (__u32) (bpf_get_current_pid_tgid() >> 32);
+		struct ebpf_proc_info *info = bpf_map_lookup_elem(&proc_info_map, &tgid);
+		if (!info) {
+			return;
+		}
+		void *new_array = cw_write_target_data(new_slice_array, new_array_size, info);
+		if (new_array == NULL) {
+			cw_bpf_debug("append_item_to_slice: failed to copy new array to userspace");
+			return;
+		}
+
+		// Update array pointer of slice
+		slice.array = new_array;
+		slice.cap++;
+
+		// cw_bpf_debug("enter the cw_append_item_to_slice222222\n");
+	}
+
+	// Update len
+	slice.len++;
+	long success = bpf_probe_write_user(slice_user_ptr, &slice, sizeof(slice));
+	if (success != 0) {
+		cw_bpf_debug("append_item_to_slice: failed to update slice in userspace");
+		return;
+	}
+}
+
 #endif

+ 6 - 5
ebpftracer/ebpf/utrace/go/include/utils.h

@@ -70,11 +70,12 @@ static __always_inline void hex_string_to_bytes(char *str, u32 size, unsigned ch
 
 static __always_inline void copy_byte_arrays(unsigned char *src, unsigned char *dst, u32 size)
 {
-#pragma clang loop unroll(full)
-    for (int i = 0; i < size; i++)
-    {
-        dst[i] = src[i];
-    }
+	cw_copy_byte_arrays(src,dst,size);
+//#pragma clang loop unroll(full)
+//    for (int i = 0; i < size; i++)
+//    {
+//        dst[i] = src[i];
+//    }
 }
 
 static __always_inline void custom_hash(const char *str, unsigned char *hash,u32 size) {

+ 8 - 6
ebpftracer/ebpf/utrace/go/mq/kafka/producer.probe.bpf.c

@@ -22,7 +22,12 @@
 // limitation on map entry size: https://github.com/iovisor/bcc/issues/2519#issuecomment-534359316
 // the default value is 100, but it can be changed by the user
 // we must specify a limit for the verifier
+#if __KERNEL_FROM >= 512
 #define MAX_BATCH_SIZE 10
+#else
+#define MAX_BATCH_SIZE 1
+#endif
+
 // https://github.com/apache/kafka/blob/0.10.2/core/src/main/scala/kafka/common/Topic.scala#L30C3-L30C34
 #define MAX_TOPIC_SIZE 256
 // No constraint on the key size, but we must have a limit for the verifier
@@ -134,7 +139,6 @@ struct tcp_addr {
 	// 忽略 Zone
 };
 
-#ifndef NO_HEADER_PROPAGATION
 
 static __always_inline int build_contxet_header(struct kafka_header_t *header, struct apm_span_context *span_ctx) {
 	if (header == NULL || span_ctx == NULL) {
@@ -215,11 +219,11 @@ static __always_inline int build_contxet_header(struct kafka_header_t *header, s
 }
 
 static __always_inline int inject_kafka_header(void *message, struct kafka_header_t *header, u64 msg_headers_pos) {
-	append_item_to_slice(header, sizeof(*header), (void *) (message + msg_headers_pos));
+//	append_item_to_slice(header, sizeof(*header), (void *) (message + msg_headers_pos));
+	cw_append_item_to_slice(header, sizeof(*header), (void *) (message + msg_headers_pos));
 	return 0;
 }
 
-#endif
 
 static __always_inline long collect_kafka_attributes(void *message, struct message_attributes_t *attrs, bool collect_topic, u64 msg_key_pos, u64 msg_topic_pos) {
 	if (collect_topic) {
@@ -417,7 +421,6 @@ int uprobe_WriteMessages(struct pt_regs *ctx) {
 			__builtin_memcpy(kafka_request->msgs[i].sc.assumed_app_id, kafka_request->msgs[0].sc.assumed_app_id, APM_APP_ID_SIZE);
 		}
 
-#ifndef NO_HEADER_PROPAGATION
 		// Build the header
 		if (build_contxet_header(header, &kafka_request->msgs[i].sc) != 0) {
 			cw_bpf_debug("uprobe/WriteMessages: Failed to build header");
@@ -427,7 +430,6 @@ int uprobe_WriteMessages(struct pt_regs *ctx) {
 		inject_kafka_header(msg_ptr, header, proc_info->kafka_message_headers_pos);
 		// Zero the header for next iteration to avoid stale data
 		__builtin_memset(header, 0, sizeof(*header));
-#endif
 		kafka_request->valid_messages++;
 		msg_ptr = msg_ptr + msg_size;
 	}
@@ -547,4 +549,4 @@ int uprobe_WriteMessages_Returns(struct pt_regs *ctx) {
 	bpf_map_delete_elem(&kafka_events, &key);
 	// don't need to stop tracking the span, as we don't have a context to propagate locally
 	return 0;
-}
+}

+ 0 - 75
ebpftracer/ebpf/utrace/go/net/grpc.client.probe.bpf.c

@@ -311,81 +311,6 @@ done:
 }
 
 
-static __always_inline void
-cw_append_item_to_slice(void *new_item, u32 item_size, void *slice_user_ptr) {
-    // read the slice descriptor
-    struct go_slice_ot slice = {0};
-    long res = bpf_probe_read_user(&slice, sizeof(slice), slice_user_ptr);
-    if (res != 0) {
-        cw_bpf_debug("cw_append_item_to_slice: failed to read slice descriptor, res=%ld\n", res);
-        return;
-    }
-
-    // cw_bpf_debug("cw_append_item_to_slice len is %d\n", slice.len);
-    // cw_bpf_debug("cw_append_item_to_slice cap is %d\n", slice.cap);
-    // cw_bpf_debug("cw_append_item_to_slice array is %p\n", slice.array);
-
-    u64 slice_len = slice.len;
-    u64 slice_cap = slice.cap;
-    if (slice_len < slice_cap && slice.array != NULL) {
-        // Room available on current array, append to the underlying array
-        // cw_bpf_debug("enter the cw_append_item_to_slice11111\n");
-        res = bpf_probe_write_user(slice.array + (item_size * slice_len), new_item, item_size);
-    } else {
-        // No room on current array - try to copy new one of size item_size * (len + 1)
-        u32 alloc_size = item_size * slice_len;
-        if (alloc_size >= MAX_SLICE_ARRAY_SIZE) {
-            return;
-        }
-
-        // Get temporary buffer
-        u32 index = 0;
-        struct slice_array_buff *map_buff = bpf_map_lookup_elem(&slice_array_buff_map, &index);
-        if (!map_buff) {
-            return;
-        }
-
-        unsigned char *new_slice_array = map_buff->buff;
-        // help the verifier
-        alloc_size &= (MAX_SLICE_ARRAY_SIZE - 1);
-        if (alloc_size + item_size > MAX_SLICE_ARRAY_SIZE) {
-            // No room for new item
-            return;
-        }
-        // Append to buffer
-        if (slice.array != NULL) {
-            bpf_probe_read_user(new_slice_array, alloc_size, slice.array);
-            // cw_bpf_debug("append_item_to_slice: copying %d bytes to new array from address 0x%llx",
-                    //    alloc_size,
-                    //    slice.array);
-        }
-        copy_byte_arrays(new_item, new_slice_array + alloc_size, item_size);
-
-        // Copy buffer to userspace
-        u32 new_array_size = alloc_size + item_size;
-
-        void *new_array = write_target_data(new_slice_array, new_array_size);
-        if (new_array == NULL) {
-            cw_bpf_debug("append_item_to_slice: failed to copy new array to userspace");
-            return;
-        }
-
-        // Update array pointer of slice
-        slice.array = new_array;
-        slice.cap++;
-
-        // cw_bpf_debug("enter the cw_append_item_to_slice222222\n");
-    }
-
-    // Update len
-    slice.len++;
-    // cw_bpf_debug("after cw_append_item_to_slice len is %d\n", slice.len);
-    long success = bpf_probe_write_user(slice_user_ptr, &slice, sizeof(slice));
-    if (success != 0) {
-        cw_bpf_debug("append_item_to_slice: failed to update slice in userspace");
-        return;
-    }
-}
 
 SEC("uprobe/loopyWriter_headerHandler")
 int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx) {