Contents|Index|Previous|Next

C and C++

Since C and C++ are so closely related, many features of GDB apply to both languages. Whenever this is the case, we discuss those languages together.

The C++ debugging facilities are jointly implemented by the GNU C++ compiler and GDB. 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 “Options for Debugging Your Program or GNU CC” in Using GNU CC in GNUPro Compiler Tools for more information.

C and C++ operators

Operators must be defined on values of specific types. For instance, + is defined on numbers and 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 previous types.

The following operators are supported. They are listed 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 opb. 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 GDB “artificial array” operator (see Expressions).

+, -
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++, GDB implements a use of ‘&’ beyond what is allowed in the C++ language itself: you can use ‘&(&ref)’ (or, if you prefer, ‘&&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, GDB 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 GDB scope operator (see Expressions). Same precedence as ::.

C and C++ constants

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

C++ expressions

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

Warning: GDB can only debug C++ code if you compile with the

GNU C++ compiler. Moreover, C++ debugging depends on the use of additional debugging information in the symbol table, and thus requires special support. GDB has this support only with the stabs debug format. In particular, if your compiler generates a.out, MIPS ECOFF, RS/6000 XCOFF, or ELF with stabs extensions to the symbol table, these facilities are all available. (With GNU CC, you can use the ‘-gstabs’ option to request stabs debugging extensions explicitly.) Where the object code format is standard COFF or DWARF in ELF , on the other hand, most of the C++ support in GDB does not work.

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, GDB allows implicit references to the class instance pointer, this, following the same rules as C++.

You can call overloaded functions; GDB 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. GDB does not perform conversions requiring constructors or user-defined type operators.

GDB 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 GDB 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’.

GDB 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’. GDB also allows resolving name scope by reference to source files, in both C and C++ debugging (see Program variables).

C and C++ defaults

If you allow GDB 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 GDB selects the working language.

If you allow GDB to set the language automatically, it recognizes source files whose names end with ‘.c’, ‘.C’, or ‘.cc’, and when GDB enters code compiled from one of these files, it sets the working language to C or C++. See Having GDB infer the source language for further details.

C and C++ type and range checks

By default, when GDB parses C or C++ expressions, type checking is not used. However, if you turn type checking on, GDB considers two variables type equivalent if:

The two variables are structured and have the same structure, union, or enumerated tag.

The two variables have the same type name, or types that have been declared equivalent through typedef.

Range checking, if turned on, is done on mathematical operations. Array indices are not checked, since they are often used to index a pointer that is not itself an array.

GDB 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 Expressions.

GDB features for C++

Some GDB commands are particularly useful with C++, and some are designed specifically for use with C++. The following is a summary:

breakpoint menus
When you want a breakpoint in a function whose name is overloaded, GDB breakpoint menus help you specify which function definition you want. See Breakpoint menus.

rbreak regex
Setting breakpoints using regular expressions is helpful for setting breakpoints on overloaded functions that are not members of any special classes. See Setting breakpoints.

catch exceptions

info catch
Debug C++ exception handling using these commands. See Breakpoints and exceptions.

ptype typename
Print inheritance relationships as well as other information for type typename. See Examining the Symbol Table.

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 Print settings.

set print object

show print object
Choose whether to print derived (actual) or declared types of objects. See Print settings.

set print vtbl

show print vtbl
Control the format for printing virtual function tables. See Print settings.

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 GDB command-line word completion facilities to list the available choices, or to finish the type list for you. See Command completion for details on how to perform this function.