The debugger supports C, and C++ programming languages. Some the debugger features may be used in expressions regardless of the language you use: the debugger @ and :: operators, and the {type}addr construct (see Chapter 28.) can be used with the constructs of any supported language.
The following sections detail to what degree each source language is supported by the debugger. These sections are not meant to be language tutorials or references, but serve only as a reference guide to what the debugger expression parser accepts, and what input and output formats should look like for different languages. There are many good books written on each of these languages; please look to these for a language reference or tutorial.
The C++ debugging facilities are jointly implemented by the GNU C++ compiler and the debugger. Therefore, to debug your C++ code effectively, you must compile your C++ programs with the GNU C++ compiler, g++.
For best results when debugging C++ programs, use the stabs debugging format. You can select that format explicitly with the g++ command-line options -gstabs or -gstabs+. See Section 1.7, for more information.
Operators must be defined on values of specific types. For instance, + is defined on numbers, but not on structures. Operators are often defined on groups of types.
For the purposes of C and C++, the following definitions hold:
Integral types include int with any of its storage-class specifiers; char; and enum.
Floating-point types include float and double.
Pointer types include all types defined as (type *).
Scalar types include all of the above.
The following operators are supported. They are listed here in order of increasing precedence:
The comma or sequencing operator. Expressions in a comma-separated list are evaluated from left to right, with the result of the entire expression being the last expression evaluated.
Assignment. The value of an assignment expression is the value assigned. Defined on scalar types.
Used in an expression of the form a op= b, and translated to a = a op b. op= and = have the same precendence. op is any one of the operators |, ^, &, <<, >>, +, -, *, /, %.
The ternary operator. a ? b : c can be thought of as: if a then b else c. a should be of an integral type.
Logical or. Defined on integral types.
Logical and. Defined on integral types.
Bitwise or. Defined on integral types.
Bitwise exclusive-or. Defined on integral types.
Bitwise and. Defined on integral types.
Equality and inequality. Defined on scalar types. The value of these expressions is 0 for false and non-zero for true.
Less than, greater than, less than or equal, greater than or equal. Defined on scalar types. The value of these expressions is 0 for false and non-zero for true.
left shift, and right shift. Defined on integral types.
The the debugger “artificial array” operator (see Section 14.1.).
Addition and subtraction. Defined on integral types, floating-point types and pointer types.
Multiplication, division, and modulus. Multiplication and division are defined on integral and floating-point types. Modulus is defined on integral types.
Increment and decrement. When appearing before a variable, the operation is performed before the variable is used in an expression; when appearing after it, the variable's value is used before the operation takes place.
Pointer dereferencing. Defined on pointer types. Same precedence as ++.
Address operator. Defined on variables. Same precedence as ++.
For debugging C++, the debugger implements a use of & beyond what is allowed in the C++ language itself: you can use &(&ref) (or, if you prefer, simply &&ref) to examine the address where a C++ reference variable (declared with &ref) is stored.
Negative. Defined on integral and floating-point types. Same precedence as ++.
Logical negation. Defined on integral types. Same precedence as ++.
Bitwise complement operator. Defined on integral types. Same precedence as ++.
Structure member, and pointer-to-structure member. For convenience, the debugger regards the two as equivalent, choosing whether to dereference a pointer based on the stored type information. Defined on struct and union data.
Array indexing. a[i] is defined as *(a+i). Same precedence as ->.
Function parameter list. Same precedence as ->.
C++ scope resolution operator. Defined on struct, union, and class types.
Doubled colons also represent the debugger scope operator (see Chapter 28.). Same precedence as ::, above.
The debugger allows you to express the constants of C and C++ in the following ways:
Integer constants are a sequence of digits. Octal constants are specified by a leading 0 (i.e. zero), and hexadecimal constants by a leading 0x or 0X. Constants may also end with a letter l, specifying that the constant should be treated as a long value.
Floating point constants are a sequence of digits, followed by a decimal point, followed by a sequence of digits, and optionally followed by an exponent. An exponent is of the form: e[[+]|-]nnn, where nnn is another sequence of digits. The + is optional for positive exponents.
Enumerated constants consist of enumerated identifiers, or their integral equivalents.
Character constants are a single character surrounded by single quotes ('), or a number — the ordinal value of the corresponding character (usually its ASCII value). Within quotes, the single character may be represented by a letter or by escape sequences, which are of the form \nnn, where nnn is the octal representation of the character's ordinal value; or of the form \x, where x is a predefined special character — for example, \n for newline.
String constants are a sequence of character constants surrounded by double quotes (").
Pointer constants are an integral value. You can also write pointers to constants using the C operator &.
Array constants are comma-separated lists surrounded by braces { and }; for example, {1,2,3} is a three-element array of integers, {{1,2}, {3,4}, {5,6}} is a three-by-two array, and {&"hi", &"there", &"fred"} is a three-element array of pointers.
The debugger expression handling has a number of extensions to interpret a significant subset of C++ expressions.
Member function calls are allowed; you can use expressions like
count = aml->GetOriginal(x, y)
While a member function is active (in the selected stack frame), your expressions have the same namespace available as the member function; that is, the debugger allows implicit references to the class instance pointer this following the same rules as C++.
You can call overloaded functions; the debugger resolves the function call to the right definition, with one restriction — you must use arguments of the type required by the function that you want to call. The debugger does not perform conversions requiring constructors or user-defined type operators.
The debugger understands variables declared as C++ references; you can use them in expressions just as you do in C++ source — they are automatically dereferenced.
In the parameter list shown when the debugger displays a frame, the values of reference variables are not displayed (unlike other variables); this avoids clutter, since references are often used for large structures. The address of a reference variable is always shown, unless you have specified set print address off.
The debugger supports the C++ name resolution operator :: — your expressions can use it just as expressions in your program do. Since one scope may be defined in another, you can use :: repeatedly if necessary, for example in an expression like scope1::scope2::name. the debugger also allows resolving name scope by reference to source files, in both C and C++ debugging (see Section 14.2.).
If you allow the debugger to set type and range checking automatically, they both default to off whenever the working language changes to C or C++. This happens regardless of whether you or the debugger selects the working language.
If you allow the debugger to set the language automatically, it recognizes source files whose names end with .c, .C, or .cc, and when the debugger enters code compiled from one of these files, it sets the working language to C or C++. See Section 15.1.3, for further details.
The set print union and show print union commands apply to the union type. When set to on, any union that is inside a struct or class is also printed. Otherwise, it appears as {...}.
The @ operator aids in the debugging of dynamic arrays, formed with pointers and a memory allocation function. See Section 14.1.
Some of the debugger commands are particularly useful with C++, and some are designed specifically for use with C++. Here is a summary:
When you want a breakpoint in a function whose name is overloaded, the debugger breakpoint menus help you specify which function definition you want. See Section 11.1.7.
Setting breakpoints using regular expressions is helpful for setting breakpoints on overloaded functions that are not members of any special classes. See Section 11.1.1.
Debug C++ exception handling using these commands.
Print inheritance relationships as well as other information for type typename. See Chapter 16: Symbols.
Control whether C++ symbols display in their source form, both when displaying code as C++ source and when displaying disassemblies. See Section 14.7.
Choose whether to print derived (actual) or declared types of objects. See Section 14.7.
Control the format for printing virtual function tables. See Section 14.7.
You can specify a particular definition of an overloaded symbol, using the same notation that is used to declare such symbols in C++: type symbol(types) rather than just symbol. You can also use the debugger command-line word completion facilities to list the available choices, or to finish the type list for you. See Command completion: Completion, for details on how to do this.