blob: 1c299617e1d7569fde1724ab7e47438ce81ced02 [file] [log] [blame]
Brendan Gregg6049d3f2016-10-16 12:33:50 -07001Demonstrations of ttysnoop, the Linux eBPF/bcc version.
2
3
4ttysnoop watches a tty or pts device, and prints the same output that is
5appearing on that device. It can be used to mirror the output from a shell
6session, or the system console.
7
8Let's snoop /dev/pts/2:
9
10# ./ttysnoop 2
11<screen clears>
12date
13Sun Oct 16 01:28:47 UTC 2016
14# uname -a
15Linux bgregg-xenial-bpf-i-xxx 4.8.0-rc4-virtual #1 SMP Wed Aug 31 22:54:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
16# df -h
17Filesystem Size Used Avail Use% Mounted on
18udev 7.4G 0 7.4G 0% /dev
19tmpfs 1.5G 89M 1.4G 6% /run
20/dev/xvda1 7.8G 4.5G 3.3G 59% /
21tmpfs 7.4G 0 7.4G 0% /dev/shm
22tmpfs 5.0M 0 5.0M 0% /run/lock
23tmpfs 7.4G 0 7.4G 0% /sys/fs/cgroup
24tmpfs 250M 0 250M 0% /run/shm
25/dev/md0 160G 20G 141G 13% /mnt
26tmpfs 1.5G 0 1.5G 0% /run/user/0
27# ^C
28
29What we're seeing is another shell session. The first line was "date" without
30the shell prompt ("#") because we began tracing after the prompt was printed.
31The other commands appeared, keystroke by keystroke, as the user was typing
32them. Spooky!
33
34Remember to Ctrl-C to exit ttysnoop.
35
36
37To figure out which pts device number to use, you can check your own with "ps"
38and other's with "w". For example:
39
40# ps -p $$
41 PID TTY TIME CMD
42 9605 pts/1 00:00:00 bash
43# w
44 01:26:37 up 9 days, 35 min, 2 users, load average: 0.22, 0.22, 0.15
45USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
46root pts/1 100.127.65.241 00:39 2.00s 0.33s 0.33s -bash
47root pts/2 100.127.65.241 00:40 16.00s 1.06s 1.06s -bash
48
49So I'm pts/1, and there's another session that's pts/2.
50
51
52This can also snoop tty devices using their full path. Eg, snooping the system
53console:
54
55# ./ttysnoop /dev/console
56Oct 16 01:32:06 bgregg-xenial-bpf-i-xxx kernel: [780087.407428] bash (9888): drop_caches: 1
57Oct 16 01:32:38 bgregg-xenial-bpf-i-xxx snmpd[2708]: Cannot statfs /sys/kernel/debug/tracing: Permission denied
58Oct 16 01:33:32 bgregg-xenial-bpf-i-xxx snmpd[2708]: Cannot statfs /sys/kernel/debug/tracing: Permission denied
59Oct 16 01:34:26 bgregg-xenial-bpf-i-xxx snmpd[2708]: Cannot statfs /sys/kernel/debug/tracing: Permission denied
60^C
61
62Neat!
63
64
65USAGE:
66
67# ./ttysnoop.py -h
68usage: ttysnoop.py [-h] [-C] device
69
70Snoop output from a pts or tty device, eg, a shell
71
72positional arguments:
73 device path to a tty device (eg, /dev/tty0) or pts number
74
75optional arguments:
76 -h, --help show this help message and exit
77 -C, --noclear don't clear the screen
78
79examples:
80 ./ttysnoop /dev/pts/2 # snoop output from /dev/pts/2
81 ./ttysnoop 2 # snoop output from /dev/pts/2 (shortcut)
82 ./ttysnoop /dev/console # snoop output from the system console
83 ./ttysnoop /dev/tty0 # snoop output from /dev/tty0