1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
4 #include <bpf/bpf_helpers.h>
6 char _license[] SEC("license") = "GPL";
8 #define MAX_STACK_TRACE_DEPTH 64
9 unsigned long entries[MAX_STACK_TRACE_DEPTH] = {};
10 #define SIZE_OF_ULONG (sizeof(unsigned long))
13 int dump_task_stack(struct bpf_iter__task *ctx)
15 struct seq_file *seq = ctx->meta->seq;
16 struct task_struct *task = ctx->task;
19 if (task == (void *)0)
22 retlen = bpf_get_task_stack(task, entries,
23 MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, 0);
27 BPF_SEQ_PRINTF(seq, "pid: %8u num_entries: %8u\n", task->pid,
28 retlen / SIZE_OF_ULONG);
29 for (i = 0; i < MAX_STACK_TRACE_DEPTH; i++) {
30 if (retlen > i * SIZE_OF_ULONG)
31 BPF_SEQ_PRINTF(seq, "[<0>] %pB\n", (void *)entries[i]);
33 BPF_SEQ_PRINTF(seq, "\n");
39 int get_task_user_stacks(struct bpf_iter__task *ctx)
41 struct seq_file *seq = ctx->meta->seq;
42 struct task_struct *task = ctx->task;
46 if (task == (void *)0)
49 res = bpf_get_task_stack(task, entries,
50 MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, BPF_F_USER_STACK);
56 /* If the verifier doesn't refine bpf_get_task_stack res, and instead
57 * assumes res is entirely unknown, this program will fail to load as
58 * the verifier will believe that max buf_sz value allows reading
59 * past the end of entries in bpf_seq_write call
61 bpf_seq_write(seq, &entries, buf_sz);