blob: 0ea72e698e510f7824a4a7d48c153021bc679269 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= Debugging C/C++ Projects Tutorial
:jbake-type: tutorial
:jbake-tags: tutorials
:jbake-status: published
:icons: font
:syntax: true
:source-highlighter: pygments
:toc: left
:toc-title:
:description: Debugging C/C++ Projects Tutorial - Apache NetBeans
:keywords: Apache NetBeans, Tutorials, Debugging C/C++ Projects Tutorial
March 2013 [Revision number: V8.0-1]
The following short tutorial takes you through some of the features for debugging a C or C++ project in NetBeans IDE.
== Requirements
*To follow this tutorial, you need the following software and resources.*
|===
|Software or Resource |Version Required
|NetBeans IDE |link:https://netbeans.org/downloads/index.html[+version 7.4 or 8.0 with NetBeans C/C++ plugin module+]
|Java Developer Kit (JDK) |link:http://www.oracle.com/technetwork/java/javase/downloads/index.html[+version 7 or 8+]
|===
See the link:../../../community/releases/80/install.html[+NetBeans IDE Installation Instructions+] and link:../../../community/releases/80/cpp-setup-instructions.html[+ 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:
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 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.
image::images/line_breakpoint1.png[]
[start=3]
. You could remove the breakpoint by clicking on the icon in the left margin.
[start=4]
. Choose Window > Debugging > Breakpoints to open the Breakpoints window. Your line breakpoint is listed under the C/C++ node.
image::images/line_breakpoint2.png[]
[start=5]
. Expand the C/C++ node to see your breakpoint.
image::images/line_breakpoint2_expand.png[]
=== Creating a Function Breakpoint
1. Click the Create New Breakpoint button in the left margin of the Breakpoints window or choose Debug > New Breakpoint (Ctrl+Shift+F8) to open the New Breakpoint dialog box.
2. In the Debugger Type list, select Native.
3. In the Breakpoint Type list, select Function.
4. Type the function name ``Customer::GetDiscount`` in the Function Name text field. Click OK.
image::images/function_breakpoint1.png[]
[start=5]
. Your function breakpoint is set and is added to the list in the Breakpoints window.
image::images/function_breakpoint2.png[]
[start=6]
. Click the Select Breakpoint Groups button in the left margin to organize the breakpoints into different groups and select Type.
image::images/group_breakpoint_type.png[]
[start=7]
. Expand the File:Line and Function groups to display your breakpoints.
image::images/breakpoint_types.png[]
== 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 debugger toolbar and prints debugger output to the Output window.
1. Start a debugging session for the ``Quote_1`` project by clicking the Debug Project button image::images/debugbutton.png[] or by choosing Debug > Debug Project.
The debugger starts and the application runs. The debug toolbar is displayed.
image::images/debugger_toolbar.png[]
[start=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 Output (Debug) window.
2. Enter a customer name after the "Enter customer name:" prompt.
The application stops at the Customer::GetDiscount function breakpoint you set earlier and the customer.cc file opens in the editor at the line where the funtion is called. 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.
image::images/function_breakpoint3.png[]
[start=3]
. Click the tab for the Call Stack window or choose Window > Debugging > Call Stack. The call stack shows three frames.
image::images/call_stack.png[]
[start=4]
. Open the Variables window and note that one variable is displayed.
[start=5]
. Click the expand icons to expand the structure.
image::images/local_variables1.png[]
[start=6]
. Click the Continue button in the toolbar.
The ``GetDiscount`` function is executed, printing the customer discount to the Output (Debug) window. Then you are prompted for input.
[start=7]
. 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.
image::images/line_breakpoint3.png[]
[start=8]
. Click the Variables tab and note the long list of variables.
image::images/local_variables2.png[]
[start=9]
. Click the Call Stack tab and note that there is now one frame in the stack.
[start=10]
. Choose Window > Debugging > Registers.
The Registers window opens, displaying the current contents of the registers.
image::images/registers.png[]
[start=11]
. Choose Window > Debugging > Disassembly.
The Disassembly window opens, displaying the assembly instructions for the current source file.
image::images/disassembly.png[]
[start=12]
. 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.
[start=13]
. 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 click the Finish Debugger Session button in the toolbar or choose Debug > Finish Debugger Session.
== See Also
Please see the link:https://netbeans.org/kb/trails/cnd.html[+C/C++ Learning Trail+] for more articles about developing with C/C++/Fortran in NetBeans IDE.
link:mailto:users@cnd.netbeans.org?subject=Feedback:%20Debugging%20C/C++%20Projects%20-%20NetBeans%20IDE%207.4%20Tutorial[+Send Feedback on This Tutorial+]