Special Expressions
GDB understands the syntax (types, operators, operator
precedence) of the language being debugged. You can use the same
syntax to enter expressions as you do to modify GDB convenience
variables (such $i++). GDB also
understands several special syntaxes that let you do things that are
not in the target language, as follows:
- Array constants
You can create an array constant in the debuggee’s memory by enclosing a list of element values in braces. For example,
{ 1, 2, 3, 42, 57 }.- Array operator
The
@array operator prints all the elements of an array up to a given subscript. For example, if your program usesmalloc()to allocate memory:double *vals = malloc(count * sizeof(double));
you can print a single element using regular subscripting:
(gdb)
print vals[3]$1 = 9However, you can access
vals[0]throughvals[2]with:(gdb)
print *vals@3$2 = {0, 1, 4}- File resolution
If you use the same variable name in several source files (for example, each one is
static), you can specify which one you mean usingfile::variable. For example:(gdb)
print 'main.c'::errcount$2 = 0It is necessary to put
main.cin single quotes to avoid ambiguity with the C++::operator.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access