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