在安卓和ios平台上可通过信号处理方式,在崩溃时打印Lua堆栈信息,方便分析bug
static void dumpHandler(int cause, siginfo_t * info, void *uap) { CCLOG("Crash dump:"); CCLuaEngine* pEngine = CCLuaEngine::defaultEngine(); me_traceStack(pEngine->getLuaStack()->getLuaState()); exit(1); } static void setupCrashReport() { struct sigaction sa; sa.sa_sigaction = dumpHandler; sigemptyset (&sa.sa_mask); sa.sa_flags = SA_SIGINFO; for (int i=0; i<kNumSignals; ++i) { if (sigaction(kSignals[i], &sa, NULL) == -1) CCLOG("sigaction [%d] failed", i); } if (sigaction (SIGSEGV, &sa, 0)) { CCLOG("setup sigaction"); } } #define SETUP_CRASH_REPORT() {setupCrashReport();} void me_traceStack (lua_State *L) { lua_print(L); int level; lua_Debug ar; std::string t; char buff[1024]; for (level = 1; level<11; ++level) { if (lua_getstack(L, level, &ar)) { lua_getinfo(L, "Snl", &ar); sprintf(buff, "frame %d \n%s :%d in function [%s], %d\n", level, ar.short_src, ar.currentline, ar.name, ar.linedefined); t += buff; } else { sprintf(buff, "frame %d \nunknown\n", level); t += buff; } } CCLOG("Lua frame stack:\n%s", t.c_str()); }
时间: 2024-10-10 16:02:59