Debugging C/C++ Projects Tutorial
Contributed by Ann
Rice and maintained by Susan Morgan
February 2013 [Revision number: V7.3-1]
The following short tutorial takes you through some of the features for
debugging a C or C++ project in NetBeans IDE.
To follow this tutorial, you need the following software and resources.
See the NetBeans IDE
Configuring the NetBeans IDE for C/C++/Fortran for
information on downloading and installing the required software.
Creating the Sample Project
The NetBeans IDE with C/C++ uses the gdb debugger and provides
numerous features for debugging your projects. To explore these features, you will use
the Quote project. If you have not already created this
project, do the following:
- Choose File > New Project.
- In the project wizard, expand the Samples category and the C/C++ subcategory.
- Select the Applications subcategory, then select the Quote
project. Click Next, then click Finish.
You can create and manipulate breakpoints in your code at any time
without starting a debugging session.
Creating and Removing a Line Breakpoint
In the Quote_1 project, open the quote.cc file.
- Set a line breakpoint by clicking in the left margin of the Source Editor
window next to line 171 (response = readChar("Enter disk module type: (S for single disks, R for RAID; Q - exit)",
'S');). The line
is highlighted in red to indicate that
the breakpoint is set.
- You could remove the breakpoint by clicking on the icon in the left margin.
- Choose Window > Debugging > Breakpoints to open the Breakpoints
window. Your line breakpoint is listed in the window.
Creating a Function Breakpoint
- Choose Debug > New Breakpoint (Ctrl+Shift+F8) to open the New Breakpoint
In the Breakpoint Type drop-down list, set the type to Function.
- Type the function name Customer::GetDiscount in the Function
Name text field. Click OK.
- Your function breakpoint is set and is added to the list in the
Starting a Debugging Session
When you start a debugging session, the IDE starts the gdb
debugger, then runs the application inside the debugger. The IDE automatically
opens the debugger
windows and prints debugger output to the Output window.
- Start a debugging session for the Quote_1 project by
right-clicking the project node and choosing Debug.
starts and the application
- Open the Sessions window by choosing Window > Debugging > Sessions.
The debugging session is shown in this window.
Inspecting the State of the Application
- The Quote_1 application prompts you for input.
- Enter a customer name after the "Enter customer name:" prompt.
The application stops at the function breakpoint you set earlier.
The Breakpoints window lists the two breakpoints you set earlier.
The green program counter arrow appears on top of the breakpoint icon of the function breakpoint.
- Open the Call Stack window by choosing Window > Debugging > Call Stack. The call stack shows three frames.
- Open the Variables window and note that one variable is displayed.
- Click the
button in the left column in the Variables tab to show Autos instead of local
- Click the expand icons to expand the structure.
- Click the Continue button in the toolbar.
function is executed, printing the customer discount to the External I/O
window. Then you are prompted for input.
- Enter the input in response to the prompts.
The program stops at the next
breakpoint, the line breakpoint you set earlier. In the
quote.cc file, the green program counter arrow appears on top
of the breakpoint icon indicating the line on which the program is
Variables tab and note the long list of variables.
- Click the Call
Stack tab and note that there is now one frame in the stack.
- Choose Window > Debugging > Registers.
The Registers window
opens, displaying the current contents of the registers.
- Choose Window > Debugging > Disassembly.
The Disassembly window
opens, displaying the assembly instructions for the current source file.
Click the Continue button in the toolbar and continue entering input in response to the
prompts in the Output window until the program is completed.
- When you
press Enter to exit the program, your
debug session ends. To end the debug session before the
execution of the program was complete, you could right-click the session in
the Sessions tab, and choose Finish, or choose Debug > Finish Debugger Session.