Application Debugging: Chapter 18 - Programming Flex 3

by Chafic Kazoun and Joey Lott

This excerpt is from Programming Flex 3. If you want to try your hand at developing rich Internet applications with Adobe's Flex 3, and already have experience with frameworks such as .NET or Java, this is the ideal book to get you started. Programming Flex 3 gives you a solid understanding of Flex 3's core concepts, and valuable insight into how, why, and when to use specific Flex features. Learn to get the most from this amazing and sophisticated technology.

buy button

One of the strengths of Flex is its modern debugging capabilities. Debugging client-side code in web applications has traditionally been cumbersome. The Flash Debug Player, provided with Flex, allows developers the ability to debug applications in the same way they have been accustomed to with other modern development platforms.

In this chapter, we will cover runtime errors, debugging applications using FDB, debugging applications using the Flex Builder debugger, remote debugging, and tracing and logging.

Section 18.1: The Flash Debug Player

The Flash Debug Player is at the core of the debugging capabilities provided to Flex. The Debug Player provides several benefits specific to developers and is required for most types of debugging you will need to do. The browser plug-in and standalone editions of the Debug Player are included in the free SDK in the /runtimes/player folder, and in the <Path to Flex Builder 3>/Player folder if you are using Flex Builder 3. Also, if you installed Flex Builder, the Debug Player browser plug-in is typically installed during the install process. You can always check to ensure that you have the latest edition of the Debug Player by visiting http://www.adobe.com/support/flashplayer/downloads.html.

If you are unsure whether you have the Debug Player installed within a browser, you can build a simple application to check the version of the player installed. Here's an example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 <mx:Label text="Flash Player Version: {flash.system.Capabilities.version},
Debug Player: {flash.system.Capabilities.isDebugger}"/>
</mx:Application>

When you open the application in your browser, you should be presented with the version of Flash Player and whether it is the debug edition. flash.system.Capabilities is a class that Flash Player provides that allows you to retrieve information about the runtime environment in which an application is executing. In this example, we are checking the isDebugger property, which should return true if you have the Debug Player installed. You can also use this property to enable additional debugging type features of an application only when they are running within a Debug Player.

The Debug Player provides several additional capabilities on top of the traditional player, as we'll see in the next section. This functionality allows a developer access to the runtime behavior of a running application and is required for some debugging tools, including the command-line debugger and the Flex Builder debugger.

Warning: Running an application in the Debug Player, especially when it is being debugged, will impact runtime performance and can even impact application behavior. You should install the Debug Player only for debugging purposes and never in a production environment.

Runtime Errors

Flash Player 9 supports runtime type checking and exceptions capabilities that developers have become accustomed to in any modern runtime. Runtime errors when identified during the development process can help a great deal when debugging applications. Runtime errors are not presented to users with the non-Debug Player installed, but for development and testing purposes, you should have the Flash Debug Player installed (as should any team members who are involved in application testing). The Debug Player will display runtime errors by presenting you with a dialog as errors occur in your application. You may wonder why such errors are not presented to the user with the regular Flash Player. This is because Adobe silently hides such errors from regular users to minimize their impact on the application experience. The runtime errors still occur, but rather than interrupt the user with a dialog and halt the applications, Flash Player attempts to silently continue code execution. This does not guarantee that an application will always continue to run; some exceptions are fatal and will cause an application to halt. Because of this, it is not advisable to deploy any application that contains runtime exceptions that are not handled. This also does not guarantee that an application will respond as expected when a nonfatal exception occurs. In general, it is a good practice to properly handle exceptions to prevent unexpected results.

Note: A good practice on a team is to have all testers and developers install the Flash Debug Player. Doing so will allow you to catch runtime errors earlier in your development process.

If you execute this code in the Debug Player, you will receive a runtime error:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize=
"initializeHandler()">
    <mx:Script>
        <![CDATA[
            private function initializeHandler():void
            {
                var loader:Loader = new Loader();
                loader.load(new URLRequest("foo"));
            }
       ]]>
    </mx:Script>
</mx:Application>

The runtime error you'll receive is IOErrorEvent, as the example doesn't handle such an exception. Figure 18-1 shows the dialog that results when running Debug Player.

Example
Figure 18-1: Flash Debug Player error dialog

You can find a list of runtime error codes, along with descriptions, in the Flex Language Reference documentation, under Appendixes→Run-Time Errors.

The Debugging API

Although runtime errors are useful, often you will require more than just runtime errors to identify bugs. For such cases, the Flash Debug Player exposes an API for debuggers to interact with an application at runtime. This includes the ability to set breakpoints, step through code, set and retrieve variables at runtime, as well as other debugging-related tasks. Adobe provides two debuggers. One is the free FDB command-line debugger provided by the Flex SDK, and the other is the integrated GUI debugger that is part of Flex Builder. The debuggers communicate with the Flash Debug Player through a TCP socket connection. Typically, this happens on the same machine, but it is possible to also do remote debugging whereby one machine is the client and the other is running the debugger.

To allow the Debug Player to initiate a debug session, the application must be compiled with the debug data included within the SWF.

To do this, you need to set the compiler flag to true.

mxmlc -debug=true main.mxml

This flag generates a debug-enabled SWF. Although you may not experience any side effects from using a debug-enabled SWF for production, this is strongly discouraged because the -debug compiler flag produces larger SWF files and exposes the internals of an application. If a user has the Debug Player installed, he could inspect the internals of your application and even change client-side variable values. Later in this chapter, we will discuss how to use debug-enabled SWF files using various debuggers available today.

Using Show Redraw Regions

Even with modern hardware, you can run into rendering performance bottlenecks with graphics-intensive applications. Isolating such bottlenecks can be challenging, especially considering all the variables involved in how Flash Player renders content. For this reason, the Debug Player exposes an option called Show Redraw Regions.

When this option is enabled, the player will highlight areas of a running application that are being redrawn, which can help you in identifying graphical regions of an application that may be drawing inefficiently. You enable this option by selecting the Show Redraw Regions option from the Debug Player's context menu or through . To enable this option through ActionScript you can call the method of the flash.profiler package. This method works only with the Player and doesn't require a debug-enabled SWF. Here's a simple example that will allow you to experiment with how this feature works:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="flash.profiler.showRedrawRegions(true)">
    <mx:HSlider width="100%"/>
</mx:Application>

Compile and run this example in the Debug Player. Drag the slider to see how the Debug Player highlights what areas are being redrawn. This is especially helpful with Flex applications because the Flex framework provides a lot of functionality that you don't need to implement and often may not even know how it is implemented. Using the Show Redraw Regions feature can help you identify rendering bottlenecks in Flash Player.

Note: By default, the showRedrawRegions() method will highlight regions by drawing a blue rectangle outline around the regions being redrawn and the player context menu will use a red outline. However, sometimes you might find the default color difficult to identify. If so, you can specify your own color values by passing in a color value for the second parameter of the showRedrawRegions() method.

Section 18.2: Using FDB

As part of the Flex SDK, Adobe includes FDB, a free command-line debugger. This debugger is fully featured, although usually you will opt to use the Flex Builder debugger if available. With that said, it is great to have access to a free debugging tool included as part of the SDK. This allows developers who do not want to purchase Flex Builder access to a fully featured debugger. We won't be covering FDB in depth, but we will discuss the basics and some of the possible benefits it has to offer.

You launch FDB from the command line as you would any other command-line application. Once it is started, you will be prompted with the FDB prompt (fdb). You can type help at the prompt for a list of available commands.

The starting point for a debug session with FDB is to launch an .swf compiled with debugging enabled in the Debug Player and establish a connection with FDB. You do this by first executing the run command at the FDB prompt. Once the command is executed, FDB will confirm that it is waiting for the player to connect. To connect Flash Player to FDB, open a debug-enabled .swf with the Debug Player. When a debug- .swf is opened, the player will attempt to auto-connect to the local debugger, if available. If you open an application without the debugger listening for a connection from the player, the Flash Debug Player will prompt you to select the debugger you want to use. Although typically a user will be running the application and the debugger on the same machine, which means you may never receive the prompt requesting you to select the debugger, it is possible to initiate a remote debugging session. Remote debugging allows you to execute an application on a machine that is separate from the debugger, allowing you to debug problems that are reproducible on only certain machines, or even debug across platforms wherein a Mac OS X machine executes an application with the debugger running on a Windows machine. For this purpose, all debugging communication occurs through TCP on port 7935. We cover remote debugging later in this chapter.

Once a connection is established, you can set breakpoints or instruct the debugger to continue execution of the application. To continue execution you can issue the command. Application trace messages are shown in the debugger as they are encountered.

Breakpoints can be set using several methods. The most typical method of setting a breakpoint is to specify the class and line number for the breakpoint, which you can achieve by issuing the break command. For example, if you wanted to insert a breakpoint in line 56 of the class MainApp, you would input the command break MainApp.mxml:56. If you want a breakpoint when a button is pressed but you are not sure of the method that will be called when the event occurs, you could enter break button. This will return a list of methods that begin with the word button. From the list, you should see buttonPressed and the location of the method. With that information, you could set the breakpoint by calling break buttonPressed. Once you do that, you will need to call the command continue, which will tell the debugger to allow the player to continue executing the application. There are other methods of setting breakpoints with FDB, which you can find by issuing the command help breakpoint.

When an application is executing, the debugger will inform you when it encounters a breakpoint. Once a breakpoint is encountered, you have several options. You can issue the continue command, which will continue execution of the application, step through the application using the step command, and set the value of a variable using the set command. When done debugging, you can exit FDB to end the debugging session, which will automatically end the active connection with the Debug Player, or you can execute the kill command.

As discussed, FDB makes it easy to search for methods on which you want to set breakpoints. Some of the other nice features of FDB that Flex Builder's debugger doesn't support are the ability to set conditional breakpoints using the condition command, and the ability to review a list of all the loaded types and functions with the info functions command.

FDB can be a powerful tool and is worth exploring, but as you will see in the next section, Flex Builder provides a more practical method of application debugging, which you will likely opt for over FDB.

Section 18.3: Debugging with Flex Builder

One of the best selling points of Flex Builder is the integrated GUI debugger. FDB is free, but for day-to-day debugging, Flex Builder's debugger makes it much easier to debug applications.

A default Flex Builder installation (see Figure 18-2) will configure the tasks needed to get you up and running for debugging applications. To debug an application you are working on, you just need to select Run→Debug from the main menu (or press F11). This will compile the application if it is not already compiled, launch it within the browser, and connect the application to the debugger. The first time you debug an application you will be prompted to switch perspectives to the debugging perspective in Flex Builder, which usually is recommended.

Example
Figure 18-2: Default debugging perspective
Example
Figure 18-3: The Breakpoints panel

When in the debugging perspective, you will have access to the currently running application. Often you will set a breakpoint to stop the application at a point during the execution process in which you are interested. You can set breakpoints by double-clicking the left margin of a source line or by using the keyboard shortcut Ctrl-Shift-B. You can do this during a debug session or even before a session is started in the development perspective. You can navigate through a list of breakpoints by using the Breakpoints panel (see Figure 18-3). The Breakpoints panel contains a list of all breakpoints currently set and lets you navigate directly to a breakpoint or disable a breakpoint. This panel can also be very useful if you aren't sure what breakpoints you have set in your application as it allows you to view all breakpoints and manage them from one central location.

When Flash Player encounters a breakpoint, as in Figure 18-4, execution will halt, the debugger will gain focus, and the call stack will be displayed. You then will have the option of having the application continue execution, step through code, set variables, or evaluate expressions.

Example
Figure 18-4: Debugging view when a breakpoint is reached

Note: Breakpoints can be set on both ActionScript and MXML code. Breakpoints on MXML code are valid only when ActionScript event handlers exist. In cases where you set a breakpoint on an invalid MXML line, Flex Builder will attempt to find the nearest valid breakpoint. It does so by scanning the next 10 lines of execution for points where a breakpoint would be valid, and automatically moving the breakpoint to that location.

To help speed up the debugging process, Flex Builder offers the following default keyboard shortcuts for stepping through code:

  • Stepping into (F5)
  • Stepping over (F6)
  • Step return (F7)
  • Resume execution (F8)

While stepping through code, the debugger will have control over the player. At times, this may cause the player and browser to seem unresponsive. This is normal as you step through code. To suspend the debugger from having control until the next breakpoint, you have to tell the debugger to resume execution by clicking on the Play button or by using the F8 keyboard shortcut.

While debugging, you also have the ability to review values of variables. The Variables panel (see Figure 18-5) will list all object instances as well as their child properties and values. You can also review the values of variables right within the code editor by hovering above a variable within the code. This will display a tool tip with the current value of the variable. To change a value, click the Value column for the field that you wish to change, as shown in Figure 18-6, or right-click the property you wish to change and select Change Value.

Example
Figure 18-5: Changing a value in the Variables panel

Changing variables will often result in other variables changing as well. To give you insight into the changes that occur as a result of changing a variable, the Variables panel will highlight in yellow the affected variables in the panel.

Note: When debugging an application, you also have the ability to set breakpoints in and step through Flex framework code. Stepping through Flex framework code will happen automatically when encountered. To set breakpoints within the framework, you will need to open the class file. An easy way to do this is to use the shortcut Ctrl-Shift-T within Flex Builder and select the appropriate type. Once the class file is opened, you can set breakpoints as you would normally.

To end a debug session you can either close Flash Player or your browser, or click the red square within the Debug panel. Caution should be taken when closing an active debug session by closing the browser when it is executing within a browser, as sessions that are in a halted state can cause a web browser to close unexpectedly.

This book does not fully cover all the functionality possible with the debugger within Flex Builder. For full coverage, you can review the documentation provided with Flex Builder.

Section 18.4: Remote Debugging

When attempting to isolate a bug in an application, it is possible that you will encounter a case where a bug is reproducible on only a specific machine. For such a case, you can use the Flash Debug Player's remote debugging feature. This feature can also be useful if you would want to use the Flex Builder debugger on a Windows or Mac-based machine while the application is executed under Linux, which does not have a native version of Flex Builder available.

As mentioned earlier, debugging occurs over a TCP connection on port 7935. Such a connection is typically established on the same machine transparently, but with remote debugging it is established from one machine to another. One machine will typically be running the debugger and can be referred to as the server, while the other will be running the application and can be referred to as the client machine. Once a connection is established between the client and the server, all of the features of using a debugger function in the same manner. It's important to remember that in a typical workstation, the client machine may not be configured properly for remote debugging. Remember that the Flash Debug Player is required for a debug session, so you will need to ensure that it is installed on the client machine. It is also important to keep in mind that the client machine will need to be executing a debug-enabled SWF in the same manner as we discussed earlier in the chapter.

To initiate a debug session, follow these steps:

  1. Initialize the debugger on the server machine.
    1. With FDB: one benefit of using FDB when initializing a remote debugging session is that the steps for initializing FDB are exactly the same as those for initializing a local session. You initialize FDB by launching FDB and executing the run command.
    2. With Flex Builder's Debugger: initializing a remote debugging session with Flex Builder is more involved. Flex Builder's debugger doesn't formally support remote debugging, although it is possible. We will cover remote debugging with Flex Builder in the next section.
  2. Initialize the debug-enabled SWF on the client machine and ensure that the Debug Player is installed.
  3. Once initialized, the player will prompt you for a debugger, because the debugger is not running on the client machine. Input the IP address of the server running the debugger and select Connect.
  4. If the server is running the debugger and listening for a connection, the client and server will connect. Note here that you will also need to ensure that the server is not blocking port 7935.

Once a connection has been established, debugging can be performed in the manner discussed earlier in this chapter.

Establishing a Remote Debugging Session with the Flex Builder Debugger

As mentioned earlier, debugging sessions—both local and remote—are established over a TCP connection on port 7935. Flex Builder uses the same connection to establish debug sessions. Although by default Flex Builder does not expose remote debugging capabilities, it still is possible to do so.

To initiate a remote debug session with Flex Builder, follow these steps:

  1. Compiled a debug-enabled .swf.
  2. Copy the debug-enabled .swf to the remote client machine.
  3. On the machine that will run the Flex Builder debugger (server), create an empty HTML file (typically within your project's bin-debug folder). Since no content exists in an empty file, the Flex Builder debugger will launch a session that is empty and just wait for a remote connection.
  4. Ensure that no firewall is actively blocking port 7935 on the client machine.
  5. Edit the debug configuration within Flex Builder (select Run→Run).
  6. Select the target debug configuration.
  7. Uncheck the “Use defaults” checkbox.
  8. Click the Browse button for the debug file path.
  9. Select the blank HTML page created in step 3.
  10. Click Debug in Flex Builder. You should see “Launching {application name}” and a progress bar in the bottom status bar of Flex Builder (at this point, Flex Builder is waiting for a remote connection, in the same manner FDB does).
  11. Open the debug-enabled .swf on the remote machine and, when prompted for a remote host address, input the host address of the machine with the Flex Builder debugger.

Pages: 1, 2

Next Pagearrow