3 <title>GDB Internals
</title>
4 <meta http-equiv=
"Content-Type" content=
"text/html">
5 <meta name=
"description" content=
"GDB Internals">
6 <meta name=
"generator" content=
"makeinfo 4.3">
7 <link href=
"http://www.gnu.org/software/texinfo/" rel=
"generator-home">
12 Node:
<a name=
"Language%20Support">Language Support
</a>,
13 Next:
<a rel=
"next" accesskey=
"n" href=
"Host-Definition.html#Host%20Definition">Host Definition
</a>,
14 Previous:
<a rel=
"previous" accesskey=
"p" href=
"Symbol-Handling.html#Symbol%20Handling">Symbol Handling
</a>,
15 Up:
<a rel=
"up" accesskey=
"u" href=
"index.html#Top">Top
</a>
19 <h2 class=
"chapter">Language Support
</h2>
21 GDB's language support is mainly driven by the symbol reader,
22 although it is possible for the user to set the source language
25 GDB chooses the source language by looking at the extension
26 of the file recorded in the debug info;
<code>.c
</code> means C,
<code>.f
</code>
27 means Fortran, etc. It may also use a special-purpose language
28 identifier if the debug format supports it, like with DWARF.
30 <h3 class=
"section">Adding a Source Language to GDB
</h3>
32 <p>To add other languages to GDB's expression parser, follow the
36 <dt><em>Create the expression parser.
</em>
38 This should reside in a file
<code></code><var>lang
</var><code>-exp.y
</code>. Routines for
39 building parsed expressions into a
<code>union exp_element
</code> list are in
42 <p>Since we can't depend upon everyone having Bison, and YACC produces
43 parsers that define a bunch of global names, the following lines
44 <strong>must
</strong> be included at the top of the YACC parser, to prevent the
45 various parsers from defining the same global names:
47 <pre class=
"example"> #define yyparse
<var>lang
</var>_parse
48 #define yylex
<var>lang
</var>_lex
49 #define yyerror
<var>lang
</var>_error
50 #define yylval
<var>lang
</var>_lval
51 #define yychar
<var>lang
</var>_char
52 #define yydebug
<var>lang
</var>_debug
53 #define yypact
<var>lang
</var>_pact
54 #define yyr1
<var>lang
</var>_r1
55 #define yyr2
<var>lang
</var>_r2
56 #define yydef
<var>lang
</var>_def
57 #define yychk
<var>lang
</var>_chk
58 #define yypgo
<var>lang
</var>_pgo
59 #define yyact
<var>lang
</var>_act
60 #define yyexca
<var>lang
</var>_exca
61 #define yyerrflag
<var>lang
</var>_errflag
62 #define yynerrs
<var>lang
</var>_nerrs
65 <p>At the bottom of your parser, define a
<code>struct language_defn
</code> and
66 initialize it with the right values for your language. Define an
67 <code>initialize_
</code><var>lang
</var><code></code> routine and have it call
68 <code>add_language(
</code><var>lang
</var><code>_language_defn)
</code> to tell the rest of GDB
69 that your language exists. You'll need some other supporting variables
70 and functions, which will be used via pointers from your
71 <code></code><var>lang
</var><code>_language_defn
</code>. See the declaration of
<code>struct
72 language_defn
</code> in
<code>language.h
</code>, and the other
<code>*-exp.y
</code> files,
75 <br><dt><em>Add any evaluation routines, if necessary
</em>
77 If you need new opcodes (that represent the operations of the language),
78 add them to the enumerated type in
<code>expression.h
</code>. Add support
79 code for these operations in the
<code>evaluate_subexp
</code> function
80 defined in the file
<code>eval.c
</code>. Add cases
81 for new opcodes in two functions from
<code>parse.c
</code>:
82 <code>prefixify_subexp
</code> and
<code>length_of_subexp
</code>. These compute
83 the number of
<code>exp_element
</code>s that a given operation takes up.
85 <br><dt><em>Update some existing code
</em>
87 Add an enumerated identifier for your language to the enumerated type
88 <code>enum language
</code> in
<code>defs.h
</code>.
90 <p>Update the routines in
<code>language.c
</code> so your language is included.
91 These routines include type predicates and such, which (in some cases)
92 are language dependent. If your language does not appear in the switch
93 statement, an error is reported.
95 <p>Also included in
<code>language.c
</code> is the code that updates the variable
96 <code>current_language
</code>, and the routines that translate the
97 <code>language_
</code><var>lang
</var><code></code> enumerated identifier into a printable
100 <p>Update the function
<code>_initialize_language
</code> to include your
101 language. This function picks the default language upon startup, so is
102 dependent upon which languages that GDB is built for.
104 <p>Update
<code>allocate_symtab
</code> in
<code>symfile.c
</code> and/or symbol-reading
105 code so that the language of each symtab (source file) is set properly.
106 This is used to determine the language to use at each stack frame level.
107 Currently, the language is set based upon the extension of the source
108 file. If the language can be better inferred from the symbol
109 information, please set the language of the symtab in the symbol-reading
112 <p>Add helper code to
<code>print_subexp
</code> (in
<code>expprint.c
</code>) to handle any new
113 expression opcodes you have added to
<code>expression.h
</code>. Also, add the
114 printed representations of your operators to
<code>op_print_tab
</code>.
116 <br><dt><em>Add a place of call
</em>
118 Add a call to
<code></code><var>lang
</var><code>_parse()
</code> and
<code></code><var>lang
</var><code>_error
</code> in
119 <code>parse_exp_1
</code> (defined in
<code>parse.c
</code>).
121 <br><dt><em>Use macros to trim code
</em>
123 The user has the option of building GDB for some or all of the
124 languages. If the user decides to build GDB for the language
125 <var>lang
</var>, then every file dependent on
<code>language.h
</code> will have the
126 macro
<code>_LANG_
</code><var>lang
</var><code></code> defined in it. Use
<code>#ifdef
</code>s to
127 leave out large routines that the user won't need if he or she is not
130 <p>Note that you do not need to do this in your YACC parser, since if GDB
131 is not build for
<var>lang
</var>, then
<code></code><var>lang
</var><code>-exp.tab.o
</code> (the
132 compiled form of your parser) is not linked into GDB at all.
134 <p>See the file
<code>configure.in
</code> for how GDB is configured
135 for different languages.
137 <br><dt><em>Edit
</em><code>Makefile.in
</code><em></em>
139 Add dependencies in
<code>Makefile.in
</code>. Make sure you update the macro
140 variables such as
<code>HFILES
</code> and
<code>OBJS
</code>, otherwise your code may
141 not get linked in, or, worse yet, it may not get
<code>tar
</code>red into the