|
| 1 | +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ |
| 2 | +/* Copyright (c) 2021 Facebook */ |
| 3 | +#include <argp.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <errno.h> |
| 6 | +#include <signal.h> |
| 7 | +#include <time.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <ctype.h> |
| 10 | + |
| 11 | +#include <bpf/libbpf.h> |
| 12 | +#include <bpf/bpf.h> |
| 13 | +#include "bashreadline.h" |
| 14 | +#include "bashreadline.skel.h" |
| 15 | +#include "trace_helpers.h" |
| 16 | +#include "uprobe_helpers.h" |
| 17 | + |
| 18 | +#define PERF_BUFFER_PAGES 16 |
| 19 | +#define PERF_POLL_TIMEOUT_MS 100 |
| 20 | +#define warn(...) fprintf(stderr, __VA_ARGS__) |
| 21 | + |
| 22 | +static volatile sig_atomic_t exiting = 0; |
| 23 | + |
| 24 | +const char *argp_program_version = "bashreadline 1.0"; |
| 25 | +const char *argp_program_bug_address = |
| 26 | + "https://github.com/iovisor/bcc/tree/master/libbpf-tools"; |
| 27 | +const char argp_program_doc[] = |
| 28 | +"Print entered bash commands from all running shells.\n" |
| 29 | +"\n" |
| 30 | +"USAGE: bashreadline [-s <path/to/libreadline.so>]\n" |
| 31 | +"\n" |
| 32 | +"EXAMPLES:\n" |
| 33 | +" bashreadline\n" |
| 34 | +" bashreadline -s /usr/lib/libreadline.so\n"; |
| 35 | + |
| 36 | +static const struct argp_option opts[] = { |
| 37 | + { "shared", 's', "PATH", 0, "the location of libreadline.so library" }, |
| 38 | + { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, |
| 39 | + {}, |
| 40 | +}; |
| 41 | + |
| 42 | +static char *libreadline_path = NULL; |
| 43 | + |
| 44 | +static error_t parse_arg(int key, char *arg, struct argp_state *state) |
| 45 | +{ |
| 46 | + switch (key) { |
| 47 | + case 's': |
| 48 | + libreadline_path = strdup(arg); |
| 49 | + if (libreadline_path == NULL) |
| 50 | + return ARGP_ERR_UNKNOWN; |
| 51 | + break; |
| 52 | + case 'h': |
| 53 | + argp_state_help(state, stderr, ARGP_HELP_STD_HELP); |
| 54 | + break; |
| 55 | + default: |
| 56 | + return ARGP_ERR_UNKNOWN; |
| 57 | + } |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static void handle_event(void *ctx, int cpu, void *data, __u32 data_size) |
| 62 | +{ |
| 63 | + struct str_t *e = data; |
| 64 | + struct tm *tm; |
| 65 | + char ts[16]; |
| 66 | + time_t t; |
| 67 | + |
| 68 | + time(&t); |
| 69 | + tm = localtime(&t); |
| 70 | + strftime(ts, sizeof(ts), "%H:%m:%S", tm); |
| 71 | + |
| 72 | + printf("%-9s %-7d %s\n", ts, e->pid, e->str); |
| 73 | +} |
| 74 | + |
| 75 | +static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt) |
| 76 | +{ |
| 77 | + warn("lost %llu events on CPU #%d\n", lost_cnt, cpu); |
| 78 | +} |
| 79 | + |
| 80 | +static char *find_readline_so() |
| 81 | +{ |
| 82 | + const char *bash_path = "/bin/bash"; |
| 83 | + FILE *fp; |
| 84 | + off_t func_off; |
| 85 | + char *line = NULL; |
| 86 | + size_t line_sz = 0; |
| 87 | + char path[128]; |
| 88 | + char *result = NULL; |
| 89 | + |
| 90 | + func_off = get_elf_func_offset(bash_path, "readline"); |
| 91 | + if (func_off >= 0) |
| 92 | + return strdup(bash_path); |
| 93 | + |
| 94 | + /* |
| 95 | + * Try to find libreadline.so if readline is not defined in |
| 96 | + * bash itself. |
| 97 | + * |
| 98 | + * ldd will print a list of names of shared objects, |
| 99 | + * dependencies, and their paths. The line for libreadline |
| 100 | + * would looks like |
| 101 | + * |
| 102 | + * libreadline.so.8 => /usr/lib/libreadline.so.8 (0x00007b....) |
| 103 | + * |
| 104 | + * Here, it finds a line with libreadline.so and extracts the |
| 105 | + * path after the arrow, '=>', symbol. |
| 106 | + */ |
| 107 | + fp = popen("ldd /bin/bash", "r"); |
| 108 | + if (fp == NULL) |
| 109 | + goto cleanup; |
| 110 | + while (getline(&line, &line_sz, fp) >= 0) { |
| 111 | + if (sscanf(line, "%*s => %127s", path) < 1) |
| 112 | + continue; |
| 113 | + if (strstr(line, "/libreadline.so")) { |
| 114 | + result = strdup(path); |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +cleanup: |
| 120 | + if (line) |
| 121 | + free(line); |
| 122 | + if (fp) |
| 123 | + fclose(fp); |
| 124 | + return result; |
| 125 | +} |
| 126 | + |
| 127 | +static void sig_int(int signo) |
| 128 | +{ |
| 129 | + exiting = 1; |
| 130 | +} |
| 131 | + |
| 132 | +int main(int argc, char **argv) |
| 133 | +{ |
| 134 | + static const struct argp argp = { |
| 135 | + .options = opts, |
| 136 | + .parser = parse_arg, |
| 137 | + .doc = argp_program_doc, |
| 138 | + }; |
| 139 | + struct bashreadline_bpf *obj = NULL; |
| 140 | + struct perf_buffer_opts pb_opts; |
| 141 | + struct perf_buffer *pb = NULL; |
| 142 | + char *readline_so_path; |
| 143 | + off_t func_off; |
| 144 | + int err; |
| 145 | + |
| 146 | + err = argp_parse(&argp, argc, argv, 0, NULL, NULL); |
| 147 | + if (err) |
| 148 | + return err; |
| 149 | + |
| 150 | + if (libreadline_path) { |
| 151 | + readline_so_path = libreadline_path; |
| 152 | + } else if ((readline_so_path = find_readline_so()) == NULL) { |
| 153 | + warn("failed to find readline\n"); |
| 154 | + return 1; |
| 155 | + } |
| 156 | + |
| 157 | + err = bump_memlock_rlimit(); |
| 158 | + if (err) { |
| 159 | + warn("failed to increase rlimit: %d\n", err); |
| 160 | + goto cleanup; |
| 161 | + } |
| 162 | + |
| 163 | + obj = bashreadline_bpf__open_and_load(); |
| 164 | + if (!obj) { |
| 165 | + warn("failed to open and load BPF object\n"); |
| 166 | + goto cleanup; |
| 167 | + } |
| 168 | + |
| 169 | + func_off = get_elf_func_offset(readline_so_path, "readline"); |
| 170 | + if (func_off < 0) { |
| 171 | + warn("cound not find readline in %s\n", readline_so_path); |
| 172 | + goto cleanup; |
| 173 | + } |
| 174 | + |
| 175 | + obj->links.printret = bpf_program__attach_uprobe(obj->progs.printret, true, -1, |
| 176 | + readline_so_path, func_off); |
| 177 | + err = libbpf_get_error(obj->links.printret); |
| 178 | + if (err) { |
| 179 | + warn("failed to attach readline: %d\n", err); |
| 180 | + goto cleanup; |
| 181 | + } |
| 182 | + |
| 183 | + pb_opts.sample_cb = handle_event; |
| 184 | + pb_opts.lost_cb = handle_lost_events; |
| 185 | + pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &pb_opts); |
| 186 | + err = libbpf_get_error(pb); |
| 187 | + if (err) { |
| 188 | + warn("failed to open perf buffer: %d\n", err); |
| 189 | + goto cleanup; |
| 190 | + } |
| 191 | + |
| 192 | + if (signal(SIGINT, sig_int) == SIG_ERR) { |
| 193 | + warn("can't set signal handler: %s\n", strerror(errno)); |
| 194 | + err = 1; |
| 195 | + goto cleanup; |
| 196 | + } |
| 197 | + |
| 198 | + printf("%-9s %-7s %s\n", "TIME", "PID", "COMMAND"); |
| 199 | + while (!exiting) { |
| 200 | + err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS); |
| 201 | + if (err < 0 && errno != EINTR) { |
| 202 | + warn("error polling perf buffer: %s\n", strerror(errno)); |
| 203 | + goto cleanup; |
| 204 | + } |
| 205 | + err = 0; |
| 206 | + } |
| 207 | + |
| 208 | +cleanup: |
| 209 | + if (readline_so_path) |
| 210 | + free(readline_so_path); |
| 211 | + perf_buffer__free(pb); |
| 212 | + bashreadline_bpf__destroy(obj); |
| 213 | + |
| 214 | + return err != 0; |
| 215 | +} |
0 commit comments