Skip to content

Commit

Permalink
bpf.lua: Add support for CFLAGS and LLVM debug flags
Browse files Browse the repository at this point in the history
  • Loading branch information
vmg committed Mar 30, 2016
1 parent a978401 commit e789360
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/lua/bashreadline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
local ffi = require("ffi")

return function(BPF)
local b = BPF:new{src_file="bashreadline.c", debug=true}
local b = BPF:new{src_file="bashreadline.c", debug=0}
b:attach_uprobe{name="/bin/bash", sym="readline", fn_name="printret", retprobe=true}

local function print_readline(cpu, event)
Expand Down
2 changes: 1 addition & 1 deletion examples/lua/memleak.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ return function(BPF, utils)
text = text:gsub("SHOULD_PRINT", args.trace and "1" or "0")
text = text:gsub("SAMPLE_EVERY_N", tostring(args.sample_rate))

local bpf = BPF:new{text=text, debug=true}
local bpf = BPF:new{text=text, debug=0}
local syms = nil
local min_age_ns = args.older * 1e6

Expand Down
2 changes: 1 addition & 1 deletion examples/lua/strlen_count.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int printarg(struct pt_regs *ctx) {
]], "PID", arg[1])

return function(BPF)
local b = BPF:new{text=program, debug=true}
local b = BPF:new{text=program, debug=0}
b:attach_uprobe{name="c", sym="strlen", fn_name="printarg"}

local pipe = b:pipe()
Expand Down
2 changes: 1 addition & 1 deletion examples/lua/task_switch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int count_sched(struct pt_regs *ctx, struct task_struct *prev) {
]]

return function(BPF)
local b = BPF:new{text=program, debug=true}
local b = BPF:new{text=program, debug=0}
b:attach_kprobe{event="finish_task_switch", fn_name="count_sched"}

print("Press any key...")
Expand Down
17 changes: 14 additions & 3 deletions src/lua/bcc/bpf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Bpf.static.open_uprobes = {}
Bpf.static.process_symbols = {}
Bpf.static.KPROBE_LIMIT = 1000
Bpf.static.tracer_pipe = nil
Bpf.static.DEFAULT_CFLAGS = {
'-D__HAVE_BUILTIN_BSWAP16__',
'-D__HAVE_BUILTIN_BSWAP32__',
'-D__HAVE_BUILTIN_BSWAP64__',
}

function Bpf.static.check_probe_quota(n)
local cur = table.count(Bpf.static.open_kprobes) + table.count(Bpf.static.open_uprobes)
Expand Down Expand Up @@ -101,17 +106,23 @@ function Bpf:initialize(args)
self.funcs = {}
self.tables = {}

local cflags = table.join(Bpf.DEFAULT_CFLAGS, args.cflags)
local cflags_ary = ffi.new("const char *[?]", #cflags, cflags)

local llvm_debug = args.debug or 0
assert(type(llvm_debug) == "number")

if args.text then
log.info("\n%s\n", args.text)
self.module = libbcc.bpf_module_create_c_from_string(args.text, args.llvm_debug or 0, nil, 0)
self.module = libbcc.bpf_module_create_c_from_string(args.text, llvm_debug, cflags_ary, #cflags)
elseif args.src_file then
local src = _find_file(Bpf.SCRIPT_ROOT, args.src_file)

if src:ends(".b") then
local hdr = _find_file(Bpf.SCRIPT_ROOT, args.hdr_file)
self.module = libbcc.bpf_module_create_b(src, hdr, args.llvm_debug or 0)
self.module = libbcc.bpf_module_create_b(src, hdr, llvm_debug)
else
self.module = libbcc.bpf_module_create_c(src, args.llvm_debug or 0, nil, 0)
self.module = libbcc.bpf_module_create_c(src, llvm_debug, cflags_ary, #cflags)
end
end

Expand Down
16 changes: 16 additions & 0 deletions src/lua/bcc/vendor/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ function table.bsearch(list, value, mkval)
return nil
end

function table.join(a, b)
assert(a)
if b == nil or #b == 0 then
return a
end

local res = {}
for _, v in ipairs(a) do
table.insert(res, v)
end
for _, v in ipairs(b) do
table.insert(res, v)
end
return res
end

function table.build(iterator_fn, build_fn)
build_fn = (build_fn or function(arg) return arg end)
local res = {}
Expand Down

0 comments on commit e789360

Please sign in to comment.