corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

Debugging C/C++ Projects Tutorial

The following short tutorial takes you through some of the features for debugging a C or C++ project in NetBeans IDE 6.5.

Contents

Content on this page applies to NetBeans IDE 6.5
Requirements
Creating the Sample Project
Creating Breakpoints
Starting a Debugging Session
Inspecting the State of the Application

Requirements

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE version 6.5 with NetBeans C/C++ pack
Java Developer Kit (JDK) version 6 or version 5
Windows platform:
GNU C and C++ compilers, make, gdb

www.cygwin.com
www.mingw.org
Solaris platforms:
GNU C and C++ compilers
Sun C and C++ compilers
Solaris make and gmake
gdb

3.4.3
5.9 (Sun Studio 12 release)
3.80
6.6 or 6.8
Linux platforms:
Red Hat Fedora Core:
gcc and g++
gdb
GNU make

Ubuntu:
gcc and g++
gdb
GNU make

4.1.2
6.6 or 6.8
3.81


4.1.2
6.6 or 6.8
3.81
Mac OS X platforms:
gcc and g++
gdb
GNU make
Xcode bundle

4.0.1
6.3.50
3.80
6.5

See the NetBeans IDE 6.5 Installation Instructions and the Installing and Configuring C/C++ Support for information on downloading and installing the required software.

Creating the Sample Project

The NetBeans C/C++ Pack 6.5 uses the gdb debugger and provides numerous features for debugging your projects. To explore these features, we'll use the Quote project. If you have not already created this project, do the following:

  1. Choose File > New Project.
  2. In the project wizard, expand the Samples category and the C/C++ subcategory.
  3. Select the Applications subcategory, then select the Quote project. Click Next, then click Finish.

Creating Breakpoints

You can create and manipulate breakpoints in your code at any time without starting a debugging session.

Creating and Removing a Line Breakpoint
  1. In the Quote_1 project, open the quote.cc file.
  2. Set a line breakpoint by clicking in the left margin of the Source Editor window next to line 173 (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.
  3. You could remove the breakpoint by clicking on the icon in the left margin.
  4. Choose Window > Debugging > Breakpoints to open the Breakpoints window. Your line breakpoint is listed in the window.

Creating a Function Breakpoint

  1. Choose Debug > New Breakpoint (Ctrl+Shift+f8) to open the New Breakpoint dialog box.
  2. In the Breakpoint Type drop-down list, set the type to Function.
  3. Type the function name Customer::GetDiscount in the Function Name text field. Click OK.

  4. Screenshot of Add Breakpoint window

  5. Your function breakpoint is set and is added to the list in the Breakpoints window.

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.

  1. Start a debugging session for the Quote_1 project by right-clicking the project node and choosing Debug. The debugger starts and the application runs, and the Local Variables, Watches, and Call Stack windows open.

  2. Open the Sessions window by choosing Window > Debugging > Sessions. The debugging session is shown in this window.

Inspecting the State of the Application

  1. The Quote_1 application prompts you for input in the External I/O window.

  2. Enter a customer name after the "Enter customer name:" prompt.

  3. The application stops at the function breakpoint you set earlier. Open the Breakpoints window by choosing Window > Debugging > Breakpoints. The window lists the two breakpoints you set earlier. The function breakpoint is displayed in bold.

  4. Screenshot of Breakpoints tab

  5. Click the Call Stack tab. The call stack shows three frames.

  6. Screenshot of Call Stack tab

  7. Click the Local Variables tab and note that one variable is displayed. Click the expand icons (small boxes with plus signs) to expand the structure.

  8. Screenshot of
        Local Variables tab

  9. Click the Continue button. The GetDiscount function is executed, printing the customer discount to the External I/O window. Then you are prompted for input.

  10. 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 stopped.

    Screenshot of the Source
      Editor

  11. Click the Local Variables tab and note the long list of local variables.

  12. Screenshot of Local Variables tab

  13. Click the Call Stack tab and note that there is now one frame in the stack.
  14. Choose Window > Debugging > Threads. The Threads window opens showing two threads. The current thread is displayed in bold.

    Screenshot of Threads window

  15. Choose Window > Debugging > Registers. The Registers window opens, displaying the current contents of the registers.

    Screenshot of Registers window

  16. Choose Window > Debugging > Disassembler. The Disassembler window opens, displaying the assembly instructions for the current source file.

    Screenshot of Disassembler window

  17. Click the Continue button and continue entering input in response to the prompts in the External I/O window until the program is completed. When you enter Q to exit the program, the External I/O window closes and 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.
Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems