So.. my not so brilliant written piece of debugging code... Suggestions, improvements are extremely welcome...
Index: code/globalincs/windebug.cpp
===================================================================
--- code/globalincs/windebug.cpp (revision 5859)
+++ code/globalincs/windebug.cpp (working copy)
@@ -988,9 +988,11 @@
dumpBuffer.Printf( "Source:\t\t%s\r\n", ar.source);
dumpBuffer.Printf( "Short source:\t%s\r\n", ar.short_src);
dumpBuffer.Printf( "Current line:\t%d\r\n", ar.currentline);
+ dumpBuffer.Printf( "- Function line:\t%d\r\n", (ar.linedefined ? (1 + ar.currentline - ar.linedefined) : 0));
}
extern lua_Debug Ade_debug_info;
+extern char debug_stack[4][32];
void LuaError(struct lua_State *L, char *format, ...)
{
int val;
@@ -1046,6 +1048,8 @@
dumpBuffer.Printf("(No stack debug info)\r\n");
}
*/
+// TEST CODE
+
dumpBuffer.Printf(Separator);
dumpBuffer.Printf( "ADE Debug:" );
dumpBuffer.Printf( "\r\n" );
@@ -1058,8 +1062,12 @@
AssertText2[0] = '\0';
dumpBuffer.Printf(Separator);
- dumpBuffer.Printf("LUA Stack:");
- dumpBuffer.Printf( "\r\n" );
+ dumpBuffer.Printf("LUA Stack:\r\n");
+ int i;
+ for (i = 0; i < 4; i++) {
+ if (debug_stack[i][0] != '\0')
+ dumpBuffer.Printf("\t%s\r\n", debug_stack[i]);
+ }
dumpBuffer.Printf(Separator);
ade_stackdump(L, AssertText2);
dumpBuffer.Printf( AssertText2 );
Index: code/parse/lua.cpp
===================================================================
--- code/parse/lua.cpp (revision 5859)
+++ code/parse/lua.cpp (working copy)
@@ -10903,18 +10903,54 @@
// *************************Housekeeping*************************
//WMC - The miraculous lines of code that make Lua debugging worth something.
lua_Debug Ade_debug_info;
+char debug_stack[4][32];
-void ade_debug_line(lua_State *L, lua_Debug *ar)
+void ade_debug_call(lua_State *L, lua_Debug *ar)
{
Assert(L != NULL);
Assert(ar != NULL);
+ lua_getstack(L, 1, ar);
+ //lua_getfield(L, LUA_GLOBALSINDEX, "f");
lua_getinfo(L, "nSlu", ar);
memcpy(&Ade_debug_info, ar, sizeof(lua_Debug));
+
+ int n;
+ for (n = 0; n < 4; n++) {
+ debug_stack[n][0] = '\0';
+ }
+
+ for (n = 0; n < 4; n++) {
+ if (lua_getstack(L,n+1, ar) == NULL)
+ break;
+ lua_getinfo(L,"n", ar);
+ if (ar->name == NULL)
+ break;
+ strcpy_s(debug_stack[n],ar->name);
+ }
}
void ade_debug_ret(lua_State *L, lua_Debug *ar)
{
- //WMC - So Lua isn't mean and uses ade_debug_line for returns
+ Assert(L != NULL);
+ Assert(ar != NULL);
+ lua_getstack(L, 1, ar);
+ //lua_getfield(L, LUA_GLOBALSINDEX, "f");
+ lua_getinfo(L, "nSlu", ar);
+ memcpy(&Ade_debug_info, ar, sizeof(lua_Debug));
+
+ int n;
+ for (n = 0; n < 4; n++) {
+ debug_stack[n][0] = '\0';
+ }
+
+ for (n = 0; n < 4; n++) {
+ if (lua_getstack(L,n+1, ar) == NULL)
+ break;
+ lua_getinfo(L,"n", ar);
+ if (ar->name == NULL)
+ break;
+ strcpy_s(debug_stack[n],ar->name);
+ }
}
//WMC - because the behavior of the return keyword
@@ -10968,7 +11004,7 @@
//*****SET DEBUG HOOKS
#ifndef NDEBUG
- lua_sethook(L, ade_debug_line, LUA_MASKLINE, 0);
+ lua_sethook(L, ade_debug_call, LUA_MASKCALL, 0);
lua_sethook(L, ade_debug_ret, LUA_MASKRET, 0);
#endif
As said, please provide feedback on how it works and how it could and should be improved.