Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 2 | # |
| 3 | # bashreadline Print entered bash commands from all running shells. |
| 4 | # For Linux, uses BCC, eBPF. Embedded C. |
| 5 | # |
JayceCao | b26e26b | 2019-02-18 14:55:12 +0800 | [diff] [blame] | 6 | # USAGE: bashreadline [-s SHARED] |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 7 | # This works by tracing the readline() function using a uretprobe (uprobes). |
JayceCao | b26e26b | 2019-02-18 14:55:12 +0800 | [diff] [blame] | 8 | # When you failed to run the script directly with error: |
| 9 | # `Exception: could not determine address of symbol b'readline'`, |
| 10 | # you may need specify the location of libreadline.so library |
| 11 | # with `-s` option. |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 12 | # |
| 13 | # Copyright 2016 Netflix, Inc. |
| 14 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 15 | # |
| 16 | # 28-Jan-2016 Brendan Gregg Created this. |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 17 | # 12-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 18 | |
| 19 | from __future__ import print_function |
| 20 | from bcc import BPF |
| 21 | from time import strftime |
JayceCao | b26e26b | 2019-02-18 14:55:12 +0800 | [diff] [blame] | 22 | import argparse |
| 23 | |
| 24 | parser = argparse.ArgumentParser( |
| 25 | description="Print entered bash commands from all running shells", |
| 26 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 27 | parser.add_argument("-s", "--shared", nargs="?", |
| 28 | const="/lib/libreadline.so", type=str, |
| 29 | help="specify the location of libreadline.so library.\ |
| 30 | Default is /lib/libreadline.so") |
| 31 | args = parser.parse_args() |
| 32 | |
| 33 | name = args.shared if args.shared else "/bin/bash" |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 34 | |
| 35 | # load BPF program |
| 36 | bpf_text = """ |
| 37 | #include <uapi/linux/ptrace.h> |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 38 | |
| 39 | struct str_t { |
| 40 | u64 pid; |
| 41 | char str[80]; |
| 42 | }; |
| 43 | |
| 44 | BPF_PERF_OUTPUT(events); |
| 45 | |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 46 | int printret(struct pt_regs *ctx) { |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 47 | struct str_t data = {}; |
| 48 | u32 pid; |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 49 | if (!PT_REGS_RC(ctx)) |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 50 | return 0; |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 51 | pid = bpf_get_current_pid_tgid(); |
| 52 | data.pid = pid; |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 53 | bpf_probe_read(&data.str, sizeof(data.str), (void *)PT_REGS_RC(ctx)); |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 54 | events.perf_submit(ctx,&data,sizeof(data)); |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 55 | |
| 56 | return 0; |
| 57 | }; |
| 58 | """ |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 59 | |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 60 | b = BPF(text=bpf_text) |
JayceCao | b26e26b | 2019-02-18 14:55:12 +0800 | [diff] [blame] | 61 | b.attach_uretprobe(name=name, sym="readline", fn_name="printret") |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 62 | |
| 63 | # header |
| 64 | print("%-9s %-6s %s" % ("TIME", "PID", "COMMAND")) |
| 65 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 66 | def print_event(cpu, data, size): |
Xiaozhou Liu | 51d62d3 | 2019-02-15 13:03:05 +0800 | [diff] [blame] | 67 | event = b["events"].event(data) |
Paul Chaignon | ae0e025 | 2017-10-07 11:52:30 +0200 | [diff] [blame] | 68 | print("%-9s %-6d %s" % (strftime("%H:%M:%S"), event.pid, |
jeromemarchand | b96ebcd | 2018-10-10 01:58:15 +0200 | [diff] [blame] | 69 | event.str.decode('utf-8', 'replace'))) |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 70 | |
| 71 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | aa87997 | 2016-01-28 22:43:37 -0800 | [diff] [blame] | 72 | while 1: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 73 | try: |
| 74 | b.perf_buffer_poll() |
| 75 | except KeyboardInterrupt: |
| 76 | exit() |