Debugging a .NET application

After you have deployed a PowerBuilder Web Forms or Windows Forms application, you can debug it. To launch the application and open the debugger, right-click the target or project in the System Tree and select Debug from its pop-up menu. You can also select the Debug Project button or the Design>Debug Project menu item in the Project painter, or the Debug button in the PainterBar.

.NET debugger restrictions

The .NET debugger supports most features of the debugger for standard PowerBuilder applications, including expression evaluation and conditional breakpoints. It does not support the Objects in Memory view or variable breakpoints, which are not supported in .NET. Local variables that are declared but not used do not display in the Local Variables view in .NET targets.

Additional debugging restrictions include the following:

For information about standard PowerBuilder debugger features, see “Debugging an application” in the User’s Guide.

Release and Debug builds

On the General page in the Project painter, you can choose to build a release build or a debug build. If you choose a debug build, an extra file with the extension .PDB is generated in the output directory and additional information displays in the Output window. If you want to stop at breakpoints in your code, you must use a debug build. Select a release build when your application is ready to distribute to users.

DEBUG preprocessor symbol

You can also enable or disable the DEBUG preprocessor symbol. This is useful if you want to add code to your application to help you debug while testing the application. Although you do not typically enable the DEBUG symbol in a release build, if a problem is reported in a production application, you can redeploy the release build with the DEBUG symbol enabled to help determine the nature or location of the problem.

When the DEBUG symbol is enabled, code that is enclosed in a code block with the following format is parsed by the pb2cs code emitter:

#if defined DEBUG then
   /*debugging code*/
#else
   /* other action*/
#end if

NoteAdding breakpoints in a DEBUG block When you use the DEBUG symbol, you can add breakpoints in the DEBUG block only for lines of code that are not in an ELSE clause that removes the DEBUG condition. If you attempt to add a breakpoint in the ELSE clause, the debugger automatically switches the breakpoint to the last line of the clause defining the DEBUG condition.

In the previous pseudocode example, if you add a breakpoint to the comment line “/* other action*/”, the breakpoint automatically switches to the “/*debugging code*/” comment line.

Figure 15-1 shows the pop-up menu item that you can use to paste the #If Defined DEBUG Then template statement in the Script view.

Figure 15-1: Menu cascade for pasting a template into a script

The Paste Special cascading menu displays the Preprocessor menu item that opens a cascading submenu. The submenu includes the #If Defined DEBUG Then statement.

For more information about using preprocessor symbols, see “About conditional compilation”.

Attaching to a running Windows Forms process

For Windows Forms projects, you can start your deployed application from its executable file before starting the debugger, and then attach to the running process from the debugger. To attach to a process that is already running, select Run>Attach to .NET Process in the Project painter to open a dialog box from which you can select the process.

After you attach to the process, it starts running in the debugger and you can set breakpoints as you normally do. Select Run>Detach to detach from the process. This gives you more flexibility than simply using just-in-time (JIT) debugging.

Breaking into the debugger when an exception is thrown

When an application throws an exception while it is being debugged, the debugger sees the exception before the program has a chance to handle it.The debugger can allow the program to continue, or it can handle the exception. This is usually referred to as the debugger’s first chance to handle the exception. If the debugger does not handle the exception, the program sees the exception. If the program does not handle the exception, the debugger gets a second chance to handle it.

You can control whether the debugger handles first-chance exceptions in the Exception Setting dialog box. To open the dialog box, open the Debugger and select Exceptions from the Debug menu. By default, all exceptions inherit from their parent and all are set to Continue. Figure 15-2 shows the DWRuntimeError exception has been set to “Break into the debugger.”

Figure 15-2: Exception Setting dialog box

The Exception Setting dialog box shows an inheritance tree of system exceptions and allows you to select the action to take when a selected exception is thrown. In this picture, the action selected is to break into the debugger when a d w runtime error occurs.

When this exception is thrown, a dialog box displays so that you can choose whether to open the debugger or pass the exception to the program.