Commit 28981655 authored by Vicent Marti's avatar Vicent Marti

bcc: Move all `argv` parsing to Lua code

parent 7601b4b3
......@@ -17,5 +17,5 @@ limitations under the License.
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)
rawset(_G, "BCC_STANDALONE_NAME", "bcc-probe")
require("bcc.run")()
......@@ -14,38 +14,37 @@ See the License for the specific language governing permissions and
limitations under the License.
]]
local function print_usage()
io.stderr:write(
"usage: bcc-probe [[--so-path=PATH|--version|--quiet] --] path_to_script.lua [...]\n")
os.exit(1)
end
local function has_prefix(s,p)
return string.sub(s,1,string.len(p))==p
end
local function strip_prefix(s,p)
return string.sub(s, string.len(p) + 1)
end
return function()
local logging = true
require("bcc.vendor.helpers")
local progname = rawget(_G, "BCC_STANDALONE_NAME") or "bcc-lua"
local function print_usage()
io.stderr:write(string.format(
"usage: %s [[--so-path=PATH|--version|--quiet] --] path_to_script.lua [...]\n",
progname))
os.exit(1)
end
while arg[1] and has_prefix(arg[1], "-") do
local k = table.remove(arg, 1)
local function print_version()
local jit = require("jit")
print(string.format("%s %s -- Running on %s (%s/%s)",
progname, rawget(_G, "BCC_VERSION") or "HEAD",
jit.version, jit.os, jit.arch))
os.exit(0)
end
while arg[1] and string.starts(arg[1], "-") do
local k = table.remove(arg, 1)
if k == "--" then
break
elseif has_prefix(k, "--so-path=") then
rawset(_G, "LIBBCC_SO_PATH", strip_prefix(k, "--so-path="))
elseif string.starts(k, "--so-path=") then
rawset(_G, "LIBBCC_SO_PATH", string.lstrip(k, "--so-path="))
elseif k == "--llvm-debug" then
rawset(_G, "LIBBCC_LLVM_DEBUG", 1)
elseif k == "-q" or k == "--quiet" then
logging = false
log.enabled = false
elseif k == "-v" or k == "--version" then
local jit = require("jit")
print(string.format("bcc-probe %s -- Running on %s (%s/%s)",
rawget(_G, "BCC_VERSION") or "HEAD",
jit.version, jit.os, jit.arch))
return true
print_version()
else
print_usage()
end
......@@ -54,16 +53,13 @@ return function()
local tracefile = table.remove(arg, 1)
if not tracefile then print_usage() end
local BCC = require("bcc.init")
local BPF = BCC.BPF
local BPF = require("bcc.bpf")
BPF.script_root(tracefile)
log.enabled = logging
local utils = {
argparse = require("bcc.vendor.argparse"),
posix = require("bcc.vendor.posix"),
sym = BCC.sym
sym = require("bcc.sym"),
}
local command = dofile(tracefile)
......
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
function string.starts(s, p)
return string.sub(s, 1, string.len(p)) == p
end
function string.ends(String,End)
return End=='' or string.sub(String,-string.len(End))==End
function string.lstrip(s, p)
return string.sub(s, string.len(p) + 1)
end
function string.ends(s, e)
return e == '' or string.sub(s, -string.len(e))==e
end
function string.escape(s)
......
......@@ -125,31 +125,25 @@ static void pushargv(lua_State *L, char **argv, int argc, int offset)
}
}
static int find_libbcc(lua_State *L)
static void find_local_libbcc(lua_State *L)
{
const char *libbcc = getenv("LIBBCC_SO_PATH");
char buffer[4096];
char *dirname;
if (libbcc == NULL) {
char *dirname;
if (readlink("/proc/self/exe", buffer, sizeof(buffer)) < 0)
return;
if (readlink("/proc/self/exe", buffer, sizeof(buffer)) < 0)
return -1;
dirname = strrchr(buffer, '/');
if (dirname == NULL)
return;
dirname = strrchr(buffer, '/');
if (dirname == NULL)
return -1;
strcpy(dirname + 1, "libbcc.so");
strcpy(dirname + 1, "libbcc.so");
libbcc = buffer;
}
if (access(libbcc, F_OK|R_OK|X_OK) != 0)
return -1;
if (access(buffer, F_OK|R_OK|X_OK) != 0)
return;
lua_pushstring(L, libbcc);
lua_pushstring(L, buffer);
lua_setglobal(L, "LIBBCC_SO_PATH");
return 0;
}
static int pmain(lua_State *L)
......@@ -160,19 +154,14 @@ static int pmain(lua_State *L)
lua_gc(L, LUA_GCSTOP, 0);
luaL_openlibs(L);
lua_gc(L, LUA_GCRESTART, 0);
if (find_libbcc(L) < 0) {
s->status = 1;
l_message(progname, "failed to find libbcc.so");
return 0;
}
find_local_libbcc(L);
s->status = dolibrary(L, "bcc", 0);
if (s->status)
return 0;
lua_pushboolean(L, 1);
lua_setglobal(L, "BCC_STANDALONE");
lua_pushstring(L, progname);
lua_setglobal(L, "BCC_STANDALONE_NAME");
pushargv(L, s->argv, s->argc, 1);
lua_setglobal(L, "arg");
......@@ -192,13 +181,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
if (argc < 2) {
fprintf(stderr, "usage: %s path_to_probe.lua [...]\n", argv[0]);
return EXIT_FAILURE;
}
progname = argv[0];
s.argc = argc;
s.argv = argv;
s.status = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment