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

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

Ccode.

9.1.1List 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',

C++ source file

'.cc',

 

'.cp',

 

'.cpp',

 

9.1 Switching between source languages 101