Hard Light Productions Forums

Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: Wanderer on January 31, 2010, 06:13:17 am

Title: Something to help with lua debugging
Post by: Wanderer on January 31, 2010, 06:13:17 am
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.
Title: Re: Something to help with lua debugging
Post by: Fury on January 31, 2010, 06:34:20 am
Ummm, what does this do?
Title: Re: Something to help with lua debugging
Post by: Wanderer on January 31, 2010, 06:36:36 am
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
Title: Re: Something to help with lua debugging
Post by: Fury on January 31, 2010, 06:40:36 am
In other words, this debugs other lua scripts?
Title: Re: Something to help with lua debugging
Post by: Wanderer on January 31, 2010, 07:00:20 am
Yes, it is a patch for FS Open to support debugging of lua scripts
Title: Re: Something to help with lua debugging
Post by: Nuke on January 31, 2010, 01:05:01 pm
more debugging facilities are always appreciated.
Title: Re: Something to help with lua debugging
Post by: Echelon9 on January 31, 2010, 10:58:19 pm
Win :) I'll give this implementation a test a bit later
Title: Re: Something to help with lua debugging
Post by: portej05 on January 31, 2010, 11:32:37 pm
E9: There's a new implementation coming up - hold off on testing this one :)
Title: Re: Something to help with lua debugging
Post by: Echelon9 on February 01, 2010, 08:35:16 am
I'll keep my eyes out for this one