Skip to content

Commit

Permalink
update net/http probe to get args from ctx instead of goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeGoldsmith committed Apr 19, 2023
1 parent 4419cc8 commit 7c076e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
6 changes: 0 additions & 6 deletions include/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
20 changes: 11 additions & 9 deletions pkg/instrumentors/bpf/net/http/server/bpf/probe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,31 @@ 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;
}

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;
}

0 comments on commit 7c076e8

Please sign in to comment.