From 7c076e861d04a1625b019064293339e6a474a5f4 Mon Sep 17 00:00:00 2001 From: Mike Goldsmth Date: Wed, 19 Apr 2023 14:09:55 +0100 Subject: [PATCH] update net/http probe to get args from ctx instead of goroutine --- include/arguments.h | 6 ------ .../bpf/net/http/server/bpf/probe.bpf.c | 20 ++++++++++--------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/include/arguments.h b/include/arguments.h index b66a90a87..a01ac9c68 100644 --- a/include/arguments.h +++ b/include/arguments.h @@ -63,9 +63,3 @@ void *get_argument(struct pt_regs *ctx, int index) return get_argument_by_stack(ctx, index); } - -// In x86, current goroutine is pointed by r14, according to -// https://go.googlesource.com/go/+/refs/heads/dev.regabi/src/cmd/compile/internal-abi.md#amd64-architecture -inline void *get_goroutine_address(struct pt_regs *ctx) { - return (void *)(ctx->r14); -} diff --git a/pkg/instrumentors/bpf/net/http/server/bpf/probe.bpf.c b/pkg/instrumentors/bpf/net/http/server/bpf/probe.bpf.c index b49802cfe..377948f56 100644 --- a/pkg/instrumentors/bpf/net/http/server/bpf/probe.bpf.c +++ b/pkg/instrumentors/bpf/net/http/server/bpf/probe.bpf.c @@ -82,13 +82,14 @@ int uprobe_ServerMux_ServeHTTP(struct pt_regs *ctx) path_size = path_size < path_len ? path_size : path_len; bpf_probe_read(&httpReq.path, path_size, path_ptr); - // Get goroutine pointer - void *goroutine = get_goroutine_address(ctx); + // Get Request.ctx + void *ctx_iface = 0; + bpf_probe_read(&ctx_iface, sizeof(ctx_iface), (void *)(req_ptr + ctx_ptr_pos + 8)); // Write event httpReq.sc = generate_span_context(); - bpf_map_update_elem(&context_to_http_events, &goroutine, &httpReq, 0); - long res = bpf_map_update_elem(&spans_in_progress, &goroutine, &httpReq.sc, 0); + bpf_map_update_elem(&context_to_http_events, &ctx_iface, &httpReq, 0); + long res = bpf_map_update_elem(&spans_in_progress, &ctx_iface, &httpReq.sc, 0); return 0; } @@ -96,15 +97,16 @@ SEC("uprobe/ServerMux_ServeHTTP") int uprobe_ServerMux_ServeHTTP_Returns(struct pt_regs *ctx) { u64 request_pos = 4; - void *req_ptr = get_argument(ctx, request_pos); - void *goroutine = get_goroutine_address(ctx); + void *req_ptr = get_argument_by_stack(ctx, request_pos); + void *ctx_iface = 0; + bpf_probe_read(&ctx_iface, sizeof(ctx_iface), (void *)(req_ptr + ctx_ptr_pos + 8)); - void *httpReq_ptr = bpf_map_lookup_elem(&context_to_http_events, &goroutine); + void *httpReq_ptr = bpf_map_lookup_elem(&context_to_http_events, &ctx_iface); struct http_request_t httpReq = {}; bpf_probe_read(&httpReq, sizeof(httpReq), httpReq_ptr); httpReq.end_time = bpf_ktime_get_ns(); bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &httpReq, sizeof(httpReq)); - bpf_map_delete_elem(&context_to_http_events, &goroutine); - bpf_map_delete_elem(&spans_in_progress, &goroutine); + bpf_map_delete_elem(&context_to_http_events, &ctx_iface); + bpf_map_delete_elem(&spans_in_progress, &ctx_iface); return 0; } \ No newline at end of file