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