1#!/usr/bin/env bcc-lua 2--[[ 3Copyright 2016 Marek Vavrusa <mvavrusa@cloudflare.com> 4 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16]] 17-- Trace readline() call from all bash instances (print bash commands from all running shells). 18-- This is rough equivallent to `bashreadline` 19-- Source: http://www.brendangregg.com/blog/2016-02-08/linux-ebpf-bcc-uprobes.html 20local ffi = require('ffi') 21local bpf = require('bpf') 22local S = require('syscall') 23-- Kernel-space part of the program 24local probe = bpf.uprobe('/bin/bash:readline', function (ptregs) 25 local line = ffi.new('char [40]') -- Create a 40 byte buffer on stack 26 ffi.copy(line, ffi.cast('char *', ptregs.ax)) -- Cast `ax` to string pointer and copy to buffer 27 print('%s\n', line) -- Print to trace_pipe 28end, true, -1, 0) 29-- User-space part of the program 30local ok, err = pcall(function() 31 local log = bpf.tracelog() 32 print(' TASK-PID CPU# TIMESTAMP FUNCTION') 33 print(' | | | | |') 34 while true do 35 print(log:read()) 36 end 37end) 38