void dbg_lua(lua_State *L, int err, const char *msg) { if( err ){ printf("%s: Error while executing file\n", msg); switch( err ){ case LUA_ERRFILE: printf("couldn't open the given file\n"); exit(-1); case LUA_ERRSYNTAX: printf("syntax error during pre-compilation\n"); luaL_traceback(L, L, msg, 1); printf("%s\n", lua_tostring(L, -1)); exit(-1); case LUA_ERRMEM: printf("memory allocation error\n"); exit(-1); case LUA_ERRRUN: { const char * msg = lua_tostring(L, -1); luaL_traceback(L, L, msg, 1); printf("%s\n", lua_tostring(L, -1)); exit(-1); } case LUA_ERRERR: printf("error while running the error handler function\n"); exit(-1); default: printf("unknown error %i\n", err); exit(-1); } } }
static void thread_error(lua_State *T) { #ifdef HAVE_TRACEBACK const char *msg = lua_tostring(T, -1); if (msg) luaL_traceback(L, T, msg, 0); #else /* adapted from Lua 5.1 source */ if (!lua_isstring(T, -1)) /* 'message' not a string? */ return; lua_getfield(T, LUA_GLOBALSINDEX, "debug"); if (!lua_istable(T, -1)) { lua_pop(T, 1); goto merror; } lua_getfield(T, -1, "traceback"); if (!lua_isfunction(T, -1)) { lua_pop(T, 2); goto merror; } lua_pushvalue(T, -3); /* pass error message */ lua_pushinteger(T, 1); /* skip traceback */ lua_call(T, 2, 1); /* call debug.traceback */ merror: lua_xmove(T, L, 1); /* move error message to L */ #endif }
static int _traceback(lua_State* L) { const char* msg = lua_tostring(L, 1); if (msg) luaL_traceback(L, L, msg, 1); else lua_pushliteral(L, "no error msg"); return 1; }
static int dump_error(lua_State *L) { const char * message = lua_tostring(L,-1); if(darktable.unmuted & DT_DEBUG_LUA) luaL_traceback(L,L,message,0); // else : the message is already on the top of the stack, don't touch return 1; }
static void thread_error(lua_State *T) { const char *msg = lua_tostring(T, -1); if (msg) luaL_traceback(L, T, msg, 0); }
static int traceback(lua_State *L) { const char *err = lua_tostring(L, 1); if (err) { luaL_traceback(L, L, err, 1); } else { lua_pushliteral(L, "(no error message)"); } return 1; }
static int _luaradio_traceback(lua_State *L) { /* Return error object directly, if it's not a string */ if (!lua_isstring(L, 1)) return 1; /* Create traceback appended to error string */ luaL_traceback(L, L, lua_tostring(L, 1), 1); return 1; }
static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) luaL_traceback(L, L, msg, 1); else { lua_pushliteral(L, "(no error message)"); } return 1; }
// Internal ------------------------------------------------------------------- // ---------------------------------------------------------------------------- static int internal_handle_error(lua_State *L) { const char *err = lua_tostring(L, -1); // = 1 lua_State *s = luaL_newstate(); luaL_traceback(s, L, NULL, 1); vm_error("%s\n%s", err, lua_tostring(s, -1)); lua_close(s); return 0; }
int cmd_backtrace(struct ldb_context *lctx, const char *cmdbuffer) { struct lua_program *lp = lctx->lprog; if(lp->status == RUNNING && lp->L != NULL) { const char *traceback; luaL_traceback(lp->L, lp->L, NULL, 1); traceback = lua_tostring(lp->L, -1); printf("%s\n", traceback); } return 0; }
static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) luaL_traceback(L, L, msg, 1); else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */ if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */ lua_pushliteral(L, "(no error message)"); } return 1; }
static int traceback(lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) luaL_traceback(L, L, msg, 1); else if (!lua_isnoneornil(L, 1)) { if (!luaL_callmeta(L, 1, "__tostring")) lua_pushliteral(L, "(no error message)"); } return 1; }
static int lluas_traceback(lua_State* L) { /* Adapted from lua.c, but without metamethod support */ const char* msg; msg = lua_tostring(L, 1); if (msg) luaL_traceback(L, L, msg, 1); else lua_pushliteral(L, "(no error message)"); return 1; }
static int traceback(lua_State *L) { if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */ if (lua_isnoneornil(L, 1) || !luaL_callmeta(L, 1, "__tostring") || !lua_isstring(L, -1)) return 1; /* Return non-string error object. */ lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */ } luaL_traceback(L, L, lua_tostring(L, 1), 1); return 1; }
/* * The outer-most error callback handler for use with lua_pcall(). On * error Lua will call this callback with a single argument that * represents the error value. In most cases this will be a string * containing an error message, but channel programs can use Lua's * error() function to return arbitrary objects as errors. This callback * returns (on the Lua stack) the original error object along with a traceback. * * Fatal Lua errors can occur while resources are held, so we also call any * registered cleanup function here. */ static int zcp_error_handler(lua_State *state) { const char *msg; zcp_cleanup(state); VERIFY3U(1, ==, lua_gettop(state)); msg = lua_tostring(state, 1); luaL_traceback(state, state, msg, 1); return (1); }
static int db_traceback (lua_State *L) { int arg; lua_State *L1 = getthread(L, &arg); const char *msg = lua_tostring(L, arg + 1); if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ lua_pushvalue(L, arg + 1); /* return it untouched */ else { int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0); luaL_traceback(L, L1, msg, level); } return 1; }
int dt_lua_dostring(lua_State *L, const char *command, int nargs, int nresults) { int load_result = luaL_loadstring(L, command); if(load_result != LUA_OK) { const char *error_msg = lua_tostring(L, -1); luaL_traceback(L, L, error_msg, 0); lua_remove(L, -2); return load_result; } lua_insert(L, -(nargs + 1)); return dt_lua_do_chunk(L, nargs, nresults); }
std::string lua::traceback(lua_State* const state, const int toplevel) { #if LUA_VERSION_NUM >= 502 std::string rv; luaL_traceback(state, state, NULL, toplevel); rv = lua::get<std::string>(state, -1); lua_pop(state, 1); return rv; #else auto getter = lua::global(state, "debug")["traceback"]; return getter("", topLevel).get<std::string>().substr(1); #endif }
static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg == NULL) { /* is error object not a string? */ if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */ lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */ return 1; /* that is the message */ else msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1)); } luaL_traceback(L, L, msg, 1); /* append a standard traceback */ return 1; /* return the traceback */ }
void print_traceback(lua_State *L) { const char *msg = lua_tostring(L, -1); if (msg) /* is error object a string? */ luaL_traceback(L, L, msg, 0); /* use standard traceback */ else if (!lua_isnoneornil(L, -1)) { /* non-string error object? */ /* try its 'tostring' metamethod */ if (!luaL_callmeta(L, -1, "__tostring")) lua_pushliteral(L, "(no error message)"); } /* else no error object, does nothing */ const char *err_w_traceback = lua_tostring(L, -1); printf("%s\n", err_w_traceback); }
static int traceback(lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg == NULL) { if (luaL_callmeta(L, 1, "__tostring") && lua_type(L, -1) == LUA_TSTRING) return 1; else msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1)); } luaL_traceback(L, L, msg, 1); return 1; }
/* * Add a stack traceback to the error message. */ static int traceback_handler(lua_State *lua) { #if LUA_VERSION_NUM < 502 lua_getfield(lua, LUA_GLOBALSINDEX, "debug"); lua_getfield(lua, -1, "traceback"); lua_pushvalue(lua, 1); lua_pushinteger(lua, 2); lua_call(lua, 2, 1); #else luaL_traceback(lua, lua, lua_tostring(lua, 1), 0); #endif return 1; }
int TenshiRunQuanta(TenshiRuntimeState s) { int ops_left = QUANTA_OPCODES; while (ops_left > 0) { TenshiActorState a; int ret = ActorDequeueHead(s, &a); if (ret != LUA_OK) return ret; if (!a) { printf("NOTHING TO RUN!\n"); return LUA_OK; } ret = threading_run_ops(a->L, ops_left, &ops_left); if (ret == THREADING_ERROR) { printf("THERE WAS AN ERROR!\n"); const char *msg = lua_tostring(a->L, -1); if (msg) /* is error object a string? */ luaL_traceback(a->L, a->L, msg, 0); /* use standard traceback */ else if (!lua_isnoneornil(a->L, -1)) { /* non-string error object? */ /* try its 'tostring' metamethod */ if (!luaL_callmeta(a->L, -1, "__tostring")) lua_pushliteral(a->L, "(no error message)"); } /* else no error object, does nothing */ const char *err_w_traceback = lua_tostring(a->L, -1); printf("%s\n", err_w_traceback); return LUA_ERRRUN; } if (ret == THREADING_EXITED) { printf("Thread exited!\n"); ActorDestroy(a); } else if (ret == THREADING_YIELD) { printf("Thread yielded (blocked)!\n"); ret = ActorSetBlocked(a); if (ret != LUA_OK) return ret; } else if (ret == THREADING_PREEMPT) { // Requeue it printf("Thread preempted!\n"); ret = ActorSetRunnable(a, 0); if (ret != LUA_OK) return ret; } } return LUA_OK; }
/* {{{ ratchet_error_tostring() */ static int ratchet_error_tostring (lua_State *L) { lua_settop (L, 1); lua_getfield (L, 1, "thread"); lua_State *L1 = lua_tothread (L, -1); lua_getfield (L, 1, "get_string"); lua_pushvalue (L, 1); lua_call (L, 1, 1); const char *msg = lua_tostring (L, -1); if (L1) luaL_traceback (L, L1, msg, 1); return 1; }
// TODO should print stack int onLuaError(lua_State *state){ _LOG_ERROR << "onLuaError called"; //std::string error; //stackDump(state); luaL_traceback(state,state,"onLuaError called",0); //const char *luaL_tolstring (lua_State *L, int idx, size_t *len); _LOG_ERROR << luaL_tolstring ( state, -1, 0); if(onLuaErrorFunction){ _LOG_ERROR << "onLuaErrorFunction defined, should be executed"; } // Returns an error return 1; }
static int traceback(lua_State *l) { luaL_traceback(l, l, lua_tostring(l, 1), 1); return 1; }
void LuaState::Traceback(const char* message, int level) { luaL_traceback(m_state, m_state, message, level); }
int dt_lua_do_chunk(lua_State *L, int nargs, int nresults) { lua_State *new_thread = lua_newthread(L); darktable.lua_state.pending_threads++; lua_insert(L, -(nargs + 2)); lua_xmove(L, new_thread, nargs + 1); int thread_result = lua_resume(new_thread, L, nargs); do { switch(thread_result) { case LUA_OK: if(nresults != LUA_MULTRET) { lua_settop(new_thread, nresults); } int result = lua_gettop(new_thread); lua_pop(L, 1); // remove the temporary thread from the main thread lua_xmove(new_thread, L, result); darktable.lua_state.pending_threads--; return LUA_OK; case LUA_YIELD: { /* This code will force a thread to exit at yield instead of waiting for the thread to stop by itself This is commented out for the time being, if(darktable.lua_state.ending) { lua_pop(L,1); if(nresults != LUA_MULTRET) for(int i = 0 ; i < nresults ; i++) lua_pushnil(L); darktable.lua_state.pending_threads--; return LUA_OK; } */ if(lua_gettop(new_thread) == 0) { lua_pushstring(new_thread, "no parameter passed to yield"); thread_result = LUA_ERRSYNTAX; goto error; } yield_type type; lua_pushcfunction(new_thread, protected_to_yield); lua_pushvalue(new_thread, 1); thread_result = lua_pcall(new_thread, 1, 1, 0); if(thread_result != LUA_OK) { goto error; } type = lua_tointeger(new_thread, -1); lua_pop(new_thread, 1); switch(type) { case WAIT_MS: { lua_pushcfunction(new_thread, protected_to_int); lua_pushvalue(new_thread, 2); thread_result = lua_pcall(new_thread, 1, 1, 0); if(thread_result != LUA_OK) { goto error; } int wait_time = lua_tointeger(new_thread, -1); lua_pop(new_thread, 3); dt_lua_unlock(); g_usleep(wait_time * 1000); dt_lua_lock(); thread_result = lua_resume(new_thread, L, 0); break; } case FILE_READABLE: { lua_pushcfunction(new_thread, protected_to_userdata); lua_pushvalue(new_thread, 2); lua_pushstring(new_thread, LUA_FILEHANDLE); thread_result = lua_pcall(new_thread, 2, 1, 0); if(thread_result != LUA_OK) { goto error; } luaL_Stream *stream = lua_touserdata(new_thread, -1); lua_pop(new_thread, 1); int myfileno = fileno(stream->f); fd_set fdset; FD_ZERO(&fdset); FD_SET(myfileno, &fdset); dt_lua_unlock(); select(myfileno + 1, &fdset, NULL, NULL, 0); dt_lua_lock(); thread_result = lua_resume(new_thread, L, 0); break; } case RUN_COMMAND: { lua_pushcfunction(new_thread, protected_to_string); lua_pushvalue(new_thread, 2); thread_result = lua_pcall(new_thread, 1, 1, 0); if(thread_result != LUA_OK) { goto error; } const char *command = lua_tostring(new_thread, -1); lua_pop(new_thread, 3); dt_lua_unlock(); int result = system(command); dt_lua_lock(); lua_pushinteger(new_thread, result); thread_result = lua_resume(new_thread, L, 1); break; } default: lua_pushstring(new_thread, "program error, shouldn't happen"); thread_result = LUA_ERRRUN; goto error; } break; } default: goto error; } } while(true); error: { const char *error_msg = lua_tostring(new_thread, -1); luaL_traceback(L, new_thread, error_msg, 0); lua_remove(L, -2); // remove the new thread from L darktable.lua_state.pending_threads--; return thread_result; } }
static int create_backtrace(lua_State*L) { luaL_traceback(L,L,lua_tostring(L,-1),0); return 1; }
int dt_lua_do_chunk(lua_State *L,int nargs,int nresults) { lua_State * new_thread = lua_newthread(L); lua_insert(L,-(nargs+2)); lua_xmove(L,new_thread,nargs+1); int thread_result = lua_resume(new_thread, L,nargs); do { switch(thread_result) { case LUA_OK: if(darktable.gui!=NULL) { dt_lua_unlock(false); dt_control_signal_raise(darktable.signals, DT_SIGNAL_FILMROLLS_CHANGED); // just for good measure dt_control_queue_redraw(); dt_lua_lock(); } if(nresults !=LUA_MULTRET) { lua_settop(new_thread,nresults); } int result= lua_gettop(new_thread); lua_pop(L,1); lua_xmove(new_thread,L,result); return result; case LUA_YIELD: { if(lua_gettop(new_thread) == 0) { lua_pushstring(new_thread,"no parameter passed to yield"); goto error; } yield_type type; lua_pushcfunction(new_thread,protected_to_yield); lua_pushvalue(new_thread,1); thread_result = lua_pcall(new_thread,1,1,0); if(thread_result!= LUA_OK) goto error; type = lua_tointeger(new_thread,-1); lua_pop(new_thread,1); switch(type) { case WAIT_MS: { lua_pushcfunction(new_thread,protected_to_int); lua_pushvalue(new_thread,2); thread_result = lua_pcall(new_thread,1,1,0); if(thread_result!= LUA_OK) goto error; int wait_time = lua_tointeger(new_thread,-1); lua_pop(new_thread,1); dt_lua_unlock(false); g_usleep(wait_time*1000); dt_lua_lock(); thread_result = lua_resume(new_thread,L,0); break; } case FILE_READABLE: { lua_pushcfunction(new_thread,protected_to_userdata); lua_pushvalue(new_thread,2); lua_pushstring(new_thread,LUA_FILEHANDLE); thread_result = lua_pcall(new_thread,2,1,0); if(thread_result!= LUA_OK) goto error; luaL_Stream * stream = lua_touserdata(new_thread,-1); lua_pop(new_thread,1); int myfileno = fileno(stream->f); fd_set fdset; FD_ZERO(&fdset); FD_SET(myfileno,&fdset); dt_lua_unlock(false); select(myfileno+1,&fdset,NULL,NULL,0); dt_lua_lock(); thread_result = lua_resume(new_thread,L,0); break; } case RUN_COMMAND: { lua_pushcfunction(new_thread,protected_to_string); lua_pushvalue(new_thread,2); thread_result = lua_pcall(new_thread,1,1,0); if(thread_result!= LUA_OK) goto error; const char* command = lua_tostring(new_thread,-1); lua_pop(L,1); dt_lua_unlock(false); int result = system(command); dt_lua_lock(); lua_pushinteger(L,result); thread_result = lua_resume(new_thread,L,1); break; } default: lua_pushstring(new_thread,"program error, shouldn't happen"); goto error; } break; } default: goto error; } }while(true); error: if(darktable.unmuted & DT_DEBUG_LUA) { dt_print(DT_DEBUG_LUA,"LUA ERROR : %s", lua_tostring(new_thread,-1)); luaL_traceback(L,new_thread,"",0); const char * error = lua_tostring(L,-1); dt_print(DT_DEBUG_LUA,error); lua_pop(L,1); } lua_pop(L,1); if(nresults !=LUA_MULTRET) { for(int i= 0 ; i < nresults; i++) { lua_pushnil(L); } return nresults; } return 0; }