|
| 1 | +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
| 2 | +/* Copyright (c) 2023 Meta */ |
| 3 | +#include <argp.h> |
| 4 | +#include <signal.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <sys/resource.h> |
| 7 | +#include <bpf/bpf.h> |
| 8 | +#include <bpf/libbpf.h> |
| 9 | +#include <unistd.h> |
| 10 | +#include "task_iter.h" |
| 11 | +#include "task_iter.skel.h" |
| 12 | + |
| 13 | +static struct env { |
| 14 | + bool verbose; |
| 15 | +} env; |
| 16 | + |
| 17 | +static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) |
| 18 | +{ |
| 19 | + if (level == LIBBPF_DEBUG && !env.verbose) |
| 20 | + return 0; |
| 21 | + return vfprintf(stderr, format, args); |
| 22 | +} |
| 23 | + |
| 24 | +static volatile bool exiting = false; |
| 25 | + |
| 26 | +static void sig_handler(int sig) |
| 27 | +{ |
| 28 | + exiting = true; |
| 29 | +} |
| 30 | + |
| 31 | +static const char *get_task_state(__u32 state) |
| 32 | +{ |
| 33 | + /* Taken from: |
| 34 | + * https://elixir.bootlin.com/linux/latest/source/include/linux/sched.h#L85 |
| 35 | + * There are a lot more states not covered here but these are common ones. |
| 36 | + */ |
| 37 | + switch (state) { |
| 38 | + case 0x0000: return "RUNNING"; |
| 39 | + case 0x0001: return "INTERRUPTIBLE"; |
| 40 | + case 0x0002: return "UNINTERRUPTIBLE"; |
| 41 | + case 0x0200: return "WAKING"; |
| 42 | + case 0x0400: return "NOLOAD"; |
| 43 | + case 0x0402: return "IDLE"; |
| 44 | + case 0x0800: return "NEW"; |
| 45 | + default: return "<unknown>"; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +int main(int argc, char **argv) |
| 50 | +{ |
| 51 | + struct task_iter_bpf *skel; |
| 52 | + struct task_info buf; |
| 53 | + int iter_fd; |
| 54 | + ssize_t ret; |
| 55 | + int err; |
| 56 | + |
| 57 | + /* Set up libbpf errors and debug info callback */ |
| 58 | + libbpf_set_print(libbpf_print_fn); |
| 59 | + |
| 60 | + /* Cleaner handling of Ctrl-C */ |
| 61 | + signal(SIGINT, sig_handler); |
| 62 | + signal(SIGTERM, sig_handler); |
| 63 | + |
| 64 | + /* Open, load, and verify BPF application */ |
| 65 | + skel = task_iter_bpf__open_and_load(); |
| 66 | + if (!skel) { |
| 67 | + fprintf(stderr, "Failed to open and load BPF skeleton\n"); |
| 68 | + goto cleanup; |
| 69 | + } |
| 70 | + |
| 71 | + /* Attach tracepoints */ |
| 72 | + err = task_iter_bpf__attach(skel); |
| 73 | + if (err) { |
| 74 | + fprintf(stderr, "Failed to attach BPF skeleton\n"); |
| 75 | + goto cleanup; |
| 76 | + } |
| 77 | + |
| 78 | + iter_fd = bpf_iter_create(bpf_link__fd(skel->links.get_tasks)); |
| 79 | + if (iter_fd < 0) { |
| 80 | + err = -1; |
| 81 | + fprintf(stderr, "Failed to create iter\n"); |
| 82 | + goto cleanup; |
| 83 | + } |
| 84 | + |
| 85 | + while (true) { |
| 86 | + ret = read(iter_fd, &buf, sizeof(struct task_info)); |
| 87 | + if (ret < 0) { |
| 88 | + if (errno == EAGAIN) |
| 89 | + continue; |
| 90 | + err = -errno; |
| 91 | + break; |
| 92 | + } |
| 93 | + if (ret == 0) |
| 94 | + break; |
| 95 | + if (buf.kstack_len <= 0) { |
| 96 | + printf("Error getting kernel stack for task. Task Info. Pid: %d. Process Name: %s. Kernel Stack Error: %d. State: %s\n", |
| 97 | + buf.pid, buf.comm, buf.kstack_len, get_task_state(buf.state)); |
| 98 | + } else { |
| 99 | + printf("Task Info. Pid: %d. Process Name: %s. Kernel Stack Len: %d. State: %s\n", |
| 100 | + buf.pid, buf.comm, buf.kstack_len, get_task_state(buf.state)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +cleanup: |
| 105 | + /* Clean up */ |
| 106 | + close(iter_fd); |
| 107 | + task_iter_bpf__destroy(skel); |
| 108 | + |
| 109 | + return err < 0 ? -err : 0; |
| 110 | +} |
0 commit comments