Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Symbol table lookup for the GNU debugger, GDB. |
cba0d141 | 2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
997a978c | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
997a978c JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
997a978c | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
997a978c JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 19 | |
bd5635a1 RP |
20 | #include "defs.h" |
21 | #include "symtab.h" | |
cba0d141 | 22 | #include "gdbtypes.h" |
bd5635a1 RP |
23 | #include "gdbcore.h" |
24 | #include "frame.h" | |
25 | #include "target.h" | |
26 | #include "value.h" | |
27 | #include "symfile.h" | |
28 | #include "gdbcmd.h" | |
5594d534 | 29 | #include "regex.h" |
cba0d141 | 30 | #include "expression.h" |
997a978c | 31 | #include "language.h" |
bd5635a1 RP |
32 | |
33 | #include <obstack.h> | |
34 | #include <assert.h> | |
35 | ||
36 | #include <sys/types.h> | |
37 | #include <fcntl.h> | |
38 | #include <string.h> | |
39 | #include <sys/stat.h> | |
2cd99985 | 40 | #include <ctype.h> |
bd5635a1 | 41 | |
cba0d141 | 42 | /* Prototypes for local functions */ |
bd5635a1 | 43 | |
2cd99985 | 44 | extern int |
cba0d141 JG |
45 | find_methods PARAMS ((struct type *, char *, char **, struct symbol **)); |
46 | ||
47 | static void | |
48 | completion_list_add_symbol PARAMS ((char *)); | |
49 | ||
50 | static struct symtabs_and_lines | |
51 | decode_line_2 PARAMS ((struct symbol *[], int, int)); | |
52 | ||
53 | static void | |
54 | rbreak_command PARAMS ((char *)); | |
55 | ||
56 | static void | |
57 | types_info PARAMS ((char *)); | |
58 | ||
59 | static void | |
60 | functions_info PARAMS ((char *)); | |
61 | ||
62 | static void | |
63 | variables_info PARAMS ((char *)); | |
64 | ||
65 | static void | |
66 | list_symbols PARAMS ((char *, int, int)); | |
67 | ||
68 | static void | |
69 | sources_info PARAMS ((void)); | |
70 | ||
71 | static void | |
72 | output_source_filename PARAMS ((char *, int *)); | |
73 | ||
74 | static char * | |
75 | operator_chars PARAMS ((char *, char **)); | |
76 | ||
77 | static int | |
78 | find_line_common PARAMS ((struct linetable *, int, int *)); | |
79 | ||
80 | static struct partial_symbol * | |
81 | lookup_partial_symbol PARAMS ((struct partial_symtab *, const char *, | |
82 | int, enum namespace)); | |
83 | ||
84 | static struct partial_symbol * | |
85 | lookup_demangled_partial_symbol PARAMS ((const struct partial_symtab *, | |
86 | const char *)); | |
87 | ||
88 | static struct symbol * | |
89 | lookup_demangled_block_symbol PARAMS ((const struct block *, const char *)); | |
bd5635a1 | 90 | |
cba0d141 JG |
91 | static struct symtab * |
92 | lookup_symtab_1 PARAMS ((char *)); | |
93 | ||
94 | /* */ | |
bd5635a1 | 95 | |
997a978c | 96 | /* The single non-language-specific builtin type */ |
bd5635a1 RP |
97 | struct type *builtin_type_error; |
98 | ||
99 | /* Block in which the most recently searched-for symbol was found. | |
100 | Might be better to make this a parameter to lookup_symbol and | |
101 | value_of_this. */ | |
cba0d141 JG |
102 | |
103 | const struct block *block_found; | |
bd5635a1 RP |
104 | |
105 | char no_symtab_msg[] = "No symbol table is loaded. Use the \"file\" command."; | |
106 | ||
107 | /* Check for a symtab of a specific name; first in symtabs, then in | |
108 | psymtabs. *If* there is no '/' in the name, a match after a '/' | |
109 | in the symtab filename will also work. */ | |
110 | ||
111 | static struct symtab * | |
112 | lookup_symtab_1 (name) | |
113 | char *name; | |
114 | { | |
115 | register struct symtab *s; | |
116 | register struct partial_symtab *ps; | |
117 | register char *slash = strchr (name, '/'); | |
118 | register int len = strlen (name); | |
cba0d141 | 119 | register struct objfile *objfile; |
bd5635a1 | 120 | |
bd5635a1 | 121 | |
cba0d141 JG |
122 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
123 | { | |
124 | for (s = objfile -> symtabs; s != NULL; s = s -> next) | |
125 | { | |
126 | if (strcmp (name, s->filename) == 0) | |
127 | { | |
128 | return (s); | |
129 | } | |
130 | } | |
131 | } | |
132 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) | |
133 | { | |
134 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps->next) | |
135 | { | |
136 | if (strcmp (name, ps -> filename) == 0) | |
137 | { | |
138 | if (ps -> readin) | |
139 | { | |
140 | error ("Internal: readin pst for `%s' found when no symtab found.", name); | |
141 | } | |
142 | return (PSYMTAB_TO_SYMTAB (ps)); | |
143 | } | |
144 | } | |
145 | } | |
bd5635a1 RP |
146 | if (!slash) |
147 | { | |
cba0d141 | 148 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 149 | { |
cba0d141 JG |
150 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
151 | { | |
152 | int l = strlen (s->filename); | |
153 | ||
154 | if (s->filename[l - len -1] == '/' | |
155 | && (strcmp (s->filename + l - len, name) == 0)) | |
156 | { | |
157 | return (s); | |
158 | } | |
159 | } | |
bd5635a1 RP |
160 | } |
161 | ||
cba0d141 | 162 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 163 | { |
cba0d141 | 164 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps -> next) |
bd5635a1 | 165 | { |
cba0d141 JG |
166 | int l = strlen (ps -> filename); |
167 | ||
168 | if (ps -> filename[l - len - 1] == '/' | |
169 | && (strcmp (ps->filename + l - len, name) == 0)) | |
170 | { | |
171 | if (ps -> readin) | |
172 | { | |
173 | error ("Internal: readin pst for `%s' found when no symtab found.", name); | |
174 | } | |
175 | return (PSYMTAB_TO_SYMTAB (ps)); | |
176 | } | |
bd5635a1 RP |
177 | } |
178 | } | |
179 | } | |
cba0d141 | 180 | return (NULL); |
bd5635a1 RP |
181 | } |
182 | ||
183 | /* Lookup the symbol table of a source file named NAME. Try a couple | |
184 | of variations if the first lookup doesn't work. */ | |
185 | ||
186 | struct symtab * | |
187 | lookup_symtab (name) | |
188 | char *name; | |
189 | { | |
190 | register struct symtab *s; | |
191 | register char *copy; | |
192 | ||
193 | s = lookup_symtab_1 (name); | |
194 | if (s) return s; | |
195 | ||
196 | /* If name not found as specified, see if adding ".c" helps. */ | |
197 | ||
198 | copy = (char *) alloca (strlen (name) + 3); | |
199 | strcpy (copy, name); | |
200 | strcat (copy, ".c"); | |
201 | s = lookup_symtab_1 (copy); | |
202 | if (s) return s; | |
203 | ||
204 | /* We didn't find anything; die. */ | |
205 | return 0; | |
206 | } | |
207 | ||
208 | /* Lookup the partial symbol table of a source file named NAME. This | |
209 | only returns true on an exact match (ie. this semantics are | |
210 | different from lookup_symtab. */ | |
211 | ||
212 | struct partial_symtab * | |
213 | lookup_partial_symtab (name) | |
214 | char *name; | |
215 | { | |
cba0d141 JG |
216 | register struct partial_symtab *pst; |
217 | register struct objfile *objfile; | |
bd5635a1 | 218 | |
cba0d141 | 219 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 220 | { |
cba0d141 | 221 | for (pst = objfile -> psymtabs; pst != NULL; pst = pst -> next) |
bd5635a1 | 222 | { |
cba0d141 JG |
223 | if (strcmp (name, pst -> filename) == 0) |
224 | { | |
225 | return (pst); | |
226 | } | |
bd5635a1 | 227 | } |
cba0d141 JG |
228 | } |
229 | return (NULL); | |
bd5635a1 | 230 | } |
cba0d141 | 231 | \f |
bd5635a1 RP |
232 | /* Demangle a GDB method stub type. */ |
233 | char * | |
bcccec8c | 234 | gdb_mangle_name (type, i, j) |
bd5635a1 | 235 | struct type *type; |
bcccec8c | 236 | int i, j; |
bd5635a1 | 237 | { |
bcccec8c PB |
238 | int mangled_name_len; |
239 | char *mangled_name; | |
240 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); | |
241 | struct fn_field *method = &f[j]; | |
242 | char *field_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
7c4f3f4a | 243 | int is_constructor = strcmp(field_name, TYPE_NAME (type)) == 0; |
bd5635a1 | 244 | |
bcccec8c | 245 | /* Need a new type prefix. */ |
bcccec8c PB |
246 | char *const_prefix = method->is_const ? "C" : ""; |
247 | char *volatile_prefix = method->is_volatile ? "V" : ""; | |
248 | char *newname = type_name_no_tag (type); | |
249 | char buf[20]; | |
250 | int len = strlen (newname); | |
251 | ||
252 | sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len); | |
7c4f3f4a | 253 | mangled_name_len = ((is_constructor ? 0 : strlen (field_name)) |
bcccec8c PB |
254 | + strlen (buf) + len |
255 | + strlen (TYPE_FN_FIELD_PHYSNAME (f, j)) | |
256 | + 1); | |
257 | ||
7d9884b9 JG |
258 | /* Only needed for GNU-mangled names. ANSI-mangled names |
259 | work with the normal mechanisms. */ | |
bcccec8c PB |
260 | if (OPNAME_PREFIX_P (field_name)) |
261 | { | |
7d9884b9 | 262 | char *opname = cplus_mangle_opname (field_name + 3, 0); |
bcccec8c PB |
263 | if (opname == NULL) |
264 | error ("No mangling for \"%s\"", field_name); | |
265 | mangled_name_len += strlen (opname); | |
266 | mangled_name = (char *)xmalloc (mangled_name_len); | |
267 | ||
268 | strncpy (mangled_name, field_name, 3); | |
269 | mangled_name[3] = '\0'; | |
270 | strcat (mangled_name, opname); | |
271 | } | |
272 | else | |
bd5635a1 | 273 | { |
bcccec8c | 274 | mangled_name = (char *)xmalloc (mangled_name_len); |
7c4f3f4a MT |
275 | if (is_constructor) |
276 | mangled_name[0] = '\0'; | |
277 | else | |
278 | strcpy (mangled_name, field_name); | |
bd5635a1 | 279 | } |
bcccec8c PB |
280 | strcat (mangled_name, buf); |
281 | strcat (mangled_name, newname); | |
282 | strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j)); | |
283 | ||
284 | return mangled_name; | |
bd5635a1 RP |
285 | } |
286 | ||
cba0d141 JG |
287 | \f |
288 | /* Find which partial symtab on contains PC. Return 0 if none. */ | |
f1d77e90 | 289 | |
cba0d141 JG |
290 | struct partial_symtab * |
291 | find_pc_psymtab (pc) | |
292 | register CORE_ADDR pc; | |
d96b54ea | 293 | { |
cba0d141 JG |
294 | register struct partial_symtab *pst; |
295 | register struct objfile *objfile; | |
d96b54ea | 296 | |
cba0d141 | 297 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 298 | { |
cba0d141 | 299 | for (pst = objfile -> psymtabs; pst != NULL; pst = pst -> next) |
bd5635a1 | 300 | { |
cba0d141 | 301 | if (pc >= pst -> textlow && pc < pst -> texthigh) |
bd5635a1 | 302 | { |
cba0d141 | 303 | return (pst); |
bd5635a1 | 304 | } |
f1d77e90 | 305 | } |
bd5635a1 | 306 | } |
cba0d141 | 307 | return (NULL); |
bd5635a1 RP |
308 | } |
309 | ||
310 | /* Find which partial symbol within a psymtab contains PC. Return 0 | |
311 | if none. Check all psymtabs if PSYMTAB is 0. */ | |
312 | struct partial_symbol * | |
313 | find_pc_psymbol (psymtab, pc) | |
314 | struct partial_symtab *psymtab; | |
315 | CORE_ADDR pc; | |
316 | { | |
317 | struct partial_symbol *best, *p; | |
318 | CORE_ADDR best_pc; | |
319 | ||
320 | if (!psymtab) | |
321 | psymtab = find_pc_psymtab (pc); | |
322 | if (!psymtab) | |
323 | return 0; | |
324 | ||
325 | best_pc = psymtab->textlow - 1; | |
326 | ||
cba0d141 JG |
327 | for (p = psymtab->objfile->static_psymbols.list + psymtab->statics_offset; |
328 | (p - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset) | |
bd5635a1 RP |
329 | < psymtab->n_static_syms); |
330 | p++) | |
331 | if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE | |
332 | && SYMBOL_CLASS (p) == LOC_BLOCK | |
333 | && pc >= SYMBOL_VALUE_ADDRESS (p) | |
334 | && SYMBOL_VALUE_ADDRESS (p) > best_pc) | |
335 | { | |
336 | best_pc = SYMBOL_VALUE_ADDRESS (p); | |
337 | best = p; | |
338 | } | |
339 | if (best_pc == psymtab->textlow - 1) | |
340 | return 0; | |
341 | return best; | |
342 | } | |
343 | ||
344 | \f | |
318bf84f FF |
345 | /* Function called via iterate_over_msymbols() that tests a minimal symbol |
346 | to see if the minimal symbol's name is a C++ mangled name that matches | |
347 | a user visible name. The user visible name (pname) is passed as arg1 | |
348 | and the number of leading characters that must match in both the mangled | |
349 | name and the user name (matchcount) is passed as arg2. Returns a pointer | |
350 | to the minimal symbol if it matches, NULL otherwise. */ | |
351 | ||
352 | static PTR | |
353 | cplus_mangled_symbol (objfile, msymbol, arg1, arg2, arg3) | |
354 | struct objfile *objfile; | |
355 | struct minimal_symbol *msymbol; | |
356 | PTR arg1; | |
357 | PTR arg2; | |
358 | PTR arg3; | |
359 | { | |
360 | char *pname = (char *) arg1; | |
361 | int matchcount = (int) arg2; | |
362 | char *demangled; | |
363 | struct minimal_symbol *foundit = NULL; | |
364 | ||
365 | if (strncmp (msymbol -> name, pname, matchcount) == 0) | |
366 | { | |
367 | if ((demangled = cplus_demangle (msymbol -> name, -1)) != NULL) | |
368 | { | |
369 | if (strcmp (demangled, pname) == 0) | |
370 | { | |
371 | foundit = msymbol; | |
372 | } | |
373 | free (demangled); | |
374 | } | |
375 | } | |
376 | return ((PTR) foundit); | |
377 | } | |
378 | ||
bd5635a1 RP |
379 | /* Find the definition for a specified symbol name NAME |
380 | in namespace NAMESPACE, visible from lexical block BLOCK. | |
381 | Returns the struct symbol pointer, or zero if no symbol is found. | |
382 | If SYMTAB is non-NULL, store the symbol table in which the | |
383 | symbol was found there, or NULL if not found. | |
384 | C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if | |
385 | NAME is a field of the current implied argument `this'. If so set | |
386 | *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero. | |
387 | BLOCK_FOUND is set to the block in which NAME is found (in the case of | |
388 | a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */ | |
389 | ||
390 | struct symbol * | |
391 | lookup_symbol (name, block, namespace, is_a_field_of_this, symtab) | |
cba0d141 JG |
392 | const char *name; |
393 | register const struct block *block; | |
394 | const enum namespace namespace; | |
bd5635a1 RP |
395 | int *is_a_field_of_this; |
396 | struct symtab **symtab; | |
397 | { | |
398 | register struct symbol *sym; | |
399 | register struct symtab *s; | |
400 | register struct partial_symtab *ps; | |
401 | struct blockvector *bv; | |
cba0d141 JG |
402 | register struct objfile *objfile; |
403 | register struct block *b; | |
404 | register int found; | |
405 | register struct minimal_symbol *msymbol; | |
bd5635a1 RP |
406 | |
407 | /* Search specified block and its superiors. */ | |
408 | ||
409 | while (block != 0) | |
410 | { | |
411 | sym = lookup_block_symbol (block, name, namespace); | |
412 | if (sym) | |
413 | { | |
414 | block_found = block; | |
415 | if (symtab != NULL) | |
416 | { | |
417 | /* Search the list of symtabs for one which contains the | |
418 | address of the start of this block. */ | |
cba0d141 JG |
419 | for (found = 0, objfile = object_files; |
420 | !found && objfile != NULL; | |
421 | objfile = objfile -> next) | |
bd5635a1 | 422 | { |
cba0d141 JG |
423 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
424 | { | |
425 | bv = BLOCKVECTOR (s); | |
426 | b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
427 | if (BLOCK_START (b) <= BLOCK_START (block) | |
428 | && BLOCK_END (b) > BLOCK_START (block)) | |
429 | { | |
430 | found++; | |
431 | break; | |
432 | } | |
433 | } | |
bd5635a1 RP |
434 | } |
435 | *symtab = s; | |
436 | } | |
437 | ||
cba0d141 | 438 | return (sym); |
bd5635a1 RP |
439 | } |
440 | block = BLOCK_SUPERBLOCK (block); | |
441 | } | |
442 | ||
b039ac3a JK |
443 | /* But that doesn't do any demangling for the STATIC_BLOCK. |
444 | I'm not sure whether demangling is needed in the case of | |
445 | nested function in inner blocks; if so this needs to be changed. | |
446 | ||
447 | Don't need to mess with the psymtabs; if we have a block, | |
448 | that file is read in. If we don't, then we deal later with | |
449 | all the psymtab stuff that needs checking. */ | |
450 | if (namespace == VAR_NAMESPACE && block != NULL) | |
451 | { | |
452 | struct block *b; | |
453 | /* Find the right symtab. */ | |
cba0d141 | 454 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
b039ac3a | 455 | { |
cba0d141 | 456 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
b039ac3a | 457 | { |
cba0d141 JG |
458 | bv = BLOCKVECTOR (s); |
459 | b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
460 | if (BLOCK_START (b) <= BLOCK_START (block) | |
461 | && BLOCK_END (b) > BLOCK_START (block)) | |
b039ac3a | 462 | { |
cba0d141 JG |
463 | sym = lookup_demangled_block_symbol (b, name); |
464 | if (sym) | |
465 | { | |
466 | block_found = b; | |
467 | if (symtab != NULL) | |
468 | *symtab = s; | |
469 | return sym; | |
470 | } | |
b039ac3a JK |
471 | } |
472 | } | |
473 | } | |
474 | } | |
475 | ||
476 | ||
bd5635a1 RP |
477 | /* C++: If requested to do so by the caller, |
478 | check to see if NAME is a field of `this'. */ | |
479 | if (is_a_field_of_this) | |
480 | { | |
481 | struct value *v = value_of_this (0); | |
482 | ||
483 | *is_a_field_of_this = 0; | |
484 | if (v && check_field (v, name)) | |
485 | { | |
486 | *is_a_field_of_this = 1; | |
487 | if (symtab != NULL) | |
488 | *symtab = NULL; | |
489 | return 0; | |
490 | } | |
491 | } | |
492 | ||
493 | /* Now search all global blocks. Do the symtab's first, then | |
494 | check the psymtab's */ | |
cba0d141 JG |
495 | |
496 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) | |
bd5635a1 | 497 | { |
cba0d141 | 498 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
bd5635a1 | 499 | { |
cba0d141 JG |
500 | bv = BLOCKVECTOR (s); |
501 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
502 | sym = lookup_block_symbol (block, name, namespace); | |
503 | if (sym) | |
504 | { | |
505 | block_found = block; | |
506 | if (symtab != NULL) | |
507 | *symtab = s; | |
508 | return sym; | |
509 | } | |
bd5635a1 RP |
510 | } |
511 | } | |
512 | ||
513 | /* Check for the possibility of the symbol being a global function | |
cba0d141 | 514 | that is stored in one of the minimal symbol tables. Eventually, all |
bd5635a1 RP |
515 | global symbols might be resolved in this way. */ |
516 | ||
517 | if (namespace == VAR_NAMESPACE) | |
518 | { | |
cba0d141 | 519 | msymbol = lookup_minimal_symbol (name, (struct objfile *) NULL); |
cba0d141 | 520 | if (msymbol == NULL) |
bd5635a1 | 521 | { |
318bf84f FF |
522 | /* Look for a mangled C++ name for NAME. */ |
523 | msymbol = (struct minimal_symbol *) | |
524 | iterate_over_msymbols (cplus_mangled_symbol, (PTR) name, | |
525 | (PTR) strlen (name), (PTR) NULL); | |
bd5635a1 | 526 | } |
cba0d141 | 527 | if (msymbol != NULL) |
bd5635a1 | 528 | { |
cba0d141 | 529 | s = find_pc_symtab (msymbol -> address); |
318bf84f | 530 | /* If S is NULL, there are no debug symbols for this file. |
997a978c | 531 | Skip this stuff and check for matching static symbols below. */ |
318bf84f | 532 | if (s != NULL) |
bd5635a1 RP |
533 | { |
534 | bv = BLOCKVECTOR (s); | |
3ba6a043 | 535 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); |
cba0d141 | 536 | sym = lookup_block_symbol (block, msymbol -> name, namespace); |
318bf84f | 537 | /* We kept static functions in minimal symbol table as well as |
818de002 | 538 | in static scope. We want to find them in the symbol table. */ |
818de002 PB |
539 | if (!sym) { |
540 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
318bf84f FF |
541 | sym = lookup_block_symbol (block, msymbol -> name, |
542 | namespace); | |
818de002 | 543 | } |
818de002 | 544 | |
cba0d141 | 545 | /* sym == 0 if symbol was found in the minimal symbol table |
bd5635a1 | 546 | but not in the symtab. |
cba0d141 | 547 | Return 0 to use the msymbol definition of "foo_". |
bd5635a1 RP |
548 | |
549 | This happens for Fortran "foo_" symbols, | |
550 | which are "foo" in the symtab. | |
551 | ||
552 | This can also happen if "asm" is used to make a | |
553 | regular symbol but not a debugging symbol, e.g. | |
554 | asm(".globl _main"); | |
555 | asm("_main:"); | |
556 | */ | |
557 | ||
558 | if (symtab != NULL) | |
559 | *symtab = s; | |
560 | return sym; | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
cba0d141 JG |
565 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
566 | { | |
567 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps->next) | |
568 | { | |
569 | if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace)) | |
570 | { | |
571 | s = PSYMTAB_TO_SYMTAB(ps); | |
572 | bv = BLOCKVECTOR (s); | |
573 | block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
574 | sym = lookup_block_symbol (block, name, namespace); | |
575 | if (!sym) | |
576 | error ("Internal: global symbol `%s' found in psymtab but not in symtab", name); | |
577 | if (symtab != NULL) | |
578 | *symtab = s; | |
579 | return sym; | |
580 | } | |
581 | } | |
582 | } | |
bd5635a1 RP |
583 | |
584 | /* Now search all per-file blocks. | |
585 | Not strictly correct, but more useful than an error. | |
586 | Do the symtabs first, then check the psymtabs */ | |
587 | ||
cba0d141 | 588 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 589 | { |
cba0d141 | 590 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
bd5635a1 | 591 | { |
cba0d141 JG |
592 | bv = BLOCKVECTOR (s); |
593 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
594 | sym = lookup_block_symbol (block, name, namespace); | |
595 | if (sym) | |
596 | { | |
597 | block_found = block; | |
598 | if (symtab != NULL) | |
599 | *symtab = s; | |
600 | return sym; | |
601 | } | |
bd5635a1 RP |
602 | } |
603 | } | |
604 | ||
cba0d141 JG |
605 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
606 | { | |
607 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps -> next) | |
608 | { | |
609 | if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace)) | |
610 | { | |
611 | s = PSYMTAB_TO_SYMTAB(ps); | |
612 | bv = BLOCKVECTOR (s); | |
613 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
614 | sym = lookup_block_symbol (block, name, namespace); | |
615 | if (!sym) | |
616 | error ("Internal: static symbol `%s' found in psymtab but not in symtab", name); | |
617 | if (symtab != NULL) | |
618 | *symtab = s; | |
619 | return sym; | |
620 | } | |
621 | } | |
622 | } | |
bd5635a1 | 623 | |
b039ac3a JK |
624 | /* Now search all per-file blocks for static mangled symbols. |
625 | Do the symtabs first, then check the psymtabs. */ | |
626 | ||
7c4f3f4a | 627 | if (namespace == VAR_NAMESPACE) |
b039ac3a | 628 | { |
cba0d141 | 629 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
b039ac3a | 630 | { |
cba0d141 | 631 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
b039ac3a | 632 | { |
cba0d141 JG |
633 | bv = BLOCKVECTOR (s); |
634 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
635 | sym = lookup_demangled_block_symbol (block, name); | |
636 | if (sym) | |
637 | { | |
638 | block_found = block; | |
639 | if (symtab != NULL) | |
640 | *symtab = s; | |
641 | return sym; | |
642 | } | |
b039ac3a JK |
643 | } |
644 | } | |
645 | ||
cba0d141 JG |
646 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
647 | { | |
648 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps -> next) | |
649 | { | |
650 | if (!ps->readin && lookup_demangled_partial_symbol (ps, name)) | |
651 | { | |
652 | s = PSYMTAB_TO_SYMTAB(ps); | |
653 | bv = BLOCKVECTOR (s); | |
654 | block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); | |
655 | sym = lookup_demangled_block_symbol (block, name); | |
656 | if (!sym) | |
657 | error ("Internal: mangled static symbol `%s' found in psymtab but not in symtab", name); | |
658 | if (symtab != NULL) | |
659 | *symtab = s; | |
660 | return sym; | |
661 | } | |
662 | } | |
663 | } | |
b039ac3a JK |
664 | } |
665 | ||
bd5635a1 RP |
666 | if (symtab != NULL) |
667 | *symtab = NULL; | |
668 | return 0; | |
669 | } | |
670 | ||
b039ac3a JK |
671 | /* Look for a static demangled symbol in block BLOCK. */ |
672 | ||
673 | static struct symbol * | |
674 | lookup_demangled_block_symbol (block, name) | |
cba0d141 JG |
675 | register const struct block *block; |
676 | const char *name; | |
b039ac3a JK |
677 | { |
678 | register int bot, top, inc; | |
679 | register struct symbol *sym; | |
680 | ||
681 | bot = 0; | |
682 | top = BLOCK_NSYMS (block); | |
683 | inc = name[0]; | |
684 | ||
685 | while (bot < top) | |
686 | { | |
687 | sym = BLOCK_SYM (block, bot); | |
688 | if (SYMBOL_NAME (sym)[0] == inc | |
689 | && SYMBOL_NAMESPACE (sym) == VAR_NAMESPACE) | |
690 | { | |
691 | char *demangled = cplus_demangle(SYMBOL_NAME (sym), -1); | |
692 | if (demangled != NULL) | |
693 | { | |
694 | int cond = strcmp (demangled, name); | |
695 | free (demangled); | |
696 | if (!cond) | |
697 | return sym; | |
698 | } | |
699 | } | |
700 | bot++; | |
701 | } | |
702 | ||
703 | return 0; | |
704 | } | |
705 | ||
706 | /* Look, in partial_symtab PST, for static mangled symbol NAME. */ | |
707 | ||
708 | static struct partial_symbol * | |
709 | lookup_demangled_partial_symbol (pst, name) | |
cba0d141 JG |
710 | const struct partial_symtab *pst; |
711 | const char *name; | |
b039ac3a JK |
712 | { |
713 | struct partial_symbol *start, *psym; | |
714 | int length = pst->n_static_syms; | |
715 | register int inc = name[0]; | |
716 | ||
717 | if (!length) | |
718 | return (struct partial_symbol *) 0; | |
719 | ||
cba0d141 | 720 | start = pst->objfile->static_psymbols.list + pst->statics_offset; |
b039ac3a JK |
721 | for (psym = start; psym < start + length; psym++) |
722 | { | |
723 | if (SYMBOL_NAME (psym)[0] == inc | |
724 | && SYMBOL_NAMESPACE (psym) == VAR_NAMESPACE) | |
725 | { | |
726 | char *demangled = cplus_demangle(SYMBOL_NAME (psym), -1); | |
727 | if (demangled != NULL) | |
728 | { | |
729 | int cond = strcmp (demangled, name); | |
730 | free (demangled); | |
731 | if (!cond) | |
732 | return psym; | |
733 | } | |
734 | } | |
735 | } | |
736 | ||
737 | return (struct partial_symbol *) 0; | |
738 | } | |
739 | ||
bd5635a1 RP |
740 | /* Look, in partial_symtab PST, for symbol NAME. Check the global |
741 | symbols if GLOBAL, the static symbols if not */ | |
742 | ||
743 | static struct partial_symbol * | |
744 | lookup_partial_symbol (pst, name, global, namespace) | |
745 | struct partial_symtab *pst; | |
cba0d141 | 746 | const char *name; |
bd5635a1 RP |
747 | int global; |
748 | enum namespace namespace; | |
749 | { | |
750 | struct partial_symbol *start, *psym; | |
751 | int length = (global ? pst->n_global_syms : pst->n_static_syms); | |
752 | ||
753 | if (!length) | |
754 | return (struct partial_symbol *) 0; | |
755 | ||
756 | start = (global ? | |
cba0d141 JG |
757 | pst->objfile->global_psymbols.list + pst->globals_offset : |
758 | pst->objfile->static_psymbols.list + pst->statics_offset ); | |
bd5635a1 RP |
759 | |
760 | if (global) /* This means we can use a binary */ | |
761 | /* search. */ | |
762 | { | |
763 | struct partial_symbol *top, *bottom, *center; | |
764 | ||
765 | /* Binary search. This search is guaranteed to end with center | |
766 | pointing at the earliest partial symbol with the correct | |
767 | name. At that point *all* partial symbols with that name | |
768 | will be checked against the correct namespace. */ | |
769 | bottom = start; | |
770 | top = start + length - 1; | |
771 | while (top > bottom) | |
772 | { | |
773 | center = bottom + (top - bottom) / 2; | |
774 | ||
775 | assert (center < top); | |
776 | ||
777 | if (strcmp (SYMBOL_NAME (center), name) >= 0) | |
778 | top = center; | |
779 | else | |
780 | bottom = center + 1; | |
781 | } | |
782 | assert (top == bottom); | |
783 | ||
784 | while (!strcmp (SYMBOL_NAME (top), name)) | |
785 | { | |
786 | if (SYMBOL_NAMESPACE (top) == namespace) | |
787 | return top; | |
788 | top ++; | |
789 | } | |
790 | } | |
791 | else | |
792 | { | |
793 | /* Can't use a binary search */ | |
794 | for (psym = start; psym < start + length; psym++) | |
795 | if (namespace == SYMBOL_NAMESPACE (psym) | |
796 | && !strcmp (name, SYMBOL_NAME (psym))) | |
797 | return psym; | |
798 | } | |
799 | ||
800 | return (struct partial_symbol *) 0; | |
801 | } | |
802 | ||
0e2a896c PB |
803 | /* Find the psymtab containing main(). */ |
804 | ||
805 | struct partial_symtab * | |
806 | find_main_psymtab () | |
807 | { | |
808 | register struct partial_symtab *pst; | |
cba0d141 JG |
809 | register struct objfile *objfile; |
810 | ||
811 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) | |
812 | { | |
813 | for (pst = objfile -> psymtabs; pst; pst = pst->next) | |
814 | { | |
815 | if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE)) | |
816 | { | |
817 | return (pst); | |
818 | } | |
819 | } | |
820 | } | |
821 | return (NULL); | |
0e2a896c PB |
822 | } |
823 | ||
bd5635a1 RP |
824 | /* Look for a symbol in block BLOCK. */ |
825 | ||
826 | struct symbol * | |
827 | lookup_block_symbol (block, name, namespace) | |
cba0d141 JG |
828 | register const struct block *block; |
829 | const char *name; | |
830 | const enum namespace namespace; | |
bd5635a1 RP |
831 | { |
832 | register int bot, top, inc; | |
833 | register struct symbol *sym, *parameter_sym; | |
834 | ||
835 | top = BLOCK_NSYMS (block); | |
836 | bot = 0; | |
837 | ||
838 | /* If the blocks's symbols were sorted, start with a binary search. */ | |
839 | ||
840 | if (BLOCK_SHOULD_SORT (block)) | |
841 | { | |
842 | /* First, advance BOT to not far before | |
843 | the first symbol whose name is NAME. */ | |
844 | ||
845 | while (1) | |
846 | { | |
847 | inc = (top - bot + 1); | |
848 | /* No need to keep binary searching for the last few bits worth. */ | |
849 | if (inc < 4) | |
850 | break; | |
851 | inc = (inc >> 1) + bot; | |
852 | sym = BLOCK_SYM (block, inc); | |
853 | if (SYMBOL_NAME (sym)[0] < name[0]) | |
854 | bot = inc; | |
855 | else if (SYMBOL_NAME (sym)[0] > name[0]) | |
856 | top = inc; | |
857 | else if (strcmp (SYMBOL_NAME (sym), name) < 0) | |
858 | bot = inc; | |
859 | else | |
860 | top = inc; | |
861 | } | |
862 | ||
863 | /* Now scan forward until we run out of symbols, | |
864 | find one whose name is greater than NAME, | |
865 | or find one we want. | |
866 | If there is more than one symbol with the right name and namespace, | |
867 | we return the first one. dbxread.c is careful to make sure | |
868 | that if one is a register then it comes first. */ | |
869 | ||
870 | top = BLOCK_NSYMS (block); | |
871 | while (bot < top) | |
872 | { | |
873 | sym = BLOCK_SYM (block, bot); | |
874 | inc = SYMBOL_NAME (sym)[0] - name[0]; | |
875 | if (inc == 0) | |
876 | inc = strcmp (SYMBOL_NAME (sym), name); | |
877 | if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace) | |
878 | return sym; | |
879 | if (inc > 0) | |
880 | return 0; | |
881 | bot++; | |
882 | } | |
883 | return 0; | |
884 | } | |
885 | ||
886 | /* Here if block isn't sorted. | |
887 | This loop is equivalent to the loop above, | |
888 | but hacked greatly for speed. | |
889 | ||
890 | Note that parameter symbols do not always show up last in the | |
891 | list; this loop makes sure to take anything else other than | |
892 | parameter symbols first; it only uses parameter symbols as a | |
893 | last resort. Note that this only takes up extra computation | |
894 | time on a match. */ | |
895 | ||
896 | parameter_sym = (struct symbol *) 0; | |
897 | top = BLOCK_NSYMS (block); | |
898 | inc = name[0]; | |
899 | while (bot < top) | |
900 | { | |
901 | sym = BLOCK_SYM (block, bot); | |
902 | if (SYMBOL_NAME (sym)[0] == inc | |
903 | && !strcmp (SYMBOL_NAME (sym), name) | |
904 | && SYMBOL_NAMESPACE (sym) == namespace) | |
905 | { | |
906 | if (SYMBOL_CLASS (sym) == LOC_ARG | |
907 | || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG | |
908 | || SYMBOL_CLASS (sym) == LOC_REF_ARG | |
909 | || SYMBOL_CLASS (sym) == LOC_REGPARM) | |
910 | parameter_sym = sym; | |
911 | else | |
912 | return sym; | |
913 | } | |
914 | bot++; | |
915 | } | |
916 | return parameter_sym; /* Will be 0 if not found. */ | |
917 | } | |
918 | \f | |
919 | /* Return the symbol for the function which contains a specified | |
920 | lexical block, described by a struct block BL. */ | |
921 | ||
922 | struct symbol * | |
923 | block_function (bl) | |
924 | struct block *bl; | |
925 | { | |
926 | while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0) | |
927 | bl = BLOCK_SUPERBLOCK (bl); | |
928 | ||
929 | return BLOCK_FUNCTION (bl); | |
930 | } | |
931 | ||
932 | /* Subroutine of find_pc_line */ | |
933 | ||
934 | struct symtab * | |
935 | find_pc_symtab (pc) | |
936 | register CORE_ADDR pc; | |
937 | { | |
938 | register struct block *b; | |
939 | struct blockvector *bv; | |
cba0d141 | 940 | register struct symtab *s = 0; |
bd5635a1 | 941 | register struct partial_symtab *ps; |
cba0d141 JG |
942 | register struct objfile *objfile; |
943 | register int found; | |
bd5635a1 RP |
944 | |
945 | /* Search all symtabs for one whose file contains our pc */ | |
946 | ||
cba0d141 JG |
947 | for (found = 0, objfile = object_files; |
948 | !found && objfile != NULL; | |
949 | objfile = objfile -> next) | |
bd5635a1 | 950 | { |
cba0d141 JG |
951 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
952 | { | |
953 | bv = BLOCKVECTOR (s); | |
954 | b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK); | |
955 | if (BLOCK_START (b) <= pc | |
956 | && BLOCK_END (b) > pc) | |
957 | { | |
958 | found++; | |
959 | break; | |
960 | } | |
961 | } | |
bd5635a1 RP |
962 | } |
963 | ||
964 | if (!s) | |
965 | { | |
966 | ps = find_pc_psymtab (pc); | |
967 | if (ps && ps->readin) | |
cba0d141 JG |
968 | { |
969 | printf_filtered ("(Internal error: pc 0x%x in read in psymtab, but not in symtab.)\n", pc); | |
970 | } | |
bd5635a1 | 971 | if (ps) |
cba0d141 JG |
972 | { |
973 | s = PSYMTAB_TO_SYMTAB (ps); | |
974 | } | |
bd5635a1 RP |
975 | } |
976 | ||
cba0d141 | 977 | return (s); |
bd5635a1 RP |
978 | } |
979 | ||
980 | /* Find the source file and line number for a given PC value. | |
981 | Return a structure containing a symtab pointer, a line number, | |
982 | and a pc range for the entire source line. | |
983 | The value's .pc field is NOT the specified pc. | |
984 | NOTCURRENT nonzero means, if specified pc is on a line boundary, | |
985 | use the line that ends there. Otherwise, in that case, the line | |
986 | that begins there is used. */ | |
987 | ||
988 | struct symtab_and_line | |
989 | find_pc_line (pc, notcurrent) | |
990 | CORE_ADDR pc; | |
991 | int notcurrent; | |
992 | { | |
993 | struct symtab *s; | |
994 | register struct linetable *l; | |
995 | register int len; | |
996 | register int i; | |
997 | register struct linetable_entry *item; | |
998 | struct symtab_and_line val; | |
999 | struct blockvector *bv; | |
1000 | ||
1001 | /* Info on best line seen so far, and where it starts, and its file. */ | |
1002 | ||
1003 | int best_line = 0; | |
1004 | CORE_ADDR best_pc = 0; | |
1005 | CORE_ADDR best_end = 0; | |
1006 | struct symtab *best_symtab = 0; | |
1007 | ||
1008 | /* Store here the first line number | |
1009 | of a file which contains the line at the smallest pc after PC. | |
1010 | If we don't find a line whose range contains PC, | |
1011 | we will use a line one less than this, | |
1012 | with a range from the start of that file to the first line's pc. */ | |
1013 | int alt_line = 0; | |
1014 | CORE_ADDR alt_pc = 0; | |
1015 | struct symtab *alt_symtab = 0; | |
1016 | ||
1017 | /* Info on best line seen in this file. */ | |
1018 | ||
1019 | int prev_line; | |
1020 | CORE_ADDR prev_pc; | |
1021 | ||
1022 | /* Info on first line of this file. */ | |
1023 | ||
1024 | int first_line; | |
1025 | CORE_ADDR first_pc; | |
1026 | ||
1027 | /* If this pc is not from the current frame, | |
1028 | it is the address of the end of a call instruction. | |
1029 | Quite likely that is the start of the following statement. | |
1030 | But what we want is the statement containing the instruction. | |
1031 | Fudge the pc to make sure we get that. */ | |
1032 | ||
1033 | if (notcurrent) pc -= 1; | |
1034 | ||
1035 | s = find_pc_symtab (pc); | |
1036 | if (s == 0) | |
1037 | { | |
1038 | val.symtab = 0; | |
1039 | val.line = 0; | |
1040 | val.pc = pc; | |
1041 | val.end = 0; | |
1042 | return val; | |
1043 | } | |
1044 | ||
1045 | bv = BLOCKVECTOR (s); | |
1046 | ||
1047 | /* Look at all the symtabs that share this blockvector. | |
1048 | They all have the same apriori range, that we found was right; | |
1049 | but they have different line tables. */ | |
1050 | ||
1051 | for (; s && BLOCKVECTOR (s) == bv; s = s->next) | |
1052 | { | |
1053 | /* Find the best line in this symtab. */ | |
1054 | l = LINETABLE (s); | |
4137c5fc JG |
1055 | if (!l) |
1056 | continue; | |
bd5635a1 RP |
1057 | len = l->nitems; |
1058 | prev_line = -1; | |
1059 | first_line = -1; | |
1060 | for (i = 0; i < len; i++) | |
1061 | { | |
1062 | item = &(l->item[i]); | |
1063 | ||
1064 | if (first_line < 0) | |
1065 | { | |
1066 | first_line = item->line; | |
1067 | first_pc = item->pc; | |
1068 | } | |
1069 | /* Return the last line that did not start after PC. */ | |
1070 | if (pc >= item->pc) | |
1071 | { | |
1072 | prev_line = item->line; | |
1073 | prev_pc = item->pc; | |
1074 | } | |
1075 | else | |
1076 | break; | |
1077 | } | |
1078 | ||
1079 | /* Is this file's best line closer than the best in the other files? | |
1080 | If so, record this file, and its best line, as best so far. */ | |
1081 | if (prev_line >= 0 && prev_pc > best_pc) | |
1082 | { | |
1083 | best_pc = prev_pc; | |
1084 | best_line = prev_line; | |
1085 | best_symtab = s; | |
cba0d141 JG |
1086 | /* If another line is in the linetable, and its PC is closer |
1087 | than the best_end we currently have, take it as best_end. */ | |
1088 | if (i < len && (best_end == 0 || best_end > item->pc)) | |
bd5635a1 | 1089 | best_end = item->pc; |
bd5635a1 RP |
1090 | } |
1091 | /* Is this file's first line closer than the first lines of other files? | |
1092 | If so, record this file, and its first line, as best alternate. */ | |
1093 | if (first_line >= 0 && first_pc > pc | |
1094 | && (alt_pc == 0 || first_pc < alt_pc)) | |
1095 | { | |
1096 | alt_pc = first_pc; | |
1097 | alt_line = first_line; | |
1098 | alt_symtab = s; | |
1099 | } | |
1100 | } | |
1101 | if (best_symtab == 0) | |
1102 | { | |
1103 | val.symtab = alt_symtab; | |
1104 | val.line = alt_line - 1; | |
3ba6a043 | 1105 | val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)); |
bd5635a1 RP |
1106 | val.end = alt_pc; |
1107 | } | |
1108 | else | |
1109 | { | |
1110 | val.symtab = best_symtab; | |
1111 | val.line = best_line; | |
1112 | val.pc = best_pc; | |
cba0d141 JG |
1113 | if (best_end && (alt_pc == 0 || best_end < alt_pc)) |
1114 | val.end = best_end; | |
1115 | else if (alt_pc) | |
1116 | val.end = alt_pc; | |
1117 | else | |
1118 | val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)); | |
bd5635a1 RP |
1119 | } |
1120 | return val; | |
1121 | } | |
1122 | \f | |
1123 | /* Find the PC value for a given source file and line number. | |
1124 | Returns zero for invalid line number. | |
1125 | The source file is specified with a struct symtab. */ | |
1126 | ||
1127 | CORE_ADDR | |
1128 | find_line_pc (symtab, line) | |
1129 | struct symtab *symtab; | |
1130 | int line; | |
1131 | { | |
1132 | register struct linetable *l; | |
1133 | register int ind; | |
1134 | int dummy; | |
1135 | ||
1136 | if (symtab == 0) | |
1137 | return 0; | |
1138 | l = LINETABLE (symtab); | |
1139 | ind = find_line_common(l, line, &dummy); | |
b203fc18 | 1140 | return (ind >= 0) ? l->item[ind].pc : 0; |
bd5635a1 RP |
1141 | } |
1142 | ||
1143 | /* Find the range of pc values in a line. | |
1144 | Store the starting pc of the line into *STARTPTR | |
1145 | and the ending pc (start of next line) into *ENDPTR. | |
1146 | Returns 1 to indicate success. | |
1147 | Returns 0 if could not find the specified line. */ | |
1148 | ||
1149 | int | |
1150 | find_line_pc_range (symtab, thisline, startptr, endptr) | |
1151 | struct symtab *symtab; | |
1152 | int thisline; | |
1153 | CORE_ADDR *startptr, *endptr; | |
1154 | { | |
1155 | register struct linetable *l; | |
1156 | register int ind; | |
1157 | int exact_match; /* did we get an exact linenumber match */ | |
1158 | ||
1159 | if (symtab == 0) | |
1160 | return 0; | |
1161 | ||
1162 | l = LINETABLE (symtab); | |
1163 | ind = find_line_common (l, thisline, &exact_match); | |
b203fc18 | 1164 | if (ind >= 0) |
bd5635a1 RP |
1165 | { |
1166 | *startptr = l->item[ind].pc; | |
1167 | /* If we have not seen an entry for the specified line, | |
1168 | assume that means the specified line has zero bytes. */ | |
1169 | if (!exact_match || ind == l->nitems-1) | |
1170 | *endptr = *startptr; | |
1171 | else | |
1172 | /* Perhaps the following entry is for the following line. | |
1173 | It's worth a try. */ | |
1174 | if (ind+1 < l->nitems | |
1175 | && l->item[ind+1].line == thisline + 1) | |
1176 | *endptr = l->item[ind+1].pc; | |
1177 | else | |
1178 | *endptr = find_line_pc (symtab, thisline+1); | |
1179 | return 1; | |
1180 | } | |
1181 | ||
1182 | return 0; | |
1183 | } | |
1184 | ||
1185 | /* Given a line table and a line number, return the index into the line | |
1186 | table for the pc of the nearest line whose number is >= the specified one. | |
b203fc18 | 1187 | Return -1 if none is found. The value is >= 0 if it is an index. |
bd5635a1 RP |
1188 | |
1189 | Set *EXACT_MATCH nonzero if the value returned is an exact match. */ | |
1190 | ||
1191 | static int | |
1192 | find_line_common (l, lineno, exact_match) | |
1193 | register struct linetable *l; | |
1194 | register int lineno; | |
1195 | int *exact_match; | |
1196 | { | |
1197 | register int i; | |
1198 | register int len; | |
1199 | ||
1200 | /* BEST is the smallest linenumber > LINENO so far seen, | |
1201 | or 0 if none has been seen so far. | |
1202 | BEST_INDEX identifies the item for it. */ | |
1203 | ||
b203fc18 | 1204 | int best_index = -1; |
bd5635a1 RP |
1205 | int best = 0; |
1206 | ||
1207 | if (lineno <= 0) | |
b203fc18 | 1208 | return -1; |
4137c5fc JG |
1209 | if (l == 0) |
1210 | return -1; | |
bd5635a1 RP |
1211 | |
1212 | len = l->nitems; | |
1213 | for (i = 0; i < len; i++) | |
1214 | { | |
1215 | register struct linetable_entry *item = &(l->item[i]); | |
1216 | ||
1217 | if (item->line == lineno) | |
1218 | { | |
1219 | *exact_match = 1; | |
1220 | return i; | |
1221 | } | |
1222 | ||
1223 | if (item->line > lineno && (best == 0 || item->line < best)) | |
1224 | { | |
1225 | best = item->line; | |
1226 | best_index = i; | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | /* If we got here, we didn't get an exact match. */ | |
1231 | ||
1232 | *exact_match = 0; | |
1233 | return best_index; | |
1234 | } | |
1235 | ||
1236 | int | |
1237 | find_pc_line_pc_range (pc, startptr, endptr) | |
1238 | CORE_ADDR pc; | |
1239 | CORE_ADDR *startptr, *endptr; | |
1240 | { | |
1241 | struct symtab_and_line sal; | |
1242 | sal = find_pc_line (pc, 0); | |
1243 | *startptr = sal.pc; | |
1244 | *endptr = sal.end; | |
1245 | return sal.symtab != 0; | |
1246 | } | |
1247 | \f | |
d96b54ea JK |
1248 | /* If P is of the form "operator[ \t]+..." where `...' is |
1249 | some legitimate operator text, return a pointer to the | |
1250 | beginning of the substring of the operator text. | |
1251 | Otherwise, return "". */ | |
1252 | static char * | |
1253 | operator_chars (p, end) | |
1254 | char *p; | |
1255 | char **end; | |
1256 | { | |
1257 | *end = ""; | |
1258 | if (strncmp (p, "operator", 8)) | |
1259 | return *end; | |
1260 | p += 8; | |
1261 | ||
1262 | /* Don't get faked out by `operator' being part of a longer | |
1263 | identifier. */ | |
2cd99985 | 1264 | if (isalpha(*p) || *p == '_' || *p == '$' || *p == '\0') |
d96b54ea JK |
1265 | return *end; |
1266 | ||
1267 | /* Allow some whitespace between `operator' and the operator symbol. */ | |
1268 | while (*p == ' ' || *p == '\t') | |
1269 | p++; | |
1270 | ||
2cd99985 PB |
1271 | /* Recognize 'operator TYPENAME'. */ |
1272 | ||
1273 | if (isalpha(*p) || *p == '_' || *p == '$') | |
1274 | { | |
1275 | register char *q = p+1; | |
1276 | while (isalnum(*q) || *q == '_' || *q == '$') | |
1277 | q++; | |
1278 | *end = q; | |
1279 | return p; | |
1280 | } | |
1281 | ||
d96b54ea JK |
1282 | switch (*p) |
1283 | { | |
1284 | case '!': | |
1285 | case '=': | |
1286 | case '*': | |
1287 | case '/': | |
1288 | case '%': | |
1289 | case '^': | |
1290 | if (p[1] == '=') | |
1291 | *end = p+2; | |
1292 | else | |
1293 | *end = p+1; | |
1294 | return p; | |
1295 | case '<': | |
1296 | case '>': | |
1297 | case '+': | |
1298 | case '-': | |
1299 | case '&': | |
1300 | case '|': | |
1301 | if (p[1] == '=' || p[1] == p[0]) | |
1302 | *end = p+2; | |
1303 | else | |
1304 | *end = p+1; | |
1305 | return p; | |
1306 | case '~': | |
1307 | case ',': | |
1308 | *end = p+1; | |
1309 | return p; | |
1310 | case '(': | |
1311 | if (p[1] != ')') | |
1312 | error ("`operator ()' must be specified without whitespace in `()'"); | |
1313 | *end = p+2; | |
1314 | return p; | |
1315 | case '?': | |
1316 | if (p[1] != ':') | |
1317 | error ("`operator ?:' must be specified without whitespace in `?:'"); | |
1318 | *end = p+2; | |
1319 | return p; | |
1320 | case '[': | |
1321 | if (p[1] != ']') | |
1322 | error ("`operator []' must be specified without whitespace in `[]'"); | |
1323 | *end = p+2; | |
1324 | return p; | |
1325 | default: | |
1326 | error ("`operator %s' not supported", p); | |
1327 | break; | |
1328 | } | |
1329 | *end = ""; | |
1330 | return *end; | |
1331 | } | |
1332 | ||
bd5635a1 RP |
1333 | /* Recursive helper function for decode_line_1. |
1334 | * Look for methods named NAME in type T. | |
1335 | * Return number of matches. | |
1336 | * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!). | |
1337 | * These allocations seem to define "big enough": | |
1338 | * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*)); | |
1339 | * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*)); | |
1340 | */ | |
1341 | ||
2cd99985 | 1342 | int |
7e258d18 | 1343 | find_methods (t, name, physnames, sym_arr) |
bd5635a1 RP |
1344 | struct type *t; |
1345 | char *name; | |
1346 | char **physnames; | |
1347 | struct symbol **sym_arr; | |
1348 | { | |
1349 | int i1 = 0; | |
1350 | int ibase; | |
1351 | struct symbol *sym_class; | |
1352 | char *class_name = type_name_no_tag (t); | |
1353 | /* Ignore this class if it doesn't have a name. | |
1354 | This prevents core dumps, but is just a workaround | |
1355 | because we might not find the function in | |
1356 | certain cases, such as | |
1357 | struct D {virtual int f();} | |
1358 | struct C : D {virtual int g();} | |
1359 | (in this case g++ 1.35.1- does not put out a name | |
1360 | for D as such, it defines type 19 (for example) in | |
1361 | the same stab as C, and then does a | |
1362 | .stabs "D:T19" and a .stabs "D:t19". | |
1363 | Thus | |
1364 | "break C::f" should not be looking for field f in | |
1365 | the class named D, | |
1366 | but just for the field f in the baseclasses of C | |
1367 | (no matter what their names). | |
1368 | ||
1369 | However, I don't know how to replace the code below | |
1370 | that depends on knowing the name of D. */ | |
1371 | if (class_name | |
1372 | && (sym_class = lookup_symbol (class_name, | |
1373 | (struct block *)NULL, | |
1374 | STRUCT_NAMESPACE, | |
1375 | (int *)NULL, | |
1376 | (struct symtab **)NULL))) | |
1377 | { | |
1378 | int method_counter; | |
1379 | t = SYMBOL_TYPE (sym_class); | |
1380 | for (method_counter = TYPE_NFN_FIELDS (t) - 1; | |
1381 | method_counter >= 0; | |
1382 | --method_counter) | |
1383 | { | |
1384 | int field_counter; | |
1385 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter); | |
1386 | ||
1387 | char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter); | |
1388 | if (!strcmp (name, method_name)) | |
1389 | /* Find all the fields with that name. */ | |
1390 | for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1; | |
1391 | field_counter >= 0; | |
1392 | --field_counter) | |
1393 | { | |
1394 | char *phys_name; | |
7e258d18 | 1395 | if (TYPE_FN_FIELD_STUB (f, field_counter)) |
bd5635a1 RP |
1396 | check_stub_method (t, method_counter, field_counter); |
1397 | phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter); | |
1398 | physnames[i1] = (char*) alloca (strlen (phys_name) + 1); | |
1399 | strcpy (physnames[i1], phys_name); | |
1400 | sym_arr[i1] = lookup_symbol (phys_name, | |
1401 | SYMBOL_BLOCK_VALUE (sym_class), | |
1402 | VAR_NAMESPACE, | |
1403 | (int *) NULL, | |
1404 | (struct symtab **) NULL); | |
1405 | if (sym_arr[i1]) i1++; | |
2cd99985 PB |
1406 | else |
1407 | { | |
1408 | fputs_filtered("(Cannot find method ", stdout); | |
1409 | fputs_demangled(phys_name, stdout, 0); | |
1410 | fputs_filtered(" - possibly inlined.)\n", stdout); | |
1411 | } | |
bd5635a1 RP |
1412 | } |
1413 | } | |
1414 | } | |
1415 | /* Only search baseclasses if there is no match yet, | |
1416 | * since names in derived classes override those in baseclasses. | |
1417 | */ | |
1418 | if (i1) | |
1419 | return i1; | |
1420 | for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++) | |
1421 | i1 += find_methods(TYPE_BASECLASS(t, ibase), name, | |
1422 | physnames + i1, sym_arr + i1); | |
1423 | return i1; | |
1424 | } | |
1425 | ||
1426 | /* Parse a string that specifies a line number. | |
1427 | Pass the address of a char * variable; that variable will be | |
1428 | advanced over the characters actually parsed. | |
1429 | ||
1430 | The string can be: | |
1431 | ||
1432 | LINENUM -- that line number in current file. PC returned is 0. | |
1433 | FILE:LINENUM -- that line in that file. PC returned is 0. | |
1434 | FUNCTION -- line number of openbrace of that function. | |
1435 | PC returned is the start of the function. | |
1436 | VARIABLE -- line number of definition of that variable. | |
1437 | PC returned is 0. | |
1438 | FILE:FUNCTION -- likewise, but prefer functions in that file. | |
1439 | *EXPR -- line in which address EXPR appears. | |
1440 | ||
cba0d141 | 1441 | FUNCTION may be an undebuggable function found in minimal symbol table. |
bd5635a1 RP |
1442 | |
1443 | If the argument FUNFIRSTLINE is nonzero, we want the first line | |
1444 | of real code inside a function when a function is specified. | |
1445 | ||
1446 | DEFAULT_SYMTAB specifies the file to use if none is specified. | |
1447 | It defaults to current_source_symtab. | |
1448 | DEFAULT_LINE specifies the line number to use for relative | |
1449 | line numbers (that start with signs). Defaults to current_source_line. | |
1450 | ||
1451 | Note that it is possible to return zero for the symtab | |
1452 | if no file is validly specified. Callers must check that. | |
1453 | Also, the line number returned may be invalid. */ | |
1454 | ||
1455 | struct symtabs_and_lines | |
1456 | decode_line_1 (argptr, funfirstline, default_symtab, default_line) | |
1457 | char **argptr; | |
1458 | int funfirstline; | |
1459 | struct symtab *default_symtab; | |
1460 | int default_line; | |
1461 | { | |
bd5635a1 RP |
1462 | struct symtabs_and_lines values; |
1463 | struct symtab_and_line val; | |
1464 | register char *p, *p1; | |
d96b54ea | 1465 | char *q, *q1; |
bd5635a1 RP |
1466 | register struct symtab *s; |
1467 | ||
1468 | register struct symbol *sym; | |
1469 | /* The symtab that SYM was found in. */ | |
1470 | struct symtab *sym_symtab; | |
1471 | ||
1472 | register CORE_ADDR pc; | |
cba0d141 | 1473 | register struct minimal_symbol *msymbol; |
bd5635a1 RP |
1474 | char *copy; |
1475 | struct symbol *sym_class; | |
1476 | int i1; | |
1477 | struct symbol **sym_arr; | |
1478 | struct type *t; | |
1479 | char **physnames; | |
1480 | ||
1481 | /* Defaults have defaults. */ | |
1482 | ||
1483 | if (default_symtab == 0) | |
1484 | { | |
1485 | default_symtab = current_source_symtab; | |
1486 | default_line = current_source_line; | |
1487 | } | |
1488 | ||
1489 | /* See if arg is *PC */ | |
1490 | ||
1491 | if (**argptr == '*') | |
1492 | { | |
1493 | (*argptr)++; | |
1494 | pc = parse_and_eval_address_1 (argptr); | |
1495 | values.sals = (struct symtab_and_line *) | |
1496 | xmalloc (sizeof (struct symtab_and_line)); | |
1497 | values.nelts = 1; | |
1498 | values.sals[0] = find_pc_line (pc, 0); | |
1499 | values.sals[0].pc = pc; | |
1500 | return values; | |
1501 | } | |
1502 | ||
1503 | /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */ | |
1504 | ||
1505 | s = 0; | |
1506 | ||
1507 | for (p = *argptr; *p; p++) | |
1508 | { | |
1509 | if (p[0] == ':' || p[0] == ' ' || p[0] == '\t') | |
1510 | break; | |
1511 | } | |
1512 | while (p[0] == ' ' || p[0] == '\t') p++; | |
1513 | ||
1514 | if (p[0] == ':') | |
1515 | { | |
1516 | ||
1517 | /* C++ */ | |
1518 | if (p[1] ==':') | |
1519 | { | |
1520 | /* Extract the class name. */ | |
1521 | p1 = p; | |
1522 | while (p != *argptr && p[-1] == ' ') --p; | |
1523 | copy = (char *) alloca (p - *argptr + 1); | |
1524 | bcopy (*argptr, copy, p - *argptr); | |
1525 | copy[p - *argptr] = 0; | |
1526 | ||
1527 | /* Discard the class name from the arg. */ | |
1528 | p = p1 + 2; | |
1529 | while (*p == ' ' || *p == '\t') p++; | |
1530 | *argptr = p; | |
1531 | ||
1532 | sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0, | |
1533 | (struct symtab **)NULL); | |
1534 | ||
1535 | if (sym_class && | |
f1d77e90 | 1536 | ( TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT |
bd5635a1 RP |
1537 | || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION)) |
1538 | { | |
1539 | /* Arg token is not digits => try it as a function name | |
1540 | Find the next token (everything up to end or next whitespace). */ | |
1541 | p = *argptr; | |
1542 | while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++; | |
d96b54ea | 1543 | q = operator_chars (*argptr, &q1); |
aec4cb91 | 1544 | |
d96b54ea JK |
1545 | if (q1 - q) |
1546 | { | |
2cd99985 PB |
1547 | char *opname; |
1548 | char *tmp = alloca (q1 - q + 1); | |
1549 | memcpy (tmp, q, q1 - q); | |
1550 | tmp[q1 - q] = '\0'; | |
1551 | opname = cplus_mangle_opname (tmp, 1); | |
1552 | if (opname == NULL) | |
1553 | error ("No mangling for \"%s\"", tmp); | |
1554 | copy = (char*) alloca (3 + strlen(opname)); | |
1555 | sprintf (copy, "__%s", opname); | |
d96b54ea JK |
1556 | p = q1; |
1557 | } | |
1558 | else | |
1559 | { | |
2cd99985 | 1560 | copy = (char *) alloca (p - *argptr + 1 + (q1 - q)); |
d96b54ea JK |
1561 | bcopy (*argptr, copy, p - *argptr); |
1562 | copy[p - *argptr] = '\0'; | |
1563 | } | |
bd5635a1 RP |
1564 | |
1565 | /* no line number may be specified */ | |
1566 | while (*p == ' ' || *p == '\t') p++; | |
1567 | *argptr = p; | |
1568 | ||
1569 | sym = 0; | |
1570 | i1 = 0; /* counter for the symbol array */ | |
1571 | t = SYMBOL_TYPE (sym_class); | |
1572 | sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*)); | |
1573 | physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*)); | |
1574 | ||
1575 | if (destructor_name_p (copy, t)) | |
1576 | { | |
1577 | /* destructors are a special case. */ | |
1578 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0); | |
1579 | int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1; | |
1580 | char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len); | |
1581 | physnames[i1] = (char *)alloca (strlen (phys_name) + 1); | |
1582 | strcpy (physnames[i1], phys_name); | |
1583 | sym_arr[i1] = | |
1584 | lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), | |
1585 | VAR_NAMESPACE, 0, (struct symtab **)NULL); | |
1586 | if (sym_arr[i1]) i1++; | |
1587 | } | |
1588 | else | |
1589 | i1 = find_methods (t, copy, physnames, sym_arr); | |
1590 | if (i1 == 1) | |
1591 | { | |
1592 | /* There is exactly one field with that name. */ | |
1593 | sym = sym_arr[0]; | |
1594 | ||
1595 | if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK) | |
1596 | { | |
1597 | /* Arg is the name of a function */ | |
1598 | pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET; | |
1599 | if (funfirstline) | |
1600 | SKIP_PROLOGUE (pc); | |
1601 | values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line)); | |
1602 | values.nelts = 1; | |
1603 | values.sals[0] = find_pc_line (pc, 0); | |
1604 | values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc; | |
1605 | } | |
1606 | else | |
1607 | { | |
1608 | values.nelts = 0; | |
1609 | } | |
1610 | return values; | |
1611 | } | |
1612 | if (i1 > 0) | |
1613 | { | |
1614 | /* There is more than one field with that name | |
1615 | (overloaded). Ask the user which one to use. */ | |
1616 | return decode_line_2 (sym_arr, i1, funfirstline); | |
1617 | } | |
1618 | else | |
d96b54ea JK |
1619 | { |
1620 | char *tmp; | |
1621 | ||
1622 | if (OPNAME_PREFIX_P (copy)) | |
1623 | { | |
1624 | tmp = (char *)alloca (strlen (copy+3) + 9); | |
1625 | strcpy (tmp, "operator "); | |
1626 | strcat (tmp, copy+3); | |
1627 | } | |
1628 | else | |
1629 | tmp = copy; | |
0e2a896c PB |
1630 | if (tmp[0] == '~') |
1631 | error ("The class `%s' does not have destructor defined", | |
1632 | sym_class->name); | |
1633 | else | |
1634 | error ("The class %s does not have any method named %s", | |
1635 | sym_class->name, tmp); | |
d96b54ea | 1636 | } |
bd5635a1 RP |
1637 | } |
1638 | else | |
1639 | /* The quotes are important if copy is empty. */ | |
1640 | error("No class, struct, or union named \"%s\"", copy ); | |
1641 | } | |
1642 | /* end of C++ */ | |
1643 | ||
1644 | ||
1645 | /* Extract the file name. */ | |
1646 | p1 = p; | |
1647 | while (p != *argptr && p[-1] == ' ') --p; | |
58050209 DHW |
1648 | copy = (char *) alloca (p - *argptr + 1); |
1649 | bcopy (*argptr, copy, p - *argptr); | |
1650 | copy[p - *argptr] = 0; | |
bd5635a1 RP |
1651 | |
1652 | /* Find that file's data. */ | |
1653 | s = lookup_symtab (copy); | |
1654 | if (s == 0) | |
1655 | { | |
cba0d141 | 1656 | if (!have_full_symbols () && !have_partial_symbols ()) |
bd5635a1 RP |
1657 | error (no_symtab_msg); |
1658 | error ("No source file named %s.", copy); | |
1659 | } | |
1660 | ||
1661 | /* Discard the file name from the arg. */ | |
1662 | p = p1 + 1; | |
1663 | while (*p == ' ' || *p == '\t') p++; | |
1664 | *argptr = p; | |
1665 | } | |
1666 | ||
1667 | /* S is specified file's symtab, or 0 if no file specified. | |
1668 | arg no longer contains the file name. */ | |
1669 | ||
1670 | /* Check whether arg is all digits (and sign) */ | |
1671 | ||
1672 | p = *argptr; | |
1673 | if (*p == '-' || *p == '+') p++; | |
1674 | while (*p >= '0' && *p <= '9') | |
1675 | p++; | |
1676 | ||
1677 | if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ',')) | |
1678 | { | |
1679 | /* We found a token consisting of all digits -- at least one digit. */ | |
1680 | enum sign {none, plus, minus} sign = none; | |
1681 | ||
1682 | /* This is where we need to make sure that we have good defaults. | |
1683 | We must guarantee that this section of code is never executed | |
1684 | when we are called with just a function name, since | |
1685 | select_source_symtab calls us with such an argument */ | |
1686 | ||
1687 | if (s == 0 && default_symtab == 0) | |
1688 | { | |
bd5635a1 RP |
1689 | select_source_symtab (0); |
1690 | default_symtab = current_source_symtab; | |
1691 | default_line = current_source_line; | |
1692 | } | |
1693 | ||
1694 | if (**argptr == '+') | |
1695 | sign = plus, (*argptr)++; | |
1696 | else if (**argptr == '-') | |
1697 | sign = minus, (*argptr)++; | |
1698 | val.line = atoi (*argptr); | |
1699 | switch (sign) | |
1700 | { | |
1701 | case plus: | |
1702 | if (p == *argptr) | |
1703 | val.line = 5; | |
1704 | if (s == 0) | |
1705 | val.line = default_line + val.line; | |
1706 | break; | |
1707 | case minus: | |
1708 | if (p == *argptr) | |
1709 | val.line = 15; | |
1710 | if (s == 0) | |
1711 | val.line = default_line - val.line; | |
1712 | else | |
1713 | val.line = 1; | |
1714 | break; | |
1715 | case none: | |
1716 | break; /* No need to adjust val.line. */ | |
1717 | } | |
1718 | ||
1719 | while (*p == ' ' || *p == '\t') p++; | |
1720 | *argptr = p; | |
1721 | if (s == 0) | |
1722 | s = default_symtab; | |
1723 | val.symtab = s; | |
1724 | val.pc = 0; | |
1725 | values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line)); | |
1726 | values.sals[0] = val; | |
1727 | values.nelts = 1; | |
1728 | return values; | |
1729 | } | |
1730 | ||
1731 | /* Arg token is not digits => try it as a variable name | |
1732 | Find the next token (everything up to end or next whitespace). */ | |
2cd99985 | 1733 | |
bd5635a1 RP |
1734 | p = *argptr; |
1735 | while (*p && *p != ' ' && *p != '\t' && *p != ',') p++; | |
1736 | copy = (char *) alloca (p - *argptr + 1); | |
1737 | bcopy (*argptr, copy, p - *argptr); | |
1738 | copy[p - *argptr] = 0; | |
1739 | while (*p == ' ' || *p == '\t') p++; | |
1740 | *argptr = p; | |
1741 | ||
1742 | /* Look up that token as a variable. | |
1743 | If file specified, use that file's per-file block to start with. */ | |
1744 | ||
1745 | sym = lookup_symbol (copy, | |
3ba6a043 | 1746 | (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK) |
bd5635a1 RP |
1747 | : get_selected_block ()), |
1748 | VAR_NAMESPACE, 0, &sym_symtab); | |
1749 | ||
1750 | if (sym != NULL) | |
1751 | { | |
1752 | if (SYMBOL_CLASS (sym) == LOC_BLOCK) | |
1753 | { | |
1754 | /* Arg is the name of a function */ | |
1755 | pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET; | |
1756 | if (funfirstline) | |
1757 | SKIP_PROLOGUE (pc); | |
1758 | val = find_pc_line (pc, 0); | |
1759 | #ifdef PROLOGUE_FIRSTLINE_OVERLAP | |
1760 | /* Convex: no need to suppress code on first line, if any */ | |
1761 | val.pc = pc; | |
1762 | #else | |
7b2a87ca JG |
1763 | /* If SKIP_PROLOGUE left us in mid-line, and the next line is still |
1764 | part of the same function: | |
1765 | advance to next line, | |
1766 | recalculate its line number (might not be N+1). */ | |
1767 | if (val.pc != pc && val.end && | |
cba0d141 | 1768 | lookup_minimal_symbol_by_pc (pc) == lookup_minimal_symbol_by_pc (val.end)) { |
7b2a87ca JG |
1769 | pc = val.end; /* First pc of next line */ |
1770 | val = find_pc_line (pc, 0); | |
1771 | } | |
1772 | val.pc = pc; | |
bd5635a1 RP |
1773 | #endif |
1774 | values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line)); | |
1775 | values.sals[0] = val; | |
1776 | values.nelts = 1; | |
1777 | ||
1778 | /* I think this is always the same as the line that | |
1779 | we calculate above, but the general principle is | |
1780 | "trust the symbols more than stuff like | |
1781 | SKIP_PROLOGUE". */ | |
1782 | if (SYMBOL_LINE (sym) != 0) | |
1783 | values.sals[0].line = SYMBOL_LINE (sym); | |
1784 | ||
1785 | return values; | |
1786 | } | |
1787 | else if (SYMBOL_LINE (sym) != 0) | |
1788 | { | |
1789 | /* We know its line number. */ | |
1790 | values.sals = (struct symtab_and_line *) | |
1791 | xmalloc (sizeof (struct symtab_and_line)); | |
1792 | values.nelts = 1; | |
1793 | bzero (&values.sals[0], sizeof (values.sals[0])); | |
1794 | values.sals[0].symtab = sym_symtab; | |
1795 | values.sals[0].line = SYMBOL_LINE (sym); | |
1796 | return values; | |
1797 | } | |
1798 | else | |
1799 | /* This can happen if it is compiled with a compiler which doesn't | |
1800 | put out line numbers for variables. */ | |
1801 | error ("Line number not known for symbol \"%s\"", copy); | |
1802 | } | |
1803 | ||
cba0d141 JG |
1804 | msymbol = lookup_minimal_symbol (copy, (struct objfile *) NULL); |
1805 | if (msymbol != NULL) | |
bd5635a1 RP |
1806 | { |
1807 | val.symtab = 0; | |
1808 | val.line = 0; | |
cba0d141 | 1809 | val.pc = msymbol -> address + FUNCTION_START_OFFSET; |
bd5635a1 RP |
1810 | if (funfirstline) |
1811 | SKIP_PROLOGUE (val.pc); | |
1812 | values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line)); | |
1813 | values.sals[0] = val; | |
1814 | values.nelts = 1; | |
1815 | return values; | |
1816 | } | |
1817 | ||
cba0d141 JG |
1818 | if (!have_full_symbols () && |
1819 | !have_partial_symbols () && !have_minimal_symbols ()) | |
997a978c JG |
1820 | error (no_symtab_msg); |
1821 | ||
bd5635a1 RP |
1822 | error ("Function %s not defined.", copy); |
1823 | return values; /* for lint */ | |
1824 | } | |
1825 | ||
1826 | struct symtabs_and_lines | |
1827 | decode_line_spec (string, funfirstline) | |
1828 | char *string; | |
1829 | int funfirstline; | |
1830 | { | |
1831 | struct symtabs_and_lines sals; | |
1832 | if (string == 0) | |
1833 | error ("Empty line specification."); | |
1834 | sals = decode_line_1 (&string, funfirstline, | |
1835 | current_source_symtab, current_source_line); | |
1836 | if (*string) | |
1837 | error ("Junk at end of line specification: %s", string); | |
1838 | return sals; | |
1839 | } | |
1840 | ||
1841 | /* Given a list of NELTS symbols in sym_arr (with corresponding | |
1842 | mangled names in physnames), return a list of lines to operate on | |
1843 | (ask user if necessary). */ | |
cba0d141 | 1844 | static struct symtabs_and_lines |
bd5635a1 RP |
1845 | decode_line_2 (sym_arr, nelts, funfirstline) |
1846 | struct symbol *sym_arr[]; | |
1847 | int nelts; | |
1848 | int funfirstline; | |
1849 | { | |
bd5635a1 RP |
1850 | struct symtabs_and_lines values, return_values; |
1851 | register CORE_ADDR pc; | |
cba0d141 | 1852 | char *args, *arg1; |
bd5635a1 RP |
1853 | int i; |
1854 | char *prompt; | |
1855 | ||
1856 | values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line)); | |
1857 | return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line)); | |
1858 | ||
1859 | i = 0; | |
1860 | printf("[0] cancel\n[1] all\n"); | |
1861 | while (i < nelts) | |
1862 | { | |
1863 | if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK) | |
1864 | { | |
1865 | /* Arg is the name of a function */ | |
1866 | pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i])) | |
1867 | + FUNCTION_START_OFFSET; | |
1868 | if (funfirstline) | |
1869 | SKIP_PROLOGUE (pc); | |
1870 | values.sals[i] = find_pc_line (pc, 0); | |
1871 | values.sals[i].pc = (values.sals[i].end && values.sals[i].pc != pc) ? | |
1872 | values.sals[i].end : pc; | |
1873 | printf("[%d] file:%s; line number:%d\n", | |
1874 | (i+2), values.sals[i].symtab->filename, values.sals[i].line); | |
1875 | } | |
1876 | else printf ("?HERE\n"); | |
1877 | i++; | |
1878 | } | |
1879 | ||
1880 | if ((prompt = getenv ("PS2")) == NULL) | |
1881 | { | |
1882 | prompt = ">"; | |
1883 | } | |
1884 | printf("%s ",prompt); | |
1885 | fflush(stdout); | |
1886 | ||
cba0d141 | 1887 | args = command_line_input ((char *) NULL, 0); |
bd5635a1 RP |
1888 | |
1889 | if (args == 0) | |
1890 | error_no_arg ("one or more choice numbers"); | |
1891 | ||
1892 | i = 0; | |
1893 | while (*args) | |
1894 | { | |
1895 | int num; | |
1896 | ||
1897 | arg1 = args; | |
1898 | while (*arg1 >= '0' && *arg1 <= '9') arg1++; | |
1899 | if (*arg1 && *arg1 != ' ' && *arg1 != '\t') | |
1900 | error ("Arguments must be choice numbers."); | |
1901 | ||
1902 | num = atoi (args); | |
1903 | ||
1904 | if (num == 0) | |
1905 | error ("cancelled"); | |
1906 | else if (num == 1) | |
1907 | { | |
1908 | bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line))); | |
1909 | return_values.nelts = nelts; | |
1910 | return return_values; | |
1911 | } | |
1912 | ||
1913 | if (num > nelts + 2) | |
1914 | { | |
1915 | printf ("No choice number %d.\n", num); | |
1916 | } | |
1917 | else | |
1918 | { | |
1919 | num -= 2; | |
1920 | if (values.sals[num].pc) | |
1921 | { | |
1922 | return_values.sals[i++] = values.sals[num]; | |
1923 | values.sals[num].pc = 0; | |
1924 | } | |
1925 | else | |
1926 | { | |
1927 | printf ("duplicate request for %d ignored.\n", num); | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | args = arg1; | |
1932 | while (*args == ' ' || *args == '\t') args++; | |
1933 | } | |
1934 | return_values.nelts = i; | |
1935 | return return_values; | |
1936 | } | |
1937 | ||
bd5635a1 RP |
1938 | \f |
1939 | /* Slave routine for sources_info. Force line breaks at ,'s. | |
1940 | NAME is the name to print and *FIRST is nonzero if this is the first | |
1941 | name printed. Set *FIRST to zero. */ | |
1942 | static void | |
1943 | output_source_filename (name, first) | |
1944 | char *name; | |
1945 | int *first; | |
1946 | { | |
cba0d141 | 1947 | static unsigned int column; |
bd5635a1 RP |
1948 | /* Table of files printed so far. Since a single source file can |
1949 | result in several partial symbol tables, we need to avoid printing | |
1950 | it more than once. Note: if some of the psymtabs are read in and | |
1951 | some are not, it gets printed both under "Source files for which | |
1952 | symbols have been read" and "Source files for which symbols will | |
1953 | be read in on demand". I consider this a reasonable way to deal | |
1954 | with the situation. I'm not sure whether this can also happen for | |
1955 | symtabs; it doesn't hurt to check. */ | |
1956 | static char **tab = NULL; | |
1957 | /* Allocated size of tab in elements. | |
1958 | Start with one 256-byte block (when using GNU malloc.c). | |
1959 | 24 is the malloc overhead when range checking is in effect. */ | |
1960 | static int tab_alloc_size = (256 - 24) / sizeof (char *); | |
1961 | /* Current size of tab in elements. */ | |
1962 | static int tab_cur_size; | |
1963 | ||
1964 | char **p; | |
1965 | ||
1966 | if (*first) | |
1967 | { | |
1968 | if (tab == NULL) | |
1969 | tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab)); | |
1970 | tab_cur_size = 0; | |
1971 | } | |
1972 | ||
1973 | /* Is NAME in tab? */ | |
1974 | for (p = tab; p < tab + tab_cur_size; p++) | |
1975 | if (strcmp (*p, name) == 0) | |
1976 | /* Yes; don't print it again. */ | |
1977 | return; | |
1978 | /* No; add it to tab. */ | |
1979 | if (tab_cur_size == tab_alloc_size) | |
1980 | { | |
1981 | tab_alloc_size *= 2; | |
cba0d141 | 1982 | tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab)); |
bd5635a1 RP |
1983 | } |
1984 | tab[tab_cur_size++] = name; | |
1985 | ||
1986 | if (*first) | |
1987 | { | |
1988 | column = 0; | |
1989 | *first = 0; | |
1990 | } | |
1991 | else | |
1992 | { | |
1993 | printf_filtered (","); | |
1994 | column++; | |
1995 | } | |
1996 | ||
1997 | if (column != 0 && column + strlen (name) >= 70) | |
1998 | { | |
1999 | printf_filtered ("\n"); | |
2000 | column = 0; | |
2001 | } | |
2002 | else if (column != 0) | |
2003 | { | |
2004 | printf_filtered (" "); | |
2005 | column++; | |
2006 | } | |
2007 | fputs_filtered (name, stdout); | |
2008 | column += strlen (name); | |
2009 | } | |
2010 | ||
2011 | static void | |
2012 | sources_info () | |
2013 | { | |
2014 | register struct symtab *s; | |
2015 | register struct partial_symtab *ps; | |
cba0d141 | 2016 | register struct objfile *objfile; |
bd5635a1 RP |
2017 | int first; |
2018 | ||
cba0d141 | 2019 | if (!have_full_symbols () && !have_partial_symbols ()) |
bd5635a1 | 2020 | { |
3053b9f2 | 2021 | error (no_symtab_msg); |
bd5635a1 RP |
2022 | } |
2023 | ||
2024 | printf_filtered ("Source files for which symbols have been read in:\n\n"); | |
2025 | ||
2026 | first = 1; | |
cba0d141 JG |
2027 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
2028 | { | |
2029 | for (s = objfile -> symtabs; s != NULL; s = s -> next) | |
2030 | { | |
2031 | output_source_filename (s -> filename, &first); | |
2032 | } | |
2033 | } | |
bd5635a1 RP |
2034 | printf_filtered ("\n\n"); |
2035 | ||
2036 | printf_filtered ("Source files for which symbols will be read in on demand:\n\n"); | |
2037 | ||
2038 | first = 1; | |
cba0d141 JG |
2039 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
2040 | { | |
2041 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps -> next) | |
2042 | { | |
2043 | if (!ps->readin) | |
2044 | { | |
2045 | output_source_filename (ps -> filename, &first); | |
2046 | } | |
2047 | } | |
2048 | } | |
bd5635a1 RP |
2049 | printf_filtered ("\n"); |
2050 | } | |
2051 | ||
2cd99985 PB |
2052 | static int |
2053 | name_match(name) | |
2054 | char *name; | |
2055 | { | |
2056 | char *demangled = cplus_demangle(name, -1); | |
2057 | if (demangled != NULL) | |
2058 | { | |
2059 | int cond = re_exec (demangled); | |
2060 | free (demangled); | |
2061 | return cond; | |
2062 | } | |
2063 | return re_exec(name); | |
2064 | } | |
2065 | #define NAME_MATCH(NAME) name_match(NAME) | |
2066 | ||
bd5635a1 RP |
2067 | /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP. |
2068 | If CLASS is zero, list all symbols except functions and type names. | |
2069 | If CLASS is 1, list only functions. | |
2070 | If CLASS is 2, list only type names. | |
997a978c | 2071 | If CLASS is 3, list only method names. |
bd5635a1 RP |
2072 | |
2073 | BPT is non-zero if we should set a breakpoint at the functions | |
2074 | we find. */ | |
2075 | ||
2076 | static void | |
2077 | list_symbols (regexp, class, bpt) | |
2078 | char *regexp; | |
2079 | int class; | |
2080 | int bpt; | |
2081 | { | |
2082 | register struct symtab *s; | |
2083 | register struct partial_symtab *ps; | |
2084 | register struct blockvector *bv; | |
2085 | struct blockvector *prev_bv = 0; | |
2086 | register struct block *b; | |
2087 | register int i, j; | |
2088 | register struct symbol *sym; | |
2089 | struct partial_symbol *psym; | |
cba0d141 JG |
2090 | struct objfile *objfile; |
2091 | struct minimal_symbol *msymbol; | |
2cd99985 PB |
2092 | char *val, *q2; |
2093 | /* char *mangled;*/ | |
bd5635a1 RP |
2094 | static char *classnames[] |
2095 | = {"variable", "function", "type", "method"}; | |
2096 | int found_in_file = 0; | |
997a978c | 2097 | int found_misc = 0; |
cba0d141 JG |
2098 | static enum minimal_symbol_type types[] |
2099 | = {mst_data, mst_text, mst_abs, mst_unknown}; | |
2100 | static enum minimal_symbol_type types2[] | |
2101 | = {mst_bss, mst_text, mst_abs, mst_unknown}; | |
2102 | enum minimal_symbol_type ourtype = types[class]; | |
2103 | enum minimal_symbol_type ourtype2 = types2[class]; | |
bd5635a1 | 2104 | |
2cd99985 | 2105 | |
bd5635a1 | 2106 | if (regexp) |
2cd99985 PB |
2107 | { |
2108 | /* Make sure spacing is right for C++ operators. | |
2109 | This is just a courtesy to make the matching less sensitive | |
2110 | to how many spaces the user leaves between 'operator' | |
2111 | and <TYPENAME> or <OPERATOR>. */ | |
2112 | char *opend; | |
2113 | char *opname = operator_chars (regexp, &opend); | |
2114 | if (*opname) | |
2115 | { | |
2116 | int fix = -1; /* -1 means ok; otherwise number of spaces needed. */ | |
2117 | if (isalpha(*opname) || *opname == '_' || *opname == '$') | |
2118 | { | |
2119 | /* There should 1 space between 'operator' and 'TYPENAME'. */ | |
2120 | if (opname[-1] != ' ' || opname[-2] == ' ') | |
2121 | fix = 1; | |
2122 | } | |
2123 | else | |
2124 | { | |
2125 | /* There should 0 spaces between 'operator' and 'OPERATOR'. */ | |
2126 | if (opname[-1] == ' ') | |
2127 | fix = 0; | |
2128 | } | |
2129 | /* If wrong number of spaces, fix it. */ | |
2130 | if (fix >= 0) | |
2131 | { | |
2132 | char *tmp = (char*) alloca(opend-opname+10); | |
2133 | sprintf(tmp, "operator%.*s%s", fix, " ", opname); | |
2134 | regexp = tmp; | |
2135 | } | |
2136 | } | |
2137 | ||
2138 | if (0 != (val = re_comp (regexp))) | |
2139 | error ("Invalid regexp (%s): %s", val, regexp); | |
2140 | } | |
bd5635a1 | 2141 | |
cba0d141 | 2142 | /* Search through the partial symtabs *first* for all symbols |
bd5635a1 RP |
2143 | matching the regexp. That way we don't have to reproduce all of |
2144 | the machinery below. */ | |
bd5635a1 | 2145 | |
cba0d141 JG |
2146 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
2147 | { | |
2148 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps -> next) | |
bd5635a1 | 2149 | { |
cba0d141 JG |
2150 | struct partial_symbol *bound, *gbound, *sbound; |
2151 | int keep_going = 1; | |
2152 | ||
2153 | if (ps->readin) continue; | |
2154 | ||
2155 | gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms; | |
2156 | sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms; | |
2157 | bound = gbound; | |
2158 | ||
2159 | /* Go through all of the symbols stored in a partial | |
2160 | symtab in one loop. */ | |
2161 | psym = objfile->global_psymbols.list + ps->globals_offset; | |
2162 | while (keep_going) | |
bd5635a1 | 2163 | { |
cba0d141 | 2164 | if (psym >= bound) |
bd5635a1 | 2165 | { |
cba0d141 JG |
2166 | if (bound == gbound && ps->n_static_syms != 0) |
2167 | { | |
2168 | psym = objfile->static_psymbols.list + ps->statics_offset; | |
2169 | bound = sbound; | |
2170 | } | |
2171 | else | |
2172 | keep_going = 0; | |
2173 | continue; | |
bd5635a1 RP |
2174 | } |
2175 | else | |
bd5635a1 | 2176 | { |
cba0d141 | 2177 | QUIT; |
2cd99985 | 2178 | |
cba0d141 JG |
2179 | /* If it would match (logic taken from loop below) |
2180 | load the file and go on to the next one */ | |
2cd99985 | 2181 | if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (psym))) |
cba0d141 JG |
2182 | && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF |
2183 | && SYMBOL_CLASS (psym) != LOC_BLOCK) | |
2184 | || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK) | |
2185 | || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF) | |
2186 | || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK))) | |
2187 | { | |
2188 | (void) PSYMTAB_TO_SYMTAB(ps); | |
2189 | keep_going = 0; | |
2190 | } | |
bd5635a1 | 2191 | } |
cba0d141 | 2192 | psym++; |
bd5635a1 | 2193 | } |
bd5635a1 RP |
2194 | } |
2195 | } | |
2196 | ||
cba0d141 | 2197 | /* Here, we search through the minimal symbol tables for functions that |
bd5635a1 RP |
2198 | match, and call find_pc_symtab on them to force their symbols to |
2199 | be read. The symbol will then be found during the scan of symtabs | |
997a978c JG |
2200 | below. If find_pc_symtab fails, set found_misc so that we will |
2201 | rescan to print any matching symbols without debug info. */ | |
2202 | ||
cba0d141 JG |
2203 | if (class == 1) |
2204 | { | |
2205 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) | |
2206 | { | |
2207 | for (msymbol = objfile -> msymbols; | |
2208 | msymbol -> name != NULL; msymbol++) | |
2209 | { | |
2210 | if (msymbol -> type == ourtype || msymbol -> type == ourtype2) | |
2211 | { | |
2cd99985 | 2212 | if (regexp == 0 || NAME_MATCH (msymbol -> name)) |
cba0d141 JG |
2213 | { |
2214 | if (0 == find_pc_symtab (msymbol -> address)) | |
2215 | { | |
2216 | found_misc = 1; | |
2217 | } | |
2218 | } | |
2219 | } | |
2220 | } | |
2221 | } | |
bd5635a1 RP |
2222 | } |
2223 | ||
2224 | /* Printout here so as to get after the "Reading in symbols" | |
2225 | messages which will be generated above. */ | |
2226 | if (!bpt) | |
2227 | printf_filtered (regexp | |
2228 | ? "All %ss matching regular expression \"%s\":\n" | |
2229 | : "All defined %ss:\n", | |
2230 | classnames[class], | |
2231 | regexp); | |
2232 | ||
cba0d141 | 2233 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 2234 | { |
cba0d141 JG |
2235 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
2236 | { | |
2237 | found_in_file = 0; | |
2238 | bv = BLOCKVECTOR (s); | |
2239 | /* Often many files share a blockvector. | |
2240 | Scan each blockvector only once so that | |
2241 | we don't get every symbol many times. | |
2242 | It happens that the first symtab in the list | |
2243 | for any given blockvector is the main file. */ | |
2244 | if (bv != prev_bv) | |
2245 | for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++) | |
bd5635a1 | 2246 | { |
cba0d141 JG |
2247 | b = BLOCKVECTOR_BLOCK (bv, i); |
2248 | /* Skip the sort if this block is always sorted. */ | |
2249 | if (!BLOCK_SHOULD_SORT (b)) | |
2250 | sort_block_syms (b); | |
2251 | for (j = 0; j < BLOCK_NSYMS (b); j++) | |
bd5635a1 | 2252 | { |
cba0d141 JG |
2253 | QUIT; |
2254 | sym = BLOCK_SYM (b, j); | |
2cd99985 | 2255 | if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (sym))) |
cba0d141 JG |
2256 | && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF |
2257 | && SYMBOL_CLASS (sym) != LOC_BLOCK) | |
2258 | || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK) | |
2259 | || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
2260 | || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK))) | |
bd5635a1 | 2261 | { |
cba0d141 JG |
2262 | if (bpt) |
2263 | { | |
2264 | /* Set a breakpoint here, if it's a function */ | |
2265 | if (class == 1) | |
2266 | break_command (SYMBOL_NAME(sym), 0); | |
2267 | } | |
2268 | else if (!found_in_file) | |
2269 | { | |
2270 | fputs_filtered ("\nFile ", stdout); | |
2271 | fputs_filtered (s->filename, stdout); | |
2272 | fputs_filtered (":\n", stdout); | |
2273 | } | |
2274 | found_in_file = 1; | |
2275 | ||
2276 | if (class != 2 && i == STATIC_BLOCK) | |
2277 | printf_filtered ("static "); | |
2278 | ||
2279 | /* Typedef that is not a C++ class */ | |
2280 | if (class == 2 | |
2281 | && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE) | |
2282 | typedef_print (SYMBOL_TYPE(sym), sym, stdout); | |
2283 | /* variable, func, or typedef-that-is-c++-class */ | |
2284 | else if (class < 2 || | |
2285 | (class == 2 && | |
2286 | SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE)) | |
2287 | { | |
2288 | type_print (SYMBOL_TYPE (sym), | |
2289 | (SYMBOL_CLASS (sym) == LOC_TYPEDEF | |
2290 | ? "" : SYMBOL_NAME (sym)), | |
2291 | stdout, 0); | |
2292 | ||
2293 | printf_filtered (";\n"); | |
2294 | } | |
2295 | else | |
2296 | { | |
bd5635a1 | 2297 | # if 0 |
cba0d141 JG |
2298 | char buf[1024]; |
2299 | type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0); | |
2300 | type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0); | |
2301 | sprintf (buf, " %s::", type_name_no_tag (t)); | |
2302 | type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout); | |
bd5635a1 | 2303 | # endif |
cba0d141 | 2304 | } |
bd5635a1 RP |
2305 | } |
2306 | } | |
2307 | } | |
cba0d141 JG |
2308 | prev_bv = bv; |
2309 | } | |
bd5635a1 | 2310 | } |
997a978c | 2311 | |
997a978c | 2312 | /* If there are no eyes, avoid all contact. I mean, if there are |
cba0d141 JG |
2313 | no debug symbols, then print directly from the msymbol_vector. */ |
2314 | ||
2315 | if (found_misc || class != 1) | |
2316 | { | |
2317 | found_in_file = 0; | |
2318 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) | |
2319 | { | |
2320 | for (msymbol = objfile -> msymbols; | |
2321 | msymbol -> name != NULL; msymbol++) | |
2322 | { | |
2323 | if (msymbol -> type == ourtype || msymbol -> type == ourtype2) | |
2324 | { | |
2cd99985 | 2325 | if (regexp == 0 || NAME_MATCH (msymbol -> name)) |
cba0d141 JG |
2326 | { |
2327 | /* Functions: Look up by address. */ | |
2328 | if (class != 1 && | |
2329 | (find_pc_symtab (msymbol -> address) != NULL)) | |
2330 | { | |
2331 | /* Variables/Absolutes: Look up by name */ | |
2332 | if (lookup_symbol (msymbol -> name, | |
2333 | (struct block *) 0, VAR_NAMESPACE, 0, | |
2334 | (struct symtab **) 0) == NULL) | |
2335 | { | |
2336 | if (!found_in_file) | |
2337 | { | |
2338 | printf_filtered ("\nNon-debugging symbols:\n"); | |
2339 | found_in_file = 1; | |
2340 | } | |
2341 | printf_filtered (" %08x %s\n", | |
2342 | msymbol -> address, | |
2343 | msymbol -> name); | |
2344 | } | |
2345 | } | |
2346 | } | |
2347 | } | |
2348 | } | |
997a978c | 2349 | } |
997a978c | 2350 | } |
bd5635a1 RP |
2351 | } |
2352 | ||
2353 | static void | |
2354 | variables_info (regexp) | |
2355 | char *regexp; | |
2356 | { | |
2357 | list_symbols (regexp, 0, 0); | |
2358 | } | |
2359 | ||
2360 | static void | |
2361 | functions_info (regexp) | |
2362 | char *regexp; | |
2363 | { | |
2364 | list_symbols (regexp, 1, 0); | |
2365 | } | |
2366 | ||
bd5635a1 RP |
2367 | static void |
2368 | types_info (regexp) | |
2369 | char *regexp; | |
2370 | { | |
2371 | list_symbols (regexp, 2, 0); | |
2372 | } | |
bd5635a1 RP |
2373 | |
2374 | #if 0 | |
2375 | /* Tiemann says: "info methods was never implemented." */ | |
2376 | static void | |
2377 | methods_info (regexp) | |
2378 | char *regexp; | |
2379 | { | |
2380 | list_symbols (regexp, 3, 0); | |
2381 | } | |
2382 | #endif /* 0 */ | |
2383 | ||
2384 | /* Breakpoint all functions matching regular expression. */ | |
2385 | static void | |
2386 | rbreak_command (regexp) | |
2387 | char *regexp; | |
2388 | { | |
2389 | list_symbols (regexp, 1, 1); | |
2390 | } | |
2391 | \f | |
bd5635a1 RP |
2392 | |
2393 | /* Return Nonzero if block a is lexically nested within block b, | |
2394 | or if a and b have the same pc range. | |
2395 | Return zero otherwise. */ | |
2396 | int | |
2397 | contained_in (a, b) | |
2398 | struct block *a, *b; | |
2399 | { | |
2400 | if (!a || !b) | |
2401 | return 0; | |
2402 | return BLOCK_START (a) >= BLOCK_START (b) | |
2403 | && BLOCK_END (a) <= BLOCK_END (b); | |
2404 | } | |
2405 | ||
2406 | \f | |
2407 | /* Helper routine for make_symbol_completion_list. */ | |
2408 | ||
2409 | int return_val_size, return_val_index; | |
2410 | char **return_val; | |
2411 | ||
cba0d141 | 2412 | static void |
bd5635a1 RP |
2413 | completion_list_add_symbol (symname) |
2414 | char *symname; | |
2415 | { | |
2416 | if (return_val_index + 3 > return_val_size) | |
cba0d141 JG |
2417 | return_val = (char **) xrealloc ((char *) return_val, |
2418 | (return_val_size *= 2) * sizeof (char *)); | |
bd5635a1 RP |
2419 | |
2420 | return_val[return_val_index] = | |
2421 | (char *)xmalloc (1 + strlen (symname)); | |
2422 | ||
2423 | strcpy (return_val[return_val_index], symname); | |
2424 | ||
2425 | return_val[++return_val_index] = (char *)NULL; | |
2426 | } | |
2427 | ||
2428 | /* Return a NULL terminated array of all symbols (regardless of class) which | |
2429 | begin by matching TEXT. If the answer is no symbols, then the return value | |
2430 | is an array which contains only a NULL pointer. | |
2431 | ||
2432 | Problem: All of the symbols have to be copied because readline | |
2433 | frees them. I'm not going to worry about this; hopefully there | |
2434 | won't be that many. */ | |
2435 | ||
2436 | char ** | |
2437 | make_symbol_completion_list (text) | |
2438 | char *text; | |
2439 | { | |
2440 | register struct symtab *s; | |
2441 | register struct partial_symtab *ps; | |
cba0d141 JG |
2442 | register struct minimal_symbol *msymbol; |
2443 | register struct objfile *objfile; | |
bd5635a1 | 2444 | register struct block *b, *surrounding_static_block = 0; |
bd5635a1 RP |
2445 | register int i, j; |
2446 | struct partial_symbol *psym; | |
2447 | ||
2448 | int text_len = strlen (text); | |
2449 | return_val_size = 100; | |
2450 | return_val_index = 0; | |
2451 | return_val = | |
2452 | (char **)xmalloc ((1 + return_val_size) *sizeof (char *)); | |
2453 | return_val[0] = (char *)NULL; | |
2454 | ||
2455 | /* Look through the partial symtabs for all symbols which begin | |
2456 | by matching TEXT. Add each one that you find to the list. */ | |
2457 | ||
cba0d141 | 2458 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 2459 | { |
cba0d141 | 2460 | for (ps = objfile -> psymtabs; ps != NULL; ps = ps -> next) |
bd5635a1 | 2461 | { |
cba0d141 JG |
2462 | /* If the psymtab's been read in we'll get it when we search |
2463 | through the blockvector. */ | |
2464 | if (ps->readin) continue; | |
2465 | ||
2466 | for (psym = objfile->global_psymbols.list + ps->globals_offset; | |
2467 | psym < (objfile->global_psymbols.list + ps->globals_offset | |
2468 | + ps->n_global_syms); | |
2469 | psym++) | |
2470 | { | |
2471 | QUIT; /* If interrupted, then quit. */ | |
2472 | if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0)) | |
2473 | completion_list_add_symbol (SYMBOL_NAME (psym)); | |
2474 | } | |
2475 | ||
2476 | for (psym = objfile->static_psymbols.list + ps->statics_offset; | |
2477 | psym < (objfile->static_psymbols.list + ps->statics_offset | |
2478 | + ps->n_static_syms); | |
2479 | psym++) | |
2480 | { | |
2481 | QUIT; | |
2482 | if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0)) | |
2483 | completion_list_add_symbol (SYMBOL_NAME (psym)); | |
2484 | } | |
bd5635a1 RP |
2485 | } |
2486 | } | |
2487 | ||
cba0d141 | 2488 | /* At this point scan through the misc symbol vectors and add each |
bd5635a1 RP |
2489 | symbol you find to the list. Eventually we want to ignore |
2490 | anything that isn't a text symbol (everything else will be | |
2491 | handled by the psymtab code above). */ | |
2492 | ||
cba0d141 JG |
2493 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
2494 | { | |
2495 | for (msymbol = objfile -> msymbols; | |
2496 | msymbol ->name != NULL; msymbol++) | |
2497 | { | |
2498 | if (strncmp (text, msymbol -> name, text_len) == 0) | |
2499 | { | |
2500 | completion_list_add_symbol (msymbol -> name); | |
2501 | } | |
2502 | } | |
2503 | } | |
bd5635a1 RP |
2504 | |
2505 | /* Search upwards from currently selected frame (so that we can | |
2506 | complete on local vars. */ | |
2507 | for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b)) | |
2508 | { | |
2509 | if (!BLOCK_SUPERBLOCK (b)) | |
2510 | surrounding_static_block = b; /* For elmin of dups */ | |
2511 | ||
2512 | /* Also catch fields of types defined in this places which | |
2513 | match our text string. Only complete on types visible | |
2514 | from current context. */ | |
2515 | for (i = 0; i < BLOCK_NSYMS (b); i++) | |
2516 | { | |
2517 | register struct symbol *sym = BLOCK_SYM (b, i); | |
2518 | ||
2519 | if (!strncmp (SYMBOL_NAME (sym), text, text_len)) | |
2520 | completion_list_add_symbol (SYMBOL_NAME (sym)); | |
2521 | ||
2522 | if (SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
2523 | { | |
2524 | struct type *t = SYMBOL_TYPE (sym); | |
2525 | enum type_code c = TYPE_CODE (t); | |
2526 | ||
2527 | if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT) | |
2528 | for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++) | |
2529 | if (TYPE_FIELD_NAME (t, j) && | |
2530 | !strncmp (TYPE_FIELD_NAME (t, j), text, text_len)) | |
2531 | completion_list_add_symbol (TYPE_FIELD_NAME (t, j)); | |
2532 | } | |
2533 | } | |
2534 | } | |
2535 | ||
2536 | /* Go through the symtabs and check the externs and statics for | |
2537 | symbols which match. */ | |
2538 | ||
cba0d141 | 2539 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 2540 | { |
cba0d141 JG |
2541 | for (s = objfile ->symtabs; s != NULL; s = s -> next) |
2542 | { | |
2543 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); | |
2544 | ||
2545 | for (i = 0; i < BLOCK_NSYMS (b); i++) | |
2546 | if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len)) | |
2547 | completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i))); | |
2548 | } | |
bd5635a1 RP |
2549 | } |
2550 | ||
cba0d141 | 2551 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 2552 | { |
cba0d141 JG |
2553 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
2554 | { | |
2555 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); | |
2556 | ||
2557 | /* Don't do this block twice. */ | |
2558 | if (b == surrounding_static_block) continue; | |
2559 | ||
2560 | for (i = 0; i < BLOCK_NSYMS (b); i++) | |
2561 | if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len)) | |
2562 | completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i))); | |
2563 | } | |
bd5635a1 RP |
2564 | } |
2565 | ||
2566 | return (return_val); | |
2567 | } | |
2568 | \f | |
997a978c JG |
2569 | #if 0 |
2570 | /* Add the type of the symbol sym to the type of the current | |
2571 | function whose block we are in (assumed). The type of | |
2572 | this current function is contained in *TYPE. | |
2573 | ||
2574 | This basically works as follows: When we find a function | |
2575 | symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record | |
2576 | a pointer to its type in the global in_function_type. Every | |
2577 | time we come across a parameter symbol ('p' in its name), then | |
2578 | this procedure adds the name and type of that parameter | |
2579 | to the function type pointed to by *TYPE. (Which should correspond | |
2580 | to in_function_type if it was called correctly). | |
2581 | ||
2582 | Note that since we are modifying a type, the result of | |
2583 | lookup_function_type() should be bcopy()ed before calling | |
2584 | this. When not in strict typing mode, the expression | |
2585 | evaluator can choose to ignore this. | |
2586 | ||
2587 | Assumption: All of a function's parameter symbols will | |
2588 | appear before another function symbol is found. The parameters | |
2589 | appear in the same order in the argument list as they do in the | |
2590 | symbol table. */ | |
2591 | ||
2592 | void | |
2593 | add_param_to_type (type,sym) | |
2594 | struct type **type; | |
2595 | struct symbol *sym; | |
2596 | { | |
2597 | int num = ++(TYPE_NFIELDS(*type)); | |
2598 | ||
2599 | if(TYPE_NFIELDS(*type)-1) | |
cba0d141 JG |
2600 | TYPE_FIELDS(*type) = (struct field *) |
2601 | (*current_objfile->xrealloc) ((char *)(TYPE_FIELDS(*type)), | |
2602 | num*sizeof(struct field)); | |
997a978c | 2603 | else |
cba0d141 JG |
2604 | TYPE_FIELDS(*type) = (struct field *) |
2605 | (*current_objfile->xmalloc) (num*sizeof(struct field)); | |
997a978c JG |
2606 | |
2607 | TYPE_FIELD_BITPOS(*type,num-1) = num-1; | |
2608 | TYPE_FIELD_BITSIZE(*type,num-1) = 0; | |
2609 | TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym); | |
2610 | TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym); | |
2611 | } | |
2612 | #endif | |
2613 | \f | |
bd5635a1 RP |
2614 | void |
2615 | _initialize_symtab () | |
2616 | { | |
2617 | add_info ("variables", variables_info, | |
2618 | "All global and static variable names, or those matching REGEXP."); | |
2619 | add_info ("functions", functions_info, | |
2620 | "All function names, or those matching REGEXP."); | |
3ba6a043 JG |
2621 | |
2622 | /* FIXME: This command has at least the following problems: | |
bd5635a1 RP |
2623 | 1. It prints builtin types (in a very strange and confusing fashion). |
2624 | 2. It doesn't print right, e.g. with | |
2625 | typedef struct foo *FOO | |
2626 | type_print prints "FOO" when we want to make it (in this situation) | |
2627 | print "struct foo *". | |
2628 | I also think "ptype" or "whatis" is more likely to be useful (but if | |
2629 | there is much disagreement "info types" can be fixed). */ | |
2630 | add_info ("types", types_info, | |
a0a6174a | 2631 | "All type names, or those matching REGEXP."); |
3ba6a043 | 2632 | |
bd5635a1 RP |
2633 | #if 0 |
2634 | add_info ("methods", methods_info, | |
2635 | "All method names, or those matching REGEXP::REGEXP.\n\ | |
2636 | If the class qualifier is ommited, it is assumed to be the current scope.\n\ | |
cba0d141 | 2637 | If the first REGEXP is omitted, then all methods matching the second REGEXP\n\ |
bd5635a1 RP |
2638 | are listed."); |
2639 | #endif | |
2640 | add_info ("sources", sources_info, | |
2641 | "Source files in the program."); | |
2642 | ||
2643 | add_com ("rbreak", no_class, rbreak_command, | |
2644 | "Set a breakpoint for all functions matching REGEXP."); | |
2645 | ||
997a978c | 2646 | /* Initialize the one built-in type that isn't language dependent... */ |
cba0d141 JG |
2647 | builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0, |
2648 | "<unknown type>", (struct objfile *) NULL); | |
bd5635a1 | 2649 | } |