Add stace traces to failed Lua executions

This commit is contained in:
Brendan Byrd 2018-11-15 00:44:18 +00:00
parent 7225958aed
commit e50f7bfd71
1 changed files with 43 additions and 14 deletions

View File

@ -1869,6 +1869,26 @@ static int memory_setregister(lua_State *L)
return 0; return 0;
} }
// Forces a stack trace and returns the string
static const char *CallLuaTraceback(lua_State *L) {
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return "";
}
lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return "";
}
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
return lua_tostring(L, -1);
}
void HandleCallbackError(lua_State* L) void HandleCallbackError(lua_State* L)
{ {
@ -1876,14 +1896,19 @@ void HandleCallbackError(lua_State* L)
// luaL_error(L, "%s", lua_tostring(L,-1)); // luaL_error(L, "%s", lua_tostring(L,-1));
//else //else
{ {
const char *trace = CallLuaTraceback(L);
lua_pushnil(L); lua_pushnil(L);
lua_setfield(L, LUA_REGISTRYINDEX, guiCallbackTable); lua_setfield(L, LUA_REGISTRYINDEX, guiCallbackTable);
char errmsg [2048];
sprintf(errmsg, "%s\n%s", lua_tostring(L,-1), trace);
// Error? // Error?
#ifdef WIN32 #ifdef WIN32
MessageBox( hAppWnd, lua_tostring(L,-1), "Lua run error", MB_OK | MB_ICONSTOP); MessageBox( hAppWnd, errmsg, "Lua run error", MB_OK | MB_ICONSTOP);
#else #else
fprintf(stderr, "Lua thread bombed out: %s\n", lua_tostring(L,-1)); fprintf(stderr, "Lua thread bombed out: %s\n", errmsg);
#endif #endif
FCEU_LuaStop(); FCEU_LuaStop();
@ -5865,17 +5890,21 @@ void FCEU_LuaFrameBoundary()
} else if (result != 0) { } else if (result != 0) {
// Done execution by bad causes // Done execution by bad causes
FCEU_LuaOnStop(); FCEU_LuaOnStop();
const char *trace = CallLuaTraceback(L);
lua_pushnil(L); lua_pushnil(L);
lua_setfield(L, LUA_REGISTRYINDEX, frameAdvanceThread); lua_setfield(L, LUA_REGISTRYINDEX, guiCallbackTable);
char errmsg [1024];
sprintf(errmsg, "%s\n%s", lua_tostring(thread,-1), trace);
// Error? // Error?
#ifdef WIN32 #ifdef WIN32
//StopSound();//StopSound(); //mbg merge 7/23/08 //StopSound();//StopSound(); //mbg merge 7/23/08
MessageBox( hAppWnd, lua_tostring(thread,-1), "Lua run error", MB_OK | MB_ICONSTOP); MessageBox( hAppWnd, errmsg, "Lua run error", MB_OK | MB_ICONSTOP);
#else #else
fprintf(stderr, "Lua thread bombed out: %s\n", lua_tostring(thread,-1)); fprintf(stderr, "Lua thread bombed out: %s\n", errmsg);
#endif #endif
} else { } else {
FCEU_LuaOnStop(); FCEU_LuaOnStop();
//FCEU_DispMessage("Script died of natural causes.\n",0); //FCEU_DispMessage("Script died of natural causes.\n",0);