GDB (API)
|
00001 /* Interface to C preprocessor macro tables for GDB. 00002 Copyright (C) 2002-2013 Free Software Foundation, Inc. 00003 Contributed by Red Hat, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #ifndef MACROTAB_H 00021 #define MACROTAB_H 00022 00023 struct obstack; 00024 struct bcache; 00025 00026 /* How do we represent a source location? I mean, how should we 00027 represent them within GDB; the user wants to use all sorts of 00028 ambiguous abbreviations, like "break 32" and "break foo.c:32" 00029 ("foo.c" may have been #included into several compilation units), 00030 but what do we disambiguate those things to? 00031 00032 - Answer 1: "Filename and line number." (Or column number, if 00033 you're picky.) That's not quite good enough. For example, the 00034 same source file can be #included into several different 00035 compilation units --- which #inclusion do you mean? 00036 00037 - Answer 2: "Compilation unit, filename, and line number." This is 00038 a pretty good answer; GDB's `struct symtab_and_line' basically 00039 embodies this representation. But it's still ambiguous; what if a 00040 given compilation unit #includes the same file twice --- how can I 00041 set a breakpoint on line 12 of the fifth #inclusion of "foo.c"? 00042 00043 - Answer 3: "Compilation unit, chain of #inclusions, and line 00044 number." This is analogous to the way GCC reports errors in 00045 #include files: 00046 00047 $ gcc -c base.c 00048 In file included from header2.h:8, 00049 from header1.h:3, 00050 from base.c:5: 00051 header3.h:1: parse error before ')' token 00052 $ 00053 00054 GCC tells you exactly what path of #inclusions led you to the 00055 problem. It gives you complete information, in a way that the 00056 following would not: 00057 00058 $ gcc -c base.c 00059 header3.h:1: parse error before ')' token 00060 $ 00061 00062 Converting all of GDB to use this is a big task, and I'm not really 00063 suggesting it should be a priority. But this module's whole 00064 purpose is to maintain structures describing the macro expansion 00065 process, so I think it's appropriate for us to take a little care 00066 to do that in a complete fashion. 00067 00068 In this interface, the first line of a file is numbered 1, not 0. 00069 This is the same convention the rest of GDB uses. */ 00070 00071 00072 /* A table of all the macro definitions for a given compilation unit. */ 00073 struct macro_table; 00074 00075 /* The definition of a single macro. */ 00076 struct macro_definition; 00077 00078 /* A source file that participated in a compilation unit --- either a 00079 main file, or an #included file. If a file is #included more than 00080 once, the presence of the `included_from' and `included_at_line' 00081 members means that we need to make one instance of this structure 00082 for each #inclusion. Taken as a group, these structures form a 00083 tree mapping the #inclusions that contributed to the compilation 00084 unit, with the main source file as its root. 00085 00086 Beware --- not every source file mentioned in a compilation unit's 00087 symtab structures will appear in the #inclusion tree! As of Oct 00088 2002, GCC does record the effect of #line directives in the source 00089 line info, but not in macro info. This means that GDB's symtabs 00090 (built from the former, among other things) may mention filenames 00091 that the #inclusion tree (built from the latter) doesn't have any 00092 record of. See macroscope.c:sal_macro_scope for how to accomodate 00093 this. 00094 00095 It's worth noting that libcpp has a simpler way of representing all 00096 this, which we should consider switching to. It might even be 00097 suitable for ordinary non-macro line number info. 00098 00099 Suppose you take your main source file, and after each line 00100 containing an #include directive you insert the text of the 00101 #included file. The result is a big file that pretty much 00102 corresponds to the full text the compiler's going to see. There's 00103 a one-to-one correspondence between lines in the big file and 00104 per-inclusion lines in the source files. (Obviously, #include 00105 directives that are #if'd out don't count. And you'll need to 00106 append a newline to any file that doesn't end in one, to avoid 00107 splicing the last #included line with the next line of the 00108 #including file.) 00109 00110 Libcpp calls line numbers in this big imaginary file "logical line 00111 numbers", and has a data structure called a "line map" that can map 00112 logical line numbers onto actual source filenames and line numbers, 00113 and also tell you the chain of #inclusions responsible for any 00114 particular logical line number. Basically, this means you can pass 00115 around a single line number and some kind of "compilation unit" 00116 object and you get nice, unambiguous source code locations that 00117 distinguish between multiple #inclusions of the same file, etc. 00118 00119 Pretty neat, huh? */ 00120 00121 struct macro_source_file 00122 { 00123 00124 /* The macro table for the compilation unit this source location is 00125 a part of. */ 00126 struct macro_table *table; 00127 00128 /* A source file --- possibly a header file. This filename is relative to 00129 the compilation directory (table->comp_dir), it exactly matches the 00130 symtab->filename content. */ 00131 const char *filename; 00132 00133 /* The location we were #included from, or zero if we are the 00134 compilation unit's main source file. */ 00135 struct macro_source_file *included_by; 00136 00137 /* If `included_from' is non-zero, the line number in that source 00138 file at which we were included. */ 00139 int included_at_line; 00140 00141 /* Head of a linked list of the source files #included by this file; 00142 our children in the #inclusion tree. This list is sorted by its 00143 elements' `included_at_line' values, which are unique. (The 00144 macro splay tree's ordering function needs this property.) */ 00145 struct macro_source_file *includes; 00146 00147 /* The next file #included by our `included_from' file; our sibling 00148 in the #inclusion tree. */ 00149 struct macro_source_file *next_included; 00150 }; 00151 00152 00153 /* Create a new, empty macro table. Allocate it in OBSTACK, or use 00154 xmalloc if OBSTACK is zero. Use BCACHE to store all macro names, 00155 arguments, definitions, and anything else that might be the same 00156 amongst compilation units in an executable file; if BCACHE is zero, 00157 don't cache these things. COMP_DIR optionally contains the compilation 00158 directory of all files for this macro table. 00159 00160 Note that, if either OBSTACK or BCACHE are non-zero, then removing 00161 information from the table may leak memory. Neither obstacks nor 00162 bcaches really allow you to remove information, so although we can 00163 update the data structure to record the change, we can't free the 00164 old data. At the moment, since we only provide obstacks and 00165 bcaches for macro tables for symtabs, this isn't a problem; only 00166 odd debugging information makes a definition and then deletes it at 00167 the same source location (although 'gcc -DFOO -UFOO -DFOO=2' does 00168 do that in GCC 4.1.2.). */ 00169 struct macro_table *new_macro_table (struct obstack *obstack, 00170 struct bcache *bcache, 00171 const char *comp_dir); 00172 00173 00174 /* Free TABLE, and any macro definitions, source file structures, 00175 etc. it owns. This will raise an internal error if TABLE was 00176 allocated on an obstack, or if it uses a bcache. */ 00177 void free_macro_table (struct macro_table *table); 00178 00179 00180 /* Set FILENAME as the main source file of TABLE. Return a source 00181 file structure describing that file; if we record the #definition 00182 of macros, or the #inclusion of other files into FILENAME, we'll 00183 use that source file structure to indicate the context. 00184 00185 The "main source file" is the one that was given to the compiler; 00186 all other source files that contributed to the compilation unit are 00187 #included, directly or indirectly, from this one. 00188 00189 The macro table makes its own copy of FILENAME; the caller is 00190 responsible for freeing FILENAME when it is no longer needed. */ 00191 struct macro_source_file *macro_set_main (struct macro_table *table, 00192 const char *filename); 00193 00194 00195 /* Return the main source file of the macro table TABLE. */ 00196 struct macro_source_file *macro_main (struct macro_table *table); 00197 00198 /* Mark the macro table TABLE so that macros defined in this table can 00199 be redefined without error. Note that it invalid to call this if 00200 TABLE is allocated on an obstack. */ 00201 void macro_allow_redefinitions (struct macro_table *table); 00202 00203 00204 /* Record a #inclusion. 00205 Record in SOURCE's macro table that, at line number LINE in SOURCE, 00206 we #included the file INCLUDED. Return a source file structure we 00207 can use for symbols #defined or files #included into that. If we've 00208 already created a source file structure for this #inclusion, return 00209 the same structure we created last time. 00210 00211 The first line of the source file has a line number of 1, not 0. 00212 00213 The macro table makes its own copy of INCLUDED; the caller is 00214 responsible for freeing INCLUDED when it is no longer needed. */ 00215 struct macro_source_file *macro_include (struct macro_source_file *source, 00216 int line, 00217 const char *included); 00218 00219 /* Define any special macros, like __FILE__ or __LINE__. This should 00220 be called once, on the main source file. */ 00221 00222 void macro_define_special (struct macro_table *table); 00223 00224 /* Find any source file structure for a file named NAME, either 00225 included into SOURCE, or SOURCE itself. Return zero if we have 00226 none. NAME is only the final portion of the filename, not the full 00227 path. e.g., `stdio.h', not `/usr/include/stdio.h'. If NAME 00228 appears more than once in the inclusion tree, return the 00229 least-nested inclusion --- the one closest to the main source file. */ 00230 struct macro_source_file *(macro_lookup_inclusion 00231 (struct macro_source_file *source, 00232 const char *name)); 00233 00234 00235 /* Record an object-like #definition (i.e., one with no parameter list). 00236 Record in SOURCE's macro table that, at line number LINE in SOURCE, 00237 we #defined a preprocessor symbol named NAME, whose replacement 00238 string is REPLACEMENT. This function makes copies of NAME and 00239 REPLACEMENT; the caller is responsible for freeing them. */ 00240 void macro_define_object (struct macro_source_file *source, int line, 00241 const char *name, const char *replacement); 00242 00243 00244 /* Record an function-like #definition (i.e., one with a parameter list). 00245 00246 Record in SOURCE's macro table that, at line number LINE in SOURCE, 00247 we #defined a preprocessor symbol named NAME, with ARGC arguments 00248 whose names are given in ARGV, whose replacement string is REPLACEMENT. If 00249 the macro takes a variable number of arguments, then ARGC should be 00250 one greater than the number of named arguments, and ARGV[ARGC-1] 00251 should be the string "...". This function makes its own copies of 00252 NAME, ARGV, and REPLACEMENT; the caller is responsible for freeing 00253 them. */ 00254 void macro_define_function (struct macro_source_file *source, int line, 00255 const char *name, int argc, const char **argv, 00256 const char *replacement); 00257 00258 00259 /* Record an #undefinition. 00260 Record in SOURCE's macro table that, at line number LINE in SOURCE, 00261 we removed the definition for the preprocessor symbol named NAME. */ 00262 void macro_undef (struct macro_source_file *source, int line, 00263 const char *name); 00264 00265 /* Different kinds of macro definitions. */ 00266 enum macro_kind 00267 { 00268 macro_object_like, 00269 macro_function_like 00270 }; 00271 00272 /* Different kinds of special macros. */ 00273 00274 enum macro_special_kind 00275 { 00276 /* Ordinary. */ 00277 macro_ordinary, 00278 /* The special macro __FILE__. */ 00279 macro_FILE, 00280 /* The special macro __LINE__. */ 00281 macro_LINE 00282 }; 00283 00284 /* A preprocessor symbol definition. */ 00285 struct macro_definition 00286 { 00287 /* The table this definition lives in. */ 00288 struct macro_table *table; 00289 00290 /* What kind of macro it is. */ 00291 ENUM_BITFIELD (macro_kind) kind : 1; 00292 00293 /* If `kind' is `macro_function_like', the number of arguments it 00294 takes, and their names. The names, and the array of pointers to 00295 them, are in the table's bcache, if it has one. If `kind' is 00296 `macro_object_like', then this is actually a `macro_special_kind' 00297 describing the macro. */ 00298 int argc : 30; 00299 const char * const *argv; 00300 00301 /* The replacement string (body) of the macro. For ordinary macros, 00302 this is in the table's bcache, if it has one. For special macros 00303 like __FILE__, this value is only valid until the next use of any 00304 special macro definition; that is, it is reset each time any 00305 special macro is looked up or iterated over. */ 00306 const char *replacement; 00307 }; 00308 00309 00310 /* Return a pointer to the macro definition for NAME in scope at line 00311 number LINE of SOURCE. If LINE is -1, return the definition in 00312 effect at the end of the file. The macro table owns the structure; 00313 the caller need not free it. Return zero if NAME is not #defined 00314 at that point. */ 00315 struct macro_definition *(macro_lookup_definition 00316 (struct macro_source_file *source, 00317 int line, const char *name)); 00318 00319 00320 /* Return the source location of the definition for NAME in scope at 00321 line number LINE of SOURCE. Set *DEFINITION_LINE to the line 00322 number of the definition, and return a source file structure for 00323 the file. Return zero if NAME has no definition in scope at that 00324 point, and leave *DEFINITION_LINE unchanged. */ 00325 struct macro_source_file *(macro_definition_location 00326 (struct macro_source_file *source, 00327 int line, 00328 const char *name, 00329 int *definition_line)); 00330 00331 /* Callback function when walking a macro table. NAME is the name of 00332 the macro, and DEFINITION is the definition. SOURCE is the file at the 00333 start of the include path, and LINE is the line number of the SOURCE file 00334 where the macro was defined. USER_DATA is an arbitrary pointer which is 00335 passed by the caller to macro_for_each or macro_for_each_in_scope. */ 00336 typedef void (*macro_callback_fn) (const char *name, 00337 const struct macro_definition *definition, 00338 struct macro_source_file *source, 00339 int line, 00340 void *user_data); 00341 00342 /* Call the function FN for each macro in the macro table TABLE. 00343 USER_DATA is passed, untranslated, to FN. */ 00344 void macro_for_each (struct macro_table *table, macro_callback_fn fn, 00345 void *user_data); 00346 00347 /* Call the function FN for each macro that is visible in a given 00348 scope. The scope is represented by FILE and LINE. USER_DATA is 00349 passed, untranslated, to FN. */ 00350 void macro_for_each_in_scope (struct macro_source_file *file, int line, 00351 macro_callback_fn fn, 00352 void *user_data); 00353 00354 /* Return FILE->filename with possibly prepended compilation directory name. 00355 This is raw concatenation without the "set substitute-path" and gdb_realpath 00356 applications done by symtab_to_fullname. Returned string must be freed by 00357 xfree. 00358 00359 THis function ignores the "set filename-display" setting. Its default 00360 setting is "relative" which is backward compatible but the former behavior 00361 of macro filenames printing was "absolute". */ 00362 extern char *macro_source_fullname (struct macro_source_file *file); 00363 00364 #endif /* MACROTAB_H */