浏览代码

Feature #TASK_QT-18250 提交代码,解决兼容问题,让AI协助排查定位问题。

rock 7 月之前
父节点
当前提交
df353c2b33

+ 1 - 1
ebpftracer/ebpf/utrace/go/include/go_net.h

@@ -24,7 +24,7 @@ const volatile u64 TCPAddr_Port_offset;
 
 static __always_inline long
 get_tcp_net_addr_from_tcp_addr(struct pt_regs *ctx, net_addr_t *addr, void *tcpAddr_ptr) {
-    go_slice_t ip;
+    go_slice_ot ip;
     long res = bpf_probe_read_user(&ip, sizeof(ip), (void *)(tcpAddr_ptr + TCPAddr_IP_offset));
     if (res != 0) {
         bpf_printk("failed to read ip slice %d", res);

+ 35 - 30
ebpftracer/ebpf/utrace/go/net/grpc.client.probe.bpf.c

@@ -9,11 +9,11 @@
 #include "uprobe.h"
 
 
-#define MAX_SIZE 50
+// #define MAX_SIZE 50
 #define MAX_CONCURRENT 50
 #define MAX_ERROR_LEN 128
 
-struct grpc_request_t {
+struct grpc_client_request_t {
     BASE_SPAN_PROPERTIES
     char err_msg[MAX_ERROR_LEN];
     char method[MAX_SIZE];
@@ -21,18 +21,18 @@ struct grpc_request_t {
     u32 status_code;
 };
 
-struct hpack_header_field {
-    struct go_string name;
-    struct go_string value;
-    bool sensitive;
-};
+// struct hpack_header_field {
+//     struct go_string name;
+//     struct go_string value;
+//     bool sensitive;
+// };
 
 struct {
     __uint(type, BPF_MAP_TYPE_HASH);
     __type(key, void *);
-    __type(value, struct grpc_request_t);
+    __type(value, struct grpc_client_request_t);
     __uint(max_entries, MAX_CONCURRENT);
-} grpc_events SEC(".maps");
+} grpc_client_events SEC(".maps");
 
 struct {
     __uint(type, BPF_MAP_TYPE_HASH);
@@ -62,18 +62,23 @@ int uprobe_ClientConn_Invoke(struct pt_regs *ctx) {
     u64 method_ptr_pos = 4;
     u64 method_len_pos = 5;
 
-    struct go_iface go_context = {0};
-    get_Go_context(ctx, 2, 0, true, &go_context);
+    // struct go_iface go_context = {0};
+    // get_Go_context(ctx, 2, 0, true);
+    void *context_ptr_val = get_Go_context(ctx, 2, 0, true);
+    if (context_ptr_val == NULL)
+    {
+        return 0;
+    }
 
     // Get key
     void *key = (void *)GOROUTINE(ctx);
-    void *grpcReq_ptr = bpf_map_lookup_elem(&grpc_events, &key);
+    void *grpcReq_ptr = bpf_map_lookup_elem(&grpc_client_events, &key);
     if (grpcReq_ptr != NULL) {
         bpf_printk("uprobe/ClientConn_Invoke already tracked with the current context");
         return 0;
     }
 
-    struct grpc_request_t grpcReq = {};
+    struct grpc_client_request_t grpcReq = {};
     grpcReq.start_time = bpf_ktime_get_ns();
 
     // Read Method
@@ -103,8 +108,8 @@ int uprobe_ClientConn_Invoke(struct pt_regs *ctx) {
     // start_span(&start_span_params);
 
     // Write event
-    bpf_map_update_elem(&grpc_events, &key, &grpcReq, 0);
-    start_tracking_span(go_context.data, &grpcReq.sc);
+    bpf_map_update_elem(&grpc_client_events, &key, &grpcReq, 0);
+    start_tracking_span(context_ptr_val, &grpcReq.sc);
     return 0;
 }
 
@@ -113,7 +118,7 @@ int uprobe_ClientConn_Invoke(struct pt_regs *ctx) {
 SEC("uprobe/ClientConn_Invoke")
 int uprobe_ClientConn_Invoke_Returns(struct pt_regs *ctx) {
     void *key = (void *)GOROUTINE(ctx);
-    struct grpc_request_t *grpc_span = bpf_map_lookup_elem(&grpc_events, &key);
+    struct grpc_client_request_t *grpc_span = bpf_map_lookup_elem(&grpc_client_events, &key);
     if (grpc_span == NULL) {
         bpf_printk("event is NULL in ret probe");
         return 0;
@@ -160,7 +165,7 @@ done:
     grpc_span->end_time = bpf_ktime_get_ns();
     // output_span_event(ctx, grpc_span, sizeof(*grpc_span), &grpc_span->sc);
     stop_tracking_span(&grpc_span->sc, &grpc_span->psc);
-    bpf_map_delete_elem(&grpc_events, &key);
+    bpf_map_delete_elem(&grpc_client_events, &key);
     return 0;
 }
 
@@ -180,7 +185,7 @@ int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx) {
     bpf_probe_read(&current_span_context, sizeof(current_span_context), sc_ptr);
 
     char tp_key[11] = "traceparent";
-    struct go_string key_str = write_user_go_string(tp_key, sizeof(tp_key));
+    struct go_string_ot key_str = write_user_go_string(tp_key, sizeof(tp_key));
     if (key_str.len == 0) {
         bpf_printk("key write failed, aborting ebpf probe");
         goto done;
@@ -189,7 +194,7 @@ int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx) {
     // Write headers
     char val[SPAN_CONTEXT_STRING_SIZE];
     span_context_to_w3c_string(&current_span_context, val);
-    struct go_string val_str = write_user_go_string(val, sizeof(val));
+    struct go_string_ot val_str = write_user_go_string(val, sizeof(val));
     if (val_str.len == 0) {
         bpf_printk("val write failed, aborting ebpf probe");
         goto done;
@@ -207,17 +212,17 @@ done:
 SEC("uprobe/http2Client_NewStream")
 // func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (*Stream, error)
 int uprobe_http2Client_NewStream(struct pt_regs *ctx) {
-    struct go_iface go_context = {0};
-    get_Go_context(ctx, 2, 0, true, &go_context);
-    void *httpclient_ptr = get_argument(ctx, 1);
-    u32 nextid = 0;
-    bpf_probe_read(&nextid, sizeof(nextid), (void *)(httpclient_ptr + (httpclient_nextid_pos)));
-    // Get the span context from go context. The mapping is created in the Invoke probe,
-    // the context here is derived from the Invoke context.
-    struct span_context *current_span_context = get_parent_span_context(&go_context);
-    if (current_span_context != NULL) {
-        bpf_map_update_elem(&streamid_to_span_contexts, &nextid, current_span_context, 0);
-    }
+    // struct go_iface go_context = {0};
+    // get_Go_context(ctx, 2, 0, true);
+    // void *httpclient_ptr = get_argument(ctx, 1);
+    // u32 nextid = 0;
+    // bpf_probe_read(&nextid, sizeof(nextid), (void *)(httpclient_ptr + (httpclient_nextid_pos)));
+    // // Get the span context from go context. The mapping is created in the Invoke probe,
+    // // the context here is derived from the Invoke context.
+    // struct span_context *current_span_context = get_parent_span_context(&go_context);
+    // if (current_span_context != NULL) {
+    //     bpf_map_update_elem(&streamid_to_span_contexts, &nextid, current_span_context, 0);
+    // }
 
     return 0;
 }

+ 74 - 50
ebpftracer/ebpf/utrace/go/net/grpc.server.probe.bpf.c

@@ -11,7 +11,7 @@
 
 // char __license[] SEC("license") = "Dual MIT/GPL";
 
-#define MAX_SIZE 100
+// #define MAX_SIZE 100
 #define MAX_CONCURRENT 50
 #define MAX_HEADERS 20
 #define MAX_HEADER_STRING 50
@@ -46,8 +46,8 @@ struct {
 } grpc_storage_map SEC(".maps");
 
 struct hpack_header_field {
-    struct go_string name;
-    struct go_string value;
+    struct go_string_ot name;
+    struct go_string_ot value;
     bool sensitive;
 };
 
@@ -100,6 +100,7 @@ handleStream(struct pt_regs *ctx, void *stream_ptr, struct go_iface *go_context)
 
     // Get parent context if exists
     u32 stream_id = 0;
+    __u32 zero = 0;
     long rc =
         bpf_probe_read_user(&stream_id, sizeof(stream_id), (void *)(stream_ptr + stream_id_pos));
     if (rc != 0) {
@@ -163,19 +164,18 @@ handleStream(struct pt_regs *ctx, void *stream_ptr, struct go_iface *go_context)
     start_tracking_span(go_context->data, &grpcReq->sc);
 
     //处理http请求之前,确认进程信息是否存在
+    __u64 id = bpf_get_current_pid_tgid();
+    __u32 pid = id >> 32;
     struct ebpf_proc_info *proc_info = bpf_map_lookup_elem(&proc_info_map, &pid);
     if (!proc_info) {
-        cw_bpf_debug("[Trace End in l7][Response][HTTP]:no proc info. pid:%d \n",k.pid);
+        cw_bpf_debug("[Trace End in l7][Response][HTTP]:no proc info. pid:%d \n",pid);
         return 0;
     }
 
-    __u64 pid_tgid = bpf_get_current_pid_tgid();
-    __u32 pid = pid_tgid >> 32;
-
     struct l7_request_key k = {};
     k.pid = pid;
-    k.fd = args->fd;
-    k.is_tls = is_tls;
+    k.fd = 0;
+    k.is_tls = 0;
     k.stream_id = -1;
 
     struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
@@ -251,13 +251,22 @@ int uprobe_server_handleStream(struct pt_regs *ctx) {
     u64 stream_pos = 4;
     void *stream_ptr = get_argument(ctx, stream_pos);
     // Get key
+    __u64 pid_tgid = bpf_get_current_pid_tgid();
+	__u32 tgid = pid_tgid >> 32;
+	struct ebpf_proc_info *proc_info =
+			bpf_map_lookup_elem(&proc_info_map, &tgid);
+    if(!proc_info)
+    {
+	    cw_bpf_debug("[uprobe_HandlerFunc_ServeHTTP] no proc info");
+        return 0;
+    }
     struct go_iface go_context = {0};
-    get_Go_context(ctx, stream_pos, stream_ctx_pos, false, &go_context);
+    get_Go_context(ctx, stream_pos, proc_info->ctx_ptr_pos, false);
 
     return handleStream(ctx, stream_ptr, &go_context);
 }
 
-UPROBE_RETURN(server_handleStream, struct grpc_request_t, grpc_events)
+// UPROBE_RETURN(server_handleStream, struct grpc_request_t, grpc_events)
 
 // This instrumentation attaches uprobe to the following function:
 // func (s *Server) handleStream(t transport.ServerTransport, stream *transport.ServerStream)
@@ -308,6 +317,21 @@ int uprobe_server_handleStream2(struct pt_regs *ctx) {
 SEC("uprobe/server_handleStream2")
 int uprobe_server_handleStream2_Returns(struct pt_regs *ctx) {
     u64 server_stream_pos = 4;
+    __u64 id = bpf_get_current_pid_tgid();
+	__u32 zero = 0;
+    __u32 fd = 0;
+	__u32 pid, tid;
+	__u32 http_status = 200;
+
+	pid = id >> 32;
+	tid =  (__u32)id;
+
+    struct l7_request_key k = {};
+    k.pid = pid;
+    k.fd = fd;
+    k.is_tls = 0;
+    k.stream_id = -1;
+
     void *server_stream_ptr = get_argument(ctx, server_stream_pos);
     void *key = NULL;
     if (server_stream_ptr == NULL) {
@@ -382,9 +406,9 @@ lookup:
     e->trace_start = 0;
     e->trace_end = 1;
     e->trace_id = trace_id;
-    e->payload_size = size;
+    e->payload_size = 0;
     e->event_count = event_count;
-    COPY_PAYLOAD(e->payload, size, payload);
+    // COPY_PAYLOAD(e->payload, size, payload);
     bpf_map_delete_elem(&active_l7_requests, &k);
 	// 清除事件计数
 	bpf_map_delete_elem(&trace_event_count_heap, &trace_id);
@@ -392,16 +416,16 @@ lookup:
 	clear_parent_span_context_by_trace_key(start_trace_info->trace_key);
 	// 清除trace信息
 	cw_clear_trace(pid, tid, fd);
-    cw_bpf_debug("socket accept bytes_sent cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
-    struct accept_connection *accept_conn = bpf_map_lookup_elem(&active_accepts, &cid);
-    if (accept_conn) {
-        cw_bpf_debug("socket accept bytes_sent after cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
-        cw_bpf_debug("rock enter the  accept_conn function cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
-        e->sport = accept_conn->sport;
-        e->dport = accept_conn->dport;
-        __builtin_memcpy(&e->saddr, &accept_conn->saddr, sizeof(e->saddr));
-        __builtin_memcpy(&e->daddr, &accept_conn->daddr, sizeof(e->daddr));
-    }
+    // cw_bpf_debug("socket accept bytes_sent cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
+    // struct accept_connection *accept_conn = bpf_map_lookup_elem(&active_accepts, &cid);
+    // if (accept_conn) {
+    //     cw_bpf_debug("socket accept bytes_sent after cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
+    //     cw_bpf_debug("rock enter the  accept_conn function cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
+    //     e->sport = accept_conn->sport;
+    //     e->dport = accept_conn->dport;
+    //     __builtin_memcpy(&e->saddr, &accept_conn->saddr, sizeof(e->saddr));
+    //     __builtin_memcpy(&e->daddr, &accept_conn->daddr, sizeof(e->daddr));
+    // }
     return 0;
 }
 
@@ -415,33 +439,33 @@ int uprobe_http2Server_operateHeader(struct pt_regs *ctx) {
     void *frame_ptr = is_new_frame_pos ? arg4 : arg2;
     struct go_slice header_fields = {};
     bpf_probe_read(&header_fields, sizeof(header_fields), (void *)(frame_ptr + frame_fields_pos));
-    char key[W3C_KEY_LENGTH] = "traceparent";
-    for (s32 i = 0; i < MAX_HEADERS; i++) {
-        if (i >= header_fields.len) {
-            break;
-        }
-        struct hpack_header_field hf = {};
-        long res =
-            bpf_probe_read(&hf, sizeof(hf), (void *)(header_fields.array + (i * sizeof(hf))));
-        if (hf.name.len == W3C_KEY_LENGTH && hf.value.len == W3C_VAL_LENGTH) {
-            char current_key[W3C_KEY_LENGTH];
-            bpf_probe_read(current_key, sizeof(current_key), hf.name.str);
-            if (bpf_memcmp(key, current_key, sizeof(key))) {
-                char val[W3C_VAL_LENGTH];
-                bpf_probe_read(val, W3C_VAL_LENGTH, hf.value.str);
-
-                // Get stream id
-                void *headers_frame = NULL;
-                bpf_probe_read(&headers_frame, sizeof(headers_frame), frame_ptr);
-                u32 stream_id = 0;
-                bpf_probe_read(
-                    &stream_id, sizeof(stream_id), (void *)(headers_frame + frame_stream_id_pod));
-                struct grpc_request_t grpcReq = {};
-                w3c_string_to_span_context(val, &grpcReq.psc);
-                bpf_map_update_elem(&streamid_to_grpc_events, &stream_id, &grpcReq, 0);
-            }
-        }
-    }
+    // char key[W3C_KEY_LENGTH] = "traceparent";
+    // for (s32 i = 0; i < MAX_HEADERS; i++) {
+    //     if (i >= header_fields.len) {
+    //         break;
+    //     }
+    //     struct hpack_header_field hf = {};
+    //     long res =
+    //         bpf_probe_read(&hf, sizeof(hf), (void *)(header_fields.ptr + (i * sizeof(hf))));
+    //     if (hf.name.len == W3C_KEY_LENGTH && hf.value.len == W3C_VAL_LENGTH) {
+    //         char current_key[W3C_KEY_LENGTH];
+    //         bpf_probe_read(current_key, sizeof(current_key), hf.name.str);
+    //         if (bpf_memcmp(key, current_key, sizeof(key))) {
+    //             char val[W3C_VAL_LENGTH];
+    //             bpf_probe_read(val, W3C_VAL_LENGTH, hf.value.str);
+
+    //             // Get stream id
+    //             void *headers_frame = NULL;
+    //             bpf_probe_read(&headers_frame, sizeof(headers_frame), frame_ptr);
+    //             u32 stream_id = 0;
+    //             bpf_probe_read(
+    //                 &stream_id, sizeof(stream_id), (void *)(headers_frame + frame_stream_id_pod));
+    //             struct grpc_request_t grpcReq = {};
+    //             w3c_string_to_span_context(val, &grpcReq.psc);
+    //             bpf_map_update_elem(&streamid_to_grpc_events, &stream_id, &grpcReq, 0);
+    //         }
+    //     }
+    // }
 
     return 0;
 }