Skip to content

Commit aa87997

Browse files
committed
bashreadline
1 parent 0030d34 commit aa87997

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Examples:
6565

6666
Tools:
6767

68+
- tools/[bashreadline](tools/bashreadline.py): Print entered bash commands system wide. [Examples](tools/bashreadline_example.txt).
6869
- tools/[biolatency](tools/biolatency.py): Summarize block device I/O latency as a histogram. [Examples](tools/biolatency_example.txt).
6970
- tools/[biosnoop](tools/biosnoop.py): Trace block device I/O with PID and latency. [Examples](tools/biosnoop_example.txt).
7071
- tools/[funccount](tools/funccount.py): Count kernel function calls. [Examples](tools/funccount_example.txt).

man/man8/bashreadline.8

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.TH bashreadline 8 "2016-01-28" "USER COMMANDS"
2+
.SH NAME
3+
bashreadline \- Print entered bash commands system wide. Uses Linux eBPF/bcc.
4+
.SH SYNOPSIS
5+
.B bashreadline
6+
.SH DESCRIPTION
7+
bashreadline traces the return of the readline() function using uprobes, to
8+
show the bash commands that were entered interactively, system wide. The
9+
entered command may fail: this is just showing what was entered.
10+
11+
This program is also a basic example of eBPF/bcc and uprobes.
12+
13+
Since this uses BPF, only the root user can use this tool.
14+
.SH REQUIREMENTS
15+
CONFIG_BPF and bcc.
16+
.SH EXAMPLES
17+
.TP
18+
Trace bash commands system wide:
19+
#
20+
.B bashreadline
21+
.SH FIELDS
22+
.TP
23+
TIME
24+
Time of the command (HH:MM:SS).
25+
.TP
26+
PID
27+
Process ID of the bash shell.
28+
.TP
29+
COMMAND
30+
Entered command.
31+
.SH OVERHEAD
32+
As the rate of interactive bash commands is expected to be very low (<<100/s),
33+
the overhead of this program is expected to be negligible.
34+
.SH SOURCE
35+
This is from bcc.
36+
.IP
37+
https://github.com/iovisor/bcc
38+
.PP
39+
Also look in the bcc distribution for a companion _examples.txt file containing
40+
example usage, output, and commentary for this tool.
41+
.SH OS
42+
Linux
43+
.SH STABILITY
44+
Unstable - in development.
45+
.SH AUTHOR
46+
Brendan Gregg
47+
.SH SEE ALSO
48+
opensnoop(8)

tools/bashreadline.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
#
3+
# bashreadline Print entered bash commands from all running shells.
4+
# For Linux, uses BCC, eBPF. Embedded C.
5+
#
6+
# This works by tracing the readline() function using a uretprobe (uprobes).
7+
#
8+
# Copyright 2016 Netflix, Inc.
9+
# Licensed under the Apache License, Version 2.0 (the "License")
10+
#
11+
# 28-Jan-2016 Brendan Gregg Created this.
12+
13+
from __future__ import print_function
14+
from bcc import BPF
15+
from time import strftime
16+
17+
# load BPF program
18+
bpf_text = """
19+
#include <uapi/linux/ptrace.h>
20+
int printret(struct pt_regs *ctx) {
21+
if (!ctx->ax)
22+
return 0;
23+
24+
char str[80] = {};
25+
bpf_probe_read(&str, sizeof(str), (void *)ctx->ax);
26+
bpf_trace_printk("%s\\n", &str);
27+
28+
return 0;
29+
};
30+
"""
31+
b = BPF(text=bpf_text)
32+
b.attach_uretprobe(name="/bin/bash", sym="readline", fn_name="printret")
33+
34+
# header
35+
print("%-9s %-6s %s" % ("TIME", "PID", "COMMAND"))
36+
37+
# format output
38+
while 1:
39+
try:
40+
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
41+
except ValueError:
42+
continue
43+
print("%-9s %-6d %s" % (strftime("%H:%M:%S"), pid, msg))

tools/bashreadline_example.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Demonstrations of bashreadline, the Linux eBPF/bcc version.
2+
3+
4+
This prints bash commands from all running bash shells on the system. For
5+
example:
6+
7+
# ./bashreadline
8+
TIME PID COMMAND
9+
05:28:25 21176 ls -l
10+
05:28:28 21176 date
11+
05:28:35 21176 echo hello world
12+
05:28:43 21176 foo this command failed
13+
05:28:45 21176 df -h
14+
05:29:04 3059 echo another shell
15+
05:29:13 21176 echo first shell again
16+
17+
The entered command may fail. This is just showing what command lines were
18+
entered interactively for bash to process.
19+
20+
It works by tracing the return of the readline() function using uprobes
21+
(specifically a uretprobe).

0 commit comments

Comments
 (0)