|
| 1 | +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
| 2 | +/* Copyright (c) 2020 Facebook */ |
| 3 | +#include <argp.h> |
| 4 | +#include <signal.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <time.h> |
| 7 | +#include <sys/resource.h> |
| 8 | +#include <bpf/libbpf.h> |
| 9 | +#include "bootstrap.h" |
| 10 | +#include "bootstrap_legacy.skel.h" |
| 11 | + |
| 12 | +static struct env { |
| 13 | + bool verbose; |
| 14 | + long min_duration_ms; |
| 15 | +} env; |
| 16 | + |
| 17 | +const char *argp_program_version = "bootstrap 0.0"; |
| 18 | +const char *argp_program_bug_address = "<[email protected]>"; |
| 19 | +const char argp_program_doc[] = "BPF bootstrap demo application.\n" |
| 20 | + "\n" |
| 21 | + "It traces process start and exits and shows associated \n" |
| 22 | + "information (filename, process duration, PID and PPID, etc).\n" |
| 23 | + "\n" |
| 24 | + "USAGE: ./bootstrap [-d <min-duration-ms>] [-v]\n"; |
| 25 | + |
| 26 | +static const struct argp_option opts[] = { |
| 27 | + { "verbose", 'v', NULL, 0, "Verbose debug output" }, |
| 28 | + { "duration", 'd', "DURATION-MS", 0, "Minimum process duration (ms) to report" }, |
| 29 | + {}, |
| 30 | +}; |
| 31 | + |
| 32 | +static error_t parse_arg(int key, char *arg, struct argp_state *state) |
| 33 | +{ |
| 34 | + switch (key) { |
| 35 | + case 'v': |
| 36 | + env.verbose = true; |
| 37 | + break; |
| 38 | + case 'd': |
| 39 | + errno = 0; |
| 40 | + env.min_duration_ms = strtol(arg, NULL, 10); |
| 41 | + if (errno || env.min_duration_ms <= 0) { |
| 42 | + fprintf(stderr, "Invalid duration: %s\n", arg); |
| 43 | + argp_usage(state); |
| 44 | + } |
| 45 | + break; |
| 46 | + case ARGP_KEY_ARG: |
| 47 | + argp_usage(state); |
| 48 | + break; |
| 49 | + default: |
| 50 | + return ARGP_ERR_UNKNOWN; |
| 51 | + } |
| 52 | + return 0; |
| 53 | +} |
| 54 | + |
| 55 | +static const struct argp argp = { |
| 56 | + .options = opts, |
| 57 | + .parser = parse_arg, |
| 58 | + .doc = argp_program_doc, |
| 59 | +}; |
| 60 | + |
| 61 | +static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) |
| 62 | +{ |
| 63 | + if (level == LIBBPF_DEBUG && !env.verbose) |
| 64 | + return 0; |
| 65 | + return vfprintf(stderr, format, args); |
| 66 | +} |
| 67 | + |
| 68 | +static volatile bool exiting = false; |
| 69 | + |
| 70 | +static void sig_handler(int sig) |
| 71 | +{ |
| 72 | + exiting = true; |
| 73 | +} |
| 74 | + |
| 75 | +static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz) |
| 76 | +{ |
| 77 | + const struct event *e = data; |
| 78 | + struct tm *tm; |
| 79 | + char ts[32]; |
| 80 | + time_t t; |
| 81 | + |
| 82 | + time(&t); |
| 83 | + tm = localtime(&t); |
| 84 | + strftime(ts, sizeof(ts), "%H:%M:%S", tm); |
| 85 | + |
| 86 | + if (e->exit_event) { |
| 87 | + printf("%-8s %-5s %-16s %-7d %-7d [%u]", ts, "EXIT", e->comm, e->pid, e->ppid, |
| 88 | + e->exit_code); |
| 89 | + if (e->duration_ns) |
| 90 | + printf(" (%llums)", e->duration_ns / 1000000); |
| 91 | + printf("\n"); |
| 92 | + } else { |
| 93 | + printf("%-8s %-5s %-16s %-7d %-7d %s\n", ts, "EXEC", e->comm, e->pid, e->ppid, |
| 94 | + e->filename); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +int main(int argc, char **argv) |
| 99 | +{ |
| 100 | + // struct ring_buffer *rb = NULL; |
| 101 | + struct perf_buffer *pe = NULL; |
| 102 | + struct bootstrap_legacy_bpf *skel; |
| 103 | + int err; |
| 104 | + |
| 105 | + /* Parse command line arguments */ |
| 106 | + err = argp_parse(&argp, argc, argv, 0, NULL, NULL); |
| 107 | + if (err) |
| 108 | + return err; |
| 109 | + |
| 110 | + /* Set up libbpf errors and debug info callback */ |
| 111 | + libbpf_set_print(libbpf_print_fn); |
| 112 | + |
| 113 | + /* Cleaner handling of Ctrl-C */ |
| 114 | + signal(SIGINT, sig_handler); |
| 115 | + signal(SIGTERM, sig_handler); |
| 116 | + |
| 117 | + /* Load and verify BPF application */ |
| 118 | + skel = bootstrap_legacy_bpf__open(); |
| 119 | + if (!skel) { |
| 120 | + fprintf(stderr, "Failed to open and load BPF skeleton\n"); |
| 121 | + return 1; |
| 122 | + } |
| 123 | + |
| 124 | + /* Parameterize BPF code with minimum duration parameter */ |
| 125 | + skel->rodata->min_duration_ns = env.min_duration_ms * 1000000ULL; |
| 126 | + |
| 127 | + /* Load & verify BPF programs */ |
| 128 | + err = bootstrap_legacy_bpf__load(skel); |
| 129 | + if (err) { |
| 130 | + fprintf(stderr, "Failed to load and verify BPF skeleton\n"); |
| 131 | + goto cleanup; |
| 132 | + } |
| 133 | + |
| 134 | + /* Attach tracepoints */ |
| 135 | + err = bootstrap_legacy_bpf__attach(skel); |
| 136 | + if (err) { |
| 137 | + fprintf(stderr, "Failed to attach BPF skeleton\n"); |
| 138 | + goto cleanup; |
| 139 | + } |
| 140 | + |
| 141 | + /* Set up perf buffer polling */ |
| 142 | + pe = perf_buffer__new(bpf_map__fd(skel->maps.pe), 8, handle_event, NULL, NULL, NULL); |
| 143 | + if (!pe) { |
| 144 | + err = -1; |
| 145 | + fprintf(stderr, "Failed to create perf event buffer\n"); |
| 146 | + goto cleanup; |
| 147 | + } |
| 148 | + |
| 149 | + /* Process events */ |
| 150 | + printf("%-8s %-5s %-16s %-7s %-7s %s\n", "TIME", "EVENT", "COMM", "PID", "PPID", |
| 151 | + "FILENAME/EXIT CODE"); |
| 152 | + while (!exiting) { |
| 153 | + err = perf_buffer__poll(pe, 100 /* timeout, ms */); |
| 154 | + /* Ctrl-C will cause -EINTR */ |
| 155 | + if (err == -EINTR) { |
| 156 | + err = 0; |
| 157 | + break; |
| 158 | + } |
| 159 | + if (err < 0) { |
| 160 | + printf("Error polling perf buffer: %d\n", err); |
| 161 | + break; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | +cleanup: |
| 166 | + /* Clean up */ |
| 167 | + perf_buffer__free(pe); |
| 168 | + bootstrap_legacy_bpf__destroy(skel); |
| 169 | + |
| 170 | + return err < 0 ? -err : 0; |
| 171 | +} |
0 commit comments