Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 21, 2022 04:59 pm GMT

[GDB-Quick] Prints No Need to do "std::cout" and compile again

For debugging, most obvious way to start is adding print.
When we have gdb, we dont need that (implied software built is in debug mode).
Lets see alternate ways to do that in gdb.

Printing Variable at some line.

We have breakpoints at hand. when breakpoint hits we can print variable values required or message needed (like which if-else branch taken (-q), _()/ ).

(gdb) break source.cpp:50  <enter>(gdb) command <enter>Type commands for breakpoint(s) 1, one per line.End with a line saying just "end".>print "hello World!" >end

When breakpoint hits on source file source.cpp at line 50 It prints "hello World!". No need to edit code or recompile it!

On breakpoint, we specify series of commands that need to process. it can be made simple as printing single variable to printing complete data-structure.

Same can be done with dprintf breakpoint and print, however first one provides more flexible and easy to use formatting. I hate dprintf!

Print but more efficient

Our first command of use is

(gdb) print a(gdb) p a

This is fine, even if we have array.
At times we are interested in buffer contents irrespective of its data structure, treating it like memory area.

(gdb) print &a@10 

Above command accepts address and 10 further locations from it. try a@10 and see how cool that feature becomes!

same with different formatting, printing 20('n'umber) 'f'ormat 'w'ords with address a

(gdb) x/20fw &a

We also want to check same variable at different breakpoints

display <var_name>

is easiest way than doing p everytime.

HTH, Happy Hacking!


Original Link: https://dev.to/maheshattarde/gdb-quicks-print-info-no-need-to-do-stdcout-and-compile-again-52lc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To