37

I want to see the stack trace in any function of my code, so i made somthing like this to call it and print the stack trace:

public function PrintStackTrace() {
    try {
        throw new Error('StackTrace');
    } catch (e:Error) {
        trace(e.getStackTrace());
    }
}

I like to know if there are other way to do this. In some place, the Error class creates the stack trace, but maybe it didn't do it with ActionScript 3.0 so maybe it's not posible, but i want to know.

Thanks!

Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83

8 Answers8

51

As far as I know, the only way to make the stack trace available to your own code is via the getStackTrace() method in the Error class, just like you're already doing. In response to the example in your question, though, I would mention that you don't actually have to throw the Error -- you can just create it and call the method on it:

var tempError:Error = new Error();
var stackTrace:String = tempError.getStackTrace();

Also, like the documentation says, this only works in the debug version of Flash Player, so you can wrap this functionality in an if-block that checks the value of Capabilities.isDebugger if you want.

hasseg
  • 6,787
  • 37
  • 41
8

From Flash Player 11.5 the stack traces are also available in the non-debugger versions of the players as well.

Glen Blanchard
  • 926
  • 8
  • 17
6

Use the Flex DeBugger (FDB) that comes with the Flex SDK. It's a command-line debugger that allows you to debug .swf, even ones online (if it's a debug version). It allows you to set break-points, print/change variables, and dump the stack, and does not require you to add any extra code. A very useful tool that you shouldn't be without!

The fdb options you will need are 'break' and to specify the class and line where you want execution to halt, and 'bt' or 'info stack' to give you a backtrace of the stack. You can also display almost everything about the application while it runs.

Joony
  • 4,498
  • 2
  • 30
  • 39
2

I've put together this little function:

public static function getStackTrace() : String
{
    var aStackTrace : Array = new Error().getStackTrace().split("\n");
    aStackTrace.shift();
    aStackTrace.shift();
    return "Stack trace: \n" + aStackTrace.join("\n");
}

I have this function in a custom "Debug" class I use with my apps when developing. The two shift() calls remove the first two lines: The first one is just the string "Error" and the second line refers to this function itself, so it's not useful. You can even remove the third line if you wish (it refers to the line where you place the call to the getStackTrace() function) by adding another shift() call, but I left it to serve as a starting point of the "stack trace".

OMA
  • 3,442
  • 2
  • 32
  • 39
2

@hasseg is right. You can also preserve the stacktrace information in release version (not debug) by providing the -compiler.verbose-stacktraces=true when compiling your SWF.

plam4u
  • 317
  • 1
  • 11
1
var tempError:Error = new Error();
var stackTrace:String = tempError.getStackTrace();

write this stackTrace string into any file so that you can see the logs of your program at run mode also. So you need not to run it in debugger mode only. Write it into uncaughtexception event of application, so it will execute lastly.

Qben
  • 2,617
  • 2
  • 24
  • 36
gaurav_gupta
  • 61
  • 1
  • 9
1

As of Flash 11.5, stack traces work in the release version of Flash.

However, that doesn't mean this is no longer an issue. If your application is set to use a compiler older than 11.5 in Flash Builder --> Project properties --> ActionScript Compiler, you won't have stack traces.

Additionally, on that same page you can see your AIR SDK version. If you're using v3.4 or older, you won't see stack traces. If this is your issue, all your developers should update their AIR SDK by following the instructions here.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
0

The getStackTrace method returns the stack trace only on the debug flash player (https://www.adobe.com/support/flashplayer/debug_downloads.html), on the release player returns null. Make sure you have the debug player installed and running.

The -compiler.verbose-stacktraces=true only adds the line number to the debug stack trace.

Sample test: https://gist.github.com/pipeno/03310d3d3cae61460ac6c590c4f355ed

Andrei Tofan
  • 116
  • 3