Game Development Community

T3D 1.1 Final - backtrace() consoleFunction broken - RESOLVED (THREED-2182)

by Fyodor -bank- Osokin · in Torque 3D Professional · 07/13/2011 (6:06 am) · 6 replies

Build: T3D 1.1 Final

Platform: Any

Target: Torque Console sub-system

Issues:
Torque crashes when calling backtrace() console function.

Steps to Repeat:
Start engine. In console type backtrace(); and press enter. Crash.

Thoughts
Back in TGE, the gEvalState.stack contained only the real stack, so at any level you had gEvalState.stack[i]->scopeName set to something, and in T3D it contains a lot of "empty" levels.

Suggested Fix:
replace function with:
DefineEngineFunction(backtrace, void, ( ),,
   "@brief Prints the scripting call stack to the console log.nn"
   "Used to trace functions called from within functions. Can help discover what functions were called "
   "(and not yet exited) before the current point in scripts.nn"
   "@ingroup Debugging")
{
   U32 totalSize = 1;

   for(U32 i = 0; i < gEvalState.getStackDepth(); i++)
   {
      if(gEvalState.stack[i]->scopeNamespace && gEvalState.stack[i]->scopeNamespace->mPackage)
         totalSize += dStrlen(gEvalState.stack[i]->scopeNamespace->mPackage) + 2;
      if(gEvalState.stack[i]->scopeName)
         totalSize += dStrlen(gEvalState.stack[i]->scopeName) + 3;
      if(gEvalState.stack[i]->scopeNamespace && gEvalState.stack[i]->scopeNamespace->mName)
         totalSize += dStrlen(gEvalState.stack[i]->scopeNamespace->mName) + 2;
   }

   char *buf = Con::getReturnBuffer(totalSize);
   buf[0] = 0;
   for(U32 i = 0; i < gEvalState.getStackDepth(); i++)
   {
      dStrcat(buf, "->");
      if(gEvalState.stack[i]->scopeNamespace && gEvalState.stack[i]->scopeNamespace->mPackage)
      {
         dStrcat(buf, "[");
         dStrcat(buf, gEvalState.stack[i]->scopeNamespace->mPackage);
         dStrcat(buf, "]");
      }
      if(gEvalState.stack[i]->scopeNamespace && gEvalState.stack[i]->scopeNamespace->mName)
      {
         dStrcat(buf, gEvalState.stack[i]->scopeNamespace->mName);
         dStrcat(buf, "::");
      }
      if(gEvalState.stack[i]->scopeName)
          dStrcat(buf, gEvalState.stack[i]->scopeName);
   }
   Con::printf("BackTrace: %s", buf);
}

I've added if(gEvalState.stack[i]->scopeName) to check if it's there and not empty.
Also, it will print the used package... BUT!!! this stack contains wrong data about current package, or I'm retrieving this incorrectly.
See next post for details.

#1
07/13/2011 (7:11 am)
Using this code:
http://tinypaste.com/af4b3
With the trace enabled, it correctly shows where it enters/leaves, but backtrace information is incorrect (package usage):
// Calling doitRain(); from console:
   Entering [packageRain]doitRain()
      BackTrace: ->->doitRain
      Entering [packageDumb]doitDump()
         BackTrace: ->->doitRain->[packageRain]doitDump
         Entering [packageStar]doitStar()
            BackTrace: ->->doitRain->[packageRain]doitDump->[packageDumb]doitStar
            Entering [packageQwerty]doitQwerty()
               BackTrace: ->->doitRain->[packageRain]doitDump->[packageDumb]doitStar->[packageStar]doitQwerty
               Entering doitRoot()
                  BackTrace: ->->doitRain->[packageRain]doitDump->[packageDumb]doitStar->[packageStar]doitQwerty->[packageQwerty]doitRoot
               Leaving doitRoot() - return 
            Leaving [packageQwerty]doitQwerty() - return 
         Leaving [packageStar]doitStar() - return 
      Leaving [packageDumb]doitDump() - return 
   Leaving [packageRain]doitRain() - return
As you may see in the final backtrace, the function names are correct, while the package names are not in sync with function names and looks like "lagging" on one stack.

BackTrace: ->->doitRain->[packageRain]doitDump->[packageDumb]doitStar->[packageStar]doitQwerty->[packageQwerty]doitRoot

Should be:

BackTrace: ->->[packageRain]doitRain->[packageDumb]doitDump->[packageStar]doitStar->[packageQwerty]doitQwerty->doitRoot
#2
07/13/2011 (9:29 am)
Thanks Fyodor. Logged as THREED-2182.
#3
07/13/2011 (12:04 pm)
re: wrong package assignment
I've just checked the stock TGE, and the output is the same there:
BackTrace: ->doitRain->[packageRain]doitDump->[packageDumb]doitStar->[packageStar]doitQwerty->[packageQwerty]doitRoot
so, it is something deep in the internals of Torque.
#4
07/14/2011 (5:23 am)
Okay, looks like I've fixed it with a workaround.
I've replaced all
gEvalState.stack[i]->scopeNamespace->mPackage
with
gEvalState.stack[i]->scopeNamespace->mEntryList->mPackage
And now it displays correct package.
#5
07/21/2011 (2:20 pm)
Your fix seems to do the trick, thanks Bank!
#6
10/10/2011 (12:15 pm)
Fixed in 1.2