|
2 | 2 | /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
|
3 | 3 |
|
4 | 4 | #include <linux/bpf.h>
|
| 5 | +#include <linux/filter.h> |
5 | 6 | #include <linux/bpf_mem_alloc.h>
|
6 | 7 | #include <linux/percpu.h>
|
7 | 8 | #include <linux/refcount.h>
|
@@ -476,3 +477,50 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
|
476 | 477 | llist_add_batch(head, tail, &stream->log);
|
477 | 478 | return 0;
|
478 | 479 | }
|
| 480 | + |
| 481 | +struct dump_stack_ctx { |
| 482 | + struct bpf_stream_stage *ss; |
| 483 | + int err; |
| 484 | +}; |
| 485 | + |
| 486 | +static bool dump_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp) |
| 487 | +{ |
| 488 | + struct dump_stack_ctx *ctxp = cookie; |
| 489 | + const char *file = "", *line = ""; |
| 490 | + struct bpf_prog *prog; |
| 491 | + int num, ret; |
| 492 | + |
| 493 | + rcu_read_lock(); |
| 494 | + prog = bpf_prog_ksym_find(ip); |
| 495 | + rcu_read_unlock(); |
| 496 | + if (prog) { |
| 497 | + ret = bpf_prog_get_file_line(prog, ip, &file, &line, &num); |
| 498 | + if (ret < 0) |
| 499 | + goto end; |
| 500 | + ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n %s @ %s:%d\n", |
| 501 | + (void *)ip, line, file, num); |
| 502 | + return !ctxp->err; |
| 503 | + } |
| 504 | +end: |
| 505 | + ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)ip); |
| 506 | + return !ctxp->err; |
| 507 | +} |
| 508 | + |
| 509 | +int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss) |
| 510 | +{ |
| 511 | + struct dump_stack_ctx ctx = { .ss = ss }; |
| 512 | + int ret; |
| 513 | + |
| 514 | + ret = bpf_stream_stage_printk(ss, "CPU: %d UID: %d PID: %d Comm: %s\n", |
| 515 | + raw_smp_processor_id(), __kuid_val(current_real_cred()->euid), |
| 516 | + current->pid, current->comm); |
| 517 | + if (ret) |
| 518 | + return ret; |
| 519 | + ret = bpf_stream_stage_printk(ss, "Call trace:\n"); |
| 520 | + if (ret) |
| 521 | + return ret; |
| 522 | + arch_bpf_stack_walk(dump_stack_cb, &ctx); |
| 523 | + if (ctx.err) |
| 524 | + return ctx.err; |
| 525 | + return bpf_stream_stage_printk(ss, "\n"); |
| 526 | +} |
0 commit comments