15.3. Supported Languages

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.

15.3.1. C and C++ operators

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:

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.

op=

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.

15.3.2. C and C++ Constants

The debugger allows you to express the constants of C and C++ in the following ways:

15.3.3. C++ Expressions

The debugger expression handling has a number of extensions to interpret a significant subset of C++ expressions.

  1. Member function calls are allowed; you can use expressions like

    count = aml->GetOriginal(x, y)
    
  2. 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++.

  3. 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.

  4. 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.

  5. 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.).

15.3.4. C and C++ Defaults

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.

15.3.5. The Debugger and C

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.

15.3.6. The Debugger Features for C++

Some of the debugger commands are particularly useful with C++, and some are designed specifically for use with C++. Here is a summary:

breakpoint menus

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.

rbreak regex

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.

catch exceptions, info catch

Debug C++ exception handling using these commands.

ptype typename

Print inheritance relationships as well as other information for type typename. See Chapter 16: Symbols.

set print demangle, show print demangle, set print asm-demangle, show print asm-demangle

Control whether C++ symbols display in their source form, both when displaying code as C++ source and when displaying disassemblies. See Section 14.7.

set print object, show print object

Choose whether to print derived (actual) or declared types of objects. See Section 14.7.

set print vtbl, show print vtbl

Control the format for printing virtual function tables. See Section 14.7.

Overloaded symbol names

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 Section 9.2: Completion, for details on how to do this.