|
| 1 | +#include <vmlinux.h> |
| 2 | +#include <bpf/bpf_tracing.h> |
| 3 | +#include "tracing.bpf.h" |
| 4 | + |
| 5 | +// On Zen v4 this is the depth. |
| 6 | +#define LBR_DEPTH 16 |
| 7 | + |
| 8 | +struct failure_span_t { |
| 9 | + struct span_base_t span_base; |
| 10 | + struct perf_branch_entry entries[LBR_DEPTH]; |
| 11 | + u32 errno; |
| 12 | +}; |
| 13 | + |
| 14 | +struct { |
| 15 | + __uint(type, BPF_MAP_TYPE_RINGBUF); |
| 16 | + __uint(max_entries, 256 * 1024); |
| 17 | +} failure_spans SEC(".maps"); |
| 18 | + |
| 19 | +SEC("fexit/do_sys_openat2") |
| 20 | +int BPF_PROG(do_sys_openat2, int dfd, const char *filename, struct open_how *how, int retval) |
| 21 | +{ |
| 22 | + struct failure_span_t span = { 0 }; |
| 23 | + struct failure_span_t *submit; |
| 24 | + u64 ts; |
| 25 | + |
| 26 | + if (retval >= 0) { |
| 27 | + return 0; |
| 28 | + } |
| 29 | + |
| 30 | + s64 snapshot_bytes = bpf_get_branch_snapshot(span.entries, sizeof(span.entries), 0); |
| 31 | + if (snapshot_bytes == 0) { |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + u64 count = snapshot_bytes / sizeof(struct perf_branch_entry); |
| 36 | + for (int i = 0; i < count; i++) { |
| 37 | + if (i >= LBR_DEPTH) { |
| 38 | + break; |
| 39 | + } |
| 40 | + |
| 41 | + bpf_printk(" lbr: %llx -> %llx", span.entries[i].from, span.entries[i].to); |
| 42 | + } |
| 43 | + |
| 44 | + ts = bpf_ktime_get_ns(); |
| 45 | + |
| 46 | + span.span_base.span_id = ts; |
| 47 | + span.span_base.parent.trace_id_lo = ts; |
| 48 | + span.errno = -retval; |
| 49 | + |
| 50 | + submit = bpf_ringbuf_reserve(&failure_spans, sizeof(struct failure_span_t), 0); |
| 51 | + if (!submit) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + *submit = span; |
| 56 | + |
| 57 | + bpf_ringbuf_submit(submit, 0); |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +char LICENSE[] SEC("license") = "GPL"; |
0 commit comments