-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to read trace id from goroutine labels of Go processes #2574
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
// +build ignore | ||
// ^^ this is a golang build tag meant to exclude this C file from compilation | ||
// by the CGO compiler | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
// Copyright 2024 The Parca Authors | ||
|
||
#include "vmlinux.h" | ||
#include "basic_types.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_core_read.h> | ||
#include "tls.h" | ||
|
||
struct go_string | ||
{ | ||
char *str; | ||
s64 len; | ||
}; | ||
|
||
struct go_slice | ||
{ | ||
void *array; | ||
s64 len; | ||
s64 cap; | ||
}; | ||
|
||
struct map_bucket { | ||
char tophash[8]; | ||
struct go_string keys[8]; | ||
struct go_string values[8]; | ||
void *overflow; | ||
}; | ||
|
||
struct | ||
{ | ||
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); | ||
__uint(key_size, sizeof(u32)); | ||
__uint(value_size, sizeof(struct map_bucket)); | ||
__uint(max_entries, 1); | ||
} golang_mapbucket_storage_map SEC(".maps"); | ||
|
||
// length of "otel.traceid" is 12 | ||
#define TRACEID_MAP_KEY_LENGTH 12 | ||
#define TRACEID_MAP_VAL_LENGTH 32 | ||
#define MAX_BUCKETS 8 | ||
|
||
static __always_inline bool bpf_memcmp(char *s1, char *s2, s32 size) | ||
{ | ||
for (int i = 0; i < size; i++) | ||
{ | ||
if (s1[i] != s2[i]) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static __always_inline void hex_string_to_bytes(char *str, u32 size, unsigned char *out) | ||
{ | ||
for (int i = 0; i < (size / 2); i++) | ||
{ | ||
char ch0 = str[2 * i]; | ||
char ch1 = str[2 * i + 1]; | ||
u8 nib0 = (ch0 & 0xF) + (ch0 >> 6) | ((ch0 >> 3) & 0x8); | ||
u8 nib1 = (ch1 & 0xF) + (ch1 >> 6) | ((ch1 >> 3) & 0x8); | ||
out[i] = (nib0 << 4) | nib1; | ||
} | ||
} | ||
|
||
// Go processes store the current goroutine in thread local store. From there | ||
// this reads the g (aka goroutine) struct, then the m (the actual operating | ||
// system thread) of that goroutine, and finally curg (current goroutine). This | ||
// chain is necessary because getg().m.curg points to the current user g | ||
// assigned to the thread (curg == getg() when not on the system stack). curg | ||
// may be nil if there is no user g, such as when running in the scheduler. If | ||
// curg is nil, then g is either a system stack (called g0) or a signal handler | ||
// g (gsignal). Neither one will ever have labels. | ||
static __always_inline bool get_trace_id(unsigned char *res_trace_id) { | ||
long res; | ||
struct task_struct *task = (struct task_struct *)bpf_get_current_task(); | ||
if (task == NULL) { | ||
return false; | ||
} | ||
|
||
// It appears from all Go binaries we looked at 0xfffffffffffffff8 is the offset of `runtime.g`. | ||
u64 g_addr_offset = 0xfffffffffffffff8; | ||
|
||
size_t g_addr; | ||
res = bpf_probe_read_user(&g_addr, sizeof(void *), (void*)(read_tls_base(task)+g_addr_offset)); | ||
if (res < 0) { | ||
return false; | ||
} | ||
|
||
// DW_TAG_member | ||
// DW_AT_name ("m") | ||
// DW_AT_data_member_location (48) | ||
// DW_AT_type (0x0000000000088e39 "runtime.m *") | ||
// DW_AT_GO_embedded_field (0x00) | ||
size_t m_ptr_addr; | ||
res = bpf_probe_read_user(&m_ptr_addr, sizeof(void *), (void*)(g_addr+48)); | ||
if (res < 0) { | ||
return false; | ||
} | ||
|
||
// DW_TAG_member | ||
// DW_AT_name ("curg") | ||
// DW_AT_data_member_location (192) | ||
// DW_AT_type (0x00000000000892b1 "runtime.g *") | ||
// DW_AT_GO_embedded_field (0x00) | ||
size_t curg_ptr_addr; | ||
res = bpf_probe_read_user(&curg_ptr_addr, sizeof(void *), (void*)(m_ptr_addr+192)); | ||
if (res < 0) { | ||
return false; | ||
} | ||
|
||
// DW_TAG_member | ||
// DW_AT_name ("labels") | ||
// DW_AT_data_member_location (360) | ||
// DW_AT_type (0x000000000005c242 "void *") | ||
// DW_AT_GO_embedded_field (0x00) | ||
void *labels_map_ptr_ptr; | ||
res = bpf_probe_read_user(&labels_map_ptr_ptr, sizeof(void *), (void*)(curg_ptr_addr+360)); | ||
if (res < 0) { | ||
return false; | ||
} | ||
|
||
void *labels_map_ptr; | ||
res = bpf_probe_read(&labels_map_ptr, sizeof(labels_map_ptr), labels_map_ptr_ptr); | ||
if (res < 0) { | ||
return false; | ||
} | ||
|
||
u64 labels_count = 0; | ||
res = bpf_probe_read(&labels_count, sizeof(labels_count), labels_map_ptr); | ||
if (res < 0) { | ||
return false; | ||
} | ||
if (labels_count == 0) { | ||
return false; | ||
} | ||
|
||
unsigned char log_2_bucket_count; | ||
res = bpf_probe_read(&log_2_bucket_count, sizeof(log_2_bucket_count), labels_map_ptr + 9); | ||
if (res < 0) { | ||
return false; | ||
} | ||
u64 bucket_count = 1 << log_2_bucket_count; | ||
void *label_buckets; | ||
res = bpf_probe_read(&label_buckets, sizeof(label_buckets), labels_map_ptr + 16); | ||
if (res < 0) { | ||
return false; | ||
} | ||
|
||
u32 map_id = 0; | ||
// This needs to be allocated in a per-cpu map, because it's too large and | ||
// can't be allocated on the stack (which is limited to 512 bytes in bpf). | ||
struct map_bucket *map_value = bpf_map_lookup_elem(&golang_mapbucket_storage_map, &map_id); | ||
if (!map_value) { | ||
return NULL; | ||
} | ||
|
||
for (u64 j = 0; j < MAX_BUCKETS; j++) { | ||
if (j >= bucket_count) { | ||
break; | ||
} | ||
res = bpf_probe_read(map_value, sizeof(struct map_bucket), label_buckets + (j * sizeof(struct map_bucket))); | ||
if (res < 0) { | ||
continue; | ||
} | ||
for (u64 i = 0; i < 8; i++) { | ||
if (map_value->tophash[i] == 0) { | ||
continue; | ||
} | ||
if (map_value->keys[i].len != TRACEID_MAP_KEY_LENGTH) { | ||
continue; | ||
} | ||
|
||
char current_label_key[TRACEID_MAP_KEY_LENGTH]; | ||
bpf_probe_read(current_label_key, sizeof(current_label_key), map_value->keys[i].str); | ||
if (!bpf_memcmp(current_label_key, "otel.traceid", TRACEID_MAP_KEY_LENGTH)) { | ||
continue; | ||
} | ||
|
||
if (map_value->values[i].len != TRACEID_MAP_VAL_LENGTH) { | ||
continue; | ||
} | ||
|
||
char trace_id[TRACEID_MAP_VAL_LENGTH]; | ||
bpf_probe_read(trace_id, TRACEID_MAP_VAL_LENGTH, map_value->values[i].str); | ||
|
||
hex_string_to_bytes(trace_id, TRACEID_MAP_VAL_LENGTH, res_trace_id); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build ignore | ||
// ^^ this is a golang build tag meant to exclude this C file from compilation | ||
// by the CGO compiler | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
// Copyright 2024 The Parca Authors | ||
|
||
#include "vmlinux.h" | ||
|
||
#include <bpf/bpf_core_read.h> | ||
|
||
static inline __attribute__((__always_inline__)) long unsigned int read_tls_base(struct task_struct *task) { | ||
long unsigned int tls_base; | ||
// This changes depending on arch and kernel version. | ||
// task->thread.fs, task->thread.uw.tp_value, etc. | ||
#if __TARGET_ARCH_x86 | ||
tls_base = BPF_CORE_READ(task, thread.fsbase); | ||
#elif __TARGET_ARCH_arm64 | ||
tls_base = BPF_CORE_READ(task, thread.uw.tp_value); | ||
#else | ||
#error "Unsupported platform" | ||
#endif | ||
return tls_base; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we declare a map struct and collapse these 3 reads into one? Would probably turn a 8 byte read, followed by 1 byte read, followed by 8 byte read into a memcpy I'm guessing? Probably a wash performance wise but maybe smaller code? Feel free to file under "thing for Tom to investigate".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be possible to optimize, but the bpf stack is only 512bytes, which already caused me to have to move one of these to be stored in a per-cpu bpf map instead