Debugging with GDB Manual HP WDB v6.3 (5900-2180, August 2012)

9 Using GDB with Different Languages
Although programming languages generally have common aspects, they are rarely expressed in
the same manner. For instance, in ANSI C, dereferencing a pointer p is accomplished by *p, but
in Modula-2, it is accomplished by p^. Values can also be represented (and displayed) differently.
Hex numbers in C appear as '0x1ae', while in Modula-2 they appear as '1AEH'.
Language-specific information is built into GDB for some languages, allowing you to express
operations like the above in the native language of your program, and allowing GDB to output
values in a manner consistent with the syntax of the native language. The language you use to
build expressions is called the working language.
9.1 Switching between source languages
There are two ways to control the working language. You can have GDB set it automatically, or
you can select it manually. You can use the set language command for either purpose. On
startup, GDB sets the default language automatically. The working language is used to determine
how expressions are interpreted, how values are printed, and so on.
In addition to the working language, every source file that GDB knows about has its own working
language. For some object file formats, the compiler might indicate which language a particular
source file is in. However, most of the time GDB infers the language from the name of the file. The
language of a source file controls whether C++ names are demangled―this way backtrace can
show each frame appropriately for its own language. There is no way to set the language of a
source file from within GDB, but you can set the language associated with a filename extension.
See “Displaying the language” (page 78).
This is a common problem when you use a program, such as cfront or f2c, that generates C
but is written in another language. In that case, make the program use #line directives in its C
output; that way GDB will know the correct language of the source code of the original program,
and will display that source code, not the generated C code.
9.1.1 List of filename extensions and languages
If a source file name ends in one of the following extensions, then GDB infers that its language is
the one indicated.
'.c' C source file
'.C',
'.cc',
C++ source file
'.cp',
'.cpp',
'.cxx',
'.c++'
'.f',
'.F',
'.f90'
Fortran source file. GDB does not distinguish between Fortran 77 and Fortran 90 files.
'.s',
'.S'
Assembler source file. This actually behaves almost like C, but GDB does not skip over
function prologues when stepping.
In addition, you may set the language associated with a filename extension. See “Displaying the
language” (page 78).
9.1.2 Setting the working language
If you allow GDB to set the language automatically, expressions are interpreted the same way in
your debugging session and your program.
9.1 Switching between source languages 77