diff --git a/bindings/lua/src/bindings.c b/bindings/lua/src/bindings.c index 04ce43cf..da10a89c 100644 --- a/bindings/lua/src/bindings.c +++ b/bindings/lua/src/bindings.c @@ -43,14 +43,33 @@ int lua_LM_FindProcess(lua_State *L) return 1; } +int lua_LM_FindModule(lua_State *L) +{ + lm_module_t *udata; + const char *modname; + + modname = luaL_checkstring(L, 1); + + lua_create_lm_module(L); + udata = (lm_module_t *)lua_touserdata(L, lua_gettop(L)); + + if (LM_FindModule((lm_string_t)modname, udata) != LM_TRUE) { + lua_pushnil(L); + } + + return 1; +} + int luaopen_libmem_lua(lua_State *L) { luaL_Reg functions[] = { { "LM_FindProcess", lua_LM_FindProcess }, + { "LM_FindModule", lua_LM_FindModule }, { NULL, NULL } }; lua_define_lm_process(L); + lua_define_lm_module(L); luaL_register(L, "libmem_lua", functions); diff --git a/bindings/lua/src/types.c b/bindings/lua/src/types.c index 5918b682..25e38957 100644 --- a/bindings/lua/src/types.c +++ b/bindings/lua/src/types.c @@ -38,6 +38,11 @@ return 1; \ } #define LM_PROCESS_META "lm_process_t" +#define LM_MODULE_META "lm_module_t" + +/**************** + * lm_process_t * + ***************/ int lua_lm_process_index(lua_State *L) { @@ -66,7 +71,7 @@ int lua_lm_process_tostring(lua_State *L) void lua_create_lm_process(lua_State *L) { - lm_process_t *udata = lua_newuserdata(L, sizeof(lm_process_t)); + lm_process_t *udata = (lm_process_t *)lua_newuserdata(L, sizeof(lm_process_t)); luaL_getmetatable(L, LM_PROCESS_META); lua_setmetatable(L, lua_gettop(L) - 1); } @@ -79,3 +84,47 @@ void lua_define_lm_process(lua_State *L) lua_pushcfunction(L, lua_lm_process_tostring); lua_setfield(L, lua_gettop(L) - 1, "__tostring"); } + +/*************** + * lm_module_t * + ***************/ + +int lua_lm_module_index(lua_State *L) +{ + lm_module_t *udata = (lm_module_t *)luaL_checkudata(L, 1, LM_MODULE_META); + const char *entry = luaL_checkstring(L, 2); + + LUA_ENTRY_INT(base) + LUA_ENTRY_INT(end) + LUA_ENTRY_INT(size) + LUA_ENTRY_STR(path) + LUA_ENTRY_STR(name) + + lua_pushnil(L); + return 1; +} + +int lua_lm_module_tostring(lua_State *L) +{ + lm_module_t *udata = (lm_module_t *)luaL_checkudata(L, 1, LM_MODULE_META); + + lua_pushfstring(L, "lm_module_t(base: %p size: %p end: %p path: \"%s\" name: \"%s\")", udata->base, udata->size, udata->end, udata->path, udata->name); + + return 1; +} + +void lua_create_lm_module(lua_State *L) +{ + lm_module_t *udata = (lm_module_t *)lua_newuserdata(L, sizeof(lm_module_t)); + luaL_getmetatable(L, LM_MODULE_META); + lua_setmetatable(L, lua_gettop(L) - 1); +} + +void lua_define_lm_module(lua_State *L) +{ + luaL_newmetatable(L, LM_MODULE_META); + lua_pushcfunction(L, lua_lm_module_index); + lua_setfield(L, lua_gettop(L) - 1, "__index"); + lua_pushcfunction(L, lua_lm_module_tostring); + lua_setfield(L, lua_gettop(L) - 1, "__tostring"); +} diff --git a/bindings/lua/src/types.h b/bindings/lua/src/types.h index 0b2a38e9..5d2e149c 100644 --- a/bindings/lua/src/types.h +++ b/bindings/lua/src/types.h @@ -30,5 +30,6 @@ void lua_define_##type(lua_State *L); DECLTYPE(lm_process) +DECLTYPE(lm_module) #endif diff --git a/bindings/lua/tests/test.lua b/bindings/lua/tests/test.lua index 53752556..296cae4c 100644 --- a/bindings/lua/tests/test.lua +++ b/bindings/lua/tests/test.lua @@ -1,5 +1,9 @@ print("Lua Tests") +function separator() + print("====================") +end + local libmem = require("libmem_lua") print("libmem_lua loaded") @@ -13,3 +17,15 @@ print("Path: " .. target_proc.path) print("Name: " .. target_proc.name) print("(invalid): " .. (target_proc.invalid or "nil")) print(target_proc) + +separator() + +mod = libmem.LM_FindModule("liblibmem.so") +print("[*] Find Module") +print("Base: " .. mod.base) +print("Size: " .. mod.size) +print("End: " .. mod["end"]) -- NOTE: mod.end won't work! +print("Path: " .. mod.path) +print("Name: " .. mod.name) +print("(invalid): " .. (mod.invalid or "nil")) +print(mod)