1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include "bpf_iter.h"
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 
7 char _license[] SEC("license") = "GPL";
8 
9 #define MAX_STACK_TRACE_DEPTH   64
10 unsigned long entries[MAX_STACK_TRACE_DEPTH] = {};
11 #define SIZE_OF_ULONG (sizeof(unsigned long))
12 
13 SEC("iter/task")
dump_task_stack(struct bpf_iter__task * ctx)14 int dump_task_stack(struct bpf_iter__task *ctx)
15 {
16 	struct seq_file *seq = ctx->meta->seq;
17 	struct task_struct *task = ctx->task;
18 	long i, retlen;
19 
20 	if (task == (void *)0)
21 		return 0;
22 
23 	retlen = bpf_get_task_stack(task, entries,
24 				    MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, 0);
25 	if (retlen < 0)
26 		return 0;
27 
28 	BPF_SEQ_PRINTF(seq, "pid: %8u num_entries: %8u\n", task->pid,
29 		       retlen / SIZE_OF_ULONG);
30 	for (i = 0; i < MAX_STACK_TRACE_DEPTH; i++) {
31 		if (retlen > i * SIZE_OF_ULONG)
32 			BPF_SEQ_PRINTF(seq, "[<0>] %pB\n", (void *)entries[i]);
33 	}
34 	BPF_SEQ_PRINTF(seq, "\n");
35 
36 	return 0;
37 }
38 
39 SEC("iter/task")
get_task_user_stacks(struct bpf_iter__task * ctx)40 int get_task_user_stacks(struct bpf_iter__task *ctx)
41 {
42 	struct seq_file *seq = ctx->meta->seq;
43 	struct task_struct *task = ctx->task;
44 	uint64_t buf_sz = 0;
45 	int64_t res;
46 
47 	if (task == (void *)0)
48 		return 0;
49 
50 	res = bpf_get_task_stack(task, entries,
51 			MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, BPF_F_USER_STACK);
52 	if (res <= 0)
53 		return 0;
54 
55 	buf_sz += res;
56 
57 	/* If the verifier doesn't refine bpf_get_task_stack res, and instead
58 	 * assumes res is entirely unknown, this program will fail to load as
59 	 * the verifier will believe that max buf_sz value allows reading
60 	 * past the end of entries in bpf_seq_write call
61 	 */
62 	bpf_seq_write(seq, &entries, buf_sz);
63 	return 0;
64 }
65