r/cpp_questions • u/BigRecognition2591 • 5d ago
OPEN tool and ways to debug c++
as the title says, what are good practices and tools to debug c++? I am using already GBD but i dont like the terminal approach and the the amount of stuff i have to type, especially for larger code
7
Upvotes
9
u/ninja-dragon 5d ago
Gdb plus vscode. Works really well. If you are using visual studio. Then the built in debugger is really good
6
u/manni66 5d ago
Depends on the operating system.
gdb can be used in TUI mode. Many IDEs provide a graphical interface to gdb.
5
u/CounterSilly3999 5d ago
QtCreator
3
u/Usual_Office_1740 5d ago
I write unit tests as much as possible. I catch more mistakes stepping through code with a debugger once or twice than I ever wood staring at the code or even talking through it.
I use pwngdb to do my debugging. It's a gdb plugin similar to the tui interface. It's website goes on about it being a hacking and reverse engineering tool but it doesn't have to be. You can configure it's layout to be whatever you want. I can monitor Registers, the call stack, the source code and the assemly while I'm stepping through the code. It handles pretty printing and stringifying and I did almost nothing to configure it to do so. I would recommend it to anyone that wants more info from gdb than the native tui gives by default.
My gdb setup allows me to do must of my debugging with -O2 on debug builds but I do also keep a cmake preset without optimizations that I can drop down into.
I haven't used it yet but C++ 26 gave us std::observable_checkpoint(). Jason Turner does a better job of going over how useful it can be than I could. Google said it was C++Weekly episode 555 if you're interested. Keeping the compiler from optimizing something away and giving you a breakpointable line can be very handy. I see myself using it from time to time instead of print debugging.
I do use print debugging sometimes. It has It's use cases.
3
u/Scotty_Bravo 5d ago
I like qtcreator for graphical debugging.
5
u/Thesorus 5d ago
What platform/OS are you on ?
Maybe check Jetbrains' CLion IDE
Or the free Visual Studio Community Edition if on Windows.
2
u/the_poope 5d ago
There's TUI mode for gdb, but why not just use the GUI interface in your editor/IDE? Every major editor/IDE supports this, both VS Code, Visual Studio, CLion, QtCreator, Kdevelop, Code::Blocks and you can find debugger plugins for Vim and Emacs.
Besides that I recommend looking into linters like clang-tidy, static analysis like cppcheck and runtime analyzers like ASAN, UBSAN & co
1
u/jedwardsol 5d ago
If you're on Windows, building with g++ and don't want to install visual studio then there is a standalone GUI debugger called
windbg.There's the old-school version : https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/ and an app version you can install from the Windows store.
You will need to convert g++'s debug symbols to the pdb format that MSVS & windbg use.
1
u/Lnk1010 5d ago
You can write scripts and then run them with "source" from the gdb prompt. What specifically do you find yourself writing often?
1
u/teerre 5d ago
Gdb is good, but there are many more tools. Dtrace (or alternatives), Valgrind, ptrace, strace, opentelemetry (or similar), variety of other sanitisers, compile time checks and several others (specially in specific domains like gpu or network)
1
u/epasveer 5d ago
SeerGdb.
1
u/DDDDarky 5d ago
Clicking "Start debugging" in VS.
1
u/RealisticDuck1957 5d ago
On linux (you didn't mention the OS for your dev computer) there are GUI wrappers for gdb, such as ddd (data display debugger).
Also unit tests to debug smaller components can not be encouraged too much.
1
u/ZakMan1421 4d ago
As many have said, most full IDEs have a built in debugger that allows you to step through code line by line and jump to breakpoints that you set.
Valgrind can be useful to help diagnose memory leaks (but realistically, if you have a memory leak there's likely something wrong with your design as a whole).
While not exactly debugging, perf can be nice for profiling if you have optimization issues.
1
u/Discordbothost 3d ago
Save the arguments that reproduce your bug in a VS Code launch profile so F5 reruns the same case. If the bug only shows up on one loop iteration or input record, right-click the breakpoint and give it a condition like i == 42. You can jump straight to that case and inspect the locals and call stack from there.
1
u/CAPTAINCAPSLOCK111 1d ago
Besides the GUI suggestions, get familiar with watchpoints. A breakpoint stops when execution reaches a line; a watchpoint can stop when a particular variable changes. If a value is correct early on and mysteriously wrong later, that can save loads of stepping. Start watching it while it’s still correct, then continue. Check its lifetime too—you don’t want to keep watching an address after the original object is gone.
1
u/CAPTAINCAPSLOCK111 15h ago
Which debugger and operating system are you using? If you have one variable that changes unexpectedly, we can use that as a small example instead of trying to configure everything at once.
1
u/debamitro 13h ago
Avoid debugging like the plague. That would be my suggestion from my experience. Try to shift left as much as possible, i.e., use strict warnings, lint, code review etc
1
u/bert8128 5d ago
Consider Visual Studio (not code) if you are on windows.