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