/* called by Lua (via the callhook mechanism) */ static void callhook(lua_State *L, lua_Debug *ar) { int currentline; lua_Debug previous_ar; lprofP_STATE* S; lua_pushlightuserdata(L, &profstate_id); lua_gettable(L, LUA_REGISTRYINDEX); S = (lprofP_STATE*)lua_touserdata(L, -1); if (lua_getstack(L, 1, &previous_ar) == 0) { currentline = -1; } else { lua_getinfo(L, "l", &previous_ar); currentline = previous_ar.currentline; } lua_getinfo(L, "nS", ar); if (!ar->event) { /* entering a function */ lprofP_callhookIN(S, (char *)ar->name, (char *)ar->source, ar->linedefined, currentline); } else { /* ar->event == "return" */ lprofP_callhookOUT(S); } }
static int profiler_stop(lua_State *L) { lprofP_STATE* S; lua_sethook(L, NULL, LUA_MASKCALL | LUA_MASKRET, 0); lua_pushlightuserdata(L, ((unsigned int)L + 1)); lua_gettable(L, LUA_REGISTRYINDEX); S = (lprofP_STATE*)lua_touserdata(L, -1); /* leave all functions under execution */ while (lprofP_callhookOUT(S)); lua_pushboolean(L, 1); return 1; }
/* of 1 (meaning that the function 'main' has been exited) */ static void exit_profiler(lua_State *L) { lprofP_STATE* S; lua_pushlightuserdata(L, &profstate_id); lua_gettable(L, LUA_REGISTRYINDEX); S = (lprofP_STATE*)lua_touserdata(L, -1); /* leave all functions under execution */ while (lprofP_callhookOUT(S)) ; /* call the original Lua 'exit' function */ lua_pushlightuserdata(L, &exit_id); lua_gettable(L, LUA_REGISTRYINDEX); lua_call(L, 0, 0); }
static int profiler_stop(lua_State *L) { lprofP_STATE* S; lua_sethook(L, (lua_Hook)callhook, 0, 0); lua_pushlightuserdata(L, &profstate_id); lua_gettable(L, LUA_REGISTRYINDEX); if(!lua_isnil(L, -1)) { S = (lprofP_STATE*)lua_touserdata(L, -1); /* leave all functions under execution */ while (lprofP_callhookOUT(S)); lprofP_close_core_profiler(S); lua_pushboolean(L, 1); } else { lua_pushboolean(L, 0); } return 1; }