Beispiel #1
0
/* 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);
  }
}
Beispiel #2
0
static int profiler_init(lua_State *L) {
  lprofP_STATE* S;
  const char* outfile;
  float function_call_time;

  lua_pushlightuserdata(L, &profstate_id);
  lua_gettable(L, LUA_REGISTRYINDEX);
  if(!lua_isnil(L, -1)) {
    profiler_stop(L);
  }
  lua_pop(L, 1);

  function_call_time = calcCallTime(L);

  outfile = NULL;
  if(lua_gettop(L) >= 1)
    outfile = luaL_checkstring(L, 1);

  /* init with default file name and printing a header line */
  if (!(S=lprofP_init_core_profiler(outfile, 1, function_call_time))) {
    return luaL_error(L,"LuaProfiler error: output file could not be opened!");
  }

  lua_sethook(L, (lua_Hook)callhook, LUA_MASKCALL | LUA_MASKRET, 0);

  lua_pushlightuserdata(L, &profstate_id);
  lua_pushlightuserdata(L, S);
  lua_settable(L, LUA_REGISTRYINDEX);
	
  /* use our own exit function instead */
  lua_getglobal(L, "os");
  lua_pushlightuserdata(L, &exit_id);
  lua_pushstring(L, "exit");
  lua_gettable(L, -3);
  lua_settable(L, LUA_REGISTRYINDEX);
  lua_pushstring(L, "exit");
  lua_pushcfunction(L, (lua_CFunction)exit_profiler);
  lua_settable(L, -3);

#if 0
  /* use our own coroutine.create function instead */
  lua_getglobal(L, "coroutine");
  lua_pushstring(L, "create");
  lua_pushcfunction(L, (lua_CFunction)coroutine_create);
  lua_settable(L, -3);
#endif

  /* the following statement is to simulate how the execution stack is */
  /* supposed to be by the time the profiler is activated when loaded  */
  /* as a library.                                                     */

  lprofP_callhookIN(S, "profiler_init", "(C)", -1, -1);
	
  lua_pushboolean(L, 1);
  return 1;
}
static int profiler_init(lua_State *L) {
	lprofP_STATE* S;
	const char* outfile;
	float function_call_time;

	function_call_time = calcCallTime(L);

	outfile = NULL;
	if(lua_gettop(L) == 1)
		outfile = luaL_checkstring(L, -1);
	
	lua_sethook(L, (lua_Hook)callhook, LUA_MASKCALL | LUA_MASKRET, 0);
	/* init with default file name and printing a header line */
	if (!(S=lprofP_init_core_profiler(outfile, 1, function_call_time))) {
		luaL_error(L,"LuaProfiler error: output file could not be opened!");
		lua_pushnil(L);
		return 1;
	}

	/* Richard 30 May 2007
	 * add one to L when used as a key, otherwise this causes the
	 * thread to be garbage collected. A bug in the lua GC?
	 */
	lua_pushlightuserdata(L, ((unsigned int)L) + 1);
	lua_pushlightuserdata(L, S);
	lua_settable(L, LUA_REGISTRYINDEX);
	
	/* use our own exit function instead */
	lua_getglobal(L, "os");
	lua_pushlightuserdata(L, &exit_id);
	lua_pushstring(L, "exit");
	lua_gettable(L, -3);
	lua_settable(L, LUA_REGISTRYINDEX);
	lua_pushstring(L, "exit");
	lua_pushcfunction(L, (lua_CFunction)exit_profiler);
	lua_settable(L, -3);

	/* use our own coroutine.create function instead */
	/*	lua_getglobal(L, "coroutine");*/
	/*	lua_pushstring(L, "create");*/
	/*	lua_pushcfunction(L, (lua_CFunction)coroutine_create);*/
	/*	lua_settable(L, -3);*/

	/* the following statement is to simulate how the execution stack is */
	/* supposed to be by the time the profiler is activated when loaded  */
	/* as a library.                                                     */

	lprofP_callhookIN(S, "", "profiler_init", "(C)", -1, -1);
	
	lua_pushboolean(L, 1);
	return 1;
}