Previous Next Contents Index Doc Set Home


Debugging A Program

4


Sun WorkShop provides an integrated debugging service that can run a program in a controlled fashion and to inspect the state of a stopped program. WorkShop gives you complete control of the dynamic execution of a program, including the collection of performance data.

You can determine where a program crashed, view the values of variables and expressions, set breakpoints in the code, and run and trace a program. In addition, machine-level and other commands are available to help you debug code. You can use standard dbx commands in the Dbx Commands window.

This chapter is organized into the following sections:

Debugging Features

page 27

Quick Start

page 29

Quick Mode

page 32


Debugging Features

When debugging in WorkShop, you have access to an extensive range of event management, process control, and data inspection features. You can:


Quick Start

WorkShop simplifies the debugging task by providing you with an intuitive, easy-to-use interface to ease the task.

The following shows the basic steps to follow when debugging a program. For more detailed information on any of the steps, see the online help for WorkShop.

Table  4-1 Task List: Debugging a Program 

Task
Description

Build the application

Compile the application using the -g or -g0 (zero) option, which instructs the compiler to generate debugging information during compilation.

Start debugging

1. Select a debugging state. Choose Debug Quick Mode to run a program normally, with the option of switching to debugging at any point.

Choose Debug Debug Mode to debug the program using the full functionality of the debugger.

For more information on Quick Mode, see "Using Quick Mode" later in this chapter.

2. If you just built your program, it is considered the current program and is the top menu item in the Debug menu picklist. To start debugging the current program, choose Debug path name/program name, or click the Debug button on the WorkShop main window tool bar.

3. If you want to debug a program other than the current program, load a new program using Debug New Program, attach to a process using Debug Attach Process, or debug a core file using Debug Load Core File.

4. Change program information such as arguments, the run directory, and environment variables.

5. Debug multiple processes at once using the Active Debugging Sessions dialog box to manage the sessions.

6. Run your program by clicking either the Start or Go button on the tool bar.

View code

1. Wait until your program stops, or interrupt execution by clicking the Interrupt button or pressing Control + Break.

2. In the editor window, step through your code one line at a time, through functions, around functions, or out of functions.

3. Continue executing your program by clicking Go or choosing Execute Go.

Set breakpoints

1. Open the Breakpoints window by choosing Windows Breakpoints or Execute Set Breakpoints.

2. Set breakpoints to force the debugger to stop execution. You can set simple breakpoints to stop at a line of code or in a procedure or function.

3. Set advanced breakpoints to break in C++ classes, track changes in data, break on a condition, break on special events, or create your own custom breakpoints.

4. Disable and re-enable breakpoints as you move through your program.

Trace code

1. In the Breakpoints window, set a trace handler so that you can view each line of source code as it is executed.

2. Add a filter to your trace to view just calls to a function, every member function of a name, every function in a class, or each exit from a function.

3. Control the speed of the trace using the Debugging Options dialog box.

Check values and data

1. Spot-check the value of expressions using the Data History pane of the Debugging window.

2. Select variables or expressions from the editor window, and click the Evaluate button or choose Data Evaluate Selected to find their value.

3. Copy a variable or expression into the Expressions field and evaluate it for value and type.

4. Copy a variable or expression into the Expressions field and click Display to open the Data Display window to monitor changes in its value.

5. While the program is stopped, change the value of a variable or expression using the Assign button.

Monitor data values

1. Use the Data Display window to watch the changes in the value of an expression while a program runs. Choose Windows Data Display.

2. Enter expressions in the window using the Display button in the Data History pane.

3. Follow changes in the value of an expression, compare current and previous values, view static information, and graph arrays.

Examine the call stack

1. View the call stack in the Stack pane of the Debugging window.

2. Navigate around the call stack using the Up and Down buttons, or click the hypertext link.

3. Remove the function you are stopped in from the stack by choosing Stack Pop.

4. Remove multiple frames by placing your cursor next to the frame you want to return to and choosing Stack Pop to Current Frame.

Debug multithreaded programs

1. When a multithreaded program is detected, the Threads pane on the Debugging window opens listing information about the threads in the program.

2. Click a thread in the Threads pane to view its context.

3. Hide and expose threads to help in managing them.

Customize debugging sessions

Set new defaults for debugging performance, output, language, and so on by choosing Debug Debugging Options.


Quick Mode

Quick Mode is a new debugging feature that allows you to run your program normally but keep debugging ready in the background to take over the process at any point. Think of Quick Mode like a safety net. If you fall--your program terminates abnormally--the Debugger is there to save the program before it core dumps, just like the net. If you decide you want to debug the program you are running, you can interrupt and turn control over to the Debugger.

If all you want to do is run your program as quickly as possible, but you are afraid you might still need some debugging, select Quick Mode. There may be a short delay while the Debugging window opens, but otherwise your program runs exactly as if you were running it from the shell. Only when you switch back to Debug mode through a termination or interrupt are the symbols for the program loaded, causing a delay.

By providing this rescue capability, Quick Mode reduces the guesswork when you are testing your program.

Advantages of Quick Mode

Quick Mode offers the following advantages:

When to Use Quick Mode

Use Quick Mode when you:

As you debug your program, you can toggle between Debug mode and Quick mode to take advantage of the best features of each.

How to Switch to Quick Mode

You can choose to run your program in Quick mode or Debug mode whenever you select a program to run or debug.

You can toggle between Quick mode and Debug mode for the current program from the Debug menu in the WorkShop main window or in the Debugging window.

If you want to change your defaults so that future programs automatically start in Quick mode, choose Debugging Options Debugging Behavior from the WorkShop main window.

Quick Mode Example

Suppose you made a change to a program and are confident that the change works. After rebuilding the program, you select Quick mode from the Debug menu, so that the program runs with minimal overhead. As this is the first time you have run or debugged a program since opening WorkShop, there is a slight delay as the Debugging window opens to provide the debugging "safety net."

Your program starts automatically and runs normally until it SEGVs.

Before the program can terminate and create a core dump, WorkShop switches into Debug mode and loads the symbols for your program. You now have access to the full debugging functionality of WorkShop and can debug the program as if you had started debugging in Debug mode. Eventually, after making many changes, fixing them, and continuing, you rebuild your program.

You again select Quick Mode before running the program.

This time, no initial pause is noticeable, but, your program appears to be stuck in an infinite loop. Pressing the Interrupt button stops the program and loads the debugging symbols. You can now view data values, set breakpoints, and do any other needed debugging actions to track down your bug.

Convinced the third time is the charm, you rebuild your program, re-enable Quick mode, and run the program again. Your program runs without a flaw, and you never see the Debugging window at all.


Previous Next Contents Index Doc Set Home