Commit 02f66f4b authored by Vicent Marti's avatar Vicent Marti

lua: build as a standalone tool with static libbcc

parent 330e4f2b
...@@ -19,6 +19,7 @@ if (LUAJIT_LIBRARIES AND LUAJIT) ...@@ -19,6 +19,7 @@ if (LUAJIT_LIBRARIES AND LUAJIT)
include_directories(${LUAJIT_INCLUDE_DIR}) include_directories(${LUAJIT_INCLUDE_DIR})
add_executable(bcc-lua src/main.c bcc.o) add_executable(bcc-lua src/main.c bcc.o)
target_link_libraries(bcc-lua ${LUAJIT_LIBRARIES}) target_link_libraries(bcc-lua ${LUAJIT_LIBRARIES})
target_link_libraries(bcc-lua -Wl,--whole-archive bcc-static -Wl,--no-whole-archive)
install(TARGETS bcc-lua RUNTIME DESTINATION bin) install(TARGETS bcc-lua RUNTIME DESTINATION bin)
endif() endif()
...@@ -17,5 +17,4 @@ limitations under the License. ...@@ -17,5 +17,4 @@ limitations under the License.
local str = require("debug").getinfo(1, "S").source:sub(2) local str = require("debug").getinfo(1, "S").source:sub(2)
local script_path = str:match("(.*/)").."/?.lua;" local script_path = str:match("(.*/)").."/?.lua;"
package.path = script_path..package.path package.path = script_path..package.path
rawset(_G, "BCC_STANDALONE_NAME", "bcc-probe")
require("bcc.run")() require("bcc.run")()
...@@ -112,4 +112,11 @@ int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym); ...@@ -112,4 +112,11 @@ int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
void bcc_symcache_refresh(void *resolver); void bcc_symcache_refresh(void *resolver);
]] ]]
return ffi.load(os.getenv("LIBBCC_SO_PATH") or rawget(_G, "LIBBCC_SO_PATH") or "bcc") if rawget(_G, "BCC_STANDALONE") then
return ffi.C
else
return ffi.load(
os.getenv("LIBBCC_SO_PATH") or
rawget(_G, "LIBBCC_SO_PATH") or
"bcc")
end
...@@ -16,11 +16,12 @@ limitations under the License. ...@@ -16,11 +16,12 @@ limitations under the License.
return function() return function()
require("bcc.vendor.helpers") require("bcc.vendor.helpers")
local progname = rawget(_G, "BCC_STANDALONE_NAME") or "bcc-lua" local standalone = rawget(_G, "BCC_STANDALONE")
local progname = standalone or "bcc-probe"
local function print_usage() local function print_usage()
io.stderr:write(string.format( io.stderr:write(string.format(
"usage: %s [[--so-path=PATH|--version|--verbose] --] path_to_script.lua [...]\n", "usage: %s [[--version|--verbose] --] path_to_script.lua [...]\n",
progname)) progname))
os.exit(1) os.exit(1)
end end
...@@ -37,7 +38,7 @@ return function() ...@@ -37,7 +38,7 @@ return function()
local k = table.remove(arg, 1) local k = table.remove(arg, 1)
if k == "--" then if k == "--" then
break break
elseif string.starts(k, "--so-path=") then elseif standalone == nil and string.starts(k, "--so-path=") then
rawset(_G, "LIBBCC_SO_PATH", string.lstrip(k, "--so-path=")) rawset(_G, "LIBBCC_SO_PATH", string.lstrip(k, "--so-path="))
elseif k == "--llvm-debug" then elseif k == "--llvm-debug" then
rawset(_G, "LIBBCC_LLVM_DEBUG", 1) rawset(_G, "LIBBCC_LLVM_DEBUG", 1)
......
...@@ -125,27 +125,6 @@ static void pushargv(lua_State *L, char **argv, int argc, int offset) ...@@ -125,27 +125,6 @@ static void pushargv(lua_State *L, char **argv, int argc, int offset)
} }
} }
static void find_local_libbcc(lua_State *L)
{
char buffer[4096];
char *dirname;
if (readlink("/proc/self/exe", buffer, sizeof(buffer)) < 0)
return;
dirname = strrchr(buffer, '/');
if (dirname == NULL)
return;
strcpy(dirname + 1, "libbcc.so");
if (access(buffer, F_OK|R_OK|X_OK) != 0)
return;
lua_pushstring(L, buffer);
lua_setglobal(L, "LIBBCC_SO_PATH");
}
static int pmain(lua_State *L) static int pmain(lua_State *L)
{ {
struct Smain *s = (struct Smain *)lua_touserdata(L, 1); struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
...@@ -154,14 +133,13 @@ static int pmain(lua_State *L) ...@@ -154,14 +133,13 @@ static int pmain(lua_State *L)
lua_gc(L, LUA_GCSTOP, 0); lua_gc(L, LUA_GCSTOP, 0);
luaL_openlibs(L); luaL_openlibs(L);
lua_gc(L, LUA_GCRESTART, 0); lua_gc(L, LUA_GCRESTART, 0);
find_local_libbcc(L);
s->status = dolibrary(L, "bcc", 0); s->status = dolibrary(L, "bcc", 0);
if (s->status) if (s->status)
return 0; return 0;
lua_pushstring(L, progname); lua_pushstring(L, progname);
lua_setglobal(L, "BCC_STANDALONE_NAME"); lua_setglobal(L, "BCC_STANDALONE");
pushargv(L, s->argv, s->argc, 1); pushargv(L, s->argv, s->argc, 1);
lua_setglobal(L, "arg"); lua_setglobal(L, "arg");
...@@ -181,6 +159,11 @@ int main(int argc, char **argv) ...@@ -181,6 +159,11 @@ int main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (geteuid() != 0) {
l_message(argv[0], "bcc-lua must be ran as root");
return EXIT_FAILURE;
}
progname = argv[0]; progname = argv[0];
s.argc = argc; s.argc = argc;
s.argv = argv; s.argv = argv;
......
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