Using Debugger And Print To Debug Your ActionScript Under Linux

 

In previous article Using Actionscript 3 to develop Adobe Flash Content On Linux(Chinese Version), we have tried to use Adobe Flex 3.0 SDK developed “hello actionscript 3″ under Linux successfully. Compared with Windows programmer, the debug for Linux programmer is more complex. Especially for these user only use the Adobe Flex 3.0 SDK, it is more difficult. But it doesn’t mean the weak function. On the contrary when you get used to Linux gdb (Flex 3.0 SDK fdg), you will find that it could following your own thinking to debugging program under Linux. Which different from the windows, you need to following the result of debugging program. Then, less nonsense. Let’s begin our travel of Debug under Linux immediately.

The way of Linux Debug

There are generally two ways as following, we will introducing them individually: “Use debugger” and “The most primitive way: print”.

 

 

?Use debugger

The use of debugger which is the way using the tool fdb in Adobe Flex 3.0 SDK to debug our program.

?Installation

You should have installed flashplayer in you system. It have been include in the Adobe Flex 3.0 SDK which we mentioned on Using Actionscript 3 to develop Adobe Flash Content On Linux.

Input following command for installation:

  1. $ tar xvfz flex/runtimes/player/lnx/flashplayer.tar.gz
  2. $ sudo cp flashplayer /usr/bin

?Direction of fdb

To use the fdb debugging the swf file, swf file must contain the information which do the debugging needed. Then when compile by mxmlc, we need add the option debug=true. The Makefile as following:

Download: Makefile

  1. MXMLC = /tmp/flex/bin/mxmlc
  2. MFLAGS = -debug=true
  3. TARGETS  = hello.swf
  4. all: $(TARGETS)
  5. clean:
  6.         $(RM) $(TARGETS)
  7. .SUFFIXES:      .as .swf
  8. .as.swf:
  9.         $(MXMLC) $(MFLAGS) $<

Then the compiled swf file will contain the information which debugging needed. Input the following command to open the swf file for debugging.

  1. $ fdb  hello.swf

Then following opation is as same as gdb.

?Help — “h”

?re-open — “c”

?set breakpoint — “b function-name”

?view source code — “list”

?view variable — “print variable”

We also could run fdb by emacs, the operation is:

?M-x gdb

?fdb hello.swf

The next step is as same as before.

?The way using print to debug

?use trace()

trace() is the function which output the debugging information for ActionScript. The use way as below:

  1. package {
  2.     import flash.display.*;
  3.     public class hello extends Sprite {
  4.         public function hello () {
  5.             trace(“hello”);
  6.         }
  7.     }
  8. }

If the upper code run under the flashplayer, should be:

  1. $ flashplayer hello.swf
  2. hello

Then output the hello. What more important and must to explain is trace() just effective on the debug version of player or plugin. Both general version and devloper(debug) version could be downloaded:

?Under Flex SDK runtimes

?http://www.adobe.com/support/flashplayer/downloads.html

But it is still more inconvenient if need intall the special debug version.

?Way of gearing with JavaScript

Besides the trace(), there is another way which using the JavaScript function to output by client browser from swf file.

  1. package {
  2.     import flash.display.*;
  3.     import flash.external.ExternalInterface;
  4.     public class hello2 extends Sprite {
  5.         public function hello2 () {
  6.             ExternalInterface.call(‘alert’, “message”);
  7.         }
  8.     }
  9. }

In fact it calls the alert function of JavaScript and show the “massage” by dialog box.

?The way use firebug

Since the JavaScript could do the debugging, of course we can use the firebug to debug it.

The detail of use way could reference firebug document. What need to be special explained is that if use firefbug it could format output as printf by function log(). For example:

  1. ExternalInterface.call(‘console.log’, “string is [%s]“,  “abcde”);

The following code is valuable part written on this basic.

Download: log.as

  1. package {
  2.   import flash.external.ExternalInterface;
  3.   import flash.utils.getQualifiedClassName;
  4.   public function log(… args):void {
  5.     var inspect:Function = function(arg:*, bracket:Boolean = true):String {
  6.         var className:String = getQualifiedClassName(arg);
  7.         var str:String;
  8.         switch(getQualifiedClassName(arg)) {
  9.             case ‘Array’:
  10.               var results:Array = [];
  11.               for (var i:uint = 0; i < arg.length; i++) {
  12.                   results.push(inspect(arg[i]));
  13.               }
  14.               if (bracket) {
  15.                 str = ‘[‘ + results.join(‘, ‘) + ‘]‘;
  16.               } else {
  17.                 str = results.join(’, ‘);
  18.               }
  19.               break;
  20.             case ‘int’:
  21.             case ‘uint’:
  22.             case ‘Number’:
  23.               str = arg.toString();
  24.               break;
  25.             case ‘String’:
  26.               str = arg;
  27.               break;
  28.             default:
  29.               str = ‘#<’ + className + ‘:’ + String(arg) + ‘>’;
  30.         }
  31.         return str;
  32.     }
  33.     var r:String = inspect(args, false);
  34.     trace(r)
  35.     ExternalInterface.call(’console.log’, r);
  36.   }
  37. }

Save as “log.as” and add in source-path of flex-config.xml.

  1. <source-path>
  2.         <path-element>/home/tei/flex2/libs</path-element>
  3. </source-path>

Then use the log() directly, for example:

  1. package {
  2.     import flash.display.*;
  3.     import flash.external.ExternalInterface;
  4.     public class hello extends Sprite {
  5.         public function hello () {
  6.             log(“hello”);
  7.         }
  8.     }
  9. }