Home | History | Annotate | Download | only in lua
      1 #!/usr/bin/env bcc-lua
      2 --[[
      3 Copyright 2016 Marek Vavrusa <mvavrusa@cloudflare.com>
      4 
      5 Licensed under the Apache License, Version 2.0 (the "License");
      6 you may not use this file except in compliance with the License.
      7 You may obtain a copy of the License at
      8 
      9 http://www.apache.org/licenses/LICENSE-2.0
     10 
     11 Unless required by applicable law or agreed to in writing, software
     12 distributed under the License is distributed on an "AS IS" BASIS,
     13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 See the License for the specific language governing permissions and
     15 limitations 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
     20 local ffi = require('ffi')
     21 local bpf = require('bpf')
     22 local S = require('syscall')
     23 -- Kernel-space part of the program
     24 local 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
     28 end, true, -1, 0)
     29 -- User-space part of the program
     30 local 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
     37 end)
     38