Skip to content

Commit

Permalink
lua: Prepare for standalone build
Browse files Browse the repository at this point in the history
  • Loading branch information
vmg committed Apr 1, 2016
1 parent 31571b4 commit 818eac9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 161 deletions.
32 changes: 5 additions & 27 deletions src/lua/bcc-probe
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]
function setup_path()
local str = require("debug").getinfo(2, "S").source:sub(2)
local script_path = str:match("(.*/)").."/?.lua;"
package.path = script_path..package.path
end

setup_path()
local BCC = require("bcc.init")
local BPF = BCC.BPF

BPF.script_root(arg[1])

local utils = {
argparse = require("bcc.vendor.argparse"),
posix = require("bcc.vendor.posix"),
sym = BCC.sym
}

local tracefile = table.remove(arg, 1)
local command = dofile(tracefile)
local res, err = pcall(command, BPF, utils)

if not res then
io.stderr:write("[ERROR] "..err.."\n")
end

BPF.cleanup_probes()
local str = require("debug").getinfo(1, "S").source:sub(2)
local script_path = str:match("(.*/)").."/?.lua;"
package.path = script_path..package.path
rawset(_G, "BCC_STANDALONE", false)
require("bcc.run")()
3 changes: 0 additions & 3 deletions src/lua/bcc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]
require("bcc.vendor.strict")
require("bcc.vendor.helpers")
class = require("bcc.vendor.middleclass")

return {
BPF = require("bcc.bpf"),
sym = require("bcc.sym"),
Expand Down
3 changes: 1 addition & 2 deletions src/lua/bcc/libbcc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,4 @@ int perf_reader_fd(struct perf_reader *reader);
void perf_reader_set_fd(struct perf_reader *reader, int fd);
]]

local libbcc = ffi.load("bcc")
return libbcc
return rawget(_G, "BCC_STANDALONE") and ffi.C or ffi.load("bcc")
39 changes: 39 additions & 0 deletions src/lua/bcc/run.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--[[
Copyright 2016 GitHub, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]

return function()
local BCC = require("bcc.init")
local BPF = BCC.BPF

BPF.script_root(arg[1])

local utils = {
argparse = require("bcc.vendor.argparse"),
posix = require("bcc.vendor.posix"),
sym = BCC.sym
}

local tracefile = table.remove(arg, 1)
local command = dofile(tracefile)
local res, err = pcall(command, BPF, utils)

if not res then
io.stderr:write("[ERROR] "..err.."\n")
end

BPF.cleanup_probes()
return res, err
end
12 changes: 11 additions & 1 deletion src/lua/bcc/vendor/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,14 @@ local function logline(...)
os.date("%H:%M:%S"), c_clear, line, msg))
end

log = { info = logline, enabled = true }
setmetatable(_G, {
__newindex = function (_, n)
error("attempt to write to undeclared variable "..n, 2)
end,
__index = function (_, n)
error("attempt to read undeclared variable "..n, 2)
end,
})

rawset(_G, "log", { info = logline, enabled = true })
rawset(_G, "class", require("bcc.vendor.middleclass"))
128 changes: 0 additions & 128 deletions src/lua/bcc/vendor/strict.lua

This file was deleted.

0 comments on commit 818eac9

Please sign in to comment.