Skip to content

Instantly share code, notes, and snippets.

@brendangregg
Created February 5, 2016 05:01
Show Gist options
  • Save brendangregg/3ddc2e92dcaa565f7719 to your computer and use it in GitHub Desktop.
Save brendangregg/3ddc2e92dcaa565f7719 to your computer and use it in GitHub Desktop.
bashreadline
#!/usr/bin/python
# [...]
from __future__ import print_function
from bcc import BPF
from time import strftime
# load BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
int printret(struct pt_regs *ctx) {
if (!ctx->ax)
return 0;
char str[80] = {};
bpf_probe_read(&str, sizeof(str), (void *)ctx->ax);
bpf_trace_printk("%s\\n", &str);
return 0;
};
"""
b = BPF(text=bpf_text)
b.attach_uretprobe(name="/bin/bash", sym="readline", fn_name="printret")
# header
print("%-9s %-6s %s" % ("TIME", "PID", "COMMAND"))
# format output
while 1:
try:
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
except ValueError:
continue
print("%-9s %-6d %s" % (strftime("%H:%M:%S"), pid, msg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment