69

I want to execute the very simple command

print var1, var2, var3, var4 

in gdb to examine the values of the vars from time to time.

I don't want to use display because it clutters up my view.

How can I do this? Right now all I can do is:

p var1  
p var2  
p var3  
p var4  
5
  • 5
    Its amazing its 2016 and this issue is still present. Commented Feb 6, 2016 at 0:53
  • 6
    Now it's 2019. Still here. Happy new year! Commented Feb 7, 2019 at 9:24
  • 6
    Oof, now it's 2020 Commented Jun 15, 2020 at 18:22
  • 3
    This thread looks like youtube comments :D P.S. 2021 yeye... Commented Mar 27, 2021 at 17:53
  • Looks like 2015 saw the fix...what are you guys complaining about? Perhaps the end of 2021 will see the end of this comment thread. Commented Dec 10, 2021 at 14:01

5 Answers 5

71

You can simply do this

print {var1,var2,var3,var4}

This will do the job.

Sign up to request clarification or add additional context in comments.

2 Comments

I run into error array elements must all be the same size.
This is quick and easy, but only if they are all the same type, or you can cast them usefully to the same type.
53

Use the printf command. It's a bit of a hassle, but it gives good control over the formatting. From the command line:

(gdb) help printf
printf "printf format string", arg1, arg2, arg3, ..., argn
This is useful for formatted output in user-defined commands.

The format string is like in C (%d for normal size ints, %s for null terminated strings, etc.).

1 Comment

I like this answer. One niche problem with it is that it only supports up to 64-bit integers: That operation is not available on integers of more than 8 bytes. (GCC on AMD64 has emulated __int128_t on some platforms.) Of course, you can shift and cast.
32

Use Macros:

For example to continue to next break point and print

(gdb) define prm 

Type commands for definition of prm. End with a line saying just end.

>continue
>print var1
>print var2
>print var3
>end

(gdb) prm
$5 = 0
$6 = 10
$7 = -1

1 Comment

this just changed how I use gdb forever
2

There may be a simpler solution, but you might be able to put together something using GDB macros: http://www.ibm.com/developerworks/aix/library/au-gdb.html

Comments

0

Use printf with the %V format specifier.

printf "%V\n%V\n%V\n", var1, var2, var3

See https://sourceware.org/gdb/current/onlinedocs/gdb.html/Output.html#g_t_0025V-Format-Specifier

Additionally, printf supports a special ‘%V’ output format. This format prints the string representation of an expression just as GDB would produce with the standard print command (see Examining Data):

(gdb) print array
$1 = {0, 1, 2, 3, 4, 5}
(gdb) printf "Array is: %V\n", array
Array is: {0, 1, 2, 3, 4, 5}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.