Author Topic: Something to help with lua debugging  (Read 1554 times)

0 Members and 1 Guest are viewing this topic.

Offline Wanderer

  • Wiki Warrior
  • 211
  • Mostly harmless
Something to help with lua debugging
So.. my not so brilliant written piece of debugging code... Suggestions, improvements are extremely welcome...

Code: [Select]
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.
« Last Edit: February 01, 2010, 03:11:34 am by Wanderer »
Do not meddle in the affairs of coders for they are soggy and hard to light

 

Offline Fury

  • The Curmudgeon
  • 213
Re: Something to help with lua debugging
Ummm, what does this do?

 

Offline Wanderer

  • Wiki Warrior
  • 211
  • Mostly harmless
Re: Something to help with lua debugging
This allows lua debugger to provide fairly useful information on the issue that caused a failure as well as provide couple of steps of stack

In short this removes the requirement of owning a crystal ball prior to starting lua debugging effort
Do not meddle in the affairs of coders for they are soggy and hard to light

  

Offline Fury

  • The Curmudgeon
  • 213
Re: Something to help with lua debugging
In other words, this debugs other lua scripts?

 

Offline Wanderer

  • Wiki Warrior
  • 211
  • Mostly harmless
Re: Something to help with lua debugging
Yes, it is a patch for FS Open to support debugging of lua scripts
Do not meddle in the affairs of coders for they are soggy and hard to light

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Something to help with lua debugging
more debugging facilities are always appreciated.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Echelon9

  • 210
Re: Something to help with lua debugging
Win :) I'll give this implementation a test a bit later

 
Re: Something to help with lua debugging
E9: There's a new implementation coming up - hold off on testing this one :)
STRONGTEA. Why can't the x86 be sane?

 

Offline Echelon9

  • 210
Re: Something to help with lua debugging
I'll keep my eyes out for this one