Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Symbol table lookup for the GNU debugger, GDB. |
8926118c AC |
2 | |
3 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, | |
4 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software | |
5 | Foundation, Inc. | |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b JM |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "symtab.h" | |
26 | #include "gdbtypes.h" | |
27 | #include "gdbcore.h" | |
28 | #include "frame.h" | |
29 | #include "target.h" | |
30 | #include "value.h" | |
31 | #include "symfile.h" | |
32 | #include "objfiles.h" | |
33 | #include "gdbcmd.h" | |
34 | #include "call-cmds.h" | |
88987551 | 35 | #include "gdb_regex.h" |
c906108c SS |
36 | #include "expression.h" |
37 | #include "language.h" | |
38 | #include "demangle.h" | |
39 | #include "inferior.h" | |
c5f0f3d0 | 40 | #include "linespec.h" |
0378c332 | 41 | #include "source.h" |
a7fdf62f | 42 | #include "filenames.h" /* for FILENAME_CMP */ |
c906108c | 43 | |
04ea0df1 | 44 | #include "gdb_obstack.h" |
c906108c SS |
45 | |
46 | #include <sys/types.h> | |
47 | #include <fcntl.h> | |
48 | #include "gdb_string.h" | |
49 | #include "gdb_stat.h" | |
50 | #include <ctype.h> | |
015a42b4 | 51 | #include "cp-abi.h" |
c906108c | 52 | |
c906108c SS |
53 | /* Prototypes for local functions */ |
54 | ||
a14ed312 | 55 | static void completion_list_add_name (char *, char *, int, char *, char *); |
c906108c | 56 | |
a14ed312 | 57 | static void rbreak_command (char *, int); |
c906108c | 58 | |
a14ed312 | 59 | static void types_info (char *, int); |
c906108c | 60 | |
a14ed312 | 61 | static void functions_info (char *, int); |
c906108c | 62 | |
a14ed312 | 63 | static void variables_info (char *, int); |
c906108c | 64 | |
a14ed312 | 65 | static void sources_info (char *, int); |
c906108c | 66 | |
a14ed312 | 67 | static void output_source_filename (char *, int *); |
c906108c | 68 | |
a14ed312 | 69 | static int find_line_common (struct linetable *, int, int *); |
c906108c | 70 | |
50641945 FN |
71 | /* This one is used by linespec.c */ |
72 | ||
73 | char *operator_chars (char *p, char **end); | |
74 | ||
b37bcaa8 KB |
75 | static struct partial_symbol *lookup_partial_symbol (struct partial_symtab *, |
76 | const char *, int, | |
77 | namespace_enum); | |
c906108c | 78 | |
3121eff0 DJ |
79 | static struct symbol *lookup_symbol_aux (const char *name, |
80 | const char *mangled_name, | |
81 | const struct block *block, | |
82 | const namespace_enum namespace, | |
83 | int *is_a_field_of_this, | |
84 | struct symtab **symtab); | |
fba7f19c | 85 | |
8155455b DC |
86 | static struct symbol *lookup_symbol_aux_local (const char *name, |
87 | const char *mangled_name, | |
88 | const struct block *block, | |
89 | const namespace_enum namespace, | |
90 | struct symtab **symtab); | |
91 | ||
92 | static | |
93 | struct symbol *lookup_symbol_aux_symtabs (int block_index, | |
94 | const char *name, | |
95 | const char *mangled_name, | |
96 | const namespace_enum namespace, | |
97 | struct symtab **symtab); | |
98 | ||
99 | static | |
100 | struct symbol *lookup_symbol_aux_psymtabs (int block_index, | |
101 | const char *name, | |
102 | const char *mangled_name, | |
103 | const namespace_enum namespace, | |
104 | struct symtab **symtab); | |
fba7f19c | 105 | |
a14ed312 | 106 | static struct symbol *find_active_alias (struct symbol *sym, CORE_ADDR addr); |
c906108c SS |
107 | |
108 | /* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */ | |
109 | /* Signals the presence of objects compiled by HP compilers */ | |
110 | int hp_som_som_object_present = 0; | |
111 | ||
a14ed312 | 112 | static void fixup_section (struct general_symbol_info *, struct objfile *); |
c906108c | 113 | |
a14ed312 | 114 | static int file_matches (char *, char **, int); |
c906108c | 115 | |
a14ed312 KB |
116 | static void print_symbol_info (namespace_enum, |
117 | struct symtab *, struct symbol *, int, char *); | |
c906108c | 118 | |
a14ed312 | 119 | static void print_msymbol_info (struct minimal_symbol *); |
c906108c | 120 | |
a14ed312 | 121 | static void symtab_symbol_info (char *, namespace_enum, int); |
c906108c | 122 | |
a14ed312 | 123 | static void overload_list_add_symbol (struct symbol *sym, char *oload_name); |
392a587b | 124 | |
a14ed312 | 125 | void _initialize_symtab (void); |
c906108c SS |
126 | |
127 | /* */ | |
128 | ||
129 | /* The single non-language-specific builtin type */ | |
130 | struct type *builtin_type_error; | |
131 | ||
132 | /* Block in which the most recently searched-for symbol was found. | |
133 | Might be better to make this a parameter to lookup_symbol and | |
134 | value_of_this. */ | |
135 | ||
136 | const struct block *block_found; | |
137 | ||
c906108c SS |
138 | /* Check for a symtab of a specific name; first in symtabs, then in |
139 | psymtabs. *If* there is no '/' in the name, a match after a '/' | |
140 | in the symtab filename will also work. */ | |
141 | ||
1b15f1fa TT |
142 | struct symtab * |
143 | lookup_symtab (const char *name) | |
c906108c SS |
144 | { |
145 | register struct symtab *s; | |
146 | register struct partial_symtab *ps; | |
c906108c | 147 | register struct objfile *objfile; |
58d370e0 | 148 | char *real_path = NULL; |
f079a2e5 | 149 | char *full_path = NULL; |
58d370e0 TT |
150 | |
151 | /* Here we are interested in canonicalizing an absolute path, not | |
152 | absolutizing a relative path. */ | |
153 | if (IS_ABSOLUTE_PATH (name)) | |
f079a2e5 JB |
154 | { |
155 | full_path = xfullpath (name); | |
156 | make_cleanup (xfree, full_path); | |
157 | real_path = gdb_realpath (name); | |
158 | make_cleanup (xfree, real_path); | |
159 | } | |
c906108c | 160 | |
c5aa993b | 161 | got_symtab: |
c906108c SS |
162 | |
163 | /* First, search for an exact match */ | |
164 | ||
165 | ALL_SYMTABS (objfile, s) | |
58d370e0 | 166 | { |
a7fdf62f | 167 | if (FILENAME_CMP (name, s->filename) == 0) |
58d370e0 | 168 | { |
58d370e0 TT |
169 | return s; |
170 | } | |
f079a2e5 | 171 | |
58d370e0 TT |
172 | /* If the user gave us an absolute path, try to find the file in |
173 | this symtab and use its absolute path. */ | |
f079a2e5 JB |
174 | |
175 | if (full_path != NULL) | |
176 | { | |
177 | const char *fp = symtab_to_filename (s); | |
178 | if (FILENAME_CMP (full_path, fp) == 0) | |
179 | { | |
180 | return s; | |
181 | } | |
182 | } | |
183 | ||
58d370e0 TT |
184 | if (real_path != NULL) |
185 | { | |
25f1b008 | 186 | char *rp = gdb_realpath (symtab_to_filename (s)); |
f079a2e5 | 187 | make_cleanup (xfree, rp); |
58d370e0 TT |
188 | if (FILENAME_CMP (real_path, rp) == 0) |
189 | { | |
58d370e0 TT |
190 | return s; |
191 | } | |
192 | } | |
193 | } | |
194 | ||
c906108c SS |
195 | /* Now, search for a matching tail (only if name doesn't have any dirs) */ |
196 | ||
caadab2c | 197 | if (lbasename (name) == name) |
c906108c | 198 | ALL_SYMTABS (objfile, s) |
c5aa993b | 199 | { |
31889e00 | 200 | if (FILENAME_CMP (lbasename (s->filename), name) == 0) |
c5aa993b JM |
201 | return s; |
202 | } | |
c906108c SS |
203 | |
204 | /* Same search rules as above apply here, but now we look thru the | |
205 | psymtabs. */ | |
206 | ||
207 | ps = lookup_partial_symtab (name); | |
208 | if (!ps) | |
209 | return (NULL); | |
210 | ||
c5aa993b | 211 | if (ps->readin) |
c906108c | 212 | error ("Internal: readin %s pst for `%s' found when no symtab found.", |
c5aa993b | 213 | ps->filename, name); |
c906108c SS |
214 | |
215 | s = PSYMTAB_TO_SYMTAB (ps); | |
216 | ||
217 | if (s) | |
218 | return s; | |
219 | ||
220 | /* At this point, we have located the psymtab for this file, but | |
221 | the conversion to a symtab has failed. This usually happens | |
222 | when we are looking up an include file. In this case, | |
223 | PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has | |
224 | been created. So, we need to run through the symtabs again in | |
225 | order to find the file. | |
226 | XXX - This is a crock, and should be fixed inside of the the | |
227 | symbol parsing routines. */ | |
228 | goto got_symtab; | |
229 | } | |
230 | ||
c906108c SS |
231 | /* Lookup the partial symbol table of a source file named NAME. |
232 | *If* there is no '/' in the name, a match after a '/' | |
233 | in the psymtab filename will also work. */ | |
234 | ||
235 | struct partial_symtab * | |
1f8cc6db | 236 | lookup_partial_symtab (const char *name) |
c906108c SS |
237 | { |
238 | register struct partial_symtab *pst; | |
239 | register struct objfile *objfile; | |
f079a2e5 | 240 | char *full_path = NULL; |
58d370e0 TT |
241 | char *real_path = NULL; |
242 | ||
243 | /* Here we are interested in canonicalizing an absolute path, not | |
244 | absolutizing a relative path. */ | |
245 | if (IS_ABSOLUTE_PATH (name)) | |
f079a2e5 JB |
246 | { |
247 | full_path = xfullpath (name); | |
248 | make_cleanup (xfree, full_path); | |
249 | real_path = gdb_realpath (name); | |
250 | make_cleanup (xfree, real_path); | |
251 | } | |
c5aa993b | 252 | |
c906108c | 253 | ALL_PSYMTABS (objfile, pst) |
c5aa993b | 254 | { |
a7fdf62f | 255 | if (FILENAME_CMP (name, pst->filename) == 0) |
c5aa993b JM |
256 | { |
257 | return (pst); | |
258 | } | |
f079a2e5 | 259 | |
58d370e0 TT |
260 | /* If the user gave us an absolute path, try to find the file in |
261 | this symtab and use its absolute path. */ | |
f079a2e5 | 262 | if (full_path != NULL) |
58d370e0 TT |
263 | { |
264 | if (pst->fullname == NULL) | |
265 | source_full_path_of (pst->filename, &pst->fullname); | |
266 | if (pst->fullname != NULL | |
f079a2e5 | 267 | && FILENAME_CMP (full_path, pst->fullname) == 0) |
58d370e0 | 268 | { |
58d370e0 TT |
269 | return pst; |
270 | } | |
271 | } | |
c906108c | 272 | |
f079a2e5 JB |
273 | if (real_path != NULL) |
274 | { | |
275 | char *rp = NULL; | |
276 | if (pst->fullname == NULL) | |
277 | source_full_path_of (pst->filename, &pst->fullname); | |
278 | if (pst->fullname != NULL) | |
279 | { | |
280 | rp = gdb_realpath (pst->fullname); | |
281 | make_cleanup (xfree, rp); | |
282 | } | |
283 | if (rp != NULL && FILENAME_CMP (real_path, rp) == 0) | |
284 | { | |
285 | return pst; | |
286 | } | |
287 | } | |
288 | } | |
58d370e0 | 289 | |
c906108c SS |
290 | /* Now, search for a matching tail (only if name doesn't have any dirs) */ |
291 | ||
caadab2c | 292 | if (lbasename (name) == name) |
c906108c | 293 | ALL_PSYMTABS (objfile, pst) |
c5aa993b | 294 | { |
31889e00 | 295 | if (FILENAME_CMP (lbasename (pst->filename), name) == 0) |
c5aa993b JM |
296 | return (pst); |
297 | } | |
c906108c SS |
298 | |
299 | return (NULL); | |
300 | } | |
301 | \f | |
302 | /* Mangle a GDB method stub type. This actually reassembles the pieces of the | |
303 | full method name, which consist of the class name (from T), the unadorned | |
304 | method name from METHOD_ID, and the signature for the specific overload, | |
305 | specified by SIGNATURE_ID. Note that this function is g++ specific. */ | |
306 | ||
307 | char * | |
fba45db2 | 308 | gdb_mangle_name (struct type *type, int method_id, int signature_id) |
c906108c SS |
309 | { |
310 | int mangled_name_len; | |
311 | char *mangled_name; | |
312 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id); | |
313 | struct fn_field *method = &f[signature_id]; | |
314 | char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id); | |
315 | char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id); | |
316 | char *newname = type_name_no_tag (type); | |
317 | ||
318 | /* Does the form of physname indicate that it is the full mangled name | |
319 | of a constructor (not just the args)? */ | |
320 | int is_full_physname_constructor; | |
321 | ||
322 | int is_constructor; | |
015a42b4 | 323 | int is_destructor = is_destructor_name (physname); |
c906108c SS |
324 | /* Need a new type prefix. */ |
325 | char *const_prefix = method->is_const ? "C" : ""; | |
326 | char *volatile_prefix = method->is_volatile ? "V" : ""; | |
327 | char buf[20]; | |
328 | int len = (newname == NULL ? 0 : strlen (newname)); | |
329 | ||
43630227 PS |
330 | /* Nothing to do if physname already contains a fully mangled v3 abi name |
331 | or an operator name. */ | |
332 | if ((physname[0] == '_' && physname[1] == 'Z') | |
333 | || is_operator_name (field_name)) | |
235d1e03 EZ |
334 | return xstrdup (physname); |
335 | ||
015a42b4 | 336 | is_full_physname_constructor = is_constructor_name (physname); |
c906108c SS |
337 | |
338 | is_constructor = | |
c5aa993b | 339 | is_full_physname_constructor || (newname && STREQ (field_name, newname)); |
c906108c SS |
340 | |
341 | if (!is_destructor) | |
c5aa993b | 342 | is_destructor = (strncmp (physname, "__dt", 4) == 0); |
c906108c SS |
343 | |
344 | if (is_destructor || is_full_physname_constructor) | |
345 | { | |
c5aa993b JM |
346 | mangled_name = (char *) xmalloc (strlen (physname) + 1); |
347 | strcpy (mangled_name, physname); | |
c906108c SS |
348 | return mangled_name; |
349 | } | |
350 | ||
351 | if (len == 0) | |
352 | { | |
353 | sprintf (buf, "__%s%s", const_prefix, volatile_prefix); | |
354 | } | |
355 | else if (physname[0] == 't' || physname[0] == 'Q') | |
356 | { | |
357 | /* The physname for template and qualified methods already includes | |
c5aa993b | 358 | the class name. */ |
c906108c SS |
359 | sprintf (buf, "__%s%s", const_prefix, volatile_prefix); |
360 | newname = NULL; | |
361 | len = 0; | |
362 | } | |
363 | else | |
364 | { | |
365 | sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len); | |
366 | } | |
367 | mangled_name_len = ((is_constructor ? 0 : strlen (field_name)) | |
235d1e03 | 368 | + strlen (buf) + len + strlen (physname) + 1); |
c906108c | 369 | |
c906108c | 370 | { |
c5aa993b | 371 | mangled_name = (char *) xmalloc (mangled_name_len); |
c906108c SS |
372 | if (is_constructor) |
373 | mangled_name[0] = '\0'; | |
374 | else | |
375 | strcpy (mangled_name, field_name); | |
376 | } | |
377 | strcat (mangled_name, buf); | |
378 | /* If the class doesn't have a name, i.e. newname NULL, then we just | |
379 | mangle it using 0 for the length of the class. Thus it gets mangled | |
c5aa993b | 380 | as something starting with `::' rather than `classname::'. */ |
c906108c SS |
381 | if (newname != NULL) |
382 | strcat (mangled_name, newname); | |
383 | ||
384 | strcat (mangled_name, physname); | |
385 | return (mangled_name); | |
386 | } | |
12af6855 JB |
387 | |
388 | \f | |
89aad1f9 EZ |
389 | /* Initialize the language dependent portion of a symbol |
390 | depending upon the language for the symbol. */ | |
391 | void | |
392 | symbol_init_language_specific (struct general_symbol_info *gsymbol, | |
393 | enum language language) | |
394 | { | |
395 | gsymbol->language = language; | |
396 | if (gsymbol->language == language_cplus | |
397 | || gsymbol->language == language_java) | |
398 | { | |
399 | gsymbol->language_specific.cplus_specific.demangled_name = NULL; | |
400 | } | |
401 | else if (gsymbol->language == language_objc) | |
402 | { | |
403 | gsymbol->language_specific.objc_specific.demangled_name = NULL; | |
404 | } | |
405 | /* OBSOLETE else if (SYMBOL_LANGUAGE (symbol) == language_chill) */ | |
406 | /* OBSOLETE { */ | |
407 | /* OBSOLETE SYMBOL_CHILL_DEMANGLED_NAME (symbol) = NULL; */ | |
408 | /* OBSOLETE } */ | |
409 | else | |
410 | { | |
411 | memset (&gsymbol->language_specific, 0, | |
412 | sizeof (gsymbol->language_specific)); | |
413 | } | |
414 | } | |
415 | ||
12af6855 JB |
416 | /* Initialize a symbol's mangled name. */ |
417 | ||
418 | /* Try to initialize the demangled name for a symbol, based on the | |
419 | language of that symbol. If the language is set to language_auto, | |
420 | it will attempt to find any demangling algorithm that works and | |
421 | then set the language appropriately. If no demangling of any kind | |
422 | is found, the language is set back to language_unknown, so we can | |
423 | avoid doing this work again the next time we encounter the symbol. | |
424 | Any required space to store the name is obtained from the specified | |
425 | obstack. */ | |
426 | ||
427 | void | |
428 | symbol_init_demangled_name (struct general_symbol_info *gsymbol, | |
429 | struct obstack *obstack) | |
430 | { | |
431 | char *mangled = gsymbol->name; | |
432 | char *demangled = NULL; | |
433 | ||
434 | if (gsymbol->language == language_unknown) | |
435 | gsymbol->language = language_auto; | |
436 | if (gsymbol->language == language_cplus | |
437 | || gsymbol->language == language_auto) | |
438 | { | |
439 | demangled = | |
440 | cplus_demangle (gsymbol->name, DMGL_PARAMS | DMGL_ANSI); | |
441 | if (demangled != NULL) | |
442 | { | |
443 | gsymbol->language = language_cplus; | |
444 | gsymbol->language_specific.cplus_specific.demangled_name = | |
445 | obsavestring (demangled, strlen (demangled), obstack); | |
446 | xfree (demangled); | |
447 | } | |
448 | else | |
449 | { | |
450 | gsymbol->language_specific.cplus_specific.demangled_name = NULL; | |
451 | } | |
452 | } | |
453 | if (gsymbol->language == language_java) | |
454 | { | |
455 | demangled = | |
456 | cplus_demangle (gsymbol->name, | |
457 | DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA); | |
458 | if (demangled != NULL) | |
459 | { | |
460 | gsymbol->language = language_java; | |
461 | gsymbol->language_specific.cplus_specific.demangled_name = | |
462 | obsavestring (demangled, strlen (demangled), obstack); | |
463 | xfree (demangled); | |
464 | } | |
465 | else | |
466 | { | |
467 | gsymbol->language_specific.cplus_specific.demangled_name = NULL; | |
468 | } | |
469 | } | |
db034ac5 AC |
470 | #if 0 |
471 | /* OBSOLETE if (demangled == NULL */ | |
472 | /* OBSOLETE && (gsymbol->language == language_chill */ | |
473 | /* OBSOLETE || gsymbol->language == language_auto)) */ | |
474 | /* OBSOLETE { */ | |
475 | /* OBSOLETE demangled = */ | |
476 | /* OBSOLETE chill_demangle (gsymbol->name); */ | |
477 | /* OBSOLETE if (demangled != NULL) */ | |
478 | /* OBSOLETE { */ | |
479 | /* OBSOLETE gsymbol->language = language_chill; */ | |
480 | /* OBSOLETE gsymbol->language_specific.chill_specific.demangled_name = */ | |
481 | /* OBSOLETE obsavestring (demangled, strlen (demangled), obstack); */ | |
482 | /* OBSOLETE xfree (demangled); */ | |
483 | /* OBSOLETE } */ | |
484 | /* OBSOLETE else */ | |
485 | /* OBSOLETE { */ | |
486 | /* OBSOLETE gsymbol->language_specific.chill_specific.demangled_name = NULL; */ | |
487 | /* OBSOLETE } */ | |
488 | /* OBSOLETE } */ | |
489 | #endif | |
12af6855 JB |
490 | } |
491 | ||
9cc0d196 EZ |
492 | /* Return the demangled name for a symbol based on the language for |
493 | that symbol. If no demangled name exists, return NULL. */ | |
494 | char * | |
495 | symbol_demangled_name (struct general_symbol_info *gsymbol) | |
496 | { | |
497 | if (gsymbol->language == language_cplus | |
498 | || gsymbol->language == language_java) | |
499 | return gsymbol->language_specific.cplus_specific.demangled_name; | |
12af6855 | 500 | |
9cc0d196 EZ |
501 | else if (gsymbol->language == language_objc) |
502 | return gsymbol->language_specific.objc_specific.demangled_name; | |
503 | ||
504 | else | |
505 | return NULL; | |
506 | ||
507 | /* OBSOLETE (SYMBOL_LANGUAGE (symbol) == language_chill */ | |
508 | /* OBSOLETE ? SYMBOL_CHILL_DEMANGLED_NAME (symbol) */ | |
509 | } | |
fe39c653 EZ |
510 | |
511 | /* Initialize the structure fields to zero values. */ | |
512 | void | |
513 | init_sal (struct symtab_and_line *sal) | |
514 | { | |
515 | sal->symtab = 0; | |
516 | sal->section = 0; | |
517 | sal->line = 0; | |
518 | sal->pc = 0; | |
519 | sal->end = 0; | |
520 | } | |
c906108c SS |
521 | \f |
522 | ||
c5aa993b | 523 | |
c906108c SS |
524 | /* Find which partial symtab on contains PC and SECTION. Return 0 if none. */ |
525 | ||
526 | struct partial_symtab * | |
fba45db2 | 527 | find_pc_sect_psymtab (CORE_ADDR pc, asection *section) |
c906108c SS |
528 | { |
529 | register struct partial_symtab *pst; | |
530 | register struct objfile *objfile; | |
8a48e967 DJ |
531 | struct minimal_symbol *msymbol; |
532 | ||
533 | /* If we know that this is not a text address, return failure. This is | |
534 | necessary because we loop based on texthigh and textlow, which do | |
535 | not include the data ranges. */ | |
536 | msymbol = lookup_minimal_symbol_by_pc_section (pc, section); | |
537 | if (msymbol | |
538 | && (msymbol->type == mst_data | |
539 | || msymbol->type == mst_bss | |
540 | || msymbol->type == mst_abs | |
541 | || msymbol->type == mst_file_data | |
542 | || msymbol->type == mst_file_bss)) | |
543 | return NULL; | |
c906108c SS |
544 | |
545 | ALL_PSYMTABS (objfile, pst) | |
c5aa993b | 546 | { |
c5aa993b | 547 | if (pc >= pst->textlow && pc < pst->texthigh) |
c5aa993b | 548 | { |
c5aa993b JM |
549 | struct partial_symtab *tpst; |
550 | ||
551 | /* An objfile that has its functions reordered might have | |
552 | many partial symbol tables containing the PC, but | |
553 | we want the partial symbol table that contains the | |
554 | function containing the PC. */ | |
555 | if (!(objfile->flags & OBJF_REORDERED) && | |
556 | section == 0) /* can't validate section this way */ | |
557 | return (pst); | |
558 | ||
c5aa993b JM |
559 | if (msymbol == NULL) |
560 | return (pst); | |
561 | ||
562 | for (tpst = pst; tpst != NULL; tpst = tpst->next) | |
563 | { | |
c5aa993b | 564 | if (pc >= tpst->textlow && pc < tpst->texthigh) |
c5aa993b JM |
565 | { |
566 | struct partial_symbol *p; | |
c906108c | 567 | |
c5aa993b JM |
568 | p = find_pc_sect_psymbol (tpst, pc, section); |
569 | if (p != NULL | |
570 | && SYMBOL_VALUE_ADDRESS (p) | |
571 | == SYMBOL_VALUE_ADDRESS (msymbol)) | |
572 | return (tpst); | |
573 | } | |
574 | } | |
575 | return (pst); | |
576 | } | |
577 | } | |
c906108c SS |
578 | return (NULL); |
579 | } | |
580 | ||
581 | /* Find which partial symtab contains PC. Return 0 if none. | |
582 | Backward compatibility, no section */ | |
583 | ||
584 | struct partial_symtab * | |
fba45db2 | 585 | find_pc_psymtab (CORE_ADDR pc) |
c906108c SS |
586 | { |
587 | return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc)); | |
588 | } | |
589 | ||
590 | /* Find which partial symbol within a psymtab matches PC and SECTION. | |
591 | Return 0 if none. Check all psymtabs if PSYMTAB is 0. */ | |
592 | ||
593 | struct partial_symbol * | |
fba45db2 KB |
594 | find_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc, |
595 | asection *section) | |
c906108c SS |
596 | { |
597 | struct partial_symbol *best = NULL, *p, **pp; | |
598 | CORE_ADDR best_pc; | |
c5aa993b | 599 | |
c906108c SS |
600 | if (!psymtab) |
601 | psymtab = find_pc_sect_psymtab (pc, section); | |
602 | if (!psymtab) | |
603 | return 0; | |
604 | ||
605 | /* Cope with programs that start at address 0 */ | |
606 | best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0; | |
607 | ||
608 | /* Search the global symbols as well as the static symbols, so that | |
609 | find_pc_partial_function doesn't use a minimal symbol and thus | |
610 | cache a bad endaddr. */ | |
611 | for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset; | |
c5aa993b JM |
612 | (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset) |
613 | < psymtab->n_global_syms); | |
c906108c SS |
614 | pp++) |
615 | { | |
616 | p = *pp; | |
617 | if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE | |
618 | && SYMBOL_CLASS (p) == LOC_BLOCK | |
619 | && pc >= SYMBOL_VALUE_ADDRESS (p) | |
620 | && (SYMBOL_VALUE_ADDRESS (p) > best_pc | |
621 | || (psymtab->textlow == 0 | |
622 | && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0))) | |
623 | { | |
c5aa993b | 624 | if (section) /* match on a specific section */ |
c906108c SS |
625 | { |
626 | fixup_psymbol_section (p, psymtab->objfile); | |
627 | if (SYMBOL_BFD_SECTION (p) != section) | |
628 | continue; | |
629 | } | |
630 | best_pc = SYMBOL_VALUE_ADDRESS (p); | |
631 | best = p; | |
632 | } | |
633 | } | |
634 | ||
635 | for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset; | |
c5aa993b JM |
636 | (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset) |
637 | < psymtab->n_static_syms); | |
c906108c SS |
638 | pp++) |
639 | { | |
640 | p = *pp; | |
641 | if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE | |
642 | && SYMBOL_CLASS (p) == LOC_BLOCK | |
643 | && pc >= SYMBOL_VALUE_ADDRESS (p) | |
644 | && (SYMBOL_VALUE_ADDRESS (p) > best_pc | |
c5aa993b | 645 | || (psymtab->textlow == 0 |
c906108c SS |
646 | && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0))) |
647 | { | |
c5aa993b | 648 | if (section) /* match on a specific section */ |
c906108c SS |
649 | { |
650 | fixup_psymbol_section (p, psymtab->objfile); | |
651 | if (SYMBOL_BFD_SECTION (p) != section) | |
652 | continue; | |
653 | } | |
654 | best_pc = SYMBOL_VALUE_ADDRESS (p); | |
655 | best = p; | |
656 | } | |
657 | } | |
658 | ||
659 | return best; | |
660 | } | |
661 | ||
662 | /* Find which partial symbol within a psymtab matches PC. Return 0 if none. | |
663 | Check all psymtabs if PSYMTAB is 0. Backwards compatibility, no section. */ | |
664 | ||
665 | struct partial_symbol * | |
fba45db2 | 666 | find_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc) |
c906108c SS |
667 | { |
668 | return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc)); | |
669 | } | |
670 | \f | |
671 | /* Debug symbols usually don't have section information. We need to dig that | |
672 | out of the minimal symbols and stash that in the debug symbol. */ | |
673 | ||
674 | static void | |
fba45db2 | 675 | fixup_section (struct general_symbol_info *ginfo, struct objfile *objfile) |
c906108c SS |
676 | { |
677 | struct minimal_symbol *msym; | |
678 | msym = lookup_minimal_symbol (ginfo->name, NULL, objfile); | |
679 | ||
680 | if (msym) | |
7a78d0ee KB |
681 | { |
682 | ginfo->bfd_section = SYMBOL_BFD_SECTION (msym); | |
683 | ginfo->section = SYMBOL_SECTION (msym); | |
684 | } | |
c906108c SS |
685 | } |
686 | ||
687 | struct symbol * | |
fba45db2 | 688 | fixup_symbol_section (struct symbol *sym, struct objfile *objfile) |
c906108c SS |
689 | { |
690 | if (!sym) | |
691 | return NULL; | |
692 | ||
693 | if (SYMBOL_BFD_SECTION (sym)) | |
694 | return sym; | |
695 | ||
696 | fixup_section (&sym->ginfo, objfile); | |
697 | ||
698 | return sym; | |
699 | } | |
700 | ||
7a78d0ee | 701 | struct partial_symbol * |
fba45db2 | 702 | fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile) |
c906108c SS |
703 | { |
704 | if (!psym) | |
705 | return NULL; | |
706 | ||
707 | if (SYMBOL_BFD_SECTION (psym)) | |
708 | return psym; | |
709 | ||
710 | fixup_section (&psym->ginfo, objfile); | |
711 | ||
712 | return psym; | |
713 | } | |
714 | ||
715 | /* Find the definition for a specified symbol name NAME | |
716 | in namespace NAMESPACE, visible from lexical block BLOCK. | |
717 | Returns the struct symbol pointer, or zero if no symbol is found. | |
718 | If SYMTAB is non-NULL, store the symbol table in which the | |
719 | symbol was found there, or NULL if not found. | |
720 | C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if | |
721 | NAME is a field of the current implied argument `this'. If so set | |
722 | *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero. | |
723 | BLOCK_FOUND is set to the block in which NAME is found (in the case of | |
724 | a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */ | |
725 | ||
726 | /* This function has a bunch of loops in it and it would seem to be | |
727 | attractive to put in some QUIT's (though I'm not really sure | |
728 | whether it can run long enough to be really important). But there | |
729 | are a few calls for which it would appear to be bad news to quit | |
730 | out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c, and | |
731 | nindy_frame_chain_valid in nindy-tdep.c. (Note that there is C++ | |
732 | code below which can error(), but that probably doesn't affect | |
733 | these calls since they are looking for a known variable and thus | |
734 | can probably assume it will never hit the C++ code). */ | |
735 | ||
736 | struct symbol * | |
fba7f19c | 737 | lookup_symbol (const char *name, const struct block *block, |
fba45db2 KB |
738 | const namespace_enum namespace, int *is_a_field_of_this, |
739 | struct symtab **symtab) | |
c906108c | 740 | { |
729051e6 DJ |
741 | char *demangled_name = NULL; |
742 | const char *modified_name = NULL; | |
3121eff0 | 743 | const char *mangled_name = NULL; |
fba7f19c EZ |
744 | int needtofreename = 0; |
745 | struct symbol *returnval; | |
c906108c | 746 | |
729051e6 DJ |
747 | modified_name = name; |
748 | ||
749 | /* If we are using C++ language, demangle the name before doing a lookup, so | |
750 | we can always binary search. */ | |
751 | if (current_language->la_language == language_cplus) | |
752 | { | |
753 | demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS); | |
754 | if (demangled_name) | |
755 | { | |
756 | mangled_name = name; | |
757 | modified_name = demangled_name; | |
758 | needtofreename = 1; | |
759 | } | |
760 | } | |
761 | ||
63872f9d JG |
762 | if (case_sensitivity == case_sensitive_off) |
763 | { | |
764 | char *copy; | |
765 | int len, i; | |
766 | ||
767 | len = strlen (name); | |
768 | copy = (char *) alloca (len + 1); | |
769 | for (i= 0; i < len; i++) | |
770 | copy[i] = tolower (name[i]); | |
771 | copy[len] = 0; | |
fba7f19c | 772 | modified_name = copy; |
63872f9d | 773 | } |
fba7f19c | 774 | |
3121eff0 DJ |
775 | returnval = lookup_symbol_aux (modified_name, mangled_name, block, |
776 | namespace, is_a_field_of_this, symtab); | |
fba7f19c | 777 | if (needtofreename) |
729051e6 | 778 | xfree (demangled_name); |
fba7f19c EZ |
779 | |
780 | return returnval; | |
781 | } | |
782 | ||
783 | static struct symbol * | |
3121eff0 DJ |
784 | lookup_symbol_aux (const char *name, const char *mangled_name, |
785 | const struct block *block, const namespace_enum namespace, | |
786 | int *is_a_field_of_this, struct symtab **symtab) | |
fba7f19c | 787 | { |
8155455b DC |
788 | struct symbol *sym; |
789 | struct symtab *s = NULL; | |
790 | struct blockvector *bv; | |
791 | struct minimal_symbol *msymbol; | |
63872f9d | 792 | |
c906108c SS |
793 | /* Search specified block and its superiors. */ |
794 | ||
8155455b DC |
795 | sym = lookup_symbol_aux_local (name, mangled_name, block, namespace, |
796 | symtab); | |
797 | if (sym != NULL) | |
798 | return sym; | |
c906108c | 799 | |
8155455b DC |
800 | #if 0 |
801 | /* NOTE: carlton/2002-11-05: At the time that this code was | |
802 | #ifdeffed out, the value of 'block' was always NULL at this | |
803 | point, hence the bemused comments below. */ | |
c906108c SS |
804 | |
805 | /* FIXME: this code is never executed--block is always NULL at this | |
806 | point. What is it trying to do, anyway? We already should have | |
807 | checked the STATIC_BLOCK above (it is the superblock of top-level | |
808 | blocks). Why is VAR_NAMESPACE special-cased? */ | |
809 | /* Don't need to mess with the psymtabs; if we have a block, | |
810 | that file is read in. If we don't, then we deal later with | |
811 | all the psymtab stuff that needs checking. */ | |
812 | /* Note (RT): The following never-executed code looks unnecessary to me also. | |
813 | * If we change the code to use the original (passed-in) | |
814 | * value of 'block', we could cause it to execute, but then what | |
815 | * would it do? The STATIC_BLOCK of the symtab containing the passed-in | |
816 | * 'block' was already searched by the above code. And the STATIC_BLOCK's | |
817 | * of *other* symtabs (those files not containing 'block' lexically) | |
818 | * should not contain 'block' address-wise. So we wouldn't expect this | |
819 | * code to find any 'sym''s that were not found above. I vote for | |
820 | * deleting the following paragraph of code. | |
821 | */ | |
822 | if (namespace == VAR_NAMESPACE && block != NULL) | |
823 | { | |
824 | struct block *b; | |
825 | /* Find the right symtab. */ | |
826 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
827 | { |
828 | bv = BLOCKVECTOR (s); | |
829 | b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
830 | if (BLOCK_START (b) <= BLOCK_START (block) | |
831 | && BLOCK_END (b) > BLOCK_START (block)) | |
832 | { | |
3121eff0 | 833 | sym = lookup_block_symbol (b, name, mangled_name, VAR_NAMESPACE); |
c5aa993b JM |
834 | if (sym) |
835 | { | |
836 | block_found = b; | |
837 | if (symtab != NULL) | |
838 | *symtab = s; | |
839 | return fixup_symbol_section (sym, objfile); | |
840 | } | |
841 | } | |
842 | } | |
c906108c | 843 | } |
8155455b | 844 | #endif /* 0 */ |
c906108c SS |
845 | |
846 | /* C++: If requested to do so by the caller, | |
847 | check to see if NAME is a field of `this'. */ | |
848 | if (is_a_field_of_this) | |
849 | { | |
850 | struct value *v = value_of_this (0); | |
c5aa993b | 851 | |
c906108c SS |
852 | *is_a_field_of_this = 0; |
853 | if (v && check_field (v, name)) | |
854 | { | |
855 | *is_a_field_of_this = 1; | |
856 | if (symtab != NULL) | |
857 | *symtab = NULL; | |
858 | return NULL; | |
859 | } | |
860 | } | |
861 | ||
862 | /* Now search all global blocks. Do the symtab's first, then | |
863 | check the psymtab's. If a psymtab indicates the existence | |
864 | of the desired name as a global, then do psymtab-to-symtab | |
865 | conversion on the fly and return the found symbol. */ | |
c5aa993b | 866 | |
8155455b DC |
867 | sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, mangled_name, |
868 | namespace, symtab); | |
869 | if (sym != NULL) | |
870 | return sym; | |
c906108c SS |
871 | |
872 | #ifndef HPUXHPPA | |
873 | ||
874 | /* Check for the possibility of the symbol being a function or | |
875 | a mangled variable that is stored in one of the minimal symbol tables. | |
876 | Eventually, all global symbols might be resolved in this way. */ | |
c5aa993b | 877 | |
c906108c SS |
878 | if (namespace == VAR_NAMESPACE) |
879 | { | |
880 | msymbol = lookup_minimal_symbol (name, NULL, NULL); | |
881 | if (msymbol != NULL) | |
882 | { | |
883 | s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol), | |
c5aa993b | 884 | SYMBOL_BFD_SECTION (msymbol)); |
c906108c SS |
885 | if (s != NULL) |
886 | { | |
887 | /* This is a function which has a symtab for its address. */ | |
888 | bv = BLOCKVECTOR (s); | |
889 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
c7612d53 JB |
890 | |
891 | /* This call used to pass `SYMBOL_NAME (msymbol)' as the | |
892 | `name' argument to lookup_block_symbol. But the name | |
893 | of a minimal symbol is always mangled, so that seems | |
894 | to be clearly the wrong thing to pass as the | |
895 | unmangled name. */ | |
896 | sym = lookup_block_symbol (block, name, mangled_name, namespace); | |
c5aa993b JM |
897 | /* We kept static functions in minimal symbol table as well as |
898 | in static scope. We want to find them in the symbol table. */ | |
899 | if (!sym) | |
900 | { | |
c906108c | 901 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); |
c7612d53 JB |
902 | sym = lookup_block_symbol (block, name, |
903 | mangled_name, namespace); | |
c906108c SS |
904 | } |
905 | ||
906 | /* sym == 0 if symbol was found in the minimal symbol table | |
c5aa993b JM |
907 | but not in the symtab. |
908 | Return 0 to use the msymbol definition of "foo_". | |
c906108c | 909 | |
c5aa993b JM |
910 | This happens for Fortran "foo_" symbols, |
911 | which are "foo" in the symtab. | |
c906108c | 912 | |
c5aa993b JM |
913 | This can also happen if "asm" is used to make a |
914 | regular symbol but not a debugging symbol, e.g. | |
915 | asm(".globl _main"); | |
916 | asm("_main:"); | |
917 | */ | |
c906108c SS |
918 | |
919 | if (symtab != NULL) | |
920 | *symtab = s; | |
29924310 | 921 | return fixup_symbol_section (sym, s->objfile); |
c906108c SS |
922 | } |
923 | else if (MSYMBOL_TYPE (msymbol) != mst_text | |
924 | && MSYMBOL_TYPE (msymbol) != mst_file_text | |
925 | && !STREQ (name, SYMBOL_NAME (msymbol))) | |
926 | { | |
927 | /* This is a mangled variable, look it up by its | |
c5aa993b | 928 | mangled name. */ |
29924310 | 929 | return lookup_symbol_aux (SYMBOL_NAME (msymbol), mangled_name, NULL, |
5dbd9048 | 930 | namespace, is_a_field_of_this, symtab); |
c906108c SS |
931 | } |
932 | /* There are no debug symbols for this file, or we are looking | |
933 | for an unmangled variable. | |
934 | Try to find a matching static symbol below. */ | |
935 | } | |
936 | } | |
c5aa993b | 937 | |
c906108c SS |
938 | #endif |
939 | ||
8155455b DC |
940 | sym = lookup_symbol_aux_psymtabs (GLOBAL_BLOCK, name, mangled_name, |
941 | namespace, symtab); | |
942 | if (sym != NULL) | |
943 | return sym; | |
c906108c | 944 | |
8155455b DC |
945 | /* Now search all static file-level symbols. Not strictly correct, |
946 | but more useful than an error. Do the symtabs first, then check | |
947 | the psymtabs. If a psymtab indicates the existence of the | |
948 | desired name as a file-level static, then do psymtab-to-symtab | |
c906108c SS |
949 | conversion on the fly and return the found symbol. */ |
950 | ||
8155455b DC |
951 | sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, mangled_name, |
952 | namespace, symtab); | |
953 | if (sym != NULL) | |
954 | return sym; | |
955 | ||
956 | sym = lookup_symbol_aux_psymtabs (STATIC_BLOCK, name, mangled_name, | |
957 | namespace, symtab); | |
958 | if (sym != NULL) | |
959 | return sym; | |
c906108c SS |
960 | |
961 | #ifdef HPUXHPPA | |
962 | ||
963 | /* Check for the possibility of the symbol being a function or | |
964 | a global variable that is stored in one of the minimal symbol tables. | |
965 | The "minimal symbol table" is built from linker-supplied info. | |
966 | ||
967 | RT: I moved this check to last, after the complete search of | |
968 | the global (p)symtab's and static (p)symtab's. For HP-generated | |
969 | symbol tables, this check was causing a premature exit from | |
970 | lookup_symbol with NULL return, and thus messing up symbol lookups | |
971 | of things like "c::f". It seems to me a check of the minimal | |
972 | symbol table ought to be a last resort in any case. I'm vaguely | |
973 | worried about the comment below which talks about FORTRAN routines "foo_" | |
974 | though... is it saying we need to do the "minsym" check before | |
975 | the static check in this case? | |
976 | */ | |
c5aa993b | 977 | |
c906108c SS |
978 | if (namespace == VAR_NAMESPACE) |
979 | { | |
980 | msymbol = lookup_minimal_symbol (name, NULL, NULL); | |
981 | if (msymbol != NULL) | |
982 | { | |
c5aa993b JM |
983 | /* OK, we found a minimal symbol in spite of not |
984 | * finding any symbol. There are various possible | |
985 | * explanations for this. One possibility is the symbol | |
986 | * exists in code not compiled -g. Another possibility | |
987 | * is that the 'psymtab' isn't doing its job. | |
988 | * A third possibility, related to #2, is that we were confused | |
989 | * by name-mangling. For instance, maybe the psymtab isn't | |
990 | * doing its job because it only know about demangled | |
991 | * names, but we were given a mangled name... | |
992 | */ | |
993 | ||
994 | /* We first use the address in the msymbol to try to | |
995 | * locate the appropriate symtab. Note that find_pc_symtab() | |
996 | * has a side-effect of doing psymtab-to-symtab expansion, | |
997 | * for the found symtab. | |
998 | */ | |
c906108c SS |
999 | s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)); |
1000 | if (s != NULL) | |
1001 | { | |
1002 | bv = BLOCKVECTOR (s); | |
1003 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
c7612d53 JB |
1004 | /* This call used to pass `SYMBOL_NAME (msymbol)' as the |
1005 | `name' argument to lookup_block_symbol. But the name | |
1006 | of a minimal symbol is always mangled, so that seems | |
1007 | to be clearly the wrong thing to pass as the | |
1008 | unmangled name. */ | |
1009 | sym = lookup_block_symbol (block, name, mangled_name, namespace); | |
c5aa993b JM |
1010 | /* We kept static functions in minimal symbol table as well as |
1011 | in static scope. We want to find them in the symbol table. */ | |
1012 | if (!sym) | |
1013 | { | |
c906108c | 1014 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); |
c7612d53 JB |
1015 | sym = lookup_block_symbol (block, name, |
1016 | mangled_name, namespace); | |
c906108c | 1017 | } |
c5aa993b JM |
1018 | /* If we found one, return it */ |
1019 | if (sym) | |
1020 | { | |
1021 | if (symtab != NULL) | |
1022 | *symtab = s; | |
1023 | return sym; | |
1024 | } | |
c906108c SS |
1025 | |
1026 | /* If we get here with sym == 0, the symbol was | |
c5aa993b JM |
1027 | found in the minimal symbol table |
1028 | but not in the symtab. | |
1029 | Fall through and return 0 to use the msymbol | |
1030 | definition of "foo_". | |
1031 | (Note that outer code generally follows up a call | |
1032 | to this routine with a call to lookup_minimal_symbol(), | |
1033 | so a 0 return means we'll just flow into that other routine). | |
1034 | ||
1035 | This happens for Fortran "foo_" symbols, | |
1036 | which are "foo" in the symtab. | |
1037 | ||
1038 | This can also happen if "asm" is used to make a | |
1039 | regular symbol but not a debugging symbol, e.g. | |
1040 | asm(".globl _main"); | |
1041 | asm("_main:"); | |
1042 | */ | |
c906108c SS |
1043 | } |
1044 | ||
c5aa993b JM |
1045 | /* If the lookup-by-address fails, try repeating the |
1046 | * entire lookup process with the symbol name from | |
1047 | * the msymbol (if different from the original symbol name). | |
1048 | */ | |
c906108c SS |
1049 | else if (MSYMBOL_TYPE (msymbol) != mst_text |
1050 | && MSYMBOL_TYPE (msymbol) != mst_file_text | |
1051 | && !STREQ (name, SYMBOL_NAME (msymbol))) | |
1052 | { | |
3121eff0 | 1053 | return lookup_symbol_aux (SYMBOL_NAME (msymbol), mangled_name, |
29924310 | 1054 | NULL, namespace, is_a_field_of_this, |
3121eff0 | 1055 | symtab); |
c906108c SS |
1056 | } |
1057 | } | |
1058 | } | |
1059 | ||
1060 | #endif | |
1061 | ||
1062 | if (symtab != NULL) | |
1063 | *symtab = NULL; | |
8155455b | 1064 | return NULL; |
c906108c | 1065 | } |
8155455b DC |
1066 | |
1067 | /* Check to see if the symbol is defined in BLOCK or its | |
1068 | superiors. */ | |
1069 | ||
1070 | static struct symbol * | |
1071 | lookup_symbol_aux_local (const char *name, const char *mangled_name, | |
1072 | const struct block *block, | |
1073 | const namespace_enum namespace, | |
1074 | struct symtab **symtab) | |
1075 | { | |
1076 | struct symbol *sym; | |
1077 | struct objfile *objfile = NULL; | |
1078 | struct blockvector *bv; | |
1079 | struct block *b; | |
1080 | struct symtab *s = NULL; | |
1081 | ||
1082 | while (block != 0) | |
1083 | { | |
1084 | sym = lookup_block_symbol (block, name, mangled_name, namespace); | |
1085 | if (sym) | |
1086 | { | |
1087 | block_found = block; | |
1088 | if (symtab != NULL) | |
1089 | { | |
1090 | /* Search the list of symtabs for one which contains the | |
1091 | address of the start of this block. */ | |
1092 | ALL_SYMTABS (objfile, s) | |
1093 | { | |
1094 | bv = BLOCKVECTOR (s); | |
1095 | b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
1096 | if (BLOCK_START (b) <= BLOCK_START (block) | |
1097 | && BLOCK_END (b) > BLOCK_START (block)) | |
1098 | goto found; | |
1099 | } | |
1100 | found: | |
1101 | *symtab = s; | |
1102 | } | |
1103 | ||
1104 | return fixup_symbol_section (sym, objfile); | |
1105 | } | |
1106 | block = BLOCK_SUPERBLOCK (block); | |
1107 | } | |
1108 | ||
1109 | return NULL; | |
1110 | } | |
1111 | ||
1112 | /* Check to see if the symbol is defined in one of the symtabs. | |
1113 | BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK, | |
1114 | depending on whether or not we want to search global symbols or | |
1115 | static symbols. */ | |
1116 | ||
1117 | static struct symbol * | |
1118 | lookup_symbol_aux_symtabs (int block_index, | |
1119 | const char *name, const char *mangled_name, | |
1120 | const namespace_enum namespace, | |
1121 | struct symtab **symtab) | |
1122 | { | |
1123 | struct symbol *sym; | |
1124 | struct objfile *objfile; | |
1125 | struct blockvector *bv; | |
1126 | const struct block *block; | |
1127 | struct symtab *s; | |
1128 | ||
1129 | ALL_SYMTABS (objfile, s) | |
1130 | { | |
1131 | bv = BLOCKVECTOR (s); | |
1132 | block = BLOCKVECTOR_BLOCK (bv, block_index); | |
1133 | sym = lookup_block_symbol (block, name, mangled_name, namespace); | |
1134 | if (sym) | |
1135 | { | |
1136 | block_found = block; | |
1137 | if (symtab != NULL) | |
1138 | *symtab = s; | |
1139 | return fixup_symbol_section (sym, objfile); | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | return NULL; | |
1144 | } | |
1145 | ||
1146 | /* Check to see if the symbol is defined in one of the partial | |
1147 | symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or | |
1148 | STATIC_BLOCK, depending on whether or not we want to search global | |
1149 | symbols or static symbols. */ | |
1150 | ||
1151 | static struct symbol * | |
1152 | lookup_symbol_aux_psymtabs (int block_index, const char *name, | |
1153 | const char *mangled_name, | |
1154 | const namespace_enum namespace, | |
1155 | struct symtab **symtab) | |
1156 | { | |
1157 | struct symbol *sym; | |
1158 | struct objfile *objfile; | |
1159 | struct blockvector *bv; | |
1160 | const struct block *block; | |
1161 | struct partial_symtab *ps; | |
1162 | struct symtab *s; | |
1163 | const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0); | |
1164 | ||
1165 | ALL_PSYMTABS (objfile, ps) | |
1166 | { | |
1167 | if (!ps->readin | |
1168 | && lookup_partial_symbol (ps, name, psymtab_index, namespace)) | |
1169 | { | |
1170 | s = PSYMTAB_TO_SYMTAB (ps); | |
1171 | bv = BLOCKVECTOR (s); | |
1172 | block = BLOCKVECTOR_BLOCK (bv, block_index); | |
1173 | sym = lookup_block_symbol (block, name, mangled_name, namespace); | |
1174 | if (!sym) | |
1175 | { | |
1176 | /* This shouldn't be necessary, but as a last resort try | |
1177 | looking in the statics even though the psymtab claimed | |
1178 | the symbol was global, or vice-versa. It's possible | |
1179 | that the psymtab gets it wrong in some cases. */ | |
1180 | ||
1181 | /* FIXME: carlton/2002-09-30: Should we really do that? | |
1182 | If that happens, isn't it likely to be a GDB error, in | |
1183 | which case we should fix the GDB error rather than | |
1184 | silently dealing with it here? So I'd vote for | |
1185 | removing the check for the symbol in the other | |
1186 | block. */ | |
1187 | block = BLOCKVECTOR_BLOCK (bv, | |
1188 | block_index == GLOBAL_BLOCK ? | |
1189 | STATIC_BLOCK : GLOBAL_BLOCK); | |
1190 | sym = lookup_block_symbol (block, name, mangled_name, namespace); | |
1191 | if (!sym) | |
1192 | error ("Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n%s may be an inlined function, or may be a template function\n(if a template, try specifying an instantiation: %s<type>).", | |
1193 | block_index == GLOBAL_BLOCK ? "global" : "static", | |
1194 | name, ps->filename, name, name); | |
1195 | } | |
1196 | if (symtab != NULL) | |
1197 | *symtab = s; | |
1198 | return fixup_symbol_section (sym, objfile); | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | return NULL; | |
1203 | } | |
1204 | ||
c906108c SS |
1205 | /* Look, in partial_symtab PST, for symbol NAME. Check the global |
1206 | symbols if GLOBAL, the static symbols if not */ | |
1207 | ||
1208 | static struct partial_symbol * | |
fba45db2 KB |
1209 | lookup_partial_symbol (struct partial_symtab *pst, const char *name, int global, |
1210 | namespace_enum namespace) | |
c906108c | 1211 | { |
357e46e7 | 1212 | struct partial_symbol *temp; |
c906108c SS |
1213 | struct partial_symbol **start, **psym; |
1214 | struct partial_symbol **top, **bottom, **center; | |
1215 | int length = (global ? pst->n_global_syms : pst->n_static_syms); | |
1216 | int do_linear_search = 1; | |
357e46e7 | 1217 | |
c906108c SS |
1218 | if (length == 0) |
1219 | { | |
1220 | return (NULL); | |
1221 | } | |
c906108c SS |
1222 | start = (global ? |
1223 | pst->objfile->global_psymbols.list + pst->globals_offset : | |
c5aa993b | 1224 | pst->objfile->static_psymbols.list + pst->statics_offset); |
357e46e7 | 1225 | |
c5aa993b | 1226 | if (global) /* This means we can use a binary search. */ |
c906108c SS |
1227 | { |
1228 | do_linear_search = 0; | |
1229 | ||
1230 | /* Binary search. This search is guaranteed to end with center | |
1231 | pointing at the earliest partial symbol with the correct | |
c5aa993b JM |
1232 | name. At that point *all* partial symbols with that name |
1233 | will be checked against the correct namespace. */ | |
c906108c SS |
1234 | |
1235 | bottom = start; | |
1236 | top = start + length - 1; | |
1237 | while (top > bottom) | |
1238 | { | |
1239 | center = bottom + (top - bottom) / 2; | |
1240 | if (!(center < top)) | |
e1e9e218 | 1241 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c | 1242 | if (!do_linear_search |
357e46e7 | 1243 | && (SYMBOL_LANGUAGE (*center) == language_java)) |
c906108c SS |
1244 | { |
1245 | do_linear_search = 1; | |
1246 | } | |
494b7ec9 | 1247 | if (strcmp (SYMBOL_SOURCE_NAME (*center), name) >= 0) |
c906108c SS |
1248 | { |
1249 | top = center; | |
1250 | } | |
1251 | else | |
1252 | { | |
1253 | bottom = center + 1; | |
1254 | } | |
1255 | } | |
1256 | if (!(top == bottom)) | |
e1e9e218 | 1257 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
357e46e7 DB |
1258 | |
1259 | /* djb - 2000-06-03 - Use SYMBOL_MATCHES_NAME, not a strcmp, so | |
1260 | we don't have to force a linear search on C++. Probably holds true | |
1261 | for JAVA as well, no way to check.*/ | |
1262 | while (SYMBOL_MATCHES_NAME (*top,name)) | |
c906108c SS |
1263 | { |
1264 | if (SYMBOL_NAMESPACE (*top) == namespace) | |
1265 | { | |
357e46e7 | 1266 | return (*top); |
c906108c | 1267 | } |
c5aa993b | 1268 | top++; |
c906108c SS |
1269 | } |
1270 | } | |
1271 | ||
1272 | /* Can't use a binary search or else we found during the binary search that | |
1273 | we should also do a linear search. */ | |
1274 | ||
1275 | if (do_linear_search) | |
357e46e7 | 1276 | { |
c906108c SS |
1277 | for (psym = start; psym < start + length; psym++) |
1278 | { | |
1279 | if (namespace == SYMBOL_NAMESPACE (*psym)) | |
1280 | { | |
1281 | if (SYMBOL_MATCHES_NAME (*psym, name)) | |
1282 | { | |
1283 | return (*psym); | |
1284 | } | |
1285 | } | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | return (NULL); | |
1290 | } | |
1291 | ||
1292 | /* Look up a type named NAME in the struct_namespace. The type returned | |
1293 | must not be opaque -- i.e., must have at least one field defined | |
1294 | ||
1295 | This code was modelled on lookup_symbol -- the parts not relevant to looking | |
1296 | up types were just left out. In particular it's assumed here that types | |
1297 | are available in struct_namespace and only at file-static or global blocks. */ | |
1298 | ||
1299 | ||
1300 | struct type * | |
fba45db2 | 1301 | lookup_transparent_type (const char *name) |
c906108c SS |
1302 | { |
1303 | register struct symbol *sym; | |
1304 | register struct symtab *s = NULL; | |
1305 | register struct partial_symtab *ps; | |
1306 | struct blockvector *bv; | |
1307 | register struct objfile *objfile; | |
1308 | register struct block *block; | |
c906108c SS |
1309 | |
1310 | /* Now search all the global symbols. Do the symtab's first, then | |
1311 | check the psymtab's. If a psymtab indicates the existence | |
1312 | of the desired name as a global, then do psymtab-to-symtab | |
1313 | conversion on the fly and return the found symbol. */ | |
c5aa993b | 1314 | |
c906108c | 1315 | ALL_SYMTABS (objfile, s) |
c5aa993b JM |
1316 | { |
1317 | bv = BLOCKVECTOR (s); | |
1318 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
3121eff0 | 1319 | sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE); |
c5aa993b JM |
1320 | if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym))) |
1321 | { | |
1322 | return SYMBOL_TYPE (sym); | |
1323 | } | |
1324 | } | |
c906108c SS |
1325 | |
1326 | ALL_PSYMTABS (objfile, ps) | |
c5aa993b JM |
1327 | { |
1328 | if (!ps->readin && lookup_partial_symbol (ps, name, 1, STRUCT_NAMESPACE)) | |
1329 | { | |
1330 | s = PSYMTAB_TO_SYMTAB (ps); | |
1331 | bv = BLOCKVECTOR (s); | |
1332 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
3121eff0 | 1333 | sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE); |
c5aa993b JM |
1334 | if (!sym) |
1335 | { | |
1336 | /* This shouldn't be necessary, but as a last resort | |
1337 | * try looking in the statics even though the psymtab | |
1338 | * claimed the symbol was global. It's possible that | |
1339 | * the psymtab gets it wrong in some cases. | |
1340 | */ | |
1341 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
3121eff0 | 1342 | sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE); |
c5aa993b JM |
1343 | if (!sym) |
1344 | error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\ | |
c906108c SS |
1345 | %s may be an inlined function, or may be a template function\n\ |
1346 | (if a template, try specifying an instantiation: %s<type>).", | |
c5aa993b JM |
1347 | name, ps->filename, name, name); |
1348 | } | |
1349 | if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym))) | |
1350 | return SYMBOL_TYPE (sym); | |
1351 | } | |
1352 | } | |
c906108c SS |
1353 | |
1354 | /* Now search the static file-level symbols. | |
1355 | Not strictly correct, but more useful than an error. | |
1356 | Do the symtab's first, then | |
1357 | check the psymtab's. If a psymtab indicates the existence | |
1358 | of the desired name as a file-level static, then do psymtab-to-symtab | |
1359 | conversion on the fly and return the found symbol. | |
1360 | */ | |
1361 | ||
1362 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
1363 | { |
1364 | bv = BLOCKVECTOR (s); | |
1365 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
3121eff0 | 1366 | sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE); |
c5aa993b JM |
1367 | if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym))) |
1368 | { | |
1369 | return SYMBOL_TYPE (sym); | |
1370 | } | |
1371 | } | |
c906108c SS |
1372 | |
1373 | ALL_PSYMTABS (objfile, ps) | |
c5aa993b JM |
1374 | { |
1375 | if (!ps->readin && lookup_partial_symbol (ps, name, 0, STRUCT_NAMESPACE)) | |
1376 | { | |
1377 | s = PSYMTAB_TO_SYMTAB (ps); | |
1378 | bv = BLOCKVECTOR (s); | |
1379 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
3121eff0 | 1380 | sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE); |
c5aa993b JM |
1381 | if (!sym) |
1382 | { | |
1383 | /* This shouldn't be necessary, but as a last resort | |
1384 | * try looking in the globals even though the psymtab | |
1385 | * claimed the symbol was static. It's possible that | |
1386 | * the psymtab gets it wrong in some cases. | |
1387 | */ | |
1388 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
3121eff0 | 1389 | sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE); |
c5aa993b JM |
1390 | if (!sym) |
1391 | error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\ | |
c906108c SS |
1392 | %s may be an inlined function, or may be a template function\n\ |
1393 | (if a template, try specifying an instantiation: %s<type>).", | |
c5aa993b JM |
1394 | name, ps->filename, name, name); |
1395 | } | |
1396 | if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym))) | |
1397 | return SYMBOL_TYPE (sym); | |
1398 | } | |
1399 | } | |
c906108c SS |
1400 | return (struct type *) 0; |
1401 | } | |
1402 | ||
1403 | ||
1404 | /* Find the psymtab containing main(). */ | |
1405 | /* FIXME: What about languages without main() or specially linked | |
1406 | executables that have no main() ? */ | |
1407 | ||
1408 | struct partial_symtab * | |
fba45db2 | 1409 | find_main_psymtab (void) |
c906108c SS |
1410 | { |
1411 | register struct partial_symtab *pst; | |
1412 | register struct objfile *objfile; | |
1413 | ||
1414 | ALL_PSYMTABS (objfile, pst) | |
c5aa993b | 1415 | { |
51cc5b07 | 1416 | if (lookup_partial_symbol (pst, main_name (), 1, VAR_NAMESPACE)) |
c5aa993b JM |
1417 | { |
1418 | return (pst); | |
1419 | } | |
1420 | } | |
c906108c SS |
1421 | return (NULL); |
1422 | } | |
1423 | ||
1424 | /* Search BLOCK for symbol NAME in NAMESPACE. | |
1425 | ||
1426 | Note that if NAME is the demangled form of a C++ symbol, we will fail | |
1427 | to find a match during the binary search of the non-encoded names, but | |
1428 | for now we don't worry about the slight inefficiency of looking for | |
1429 | a match we'll never find, since it will go pretty quick. Once the | |
1430 | binary search terminates, we drop through and do a straight linear | |
1431 | search on the symbols. Each symbol which is marked as being a C++ | |
1432 | symbol (language_cplus set) has both the encoded and non-encoded names | |
3121eff0 DJ |
1433 | tested for a match. |
1434 | ||
1435 | If MANGLED_NAME is non-NULL, verify that any symbol we find has this | |
1436 | particular mangled name. | |
1437 | */ | |
c906108c SS |
1438 | |
1439 | struct symbol * | |
fba45db2 | 1440 | lookup_block_symbol (register const struct block *block, const char *name, |
3121eff0 | 1441 | const char *mangled_name, |
fba45db2 | 1442 | const namespace_enum namespace) |
c906108c SS |
1443 | { |
1444 | register int bot, top, inc; | |
1445 | register struct symbol *sym; | |
1446 | register struct symbol *sym_found = NULL; | |
1447 | register int do_linear_search = 1; | |
1448 | ||
261397f8 DJ |
1449 | if (BLOCK_HASHTABLE (block)) |
1450 | { | |
1451 | unsigned int hash_index; | |
1452 | hash_index = msymbol_hash_iw (name); | |
1453 | hash_index = hash_index % BLOCK_BUCKETS (block); | |
1454 | for (sym = BLOCK_BUCKET (block, hash_index); sym; sym = sym->hash_next) | |
1455 | { | |
1456 | if (SYMBOL_NAMESPACE (sym) == namespace | |
1457 | && (mangled_name | |
1458 | ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0 | |
1459 | : SYMBOL_MATCHES_NAME (sym, name))) | |
1460 | return sym; | |
1461 | } | |
1462 | return NULL; | |
1463 | } | |
1464 | ||
c906108c SS |
1465 | /* If the blocks's symbols were sorted, start with a binary search. */ |
1466 | ||
1467 | if (BLOCK_SHOULD_SORT (block)) | |
1468 | { | |
1469 | /* Reset the linear search flag so if the binary search fails, we | |
c5aa993b | 1470 | won't do the linear search once unless we find some reason to |
fba7f19c | 1471 | do so */ |
c906108c SS |
1472 | |
1473 | do_linear_search = 0; | |
1474 | top = BLOCK_NSYMS (block); | |
1475 | bot = 0; | |
1476 | ||
1477 | /* Advance BOT to not far before the first symbol whose name is NAME. */ | |
1478 | ||
1479 | while (1) | |
1480 | { | |
1481 | inc = (top - bot + 1); | |
1482 | /* No need to keep binary searching for the last few bits worth. */ | |
1483 | if (inc < 4) | |
1484 | { | |
1485 | break; | |
1486 | } | |
1487 | inc = (inc >> 1) + bot; | |
1488 | sym = BLOCK_SYM (block, inc); | |
fba7f19c | 1489 | if (!do_linear_search && (SYMBOL_LANGUAGE (sym) == language_java)) |
c906108c SS |
1490 | { |
1491 | do_linear_search = 1; | |
1492 | } | |
fba7f19c | 1493 | if (SYMBOL_SOURCE_NAME (sym)[0] < name[0]) |
c906108c SS |
1494 | { |
1495 | bot = inc; | |
1496 | } | |
fba7f19c | 1497 | else if (SYMBOL_SOURCE_NAME (sym)[0] > name[0]) |
c906108c SS |
1498 | { |
1499 | top = inc; | |
1500 | } | |
494b7ec9 | 1501 | else if (strcmp (SYMBOL_SOURCE_NAME (sym), name) < 0) |
c906108c SS |
1502 | { |
1503 | bot = inc; | |
1504 | } | |
1505 | else | |
1506 | { | |
1507 | top = inc; | |
1508 | } | |
1509 | } | |
1510 | ||
1511 | /* Now scan forward until we run out of symbols, find one whose | |
c5aa993b JM |
1512 | name is greater than NAME, or find one we want. If there is |
1513 | more than one symbol with the right name and namespace, we | |
1514 | return the first one; I believe it is now impossible for us | |
1515 | to encounter two symbols with the same name and namespace | |
1516 | here, because blocks containing argument symbols are no | |
3121eff0 DJ |
1517 | longer sorted. The exception is for C++, where multiple functions |
1518 | (cloned constructors / destructors, in particular) can have | |
1519 | the same demangled name. So if we have a particular | |
1520 | mangled name to match, try to do so. */ | |
c906108c SS |
1521 | |
1522 | top = BLOCK_NSYMS (block); | |
1523 | while (bot < top) | |
1524 | { | |
1525 | sym = BLOCK_SYM (block, bot); | |
3121eff0 DJ |
1526 | if (SYMBOL_NAMESPACE (sym) == namespace |
1527 | && (mangled_name | |
1528 | ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0 | |
1529 | : SYMBOL_MATCHES_NAME (sym, name))) | |
c9049fc9 MC |
1530 | { |
1531 | return sym; | |
1532 | } | |
1ba7c32c JM |
1533 | if (SYMBOL_SOURCE_NAME (sym)[0] > name[0]) |
1534 | { | |
1535 | break; | |
1536 | } | |
c906108c SS |
1537 | bot++; |
1538 | } | |
1539 | } | |
1540 | ||
1541 | /* Here if block isn't sorted, or we fail to find a match during the | |
1542 | binary search above. If during the binary search above, we find a | |
8cc1c882 | 1543 | symbol which is a Java symbol, then we have re-enabled the linear |
c906108c SS |
1544 | search flag which was reset when starting the binary search. |
1545 | ||
1546 | This loop is equivalent to the loop above, but hacked greatly for speed. | |
1547 | ||
1548 | Note that parameter symbols do not always show up last in the | |
1549 | list; this loop makes sure to take anything else other than | |
1550 | parameter symbols first; it only uses parameter symbols as a | |
1551 | last resort. Note that this only takes up extra computation | |
1552 | time on a match. */ | |
1553 | ||
1554 | if (do_linear_search) | |
1555 | { | |
1556 | top = BLOCK_NSYMS (block); | |
1557 | bot = 0; | |
1558 | while (bot < top) | |
1559 | { | |
1560 | sym = BLOCK_SYM (block, bot); | |
3121eff0 DJ |
1561 | if (SYMBOL_NAMESPACE (sym) == namespace |
1562 | && (mangled_name | |
1563 | ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0 | |
1564 | : SYMBOL_MATCHES_NAME (sym, name))) | |
c906108c SS |
1565 | { |
1566 | /* If SYM has aliases, then use any alias that is active | |
c5aa993b JM |
1567 | at the current PC. If no alias is active at the current |
1568 | PC, then use the main symbol. | |
c906108c | 1569 | |
c5aa993b | 1570 | ?!? Is checking the current pc correct? Is this routine |
a0b3c4fd JM |
1571 | ever called to look up a symbol from another context? |
1572 | ||
1573 | FIXME: No, it's not correct. If someone sets a | |
1574 | conditional breakpoint at an address, then the | |
1575 | breakpoint's `struct expression' should refer to the | |
1576 | `struct symbol' appropriate for the breakpoint's | |
1577 | address, which may not be the PC. | |
1578 | ||
1579 | Even if it were never called from another context, | |
1580 | it's totally bizarre for lookup_symbol's behavior to | |
1581 | depend on the value of the inferior's current PC. We | |
1582 | should pass in the appropriate PC as well as the | |
1583 | block. The interface to lookup_symbol should change | |
1584 | to require the caller to provide a PC. */ | |
1585 | ||
c5aa993b JM |
1586 | if (SYMBOL_ALIASES (sym)) |
1587 | sym = find_active_alias (sym, read_pc ()); | |
c906108c SS |
1588 | |
1589 | sym_found = sym; | |
1590 | if (SYMBOL_CLASS (sym) != LOC_ARG && | |
1591 | SYMBOL_CLASS (sym) != LOC_LOCAL_ARG && | |
1592 | SYMBOL_CLASS (sym) != LOC_REF_ARG && | |
1593 | SYMBOL_CLASS (sym) != LOC_REGPARM && | |
1594 | SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR && | |
1595 | SYMBOL_CLASS (sym) != LOC_BASEREG_ARG) | |
1596 | { | |
1597 | break; | |
1598 | } | |
1599 | } | |
1600 | bot++; | |
1601 | } | |
1602 | } | |
1603 | return (sym_found); /* Will be NULL if not found. */ | |
1604 | } | |
1605 | ||
1606 | /* Given a main symbol SYM and ADDR, search through the alias | |
1607 | list to determine if an alias is active at ADDR and return | |
1608 | the active alias. | |
1609 | ||
1610 | If no alias is active, then return SYM. */ | |
1611 | ||
1612 | static struct symbol * | |
fba45db2 | 1613 | find_active_alias (struct symbol *sym, CORE_ADDR addr) |
c906108c SS |
1614 | { |
1615 | struct range_list *r; | |
1616 | struct alias_list *aliases; | |
1617 | ||
1618 | /* If we have aliases, check them first. */ | |
1619 | aliases = SYMBOL_ALIASES (sym); | |
1620 | ||
1621 | while (aliases) | |
1622 | { | |
1623 | if (!SYMBOL_RANGES (aliases->sym)) | |
c5aa993b | 1624 | return aliases->sym; |
c906108c SS |
1625 | for (r = SYMBOL_RANGES (aliases->sym); r; r = r->next) |
1626 | { | |
1627 | if (r->start <= addr && r->end > addr) | |
1628 | return aliases->sym; | |
1629 | } | |
1630 | aliases = aliases->next; | |
1631 | } | |
1632 | ||
1633 | /* Nothing found, return the main symbol. */ | |
1634 | return sym; | |
1635 | } | |
c906108c | 1636 | \f |
c5aa993b | 1637 | |
c906108c SS |
1638 | /* Return the symbol for the function which contains a specified |
1639 | lexical block, described by a struct block BL. */ | |
1640 | ||
1641 | struct symbol * | |
fba45db2 | 1642 | block_function (struct block *bl) |
c906108c SS |
1643 | { |
1644 | while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0) | |
1645 | bl = BLOCK_SUPERBLOCK (bl); | |
1646 | ||
1647 | return BLOCK_FUNCTION (bl); | |
1648 | } | |
1649 | ||
1650 | /* Find the symtab associated with PC and SECTION. Look through the | |
1651 | psymtabs and read in another symtab if necessary. */ | |
1652 | ||
1653 | struct symtab * | |
fba45db2 | 1654 | find_pc_sect_symtab (CORE_ADDR pc, asection *section) |
c906108c SS |
1655 | { |
1656 | register struct block *b; | |
1657 | struct blockvector *bv; | |
1658 | register struct symtab *s = NULL; | |
1659 | register struct symtab *best_s = NULL; | |
1660 | register struct partial_symtab *ps; | |
1661 | register struct objfile *objfile; | |
1662 | CORE_ADDR distance = 0; | |
8a48e967 DJ |
1663 | struct minimal_symbol *msymbol; |
1664 | ||
1665 | /* If we know that this is not a text address, return failure. This is | |
1666 | necessary because we loop based on the block's high and low code | |
1667 | addresses, which do not include the data ranges, and because | |
1668 | we call find_pc_sect_psymtab which has a similar restriction based | |
1669 | on the partial_symtab's texthigh and textlow. */ | |
1670 | msymbol = lookup_minimal_symbol_by_pc_section (pc, section); | |
1671 | if (msymbol | |
1672 | && (msymbol->type == mst_data | |
1673 | || msymbol->type == mst_bss | |
1674 | || msymbol->type == mst_abs | |
1675 | || msymbol->type == mst_file_data | |
1676 | || msymbol->type == mst_file_bss)) | |
1677 | return NULL; | |
c906108c SS |
1678 | |
1679 | /* Search all symtabs for the one whose file contains our address, and which | |
1680 | is the smallest of all the ones containing the address. This is designed | |
1681 | to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000 | |
1682 | and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from | |
1683 | 0x1000-0x4000, but for address 0x2345 we want to return symtab b. | |
1684 | ||
1685 | This happens for native ecoff format, where code from included files | |
1686 | gets its own symtab. The symtab for the included file should have | |
1687 | been read in already via the dependency mechanism. | |
1688 | It might be swifter to create several symtabs with the same name | |
1689 | like xcoff does (I'm not sure). | |
1690 | ||
1691 | It also happens for objfiles that have their functions reordered. | |
1692 | For these, the symtab we are looking for is not necessarily read in. */ | |
1693 | ||
1694 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
1695 | { |
1696 | bv = BLOCKVECTOR (s); | |
1697 | b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
c906108c | 1698 | |
c5aa993b | 1699 | if (BLOCK_START (b) <= pc |
c5aa993b | 1700 | && BLOCK_END (b) > pc |
c5aa993b JM |
1701 | && (distance == 0 |
1702 | || BLOCK_END (b) - BLOCK_START (b) < distance)) | |
1703 | { | |
1704 | /* For an objfile that has its functions reordered, | |
1705 | find_pc_psymtab will find the proper partial symbol table | |
1706 | and we simply return its corresponding symtab. */ | |
1707 | /* In order to better support objfiles that contain both | |
1708 | stabs and coff debugging info, we continue on if a psymtab | |
1709 | can't be found. */ | |
1710 | if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs) | |
1711 | { | |
1712 | ps = find_pc_sect_psymtab (pc, section); | |
1713 | if (ps) | |
1714 | return PSYMTAB_TO_SYMTAB (ps); | |
1715 | } | |
1716 | if (section != 0) | |
1717 | { | |
1718 | int i; | |
261397f8 | 1719 | struct symbol *sym = NULL; |
c906108c | 1720 | |
261397f8 | 1721 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c5aa993b | 1722 | { |
261397f8 DJ |
1723 | fixup_symbol_section (sym, objfile); |
1724 | if (section == SYMBOL_BFD_SECTION (sym)) | |
c5aa993b JM |
1725 | break; |
1726 | } | |
261397f8 | 1727 | if ((i >= BLOCK_BUCKETS (b)) && (sym == NULL)) |
c5aa993b JM |
1728 | continue; /* no symbol in this symtab matches section */ |
1729 | } | |
1730 | distance = BLOCK_END (b) - BLOCK_START (b); | |
1731 | best_s = s; | |
1732 | } | |
1733 | } | |
c906108c SS |
1734 | |
1735 | if (best_s != NULL) | |
c5aa993b | 1736 | return (best_s); |
c906108c SS |
1737 | |
1738 | s = NULL; | |
1739 | ps = find_pc_sect_psymtab (pc, section); | |
1740 | if (ps) | |
1741 | { | |
1742 | if (ps->readin) | |
1743 | /* Might want to error() here (in case symtab is corrupt and | |
1744 | will cause a core dump), but maybe we can successfully | |
1745 | continue, so let's not. */ | |
c906108c | 1746 | warning ("\ |
d730266b AC |
1747 | (Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n", |
1748 | paddr_nz (pc)); | |
c906108c SS |
1749 | s = PSYMTAB_TO_SYMTAB (ps); |
1750 | } | |
1751 | return (s); | |
1752 | } | |
1753 | ||
1754 | /* Find the symtab associated with PC. Look through the psymtabs and | |
1755 | read in another symtab if necessary. Backward compatibility, no section */ | |
1756 | ||
1757 | struct symtab * | |
fba45db2 | 1758 | find_pc_symtab (CORE_ADDR pc) |
c906108c SS |
1759 | { |
1760 | return find_pc_sect_symtab (pc, find_pc_mapped_section (pc)); | |
1761 | } | |
c906108c | 1762 | \f |
c5aa993b | 1763 | |
7e73cedf | 1764 | /* Find the source file and line number for a given PC value and SECTION. |
c906108c SS |
1765 | Return a structure containing a symtab pointer, a line number, |
1766 | and a pc range for the entire source line. | |
1767 | The value's .pc field is NOT the specified pc. | |
1768 | NOTCURRENT nonzero means, if specified pc is on a line boundary, | |
1769 | use the line that ends there. Otherwise, in that case, the line | |
1770 | that begins there is used. */ | |
1771 | ||
1772 | /* The big complication here is that a line may start in one file, and end just | |
1773 | before the start of another file. This usually occurs when you #include | |
1774 | code in the middle of a subroutine. To properly find the end of a line's PC | |
1775 | range, we must search all symtabs associated with this compilation unit, and | |
1776 | find the one whose first PC is closer than that of the next line in this | |
1777 | symtab. */ | |
1778 | ||
1779 | /* If it's worth the effort, we could be using a binary search. */ | |
1780 | ||
1781 | struct symtab_and_line | |
fba45db2 | 1782 | find_pc_sect_line (CORE_ADDR pc, struct sec *section, int notcurrent) |
c906108c SS |
1783 | { |
1784 | struct symtab *s; | |
1785 | register struct linetable *l; | |
1786 | register int len; | |
1787 | register int i; | |
1788 | register struct linetable_entry *item; | |
1789 | struct symtab_and_line val; | |
1790 | struct blockvector *bv; | |
1791 | struct minimal_symbol *msymbol; | |
1792 | struct minimal_symbol *mfunsym; | |
1793 | ||
1794 | /* Info on best line seen so far, and where it starts, and its file. */ | |
1795 | ||
1796 | struct linetable_entry *best = NULL; | |
1797 | CORE_ADDR best_end = 0; | |
1798 | struct symtab *best_symtab = 0; | |
1799 | ||
1800 | /* Store here the first line number | |
1801 | of a file which contains the line at the smallest pc after PC. | |
1802 | If we don't find a line whose range contains PC, | |
1803 | we will use a line one less than this, | |
1804 | with a range from the start of that file to the first line's pc. */ | |
1805 | struct linetable_entry *alt = NULL; | |
1806 | struct symtab *alt_symtab = 0; | |
1807 | ||
1808 | /* Info on best line seen in this file. */ | |
1809 | ||
1810 | struct linetable_entry *prev; | |
1811 | ||
1812 | /* If this pc is not from the current frame, | |
1813 | it is the address of the end of a call instruction. | |
1814 | Quite likely that is the start of the following statement. | |
1815 | But what we want is the statement containing the instruction. | |
1816 | Fudge the pc to make sure we get that. */ | |
1817 | ||
fe39c653 | 1818 | init_sal (&val); /* initialize to zeroes */ |
c906108c | 1819 | |
b77b1eb7 JB |
1820 | /* It's tempting to assume that, if we can't find debugging info for |
1821 | any function enclosing PC, that we shouldn't search for line | |
1822 | number info, either. However, GAS can emit line number info for | |
1823 | assembly files --- very helpful when debugging hand-written | |
1824 | assembly code. In such a case, we'd have no debug info for the | |
1825 | function, but we would have line info. */ | |
648f4f79 | 1826 | |
c906108c SS |
1827 | if (notcurrent) |
1828 | pc -= 1; | |
1829 | ||
c5aa993b | 1830 | /* elz: added this because this function returned the wrong |
c906108c SS |
1831 | information if the pc belongs to a stub (import/export) |
1832 | to call a shlib function. This stub would be anywhere between | |
1833 | two functions in the target, and the line info was erroneously | |
1834 | taken to be the one of the line before the pc. | |
c5aa993b | 1835 | */ |
c906108c | 1836 | /* RT: Further explanation: |
c5aa993b | 1837 | |
c906108c SS |
1838 | * We have stubs (trampolines) inserted between procedures. |
1839 | * | |
1840 | * Example: "shr1" exists in a shared library, and a "shr1" stub also | |
1841 | * exists in the main image. | |
1842 | * | |
1843 | * In the minimal symbol table, we have a bunch of symbols | |
1844 | * sorted by start address. The stubs are marked as "trampoline", | |
1845 | * the others appear as text. E.g.: | |
1846 | * | |
1847 | * Minimal symbol table for main image | |
1848 | * main: code for main (text symbol) | |
1849 | * shr1: stub (trampoline symbol) | |
1850 | * foo: code for foo (text symbol) | |
1851 | * ... | |
1852 | * Minimal symbol table for "shr1" image: | |
1853 | * ... | |
1854 | * shr1: code for shr1 (text symbol) | |
1855 | * ... | |
1856 | * | |
1857 | * So the code below is trying to detect if we are in the stub | |
1858 | * ("shr1" stub), and if so, find the real code ("shr1" trampoline), | |
1859 | * and if found, do the symbolization from the real-code address | |
1860 | * rather than the stub address. | |
1861 | * | |
1862 | * Assumptions being made about the minimal symbol table: | |
1863 | * 1. lookup_minimal_symbol_by_pc() will return a trampoline only | |
1864 | * if we're really in the trampoline. If we're beyond it (say | |
1865 | * we're in "foo" in the above example), it'll have a closer | |
1866 | * symbol (the "foo" text symbol for example) and will not | |
1867 | * return the trampoline. | |
1868 | * 2. lookup_minimal_symbol_text() will find a real text symbol | |
1869 | * corresponding to the trampoline, and whose address will | |
1870 | * be different than the trampoline address. I put in a sanity | |
1871 | * check for the address being the same, to avoid an | |
1872 | * infinite recursion. | |
1873 | */ | |
c5aa993b JM |
1874 | msymbol = lookup_minimal_symbol_by_pc (pc); |
1875 | if (msymbol != NULL) | |
c906108c | 1876 | if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline) |
c5aa993b JM |
1877 | { |
1878 | mfunsym = lookup_minimal_symbol_text (SYMBOL_NAME (msymbol), NULL, NULL); | |
1879 | if (mfunsym == NULL) | |
1880 | /* I eliminated this warning since it is coming out | |
1881 | * in the following situation: | |
1882 | * gdb shmain // test program with shared libraries | |
1883 | * (gdb) break shr1 // function in shared lib | |
1884 | * Warning: In stub for ... | |
1885 | * In the above situation, the shared lib is not loaded yet, | |
1886 | * so of course we can't find the real func/line info, | |
1887 | * but the "break" still works, and the warning is annoying. | |
1888 | * So I commented out the warning. RT */ | |
1889 | /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ; | |
1890 | /* fall through */ | |
1891 | else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol)) | |
1892 | /* Avoid infinite recursion */ | |
1893 | /* See above comment about why warning is commented out */ | |
1894 | /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ; | |
1895 | /* fall through */ | |
1896 | else | |
1897 | return find_pc_line (SYMBOL_VALUE (mfunsym), 0); | |
1898 | } | |
c906108c SS |
1899 | |
1900 | ||
1901 | s = find_pc_sect_symtab (pc, section); | |
1902 | if (!s) | |
1903 | { | |
1904 | /* if no symbol information, return previous pc */ | |
1905 | if (notcurrent) | |
1906 | pc++; | |
1907 | val.pc = pc; | |
1908 | return val; | |
1909 | } | |
1910 | ||
1911 | bv = BLOCKVECTOR (s); | |
1912 | ||
1913 | /* Look at all the symtabs that share this blockvector. | |
1914 | They all have the same apriori range, that we found was right; | |
1915 | but they have different line tables. */ | |
1916 | ||
1917 | for (; s && BLOCKVECTOR (s) == bv; s = s->next) | |
1918 | { | |
1919 | /* Find the best line in this symtab. */ | |
1920 | l = LINETABLE (s); | |
1921 | if (!l) | |
c5aa993b | 1922 | continue; |
c906108c SS |
1923 | len = l->nitems; |
1924 | if (len <= 0) | |
1925 | { | |
1926 | /* I think len can be zero if the symtab lacks line numbers | |
1927 | (e.g. gcc -g1). (Either that or the LINETABLE is NULL; | |
1928 | I'm not sure which, and maybe it depends on the symbol | |
1929 | reader). */ | |
1930 | continue; | |
1931 | } | |
1932 | ||
1933 | prev = NULL; | |
1934 | item = l->item; /* Get first line info */ | |
1935 | ||
1936 | /* Is this file's first line closer than the first lines of other files? | |
c5aa993b | 1937 | If so, record this file, and its first line, as best alternate. */ |
c906108c SS |
1938 | if (item->pc > pc && (!alt || item->pc < alt->pc)) |
1939 | { | |
1940 | alt = item; | |
1941 | alt_symtab = s; | |
1942 | } | |
1943 | ||
1944 | for (i = 0; i < len; i++, item++) | |
1945 | { | |
1946 | /* Leave prev pointing to the linetable entry for the last line | |
1947 | that started at or before PC. */ | |
1948 | if (item->pc > pc) | |
1949 | break; | |
1950 | ||
1951 | prev = item; | |
1952 | } | |
1953 | ||
1954 | /* At this point, prev points at the line whose start addr is <= pc, and | |
c5aa993b JM |
1955 | item points at the next line. If we ran off the end of the linetable |
1956 | (pc >= start of the last line), then prev == item. If pc < start of | |
1957 | the first line, prev will not be set. */ | |
c906108c SS |
1958 | |
1959 | /* Is this file's best line closer than the best in the other files? | |
c5aa993b | 1960 | If so, record this file, and its best line, as best so far. */ |
c906108c SS |
1961 | |
1962 | if (prev && (!best || prev->pc > best->pc)) | |
1963 | { | |
1964 | best = prev; | |
1965 | best_symtab = s; | |
25d53da1 KB |
1966 | |
1967 | /* Discard BEST_END if it's before the PC of the current BEST. */ | |
1968 | if (best_end <= best->pc) | |
1969 | best_end = 0; | |
c906108c | 1970 | } |
25d53da1 KB |
1971 | |
1972 | /* If another line (denoted by ITEM) is in the linetable and its | |
1973 | PC is after BEST's PC, but before the current BEST_END, then | |
1974 | use ITEM's PC as the new best_end. */ | |
1975 | if (best && i < len && item->pc > best->pc | |
1976 | && (best_end == 0 || best_end > item->pc)) | |
1977 | best_end = item->pc; | |
c906108c SS |
1978 | } |
1979 | ||
1980 | if (!best_symtab) | |
1981 | { | |
1982 | if (!alt_symtab) | |
1983 | { /* If we didn't find any line # info, just | |
1984 | return zeros. */ | |
1985 | val.pc = pc; | |
1986 | } | |
1987 | else | |
1988 | { | |
1989 | val.symtab = alt_symtab; | |
1990 | val.line = alt->line - 1; | |
1991 | ||
1992 | /* Don't return line 0, that means that we didn't find the line. */ | |
c5aa993b JM |
1993 | if (val.line == 0) |
1994 | ++val.line; | |
c906108c SS |
1995 | |
1996 | val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)); | |
1997 | val.end = alt->pc; | |
1998 | } | |
1999 | } | |
e8717518 FF |
2000 | else if (best->line == 0) |
2001 | { | |
2002 | /* If our best fit is in a range of PC's for which no line | |
2003 | number info is available (line number is zero) then we didn't | |
2004 | find any valid line information. */ | |
2005 | val.pc = pc; | |
2006 | } | |
c906108c SS |
2007 | else |
2008 | { | |
2009 | val.symtab = best_symtab; | |
2010 | val.line = best->line; | |
2011 | val.pc = best->pc; | |
2012 | if (best_end && (!alt || best_end < alt->pc)) | |
2013 | val.end = best_end; | |
2014 | else if (alt) | |
2015 | val.end = alt->pc; | |
2016 | else | |
2017 | val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)); | |
2018 | } | |
2019 | val.section = section; | |
2020 | return val; | |
2021 | } | |
2022 | ||
2023 | /* Backward compatibility (no section) */ | |
2024 | ||
2025 | struct symtab_and_line | |
fba45db2 | 2026 | find_pc_line (CORE_ADDR pc, int notcurrent) |
c906108c | 2027 | { |
c5aa993b | 2028 | asection *section; |
c906108c SS |
2029 | |
2030 | section = find_pc_overlay (pc); | |
2031 | if (pc_in_unmapped_range (pc, section)) | |
2032 | pc = overlay_mapped_address (pc, section); | |
2033 | return find_pc_sect_line (pc, section, notcurrent); | |
2034 | } | |
c906108c | 2035 | \f |
c906108c SS |
2036 | /* Find line number LINE in any symtab whose name is the same as |
2037 | SYMTAB. | |
2038 | ||
2039 | If found, return the symtab that contains the linetable in which it was | |
2040 | found, set *INDEX to the index in the linetable of the best entry | |
2041 | found, and set *EXACT_MATCH nonzero if the value returned is an | |
2042 | exact match. | |
2043 | ||
2044 | If not found, return NULL. */ | |
2045 | ||
50641945 | 2046 | struct symtab * |
fba45db2 | 2047 | find_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match) |
c906108c SS |
2048 | { |
2049 | int exact; | |
2050 | ||
2051 | /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE | |
2052 | so far seen. */ | |
2053 | ||
2054 | int best_index; | |
2055 | struct linetable *best_linetable; | |
2056 | struct symtab *best_symtab; | |
2057 | ||
2058 | /* First try looking it up in the given symtab. */ | |
2059 | best_linetable = LINETABLE (symtab); | |
2060 | best_symtab = symtab; | |
2061 | best_index = find_line_common (best_linetable, line, &exact); | |
2062 | if (best_index < 0 || !exact) | |
2063 | { | |
2064 | /* Didn't find an exact match. So we better keep looking for | |
c5aa993b JM |
2065 | another symtab with the same name. In the case of xcoff, |
2066 | multiple csects for one source file (produced by IBM's FORTRAN | |
2067 | compiler) produce multiple symtabs (this is unavoidable | |
2068 | assuming csects can be at arbitrary places in memory and that | |
2069 | the GLOBAL_BLOCK of a symtab has a begin and end address). */ | |
c906108c SS |
2070 | |
2071 | /* BEST is the smallest linenumber > LINE so far seen, | |
c5aa993b JM |
2072 | or 0 if none has been seen so far. |
2073 | BEST_INDEX and BEST_LINETABLE identify the item for it. */ | |
c906108c SS |
2074 | int best; |
2075 | ||
2076 | struct objfile *objfile; | |
2077 | struct symtab *s; | |
2078 | ||
2079 | if (best_index >= 0) | |
2080 | best = best_linetable->item[best_index].line; | |
2081 | else | |
2082 | best = 0; | |
2083 | ||
2084 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
2085 | { |
2086 | struct linetable *l; | |
2087 | int ind; | |
c906108c | 2088 | |
c5aa993b JM |
2089 | if (!STREQ (symtab->filename, s->filename)) |
2090 | continue; | |
2091 | l = LINETABLE (s); | |
2092 | ind = find_line_common (l, line, &exact); | |
2093 | if (ind >= 0) | |
2094 | { | |
2095 | if (exact) | |
2096 | { | |
2097 | best_index = ind; | |
2098 | best_linetable = l; | |
2099 | best_symtab = s; | |
2100 | goto done; | |
2101 | } | |
2102 | if (best == 0 || l->item[ind].line < best) | |
2103 | { | |
2104 | best = l->item[ind].line; | |
2105 | best_index = ind; | |
2106 | best_linetable = l; | |
2107 | best_symtab = s; | |
2108 | } | |
2109 | } | |
2110 | } | |
c906108c | 2111 | } |
c5aa993b | 2112 | done: |
c906108c SS |
2113 | if (best_index < 0) |
2114 | return NULL; | |
2115 | ||
2116 | if (index) | |
2117 | *index = best_index; | |
2118 | if (exact_match) | |
2119 | *exact_match = exact; | |
2120 | ||
2121 | return best_symtab; | |
2122 | } | |
2123 | \f | |
2124 | /* Set the PC value for a given source file and line number and return true. | |
2125 | Returns zero for invalid line number (and sets the PC to 0). | |
2126 | The source file is specified with a struct symtab. */ | |
2127 | ||
2128 | int | |
fba45db2 | 2129 | find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc) |
c906108c SS |
2130 | { |
2131 | struct linetable *l; | |
2132 | int ind; | |
2133 | ||
2134 | *pc = 0; | |
2135 | if (symtab == 0) | |
2136 | return 0; | |
2137 | ||
2138 | symtab = find_line_symtab (symtab, line, &ind, NULL); | |
2139 | if (symtab != NULL) | |
2140 | { | |
2141 | l = LINETABLE (symtab); | |
2142 | *pc = l->item[ind].pc; | |
2143 | return 1; | |
2144 | } | |
2145 | else | |
2146 | return 0; | |
2147 | } | |
2148 | ||
2149 | /* Find the range of pc values in a line. | |
2150 | Store the starting pc of the line into *STARTPTR | |
2151 | and the ending pc (start of next line) into *ENDPTR. | |
2152 | Returns 1 to indicate success. | |
2153 | Returns 0 if could not find the specified line. */ | |
2154 | ||
2155 | int | |
fba45db2 KB |
2156 | find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr, |
2157 | CORE_ADDR *endptr) | |
c906108c SS |
2158 | { |
2159 | CORE_ADDR startaddr; | |
2160 | struct symtab_and_line found_sal; | |
2161 | ||
2162 | startaddr = sal.pc; | |
c5aa993b | 2163 | if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr)) |
c906108c SS |
2164 | return 0; |
2165 | ||
2166 | /* This whole function is based on address. For example, if line 10 has | |
2167 | two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then | |
2168 | "info line *0x123" should say the line goes from 0x100 to 0x200 | |
2169 | and "info line *0x355" should say the line goes from 0x300 to 0x400. | |
2170 | This also insures that we never give a range like "starts at 0x134 | |
2171 | and ends at 0x12c". */ | |
2172 | ||
2173 | found_sal = find_pc_sect_line (startaddr, sal.section, 0); | |
2174 | if (found_sal.line != sal.line) | |
2175 | { | |
2176 | /* The specified line (sal) has zero bytes. */ | |
2177 | *startptr = found_sal.pc; | |
2178 | *endptr = found_sal.pc; | |
2179 | } | |
2180 | else | |
2181 | { | |
2182 | *startptr = found_sal.pc; | |
2183 | *endptr = found_sal.end; | |
2184 | } | |
2185 | return 1; | |
2186 | } | |
2187 | ||
2188 | /* Given a line table and a line number, return the index into the line | |
2189 | table for the pc of the nearest line whose number is >= the specified one. | |
2190 | Return -1 if none is found. The value is >= 0 if it is an index. | |
2191 | ||
2192 | Set *EXACT_MATCH nonzero if the value returned is an exact match. */ | |
2193 | ||
2194 | static int | |
fba45db2 KB |
2195 | find_line_common (register struct linetable *l, register int lineno, |
2196 | int *exact_match) | |
c906108c SS |
2197 | { |
2198 | register int i; | |
2199 | register int len; | |
2200 | ||
2201 | /* BEST is the smallest linenumber > LINENO so far seen, | |
2202 | or 0 if none has been seen so far. | |
2203 | BEST_INDEX identifies the item for it. */ | |
2204 | ||
2205 | int best_index = -1; | |
2206 | int best = 0; | |
2207 | ||
2208 | if (lineno <= 0) | |
2209 | return -1; | |
2210 | if (l == 0) | |
2211 | return -1; | |
2212 | ||
2213 | len = l->nitems; | |
2214 | for (i = 0; i < len; i++) | |
2215 | { | |
2216 | register struct linetable_entry *item = &(l->item[i]); | |
2217 | ||
2218 | if (item->line == lineno) | |
2219 | { | |
2220 | /* Return the first (lowest address) entry which matches. */ | |
2221 | *exact_match = 1; | |
2222 | return i; | |
2223 | } | |
2224 | ||
2225 | if (item->line > lineno && (best == 0 || item->line < best)) | |
2226 | { | |
2227 | best = item->line; | |
2228 | best_index = i; | |
2229 | } | |
2230 | } | |
2231 | ||
2232 | /* If we got here, we didn't get an exact match. */ | |
2233 | ||
2234 | *exact_match = 0; | |
2235 | return best_index; | |
2236 | } | |
2237 | ||
2238 | int | |
fba45db2 | 2239 | find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr) |
c906108c SS |
2240 | { |
2241 | struct symtab_and_line sal; | |
2242 | sal = find_pc_line (pc, 0); | |
2243 | *startptr = sal.pc; | |
2244 | *endptr = sal.end; | |
2245 | return sal.symtab != 0; | |
2246 | } | |
2247 | ||
2248 | /* Given a function symbol SYM, find the symtab and line for the start | |
2249 | of the function. | |
2250 | If the argument FUNFIRSTLINE is nonzero, we want the first line | |
2251 | of real code inside the function. */ | |
2252 | ||
50641945 | 2253 | struct symtab_and_line |
fba45db2 | 2254 | find_function_start_sal (struct symbol *sym, int funfirstline) |
c906108c SS |
2255 | { |
2256 | CORE_ADDR pc; | |
2257 | struct symtab_and_line sal; | |
2258 | ||
2259 | pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)); | |
2260 | fixup_symbol_section (sym, NULL); | |
2261 | if (funfirstline) | |
c5aa993b | 2262 | { /* skip "first line" of function (which is actually its prologue) */ |
c906108c SS |
2263 | asection *section = SYMBOL_BFD_SECTION (sym); |
2264 | /* If function is in an unmapped overlay, use its unmapped LMA | |
c5aa993b | 2265 | address, so that SKIP_PROLOGUE has something unique to work on */ |
c906108c SS |
2266 | if (section_is_overlay (section) && |
2267 | !section_is_mapped (section)) | |
2268 | pc = overlay_unmapped_address (pc, section); | |
2269 | ||
2270 | pc += FUNCTION_START_OFFSET; | |
b83266a0 | 2271 | pc = SKIP_PROLOGUE (pc); |
c906108c SS |
2272 | |
2273 | /* For overlays, map pc back into its mapped VMA range */ | |
2274 | pc = overlay_mapped_address (pc, section); | |
2275 | } | |
2276 | sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0); | |
2277 | ||
2278 | #ifdef PROLOGUE_FIRSTLINE_OVERLAP | |
2279 | /* Convex: no need to suppress code on first line, if any */ | |
2280 | sal.pc = pc; | |
2281 | #else | |
2282 | /* Check if SKIP_PROLOGUE left us in mid-line, and the next | |
2283 | line is still part of the same function. */ | |
2284 | if (sal.pc != pc | |
2285 | && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end | |
2286 | && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym))) | |
2287 | { | |
2288 | /* First pc of next line */ | |
2289 | pc = sal.end; | |
2290 | /* Recalculate the line number (might not be N+1). */ | |
2291 | sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0); | |
2292 | } | |
2293 | sal.pc = pc; | |
2294 | #endif | |
2295 | ||
2296 | return sal; | |
2297 | } | |
50641945 | 2298 | |
c906108c SS |
2299 | /* If P is of the form "operator[ \t]+..." where `...' is |
2300 | some legitimate operator text, return a pointer to the | |
2301 | beginning of the substring of the operator text. | |
2302 | Otherwise, return "". */ | |
2303 | char * | |
fba45db2 | 2304 | operator_chars (char *p, char **end) |
c906108c SS |
2305 | { |
2306 | *end = ""; | |
2307 | if (strncmp (p, "operator", 8)) | |
2308 | return *end; | |
2309 | p += 8; | |
2310 | ||
2311 | /* Don't get faked out by `operator' being part of a longer | |
2312 | identifier. */ | |
c5aa993b | 2313 | if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0') |
c906108c SS |
2314 | return *end; |
2315 | ||
2316 | /* Allow some whitespace between `operator' and the operator symbol. */ | |
2317 | while (*p == ' ' || *p == '\t') | |
2318 | p++; | |
2319 | ||
2320 | /* Recognize 'operator TYPENAME'. */ | |
2321 | ||
c5aa993b | 2322 | if (isalpha (*p) || *p == '_' || *p == '$') |
c906108c | 2323 | { |
c5aa993b JM |
2324 | register char *q = p + 1; |
2325 | while (isalnum (*q) || *q == '_' || *q == '$') | |
c906108c SS |
2326 | q++; |
2327 | *end = q; | |
2328 | return p; | |
2329 | } | |
2330 | ||
53e8ad3d MS |
2331 | while (*p) |
2332 | switch (*p) | |
2333 | { | |
2334 | case '\\': /* regexp quoting */ | |
2335 | if (p[1] == '*') | |
2336 | { | |
2337 | if (p[2] == '=') /* 'operator\*=' */ | |
2338 | *end = p + 3; | |
2339 | else /* 'operator\*' */ | |
2340 | *end = p + 2; | |
2341 | return p; | |
2342 | } | |
2343 | else if (p[1] == '[') | |
2344 | { | |
2345 | if (p[2] == ']') | |
2346 | error ("mismatched quoting on brackets, try 'operator\\[\\]'"); | |
2347 | else if (p[2] == '\\' && p[3] == ']') | |
2348 | { | |
2349 | *end = p + 4; /* 'operator\[\]' */ | |
2350 | return p; | |
2351 | } | |
2352 | else | |
2353 | error ("nothing is allowed between '[' and ']'"); | |
2354 | } | |
2355 | else | |
2356 | { | |
2357 | /* Gratuitous qoute: skip it and move on. */ | |
2358 | p++; | |
2359 | continue; | |
2360 | } | |
2361 | break; | |
2362 | case '!': | |
2363 | case '=': | |
2364 | case '*': | |
2365 | case '/': | |
2366 | case '%': | |
2367 | case '^': | |
2368 | if (p[1] == '=') | |
2369 | *end = p + 2; | |
2370 | else | |
2371 | *end = p + 1; | |
2372 | return p; | |
2373 | case '<': | |
2374 | case '>': | |
2375 | case '+': | |
2376 | case '-': | |
2377 | case '&': | |
2378 | case '|': | |
2379 | if (p[0] == '-' && p[1] == '>') | |
2380 | { | |
2381 | /* Struct pointer member operator 'operator->'. */ | |
2382 | if (p[2] == '*') | |
2383 | { | |
2384 | *end = p + 3; /* 'operator->*' */ | |
2385 | return p; | |
2386 | } | |
2387 | else if (p[2] == '\\') | |
2388 | { | |
2389 | *end = p + 4; /* Hopefully 'operator->\*' */ | |
2390 | return p; | |
2391 | } | |
2392 | else | |
2393 | { | |
2394 | *end = p + 2; /* 'operator->' */ | |
2395 | return p; | |
2396 | } | |
2397 | } | |
2398 | if (p[1] == '=' || p[1] == p[0]) | |
2399 | *end = p + 2; | |
2400 | else | |
2401 | *end = p + 1; | |
2402 | return p; | |
2403 | case '~': | |
2404 | case ',': | |
c5aa993b | 2405 | *end = p + 1; |
53e8ad3d MS |
2406 | return p; |
2407 | case '(': | |
2408 | if (p[1] != ')') | |
2409 | error ("`operator ()' must be specified without whitespace in `()'"); | |
c5aa993b | 2410 | *end = p + 2; |
53e8ad3d MS |
2411 | return p; |
2412 | case '?': | |
2413 | if (p[1] != ':') | |
2414 | error ("`operator ?:' must be specified without whitespace in `?:'"); | |
2415 | *end = p + 2; | |
2416 | return p; | |
2417 | case '[': | |
2418 | if (p[1] != ']') | |
2419 | error ("`operator []' must be specified without whitespace in `[]'"); | |
2420 | *end = p + 2; | |
2421 | return p; | |
2422 | default: | |
2423 | error ("`operator %s' not supported", p); | |
2424 | break; | |
2425 | } | |
2426 | ||
c906108c SS |
2427 | *end = ""; |
2428 | return *end; | |
2429 | } | |
c906108c | 2430 | \f |
c5aa993b | 2431 | |
c94fdfd0 EZ |
2432 | /* If FILE is not already in the table of files, return zero; |
2433 | otherwise return non-zero. Optionally add FILE to the table if ADD | |
2434 | is non-zero. If *FIRST is non-zero, forget the old table | |
2435 | contents. */ | |
2436 | static int | |
2437 | filename_seen (const char *file, int add, int *first) | |
c906108c | 2438 | { |
c94fdfd0 EZ |
2439 | /* Table of files seen so far. */ |
2440 | static const char **tab = NULL; | |
c906108c SS |
2441 | /* Allocated size of tab in elements. |
2442 | Start with one 256-byte block (when using GNU malloc.c). | |
2443 | 24 is the malloc overhead when range checking is in effect. */ | |
2444 | static int tab_alloc_size = (256 - 24) / sizeof (char *); | |
2445 | /* Current size of tab in elements. */ | |
2446 | static int tab_cur_size; | |
c94fdfd0 | 2447 | const char **p; |
c906108c SS |
2448 | |
2449 | if (*first) | |
2450 | { | |
2451 | if (tab == NULL) | |
c94fdfd0 | 2452 | tab = (const char **) xmalloc (tab_alloc_size * sizeof (*tab)); |
c906108c SS |
2453 | tab_cur_size = 0; |
2454 | } | |
2455 | ||
c94fdfd0 | 2456 | /* Is FILE in tab? */ |
c906108c | 2457 | for (p = tab; p < tab + tab_cur_size; p++) |
c94fdfd0 EZ |
2458 | if (strcmp (*p, file) == 0) |
2459 | return 1; | |
2460 | ||
2461 | /* No; maybe add it to tab. */ | |
2462 | if (add) | |
c906108c | 2463 | { |
c94fdfd0 EZ |
2464 | if (tab_cur_size == tab_alloc_size) |
2465 | { | |
2466 | tab_alloc_size *= 2; | |
2467 | tab = (const char **) xrealloc ((char *) tab, | |
2468 | tab_alloc_size * sizeof (*tab)); | |
2469 | } | |
2470 | tab[tab_cur_size++] = file; | |
c906108c | 2471 | } |
c906108c | 2472 | |
c94fdfd0 EZ |
2473 | return 0; |
2474 | } | |
2475 | ||
2476 | /* Slave routine for sources_info. Force line breaks at ,'s. | |
2477 | NAME is the name to print and *FIRST is nonzero if this is the first | |
2478 | name printed. Set *FIRST to zero. */ | |
2479 | static void | |
2480 | output_source_filename (char *name, int *first) | |
2481 | { | |
2482 | /* Since a single source file can result in several partial symbol | |
2483 | tables, we need to avoid printing it more than once. Note: if | |
2484 | some of the psymtabs are read in and some are not, it gets | |
2485 | printed both under "Source files for which symbols have been | |
2486 | read" and "Source files for which symbols will be read in on | |
2487 | demand". I consider this a reasonable way to deal with the | |
2488 | situation. I'm not sure whether this can also happen for | |
2489 | symtabs; it doesn't hurt to check. */ | |
2490 | ||
2491 | /* Was NAME already seen? */ | |
2492 | if (filename_seen (name, 1, first)) | |
2493 | { | |
2494 | /* Yes; don't print it again. */ | |
2495 | return; | |
2496 | } | |
2497 | /* No; print it and reset *FIRST. */ | |
c906108c SS |
2498 | if (*first) |
2499 | { | |
2500 | *first = 0; | |
2501 | } | |
2502 | else | |
2503 | { | |
2504 | printf_filtered (", "); | |
2505 | } | |
2506 | ||
2507 | wrap_here (""); | |
2508 | fputs_filtered (name, gdb_stdout); | |
c5aa993b | 2509 | } |
c906108c SS |
2510 | |
2511 | static void | |
fba45db2 | 2512 | sources_info (char *ignore, int from_tty) |
c906108c SS |
2513 | { |
2514 | register struct symtab *s; | |
2515 | register struct partial_symtab *ps; | |
2516 | register struct objfile *objfile; | |
2517 | int first; | |
c5aa993b | 2518 | |
c906108c SS |
2519 | if (!have_full_symbols () && !have_partial_symbols ()) |
2520 | { | |
e85428fc | 2521 | error ("No symbol table is loaded. Use the \"file\" command."); |
c906108c | 2522 | } |
c5aa993b | 2523 | |
c906108c SS |
2524 | printf_filtered ("Source files for which symbols have been read in:\n\n"); |
2525 | ||
2526 | first = 1; | |
2527 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
2528 | { |
2529 | output_source_filename (s->filename, &first); | |
2530 | } | |
c906108c | 2531 | printf_filtered ("\n\n"); |
c5aa993b | 2532 | |
c906108c SS |
2533 | printf_filtered ("Source files for which symbols will be read in on demand:\n\n"); |
2534 | ||
2535 | first = 1; | |
2536 | ALL_PSYMTABS (objfile, ps) | |
c5aa993b JM |
2537 | { |
2538 | if (!ps->readin) | |
2539 | { | |
2540 | output_source_filename (ps->filename, &first); | |
2541 | } | |
2542 | } | |
c906108c SS |
2543 | printf_filtered ("\n"); |
2544 | } | |
2545 | ||
2546 | static int | |
fd118b61 | 2547 | file_matches (char *file, char *files[], int nfiles) |
c906108c SS |
2548 | { |
2549 | int i; | |
2550 | ||
2551 | if (file != NULL && nfiles != 0) | |
2552 | { | |
2553 | for (i = 0; i < nfiles; i++) | |
c5aa993b | 2554 | { |
31889e00 | 2555 | if (strcmp (files[i], lbasename (file)) == 0) |
c5aa993b JM |
2556 | return 1; |
2557 | } | |
c906108c SS |
2558 | } |
2559 | else if (nfiles == 0) | |
2560 | return 1; | |
2561 | return 0; | |
2562 | } | |
2563 | ||
2564 | /* Free any memory associated with a search. */ | |
2565 | void | |
fba45db2 | 2566 | free_search_symbols (struct symbol_search *symbols) |
c906108c SS |
2567 | { |
2568 | struct symbol_search *p; | |
2569 | struct symbol_search *next; | |
2570 | ||
2571 | for (p = symbols; p != NULL; p = next) | |
2572 | { | |
2573 | next = p->next; | |
b8c9b27d | 2574 | xfree (p); |
c906108c SS |
2575 | } |
2576 | } | |
2577 | ||
5bd98722 AC |
2578 | static void |
2579 | do_free_search_symbols_cleanup (void *symbols) | |
2580 | { | |
2581 | free_search_symbols (symbols); | |
2582 | } | |
2583 | ||
2584 | struct cleanup * | |
2585 | make_cleanup_free_search_symbols (struct symbol_search *symbols) | |
2586 | { | |
2587 | return make_cleanup (do_free_search_symbols_cleanup, symbols); | |
2588 | } | |
2589 | ||
434d2d4f DJ |
2590 | /* Helper function for sort_search_symbols and qsort. Can only |
2591 | sort symbols, not minimal symbols. */ | |
2592 | static int | |
2593 | compare_search_syms (const void *sa, const void *sb) | |
2594 | { | |
2595 | struct symbol_search **sym_a = (struct symbol_search **) sa; | |
2596 | struct symbol_search **sym_b = (struct symbol_search **) sb; | |
2597 | ||
2598 | return strcmp (SYMBOL_SOURCE_NAME ((*sym_a)->symbol), | |
2599 | SYMBOL_SOURCE_NAME ((*sym_b)->symbol)); | |
2600 | } | |
2601 | ||
2602 | /* Sort the ``nfound'' symbols in the list after prevtail. Leave | |
2603 | prevtail where it is, but update its next pointer to point to | |
2604 | the first of the sorted symbols. */ | |
2605 | static struct symbol_search * | |
2606 | sort_search_symbols (struct symbol_search *prevtail, int nfound) | |
2607 | { | |
2608 | struct symbol_search **symbols, *symp, *old_next; | |
2609 | int i; | |
2610 | ||
2611 | symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *) | |
2612 | * nfound); | |
2613 | symp = prevtail->next; | |
2614 | for (i = 0; i < nfound; i++) | |
2615 | { | |
2616 | symbols[i] = symp; | |
2617 | symp = symp->next; | |
2618 | } | |
2619 | /* Generally NULL. */ | |
2620 | old_next = symp; | |
2621 | ||
2622 | qsort (symbols, nfound, sizeof (struct symbol_search *), | |
2623 | compare_search_syms); | |
2624 | ||
2625 | symp = prevtail; | |
2626 | for (i = 0; i < nfound; i++) | |
2627 | { | |
2628 | symp->next = symbols[i]; | |
2629 | symp = symp->next; | |
2630 | } | |
2631 | symp->next = old_next; | |
2632 | ||
8ed32cc0 | 2633 | xfree (symbols); |
434d2d4f DJ |
2634 | return symp; |
2635 | } | |
5bd98722 | 2636 | |
c906108c SS |
2637 | /* Search the symbol table for matches to the regular expression REGEXP, |
2638 | returning the results in *MATCHES. | |
2639 | ||
2640 | Only symbols of KIND are searched: | |
c5aa993b JM |
2641 | FUNCTIONS_NAMESPACE - search all functions |
2642 | TYPES_NAMESPACE - search all type names | |
2643 | METHODS_NAMESPACE - search all methods NOT IMPLEMENTED | |
2644 | VARIABLES_NAMESPACE - search all symbols, excluding functions, type names, | |
2645 | and constants (enums) | |
c906108c SS |
2646 | |
2647 | free_search_symbols should be called when *MATCHES is no longer needed. | |
434d2d4f DJ |
2648 | |
2649 | The results are sorted locally; each symtab's global and static blocks are | |
2650 | separately alphabetized. | |
c5aa993b | 2651 | */ |
c906108c | 2652 | void |
fd118b61 KB |
2653 | search_symbols (char *regexp, namespace_enum kind, int nfiles, char *files[], |
2654 | struct symbol_search **matches) | |
c906108c SS |
2655 | { |
2656 | register struct symtab *s; | |
2657 | register struct partial_symtab *ps; | |
2658 | register struct blockvector *bv; | |
2659 | struct blockvector *prev_bv = 0; | |
2660 | register struct block *b; | |
2661 | register int i = 0; | |
2662 | register int j; | |
2663 | register struct symbol *sym; | |
2664 | struct partial_symbol **psym; | |
2665 | struct objfile *objfile; | |
2666 | struct minimal_symbol *msymbol; | |
2667 | char *val; | |
2668 | int found_misc = 0; | |
2669 | static enum minimal_symbol_type types[] | |
c5aa993b JM |
2670 | = |
2671 | {mst_data, mst_text, mst_abs, mst_unknown}; | |
c906108c | 2672 | static enum minimal_symbol_type types2[] |
c5aa993b JM |
2673 | = |
2674 | {mst_bss, mst_file_text, mst_abs, mst_unknown}; | |
c906108c | 2675 | static enum minimal_symbol_type types3[] |
c5aa993b JM |
2676 | = |
2677 | {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown}; | |
c906108c | 2678 | static enum minimal_symbol_type types4[] |
c5aa993b JM |
2679 | = |
2680 | {mst_file_bss, mst_text, mst_abs, mst_unknown}; | |
c906108c SS |
2681 | enum minimal_symbol_type ourtype; |
2682 | enum minimal_symbol_type ourtype2; | |
2683 | enum minimal_symbol_type ourtype3; | |
2684 | enum minimal_symbol_type ourtype4; | |
2685 | struct symbol_search *sr; | |
2686 | struct symbol_search *psr; | |
2687 | struct symbol_search *tail; | |
2688 | struct cleanup *old_chain = NULL; | |
2689 | ||
993f3aa5 | 2690 | if (kind < VARIABLES_NAMESPACE) |
c906108c SS |
2691 | error ("must search on specific namespace"); |
2692 | ||
52204a0b DT |
2693 | ourtype = types[(int) (kind - VARIABLES_NAMESPACE)]; |
2694 | ourtype2 = types2[(int) (kind - VARIABLES_NAMESPACE)]; | |
2695 | ourtype3 = types3[(int) (kind - VARIABLES_NAMESPACE)]; | |
2696 | ourtype4 = types4[(int) (kind - VARIABLES_NAMESPACE)]; | |
c906108c SS |
2697 | |
2698 | sr = *matches = NULL; | |
2699 | tail = NULL; | |
2700 | ||
2701 | if (regexp != NULL) | |
2702 | { | |
2703 | /* Make sure spacing is right for C++ operators. | |
2704 | This is just a courtesy to make the matching less sensitive | |
2705 | to how many spaces the user leaves between 'operator' | |
2706 | and <TYPENAME> or <OPERATOR>. */ | |
2707 | char *opend; | |
2708 | char *opname = operator_chars (regexp, &opend); | |
2709 | if (*opname) | |
c5aa993b JM |
2710 | { |
2711 | int fix = -1; /* -1 means ok; otherwise number of spaces needed. */ | |
2712 | if (isalpha (*opname) || *opname == '_' || *opname == '$') | |
2713 | { | |
2714 | /* There should 1 space between 'operator' and 'TYPENAME'. */ | |
2715 | if (opname[-1] != ' ' || opname[-2] == ' ') | |
2716 | fix = 1; | |
2717 | } | |
2718 | else | |
2719 | { | |
2720 | /* There should 0 spaces between 'operator' and 'OPERATOR'. */ | |
2721 | if (opname[-1] == ' ') | |
2722 | fix = 0; | |
2723 | } | |
2724 | /* If wrong number of spaces, fix it. */ | |
2725 | if (fix >= 0) | |
2726 | { | |
045f55a6 | 2727 | char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1); |
c5aa993b JM |
2728 | sprintf (tmp, "operator%.*s%s", fix, " ", opname); |
2729 | regexp = tmp; | |
2730 | } | |
2731 | } | |
2732 | ||
c906108c | 2733 | if (0 != (val = re_comp (regexp))) |
c5aa993b | 2734 | error ("Invalid regexp (%s): %s", val, regexp); |
c906108c SS |
2735 | } |
2736 | ||
2737 | /* Search through the partial symtabs *first* for all symbols | |
2738 | matching the regexp. That way we don't have to reproduce all of | |
2739 | the machinery below. */ | |
2740 | ||
2741 | ALL_PSYMTABS (objfile, ps) | |
c5aa993b JM |
2742 | { |
2743 | struct partial_symbol **bound, **gbound, **sbound; | |
2744 | int keep_going = 1; | |
2745 | ||
2746 | if (ps->readin) | |
2747 | continue; | |
2748 | ||
2749 | gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms; | |
2750 | sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms; | |
2751 | bound = gbound; | |
2752 | ||
2753 | /* Go through all of the symbols stored in a partial | |
2754 | symtab in one loop. */ | |
2755 | psym = objfile->global_psymbols.list + ps->globals_offset; | |
2756 | while (keep_going) | |
2757 | { | |
2758 | if (psym >= bound) | |
2759 | { | |
2760 | if (bound == gbound && ps->n_static_syms != 0) | |
2761 | { | |
2762 | psym = objfile->static_psymbols.list + ps->statics_offset; | |
2763 | bound = sbound; | |
2764 | } | |
2765 | else | |
2766 | keep_going = 0; | |
2767 | continue; | |
2768 | } | |
2769 | else | |
2770 | { | |
2771 | QUIT; | |
2772 | ||
2773 | /* If it would match (logic taken from loop below) | |
2774 | load the file and go on to the next one */ | |
2775 | if (file_matches (ps->filename, files, nfiles) | |
2776 | && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (*psym)) | |
2777 | && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (*psym) != LOC_TYPEDEF | |
2778 | && SYMBOL_CLASS (*psym) != LOC_BLOCK) | |
2779 | || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK) | |
2780 | || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_TYPEDEF) | |
2781 | || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK)))) | |
2782 | { | |
2783 | PSYMTAB_TO_SYMTAB (ps); | |
2784 | keep_going = 0; | |
2785 | } | |
2786 | } | |
2787 | psym++; | |
2788 | } | |
2789 | } | |
c906108c SS |
2790 | |
2791 | /* Here, we search through the minimal symbol tables for functions | |
2792 | and variables that match, and force their symbols to be read. | |
2793 | This is in particular necessary for demangled variable names, | |
2794 | which are no longer put into the partial symbol tables. | |
2795 | The symbol will then be found during the scan of symtabs below. | |
2796 | ||
2797 | For functions, find_pc_symtab should succeed if we have debug info | |
2798 | for the function, for variables we have to call lookup_symbol | |
2799 | to determine if the variable has debug info. | |
2800 | If the lookup fails, set found_misc so that we will rescan to print | |
2801 | any matching symbols without debug info. | |
c5aa993b | 2802 | */ |
c906108c SS |
2803 | |
2804 | if (nfiles == 0 && (kind == VARIABLES_NAMESPACE || kind == FUNCTIONS_NAMESPACE)) | |
2805 | { | |
2806 | ALL_MSYMBOLS (objfile, msymbol) | |
c5aa993b JM |
2807 | { |
2808 | if (MSYMBOL_TYPE (msymbol) == ourtype || | |
2809 | MSYMBOL_TYPE (msymbol) == ourtype2 || | |
2810 | MSYMBOL_TYPE (msymbol) == ourtype3 || | |
2811 | MSYMBOL_TYPE (msymbol) == ourtype4) | |
2812 | { | |
2813 | if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol)) | |
2814 | { | |
2815 | if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))) | |
2816 | { | |
2817 | if (kind == FUNCTIONS_NAMESPACE | |
2818 | || lookup_symbol (SYMBOL_NAME (msymbol), | |
2819 | (struct block *) NULL, | |
2820 | VAR_NAMESPACE, | |
2821 | 0, (struct symtab **) NULL) == NULL) | |
2822 | found_misc = 1; | |
2823 | } | |
2824 | } | |
2825 | } | |
2826 | } | |
c906108c SS |
2827 | } |
2828 | ||
2829 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
2830 | { |
2831 | bv = BLOCKVECTOR (s); | |
2832 | /* Often many files share a blockvector. | |
2833 | Scan each blockvector only once so that | |
2834 | we don't get every symbol many times. | |
2835 | It happens that the first symtab in the list | |
2836 | for any given blockvector is the main file. */ | |
2837 | if (bv != prev_bv) | |
2838 | for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++) | |
2839 | { | |
434d2d4f DJ |
2840 | struct symbol_search *prevtail = tail; |
2841 | int nfound = 0; | |
c5aa993b | 2842 | b = BLOCKVECTOR_BLOCK (bv, i); |
261397f8 | 2843 | ALL_BLOCK_SYMBOLS (b, j, sym) |
c5aa993b JM |
2844 | { |
2845 | QUIT; | |
c5aa993b JM |
2846 | if (file_matches (s->filename, files, nfiles) |
2847 | && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (sym)) | |
2848 | && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (sym) != LOC_TYPEDEF | |
2849 | && SYMBOL_CLASS (sym) != LOC_BLOCK | |
2850 | && SYMBOL_CLASS (sym) != LOC_CONST) | |
2851 | || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK) | |
2852 | || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
2853 | || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK)))) | |
2854 | { | |
2855 | /* match */ | |
2856 | psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search)); | |
2857 | psr->block = i; | |
2858 | psr->symtab = s; | |
2859 | psr->symbol = sym; | |
2860 | psr->msymbol = NULL; | |
2861 | psr->next = NULL; | |
2862 | if (tail == NULL) | |
434d2d4f | 2863 | sr = psr; |
c5aa993b JM |
2864 | else |
2865 | tail->next = psr; | |
2866 | tail = psr; | |
434d2d4f DJ |
2867 | nfound ++; |
2868 | } | |
2869 | } | |
2870 | if (nfound > 0) | |
2871 | { | |
2872 | if (prevtail == NULL) | |
2873 | { | |
2874 | struct symbol_search dummy; | |
2875 | ||
2876 | dummy.next = sr; | |
2877 | tail = sort_search_symbols (&dummy, nfound); | |
2878 | sr = dummy.next; | |
2879 | ||
2880 | old_chain = make_cleanup_free_search_symbols (sr); | |
c5aa993b | 2881 | } |
434d2d4f DJ |
2882 | else |
2883 | tail = sort_search_symbols (prevtail, nfound); | |
c5aa993b JM |
2884 | } |
2885 | } | |
2886 | prev_bv = bv; | |
2887 | } | |
c906108c SS |
2888 | |
2889 | /* If there are no eyes, avoid all contact. I mean, if there are | |
2890 | no debug symbols, then print directly from the msymbol_vector. */ | |
2891 | ||
2892 | if (found_misc || kind != FUNCTIONS_NAMESPACE) | |
2893 | { | |
2894 | ALL_MSYMBOLS (objfile, msymbol) | |
c5aa993b JM |
2895 | { |
2896 | if (MSYMBOL_TYPE (msymbol) == ourtype || | |
2897 | MSYMBOL_TYPE (msymbol) == ourtype2 || | |
2898 | MSYMBOL_TYPE (msymbol) == ourtype3 || | |
2899 | MSYMBOL_TYPE (msymbol) == ourtype4) | |
2900 | { | |
2901 | if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol)) | |
2902 | { | |
2903 | /* Functions: Look up by address. */ | |
2904 | if (kind != FUNCTIONS_NAMESPACE || | |
2905 | (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))) | |
2906 | { | |
2907 | /* Variables/Absolutes: Look up by name */ | |
2908 | if (lookup_symbol (SYMBOL_NAME (msymbol), | |
2909 | (struct block *) NULL, VAR_NAMESPACE, | |
2910 | 0, (struct symtab **) NULL) == NULL) | |
2911 | { | |
2912 | /* match */ | |
2913 | psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search)); | |
2914 | psr->block = i; | |
2915 | psr->msymbol = msymbol; | |
2916 | psr->symtab = NULL; | |
2917 | psr->symbol = NULL; | |
2918 | psr->next = NULL; | |
2919 | if (tail == NULL) | |
2920 | { | |
2921 | sr = psr; | |
5bd98722 | 2922 | old_chain = make_cleanup_free_search_symbols (sr); |
c5aa993b JM |
2923 | } |
2924 | else | |
2925 | tail->next = psr; | |
2926 | tail = psr; | |
2927 | } | |
2928 | } | |
2929 | } | |
2930 | } | |
2931 | } | |
c906108c SS |
2932 | } |
2933 | ||
2934 | *matches = sr; | |
2935 | if (sr != NULL) | |
2936 | discard_cleanups (old_chain); | |
2937 | } | |
2938 | ||
2939 | /* Helper function for symtab_symbol_info, this function uses | |
2940 | the data returned from search_symbols() to print information | |
2941 | regarding the match to gdb_stdout. | |
c5aa993b | 2942 | */ |
c906108c | 2943 | static void |
fba45db2 KB |
2944 | print_symbol_info (namespace_enum kind, struct symtab *s, struct symbol *sym, |
2945 | int block, char *last) | |
c906108c SS |
2946 | { |
2947 | if (last == NULL || strcmp (last, s->filename) != 0) | |
2948 | { | |
2949 | fputs_filtered ("\nFile ", gdb_stdout); | |
2950 | fputs_filtered (s->filename, gdb_stdout); | |
2951 | fputs_filtered (":\n", gdb_stdout); | |
2952 | } | |
2953 | ||
2954 | if (kind != TYPES_NAMESPACE && block == STATIC_BLOCK) | |
2955 | printf_filtered ("static "); | |
c5aa993b | 2956 | |
c906108c SS |
2957 | /* Typedef that is not a C++ class */ |
2958 | if (kind == TYPES_NAMESPACE | |
2959 | && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE) | |
a5238fbc | 2960 | typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout); |
c906108c | 2961 | /* variable, func, or typedef-that-is-c++-class */ |
c5aa993b JM |
2962 | else if (kind < TYPES_NAMESPACE || |
2963 | (kind == TYPES_NAMESPACE && | |
2964 | SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE)) | |
c906108c SS |
2965 | { |
2966 | type_print (SYMBOL_TYPE (sym), | |
c5aa993b JM |
2967 | (SYMBOL_CLASS (sym) == LOC_TYPEDEF |
2968 | ? "" : SYMBOL_SOURCE_NAME (sym)), | |
2969 | gdb_stdout, 0); | |
c906108c SS |
2970 | |
2971 | printf_filtered (";\n"); | |
2972 | } | |
c906108c SS |
2973 | } |
2974 | ||
2975 | /* This help function for symtab_symbol_info() prints information | |
2976 | for non-debugging symbols to gdb_stdout. | |
c5aa993b | 2977 | */ |
c906108c | 2978 | static void |
fba45db2 | 2979 | print_msymbol_info (struct minimal_symbol *msymbol) |
c906108c | 2980 | { |
3ac4495a MS |
2981 | char *tmp; |
2982 | ||
2983 | if (TARGET_ADDR_BIT <= 32) | |
14a5e767 AC |
2984 | tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol) |
2985 | & (CORE_ADDR) 0xffffffff, | |
2986 | "08l"); | |
3ac4495a | 2987 | else |
14a5e767 AC |
2988 | tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol), |
2989 | "016l"); | |
3ac4495a MS |
2990 | printf_filtered ("%s %s\n", |
2991 | tmp, SYMBOL_SOURCE_NAME (msymbol)); | |
c906108c SS |
2992 | } |
2993 | ||
2994 | /* This is the guts of the commands "info functions", "info types", and | |
2995 | "info variables". It calls search_symbols to find all matches and then | |
2996 | print_[m]symbol_info to print out some useful information about the | |
2997 | matches. | |
c5aa993b | 2998 | */ |
c906108c | 2999 | static void |
fba45db2 | 3000 | symtab_symbol_info (char *regexp, namespace_enum kind, int from_tty) |
c906108c SS |
3001 | { |
3002 | static char *classnames[] | |
c5aa993b JM |
3003 | = |
3004 | {"variable", "function", "type", "method"}; | |
c906108c SS |
3005 | struct symbol_search *symbols; |
3006 | struct symbol_search *p; | |
3007 | struct cleanup *old_chain; | |
3008 | char *last_filename = NULL; | |
3009 | int first = 1; | |
3010 | ||
3011 | /* must make sure that if we're interrupted, symbols gets freed */ | |
3012 | search_symbols (regexp, kind, 0, (char **) NULL, &symbols); | |
5bd98722 | 3013 | old_chain = make_cleanup_free_search_symbols (symbols); |
c906108c SS |
3014 | |
3015 | printf_filtered (regexp | |
c5aa993b JM |
3016 | ? "All %ss matching regular expression \"%s\":\n" |
3017 | : "All defined %ss:\n", | |
52204a0b | 3018 | classnames[(int) (kind - VARIABLES_NAMESPACE)], regexp); |
c906108c SS |
3019 | |
3020 | for (p = symbols; p != NULL; p = p->next) | |
3021 | { | |
3022 | QUIT; | |
3023 | ||
3024 | if (p->msymbol != NULL) | |
c5aa993b JM |
3025 | { |
3026 | if (first) | |
3027 | { | |
3028 | printf_filtered ("\nNon-debugging symbols:\n"); | |
3029 | first = 0; | |
3030 | } | |
3031 | print_msymbol_info (p->msymbol); | |
3032 | } | |
c906108c | 3033 | else |
c5aa993b JM |
3034 | { |
3035 | print_symbol_info (kind, | |
3036 | p->symtab, | |
3037 | p->symbol, | |
3038 | p->block, | |
3039 | last_filename); | |
3040 | last_filename = p->symtab->filename; | |
3041 | } | |
c906108c SS |
3042 | } |
3043 | ||
3044 | do_cleanups (old_chain); | |
3045 | } | |
3046 | ||
3047 | static void | |
fba45db2 | 3048 | variables_info (char *regexp, int from_tty) |
c906108c SS |
3049 | { |
3050 | symtab_symbol_info (regexp, VARIABLES_NAMESPACE, from_tty); | |
3051 | } | |
3052 | ||
3053 | static void | |
fba45db2 | 3054 | functions_info (char *regexp, int from_tty) |
c906108c SS |
3055 | { |
3056 | symtab_symbol_info (regexp, FUNCTIONS_NAMESPACE, from_tty); | |
3057 | } | |
3058 | ||
357e46e7 | 3059 | |
c906108c | 3060 | static void |
fba45db2 | 3061 | types_info (char *regexp, int from_tty) |
c906108c SS |
3062 | { |
3063 | symtab_symbol_info (regexp, TYPES_NAMESPACE, from_tty); | |
3064 | } | |
3065 | ||
c906108c | 3066 | /* Breakpoint all functions matching regular expression. */ |
8926118c | 3067 | |
8b93c638 | 3068 | void |
fba45db2 | 3069 | rbreak_command_wrapper (char *regexp, int from_tty) |
8b93c638 JM |
3070 | { |
3071 | rbreak_command (regexp, from_tty); | |
3072 | } | |
8926118c | 3073 | |
c906108c | 3074 | static void |
fba45db2 | 3075 | rbreak_command (char *regexp, int from_tty) |
c906108c SS |
3076 | { |
3077 | struct symbol_search *ss; | |
3078 | struct symbol_search *p; | |
3079 | struct cleanup *old_chain; | |
3080 | ||
3081 | search_symbols (regexp, FUNCTIONS_NAMESPACE, 0, (char **) NULL, &ss); | |
5bd98722 | 3082 | old_chain = make_cleanup_free_search_symbols (ss); |
c906108c SS |
3083 | |
3084 | for (p = ss; p != NULL; p = p->next) | |
3085 | { | |
3086 | if (p->msymbol == NULL) | |
c5aa993b JM |
3087 | { |
3088 | char *string = (char *) alloca (strlen (p->symtab->filename) | |
3089 | + strlen (SYMBOL_NAME (p->symbol)) | |
3090 | + 4); | |
3091 | strcpy (string, p->symtab->filename); | |
3092 | strcat (string, ":'"); | |
3093 | strcat (string, SYMBOL_NAME (p->symbol)); | |
3094 | strcat (string, "'"); | |
3095 | break_command (string, from_tty); | |
3096 | print_symbol_info (FUNCTIONS_NAMESPACE, | |
3097 | p->symtab, | |
3098 | p->symbol, | |
3099 | p->block, | |
3100 | p->symtab->filename); | |
3101 | } | |
c906108c | 3102 | else |
c5aa993b JM |
3103 | { |
3104 | break_command (SYMBOL_NAME (p->msymbol), from_tty); | |
3105 | printf_filtered ("<function, no debug info> %s;\n", | |
3106 | SYMBOL_SOURCE_NAME (p->msymbol)); | |
3107 | } | |
c906108c SS |
3108 | } |
3109 | ||
3110 | do_cleanups (old_chain); | |
3111 | } | |
c906108c | 3112 | \f |
c5aa993b | 3113 | |
c906108c SS |
3114 | /* Return Nonzero if block a is lexically nested within block b, |
3115 | or if a and b have the same pc range. | |
3116 | Return zero otherwise. */ | |
3117 | int | |
fba45db2 | 3118 | contained_in (struct block *a, struct block *b) |
c906108c SS |
3119 | { |
3120 | if (!a || !b) | |
3121 | return 0; | |
3122 | return BLOCK_START (a) >= BLOCK_START (b) | |
c5aa993b | 3123 | && BLOCK_END (a) <= BLOCK_END (b); |
c906108c | 3124 | } |
c906108c | 3125 | \f |
c5aa993b | 3126 | |
c906108c SS |
3127 | /* Helper routine for make_symbol_completion_list. */ |
3128 | ||
3129 | static int return_val_size; | |
3130 | static int return_val_index; | |
3131 | static char **return_val; | |
3132 | ||
3133 | #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \ | |
3134 | do { \ | |
3135 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \ | |
3136 | /* Put only the mangled name on the list. */ \ | |
3137 | /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \ | |
3138 | /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \ | |
3139 | completion_list_add_name \ | |
3140 | (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \ | |
3141 | else \ | |
3142 | completion_list_add_name \ | |
3143 | (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \ | |
3144 | } while (0) | |
3145 | ||
3146 | /* Test to see if the symbol specified by SYMNAME (which is already | |
c5aa993b JM |
3147 | demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN |
3148 | characters. If so, add it to the current completion list. */ | |
c906108c SS |
3149 | |
3150 | static void | |
fba45db2 KB |
3151 | completion_list_add_name (char *symname, char *sym_text, int sym_text_len, |
3152 | char *text, char *word) | |
c906108c SS |
3153 | { |
3154 | int newsize; | |
3155 | int i; | |
3156 | ||
3157 | /* clip symbols that cannot match */ | |
3158 | ||
3159 | if (strncmp (symname, sym_text, sym_text_len) != 0) | |
3160 | { | |
3161 | return; | |
3162 | } | |
3163 | ||
c906108c SS |
3164 | /* We have a match for a completion, so add SYMNAME to the current list |
3165 | of matches. Note that the name is moved to freshly malloc'd space. */ | |
3166 | ||
3167 | { | |
3168 | char *new; | |
3169 | if (word == sym_text) | |
3170 | { | |
3171 | new = xmalloc (strlen (symname) + 5); | |
3172 | strcpy (new, symname); | |
3173 | } | |
3174 | else if (word > sym_text) | |
3175 | { | |
3176 | /* Return some portion of symname. */ | |
3177 | new = xmalloc (strlen (symname) + 5); | |
3178 | strcpy (new, symname + (word - sym_text)); | |
3179 | } | |
3180 | else | |
3181 | { | |
3182 | /* Return some of SYM_TEXT plus symname. */ | |
3183 | new = xmalloc (strlen (symname) + (sym_text - word) + 5); | |
3184 | strncpy (new, word, sym_text - word); | |
3185 | new[sym_text - word] = '\0'; | |
3186 | strcat (new, symname); | |
3187 | } | |
3188 | ||
c906108c SS |
3189 | if (return_val_index + 3 > return_val_size) |
3190 | { | |
3191 | newsize = (return_val_size *= 2) * sizeof (char *); | |
3192 | return_val = (char **) xrealloc ((char *) return_val, newsize); | |
3193 | } | |
3194 | return_val[return_val_index++] = new; | |
3195 | return_val[return_val_index] = NULL; | |
3196 | } | |
3197 | } | |
3198 | ||
c94fdfd0 EZ |
3199 | /* Return a NULL terminated array of all symbols (regardless of class) |
3200 | which begin by matching TEXT. If the answer is no symbols, then | |
3201 | the return value is an array which contains only a NULL pointer. | |
c906108c SS |
3202 | |
3203 | Problem: All of the symbols have to be copied because readline frees them. | |
3204 | I'm not going to worry about this; hopefully there won't be that many. */ | |
3205 | ||
3206 | char ** | |
fba45db2 | 3207 | make_symbol_completion_list (char *text, char *word) |
c906108c SS |
3208 | { |
3209 | register struct symbol *sym; | |
3210 | register struct symtab *s; | |
3211 | register struct partial_symtab *ps; | |
3212 | register struct minimal_symbol *msymbol; | |
3213 | register struct objfile *objfile; | |
3214 | register struct block *b, *surrounding_static_block = 0; | |
3215 | register int i, j; | |
3216 | struct partial_symbol **psym; | |
3217 | /* The symbol we are completing on. Points in same buffer as text. */ | |
3218 | char *sym_text; | |
3219 | /* Length of sym_text. */ | |
3220 | int sym_text_len; | |
3221 | ||
3222 | /* Now look for the symbol we are supposed to complete on. | |
3223 | FIXME: This should be language-specific. */ | |
3224 | { | |
3225 | char *p; | |
3226 | char quote_found; | |
3227 | char *quote_pos = NULL; | |
3228 | ||
3229 | /* First see if this is a quoted string. */ | |
3230 | quote_found = '\0'; | |
3231 | for (p = text; *p != '\0'; ++p) | |
3232 | { | |
3233 | if (quote_found != '\0') | |
3234 | { | |
3235 | if (*p == quote_found) | |
3236 | /* Found close quote. */ | |
3237 | quote_found = '\0'; | |
3238 | else if (*p == '\\' && p[1] == quote_found) | |
3239 | /* A backslash followed by the quote character | |
c5aa993b | 3240 | doesn't end the string. */ |
c906108c SS |
3241 | ++p; |
3242 | } | |
3243 | else if (*p == '\'' || *p == '"') | |
3244 | { | |
3245 | quote_found = *p; | |
3246 | quote_pos = p; | |
3247 | } | |
3248 | } | |
3249 | if (quote_found == '\'') | |
3250 | /* A string within single quotes can be a symbol, so complete on it. */ | |
3251 | sym_text = quote_pos + 1; | |
3252 | else if (quote_found == '"') | |
3253 | /* A double-quoted string is never a symbol, nor does it make sense | |
c5aa993b | 3254 | to complete it any other way. */ |
c94fdfd0 EZ |
3255 | { |
3256 | return_val = (char **) xmalloc (sizeof (char *)); | |
3257 | return_val[0] = NULL; | |
3258 | return return_val; | |
3259 | } | |
c906108c SS |
3260 | else |
3261 | { | |
3262 | /* It is not a quoted string. Break it based on the characters | |
3263 | which are in symbols. */ | |
3264 | while (p > text) | |
3265 | { | |
3266 | if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0') | |
3267 | --p; | |
3268 | else | |
3269 | break; | |
3270 | } | |
3271 | sym_text = p; | |
3272 | } | |
3273 | } | |
3274 | ||
3275 | sym_text_len = strlen (sym_text); | |
3276 | ||
3277 | return_val_size = 100; | |
3278 | return_val_index = 0; | |
3279 | return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *)); | |
3280 | return_val[0] = NULL; | |
3281 | ||
3282 | /* Look through the partial symtabs for all symbols which begin | |
3283 | by matching SYM_TEXT. Add each one that you find to the list. */ | |
3284 | ||
3285 | ALL_PSYMTABS (objfile, ps) | |
c5aa993b JM |
3286 | { |
3287 | /* If the psymtab's been read in we'll get it when we search | |
3288 | through the blockvector. */ | |
3289 | if (ps->readin) | |
3290 | continue; | |
3291 | ||
3292 | for (psym = objfile->global_psymbols.list + ps->globals_offset; | |
3293 | psym < (objfile->global_psymbols.list + ps->globals_offset | |
3294 | + ps->n_global_syms); | |
3295 | psym++) | |
3296 | { | |
3297 | /* If interrupted, then quit. */ | |
3298 | QUIT; | |
3299 | COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word); | |
3300 | } | |
3301 | ||
3302 | for (psym = objfile->static_psymbols.list + ps->statics_offset; | |
3303 | psym < (objfile->static_psymbols.list + ps->statics_offset | |
3304 | + ps->n_static_syms); | |
3305 | psym++) | |
3306 | { | |
3307 | QUIT; | |
3308 | COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word); | |
3309 | } | |
3310 | } | |
c906108c SS |
3311 | |
3312 | /* At this point scan through the misc symbol vectors and add each | |
3313 | symbol you find to the list. Eventually we want to ignore | |
3314 | anything that isn't a text symbol (everything else will be | |
3315 | handled by the psymtab code above). */ | |
3316 | ||
3317 | ALL_MSYMBOLS (objfile, msymbol) | |
c5aa993b JM |
3318 | { |
3319 | QUIT; | |
3320 | COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word); | |
3321 | } | |
c906108c SS |
3322 | |
3323 | /* Search upwards from currently selected frame (so that we can | |
3324 | complete on local vars. */ | |
3325 | ||
ae767bfb | 3326 | for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) |
c906108c SS |
3327 | { |
3328 | if (!BLOCK_SUPERBLOCK (b)) | |
3329 | { | |
c5aa993b | 3330 | surrounding_static_block = b; /* For elmin of dups */ |
c906108c | 3331 | } |
c5aa993b | 3332 | |
c906108c | 3333 | /* Also catch fields of types defined in this places which match our |
c5aa993b | 3334 | text string. Only complete on types visible from current context. */ |
c906108c | 3335 | |
e88c90f2 | 3336 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c906108c | 3337 | { |
c906108c SS |
3338 | COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word); |
3339 | if (SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
3340 | { | |
3341 | struct type *t = SYMBOL_TYPE (sym); | |
3342 | enum type_code c = TYPE_CODE (t); | |
3343 | ||
3344 | if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT) | |
3345 | { | |
3346 | for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++) | |
3347 | { | |
3348 | if (TYPE_FIELD_NAME (t, j)) | |
3349 | { | |
3350 | completion_list_add_name (TYPE_FIELD_NAME (t, j), | |
c5aa993b | 3351 | sym_text, sym_text_len, text, word); |
c906108c SS |
3352 | } |
3353 | } | |
3354 | } | |
3355 | } | |
3356 | } | |
3357 | } | |
3358 | ||
3359 | /* Go through the symtabs and check the externs and statics for | |
3360 | symbols which match. */ | |
3361 | ||
3362 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
3363 | { |
3364 | QUIT; | |
3365 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); | |
e88c90f2 | 3366 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c5aa993b | 3367 | { |
c5aa993b JM |
3368 | COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word); |
3369 | } | |
3370 | } | |
c906108c SS |
3371 | |
3372 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
3373 | { |
3374 | QUIT; | |
3375 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); | |
3376 | /* Don't do this block twice. */ | |
3377 | if (b == surrounding_static_block) | |
3378 | continue; | |
e88c90f2 | 3379 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c5aa993b | 3380 | { |
c5aa993b JM |
3381 | COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word); |
3382 | } | |
3383 | } | |
c906108c SS |
3384 | |
3385 | return (return_val); | |
3386 | } | |
3387 | ||
c94fdfd0 EZ |
3388 | /* Like make_symbol_completion_list, but returns a list of symbols |
3389 | defined in a source file FILE. */ | |
3390 | ||
3391 | char ** | |
3392 | make_file_symbol_completion_list (char *text, char *word, char *srcfile) | |
3393 | { | |
3394 | register struct symbol *sym; | |
3395 | register struct symtab *s; | |
3396 | register struct block *b; | |
3397 | register int i; | |
3398 | /* The symbol we are completing on. Points in same buffer as text. */ | |
3399 | char *sym_text; | |
3400 | /* Length of sym_text. */ | |
3401 | int sym_text_len; | |
3402 | ||
3403 | /* Now look for the symbol we are supposed to complete on. | |
3404 | FIXME: This should be language-specific. */ | |
3405 | { | |
3406 | char *p; | |
3407 | char quote_found; | |
3408 | char *quote_pos = NULL; | |
3409 | ||
3410 | /* First see if this is a quoted string. */ | |
3411 | quote_found = '\0'; | |
3412 | for (p = text; *p != '\0'; ++p) | |
3413 | { | |
3414 | if (quote_found != '\0') | |
3415 | { | |
3416 | if (*p == quote_found) | |
3417 | /* Found close quote. */ | |
3418 | quote_found = '\0'; | |
3419 | else if (*p == '\\' && p[1] == quote_found) | |
3420 | /* A backslash followed by the quote character | |
3421 | doesn't end the string. */ | |
3422 | ++p; | |
3423 | } | |
3424 | else if (*p == '\'' || *p == '"') | |
3425 | { | |
3426 | quote_found = *p; | |
3427 | quote_pos = p; | |
3428 | } | |
3429 | } | |
3430 | if (quote_found == '\'') | |
3431 | /* A string within single quotes can be a symbol, so complete on it. */ | |
3432 | sym_text = quote_pos + 1; | |
3433 | else if (quote_found == '"') | |
3434 | /* A double-quoted string is never a symbol, nor does it make sense | |
3435 | to complete it any other way. */ | |
3436 | { | |
3437 | return_val = (char **) xmalloc (sizeof (char *)); | |
3438 | return_val[0] = NULL; | |
3439 | return return_val; | |
3440 | } | |
3441 | else | |
3442 | { | |
3443 | /* It is not a quoted string. Break it based on the characters | |
3444 | which are in symbols. */ | |
3445 | while (p > text) | |
3446 | { | |
3447 | if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0') | |
3448 | --p; | |
3449 | else | |
3450 | break; | |
3451 | } | |
3452 | sym_text = p; | |
3453 | } | |
3454 | } | |
3455 | ||
3456 | sym_text_len = strlen (sym_text); | |
3457 | ||
3458 | return_val_size = 10; | |
3459 | return_val_index = 0; | |
3460 | return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *)); | |
3461 | return_val[0] = NULL; | |
3462 | ||
3463 | /* Find the symtab for SRCFILE (this loads it if it was not yet read | |
3464 | in). */ | |
3465 | s = lookup_symtab (srcfile); | |
3466 | if (s == NULL) | |
3467 | { | |
3468 | /* Maybe they typed the file with leading directories, while the | |
3469 | symbol tables record only its basename. */ | |
31889e00 | 3470 | const char *tail = lbasename (srcfile); |
c94fdfd0 EZ |
3471 | |
3472 | if (tail > srcfile) | |
3473 | s = lookup_symtab (tail); | |
3474 | } | |
3475 | ||
3476 | /* If we have no symtab for that file, return an empty list. */ | |
3477 | if (s == NULL) | |
3478 | return (return_val); | |
3479 | ||
3480 | /* Go through this symtab and check the externs and statics for | |
3481 | symbols which match. */ | |
3482 | ||
3483 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); | |
e88c90f2 | 3484 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c94fdfd0 | 3485 | { |
c94fdfd0 EZ |
3486 | COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word); |
3487 | } | |
3488 | ||
3489 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); | |
e88c90f2 | 3490 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c94fdfd0 | 3491 | { |
c94fdfd0 EZ |
3492 | COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word); |
3493 | } | |
3494 | ||
3495 | return (return_val); | |
3496 | } | |
3497 | ||
3498 | /* A helper function for make_source_files_completion_list. It adds | |
3499 | another file name to a list of possible completions, growing the | |
3500 | list as necessary. */ | |
3501 | ||
3502 | static void | |
3503 | add_filename_to_list (const char *fname, char *text, char *word, | |
3504 | char ***list, int *list_used, int *list_alloced) | |
3505 | { | |
3506 | char *new; | |
3507 | size_t fnlen = strlen (fname); | |
3508 | ||
3509 | if (*list_used + 1 >= *list_alloced) | |
3510 | { | |
3511 | *list_alloced *= 2; | |
3512 | *list = (char **) xrealloc ((char *) *list, | |
3513 | *list_alloced * sizeof (char *)); | |
3514 | } | |
3515 | ||
3516 | if (word == text) | |
3517 | { | |
3518 | /* Return exactly fname. */ | |
3519 | new = xmalloc (fnlen + 5); | |
3520 | strcpy (new, fname); | |
3521 | } | |
3522 | else if (word > text) | |
3523 | { | |
3524 | /* Return some portion of fname. */ | |
3525 | new = xmalloc (fnlen + 5); | |
3526 | strcpy (new, fname + (word - text)); | |
3527 | } | |
3528 | else | |
3529 | { | |
3530 | /* Return some of TEXT plus fname. */ | |
3531 | new = xmalloc (fnlen + (text - word) + 5); | |
3532 | strncpy (new, word, text - word); | |
3533 | new[text - word] = '\0'; | |
3534 | strcat (new, fname); | |
3535 | } | |
3536 | (*list)[*list_used] = new; | |
3537 | (*list)[++*list_used] = NULL; | |
3538 | } | |
3539 | ||
3540 | static int | |
3541 | not_interesting_fname (const char *fname) | |
3542 | { | |
3543 | static const char *illegal_aliens[] = { | |
3544 | "_globals_", /* inserted by coff_symtab_read */ | |
3545 | NULL | |
3546 | }; | |
3547 | int i; | |
3548 | ||
3549 | for (i = 0; illegal_aliens[i]; i++) | |
3550 | { | |
3551 | if (strcmp (fname, illegal_aliens[i]) == 0) | |
3552 | return 1; | |
3553 | } | |
3554 | return 0; | |
3555 | } | |
3556 | ||
3557 | /* Return a NULL terminated array of all source files whose names | |
3558 | begin with matching TEXT. The file names are looked up in the | |
3559 | symbol tables of this program. If the answer is no matchess, then | |
3560 | the return value is an array which contains only a NULL pointer. */ | |
3561 | ||
3562 | char ** | |
3563 | make_source_files_completion_list (char *text, char *word) | |
3564 | { | |
3565 | register struct symtab *s; | |
3566 | register struct partial_symtab *ps; | |
3567 | register struct objfile *objfile; | |
3568 | int first = 1; | |
3569 | int list_alloced = 1; | |
3570 | int list_used = 0; | |
3571 | size_t text_len = strlen (text); | |
3572 | char **list = (char **) xmalloc (list_alloced * sizeof (char *)); | |
31889e00 | 3573 | const char *base_name; |
c94fdfd0 EZ |
3574 | |
3575 | list[0] = NULL; | |
3576 | ||
3577 | if (!have_full_symbols () && !have_partial_symbols ()) | |
3578 | return list; | |
3579 | ||
3580 | ALL_SYMTABS (objfile, s) | |
3581 | { | |
3582 | if (not_interesting_fname (s->filename)) | |
3583 | continue; | |
3584 | if (!filename_seen (s->filename, 1, &first) | |
3585 | #if HAVE_DOS_BASED_FILE_SYSTEM | |
3586 | && strncasecmp (s->filename, text, text_len) == 0 | |
3587 | #else | |
3588 | && strncmp (s->filename, text, text_len) == 0 | |
3589 | #endif | |
3590 | ) | |
3591 | { | |
3592 | /* This file matches for a completion; add it to the current | |
3593 | list of matches. */ | |
3594 | add_filename_to_list (s->filename, text, word, | |
3595 | &list, &list_used, &list_alloced); | |
3596 | } | |
3597 | else | |
3598 | { | |
3599 | /* NOTE: We allow the user to type a base name when the | |
3600 | debug info records leading directories, but not the other | |
3601 | way around. This is what subroutines of breakpoint | |
3602 | command do when they parse file names. */ | |
31889e00 | 3603 | base_name = lbasename (s->filename); |
c94fdfd0 EZ |
3604 | if (base_name != s->filename |
3605 | && !filename_seen (base_name, 1, &first) | |
3606 | #if HAVE_DOS_BASED_FILE_SYSTEM | |
3607 | && strncasecmp (base_name, text, text_len) == 0 | |
3608 | #else | |
3609 | && strncmp (base_name, text, text_len) == 0 | |
3610 | #endif | |
3611 | ) | |
3612 | add_filename_to_list (base_name, text, word, | |
3613 | &list, &list_used, &list_alloced); | |
3614 | } | |
3615 | } | |
3616 | ||
3617 | ALL_PSYMTABS (objfile, ps) | |
3618 | { | |
3619 | if (not_interesting_fname (ps->filename)) | |
3620 | continue; | |
3621 | if (!ps->readin) | |
3622 | { | |
3623 | if (!filename_seen (ps->filename, 1, &first) | |
3624 | #if HAVE_DOS_BASED_FILE_SYSTEM | |
3625 | && strncasecmp (ps->filename, text, text_len) == 0 | |
3626 | #else | |
3627 | && strncmp (ps->filename, text, text_len) == 0 | |
3628 | #endif | |
3629 | ) | |
3630 | { | |
3631 | /* This file matches for a completion; add it to the | |
3632 | current list of matches. */ | |
3633 | add_filename_to_list (ps->filename, text, word, | |
3634 | &list, &list_used, &list_alloced); | |
3635 | ||
3636 | } | |
3637 | else | |
3638 | { | |
31889e00 | 3639 | base_name = lbasename (ps->filename); |
c94fdfd0 EZ |
3640 | if (base_name != ps->filename |
3641 | && !filename_seen (base_name, 1, &first) | |
3642 | #if HAVE_DOS_BASED_FILE_SYSTEM | |
3643 | && strncasecmp (base_name, text, text_len) == 0 | |
3644 | #else | |
3645 | && strncmp (base_name, text, text_len) == 0 | |
3646 | #endif | |
3647 | ) | |
3648 | add_filename_to_list (base_name, text, word, | |
3649 | &list, &list_used, &list_alloced); | |
3650 | } | |
3651 | } | |
3652 | } | |
3653 | ||
3654 | return list; | |
3655 | } | |
3656 | ||
c906108c SS |
3657 | /* Determine if PC is in the prologue of a function. The prologue is the area |
3658 | between the first instruction of a function, and the first executable line. | |
3659 | Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue. | |
3660 | ||
3661 | If non-zero, func_start is where we think the prologue starts, possibly | |
3662 | by previous examination of symbol table information. | |
3663 | */ | |
3664 | ||
3665 | int | |
fba45db2 | 3666 | in_prologue (CORE_ADDR pc, CORE_ADDR func_start) |
c906108c SS |
3667 | { |
3668 | struct symtab_and_line sal; | |
3669 | CORE_ADDR func_addr, func_end; | |
3670 | ||
54cf9c03 EZ |
3671 | /* We have several sources of information we can consult to figure |
3672 | this out. | |
3673 | - Compilers usually emit line number info that marks the prologue | |
3674 | as its own "source line". So the ending address of that "line" | |
3675 | is the end of the prologue. If available, this is the most | |
3676 | reliable method. | |
3677 | - The minimal symbols and partial symbols, which can usually tell | |
3678 | us the starting and ending addresses of a function. | |
3679 | - If we know the function's start address, we can call the | |
3680 | architecture-defined SKIP_PROLOGUE function to analyze the | |
3681 | instruction stream and guess where the prologue ends. | |
3682 | - Our `func_start' argument; if non-zero, this is the caller's | |
3683 | best guess as to the function's entry point. At the time of | |
3684 | this writing, handle_inferior_event doesn't get this right, so | |
3685 | it should be our last resort. */ | |
3686 | ||
3687 | /* Consult the partial symbol table, to find which function | |
3688 | the PC is in. */ | |
3689 | if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end)) | |
3690 | { | |
3691 | CORE_ADDR prologue_end; | |
c906108c | 3692 | |
54cf9c03 EZ |
3693 | /* We don't even have minsym information, so fall back to using |
3694 | func_start, if given. */ | |
3695 | if (! func_start) | |
3696 | return 1; /* We *might* be in a prologue. */ | |
c906108c | 3697 | |
54cf9c03 | 3698 | prologue_end = SKIP_PROLOGUE (func_start); |
c906108c | 3699 | |
54cf9c03 EZ |
3700 | return func_start <= pc && pc < prologue_end; |
3701 | } | |
c906108c | 3702 | |
54cf9c03 EZ |
3703 | /* If we have line number information for the function, that's |
3704 | usually pretty reliable. */ | |
3705 | sal = find_pc_line (func_addr, 0); | |
c906108c | 3706 | |
54cf9c03 EZ |
3707 | /* Now sal describes the source line at the function's entry point, |
3708 | which (by convention) is the prologue. The end of that "line", | |
3709 | sal.end, is the end of the prologue. | |
3710 | ||
3711 | Note that, for functions whose source code is all on a single | |
3712 | line, the line number information doesn't always end up this way. | |
3713 | So we must verify that our purported end-of-prologue address is | |
3714 | *within* the function, not at its start or end. */ | |
3715 | if (sal.line == 0 | |
3716 | || sal.end <= func_addr | |
3717 | || func_end <= sal.end) | |
3718 | { | |
3719 | /* We don't have any good line number info, so use the minsym | |
3720 | information, together with the architecture-specific prologue | |
3721 | scanning code. */ | |
3722 | CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr); | |
c906108c | 3723 | |
54cf9c03 EZ |
3724 | return func_addr <= pc && pc < prologue_end; |
3725 | } | |
c906108c | 3726 | |
54cf9c03 EZ |
3727 | /* We have line number info, and it looks good. */ |
3728 | return func_addr <= pc && pc < sal.end; | |
c906108c SS |
3729 | } |
3730 | ||
3731 | ||
3732 | /* Begin overload resolution functions */ | |
228c6d41 DJ |
3733 | |
3734 | static char * | |
3735 | remove_params (const char *demangled_name) | |
3736 | { | |
3737 | const char *argp; | |
3738 | char *new_name; | |
3739 | int depth; | |
3740 | ||
3741 | if (demangled_name == NULL) | |
3742 | return NULL; | |
3743 | ||
3744 | /* First find the end of the arg list. */ | |
3745 | argp = strrchr (demangled_name, ')'); | |
3746 | if (argp == NULL) | |
3747 | return NULL; | |
3748 | ||
3749 | /* Back up to the beginning. */ | |
3750 | depth = 1; | |
3751 | ||
3752 | while (argp-- > demangled_name) | |
3753 | { | |
3754 | if (*argp == ')') | |
3755 | depth ++; | |
3756 | else if (*argp == '(') | |
3757 | { | |
3758 | depth --; | |
3759 | ||
3760 | if (depth == 0) | |
3761 | break; | |
3762 | } | |
3763 | } | |
3764 | if (depth != 0) | |
3765 | internal_error (__FILE__, __LINE__, | |
3766 | "bad demangled name %s\n", demangled_name); | |
3767 | while (argp[-1] == ' ' && argp > demangled_name) | |
3768 | argp --; | |
3769 | ||
3770 | new_name = xmalloc (argp - demangled_name + 1); | |
3771 | memcpy (new_name, demangled_name, argp - demangled_name); | |
3772 | new_name[argp - demangled_name] = '\0'; | |
3773 | return new_name; | |
3774 | } | |
3775 | ||
c906108c SS |
3776 | /* Helper routine for make_symbol_completion_list. */ |
3777 | ||
3778 | static int sym_return_val_size; | |
3779 | static int sym_return_val_index; | |
3780 | static struct symbol **sym_return_val; | |
3781 | ||
3782 | /* Test to see if the symbol specified by SYMNAME (which is already | |
c5aa993b JM |
3783 | demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN |
3784 | characters. If so, add it to the current completion list. */ | |
c906108c SS |
3785 | |
3786 | static void | |
fba45db2 | 3787 | overload_list_add_symbol (struct symbol *sym, char *oload_name) |
c906108c SS |
3788 | { |
3789 | int newsize; | |
3790 | int i; | |
228c6d41 DJ |
3791 | char *sym_name; |
3792 | ||
3793 | /* If there is no type information, we can't do anything, so skip */ | |
3794 | if (SYMBOL_TYPE (sym) == NULL) | |
3795 | return; | |
3796 | ||
3797 | /* skip any symbols that we've already considered. */ | |
3798 | for (i = 0; i < sym_return_val_index; ++i) | |
3799 | if (!strcmp (SYMBOL_NAME (sym), SYMBOL_NAME (sym_return_val[i]))) | |
3800 | return; | |
c906108c SS |
3801 | |
3802 | /* Get the demangled name without parameters */ | |
228c6d41 | 3803 | sym_name = remove_params (SYMBOL_DEMANGLED_NAME (sym)); |
c906108c | 3804 | if (!sym_name) |
228c6d41 | 3805 | return; |
c906108c SS |
3806 | |
3807 | /* skip symbols that cannot match */ | |
3808 | if (strcmp (sym_name, oload_name) != 0) | |
917317f4 | 3809 | { |
b8c9b27d | 3810 | xfree (sym_name); |
917317f4 JM |
3811 | return; |
3812 | } | |
c906108c | 3813 | |
228c6d41 | 3814 | xfree (sym_name); |
c906108c SS |
3815 | |
3816 | /* We have a match for an overload instance, so add SYM to the current list | |
3817 | * of overload instances */ | |
3818 | if (sym_return_val_index + 3 > sym_return_val_size) | |
3819 | { | |
3820 | newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); | |
3821 | sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize); | |
3822 | } | |
3823 | sym_return_val[sym_return_val_index++] = sym; | |
3824 | sym_return_val[sym_return_val_index] = NULL; | |
c906108c SS |
3825 | } |
3826 | ||
3827 | /* Return a null-terminated list of pointers to function symbols that | |
3828 | * match name of the supplied symbol FSYM. | |
3829 | * This is used in finding all overloaded instances of a function name. | |
3830 | * This has been modified from make_symbol_completion_list. */ | |
3831 | ||
3832 | ||
3833 | struct symbol ** | |
fba45db2 | 3834 | make_symbol_overload_list (struct symbol *fsym) |
c906108c SS |
3835 | { |
3836 | register struct symbol *sym; | |
3837 | register struct symtab *s; | |
3838 | register struct partial_symtab *ps; | |
c906108c SS |
3839 | register struct objfile *objfile; |
3840 | register struct block *b, *surrounding_static_block = 0; | |
d4f3574e | 3841 | register int i; |
c906108c SS |
3842 | /* The name we are completing on. */ |
3843 | char *oload_name = NULL; | |
3844 | /* Length of name. */ | |
3845 | int oload_name_len = 0; | |
3846 | ||
228c6d41 | 3847 | /* Look for the symbol we are supposed to complete on. */ |
c906108c | 3848 | |
228c6d41 | 3849 | oload_name = remove_params (SYMBOL_DEMANGLED_NAME (fsym)); |
c906108c SS |
3850 | if (!oload_name) |
3851 | { | |
228c6d41 DJ |
3852 | sym_return_val_size = 1; |
3853 | sym_return_val = (struct symbol **) xmalloc (2 * sizeof (struct symbol *)); | |
3854 | sym_return_val[0] = fsym; | |
3855 | sym_return_val[1] = NULL; | |
3856 | ||
3857 | return sym_return_val; | |
c906108c SS |
3858 | } |
3859 | oload_name_len = strlen (oload_name); | |
3860 | ||
3861 | sym_return_val_size = 100; | |
3862 | sym_return_val_index = 0; | |
3863 | sym_return_val = (struct symbol **) xmalloc ((sym_return_val_size + 1) * sizeof (struct symbol *)); | |
3864 | sym_return_val[0] = NULL; | |
3865 | ||
3866 | /* Look through the partial symtabs for all symbols which begin | |
917317f4 | 3867 | by matching OLOAD_NAME. Make sure we read that symbol table in. */ |
c906108c SS |
3868 | |
3869 | ALL_PSYMTABS (objfile, ps) | |
c5aa993b | 3870 | { |
d4f3574e SS |
3871 | struct partial_symbol **psym; |
3872 | ||
c5aa993b JM |
3873 | /* If the psymtab's been read in we'll get it when we search |
3874 | through the blockvector. */ | |
3875 | if (ps->readin) | |
3876 | continue; | |
3877 | ||
3878 | for (psym = objfile->global_psymbols.list + ps->globals_offset; | |
3879 | psym < (objfile->global_psymbols.list + ps->globals_offset | |
3880 | + ps->n_global_syms); | |
3881 | psym++) | |
3882 | { | |
3883 | /* If interrupted, then quit. */ | |
3884 | QUIT; | |
917317f4 JM |
3885 | /* This will cause the symbol table to be read if it has not yet been */ |
3886 | s = PSYMTAB_TO_SYMTAB (ps); | |
c5aa993b JM |
3887 | } |
3888 | ||
3889 | for (psym = objfile->static_psymbols.list + ps->statics_offset; | |
3890 | psym < (objfile->static_psymbols.list + ps->statics_offset | |
3891 | + ps->n_static_syms); | |
3892 | psym++) | |
3893 | { | |
3894 | QUIT; | |
917317f4 JM |
3895 | /* This will cause the symbol table to be read if it has not yet been */ |
3896 | s = PSYMTAB_TO_SYMTAB (ps); | |
c5aa993b JM |
3897 | } |
3898 | } | |
c906108c | 3899 | |
c906108c SS |
3900 | /* Search upwards from currently selected frame (so that we can |
3901 | complete on local vars. */ | |
3902 | ||
ae767bfb | 3903 | for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) |
c906108c SS |
3904 | { |
3905 | if (!BLOCK_SUPERBLOCK (b)) | |
3906 | { | |
c5aa993b | 3907 | surrounding_static_block = b; /* For elimination of dups */ |
c906108c | 3908 | } |
c5aa993b | 3909 | |
c906108c | 3910 | /* Also catch fields of types defined in this places which match our |
c5aa993b | 3911 | text string. Only complete on types visible from current context. */ |
c906108c | 3912 | |
e88c90f2 | 3913 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c906108c | 3914 | { |
c906108c SS |
3915 | overload_list_add_symbol (sym, oload_name); |
3916 | } | |
3917 | } | |
3918 | ||
3919 | /* Go through the symtabs and check the externs and statics for | |
3920 | symbols which match. */ | |
3921 | ||
3922 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
3923 | { |
3924 | QUIT; | |
3925 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); | |
e88c90f2 | 3926 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c5aa993b | 3927 | { |
c5aa993b JM |
3928 | overload_list_add_symbol (sym, oload_name); |
3929 | } | |
3930 | } | |
c906108c SS |
3931 | |
3932 | ALL_SYMTABS (objfile, s) | |
c5aa993b JM |
3933 | { |
3934 | QUIT; | |
3935 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); | |
3936 | /* Don't do this block twice. */ | |
3937 | if (b == surrounding_static_block) | |
3938 | continue; | |
e88c90f2 | 3939 | ALL_BLOCK_SYMBOLS (b, i, sym) |
c5aa993b | 3940 | { |
c5aa993b JM |
3941 | overload_list_add_symbol (sym, oload_name); |
3942 | } | |
3943 | } | |
c906108c | 3944 | |
b8c9b27d | 3945 | xfree (oload_name); |
c906108c SS |
3946 | |
3947 | return (sym_return_val); | |
3948 | } | |
3949 | ||
3950 | /* End of overload resolution functions */ | |
c906108c | 3951 | \f |
50641945 FN |
3952 | struct symtabs_and_lines |
3953 | decode_line_spec (char *string, int funfirstline) | |
3954 | { | |
3955 | struct symtabs_and_lines sals; | |
0378c332 FN |
3956 | struct symtab_and_line cursal; |
3957 | ||
50641945 FN |
3958 | if (string == 0) |
3959 | error ("Empty line specification."); | |
0378c332 FN |
3960 | |
3961 | /* We use whatever is set as the current source line. We do not try | |
3962 | and get a default or it will recursively call us! */ | |
3963 | cursal = get_current_source_symtab_and_line (); | |
3964 | ||
50641945 | 3965 | sals = decode_line_1 (&string, funfirstline, |
0378c332 | 3966 | cursal.symtab, cursal.line, |
50641945 | 3967 | (char ***) NULL); |
0378c332 | 3968 | |
50641945 FN |
3969 | if (*string) |
3970 | error ("Junk at end of line specification: %s", string); | |
3971 | return sals; | |
3972 | } | |
c5aa993b | 3973 | |
51cc5b07 AC |
3974 | /* Track MAIN */ |
3975 | static char *name_of_main; | |
3976 | ||
3977 | void | |
3978 | set_main_name (const char *name) | |
3979 | { | |
3980 | if (name_of_main != NULL) | |
3981 | { | |
3982 | xfree (name_of_main); | |
3983 | name_of_main = NULL; | |
3984 | } | |
3985 | if (name != NULL) | |
3986 | { | |
3987 | name_of_main = xstrdup (name); | |
3988 | } | |
3989 | } | |
3990 | ||
3991 | char * | |
3992 | main_name (void) | |
3993 | { | |
3994 | if (name_of_main != NULL) | |
3995 | return name_of_main; | |
3996 | else | |
3997 | return "main"; | |
3998 | } | |
3999 | ||
4000 | ||
c906108c | 4001 | void |
fba45db2 | 4002 | _initialize_symtab (void) |
c906108c SS |
4003 | { |
4004 | add_info ("variables", variables_info, | |
c5aa993b | 4005 | "All global and static variable names, or those matching REGEXP."); |
c906108c | 4006 | if (dbx_commands) |
c5aa993b JM |
4007 | add_com ("whereis", class_info, variables_info, |
4008 | "All global and static variable names, or those matching REGEXP."); | |
c906108c SS |
4009 | |
4010 | add_info ("functions", functions_info, | |
4011 | "All function names, or those matching REGEXP."); | |
4012 | ||
357e46e7 | 4013 | |
c906108c SS |
4014 | /* FIXME: This command has at least the following problems: |
4015 | 1. It prints builtin types (in a very strange and confusing fashion). | |
4016 | 2. It doesn't print right, e.g. with | |
c5aa993b JM |
4017 | typedef struct foo *FOO |
4018 | type_print prints "FOO" when we want to make it (in this situation) | |
4019 | print "struct foo *". | |
c906108c SS |
4020 | I also think "ptype" or "whatis" is more likely to be useful (but if |
4021 | there is much disagreement "info types" can be fixed). */ | |
4022 | add_info ("types", types_info, | |
4023 | "All type names, or those matching REGEXP."); | |
4024 | ||
c906108c SS |
4025 | add_info ("sources", sources_info, |
4026 | "Source files in the program."); | |
4027 | ||
4028 | add_com ("rbreak", class_breakpoint, rbreak_command, | |
c5aa993b | 4029 | "Set a breakpoint for all functions matching REGEXP."); |
c906108c SS |
4030 | |
4031 | if (xdb_commands) | |
4032 | { | |
4033 | add_com ("lf", class_info, sources_info, "Source files in the program"); | |
4034 | add_com ("lg", class_info, variables_info, | |
c5aa993b | 4035 | "All global and static variable names, or those matching REGEXP."); |
c906108c SS |
4036 | } |
4037 | ||
4038 | /* Initialize the one built-in type that isn't language dependent... */ | |
4039 | builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0, | |
4040 | "<unknown type>", (struct objfile *) NULL); | |
4041 | } |