Debugging Tricks

FlexBuilder 3 Beta 2 no longer makes both release and debug versions on every build, in order to speed up build times. This means that the debug version is no longer suffixed with -debug and it appears more than one of you used that to turn on debugging functionality w/o changing your code.
Also, a few folks have asked for a single line of code that could be pasted verbatim into function bodies that would report the name of the function, and a few others have wondered if there is a way to get the name of the calling function.
Assuming you are using a debugger player, the source code at this link demonstrates how to do each of these things. See setDebugFlag(), getFunctionName() and getCallingFunctionName().
Download file

private function setDebugFlag():void
{
var e:Error = new Error();
var s:String = e.getStackTrace();
// trace(s);
var i:int = s.indexOf("setDebugFlag");
if (s.charAt(i + 14) == '[')
debugMode = true;
}
[Bindable]
public var debugMode:Boolean = false;
private function getFunctionName(e:Error):String
{
var s:String = e.getStackTrace();
var i:int = s.indexOf("at ");
var j:int = s.indexOf("()");
return s.substring(i + 3, j);
}
private function getCallingFunctionName(e:Error):String
{
var s:String = e.getStackTrace();
// trace(s);
var i:int = s.indexOf("at ");
i = s.indexOf("at ", i + 3);
if (i == -1)
return "caller unknown";
var j:int = s.indexOf("()", i + 3);
return s.substring(i + 3, j);
}

where the latter two functions are can be used like this:

private function doSomething():void
{
trace(getFunctionName(new Error()));
doit();
}
private function doSomethingElse():void
{
trace(getFunctionName(new Error()));
doit();
}
private function doit():void
{
trace(getFunctionName(new Error()));
trace("   called by", getCallingFunctionName(new Error()));
}

Usual caveats apply, and the numbers and strings might have to be adjusted for different languages.

7 Responses to Debugging Tricks

  1. tksh says:

    I just wonder why you look at stacktrace instead of using flash.system.Capabilities.isDebugger, in order to detect user’s player edition.. Any good reason?
    ————-
    isDebugger just tells you if the player has the ability to stop at breakpoints, report unhandled exception, etc. It doesn’t tell you whether the swf has debug information like line numbers compiled in, which seemed to be what folks wanted to know

  2. Shigeru says:

    Are there any smarter way?
    Can I know which debug session is active or not?
    ————————-
    Not sure I understand the question. What do you mean by debug session? When would you have more than one?

  3. Shigeru says:

    I meant that debug-SWF is connecting to FlexBuilder debugger.
    —————
    I can’t think of a general way to know you’ve been connected to the debugger. The debugger tries to have a s little impact as possible. If you hit a breakpoint, the debugger will call all of the getters on the ‘this’ at the breakpoint, so I suppose you could always hit a particular breakpoint early in the app which would call the getter which could set the flag for everyone else to use.

  4. Clint Modien says:

    It may be worth noting that in order to get this to work in production… you need to turn on the -verbose-stacktraces=true compiler option.
    ——————–
    True, but that’ll fatten your swf in a significant way.

  5. Webdesign says:

    Many thanks for the usefully Informations.

  6. Ed Syrett says:

    There is one big caveat with the code above – it will throw an exception if you run it in the non-debug Flash Player.
    The reason is that getStackTrace() returns null when run in the non-debug player, and in all the code above the returned variable is used to call indexOf(). Obviously this will throw an exception.
    If you use this code, you’ll either have to take it out before you build a production version, use conditional compilation, or enclose the whole lot in a try/catch block.

  7. Oliver says:

    Great post, will have to research more on this
    I’ve also posted a simple example for finding the function name from outside the function
    http://flashontherocks.com/2010/03/12/getting-function-name-in-actionsctipt-3/