# Debugging C++ ROS Node with GDB
This guide provides instructions for debugging a C++ ROS node using GDB (GNU Debugger). GDB allows you to set breakpoints, inspect variables, and step through your code to diagnose issues in your ROS node.
## Prerequisites
- Basic knowledge of ROS and C++
- GDB installed on your system
- ROS workspace containing your ROS package
## Steps
1. **Compile with Debug Symbols**
Ensure that your ROS package is compiled with debugging symbols enabled. Modify your `CMakeLists.txt` file to include the `-g` flag in the compiler options:
```cmake
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
```

2. **Clean and Rebuild**
Clean your ROS workspace and rebuild your ROS package to ensure that the debugging symbols are included in the executable:
```bash
cd /path/to/your/catkin_workspace
catkin clean -y
catkin_make
```
3. **Start GDB**
Start GDB and load your executable:
```bash
gdb /path/to/your/executable
```
In my case I am directily running a ros launch script so for me it is
```bash
roslaunch insulator_main launch_jetson.launch
```
Output:

4. **Set Breakpoints**
Set breakpoints in your code using the `break` command. For example, to set a breakpoint at line 269 of a file:
```bash
(gdb) break your_file.cpp:269
```
But since I had already set flags in my .launch file I did not need to specify the file name rather just line number. You may also do the same shown in the screenshot:

```bash
<node pkg="ai_modules" type="segmentation_inference" name="segmentation_node" output="screen" launch-prefix="gdb --args"/>
```
```bash
(gdb) break 269
```

5. **Run the Program**
Start your ROS node with GDB:
```bash
(gdb) run
```

6. **Debugging**
- When the program hits a breakpoint, you can use the `print` command to inspect variables:
```bash
(gdb) print your_variable
```
- Use `continue` to resume execution until the next breakpoint:
```bash
(gdb) continue
```

- Use `next` to execute the current line and stop at the next line:
```bash
(gdb) next
```

- Use `step` to step into function calls:
```bash
(gdb) step
```

7. **Exit GDB**
To exit GDB, use the `quit` command:
```bash
(gdb) quit
## Listing and Removing breakpoints
To remove a breakpoint in GDB, you can use the `delete` command followed by the breakpoint number. Here are the steps you can follow:
1. **List Breakpoints:**
First, list all the breakpoints to find the number of the breakpoint you want to remove. You can do this with the `info breakpoints` command:
```gdb
(gdb) info breakpoints
```
This command will show a list of all breakpoints set, along with their numbers.
2. **Delete a Breakpoint:**
Once you identify the breakpoint you want to remove, use the `delete` command followed by the breakpoint number:
```gdb
(gdb) delete <breakpoint_number>
```
Replace `<breakpoint_number>` with the actual number of the breakpoint you want to delete.
3. **Confirm Deletion (if prompted):**
GDB may ask you to confirm the deletion of the breakpoint. You can confirm by typing `y` and pressing Enter.
If you want to delete all breakpoints, you can use the `delete` command without specifying a number:
```gdb
(gdb) delete
```
GDB will then ask if you are sure you want to delete all breakpoints.
4. **Verify Breakpoints are Deleted:**
To ensure that the breakpoint has been successfully removed, list the breakpoints again:
```gdb
(gdb) info breakpoints
```
This will show you the current list of breakpoints, and the one you deleted should no longer appear.