Skip to content

Commit c72996e

Browse files
korniltsevgnurizen
authored andcommitted
coredump: enable cgo warnings (open-telemetry#613)
1 parent e8c8975 commit c72996e

21 files changed

Lines changed: 207 additions & 198 deletions

support/ebpf/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ FLAGS=$(TARGET_FLAGS) -g \
3939
-Wall -Wextra -Werror \
4040
-Wno-address-of-packed-member \
4141
-Wno-unused-label \
42-
-Wno-unused-parameter \
4342
-Wno-sign-compare \
4443
-fno-stack-protector \
4544
-D__SOURCE_DATE_EPOCH__=0 \
@@ -81,7 +80,7 @@ bloatcheck: $(TRACER_NAME)
8180
python3 bloat-o-meter $(TRACER_NAME).baseline $(TRACER_NAME)
8281

8382
lint:
84-
$(CLANG_FORMAT) -Werror --dry-run -style=file *.[ch]
83+
$(CLANG_FORMAT) -Werror --dry-run -style=file *.[ch] ../../tools/coredump/*.[ch]
8584

8685
format:
8786
$(CLANG_FORMAT) -i -style=file *.[ch]

support/ebpf/bpfdefs.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// with_debug_output is declared in native_stack_trace.ebpf.c
88
extern u32 with_debug_output;
99

10+
// UNUSED is a macro that marks a parameter or variable as intentionally unused.
11+
// It prevents compiler warnings about unused variables while keeping them in the code.
12+
#define UNUSED __attribute__((unused))
13+
1014
#if defined(TESTING_COREDUMP)
1115

1216
// BPF_RODATA_VAR declares a global variable in the .rodata section,
@@ -26,6 +30,9 @@ extern u32 with_debug_output;
2630
#define printt(fmt, ...) bpf_log(fmt, ##__VA_ARGS__)
2731
#define DEBUG_PRINT(fmt, ...) bpf_log(fmt, ##__VA_ARGS__)
2832

33+
// Macro for loop unrolling. Expands to nothing for TESTING_COREDUMP.
34+
#define UNROLL
35+
2936
// BPF helpers. Mostly stubs to dispatch the call to Go code with the context ID.
3037
int bpf_tail_call(void *ctx, bpf_map_def *map, int index);
3138
unsigned long long bpf_ktime_get_ns(void);
@@ -37,7 +44,7 @@ static inline long bpf_probe_read_user(void *buf, u32 sz, const void *ptr)
3744
return __bpf_probe_read_user(__cgo_ctx->id, buf, sz, ptr);
3845
}
3946

40-
static inline long bpf_probe_read_kernel(void *buf, u32 sz, const void *ptr)
47+
static inline long bpf_probe_read_kernel(UNUSED void *buf, UNUSED u32 sz, UNUSED const void *ptr)
4148
{
4249
return -1;
4350
}
@@ -53,23 +60,29 @@ static inline void *bpf_map_lookup_elem(bpf_map_def *map, const void *key)
5360
return __bpf_map_lookup_elem(__cgo_ctx->id, map, key);
5461
}
5562

56-
static inline int bpf_map_update_elem(bpf_map_def *map, const void *key, const void *val, u64 flags)
63+
static inline int bpf_map_update_elem(
64+
UNUSED bpf_map_def *map, UNUSED const void *key, UNUSED const void *val, UNUSED u64 flags)
5765
{
5866
return -1;
5967
}
6068

61-
static inline int bpf_map_delete_elem(bpf_map_def *map, const void *key)
69+
static inline int bpf_map_delete_elem(UNUSED bpf_map_def *map, UNUSED const void *key)
6270
{
6371
return -1;
6472
}
6573

6674
static inline int bpf_perf_event_output(
67-
void *ctx, bpf_map_def *mapdef, unsigned long long flags, void *data, int size)
75+
UNUSED void *ctx,
76+
UNUSED bpf_map_def *mapdef,
77+
UNUSED unsigned long long flags,
78+
UNUSED void *data,
79+
UNUSED int size)
6880
{
81+
6982
return 0;
7083
}
7184

72-
static inline int bpf_get_stackid(void *ctx, bpf_map_def *map, u64 flags)
85+
static inline int bpf_get_stackid(UNUSED void *ctx, UNUSED bpf_map_def *map, UNUSED u64 flags)
7386
{
7487
return -1;
7588
}
@@ -168,6 +181,15 @@ static long (*bpf_probe_read_kernel)(void *dst, int size, const void *unsafe_ptr
168181
__attribute__((section(name), used)) _Pragma("GCC diagnostic pop")
169182
#define EBPF_INLINE __attribute__((__always_inline__))
170183

184+
#if defined(__clang__)
185+
// Macro for loop unrolling. Expands to the appropriate pragma for clang.
186+
// Unrolls up to 256 loop iterations.
187+
#define UNROLL _Pragma("unroll 256")
188+
#else
189+
// Macro for loop unrolling. Expands to nothing for gcc.
190+
#define UNROLL
191+
#endif
192+
171193
#endif // !TESTING_COREDUMP
172194

173195
#define MIN(a, b) (((a) < (b)) ? (a) : (b))

support/ebpf/dotnet_tracer.ebpf.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ bpf_map_def SEC("maps") dotnet_procs = {
4444
// currently not Garbage Collected by the runtime. Though, we have submitted also an enhancement
4545
// request to fix the nibble map format to something sane, and this might get implemented.
4646
// see: https://github.com/dotnet/runtime/issues/93550
47-
static EBPF_INLINE ErrorCode
48-
dotnet_find_code_start(PerCPURecord *record, DotnetProcInfo *vi, u64 pc, u64 *code_start)
47+
static EBPF_INLINE ErrorCode dotnet_find_code_start(PerCPURecord *record, u64 pc, u64 *code_start)
4948
{
5049
// This is an ebpf optimized implementation of EEJitManager::FindMethodCode()
5150
// https://github.com/dotnet/runtime/blob/v7.0.15/src/coreclr/vm/codeman.cpp#L4115
@@ -107,11 +106,12 @@ dotnet_find_code_start(PerCPURecord *record, DotnetProcInfo *vi, u64 pc, u64 *co
107106
// convert it from u32 to u64 offset
108107
int pos64 = (map_elements - 2) / 2;
109108
u64 val64 = scratch->map64[--pos64];
110-
#pragma unroll 256
109+
111110
// the loop iterations is number of u64 elements minus two:
112111
// - the last element special handled earlier
113112
// - the second last element which is preloaded immediately above
114-
for (int i = 0; i < map_elements / 2 - 2; i++) {
113+
UNROLL for (int i = 0; i < map_elements / 2 - 2; i++)
114+
{
115115
if (val64 != 0) {
116116
break;
117117
}
@@ -146,8 +146,8 @@ dotnet_find_code_start(PerCPURecord *record, DotnetProcInfo *vi, u64 pc, u64 *co
146146
}
147147

148148
// Decode the code start info from the entry
149-
#pragma unroll
150-
for (int i = 0; i < DOTNET_CODE_NIBBLES_PER_ENTRY; i++) {
149+
UNROLL for (int i = 0; i < DOTNET_CODE_NIBBLES_PER_ENTRY; i++)
150+
{
151151
u8 nybble = val & 0xf;
152152
if (nybble != 0) {
153153
*code_start = pc_base + pc_delta + (nybble - 1) * DOTNET_CODE_ALIGN;
@@ -177,8 +177,7 @@ push_dotnet(Trace *trace, u64 code_header_ptr, u64 pc_offset, bool return_addres
177177
}
178178

179179
// Unwind one dotnet frame
180-
static EBPF_INLINE ErrorCode
181-
unwind_one_dotnet_frame(PerCPURecord *record, DotnetProcInfo *vi, bool top)
180+
static EBPF_INLINE ErrorCode unwind_one_dotnet_frame(PerCPURecord *record)
182181
{
183182
UnwindState *state = &record->state;
184183
Trace *trace = &record->trace;
@@ -241,7 +240,7 @@ unwind_one_dotnet_frame(PerCPURecord *record, DotnetProcInfo *vi, bool top)
241240
}
242241

243242
// JIT generated code, locate code start
244-
ErrorCode error = dotnet_find_code_start(record, vi, pc, &code_start);
243+
ErrorCode error = dotnet_find_code_start(record, pc, &code_start);
245244
if (error != ERR_OK) {
246245
DEBUG_PRINT("dotnet: --> code_start failed with %d", error);
247246
// dotnet_find_code_start incremented the metric already
@@ -305,11 +304,11 @@ static EBPF_INLINE int unwind_dotnet(struct pt_regs *ctx)
305304
record->ratelimitAction = RATELIMIT_ACTION_FAST;
306305
increment_metric(metricID_UnwindDotnetAttempts);
307306

308-
#pragma unroll
309-
for (int i = 0; i < DOTNET_FRAMES_PER_PROGRAM; i++) {
307+
UNROLL for (int i = 0; i < DOTNET_FRAMES_PER_PROGRAM; i++)
308+
{
310309
unwinder = PROG_UNWIND_STOP;
311310

312-
error = unwind_one_dotnet_frame(record, vi, i == 0);
311+
error = unwind_one_dotnet_frame(record);
313312
if (error) {
314313
break;
315314
}

support/ebpf/hotspot_tracer.ebpf.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ static EBPF_INLINE u64 hotspot_find_codeblob(const UnwindState *state, const Hot
161161
// Segment map start is put in to the PidPageMapping's file_id.
162162
segmap_start = (state->text_section_id >> HS_TSID_SEG_MAP_BIT) & HS_TSID_SEG_MAP_MASK;
163163

164-
#pragma unroll
165-
for (int i = 0; i < HOTSPOT_SEGMAP_ITERATIONS; i++) {
164+
UNROLL for (int i = 0; i < HOTSPOT_SEGMAP_ITERATIONS; i++)
165+
{
166166
if (bpf_probe_read_user(&tag, sizeof(tag), (void *)(segmap_start + segment))) {
167167
return 0;
168168
}
@@ -304,7 +304,7 @@ static EBPF_INLINE ErrorCode hotspot_handle_interpreter(
304304
}
305305

306306
#if defined(__x86_64__)
307-
static EBPF_INLINE void breadcrumb_fixup(HotspotUnwindInfo *ui)
307+
static EBPF_INLINE void breadcrumb_fixup(UNUSED HotspotUnwindInfo *ui)
308308
{
309309
// Nothing to do: breadcrumbs are not a thing on X86.
310310
}
@@ -411,8 +411,8 @@ hotspot_handle_prologue(const CodeBlobInfo *cbi, HotspotUnwindInfo *ui, HotspotU
411411
#endif
412412

413413
#if defined(__x86_64__)
414-
static EBPF_INLINE bool
415-
hotspot_handle_epilogue(const CodeBlobInfo *cbi, HotspotUnwindInfo *ui, HotspotUnwindAction *action)
414+
static EBPF_INLINE bool hotspot_handle_epilogue(
415+
UNUSED const CodeBlobInfo *cbi, HotspotUnwindInfo *ui, HotspotUnwindAction *action)
416416
{
417417
// On X86, use a heuristic to catch the likely spots of the epilogue.
418418
#define CODE_CUR 1
@@ -431,8 +431,8 @@ hotspot_handle_epilogue(const CodeBlobInfo *cbi, HotspotUnwindInfo *ui, HotspotU
431431
// Is 'ret' instruction *possible* in the next 'code' bytes?
432432
// NOTE: This can find false positives because x86 is variable length
433433
// instruction set.
434-
#pragma unroll
435-
for (int i = CODE_CUR + 1; i < sizeof(code); i++) {
434+
UNROLL for (int i = CODE_CUR + 1; i < sizeof(code); i++)
435+
{
436436
if (code[i] == 0xc3) {
437437
goto found_ret;
438438
}
@@ -530,8 +530,8 @@ hotspot_handle_epilogue(const CodeBlobInfo *cbi, HotspotUnwindInfo *ui, HotspotU
530530
return false;
531531
}
532532

533-
#pragma unroll
534-
for (; find_offset < EPI_LOOKBACK - 1; ++find_offset) {
533+
UNROLL for (; find_offset < EPI_LOOKBACK - 1; ++find_offset)
534+
{
535535
if (*(u64 *)&window[find_offset] == needle) {
536536
goto pattern_found;
537537
}
@@ -574,7 +574,7 @@ pattern_found:;
574574

575575
static EBPF_INLINE ErrorCode hotspot_handle_nmethod(
576576
const CodeBlobInfo *cbi,
577-
Trace *trace,
577+
UNUSED Trace *trace,
578578
HotspotUnwindInfo *ui,
579579
HotspotProcInfo *ji,
580580
HotspotUnwindAction *action,
@@ -697,7 +697,7 @@ hotspot_handle_stub_fallback(const CodeBlobInfo *cbi, HotspotUnwindAction *actio
697697
}
698698

699699
static EBPF_INLINE ErrorCode hotspot_handle_stub(
700-
const UnwindState *state,
700+
UNUSED const UnwindState *state,
701701
const CodeBlobInfo *cbi,
702702
HotspotUnwindInfo *ui,
703703
HotspotUnwindAction *action)
@@ -940,8 +940,8 @@ static EBPF_INLINE int unwind_hotspot(struct pt_regs *ctx)
940940

941941
int unwinder = PROG_UNWIND_STOP;
942942
ErrorCode error = ERR_OK;
943-
#pragma unroll
944-
for (int i = 0; i < HOTSPOT_FRAMES_PER_PROGRAM; i++) {
943+
UNROLL for (int i = 0; i < HOTSPOT_FRAMES_PER_PROGRAM; i++)
944+
{
945945
unwinder = PROG_UNWIND_STOP;
946946
error = hotspot_unwind_one_frame(record, ji, i == 0);
947947
if (error) {

support/ebpf/integration_test.ebpf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ static EBPF_INLINE void send_sample_traces(void *ctx, u64 pid, s32 kstack)
6868
trace->comm[3] = 4;
6969
trace->stack_len = MAX_FRAME_UNWINDS;
7070
trace->kernel_stack_id = kstack;
71-
#pragma unroll
72-
for (u64 i = 0; i < MAX_FRAME_UNWINDS; ++i) {
71+
UNROLL for (u64 i = 0; i < MAX_FRAME_UNWINDS; ++i)
72+
{
7373
// NOTE: this init schema eats up a lot of instructions. If we need more
7474
// space later, we can instead just init `.kind` and a few fields in the
7575
// start, middle, and end of the trace.

support/ebpf/interpreter_dispatcher.ebpf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ bpf_map_def SEC("maps") cl_procs = {
140140
.max_entries = 128,
141141
};
142142

143-
static EBPF_INLINE void *get_m_ptr_legacy(struct GoCustomLabelsOffsets *offs, UnwindState *state)
143+
static EBPF_INLINE void *
144+
get_m_ptr_legacy(struct GoCustomLabelsOffsets *offs, UNUSED UnwindState *state)
144145
{
145146
long res;
146147

@@ -174,7 +175,7 @@ static EBPF_INLINE void *get_m_ptr_legacy(struct GoCustomLabelsOffsets *offs, Un
174175
return m_ptr_addr;
175176
}
176177

177-
static EBPF_INLINE void *get_m_ptr(struct GoLabelsOffsets *offs, UnwindState *state)
178+
static EBPF_INLINE void *get_m_ptr(struct GoLabelsOffsets *offs, UNUSED UnwindState *state)
178179
{
179180
u64 g_addr = 0;
180181
void *tls_base = NULL;

support/ebpf/luajit_tracer.ebpf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ walk_luajit_stack(PerCPURecord *record, const LuaJITProcInfo *info, int *next_un
393393
LJState *L = &record->luajitUnwindScratch.L;
394394
TValue *prevframe = record->luajitUnwindState.prevframe;
395395
TValue *bot = L->stack + 1;
396-
#pragma unroll
396+
397397
for (int i = 0; i < FRAMES_PER_WALK_LUAJIT_STACK; i++) {
398398
TValue *frame = (TValue *)(record->luajitUnwindState.frame);
399399
if (frame <= bot) {
@@ -582,7 +582,7 @@ static inline __attribute__((__always_inline__)) int unwind_luajit(struct pt_reg
582582
return -1;
583583

584584
UnwindState *state = &record->state;
585-
int unwinder = get_next_unwinder_after_interpreter(record);
585+
int unwinder = get_next_unwinder_after_interpreter();
586586
ErrorCode error = ERR_OK;
587587
u32 pid = record->trace.pid;
588588
LuaJITProcInfo *info = bpf_map_lookup_elem(&luajit_procs, &pid);

support/ebpf/native_stack_trace.ebpf.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ static EBPF_INLINE ErrorCode get_stack_delta(UnwindState *state, int *addrDiff,
191191
// Do the binary search, up to 16 iterations. Deltas are paged to 64kB pages.
192192
// They can contain at most 64kB deltas even if everything is single byte opcodes.
193193
int i;
194-
#pragma unroll
195-
for (i = 0; i < 16; i++) {
194+
UNROLL for (i = 0; i < 16; i++)
195+
{
196196
if (!bsearch_step(inner_map, &lo, &hi, page_offset)) {
197197
break;
198198
}
@@ -357,8 +357,7 @@ static EBPF_INLINE u64 unwind_register_address(UnwindState *state, u64 cfa, u8 o
357357
// is marked with UNWIND_COMMAND_STOP which marks entry points (main function,
358358
// thread spawn function, signal handlers, ...).
359359
#if defined(__x86_64__)
360-
static EBPF_INLINE ErrorCode
361-
unwind_one_frame(u64 pid, u32 frame_idx, UnwindState *state, bool *stop)
360+
static EBPF_INLINE ErrorCode unwind_one_frame(UnwindState *state, bool *stop)
362361
{
363362
*stop = false;
364363

@@ -452,8 +451,7 @@ unwind_one_frame(u64 pid, u32 frame_idx, UnwindState *state, bool *stop)
452451
return ERR_OK;
453452
}
454453
#elif defined(__aarch64__)
455-
static EBPF_INLINE ErrorCode
456-
unwind_one_frame(u64 pid, u32 frame_idx, struct UnwindState *state, bool *stop)
454+
static EBPF_INLINE ErrorCode unwind_one_frame(struct UnwindState *state, bool *stop)
457455
{
458456
*stop = false;
459457

@@ -593,8 +591,8 @@ static EBPF_INLINE int unwind_native(struct pt_regs *ctx)
593591
Trace *trace = &record->trace;
594592
int unwinder;
595593
ErrorCode error;
596-
#pragma unroll
597-
for (int i = 0; i < NATIVE_FRAMES_PER_PROGRAM; i++) {
594+
UNROLL for (int i = 0; i < NATIVE_FRAMES_PER_PROGRAM; i++)
595+
{
598596
unwinder = PROG_UNWIND_STOP;
599597

600598
// Unwind native code
@@ -620,7 +618,7 @@ static EBPF_INLINE int unwind_native(struct pt_regs *ctx)
620618

621619
// Unwind the native frame using stack deltas. Stop if no next frame.
622620
bool stop;
623-
error = unwind_one_frame(trace->pid, frame_idx, &record->state, &stop);
621+
error = unwind_one_frame(&record->state, &stop);
624622
if (error || stop) {
625623
break;
626624
}

support/ebpf/off_cpu.ebpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bpf_map_def SEC("maps") sched_times = {
2020

2121
// tracepoint__sched_switch serves as entry point for off cpu profiling.
2222
SEC("tracepoint/sched/sched_switch")
23-
int tracepoint__sched_switch(void *ctx)
23+
int tracepoint__sched_switch(UNUSED void *ctx)
2424
{
2525
u64 pid_tgid = bpf_get_current_pid_tgid();
2626
u32 pid = pid_tgid >> 32;

0 commit comments

Comments
 (0)