If you are asking, “How do I clear GDB?”, the answer depends on what you want to clear. In GDB, the clear command removes breakpoints at a particular source-code location. It does not erase everything in the debugger or clear the terminal window. To remove all breakpoints, use delete; to clean up the screen, use a terminal command such as shell clear or press Ctrl+L.
This guide explains how to clear one breakpoint, remove several or all breakpoints, temporarily disable them, and clear the GDB screen. It also covers common command mistakes so you can choose the right option without losing debugging settings you still need.
What Does “Clear” Mean in GDB?
GDB is the GNU Debugger, a tool for running a program under controlled conditions and inspecting its behavior. During a debugging session, you might set breakpoints to pause execution at a function, source line, or machine instruction. The GDB command clear removes breakpoints associated with a specified location.
The important distinction is that clear works with a location, not a breakpoint number. If you know the breakpoint’s number but want to remove that exact breakpoint, use delete instead. If you mean that the terminal display is cluttered, clearing the screen is a separate task.
Before changing your breakpoints, you can review them with:
(gdb) info breakpoints
GDB displays a numbered list that usually includes each breakpoint’s location, whether it is enabled, and other details. Checking this list first helps you confirm what you are about to remove or disable.
How to Clear a Breakpoint at a Specific Location
Use clear followed by the location where the breakpoint is set. For example, if a breakpoint is at line 42 of the current source file, enter:
(gdb) clear 42
To specify both the source file and line number, use a filename followed by a colon and the line number:
(gdb) clear parser.c:42
You can also clear breakpoints associated with a function by naming the function:
(gdb) clear calculate_total
GDB removes breakpoints that match the location you provide. If the location is ambiguous, or more than one breakpoint matches, check the output and then use info breakpoints to verify what remains. Source filenames and function names must be recognizable to the current debugging session, so make sure the program was compiled with suitable debugging information when you need source-level locations.
When you run clear without an argument, GDB attempts to remove a breakpoint at the next instruction to be executed in the selected stack frame. This can be useful when execution has stopped at the breakpoint you want to remove. However, it does not mean “clear every breakpoint.” If you want to remove all breakpoints, use the appropriate delete command.
How to Delete a Breakpoint by Number
Every breakpoint shown by info breakpoints has a number. Use delete followed by that number to remove a specific breakpoint. For example:
(gdb) delete 2
This removes breakpoint number 2, regardless of the location where it was set. It is often the clearest choice when you have several breakpoints in one function or source line and want to remove only one of them.
You can delete more than one numbered breakpoint in a single command by providing multiple numbers or a range, depending on your GDB version and command syntax. For instance, you can enter separate numbers like this:
(gdb) delete 2 4
After deleting breakpoints, run info breakpoints again to confirm the result. If a number is no longer listed, that breakpoint has been removed.
How to Clear All Breakpoints in GDB
To remove all breakpoints, enter:
(gdb) delete
GDB may ask you to confirm before deleting everything. Read the prompt carefully and respond as appropriate. In many GDB versions, you can make the command’s purpose more explicit with:
(gdb) delete breakpoints
Deleting all breakpoints is useful when you want to start a fresh debugging session or clear out temporary stops created during testing. Because the action affects every breakpoint, check info breakpoints first if you may want to keep any of them.
Removing breakpoints is different from restarting your program. A command such as run starts or restarts program execution, while delete changes your breakpoint list. If your goal is to restart the program but keep your breakpoints, use the run command and respond to any restart prompt without deleting the breakpoints.
How to Disable Breakpoints Without Removing Them
If you might need a breakpoint later, disable it instead of deleting it. A disabled breakpoint remains in GDB’s list but does not stop the program. To disable a particular breakpoint, use its number:
(gdb) disable 2
To disable all breakpoints, enter:
(gdb) disable
Use info breakpoints to check which entries are enabled. When you want to use a disabled breakpoint again, re-enable it with its number:
(gdb) enable 2
Disabling is a good option when you want to temporarily run past a breakpoint without losing its location or configuration. By contrast, delete removes the entry, so you will need to set it again if you want to use it later.
How to Clear the GDB Screen
If by “clear GDB” you mean clear the text shown in the terminal, the GDB clear command is not the one you need. GDB uses clear for breakpoints. To clear a terminal screen from inside GDB, try:
(gdb) shell clear
On some systems, the terminal command is clear; on others, particularly some Windows environments, it may not be available or may behave differently. You can also try pressing Ctrl+L in a terminal that supports readline. This typically redraws the screen and moves the current prompt into view, although it may not erase scrollback.
If you are using GDB inside an integrated development environment, such as an editor’s debugging panel, the terminal shortcut may not work. Use the interface’s clear-console or clear-output control instead. Clearing the display does not remove breakpoints, change program state, or erase GDB’s command history.
Common GDB “Clear” Mistakes
-
Using
clearto remove every breakpoint:cleartargets a source location. Usedeletewhen you intend to remove all breakpoints. -
Using a breakpoint number with
clear: Numbers identify breakpoint entries, not source locations. Usedelete 2to remove breakpoint 2. -
Deleting a breakpoint when you only want it inactive temporarily: Use
disableto keep it available for later. -
Expecting
clearto erase the terminal: Useshell clear,Ctrl+L, or your debugger interface’s console controls. -
Assuming a command affected the intended location: Check the result with
info breakpoints, especially if several breakpoints share a line or function.
GDB commands can also be shortened when the abbreviation is unambiguous, but beginners should generally type the full command. Writing delete, disable, or clear explicitly makes it easier to understand what a command will do and reduces the chance of removing a breakpoint by mistake.
A Quick Example: Clean Up and Keep Debugging
Suppose you set three breakpoints and want to remove one, temporarily turn off another, and leave the last one active. First, inspect the list:
(gdb) info breakpoints
If breakpoint 2 is no longer needed, delete it. If breakpoint 3 should remain available but should not interrupt the current run, disable it:
(gdb) delete 2
(gdb) disable 3
Check the list once more to confirm breakpoint 2 is gone and breakpoint 3 is disabled. You can later restore breakpoint 3 with enable 3. This approach gives you more control than deleting every breakpoint and recreating the ones you still need.
Conclusion
To clear a specific breakpoint in GDB, use clear with its source location, such as clear file.c:42. To remove a breakpoint by number, use delete followed by that number; to remove all breakpoints, use delete with no arguments. Use disable when you want to keep breakpoints but stop them from interrupting execution temporarily.
If you meant clearing the terminal rather than clearing breakpoints, try shell clear or Ctrl+L. When in doubt, run info breakpoints before and after making changes. That simple check helps ensure you have cleared exactly what you intended and can continue debugging with the right breakpoints in place.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.