Hey. I stumbled on this post when attempting to figure out myself how to debug Urho3D’s embedded Lua. I’ve managed to get debugging working under the following conditions (note I tried to post some links but am not allowed yet - google is your friend):
[ul]
[li] I have to use the LuaJIT Urho3D interpreter option, to get around the “dynamic libraries not enabled” issue (you say LuaJIT didn’t work for you, but it definitely does for me, only the standard Lua interpreter fails with this error).[/li]
[li] I also had problems getting the socket
library installed in a “nice” way. Eventually I installed via the luarocks package manager, which installs to the system path rather than the project folder (annoying), but since it’s just for debugging and won’t be shipped I figured it’s fine.[/li]
[li] I use eclipse with the LDT plugin, which comes with a debugger.lua
that can be injected into your project.[/li][/ul]
This then got me a semi-working debugger. However, I found that breakpoints wouldn’t be hit. I enabled the “break on first line” option in LDT debugger, which did pause the script on loading it, but the source was not available. Interestingly, the source higher up the stack was available, however, just not the source of my script.
I traced this to the way Urho3D (necessarily) loads it’s scripts into Lua. A file is read to a character array and then the character array is read by the Lua interpreter, so the mapping to the original file is lost (see LuaFile.cpp
).
So, to execute a Lua file directly, rather than going through the resource cache, I added the following method to LuaScript.cpp
as an alternative the the existing ExecuteFile
method:
bool LuaScript::ExecuteRawFile(const String& fileName)
{
PROFILE(ExecuteRawFile);
ResourceCache* cache = GetSubsystem<ResourceCache>();
String filePath = cache->GetResourceFileName(fileName);
LOGINFO("Executing Lua file from file system: " + filePath);
if (filePath.Empty())
{
LOGERROR("Lua file not found: " + filePath);
return false;
}
int top = lua_gettop(luaState_);
if (luaL_loadfile (luaState_, filePath.CString()))
{
const char* message = lua_tostring(luaState_, -1);
LOGERROR("Load Lua file failed: " + String(message));
lua_settop(luaState_, top);
return false;
}
if (lua_pcall(luaState_, 0, 0, 0))
{
const char* message = lua_tostring(luaState_, -1);
LOGERROR("Execute Lua file failed: " + String(message));
lua_settop(luaState_, top);
return false;
}
return true;
}
This combined with my findings above allows me to set breakpoints and step through my Lua code just fine (gotta love simultaneous debugging C++ and Lua code breakpoints).
NOTE: I am very new to Urho3D and Lua and the code above is spliced together from other methods and may not be the correct way of doing things. But it does work for now and I’ll switch back to the ‘proper’ resource cache way of loading scripts in production.
Hope this helps.