Supported on windows
Supported on linux
Supported on android
print command
Prints the value of a given expression.
Syntax
print [Expression]
print $[Previous value number]
print {[Type]}[Address]
print [First element]@[Element count]
print /[Format] [Expression]
print $[Previous value number]
print {[Type]}[Address]
print [First element]@[Element count]
print /[Format] [Expression]
Parameters
- Expression
- Specifies the expression that will be evaluated and printed. The expression can include references to variables (e.g. i), registers (e.g. $eax) and pseudo-registers (e.g. $pc). Note that if your expression refers to a local variable, you need to ensure that you have selected the correct frame. You can navigate frames using the up and down commands.
- Previous value number
- When this format is used and i is specified as the previous value number, the print command will repeat the output produced by its i-th invocation.
- Type/Address
- This format allows explicitly specifying the address of the evaluated expression and can be used as a shortcut to the C/C++ type conversion. E.g. *((int *)p) is equivalent to {int}p
- First element/Element count
- This form allows interpreting the First element expression as an array of Element count sequential elements. The most common example of it is *argv@argc
- Format
- If specified, allows overriding the output format used by the command. Valid format specifiers are:
- o - octal
- x - hexadecimal
- u - unsigned decimal
- t - binary
- f - floating point
- a - address
- c - char
- s - string
Examples
We will demonstrate the print command using a basic C++ program that prints its own command-line arguments:
#include <stdio.h>
int main(int argc, char **argv)
{
for(int i = 0; i < argc; i++)
printf("Arg %d: %s\n", i, argv[i]);
return 0;
}
int main(int argc, char **argv)
{
for(int i = 0; i < argc; i++)
printf("Arg %d: %s\n", i, argv[i]);
return 0;
}
We will step into the loop and use several forms of print to show the values of various variables:
(gdb) set args arg1 arg2 arg3
(gdb) start
Temporary breakpoint 1 at 0x8048426: file test.c, line 5.
Starting program: /home/bazis/test arg1 arg2 arg3
Temporary breakpoint 1, main (argc=4, argv=0xbffff024) at test.c:5
warning: Source file is more recent than executable.
5 for(int i = 0; i < argc; i++)
(gdb) next
6 printf("Arg %d: %si, argv[i]);
(gdb) print i
$1 = 0
(gdb) print argv[i]
$2 = 0xbffff204 "/home/bazis/test"
(gdb) print /a argv[i]
$3 = 0xbffff204
(gdb) print /s argv[i]
$4 = 0xbffff204 "/home/bazis/test"
(gdb) print /c argv[i]
$5 = 4 '\004'
(gdb) print *argv
$6 = 0xbffff204 "/home/bazis/test"
(gdb) print *argv@argc
$7 = {0xbffff204 "/home/bazis/test", 0xbffff215 "arg1", 0xbffff21a "arg2", 0xbffff21f "arg3"}
(gdb) print {void *}argv@argc
$8 = {0xbffff204, 0xbffff215, 0xbffff21a, 0xbffff21f}
(gdb) print $2
$9 = 0xbffff204 "/home/bazis/test"
(gdb) next
Arg 0: /home/bazis/test
5 for(int i = 0; i < argc; i++)
(gdb) next
6 printf("Arg %d: %si, argv[i]);
(gdb) print $2
$10 = 0xbffff204 "/home/bazis/test"
(gdb) print argv[i]
$11 = 0xbffff215 "arg1"
(gdb) print /t argv[i]
$12 = 10111111111111111111001000010101
(gdb) start
Temporary breakpoint 1 at 0x8048426: file test.c, line 5.
Starting program: /home/bazis/test arg1 arg2 arg3
Temporary breakpoint 1, main (argc=4, argv=0xbffff024) at test.c:5
warning: Source file is more recent than executable.
5 for(int i = 0; i < argc; i++)
(gdb) next
6 printf("Arg %d: %si, argv[i]);
(gdb) print i
$1 = 0
(gdb) print argv[i]
$2 = 0xbffff204 "/home/bazis/test"
(gdb) print /a argv[i]
$3 = 0xbffff204
(gdb) print /s argv[i]
$4 = 0xbffff204 "/home/bazis/test"
(gdb) print /c argv[i]
$5 = 4 '\004'
(gdb) print *argv
$6 = 0xbffff204 "/home/bazis/test"
(gdb) print *argv@argc
$7 = {0xbffff204 "/home/bazis/test", 0xbffff215 "arg1", 0xbffff21a "arg2", 0xbffff21f "arg3"}
(gdb) print {void *}argv@argc
$8 = {0xbffff204, 0xbffff215, 0xbffff21a, 0xbffff21f}
(gdb) print $2
$9 = 0xbffff204 "/home/bazis/test"
(gdb) next
Arg 0: /home/bazis/test
5 for(int i = 0; i < argc; i++)
(gdb) next
6 printf("Arg %d: %si, argv[i]);
(gdb) print $2
$10 = 0xbffff204 "/home/bazis/test"
(gdb) print argv[i]
$11 = 0xbffff215 "arg1"
(gdb) print /t argv[i]
$12 = 10111111111111111111001000010101
Compatibility with VisualGDB
You can use the print command normally using the GDB Session window in Visual Studio.