690cda1ff55e4b027554644d665a48d3fd0ae173
[platform/upstream/binutils.git] / gdb / symtab.c
1 /* Symbol table lookup for the GNU debugger, GDB.
2
3    Copyright (C) 1986-2013 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "gdbcore.h"
24 #include "frame.h"
25 #include "target.h"
26 #include "value.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "gdbcmd.h"
30 #include "gdb_regex.h"
31 #include "expression.h"
32 #include "language.h"
33 #include "demangle.h"
34 #include "inferior.h"
35 #include "source.h"
36 #include "filenames.h"          /* for FILENAME_CMP */
37 #include "objc-lang.h"
38 #include "d-lang.h"
39 #include "ada-lang.h"
40 #include "go-lang.h"
41 #include "p-lang.h"
42 #include "addrmap.h"
43
44 #include "hashtab.h"
45
46 #include "gdb_obstack.h"
47 #include "block.h"
48 #include "dictionary.h"
49
50 #include <sys/types.h>
51 #include <fcntl.h>
52 #include "gdb_string.h"
53 #include "gdb_stat.h"
54 #include <ctype.h>
55 #include "cp-abi.h"
56 #include "cp-support.h"
57 #include "observer.h"
58 #include "gdb_assert.h"
59 #include "solist.h"
60 #include "macrotab.h"
61 #include "macroscope.h"
62
63 #include "psymtab.h"
64 #include "parser-defs.h"
65
66 /* Prototypes for local functions */
67
68 static void rbreak_command (char *, int);
69
70 static void types_info (char *, int);
71
72 static void functions_info (char *, int);
73
74 static void variables_info (char *, int);
75
76 static void sources_info (char *, int);
77
78 static int find_line_common (struct linetable *, int, int *, int);
79
80 static struct symbol *lookup_symbol_aux (const char *name,
81                                          const struct block *block,
82                                          const domain_enum domain,
83                                          enum language language,
84                                          struct field_of_this_result *is_a_field_of_this);
85
86 static
87 struct symbol *lookup_symbol_aux_local (const char *name,
88                                         const struct block *block,
89                                         const domain_enum domain,
90                                         enum language language);
91
92 static
93 struct symbol *lookup_symbol_aux_symtabs (int block_index,
94                                           const char *name,
95                                           const domain_enum domain);
96
97 static
98 struct symbol *lookup_symbol_aux_quick (struct objfile *objfile,
99                                         int block_index,
100                                         const char *name,
101                                         const domain_enum domain);
102
103 static void print_msymbol_info (struct minimal_symbol *);
104
105 void _initialize_symtab (void);
106
107 /* */
108
109 /* When non-zero, print debugging messages related to symtab creation.  */
110 int symtab_create_debug = 0;
111
112 /* Non-zero if a file may be known by two different basenames.
113    This is the uncommon case, and significantly slows down gdb.
114    Default set to "off" to not slow down the common case.  */
115 int basenames_may_differ = 0;
116
117 /* Allow the user to configure the debugger behavior with respect
118    to multiple-choice menus when more than one symbol matches during
119    a symbol lookup.  */
120
121 const char multiple_symbols_ask[] = "ask";
122 const char multiple_symbols_all[] = "all";
123 const char multiple_symbols_cancel[] = "cancel";
124 static const char *const multiple_symbols_modes[] =
125 {
126   multiple_symbols_ask,
127   multiple_symbols_all,
128   multiple_symbols_cancel,
129   NULL
130 };
131 static const char *multiple_symbols_mode = multiple_symbols_all;
132
133 /* Read-only accessor to AUTO_SELECT_MODE.  */
134
135 const char *
136 multiple_symbols_select_mode (void)
137 {
138   return multiple_symbols_mode;
139 }
140
141 /* Block in which the most recently searched-for symbol was found.
142    Might be better to make this a parameter to lookup_symbol and
143    value_of_this.  */
144
145 const struct block *block_found;
146
147 /* See whether FILENAME matches SEARCH_NAME using the rule that we
148    advertise to the user.  (The manual's description of linespecs
149    describes what we advertise).  Returns true if they match, false
150    otherwise.  */
151
152 int
153 compare_filenames_for_search (const char *filename, const char *search_name)
154 {
155   int len = strlen (filename);
156   size_t search_len = strlen (search_name);
157
158   if (len < search_len)
159     return 0;
160
161   /* The tail of FILENAME must match.  */
162   if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
163     return 0;
164
165   /* Either the names must completely match, or the character
166      preceding the trailing SEARCH_NAME segment of FILENAME must be a
167      directory separator.
168
169      The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
170      cannot match FILENAME "/path//dir/file.c" - as user has requested
171      absolute path.  The sama applies for "c:\file.c" possibly
172      incorrectly hypothetically matching "d:\dir\c:\file.c".
173
174      The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
175      compatible with SEARCH_NAME "file.c".  In such case a compiler had
176      to put the "c:file.c" name into debug info.  Such compatibility
177      works only on GDB built for DOS host.  */
178   return (len == search_len
179           || (!IS_ABSOLUTE_PATH (search_name)
180               && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
181           || (HAS_DRIVE_SPEC (filename)
182               && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
183 }
184
185 /* Check for a symtab of a specific name by searching some symtabs.
186    This is a helper function for callbacks of iterate_over_symtabs.
187
188    The return value, NAME, REAL_PATH, CALLBACK, and DATA
189    are identical to the `map_symtabs_matching_filename' method of
190    quick_symbol_functions.
191
192    FIRST and AFTER_LAST indicate the range of symtabs to search.
193    AFTER_LAST is one past the last symtab to search; NULL means to
194    search until the end of the list.  */
195
196 int
197 iterate_over_some_symtabs (const char *name,
198                            const char *real_path,
199                            int (*callback) (struct symtab *symtab,
200                                             void *data),
201                            void *data,
202                            struct symtab *first,
203                            struct symtab *after_last)
204 {
205   struct symtab *s = NULL;
206   const char* base_name = lbasename (name);
207
208   for (s = first; s != NULL && s != after_last; s = s->next)
209     {
210       if (compare_filenames_for_search (s->filename, name))
211         {
212           if (callback (s, data))
213             return 1;
214         }
215
216     /* Before we invoke realpath, which can get expensive when many
217        files are involved, do a quick comparison of the basenames.  */
218     if (! basenames_may_differ
219         && FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
220       continue;
221
222     /* If the user gave us an absolute path, try to find the file in
223        this symtab and use its absolute path.  */
224
225     if (real_path != NULL)
226       {
227         const char *fullname = symtab_to_fullname (s);
228
229         gdb_assert (IS_ABSOLUTE_PATH (real_path));
230         gdb_assert (IS_ABSOLUTE_PATH (name));
231         if (FILENAME_CMP (real_path, fullname) == 0)
232           {
233             if (callback (s, data))
234               return 1;
235           }
236       }
237     }
238
239   return 0;
240 }
241
242 /* Check for a symtab of a specific name; first in symtabs, then in
243    psymtabs.  *If* there is no '/' in the name, a match after a '/'
244    in the symtab filename will also work.
245
246    Calls CALLBACK with each symtab that is found and with the supplied
247    DATA.  If CALLBACK returns true, the search stops.  */
248
249 void
250 iterate_over_symtabs (const char *name,
251                       int (*callback) (struct symtab *symtab,
252                                        void *data),
253                       void *data)
254 {
255   struct objfile *objfile;
256   char *real_path = NULL;
257   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
258
259   /* Here we are interested in canonicalizing an absolute path, not
260      absolutizing a relative path.  */
261   if (IS_ABSOLUTE_PATH (name))
262     {
263       real_path = gdb_realpath (name);
264       make_cleanup (xfree, real_path);
265       gdb_assert (IS_ABSOLUTE_PATH (real_path));
266     }
267
268   ALL_OBJFILES (objfile)
269   {
270     if (iterate_over_some_symtabs (name, real_path, callback, data,
271                                    objfile->symtabs, NULL))
272       {
273         do_cleanups (cleanups);
274         return;
275       }
276   }
277
278   /* Same search rules as above apply here, but now we look thru the
279      psymtabs.  */
280
281   ALL_OBJFILES (objfile)
282   {
283     if (objfile->sf
284         && objfile->sf->qf->map_symtabs_matching_filename (objfile,
285                                                            name,
286                                                            real_path,
287                                                            callback,
288                                                            data))
289       {
290         do_cleanups (cleanups);
291         return;
292       }
293   }
294
295   do_cleanups (cleanups);
296 }
297
298 /* The callback function used by lookup_symtab.  */
299
300 static int
301 lookup_symtab_callback (struct symtab *symtab, void *data)
302 {
303   struct symtab **result_ptr = data;
304
305   *result_ptr = symtab;
306   return 1;
307 }
308
309 /* A wrapper for iterate_over_symtabs that returns the first matching
310    symtab, or NULL.  */
311
312 struct symtab *
313 lookup_symtab (const char *name)
314 {
315   struct symtab *result = NULL;
316
317   iterate_over_symtabs (name, lookup_symtab_callback, &result);
318   return result;
319 }
320
321 \f
322 /* Mangle a GDB method stub type.  This actually reassembles the pieces of the
323    full method name, which consist of the class name (from T), the unadorned
324    method name from METHOD_ID, and the signature for the specific overload,
325    specified by SIGNATURE_ID.  Note that this function is g++ specific.  */
326
327 char *
328 gdb_mangle_name (struct type *type, int method_id, int signature_id)
329 {
330   int mangled_name_len;
331   char *mangled_name;
332   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
333   struct fn_field *method = &f[signature_id];
334   const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
335   const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
336   const char *newname = type_name_no_tag (type);
337
338   /* Does the form of physname indicate that it is the full mangled name
339      of a constructor (not just the args)?  */
340   int is_full_physname_constructor;
341
342   int is_constructor;
343   int is_destructor = is_destructor_name (physname);
344   /* Need a new type prefix.  */
345   char *const_prefix = method->is_const ? "C" : "";
346   char *volatile_prefix = method->is_volatile ? "V" : "";
347   char buf[20];
348   int len = (newname == NULL ? 0 : strlen (newname));
349
350   /* Nothing to do if physname already contains a fully mangled v3 abi name
351      or an operator name.  */
352   if ((physname[0] == '_' && physname[1] == 'Z')
353       || is_operator_name (field_name))
354     return xstrdup (physname);
355
356   is_full_physname_constructor = is_constructor_name (physname);
357
358   is_constructor = is_full_physname_constructor 
359     || (newname && strcmp (field_name, newname) == 0);
360
361   if (!is_destructor)
362     is_destructor = (strncmp (physname, "__dt", 4) == 0);
363
364   if (is_destructor || is_full_physname_constructor)
365     {
366       mangled_name = (char *) xmalloc (strlen (physname) + 1);
367       strcpy (mangled_name, physname);
368       return mangled_name;
369     }
370
371   if (len == 0)
372     {
373       xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
374     }
375   else if (physname[0] == 't' || physname[0] == 'Q')
376     {
377       /* The physname for template and qualified methods already includes
378          the class name.  */
379       xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
380       newname = NULL;
381       len = 0;
382     }
383   else
384     {
385       xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
386                  volatile_prefix, len);
387     }
388   mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
389                       + strlen (buf) + len + strlen (physname) + 1);
390
391   mangled_name = (char *) xmalloc (mangled_name_len);
392   if (is_constructor)
393     mangled_name[0] = '\0';
394   else
395     strcpy (mangled_name, field_name);
396
397   strcat (mangled_name, buf);
398   /* If the class doesn't have a name, i.e. newname NULL, then we just
399      mangle it using 0 for the length of the class.  Thus it gets mangled
400      as something starting with `::' rather than `classname::'.  */
401   if (newname != NULL)
402     strcat (mangled_name, newname);
403
404   strcat (mangled_name, physname);
405   return (mangled_name);
406 }
407
408 /* Initialize the cplus_specific structure.  'cplus_specific' should
409    only be allocated for use with cplus symbols.  */
410
411 static void
412 symbol_init_cplus_specific (struct general_symbol_info *gsymbol,
413                            struct objfile *objfile)
414 {
415   /* A language_specific structure should not have been previously
416      initialized.  */
417   gdb_assert (gsymbol->language_specific.cplus_specific == NULL);
418   gdb_assert (objfile != NULL);
419
420   gsymbol->language_specific.cplus_specific =
421       OBSTACK_ZALLOC (&objfile->objfile_obstack, struct cplus_specific);
422 }
423
424 /* Set the demangled name of GSYMBOL to NAME.  NAME must be already
425    correctly allocated.  For C++ symbols a cplus_specific struct is
426    allocated so OBJFILE must not be NULL.  If this is a non C++ symbol
427    OBJFILE can be NULL.  */
428
429 void
430 symbol_set_demangled_name (struct general_symbol_info *gsymbol,
431                            const char *name,
432                            struct objfile *objfile)
433 {
434   if (gsymbol->language == language_cplus)
435     {
436       if (gsymbol->language_specific.cplus_specific == NULL)
437         symbol_init_cplus_specific (gsymbol, objfile);
438
439       gsymbol->language_specific.cplus_specific->demangled_name = name;
440     }
441   else
442     gsymbol->language_specific.mangled_lang.demangled_name = name;
443 }
444
445 /* Return the demangled name of GSYMBOL.  */
446
447 const char *
448 symbol_get_demangled_name (const struct general_symbol_info *gsymbol)
449 {
450   if (gsymbol->language == language_cplus)
451     {
452       if (gsymbol->language_specific.cplus_specific != NULL)
453         return gsymbol->language_specific.cplus_specific->demangled_name;
454       else
455         return NULL;
456     }
457   else
458     return gsymbol->language_specific.mangled_lang.demangled_name;
459 }
460
461 \f
462 /* Initialize the language dependent portion of a symbol
463    depending upon the language for the symbol.  */
464
465 void
466 symbol_set_language (struct general_symbol_info *gsymbol,
467                      enum language language)
468 {
469   gsymbol->language = language;
470   if (gsymbol->language == language_d
471       || gsymbol->language == language_go
472       || gsymbol->language == language_java
473       || gsymbol->language == language_objc
474       || gsymbol->language == language_fortran)
475     {
476       symbol_set_demangled_name (gsymbol, NULL, NULL);
477     }
478   else if (gsymbol->language == language_cplus)
479     gsymbol->language_specific.cplus_specific = NULL;
480   else
481     {
482       memset (&gsymbol->language_specific, 0,
483               sizeof (gsymbol->language_specific));
484     }
485 }
486
487 /* Functions to initialize a symbol's mangled name.  */
488
489 /* Objects of this type are stored in the demangled name hash table.  */
490 struct demangled_name_entry
491 {
492   const char *mangled;
493   char demangled[1];
494 };
495
496 /* Hash function for the demangled name hash.  */
497
498 static hashval_t
499 hash_demangled_name_entry (const void *data)
500 {
501   const struct demangled_name_entry *e = data;
502
503   return htab_hash_string (e->mangled);
504 }
505
506 /* Equality function for the demangled name hash.  */
507
508 static int
509 eq_demangled_name_entry (const void *a, const void *b)
510 {
511   const struct demangled_name_entry *da = a;
512   const struct demangled_name_entry *db = b;
513
514   return strcmp (da->mangled, db->mangled) == 0;
515 }
516
517 /* Create the hash table used for demangled names.  Each hash entry is
518    a pair of strings; one for the mangled name and one for the demangled
519    name.  The entry is hashed via just the mangled name.  */
520
521 static void
522 create_demangled_names_hash (struct objfile *objfile)
523 {
524   /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
525      The hash table code will round this up to the next prime number.
526      Choosing a much larger table size wastes memory, and saves only about
527      1% in symbol reading.  */
528
529   objfile->demangled_names_hash = htab_create_alloc
530     (256, hash_demangled_name_entry, eq_demangled_name_entry,
531      NULL, xcalloc, xfree);
532 }
533
534 /* Try to determine the demangled name for a symbol, based on the
535    language of that symbol.  If the language is set to language_auto,
536    it will attempt to find any demangling algorithm that works and
537    then set the language appropriately.  The returned name is allocated
538    by the demangler and should be xfree'd.  */
539
540 static char *
541 symbol_find_demangled_name (struct general_symbol_info *gsymbol,
542                             const char *mangled)
543 {
544   char *demangled = NULL;
545
546   if (gsymbol->language == language_unknown)
547     gsymbol->language = language_auto;
548
549   if (gsymbol->language == language_objc
550       || gsymbol->language == language_auto)
551     {
552       demangled =
553         objc_demangle (mangled, 0);
554       if (demangled != NULL)
555         {
556           gsymbol->language = language_objc;
557           return demangled;
558         }
559     }
560   if (gsymbol->language == language_cplus
561       || gsymbol->language == language_auto)
562     {
563       demangled =
564         cplus_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
565       if (demangled != NULL)
566         {
567           gsymbol->language = language_cplus;
568           return demangled;
569         }
570     }
571   if (gsymbol->language == language_java)
572     {
573       demangled =
574         cplus_demangle (mangled,
575                         DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
576       if (demangled != NULL)
577         {
578           gsymbol->language = language_java;
579           return demangled;
580         }
581     }
582   if (gsymbol->language == language_d
583       || gsymbol->language == language_auto)
584     {
585       demangled = d_demangle(mangled, 0);
586       if (demangled != NULL)
587         {
588           gsymbol->language = language_d;
589           return demangled;
590         }
591     }
592   /* FIXME(dje): Continually adding languages here is clumsy.
593      Better to just call la_demangle if !auto, and if auto then call
594      a utility routine that tries successive languages in turn and reports
595      which one it finds.  I realize the la_demangle options may be different
596      for different languages but there's already a FIXME for that.  */
597   if (gsymbol->language == language_go
598       || gsymbol->language == language_auto)
599     {
600       demangled = go_demangle (mangled, 0);
601       if (demangled != NULL)
602         {
603           gsymbol->language = language_go;
604           return demangled;
605         }
606     }
607
608   /* We could support `gsymbol->language == language_fortran' here to provide
609      module namespaces also for inferiors with only minimal symbol table (ELF
610      symbols).  Just the mangling standard is not standardized across compilers
611      and there is no DW_AT_producer available for inferiors with only the ELF
612      symbols to check the mangling kind.  */
613   return NULL;
614 }
615
616 /* Set both the mangled and demangled (if any) names for GSYMBOL based
617    on LINKAGE_NAME and LEN.  Ordinarily, NAME is copied onto the
618    objfile's obstack; but if COPY_NAME is 0 and if NAME is
619    NUL-terminated, then this function assumes that NAME is already
620    correctly saved (either permanently or with a lifetime tied to the
621    objfile), and it will not be copied.
622
623    The hash table corresponding to OBJFILE is used, and the memory
624    comes from that objfile's objfile_obstack.  LINKAGE_NAME is copied,
625    so the pointer can be discarded after calling this function.  */
626
627 /* We have to be careful when dealing with Java names: when we run
628    into a Java minimal symbol, we don't know it's a Java symbol, so it
629    gets demangled as a C++ name.  This is unfortunate, but there's not
630    much we can do about it: but when demangling partial symbols and
631    regular symbols, we'd better not reuse the wrong demangled name.
632    (See PR gdb/1039.)  We solve this by putting a distinctive prefix
633    on Java names when storing them in the hash table.  */
634
635 /* FIXME: carlton/2003-03-13: This is an unfortunate situation.  I
636    don't mind the Java prefix so much: different languages have
637    different demangling requirements, so it's only natural that we
638    need to keep language data around in our demangling cache.  But
639    it's not good that the minimal symbol has the wrong demangled name.
640    Unfortunately, I can't think of any easy solution to that
641    problem.  */
642
643 #define JAVA_PREFIX "##JAVA$$"
644 #define JAVA_PREFIX_LEN 8
645
646 void
647 symbol_set_names (struct general_symbol_info *gsymbol,
648                   const char *linkage_name, int len, int copy_name,
649                   struct objfile *objfile)
650 {
651   struct demangled_name_entry **slot;
652   /* A 0-terminated copy of the linkage name.  */
653   const char *linkage_name_copy;
654   /* A copy of the linkage name that might have a special Java prefix
655      added to it, for use when looking names up in the hash table.  */
656   const char *lookup_name;
657   /* The length of lookup_name.  */
658   int lookup_len;
659   struct demangled_name_entry entry;
660
661   if (gsymbol->language == language_ada)
662     {
663       /* In Ada, we do the symbol lookups using the mangled name, so
664          we can save some space by not storing the demangled name.
665
666          As a side note, we have also observed some overlap between
667          the C++ mangling and Ada mangling, similarly to what has
668          been observed with Java.  Because we don't store the demangled
669          name with the symbol, we don't need to use the same trick
670          as Java.  */
671       if (!copy_name)
672         gsymbol->name = linkage_name;
673       else
674         {
675           char *name = obstack_alloc (&objfile->objfile_obstack, len + 1);
676
677           memcpy (name, linkage_name, len);
678           name[len] = '\0';
679           gsymbol->name = name;
680         }
681       symbol_set_demangled_name (gsymbol, NULL, NULL);
682
683       return;
684     }
685
686   if (objfile->demangled_names_hash == NULL)
687     create_demangled_names_hash (objfile);
688
689   /* The stabs reader generally provides names that are not
690      NUL-terminated; most of the other readers don't do this, so we
691      can just use the given copy, unless we're in the Java case.  */
692   if (gsymbol->language == language_java)
693     {
694       char *alloc_name;
695
696       lookup_len = len + JAVA_PREFIX_LEN;
697       alloc_name = alloca (lookup_len + 1);
698       memcpy (alloc_name, JAVA_PREFIX, JAVA_PREFIX_LEN);
699       memcpy (alloc_name + JAVA_PREFIX_LEN, linkage_name, len);
700       alloc_name[lookup_len] = '\0';
701
702       lookup_name = alloc_name;
703       linkage_name_copy = alloc_name + JAVA_PREFIX_LEN;
704     }
705   else if (linkage_name[len] != '\0')
706     {
707       char *alloc_name;
708
709       lookup_len = len;
710       alloc_name = alloca (lookup_len + 1);
711       memcpy (alloc_name, linkage_name, len);
712       alloc_name[lookup_len] = '\0';
713
714       lookup_name = alloc_name;
715       linkage_name_copy = alloc_name;
716     }
717   else
718     {
719       lookup_len = len;
720       lookup_name = linkage_name;
721       linkage_name_copy = linkage_name;
722     }
723
724   entry.mangled = lookup_name;
725   slot = ((struct demangled_name_entry **)
726           htab_find_slot (objfile->demangled_names_hash,
727                           &entry, INSERT));
728
729   /* If this name is not in the hash table, add it.  */
730   if (*slot == NULL
731       /* A C version of the symbol may have already snuck into the table.
732          This happens to, e.g., main.init (__go_init_main).  Cope.  */
733       || (gsymbol->language == language_go
734           && (*slot)->demangled[0] == '\0'))
735     {
736       char *demangled_name = symbol_find_demangled_name (gsymbol,
737                                                          linkage_name_copy);
738       int demangled_len = demangled_name ? strlen (demangled_name) : 0;
739
740       /* Suppose we have demangled_name==NULL, copy_name==0, and
741          lookup_name==linkage_name.  In this case, we already have the
742          mangled name saved, and we don't have a demangled name.  So,
743          you might think we could save a little space by not recording
744          this in the hash table at all.
745          
746          It turns out that it is actually important to still save such
747          an entry in the hash table, because storing this name gives
748          us better bcache hit rates for partial symbols.  */
749       if (!copy_name && lookup_name == linkage_name)
750         {
751           *slot = obstack_alloc (&objfile->objfile_obstack,
752                                  offsetof (struct demangled_name_entry,
753                                            demangled)
754                                  + demangled_len + 1);
755           (*slot)->mangled = lookup_name;
756         }
757       else
758         {
759           char *mangled_ptr;
760
761           /* If we must copy the mangled name, put it directly after
762              the demangled name so we can have a single
763              allocation.  */
764           *slot = obstack_alloc (&objfile->objfile_obstack,
765                                  offsetof (struct demangled_name_entry,
766                                            demangled)
767                                  + lookup_len + demangled_len + 2);
768           mangled_ptr = &((*slot)->demangled[demangled_len + 1]);
769           strcpy (mangled_ptr, lookup_name);
770           (*slot)->mangled = mangled_ptr;
771         }
772
773       if (demangled_name != NULL)
774         {
775           strcpy ((*slot)->demangled, demangled_name);
776           xfree (demangled_name);
777         }
778       else
779         (*slot)->demangled[0] = '\0';
780     }
781
782   gsymbol->name = (*slot)->mangled + lookup_len - len;
783   if ((*slot)->demangled[0] != '\0')
784     symbol_set_demangled_name (gsymbol, (*slot)->demangled, objfile);
785   else
786     symbol_set_demangled_name (gsymbol, NULL, objfile);
787 }
788
789 /* Return the source code name of a symbol.  In languages where
790    demangling is necessary, this is the demangled name.  */
791
792 const char *
793 symbol_natural_name (const struct general_symbol_info *gsymbol)
794 {
795   switch (gsymbol->language)
796     {
797     case language_cplus:
798     case language_d:
799     case language_go:
800     case language_java:
801     case language_objc:
802     case language_fortran:
803       if (symbol_get_demangled_name (gsymbol) != NULL)
804         return symbol_get_demangled_name (gsymbol);
805       break;
806     case language_ada:
807       if (symbol_get_demangled_name (gsymbol) != NULL)
808         return symbol_get_demangled_name (gsymbol);
809       else
810         return ada_decode_symbol (gsymbol);
811       break;
812     default:
813       break;
814     }
815   return gsymbol->name;
816 }
817
818 /* Return the demangled name for a symbol based on the language for
819    that symbol.  If no demangled name exists, return NULL.  */
820
821 const char *
822 symbol_demangled_name (const struct general_symbol_info *gsymbol)
823 {
824   const char *dem_name = NULL;
825
826   switch (gsymbol->language)
827     {
828     case language_cplus:
829     case language_d:
830     case language_go:
831     case language_java:
832     case language_objc:
833     case language_fortran:
834       dem_name = symbol_get_demangled_name (gsymbol);
835       break;
836     case language_ada:
837       dem_name = symbol_get_demangled_name (gsymbol);
838       if (dem_name == NULL)
839         dem_name = ada_decode_symbol (gsymbol);
840       break;
841     default:
842       break;
843     }
844   return dem_name;
845 }
846
847 /* Return the search name of a symbol---generally the demangled or
848    linkage name of the symbol, depending on how it will be searched for.
849    If there is no distinct demangled name, then returns the same value
850    (same pointer) as SYMBOL_LINKAGE_NAME.  */
851
852 const char *
853 symbol_search_name (const struct general_symbol_info *gsymbol)
854 {
855   if (gsymbol->language == language_ada)
856     return gsymbol->name;
857   else
858     return symbol_natural_name (gsymbol);
859 }
860
861 /* Initialize the structure fields to zero values.  */
862
863 void
864 init_sal (struct symtab_and_line *sal)
865 {
866   sal->pspace = NULL;
867   sal->symtab = 0;
868   sal->section = 0;
869   sal->line = 0;
870   sal->pc = 0;
871   sal->end = 0;
872   sal->explicit_pc = 0;
873   sal->explicit_line = 0;
874   sal->probe = NULL;
875 }
876 \f
877
878 /* Return 1 if the two sections are the same, or if they could
879    plausibly be copies of each other, one in an original object
880    file and another in a separated debug file.  */
881
882 int
883 matching_obj_sections (struct obj_section *obj_first,
884                        struct obj_section *obj_second)
885 {
886   asection *first = obj_first? obj_first->the_bfd_section : NULL;
887   asection *second = obj_second? obj_second->the_bfd_section : NULL;
888   struct objfile *obj;
889
890   /* If they're the same section, then they match.  */
891   if (first == second)
892     return 1;
893
894   /* If either is NULL, give up.  */
895   if (first == NULL || second == NULL)
896     return 0;
897
898   /* This doesn't apply to absolute symbols.  */
899   if (first->owner == NULL || second->owner == NULL)
900     return 0;
901
902   /* If they're in the same object file, they must be different sections.  */
903   if (first->owner == second->owner)
904     return 0;
905
906   /* Check whether the two sections are potentially corresponding.  They must
907      have the same size, address, and name.  We can't compare section indexes,
908      which would be more reliable, because some sections may have been
909      stripped.  */
910   if (bfd_get_section_size (first) != bfd_get_section_size (second))
911     return 0;
912
913   /* In-memory addresses may start at a different offset, relativize them.  */
914   if (bfd_get_section_vma (first->owner, first)
915       - bfd_get_start_address (first->owner)
916       != bfd_get_section_vma (second->owner, second)
917          - bfd_get_start_address (second->owner))
918     return 0;
919
920   if (bfd_get_section_name (first->owner, first) == NULL
921       || bfd_get_section_name (second->owner, second) == NULL
922       || strcmp (bfd_get_section_name (first->owner, first),
923                  bfd_get_section_name (second->owner, second)) != 0)
924     return 0;
925
926   /* Otherwise check that they are in corresponding objfiles.  */
927
928   ALL_OBJFILES (obj)
929     if (obj->obfd == first->owner)
930       break;
931   gdb_assert (obj != NULL);
932
933   if (obj->separate_debug_objfile != NULL
934       && obj->separate_debug_objfile->obfd == second->owner)
935     return 1;
936   if (obj->separate_debug_objfile_backlink != NULL
937       && obj->separate_debug_objfile_backlink->obfd == second->owner)
938     return 1;
939
940   return 0;
941 }
942
943 struct symtab *
944 find_pc_sect_symtab_via_partial (CORE_ADDR pc, struct obj_section *section)
945 {
946   struct objfile *objfile;
947   struct minimal_symbol *msymbol;
948
949   /* If we know that this is not a text address, return failure.  This is
950      necessary because we loop based on texthigh and textlow, which do
951      not include the data ranges.  */
952   msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
953   if (msymbol
954       && (MSYMBOL_TYPE (msymbol) == mst_data
955           || MSYMBOL_TYPE (msymbol) == mst_bss
956           || MSYMBOL_TYPE (msymbol) == mst_abs
957           || MSYMBOL_TYPE (msymbol) == mst_file_data
958           || MSYMBOL_TYPE (msymbol) == mst_file_bss))
959     return NULL;
960
961   ALL_OBJFILES (objfile)
962   {
963     struct symtab *result = NULL;
964
965     if (objfile->sf)
966       result = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
967                                                      pc, section, 0);
968     if (result)
969       return result;
970   }
971
972   return NULL;
973 }
974 \f
975 /* Debug symbols usually don't have section information.  We need to dig that
976    out of the minimal symbols and stash that in the debug symbol.  */
977
978 void
979 fixup_section (struct general_symbol_info *ginfo,
980                CORE_ADDR addr, struct objfile *objfile)
981 {
982   struct minimal_symbol *msym;
983
984   /* First, check whether a minimal symbol with the same name exists
985      and points to the same address.  The address check is required
986      e.g. on PowerPC64, where the minimal symbol for a function will
987      point to the function descriptor, while the debug symbol will
988      point to the actual function code.  */
989   msym = lookup_minimal_symbol_by_pc_name (addr, ginfo->name, objfile);
990   if (msym)
991     {
992       ginfo->obj_section = SYMBOL_OBJ_SECTION (msym);
993       ginfo->section = SYMBOL_SECTION (msym);
994     }
995   else
996     {
997       /* Static, function-local variables do appear in the linker
998          (minimal) symbols, but are frequently given names that won't
999          be found via lookup_minimal_symbol().  E.g., it has been
1000          observed in frv-uclinux (ELF) executables that a static,
1001          function-local variable named "foo" might appear in the
1002          linker symbols as "foo.6" or "foo.3".  Thus, there is no
1003          point in attempting to extend the lookup-by-name mechanism to
1004          handle this case due to the fact that there can be multiple
1005          names.
1006
1007          So, instead, search the section table when lookup by name has
1008          failed.  The ``addr'' and ``endaddr'' fields may have already
1009          been relocated.  If so, the relocation offset (i.e. the
1010          ANOFFSET value) needs to be subtracted from these values when
1011          performing the comparison.  We unconditionally subtract it,
1012          because, when no relocation has been performed, the ANOFFSET
1013          value will simply be zero.
1014
1015          The address of the symbol whose section we're fixing up HAS
1016          NOT BEEN adjusted (relocated) yet.  It can't have been since
1017          the section isn't yet known and knowing the section is
1018          necessary in order to add the correct relocation value.  In
1019          other words, we wouldn't even be in this function (attempting
1020          to compute the section) if it were already known.
1021
1022          Note that it is possible to search the minimal symbols
1023          (subtracting the relocation value if necessary) to find the
1024          matching minimal symbol, but this is overkill and much less
1025          efficient.  It is not necessary to find the matching minimal
1026          symbol, only its section.
1027
1028          Note that this technique (of doing a section table search)
1029          can fail when unrelocated section addresses overlap.  For
1030          this reason, we still attempt a lookup by name prior to doing
1031          a search of the section table.  */
1032
1033       struct obj_section *s;
1034
1035       ALL_OBJFILE_OSECTIONS (objfile, s)
1036         {
1037           int idx = s->the_bfd_section->index;
1038           CORE_ADDR offset = ANOFFSET (objfile->section_offsets, idx);
1039
1040           if (obj_section_addr (s) - offset <= addr
1041               && addr < obj_section_endaddr (s) - offset)
1042             {
1043               ginfo->obj_section = s;
1044               ginfo->section = idx;
1045               return;
1046             }
1047         }
1048     }
1049 }
1050
1051 struct symbol *
1052 fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
1053 {
1054   CORE_ADDR addr;
1055
1056   if (!sym)
1057     return NULL;
1058
1059   if (SYMBOL_OBJ_SECTION (sym))
1060     return sym;
1061
1062   /* We either have an OBJFILE, or we can get at it from the sym's
1063      symtab.  Anything else is a bug.  */
1064   gdb_assert (objfile || SYMBOL_SYMTAB (sym));
1065
1066   if (objfile == NULL)
1067     objfile = SYMBOL_SYMTAB (sym)->objfile;
1068
1069   /* We should have an objfile by now.  */
1070   gdb_assert (objfile);
1071
1072   switch (SYMBOL_CLASS (sym))
1073     {
1074     case LOC_STATIC:
1075     case LOC_LABEL:
1076       addr = SYMBOL_VALUE_ADDRESS (sym);
1077       break;
1078     case LOC_BLOCK:
1079       addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1080       break;
1081
1082     default:
1083       /* Nothing else will be listed in the minsyms -- no use looking
1084          it up.  */
1085       return sym;
1086     }
1087
1088   fixup_section (&sym->ginfo, addr, objfile);
1089
1090   return sym;
1091 }
1092
1093 /* Compute the demangled form of NAME as used by the various symbol
1094    lookup functions.  The result is stored in *RESULT_NAME.  Returns a
1095    cleanup which can be used to clean up the result.
1096
1097    For Ada, this function just sets *RESULT_NAME to NAME, unmodified.
1098    Normally, Ada symbol lookups are performed using the encoded name
1099    rather than the demangled name, and so it might seem to make sense
1100    for this function to return an encoded version of NAME.
1101    Unfortunately, we cannot do this, because this function is used in
1102    circumstances where it is not appropriate to try to encode NAME.
1103    For instance, when displaying the frame info, we demangle the name
1104    of each parameter, and then perform a symbol lookup inside our
1105    function using that demangled name.  In Ada, certain functions
1106    have internally-generated parameters whose name contain uppercase
1107    characters.  Encoding those name would result in those uppercase
1108    characters to become lowercase, and thus cause the symbol lookup
1109    to fail.  */
1110
1111 struct cleanup *
1112 demangle_for_lookup (const char *name, enum language lang,
1113                      const char **result_name)
1114 {
1115   char *demangled_name = NULL;
1116   const char *modified_name = NULL;
1117   struct cleanup *cleanup = make_cleanup (null_cleanup, 0);
1118
1119   modified_name = name;
1120
1121   /* If we are using C++, D, Go, or Java, demangle the name before doing a
1122      lookup, so we can always binary search.  */
1123   if (lang == language_cplus)
1124     {
1125       demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1126       if (demangled_name)
1127         {
1128           modified_name = demangled_name;
1129           make_cleanup (xfree, demangled_name);
1130         }
1131       else
1132         {
1133           /* If we were given a non-mangled name, canonicalize it
1134              according to the language (so far only for C++).  */
1135           demangled_name = cp_canonicalize_string (name);
1136           if (demangled_name)
1137             {
1138               modified_name = demangled_name;
1139               make_cleanup (xfree, demangled_name);
1140             }
1141         }
1142     }
1143   else if (lang == language_java)
1144     {
1145       demangled_name = cplus_demangle (name,
1146                                        DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
1147       if (demangled_name)
1148         {
1149           modified_name = demangled_name;
1150           make_cleanup (xfree, demangled_name);
1151         }
1152     }
1153   else if (lang == language_d)
1154     {
1155       demangled_name = d_demangle (name, 0);
1156       if (demangled_name)
1157         {
1158           modified_name = demangled_name;
1159           make_cleanup (xfree, demangled_name);
1160         }
1161     }
1162   else if (lang == language_go)
1163     {
1164       demangled_name = go_demangle (name, 0);
1165       if (demangled_name)
1166         {
1167           modified_name = demangled_name;
1168           make_cleanup (xfree, demangled_name);
1169         }
1170     }
1171
1172   *result_name = modified_name;
1173   return cleanup;
1174 }
1175
1176 /* Find the definition for a specified symbol name NAME
1177    in domain DOMAIN, visible from lexical block BLOCK.
1178    Returns the struct symbol pointer, or zero if no symbol is found.
1179    C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
1180    NAME is a field of the current implied argument `this'.  If so set
1181    *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
1182    BLOCK_FOUND is set to the block in which NAME is found (in the case of
1183    a field of `this', value_of_this sets BLOCK_FOUND to the proper value.)  */
1184
1185 /* This function (or rather its subordinates) have a bunch of loops and
1186    it would seem to be attractive to put in some QUIT's (though I'm not really
1187    sure whether it can run long enough to be really important).  But there
1188    are a few calls for which it would appear to be bad news to quit
1189    out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c.  (Note
1190    that there is C++ code below which can error(), but that probably
1191    doesn't affect these calls since they are looking for a known
1192    variable and thus can probably assume it will never hit the C++
1193    code).  */
1194
1195 struct symbol *
1196 lookup_symbol_in_language (const char *name, const struct block *block,
1197                            const domain_enum domain, enum language lang,
1198                            struct field_of_this_result *is_a_field_of_this)
1199 {
1200   const char *modified_name;
1201   struct symbol *returnval;
1202   struct cleanup *cleanup = demangle_for_lookup (name, lang, &modified_name);
1203
1204   returnval = lookup_symbol_aux (modified_name, block, domain, lang,
1205                                  is_a_field_of_this);
1206   do_cleanups (cleanup);
1207
1208   return returnval;
1209 }
1210
1211 /* Behave like lookup_symbol_in_language, but performed with the
1212    current language.  */
1213
1214 struct symbol *
1215 lookup_symbol (const char *name, const struct block *block,
1216                domain_enum domain,
1217                struct field_of_this_result *is_a_field_of_this)
1218 {
1219   return lookup_symbol_in_language (name, block, domain,
1220                                     current_language->la_language,
1221                                     is_a_field_of_this);
1222 }
1223
1224 /* Look up the `this' symbol for LANG in BLOCK.  Return the symbol if
1225    found, or NULL if not found.  */
1226
1227 struct symbol *
1228 lookup_language_this (const struct language_defn *lang,
1229                       const struct block *block)
1230 {
1231   if (lang->la_name_of_this == NULL || block == NULL)
1232     return NULL;
1233
1234   while (block)
1235     {
1236       struct symbol *sym;
1237
1238       sym = lookup_block_symbol (block, lang->la_name_of_this, VAR_DOMAIN);
1239       if (sym != NULL)
1240         {
1241           block_found = block;
1242           return sym;
1243         }
1244       if (BLOCK_FUNCTION (block))
1245         break;
1246       block = BLOCK_SUPERBLOCK (block);
1247     }
1248
1249   return NULL;
1250 }
1251
1252 /* Given TYPE, a structure/union,
1253    return 1 if the component named NAME from the ultimate target
1254    structure/union is defined, otherwise, return 0.  */
1255
1256 static int
1257 check_field (struct type *type, const char *name,
1258              struct field_of_this_result *is_a_field_of_this)
1259 {
1260   int i;
1261
1262   /* The type may be a stub.  */
1263   CHECK_TYPEDEF (type);
1264
1265   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1266     {
1267       const char *t_field_name = TYPE_FIELD_NAME (type, i);
1268
1269       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1270         {
1271           is_a_field_of_this->type = type;
1272           is_a_field_of_this->field = &TYPE_FIELD (type, i);
1273           return 1;
1274         }
1275     }
1276
1277   /* C++: If it was not found as a data field, then try to return it
1278      as a pointer to a method.  */
1279
1280   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1281     {
1282       if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
1283         {
1284           is_a_field_of_this->type = type;
1285           is_a_field_of_this->fn_field = &TYPE_FN_FIELDLIST (type, i);
1286           return 1;
1287         }
1288     }
1289
1290   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1291     if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
1292       return 1;
1293
1294   return 0;
1295 }
1296
1297 /* Behave like lookup_symbol except that NAME is the natural name
1298    (e.g., demangled name) of the symbol that we're looking for.  */
1299
1300 static struct symbol *
1301 lookup_symbol_aux (const char *name, const struct block *block,
1302                    const domain_enum domain, enum language language,
1303                    struct field_of_this_result *is_a_field_of_this)
1304 {
1305   struct symbol *sym;
1306   const struct language_defn *langdef;
1307
1308   /* Make sure we do something sensible with is_a_field_of_this, since
1309      the callers that set this parameter to some non-null value will
1310      certainly use it later.  If we don't set it, the contents of
1311      is_a_field_of_this are undefined.  */
1312   if (is_a_field_of_this != NULL)
1313     memset (is_a_field_of_this, 0, sizeof (*is_a_field_of_this));
1314
1315   /* Search specified block and its superiors.  Don't search
1316      STATIC_BLOCK or GLOBAL_BLOCK.  */
1317
1318   sym = lookup_symbol_aux_local (name, block, domain, language);
1319   if (sym != NULL)
1320     return sym;
1321
1322   /* If requested to do so by the caller and if appropriate for LANGUAGE,
1323      check to see if NAME is a field of `this'.  */
1324
1325   langdef = language_def (language);
1326
1327   /* Don't do this check if we are searching for a struct.  It will
1328      not be found by check_field, but will be found by other
1329      means.  */
1330   if (is_a_field_of_this != NULL && domain != STRUCT_DOMAIN)
1331     {
1332       struct symbol *sym = lookup_language_this (langdef, block);
1333
1334       if (sym)
1335         {
1336           struct type *t = sym->type;
1337
1338           /* I'm not really sure that type of this can ever
1339              be typedefed; just be safe.  */
1340           CHECK_TYPEDEF (t);
1341           if (TYPE_CODE (t) == TYPE_CODE_PTR
1342               || TYPE_CODE (t) == TYPE_CODE_REF)
1343             t = TYPE_TARGET_TYPE (t);
1344
1345           if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1346               && TYPE_CODE (t) != TYPE_CODE_UNION)
1347             error (_("Internal error: `%s' is not an aggregate"),
1348                    langdef->la_name_of_this);
1349
1350           if (check_field (t, name, is_a_field_of_this))
1351             return NULL;
1352         }
1353     }
1354
1355   /* Now do whatever is appropriate for LANGUAGE to look
1356      up static and global variables.  */
1357
1358   sym = langdef->la_lookup_symbol_nonlocal (name, block, domain);
1359   if (sym != NULL)
1360     return sym;
1361
1362   /* Now search all static file-level symbols.  Not strictly correct,
1363      but more useful than an error.  */
1364
1365   return lookup_static_symbol_aux (name, domain);
1366 }
1367
1368 /* Search all static file-level symbols for NAME from DOMAIN.  Do the symtabs
1369    first, then check the psymtabs.  If a psymtab indicates the existence of the
1370    desired name as a file-level static, then do psymtab-to-symtab conversion on
1371    the fly and return the found symbol.  */
1372
1373 struct symbol *
1374 lookup_static_symbol_aux (const char *name, const domain_enum domain)
1375 {
1376   struct objfile *objfile;
1377   struct symbol *sym;
1378
1379   sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, domain);
1380   if (sym != NULL)
1381     return sym;
1382
1383   ALL_OBJFILES (objfile)
1384   {
1385     sym = lookup_symbol_aux_quick (objfile, STATIC_BLOCK, name, domain);
1386     if (sym != NULL)
1387       return sym;
1388   }
1389
1390   return NULL;
1391 }
1392
1393 /* Check to see if the symbol is defined in BLOCK or its superiors.
1394    Don't search STATIC_BLOCK or GLOBAL_BLOCK.  */
1395
1396 static struct symbol *
1397 lookup_symbol_aux_local (const char *name, const struct block *block,
1398                          const domain_enum domain,
1399                          enum language language)
1400 {
1401   struct symbol *sym;
1402   const struct block *static_block = block_static_block (block);
1403   const char *scope = block_scope (block);
1404   
1405   /* Check if either no block is specified or it's a global block.  */
1406
1407   if (static_block == NULL)
1408     return NULL;
1409
1410   while (block != static_block)
1411     {
1412       sym = lookup_symbol_aux_block (name, block, domain);
1413       if (sym != NULL)
1414         return sym;
1415
1416       if (language == language_cplus || language == language_fortran)
1417         {
1418           sym = cp_lookup_symbol_imports_or_template (scope, name, block,
1419                                                       domain);
1420           if (sym != NULL)
1421             return sym;
1422         }
1423
1424       if (BLOCK_FUNCTION (block) != NULL && block_inlined_p (block))
1425         break;
1426       block = BLOCK_SUPERBLOCK (block);
1427     }
1428
1429   /* We've reached the edge of the function without finding a result.  */
1430
1431   return NULL;
1432 }
1433
1434 /* Look up OBJFILE to BLOCK.  */
1435
1436 struct objfile *
1437 lookup_objfile_from_block (const struct block *block)
1438 {
1439   struct objfile *obj;
1440   struct symtab *s;
1441
1442   if (block == NULL)
1443     return NULL;
1444
1445   block = block_global_block (block);
1446   /* Go through SYMTABS.  */
1447   ALL_SYMTABS (obj, s)
1448     if (block == BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK))
1449       {
1450         if (obj->separate_debug_objfile_backlink)
1451           obj = obj->separate_debug_objfile_backlink;
1452
1453         return obj;
1454       }
1455
1456   return NULL;
1457 }
1458
1459 /* Look up a symbol in a block; if found, fixup the symbol, and set
1460    block_found appropriately.  */
1461
1462 struct symbol *
1463 lookup_symbol_aux_block (const char *name, const struct block *block,
1464                          const domain_enum domain)
1465 {
1466   struct symbol *sym;
1467
1468   sym = lookup_block_symbol (block, name, domain);
1469   if (sym)
1470     {
1471       block_found = block;
1472       return fixup_symbol_section (sym, NULL);
1473     }
1474
1475   return NULL;
1476 }
1477
1478 /* Check all global symbols in OBJFILE in symtabs and
1479    psymtabs.  */
1480
1481 struct symbol *
1482 lookup_global_symbol_from_objfile (const struct objfile *main_objfile,
1483                                    const char *name,
1484                                    const domain_enum domain)
1485 {
1486   const struct objfile *objfile;
1487   struct symbol *sym;
1488   struct blockvector *bv;
1489   const struct block *block;
1490   struct symtab *s;
1491
1492   for (objfile = main_objfile;
1493        objfile;
1494        objfile = objfile_separate_debug_iterate (main_objfile, objfile))
1495     {
1496       /* Go through symtabs.  */
1497       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
1498         {
1499           bv = BLOCKVECTOR (s);
1500           block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1501           sym = lookup_block_symbol (block, name, domain);
1502           if (sym)
1503             {
1504               block_found = block;
1505               return fixup_symbol_section (sym, (struct objfile *)objfile);
1506             }
1507         }
1508
1509       sym = lookup_symbol_aux_quick ((struct objfile *) objfile, GLOBAL_BLOCK,
1510                                      name, domain);
1511       if (sym)
1512         return sym;
1513     }
1514
1515   return NULL;
1516 }
1517
1518 /* Check to see if the symbol is defined in one of the OBJFILE's
1519    symtabs.  BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
1520    depending on whether or not we want to search global symbols or
1521    static symbols.  */
1522
1523 static struct symbol *
1524 lookup_symbol_aux_objfile (struct objfile *objfile, int block_index,
1525                            const char *name, const domain_enum domain)
1526 {
1527   struct symbol *sym = NULL;
1528   struct blockvector *bv;
1529   const struct block *block;
1530   struct symtab *s;
1531
1532   ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
1533     {
1534       bv = BLOCKVECTOR (s);
1535       block = BLOCKVECTOR_BLOCK (bv, block_index);
1536       sym = lookup_block_symbol (block, name, domain);
1537       if (sym)
1538         {
1539           block_found = block;
1540           return fixup_symbol_section (sym, objfile);
1541         }
1542     }
1543
1544   return NULL;
1545 }
1546
1547 /* Same as lookup_symbol_aux_objfile, except that it searches all
1548    objfiles.  Return the first match found.  */
1549
1550 static struct symbol *
1551 lookup_symbol_aux_symtabs (int block_index, const char *name,
1552                            const domain_enum domain)
1553 {
1554   struct symbol *sym;
1555   struct objfile *objfile;
1556
1557   ALL_OBJFILES (objfile)
1558   {
1559     sym = lookup_symbol_aux_objfile (objfile, block_index, name, domain);
1560     if (sym)
1561       return sym;
1562   }
1563
1564   return NULL;
1565 }
1566
1567 /* Wrapper around lookup_symbol_aux_objfile for search_symbols.
1568    Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
1569    and all related objfiles.  */
1570
1571 static struct symbol *
1572 lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
1573                                             const char *linkage_name,
1574                                             domain_enum domain)
1575 {
1576   enum language lang = current_language->la_language;
1577   const char *modified_name;
1578   struct cleanup *cleanup = demangle_for_lookup (linkage_name, lang,
1579                                                  &modified_name);
1580   struct objfile *main_objfile, *cur_objfile;
1581
1582   if (objfile->separate_debug_objfile_backlink)
1583     main_objfile = objfile->separate_debug_objfile_backlink;
1584   else
1585     main_objfile = objfile;
1586
1587   for (cur_objfile = main_objfile;
1588        cur_objfile;
1589        cur_objfile = objfile_separate_debug_iterate (main_objfile, cur_objfile))
1590     {
1591       struct symbol *sym;
1592
1593       sym = lookup_symbol_aux_objfile (cur_objfile, GLOBAL_BLOCK,
1594                                        modified_name, domain);
1595       if (sym == NULL)
1596         sym = lookup_symbol_aux_objfile (cur_objfile, STATIC_BLOCK,
1597                                          modified_name, domain);
1598       if (sym != NULL)
1599         {
1600           do_cleanups (cleanup);
1601           return sym;
1602         }
1603     }
1604
1605   do_cleanups (cleanup);
1606   return NULL;
1607 }
1608
1609 /* A helper function for lookup_symbol_aux that interfaces with the
1610    "quick" symbol table functions.  */
1611
1612 static struct symbol *
1613 lookup_symbol_aux_quick (struct objfile *objfile, int kind,
1614                          const char *name, const domain_enum domain)
1615 {
1616   struct symtab *symtab;
1617   struct blockvector *bv;
1618   const struct block *block;
1619   struct symbol *sym;
1620
1621   if (!objfile->sf)
1622     return NULL;
1623   symtab = objfile->sf->qf->lookup_symbol (objfile, kind, name, domain);
1624   if (!symtab)
1625     return NULL;
1626
1627   bv = BLOCKVECTOR (symtab);
1628   block = BLOCKVECTOR_BLOCK (bv, kind);
1629   sym = lookup_block_symbol (block, name, domain);
1630   if (!sym)
1631     {
1632       /* This shouldn't be necessary, but as a last resort try
1633          looking in the statics even though the psymtab claimed
1634          the symbol was global, or vice-versa.  It's possible
1635          that the psymtab gets it wrong in some cases.  */
1636
1637       /* FIXME: carlton/2002-09-30: Should we really do that?
1638          If that happens, isn't it likely to be a GDB error, in
1639          which case we should fix the GDB error rather than
1640          silently dealing with it here?  So I'd vote for
1641          removing the check for the symbol in the other
1642          block.  */
1643       block = BLOCKVECTOR_BLOCK (bv,
1644                                  kind == GLOBAL_BLOCK ?
1645                                  STATIC_BLOCK : GLOBAL_BLOCK);
1646       sym = lookup_block_symbol (block, name, domain);
1647       if (!sym)
1648         error (_("\
1649 Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
1650 %s may be an inlined function, or may be a template function\n\
1651 (if a template, try specifying an instantiation: %s<type>)."),
1652                kind == GLOBAL_BLOCK ? "global" : "static",
1653                name, symtab->filename, name, name);
1654     }
1655   return fixup_symbol_section (sym, objfile);
1656 }
1657
1658 /* A default version of lookup_symbol_nonlocal for use by languages
1659    that can't think of anything better to do.  This implements the C
1660    lookup rules.  */
1661
1662 struct symbol *
1663 basic_lookup_symbol_nonlocal (const char *name,
1664                               const struct block *block,
1665                               const domain_enum domain)
1666 {
1667   struct symbol *sym;
1668
1669   /* NOTE: carlton/2003-05-19: The comments below were written when
1670      this (or what turned into this) was part of lookup_symbol_aux;
1671      I'm much less worried about these questions now, since these
1672      decisions have turned out well, but I leave these comments here
1673      for posterity.  */
1674
1675   /* NOTE: carlton/2002-12-05: There is a question as to whether or
1676      not it would be appropriate to search the current global block
1677      here as well.  (That's what this code used to do before the
1678      is_a_field_of_this check was moved up.)  On the one hand, it's
1679      redundant with the lookup_symbol_aux_symtabs search that happens
1680      next.  On the other hand, if decode_line_1 is passed an argument
1681      like filename:var, then the user presumably wants 'var' to be
1682      searched for in filename.  On the third hand, there shouldn't be
1683      multiple global variables all of which are named 'var', and it's
1684      not like decode_line_1 has ever restricted its search to only
1685      global variables in a single filename.  All in all, only
1686      searching the static block here seems best: it's correct and it's
1687      cleanest.  */
1688
1689   /* NOTE: carlton/2002-12-05: There's also a possible performance
1690      issue here: if you usually search for global symbols in the
1691      current file, then it would be slightly better to search the
1692      current global block before searching all the symtabs.  But there
1693      are other factors that have a much greater effect on performance
1694      than that one, so I don't think we should worry about that for
1695      now.  */
1696
1697   sym = lookup_symbol_static (name, block, domain);
1698   if (sym != NULL)
1699     return sym;
1700
1701   return lookup_symbol_global (name, block, domain);
1702 }
1703
1704 /* Lookup a symbol in the static block associated to BLOCK, if there
1705    is one; do nothing if BLOCK is NULL or a global block.  */
1706
1707 struct symbol *
1708 lookup_symbol_static (const char *name,
1709                       const struct block *block,
1710                       const domain_enum domain)
1711 {
1712   const struct block *static_block = block_static_block (block);
1713
1714   if (static_block != NULL)
1715     return lookup_symbol_aux_block (name, static_block, domain);
1716   else
1717     return NULL;
1718 }
1719
1720 /* Private data to be used with lookup_symbol_global_iterator_cb.  */
1721
1722 struct global_sym_lookup_data
1723 {
1724   /* The name of the symbol we are searching for.  */
1725   const char *name;
1726
1727   /* The domain to use for our search.  */
1728   domain_enum domain;
1729
1730   /* The field where the callback should store the symbol if found.
1731      It should be initialized to NULL before the search is started.  */
1732   struct symbol *result;
1733 };
1734
1735 /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
1736    It searches by name for a symbol in the GLOBAL_BLOCK of the given
1737    OBJFILE.  The arguments for the search are passed via CB_DATA,
1738    which in reality is a pointer to struct global_sym_lookup_data.  */
1739
1740 static int
1741 lookup_symbol_global_iterator_cb (struct objfile *objfile,
1742                                   void *cb_data)
1743 {
1744   struct global_sym_lookup_data *data =
1745     (struct global_sym_lookup_data *) cb_data;
1746
1747   gdb_assert (data->result == NULL);
1748
1749   data->result = lookup_symbol_aux_objfile (objfile, GLOBAL_BLOCK,
1750                                             data->name, data->domain);
1751   if (data->result == NULL)
1752     data->result = lookup_symbol_aux_quick (objfile, GLOBAL_BLOCK,
1753                                             data->name, data->domain);
1754
1755   /* If we found a match, tell the iterator to stop.  Otherwise,
1756      keep going.  */
1757   return (data->result != NULL);
1758 }
1759
1760 /* Lookup a symbol in all files' global blocks (searching psymtabs if
1761    necessary).  */
1762
1763 struct symbol *
1764 lookup_symbol_global (const char *name,
1765                       const struct block *block,
1766                       const domain_enum domain)
1767 {
1768   struct symbol *sym = NULL;
1769   struct objfile *objfile = NULL;
1770   struct global_sym_lookup_data lookup_data;
1771
1772   /* Call library-specific lookup procedure.  */
1773   objfile = lookup_objfile_from_block (block);
1774   if (objfile != NULL)
1775     sym = solib_global_lookup (objfile, name, domain);
1776   if (sym != NULL)
1777     return sym;
1778
1779   memset (&lookup_data, 0, sizeof (lookup_data));
1780   lookup_data.name = name;
1781   lookup_data.domain = domain;
1782   gdbarch_iterate_over_objfiles_in_search_order
1783     (objfile != NULL ? get_objfile_arch (objfile) : target_gdbarch (),
1784      lookup_symbol_global_iterator_cb, &lookup_data, objfile);
1785
1786   return lookup_data.result;
1787 }
1788
1789 int
1790 symbol_matches_domain (enum language symbol_language,
1791                        domain_enum symbol_domain,
1792                        domain_enum domain)
1793 {
1794   /* For C++ "struct foo { ... }" also defines a typedef for "foo".
1795      A Java class declaration also defines a typedef for the class.
1796      Similarly, any Ada type declaration implicitly defines a typedef.  */
1797   if (symbol_language == language_cplus
1798       || symbol_language == language_d
1799       || symbol_language == language_java
1800       || symbol_language == language_ada)
1801     {
1802       if ((domain == VAR_DOMAIN || domain == STRUCT_DOMAIN)
1803           && symbol_domain == STRUCT_DOMAIN)
1804         return 1;
1805     }
1806   /* For all other languages, strict match is required.  */
1807   return (symbol_domain == domain);
1808 }
1809
1810 /* Look up a type named NAME in the struct_domain.  The type returned
1811    must not be opaque -- i.e., must have at least one field
1812    defined.  */
1813
1814 struct type *
1815 lookup_transparent_type (const char *name)
1816 {
1817   return current_language->la_lookup_transparent_type (name);
1818 }
1819
1820 /* A helper for basic_lookup_transparent_type that interfaces with the
1821    "quick" symbol table functions.  */
1822
1823 static struct type *
1824 basic_lookup_transparent_type_quick (struct objfile *objfile, int kind,
1825                                      const char *name)
1826 {
1827   struct symtab *symtab;
1828   struct blockvector *bv;
1829   struct block *block;
1830   struct symbol *sym;
1831
1832   if (!objfile->sf)
1833     return NULL;
1834   symtab = objfile->sf->qf->lookup_symbol (objfile, kind, name, STRUCT_DOMAIN);
1835   if (!symtab)
1836     return NULL;
1837
1838   bv = BLOCKVECTOR (symtab);
1839   block = BLOCKVECTOR_BLOCK (bv, kind);
1840   sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
1841   if (!sym)
1842     {
1843       int other_kind = kind == GLOBAL_BLOCK ? STATIC_BLOCK : GLOBAL_BLOCK;
1844
1845       /* This shouldn't be necessary, but as a last resort
1846        * try looking in the 'other kind' even though the psymtab
1847        * claimed the symbol was one thing.  It's possible that
1848        * the psymtab gets it wrong in some cases.
1849        */
1850       block = BLOCKVECTOR_BLOCK (bv, other_kind);
1851       sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
1852       if (!sym)
1853         /* FIXME; error is wrong in one case.  */
1854         error (_("\
1855 Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1856 %s may be an inlined function, or may be a template function\n\
1857 (if a template, try specifying an instantiation: %s<type>)."),
1858                name, symtab->filename, name, name);
1859     }
1860   if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1861     return SYMBOL_TYPE (sym);
1862
1863   return NULL;
1864 }
1865
1866 /* The standard implementation of lookup_transparent_type.  This code
1867    was modeled on lookup_symbol -- the parts not relevant to looking
1868    up types were just left out.  In particular it's assumed here that
1869    types are available in struct_domain and only at file-static or
1870    global blocks.  */
1871
1872 struct type *
1873 basic_lookup_transparent_type (const char *name)
1874 {
1875   struct symbol *sym;
1876   struct symtab *s = NULL;
1877   struct blockvector *bv;
1878   struct objfile *objfile;
1879   struct block *block;
1880   struct type *t;
1881
1882   /* Now search all the global symbols.  Do the symtab's first, then
1883      check the psymtab's.  If a psymtab indicates the existence
1884      of the desired name as a global, then do psymtab-to-symtab
1885      conversion on the fly and return the found symbol.  */
1886
1887   ALL_OBJFILES (objfile)
1888   {
1889     ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
1890       {
1891         bv = BLOCKVECTOR (s);
1892         block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1893         sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
1894         if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1895           {
1896             return SYMBOL_TYPE (sym);
1897           }
1898       }
1899   }
1900
1901   ALL_OBJFILES (objfile)
1902   {
1903     t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
1904     if (t)
1905       return t;
1906   }
1907
1908   /* Now search the static file-level symbols.
1909      Not strictly correct, but more useful than an error.
1910      Do the symtab's first, then
1911      check the psymtab's.  If a psymtab indicates the existence
1912      of the desired name as a file-level static, then do psymtab-to-symtab
1913      conversion on the fly and return the found symbol.  */
1914
1915   ALL_OBJFILES (objfile)
1916   {
1917     ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
1918       {
1919         bv = BLOCKVECTOR (s);
1920         block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1921         sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
1922         if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1923           {
1924             return SYMBOL_TYPE (sym);
1925           }
1926       }
1927   }
1928
1929   ALL_OBJFILES (objfile)
1930   {
1931     t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
1932     if (t)
1933       return t;
1934   }
1935
1936   return (struct type *) 0;
1937 }
1938
1939 /* Find the name of the file containing main().  */
1940 /* FIXME:  What about languages without main() or specially linked
1941    executables that have no main() ?   */
1942
1943 const char *
1944 find_main_filename (void)
1945 {
1946   struct objfile *objfile;
1947   char *name = main_name ();
1948
1949   ALL_OBJFILES (objfile)
1950   {
1951     const char *result;
1952
1953     if (!objfile->sf)
1954       continue;
1955     result = objfile->sf->qf->find_symbol_file (objfile, name);
1956     if (result)
1957       return result;
1958   }
1959   return (NULL);
1960 }
1961
1962 /* Search BLOCK for symbol NAME in DOMAIN.
1963
1964    Note that if NAME is the demangled form of a C++ symbol, we will fail
1965    to find a match during the binary search of the non-encoded names, but
1966    for now we don't worry about the slight inefficiency of looking for
1967    a match we'll never find, since it will go pretty quick.  Once the
1968    binary search terminates, we drop through and do a straight linear
1969    search on the symbols.  Each symbol which is marked as being a ObjC/C++
1970    symbol (language_cplus or language_objc set) has both the encoded and
1971    non-encoded names tested for a match.  */
1972
1973 struct symbol *
1974 lookup_block_symbol (const struct block *block, const char *name,
1975                      const domain_enum domain)
1976 {
1977   struct block_iterator iter;
1978   struct symbol *sym;
1979
1980   if (!BLOCK_FUNCTION (block))
1981     {
1982       for (sym = block_iter_name_first (block, name, &iter);
1983            sym != NULL;
1984            sym = block_iter_name_next (name, &iter))
1985         {
1986           if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1987                                      SYMBOL_DOMAIN (sym), domain))
1988             return sym;
1989         }
1990       return NULL;
1991     }
1992   else
1993     {
1994       /* Note that parameter symbols do not always show up last in the
1995          list; this loop makes sure to take anything else other than
1996          parameter symbols first; it only uses parameter symbols as a
1997          last resort.  Note that this only takes up extra computation
1998          time on a match.  */
1999
2000       struct symbol *sym_found = NULL;
2001
2002       for (sym = block_iter_name_first (block, name, &iter);
2003            sym != NULL;
2004            sym = block_iter_name_next (name, &iter))
2005         {
2006           if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
2007                                      SYMBOL_DOMAIN (sym), domain))
2008             {
2009               sym_found = sym;
2010               if (!SYMBOL_IS_ARGUMENT (sym))
2011                 {
2012                   break;
2013                 }
2014             }
2015         }
2016       return (sym_found);       /* Will be NULL if not found.  */
2017     }
2018 }
2019
2020 /* Iterate over the symbols named NAME, matching DOMAIN, starting with
2021    BLOCK.
2022    
2023    For each symbol that matches, CALLBACK is called.  The symbol and
2024    DATA are passed to the callback.
2025    
2026    If CALLBACK returns zero, the iteration ends.  Otherwise, the
2027    search continues.  This function iterates upward through blocks.
2028    When the outermost block has been finished, the function
2029    returns.  */
2030
2031 void
2032 iterate_over_symbols (const struct block *block, const char *name,
2033                       const domain_enum domain,
2034                       symbol_found_callback_ftype *callback,
2035                       void *data)
2036 {
2037   while (block)
2038     {
2039       struct block_iterator iter;
2040       struct symbol *sym;
2041
2042       for (sym = block_iter_name_first (block, name, &iter);
2043            sym != NULL;
2044            sym = block_iter_name_next (name, &iter))
2045         {
2046           if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
2047                                      SYMBOL_DOMAIN (sym), domain))
2048             {
2049               if (!callback (sym, data))
2050                 return;
2051             }
2052         }
2053
2054       block = BLOCK_SUPERBLOCK (block);
2055     }
2056 }
2057
2058 /* Find the symtab associated with PC and SECTION.  Look through the
2059    psymtabs and read in another symtab if necessary.  */
2060
2061 struct symtab *
2062 find_pc_sect_symtab (CORE_ADDR pc, struct obj_section *section)
2063 {
2064   struct block *b;
2065   struct blockvector *bv;
2066   struct symtab *s = NULL;
2067   struct symtab *best_s = NULL;
2068   struct objfile *objfile;
2069   CORE_ADDR distance = 0;
2070   struct minimal_symbol *msymbol;
2071
2072   /* If we know that this is not a text address, return failure.  This is
2073      necessary because we loop based on the block's high and low code
2074      addresses, which do not include the data ranges, and because
2075      we call find_pc_sect_psymtab which has a similar restriction based
2076      on the partial_symtab's texthigh and textlow.  */
2077   msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
2078   if (msymbol
2079       && (MSYMBOL_TYPE (msymbol) == mst_data
2080           || MSYMBOL_TYPE (msymbol) == mst_bss
2081           || MSYMBOL_TYPE (msymbol) == mst_abs
2082           || MSYMBOL_TYPE (msymbol) == mst_file_data
2083           || MSYMBOL_TYPE (msymbol) == mst_file_bss))
2084     return NULL;
2085
2086   /* Search all symtabs for the one whose file contains our address, and which
2087      is the smallest of all the ones containing the address.  This is designed
2088      to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
2089      and symtab b is at 0x2000-0x3000.  So the GLOBAL_BLOCK for a is from
2090      0x1000-0x4000, but for address 0x2345 we want to return symtab b.
2091
2092      This happens for native ecoff format, where code from included files
2093      gets its own symtab.  The symtab for the included file should have
2094      been read in already via the dependency mechanism.
2095      It might be swifter to create several symtabs with the same name
2096      like xcoff does (I'm not sure).
2097
2098      It also happens for objfiles that have their functions reordered.
2099      For these, the symtab we are looking for is not necessarily read in.  */
2100
2101   ALL_PRIMARY_SYMTABS (objfile, s)
2102   {
2103     bv = BLOCKVECTOR (s);
2104     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2105
2106     if (BLOCK_START (b) <= pc
2107         && BLOCK_END (b) > pc
2108         && (distance == 0
2109             || BLOCK_END (b) - BLOCK_START (b) < distance))
2110       {
2111         /* For an objfile that has its functions reordered,
2112            find_pc_psymtab will find the proper partial symbol table
2113            and we simply return its corresponding symtab.  */
2114         /* In order to better support objfiles that contain both
2115            stabs and coff debugging info, we continue on if a psymtab
2116            can't be found.  */
2117         if ((objfile->flags & OBJF_REORDERED) && objfile->sf)
2118           {
2119             struct symtab *result;
2120
2121             result
2122               = objfile->sf->qf->find_pc_sect_symtab (objfile,
2123                                                       msymbol,
2124                                                       pc, section,
2125                                                       0);
2126             if (result)
2127               return result;
2128           }
2129         if (section != 0)
2130           {
2131             struct block_iterator iter;
2132             struct symbol *sym = NULL;
2133
2134             ALL_BLOCK_SYMBOLS (b, iter, sym)
2135               {
2136                 fixup_symbol_section (sym, objfile);
2137                 if (matching_obj_sections (SYMBOL_OBJ_SECTION (sym), section))
2138                   break;
2139               }
2140             if (sym == NULL)
2141               continue;         /* No symbol in this symtab matches
2142                                    section.  */
2143           }
2144         distance = BLOCK_END (b) - BLOCK_START (b);
2145         best_s = s;
2146       }
2147   }
2148
2149   if (best_s != NULL)
2150     return (best_s);
2151
2152   /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs).  */
2153
2154   ALL_OBJFILES (objfile)
2155   {
2156     struct symtab *result;
2157
2158     if (!objfile->sf)
2159       continue;
2160     result = objfile->sf->qf->find_pc_sect_symtab (objfile,
2161                                                    msymbol,
2162                                                    pc, section,
2163                                                    1);
2164     if (result)
2165       return result;
2166   }
2167
2168   return NULL;
2169 }
2170
2171 /* Find the symtab associated with PC.  Look through the psymtabs and read
2172    in another symtab if necessary.  Backward compatibility, no section.  */
2173
2174 struct symtab *
2175 find_pc_symtab (CORE_ADDR pc)
2176 {
2177   return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
2178 }
2179 \f
2180
2181 /* Find the source file and line number for a given PC value and SECTION.
2182    Return a structure containing a symtab pointer, a line number,
2183    and a pc range for the entire source line.
2184    The value's .pc field is NOT the specified pc.
2185    NOTCURRENT nonzero means, if specified pc is on a line boundary,
2186    use the line that ends there.  Otherwise, in that case, the line
2187    that begins there is used.  */
2188
2189 /* The big complication here is that a line may start in one file, and end just
2190    before the start of another file.  This usually occurs when you #include
2191    code in the middle of a subroutine.  To properly find the end of a line's PC
2192    range, we must search all symtabs associated with this compilation unit, and
2193    find the one whose first PC is closer than that of the next line in this
2194    symtab.  */
2195
2196 /* If it's worth the effort, we could be using a binary search.  */
2197
2198 struct symtab_and_line
2199 find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
2200 {
2201   struct symtab *s;
2202   struct linetable *l;
2203   int len;
2204   int i;
2205   struct linetable_entry *item;
2206   struct symtab_and_line val;
2207   struct blockvector *bv;
2208   struct minimal_symbol *msymbol;
2209   struct minimal_symbol *mfunsym;
2210   struct objfile *objfile;
2211
2212   /* Info on best line seen so far, and where it starts, and its file.  */
2213
2214   struct linetable_entry *best = NULL;
2215   CORE_ADDR best_end = 0;
2216   struct symtab *best_symtab = 0;
2217
2218   /* Store here the first line number
2219      of a file which contains the line at the smallest pc after PC.
2220      If we don't find a line whose range contains PC,
2221      we will use a line one less than this,
2222      with a range from the start of that file to the first line's pc.  */
2223   struct linetable_entry *alt = NULL;
2224
2225   /* Info on best line seen in this file.  */
2226
2227   struct linetable_entry *prev;
2228
2229   /* If this pc is not from the current frame,
2230      it is the address of the end of a call instruction.
2231      Quite likely that is the start of the following statement.
2232      But what we want is the statement containing the instruction.
2233      Fudge the pc to make sure we get that.  */
2234
2235   init_sal (&val);              /* initialize to zeroes */
2236
2237   val.pspace = current_program_space;
2238
2239   /* It's tempting to assume that, if we can't find debugging info for
2240      any function enclosing PC, that we shouldn't search for line
2241      number info, either.  However, GAS can emit line number info for
2242      assembly files --- very helpful when debugging hand-written
2243      assembly code.  In such a case, we'd have no debug info for the
2244      function, but we would have line info.  */
2245
2246   if (notcurrent)
2247     pc -= 1;
2248
2249   /* elz: added this because this function returned the wrong
2250      information if the pc belongs to a stub (import/export)
2251      to call a shlib function.  This stub would be anywhere between
2252      two functions in the target, and the line info was erroneously
2253      taken to be the one of the line before the pc.  */
2254
2255   /* RT: Further explanation:
2256
2257    * We have stubs (trampolines) inserted between procedures.
2258    *
2259    * Example: "shr1" exists in a shared library, and a "shr1" stub also
2260    * exists in the main image.
2261    *
2262    * In the minimal symbol table, we have a bunch of symbols
2263    * sorted by start address.  The stubs are marked as "trampoline",
2264    * the others appear as text. E.g.:
2265    *
2266    *  Minimal symbol table for main image
2267    *     main:  code for main (text symbol)
2268    *     shr1: stub  (trampoline symbol)
2269    *     foo:   code for foo (text symbol)
2270    *     ...
2271    *  Minimal symbol table for "shr1" image:
2272    *     ...
2273    *     shr1: code for shr1 (text symbol)
2274    *     ...
2275    *
2276    * So the code below is trying to detect if we are in the stub
2277    * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
2278    * and if found,  do the symbolization from the real-code address
2279    * rather than the stub address.
2280    *
2281    * Assumptions being made about the minimal symbol table:
2282    *   1. lookup_minimal_symbol_by_pc() will return a trampoline only
2283    *      if we're really in the trampoline.s If we're beyond it (say
2284    *      we're in "foo" in the above example), it'll have a closer
2285    *      symbol (the "foo" text symbol for example) and will not
2286    *      return the trampoline.
2287    *   2. lookup_minimal_symbol_text() will find a real text symbol
2288    *      corresponding to the trampoline, and whose address will
2289    *      be different than the trampoline address.  I put in a sanity
2290    *      check for the address being the same, to avoid an
2291    *      infinite recursion.
2292    */
2293   msymbol = lookup_minimal_symbol_by_pc (pc);
2294   if (msymbol != NULL)
2295     if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
2296       {
2297         mfunsym = lookup_minimal_symbol_text (SYMBOL_LINKAGE_NAME (msymbol),
2298                                               NULL);
2299         if (mfunsym == NULL)
2300           /* I eliminated this warning since it is coming out
2301            * in the following situation:
2302            * gdb shmain // test program with shared libraries
2303            * (gdb) break shr1  // function in shared lib
2304            * Warning: In stub for ...
2305            * In the above situation, the shared lib is not loaded yet,
2306            * so of course we can't find the real func/line info,
2307            * but the "break" still works, and the warning is annoying.
2308            * So I commented out the warning.  RT */
2309           /* warning ("In stub for %s; unable to find real function/line info",
2310              SYMBOL_LINKAGE_NAME (msymbol)); */
2311           ;
2312         /* fall through */
2313         else if (SYMBOL_VALUE_ADDRESS (mfunsym)
2314                  == SYMBOL_VALUE_ADDRESS (msymbol))
2315           /* Avoid infinite recursion */
2316           /* See above comment about why warning is commented out.  */
2317           /* warning ("In stub for %s; unable to find real function/line info",
2318              SYMBOL_LINKAGE_NAME (msymbol)); */
2319           ;
2320         /* fall through */
2321         else
2322           return find_pc_line (SYMBOL_VALUE_ADDRESS (mfunsym), 0);
2323       }
2324
2325
2326   s = find_pc_sect_symtab (pc, section);
2327   if (!s)
2328     {
2329       /* If no symbol information, return previous pc.  */
2330       if (notcurrent)
2331         pc++;
2332       val.pc = pc;
2333       return val;
2334     }
2335
2336   bv = BLOCKVECTOR (s);
2337   objfile = s->objfile;
2338
2339   /* Look at all the symtabs that share this blockvector.
2340      They all have the same apriori range, that we found was right;
2341      but they have different line tables.  */
2342
2343   ALL_OBJFILE_SYMTABS (objfile, s)
2344     {
2345       if (BLOCKVECTOR (s) != bv)
2346         continue;
2347
2348       /* Find the best line in this symtab.  */
2349       l = LINETABLE (s);
2350       if (!l)
2351         continue;
2352       len = l->nitems;
2353       if (len <= 0)
2354         {
2355           /* I think len can be zero if the symtab lacks line numbers
2356              (e.g. gcc -g1).  (Either that or the LINETABLE is NULL;
2357              I'm not sure which, and maybe it depends on the symbol
2358              reader).  */
2359           continue;
2360         }
2361
2362       prev = NULL;
2363       item = l->item;           /* Get first line info.  */
2364
2365       /* Is this file's first line closer than the first lines of other files?
2366          If so, record this file, and its first line, as best alternate.  */
2367       if (item->pc > pc && (!alt || item->pc < alt->pc))
2368         alt = item;
2369
2370       for (i = 0; i < len; i++, item++)
2371         {
2372           /* Leave prev pointing to the linetable entry for the last line
2373              that started at or before PC.  */
2374           if (item->pc > pc)
2375             break;
2376
2377           prev = item;
2378         }
2379
2380       /* At this point, prev points at the line whose start addr is <= pc, and
2381          item points at the next line.  If we ran off the end of the linetable
2382          (pc >= start of the last line), then prev == item.  If pc < start of
2383          the first line, prev will not be set.  */
2384
2385       /* Is this file's best line closer than the best in the other files?
2386          If so, record this file, and its best line, as best so far.  Don't
2387          save prev if it represents the end of a function (i.e. line number
2388          0) instead of a real line.  */
2389
2390       if (prev && prev->line && (!best || prev->pc > best->pc))
2391         {
2392           best = prev;
2393           best_symtab = s;
2394
2395           /* Discard BEST_END if it's before the PC of the current BEST.  */
2396           if (best_end <= best->pc)
2397             best_end = 0;
2398         }
2399
2400       /* If another line (denoted by ITEM) is in the linetable and its
2401          PC is after BEST's PC, but before the current BEST_END, then
2402          use ITEM's PC as the new best_end.  */
2403       if (best && i < len && item->pc > best->pc
2404           && (best_end == 0 || best_end > item->pc))
2405         best_end = item->pc;
2406     }
2407
2408   if (!best_symtab)
2409     {
2410       /* If we didn't find any line number info, just return zeros.
2411          We used to return alt->line - 1 here, but that could be
2412          anywhere; if we don't have line number info for this PC,
2413          don't make some up.  */
2414       val.pc = pc;
2415     }
2416   else if (best->line == 0)
2417     {
2418       /* If our best fit is in a range of PC's for which no line
2419          number info is available (line number is zero) then we didn't
2420          find any valid line information.  */
2421       val.pc = pc;
2422     }
2423   else
2424     {
2425       val.symtab = best_symtab;
2426       val.line = best->line;
2427       val.pc = best->pc;
2428       if (best_end && (!alt || best_end < alt->pc))
2429         val.end = best_end;
2430       else if (alt)
2431         val.end = alt->pc;
2432       else
2433         val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2434     }
2435   val.section = section;
2436   return val;
2437 }
2438
2439 /* Backward compatibility (no section).  */
2440
2441 struct symtab_and_line
2442 find_pc_line (CORE_ADDR pc, int notcurrent)
2443 {
2444   struct obj_section *section;
2445
2446   section = find_pc_overlay (pc);
2447   if (pc_in_unmapped_range (pc, section))
2448     pc = overlay_mapped_address (pc, section);
2449   return find_pc_sect_line (pc, section, notcurrent);
2450 }
2451 \f
2452 /* Find line number LINE in any symtab whose name is the same as
2453    SYMTAB.
2454
2455    If found, return the symtab that contains the linetable in which it was
2456    found, set *INDEX to the index in the linetable of the best entry
2457    found, and set *EXACT_MATCH nonzero if the value returned is an
2458    exact match.
2459
2460    If not found, return NULL.  */
2461
2462 struct symtab *
2463 find_line_symtab (struct symtab *symtab, int line,
2464                   int *index, int *exact_match)
2465 {
2466   int exact = 0;  /* Initialized here to avoid a compiler warning.  */
2467
2468   /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
2469      so far seen.  */
2470
2471   int best_index;
2472   struct linetable *best_linetable;
2473   struct symtab *best_symtab;
2474
2475   /* First try looking it up in the given symtab.  */
2476   best_linetable = LINETABLE (symtab);
2477   best_symtab = symtab;
2478   best_index = find_line_common (best_linetable, line, &exact, 0);
2479   if (best_index < 0 || !exact)
2480     {
2481       /* Didn't find an exact match.  So we better keep looking for
2482          another symtab with the same name.  In the case of xcoff,
2483          multiple csects for one source file (produced by IBM's FORTRAN
2484          compiler) produce multiple symtabs (this is unavoidable
2485          assuming csects can be at arbitrary places in memory and that
2486          the GLOBAL_BLOCK of a symtab has a begin and end address).  */
2487
2488       /* BEST is the smallest linenumber > LINE so far seen,
2489          or 0 if none has been seen so far.
2490          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
2491       int best;
2492
2493       struct objfile *objfile;
2494       struct symtab *s;
2495
2496       if (best_index >= 0)
2497         best = best_linetable->item[best_index].line;
2498       else
2499         best = 0;
2500
2501       ALL_OBJFILES (objfile)
2502       {
2503         if (objfile->sf)
2504           objfile->sf->qf->expand_symtabs_with_fullname (objfile,
2505                                                          symtab->filename);
2506       }
2507
2508       ALL_SYMTABS (objfile, s)
2509       {
2510         struct linetable *l;
2511         int ind;
2512
2513         if (FILENAME_CMP (symtab->filename, s->filename) != 0)
2514           continue;
2515         if (FILENAME_CMP (symtab_to_fullname (symtab),
2516                           symtab_to_fullname (s)) != 0)
2517           continue;     
2518         l = LINETABLE (s);
2519         ind = find_line_common (l, line, &exact, 0);
2520         if (ind >= 0)
2521           {
2522             if (exact)
2523               {
2524                 best_index = ind;
2525                 best_linetable = l;
2526                 best_symtab = s;
2527                 goto done;
2528               }
2529             if (best == 0 || l->item[ind].line < best)
2530               {
2531                 best = l->item[ind].line;
2532                 best_index = ind;
2533                 best_linetable = l;
2534                 best_symtab = s;
2535               }
2536           }
2537       }
2538     }
2539 done:
2540   if (best_index < 0)
2541     return NULL;
2542
2543   if (index)
2544     *index = best_index;
2545   if (exact_match)
2546     *exact_match = exact;
2547
2548   return best_symtab;
2549 }
2550
2551 /* Given SYMTAB, returns all the PCs function in the symtab that
2552    exactly match LINE.  Returns NULL if there are no exact matches,
2553    but updates BEST_ITEM in this case.  */
2554
2555 VEC (CORE_ADDR) *
2556 find_pcs_for_symtab_line (struct symtab *symtab, int line,
2557                           struct linetable_entry **best_item)
2558 {
2559   int start = 0;
2560   VEC (CORE_ADDR) *result = NULL;
2561
2562   /* First, collect all the PCs that are at this line.  */
2563   while (1)
2564     {
2565       int was_exact;
2566       int idx;
2567
2568       idx = find_line_common (LINETABLE (symtab), line, &was_exact, start);
2569       if (idx < 0)
2570         break;
2571
2572       if (!was_exact)
2573         {
2574           struct linetable_entry *item = &LINETABLE (symtab)->item[idx];
2575
2576           if (*best_item == NULL || item->line < (*best_item)->line)
2577             *best_item = item;
2578
2579           break;
2580         }
2581
2582       VEC_safe_push (CORE_ADDR, result, LINETABLE (symtab)->item[idx].pc);
2583       start = idx + 1;
2584     }
2585
2586   return result;
2587 }
2588
2589 \f
2590 /* Set the PC value for a given source file and line number and return true.
2591    Returns zero for invalid line number (and sets the PC to 0).
2592    The source file is specified with a struct symtab.  */
2593
2594 int
2595 find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
2596 {
2597   struct linetable *l;
2598   int ind;
2599
2600   *pc = 0;
2601   if (symtab == 0)
2602     return 0;
2603
2604   symtab = find_line_symtab (symtab, line, &ind, NULL);
2605   if (symtab != NULL)
2606     {
2607       l = LINETABLE (symtab);
2608       *pc = l->item[ind].pc;
2609       return 1;
2610     }
2611   else
2612     return 0;
2613 }
2614
2615 /* Find the range of pc values in a line.
2616    Store the starting pc of the line into *STARTPTR
2617    and the ending pc (start of next line) into *ENDPTR.
2618    Returns 1 to indicate success.
2619    Returns 0 if could not find the specified line.  */
2620
2621 int
2622 find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
2623                     CORE_ADDR *endptr)
2624 {
2625   CORE_ADDR startaddr;
2626   struct symtab_and_line found_sal;
2627
2628   startaddr = sal.pc;
2629   if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
2630     return 0;
2631
2632   /* This whole function is based on address.  For example, if line 10 has
2633      two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
2634      "info line *0x123" should say the line goes from 0x100 to 0x200
2635      and "info line *0x355" should say the line goes from 0x300 to 0x400.
2636      This also insures that we never give a range like "starts at 0x134
2637      and ends at 0x12c".  */
2638
2639   found_sal = find_pc_sect_line (startaddr, sal.section, 0);
2640   if (found_sal.line != sal.line)
2641     {
2642       /* The specified line (sal) has zero bytes.  */
2643       *startptr = found_sal.pc;
2644       *endptr = found_sal.pc;
2645     }
2646   else
2647     {
2648       *startptr = found_sal.pc;
2649       *endptr = found_sal.end;
2650     }
2651   return 1;
2652 }
2653
2654 /* Given a line table and a line number, return the index into the line
2655    table for the pc of the nearest line whose number is >= the specified one.
2656    Return -1 if none is found.  The value is >= 0 if it is an index.
2657    START is the index at which to start searching the line table.
2658
2659    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
2660
2661 static int
2662 find_line_common (struct linetable *l, int lineno,
2663                   int *exact_match, int start)
2664 {
2665   int i;
2666   int len;
2667
2668   /* BEST is the smallest linenumber > LINENO so far seen,
2669      or 0 if none has been seen so far.
2670      BEST_INDEX identifies the item for it.  */
2671
2672   int best_index = -1;
2673   int best = 0;
2674
2675   *exact_match = 0;
2676
2677   if (lineno <= 0)
2678     return -1;
2679   if (l == 0)
2680     return -1;
2681
2682   len = l->nitems;
2683   for (i = start; i < len; i++)
2684     {
2685       struct linetable_entry *item = &(l->item[i]);
2686
2687       if (item->line == lineno)
2688         {
2689           /* Return the first (lowest address) entry which matches.  */
2690           *exact_match = 1;
2691           return i;
2692         }
2693
2694       if (item->line > lineno && (best == 0 || item->line < best))
2695         {
2696           best = item->line;
2697           best_index = i;
2698         }
2699     }
2700
2701   /* If we got here, we didn't get an exact match.  */
2702   return best_index;
2703 }
2704
2705 int
2706 find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
2707 {
2708   struct symtab_and_line sal;
2709
2710   sal = find_pc_line (pc, 0);
2711   *startptr = sal.pc;
2712   *endptr = sal.end;
2713   return sal.symtab != 0;
2714 }
2715
2716 /* Given a function start address FUNC_ADDR and SYMTAB, find the first
2717    address for that function that has an entry in SYMTAB's line info
2718    table.  If such an entry cannot be found, return FUNC_ADDR
2719    unaltered.  */
2720
2721 static CORE_ADDR
2722 skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
2723 {
2724   CORE_ADDR func_start, func_end;
2725   struct linetable *l;
2726   int i;
2727
2728   /* Give up if this symbol has no lineinfo table.  */
2729   l = LINETABLE (symtab);
2730   if (l == NULL)
2731     return func_addr;
2732
2733   /* Get the range for the function's PC values, or give up if we
2734      cannot, for some reason.  */
2735   if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
2736     return func_addr;
2737
2738   /* Linetable entries are ordered by PC values, see the commentary in
2739      symtab.h where `struct linetable' is defined.  Thus, the first
2740      entry whose PC is in the range [FUNC_START..FUNC_END[ is the
2741      address we are looking for.  */
2742   for (i = 0; i < l->nitems; i++)
2743     {
2744       struct linetable_entry *item = &(l->item[i]);
2745
2746       /* Don't use line numbers of zero, they mark special entries in
2747          the table.  See the commentary on symtab.h before the
2748          definition of struct linetable.  */
2749       if (item->line > 0 && func_start <= item->pc && item->pc < func_end)
2750         return item->pc;
2751     }
2752
2753   return func_addr;
2754 }
2755
2756 /* Given a function symbol SYM, find the symtab and line for the start
2757    of the function.
2758    If the argument FUNFIRSTLINE is nonzero, we want the first line
2759    of real code inside the function.  */
2760
2761 struct symtab_and_line
2762 find_function_start_sal (struct symbol *sym, int funfirstline)
2763 {
2764   struct symtab_and_line sal;
2765
2766   fixup_symbol_section (sym, NULL);
2767   sal = find_pc_sect_line (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)),
2768                            SYMBOL_OBJ_SECTION (sym), 0);
2769
2770   /* We always should have a line for the function start address.
2771      If we don't, something is odd.  Create a plain SAL refering
2772      just the PC and hope that skip_prologue_sal (if requested)
2773      can find a line number for after the prologue.  */
2774   if (sal.pc < BLOCK_START (SYMBOL_BLOCK_VALUE (sym)))
2775     {
2776       init_sal (&sal);
2777       sal.pspace = current_program_space;
2778       sal.pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2779       sal.section = SYMBOL_OBJ_SECTION (sym);
2780     }
2781
2782   if (funfirstline)
2783     skip_prologue_sal (&sal);
2784
2785   return sal;
2786 }
2787
2788 /* Adjust SAL to the first instruction past the function prologue.
2789    If the PC was explicitly specified, the SAL is not changed.
2790    If the line number was explicitly specified, at most the SAL's PC
2791    is updated.  If SAL is already past the prologue, then do nothing.  */
2792
2793 void
2794 skip_prologue_sal (struct symtab_and_line *sal)
2795 {
2796   struct symbol *sym;
2797   struct symtab_and_line start_sal;
2798   struct cleanup *old_chain;
2799   CORE_ADDR pc, saved_pc;
2800   struct obj_section *section;
2801   const char *name;
2802   struct objfile *objfile;
2803   struct gdbarch *gdbarch;
2804   struct block *b, *function_block;
2805   int force_skip, skip;
2806
2807   /* Do not change the SAL if PC was specified explicitly.  */
2808   if (sal->explicit_pc)
2809     return;
2810
2811   old_chain = save_current_space_and_thread ();
2812   switch_to_program_space_and_thread (sal->pspace);
2813
2814   sym = find_pc_sect_function (sal->pc, sal->section);
2815   if (sym != NULL)
2816     {
2817       fixup_symbol_section (sym, NULL);
2818
2819       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2820       section = SYMBOL_OBJ_SECTION (sym);
2821       name = SYMBOL_LINKAGE_NAME (sym);
2822       objfile = SYMBOL_SYMTAB (sym)->objfile;
2823     }
2824   else
2825     {
2826       struct minimal_symbol *msymbol
2827         = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);
2828
2829       if (msymbol == NULL)
2830         {
2831           do_cleanups (old_chain);
2832           return;
2833         }
2834
2835       pc = SYMBOL_VALUE_ADDRESS (msymbol);
2836       section = SYMBOL_OBJ_SECTION (msymbol);
2837       name = SYMBOL_LINKAGE_NAME (msymbol);
2838       objfile = msymbol_objfile (msymbol);
2839     }
2840
2841   gdbarch = get_objfile_arch (objfile);
2842
2843   /* Process the prologue in two passes.  In the first pass try to skip the
2844      prologue (SKIP is true) and verify there is a real need for it (indicated
2845      by FORCE_SKIP).  If no such reason was found run a second pass where the
2846      prologue is not skipped (SKIP is false).  */
2847
2848   skip = 1;
2849   force_skip = 1;
2850
2851   /* Be conservative - allow direct PC (without skipping prologue) only if we
2852      have proven the CU (Compilation Unit) supports it.  sal->SYMTAB does not
2853      have to be set by the caller so we use SYM instead.  */
2854   if (sym && SYMBOL_SYMTAB (sym)->locations_valid)
2855     force_skip = 0;
2856
2857   saved_pc = pc;
2858   do
2859     {
2860       pc = saved_pc;
2861
2862       /* If the function is in an unmapped overlay, use its unmapped LMA address,
2863          so that gdbarch_skip_prologue has something unique to work on.  */
2864       if (section_is_overlay (section) && !section_is_mapped (section))
2865         pc = overlay_unmapped_address (pc, section);
2866
2867       /* Skip "first line" of function (which is actually its prologue).  */
2868       pc += gdbarch_deprecated_function_start_offset (gdbarch);
2869       if (skip)
2870         pc = gdbarch_skip_prologue (gdbarch, pc);
2871
2872       /* For overlays, map pc back into its mapped VMA range.  */
2873       pc = overlay_mapped_address (pc, section);
2874
2875       /* Calculate line number.  */
2876       start_sal = find_pc_sect_line (pc, section, 0);
2877
2878       /* Check if gdbarch_skip_prologue left us in mid-line, and the next
2879          line is still part of the same function.  */
2880       if (skip && start_sal.pc != pc
2881           && (sym ? (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= start_sal.end
2882                      && start_sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2883               : (lookup_minimal_symbol_by_pc_section (start_sal.end, section)
2884                  == lookup_minimal_symbol_by_pc_section (pc, section))))
2885         {
2886           /* First pc of next line */
2887           pc = start_sal.end;
2888           /* Recalculate the line number (might not be N+1).  */
2889           start_sal = find_pc_sect_line (pc, section, 0);
2890         }
2891
2892       /* On targets with executable formats that don't have a concept of
2893          constructors (ELF with .init has, PE doesn't), gcc emits a call
2894          to `__main' in `main' between the prologue and before user
2895          code.  */
2896       if (gdbarch_skip_main_prologue_p (gdbarch)
2897           && name && strcmp_iw (name, "main") == 0)
2898         {
2899           pc = gdbarch_skip_main_prologue (gdbarch, pc);
2900           /* Recalculate the line number (might not be N+1).  */
2901           start_sal = find_pc_sect_line (pc, section, 0);
2902           force_skip = 1;
2903         }
2904     }
2905   while (!force_skip && skip--);
2906
2907   /* If we still don't have a valid source line, try to find the first
2908      PC in the lineinfo table that belongs to the same function.  This
2909      happens with COFF debug info, which does not seem to have an
2910      entry in lineinfo table for the code after the prologue which has
2911      no direct relation to source.  For example, this was found to be
2912      the case with the DJGPP target using "gcc -gcoff" when the
2913      compiler inserted code after the prologue to make sure the stack
2914      is aligned.  */
2915   if (!force_skip && sym && start_sal.symtab == NULL)
2916     {
2917       pc = skip_prologue_using_lineinfo (pc, SYMBOL_SYMTAB (sym));
2918       /* Recalculate the line number.  */
2919       start_sal = find_pc_sect_line (pc, section, 0);
2920     }
2921
2922   do_cleanups (old_chain);
2923
2924   /* If we're already past the prologue, leave SAL unchanged.  Otherwise
2925      forward SAL to the end of the prologue.  */
2926   if (sal->pc >= pc)
2927     return;
2928
2929   sal->pc = pc;
2930   sal->section = section;
2931
2932   /* Unless the explicit_line flag was set, update the SAL line
2933      and symtab to correspond to the modified PC location.  */
2934   if (sal->explicit_line)
2935     return;
2936
2937   sal->symtab = start_sal.symtab;
2938   sal->line = start_sal.line;
2939   sal->end = start_sal.end;
2940
2941   /* Check if we are now inside an inlined function.  If we can,
2942      use the call site of the function instead.  */
2943   b = block_for_pc_sect (sal->pc, sal->section);
2944   function_block = NULL;
2945   while (b != NULL)
2946     {
2947       if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
2948         function_block = b;
2949       else if (BLOCK_FUNCTION (b) != NULL)
2950         break;
2951       b = BLOCK_SUPERBLOCK (b);
2952     }
2953   if (function_block != NULL
2954       && SYMBOL_LINE (BLOCK_FUNCTION (function_block)) != 0)
2955     {
2956       sal->line = SYMBOL_LINE (BLOCK_FUNCTION (function_block));
2957       sal->symtab = SYMBOL_SYMTAB (BLOCK_FUNCTION (function_block));
2958     }
2959 }
2960
2961 /* If P is of the form "operator[ \t]+..." where `...' is
2962    some legitimate operator text, return a pointer to the
2963    beginning of the substring of the operator text.
2964    Otherwise, return "".  */
2965
2966 static char *
2967 operator_chars (char *p, char **end)
2968 {
2969   *end = "";
2970   if (strncmp (p, "operator", 8))
2971     return *end;
2972   p += 8;
2973
2974   /* Don't get faked out by `operator' being part of a longer
2975      identifier.  */
2976   if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
2977     return *end;
2978
2979   /* Allow some whitespace between `operator' and the operator symbol.  */
2980   while (*p == ' ' || *p == '\t')
2981     p++;
2982
2983   /* Recognize 'operator TYPENAME'.  */
2984
2985   if (isalpha (*p) || *p == '_' || *p == '$')
2986     {
2987       char *q = p + 1;
2988
2989       while (isalnum (*q) || *q == '_' || *q == '$')
2990         q++;
2991       *end = q;
2992       return p;
2993     }
2994
2995   while (*p)
2996     switch (*p)
2997       {
2998       case '\\':                        /* regexp quoting */
2999         if (p[1] == '*')
3000           {
3001             if (p[2] == '=')            /* 'operator\*=' */
3002               *end = p + 3;
3003             else                        /* 'operator\*'  */
3004               *end = p + 2;
3005             return p;
3006           }
3007         else if (p[1] == '[')
3008           {
3009             if (p[2] == ']')
3010               error (_("mismatched quoting on brackets, "
3011                        "try 'operator\\[\\]'"));
3012             else if (p[2] == '\\' && p[3] == ']')
3013               {
3014                 *end = p + 4;   /* 'operator\[\]' */
3015                 return p;
3016               }
3017             else
3018               error (_("nothing is allowed between '[' and ']'"));
3019           }
3020         else
3021           {
3022             /* Gratuitous qoute: skip it and move on.  */
3023             p++;
3024             continue;
3025           }
3026         break;
3027       case '!':
3028       case '=':
3029       case '*':
3030       case '/':
3031       case '%':
3032       case '^':
3033         if (p[1] == '=')
3034           *end = p + 2;
3035         else
3036           *end = p + 1;
3037         return p;
3038       case '<':
3039       case '>':
3040       case '+':
3041       case '-':
3042       case '&':
3043       case '|':
3044         if (p[0] == '-' && p[1] == '>')
3045           {
3046             /* Struct pointer member operator 'operator->'.  */
3047             if (p[2] == '*')
3048               {
3049                 *end = p + 3;   /* 'operator->*' */
3050                 return p;
3051               }
3052             else if (p[2] == '\\')
3053               {
3054                 *end = p + 4;   /* Hopefully 'operator->\*' */
3055                 return p;
3056               }
3057             else
3058               {
3059                 *end = p + 2;   /* 'operator->' */
3060                 return p;
3061               }
3062           }
3063         if (p[1] == '=' || p[1] == p[0])
3064           *end = p + 2;
3065         else
3066           *end = p + 1;
3067         return p;
3068       case '~':
3069       case ',':
3070         *end = p + 1;
3071         return p;
3072       case '(':
3073         if (p[1] != ')')
3074           error (_("`operator ()' must be specified "
3075                    "without whitespace in `()'"));
3076         *end = p + 2;
3077         return p;
3078       case '?':
3079         if (p[1] != ':')
3080           error (_("`operator ?:' must be specified "
3081                    "without whitespace in `?:'"));
3082         *end = p + 2;
3083         return p;
3084       case '[':
3085         if (p[1] != ']')
3086           error (_("`operator []' must be specified "
3087                    "without whitespace in `[]'"));
3088         *end = p + 2;
3089         return p;
3090       default:
3091         error (_("`operator %s' not supported"), p);
3092         break;
3093       }
3094
3095   *end = "";
3096   return *end;
3097 }
3098 \f
3099
3100 /* Cache to watch for file names already seen by filename_seen.  */
3101
3102 struct filename_seen_cache
3103 {
3104   /* Table of files seen so far.  */
3105   htab_t tab;
3106   /* Initial size of the table.  It automagically grows from here.  */
3107 #define INITIAL_FILENAME_SEEN_CACHE_SIZE 100
3108 };
3109
3110 /* filename_seen_cache constructor.  */
3111
3112 static struct filename_seen_cache *
3113 create_filename_seen_cache (void)
3114 {
3115   struct filename_seen_cache *cache;
3116
3117   cache = XNEW (struct filename_seen_cache);
3118   cache->tab = htab_create_alloc (INITIAL_FILENAME_SEEN_CACHE_SIZE,
3119                                   filename_hash, filename_eq,
3120                                   NULL, xcalloc, xfree);
3121
3122   return cache;
3123 }
3124
3125 /* Empty the cache, but do not delete it.  */
3126
3127 static void
3128 clear_filename_seen_cache (struct filename_seen_cache *cache)
3129 {
3130   htab_empty (cache->tab);
3131 }
3132
3133 /* filename_seen_cache destructor.
3134    This takes a void * argument as it is generally used as a cleanup.  */
3135
3136 static void
3137 delete_filename_seen_cache (void *ptr)
3138 {
3139   struct filename_seen_cache *cache = ptr;
3140
3141   htab_delete (cache->tab);
3142   xfree (cache);
3143 }
3144
3145 /* If FILE is not already in the table of files in CACHE, return zero;
3146    otherwise return non-zero.  Optionally add FILE to the table if ADD
3147    is non-zero.
3148
3149    NOTE: We don't manage space for FILE, we assume FILE lives as long
3150    as the caller needs.  */
3151
3152 static int
3153 filename_seen (struct filename_seen_cache *cache, const char *file, int add)
3154 {
3155   void **slot;
3156
3157   /* Is FILE in tab?  */
3158   slot = htab_find_slot (cache->tab, file, add ? INSERT : NO_INSERT);
3159   if (*slot != NULL)
3160     return 1;
3161
3162   /* No; maybe add it to tab.  */
3163   if (add)
3164     *slot = (char *) file;
3165
3166   return 0;
3167 }
3168
3169 /* Data structure to maintain printing state for output_source_filename.  */
3170
3171 struct output_source_filename_data
3172 {
3173   /* Cache of what we've seen so far.  */
3174   struct filename_seen_cache *filename_seen_cache;
3175
3176   /* Flag of whether we're printing the first one.  */
3177   int first;
3178 };
3179
3180 /* Slave routine for sources_info.  Force line breaks at ,'s.
3181    NAME is the name to print.
3182    DATA contains the state for printing and watching for duplicates.  */
3183
3184 static void
3185 output_source_filename (const char *name,
3186                         struct output_source_filename_data *data)
3187 {
3188   /* Since a single source file can result in several partial symbol
3189      tables, we need to avoid printing it more than once.  Note: if
3190      some of the psymtabs are read in and some are not, it gets
3191      printed both under "Source files for which symbols have been
3192      read" and "Source files for which symbols will be read in on
3193      demand".  I consider this a reasonable way to deal with the
3194      situation.  I'm not sure whether this can also happen for
3195      symtabs; it doesn't hurt to check.  */
3196
3197   /* Was NAME already seen?  */
3198   if (filename_seen (data->filename_seen_cache, name, 1))
3199     {
3200       /* Yes; don't print it again.  */
3201       return;
3202     }
3203
3204   /* No; print it and reset *FIRST.  */
3205   if (! data->first)
3206     printf_filtered (", ");
3207   data->first = 0;
3208
3209   wrap_here ("");
3210   fputs_filtered (name, gdb_stdout);
3211 }
3212
3213 /* A callback for map_partial_symbol_filenames.  */
3214
3215 static void
3216 output_partial_symbol_filename (const char *filename, const char *fullname,
3217                                 void *data)
3218 {
3219   output_source_filename (fullname ? fullname : filename, data);
3220 }
3221
3222 static void
3223 sources_info (char *ignore, int from_tty)
3224 {
3225   struct symtab *s;
3226   struct objfile *objfile;
3227   struct output_source_filename_data data;
3228   struct cleanup *cleanups;
3229
3230   if (!have_full_symbols () && !have_partial_symbols ())
3231     {
3232       error (_("No symbol table is loaded.  Use the \"file\" command."));
3233     }
3234
3235   data.filename_seen_cache = create_filename_seen_cache ();
3236   cleanups = make_cleanup (delete_filename_seen_cache,
3237                            data.filename_seen_cache);
3238
3239   printf_filtered ("Source files for which symbols have been read in:\n\n");
3240
3241   data.first = 1;
3242   ALL_SYMTABS (objfile, s)
3243   {
3244     const char *fullname = symtab_to_fullname (s);
3245
3246     output_source_filename (fullname, &data);
3247   }
3248   printf_filtered ("\n\n");
3249
3250   printf_filtered ("Source files for which symbols "
3251                    "will be read in on demand:\n\n");
3252
3253   clear_filename_seen_cache (data.filename_seen_cache);
3254   data.first = 1;
3255   map_partial_symbol_filenames (output_partial_symbol_filename, &data,
3256                                 1 /*need_fullname*/);
3257   printf_filtered ("\n");
3258
3259   do_cleanups (cleanups);
3260 }
3261
3262 static int
3263 file_matches (const char *file, char *files[], int nfiles)
3264 {
3265   int i;
3266
3267   if (file != NULL && nfiles != 0)
3268     {
3269       for (i = 0; i < nfiles; i++)
3270         {
3271           if (filename_cmp (files[i], lbasename (file)) == 0)
3272             return 1;
3273         }
3274     }
3275   else if (nfiles == 0)
3276     return 1;
3277   return 0;
3278 }
3279
3280 /* Free any memory associated with a search.  */
3281
3282 void
3283 free_search_symbols (struct symbol_search *symbols)
3284 {
3285   struct symbol_search *p;
3286   struct symbol_search *next;
3287
3288   for (p = symbols; p != NULL; p = next)
3289     {
3290       next = p->next;
3291       xfree (p);
3292     }
3293 }
3294
3295 static void
3296 do_free_search_symbols_cleanup (void *symbols)
3297 {
3298   free_search_symbols (symbols);
3299 }
3300
3301 struct cleanup *
3302 make_cleanup_free_search_symbols (struct symbol_search *symbols)
3303 {
3304   return make_cleanup (do_free_search_symbols_cleanup, symbols);
3305 }
3306
3307 /* Helper function for sort_search_symbols and qsort.  Can only
3308    sort symbols, not minimal symbols.  */
3309
3310 static int
3311 compare_search_syms (const void *sa, const void *sb)
3312 {
3313   struct symbol_search **sym_a = (struct symbol_search **) sa;
3314   struct symbol_search **sym_b = (struct symbol_search **) sb;
3315
3316   return strcmp (SYMBOL_PRINT_NAME ((*sym_a)->symbol),
3317                  SYMBOL_PRINT_NAME ((*sym_b)->symbol));
3318 }
3319
3320 /* Sort the ``nfound'' symbols in the list after prevtail.  Leave
3321    prevtail where it is, but update its next pointer to point to
3322    the first of the sorted symbols.  */
3323
3324 static struct symbol_search *
3325 sort_search_symbols (struct symbol_search *prevtail, int nfound)
3326 {
3327   struct symbol_search **symbols, *symp, *old_next;
3328   int i;
3329
3330   symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
3331                                                * nfound);
3332   symp = prevtail->next;
3333   for (i = 0; i < nfound; i++)
3334     {
3335       symbols[i] = symp;
3336       symp = symp->next;
3337     }
3338   /* Generally NULL.  */
3339   old_next = symp;
3340
3341   qsort (symbols, nfound, sizeof (struct symbol_search *),
3342          compare_search_syms);
3343
3344   symp = prevtail;
3345   for (i = 0; i < nfound; i++)
3346     {
3347       symp->next = symbols[i];
3348       symp = symp->next;
3349     }
3350   symp->next = old_next;
3351
3352   xfree (symbols);
3353   return symp;
3354 }
3355
3356 /* An object of this type is passed as the user_data to the
3357    expand_symtabs_matching method.  */
3358 struct search_symbols_data
3359 {
3360   int nfiles;
3361   char **files;
3362
3363   /* It is true if PREG contains valid data, false otherwise.  */
3364   unsigned preg_p : 1;
3365   regex_t preg;
3366 };
3367
3368 /* A callback for expand_symtabs_matching.  */
3369
3370 static int
3371 search_symbols_file_matches (const char *filename, void *user_data)
3372 {
3373   struct search_symbols_data *data = user_data;
3374
3375   return file_matches (filename, data->files, data->nfiles);
3376 }
3377
3378 /* A callback for expand_symtabs_matching.  */
3379
3380 static int
3381 search_symbols_name_matches (const char *symname, void *user_data)
3382 {
3383   struct search_symbols_data *data = user_data;
3384
3385   return !data->preg_p || regexec (&data->preg, symname, 0, NULL, 0) == 0;
3386 }
3387
3388 /* Search the symbol table for matches to the regular expression REGEXP,
3389    returning the results in *MATCHES.
3390
3391    Only symbols of KIND are searched:
3392    VARIABLES_DOMAIN - search all symbols, excluding functions, type names,
3393                       and constants (enums)
3394    FUNCTIONS_DOMAIN - search all functions
3395    TYPES_DOMAIN     - search all type names
3396    ALL_DOMAIN       - an internal error for this function
3397
3398    free_search_symbols should be called when *MATCHES is no longer needed.
3399
3400    The results are sorted locally; each symtab's global and static blocks are
3401    separately alphabetized.  */
3402
3403 void
3404 search_symbols (char *regexp, enum search_domain kind,
3405                 int nfiles, char *files[],
3406                 struct symbol_search **matches)
3407 {
3408   struct symtab *s;
3409   struct blockvector *bv;
3410   struct block *b;
3411   int i = 0;
3412   struct block_iterator iter;
3413   struct symbol *sym;
3414   struct objfile *objfile;
3415   struct minimal_symbol *msymbol;
3416   int found_misc = 0;
3417   static const enum minimal_symbol_type types[]
3418     = {mst_data, mst_text, mst_abs};
3419   static const enum minimal_symbol_type types2[]
3420     = {mst_bss, mst_file_text, mst_abs};
3421   static const enum minimal_symbol_type types3[]
3422     = {mst_file_data, mst_solib_trampoline, mst_abs};
3423   static const enum minimal_symbol_type types4[]
3424     = {mst_file_bss, mst_text_gnu_ifunc, mst_abs};
3425   enum minimal_symbol_type ourtype;
3426   enum minimal_symbol_type ourtype2;
3427   enum minimal_symbol_type ourtype3;
3428   enum minimal_symbol_type ourtype4;
3429   struct symbol_search *sr;
3430   struct symbol_search *psr;
3431   struct symbol_search *tail;
3432   struct search_symbols_data datum;
3433
3434   /* OLD_CHAIN .. RETVAL_CHAIN is always freed, RETVAL_CHAIN .. current
3435      CLEANUP_CHAIN is freed only in the case of an error.  */
3436   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
3437   struct cleanup *retval_chain;
3438
3439   gdb_assert (kind <= TYPES_DOMAIN);
3440
3441   ourtype = types[kind];
3442   ourtype2 = types2[kind];
3443   ourtype3 = types3[kind];
3444   ourtype4 = types4[kind];
3445
3446   sr = *matches = NULL;
3447   tail = NULL;
3448   datum.preg_p = 0;
3449
3450   if (regexp != NULL)
3451     {
3452       /* Make sure spacing is right for C++ operators.
3453          This is just a courtesy to make the matching less sensitive
3454          to how many spaces the user leaves between 'operator'
3455          and <TYPENAME> or <OPERATOR>.  */
3456       char *opend;
3457       char *opname = operator_chars (regexp, &opend);
3458       int errcode;
3459
3460       if (*opname)
3461         {
3462           int fix = -1;         /* -1 means ok; otherwise number of
3463                                     spaces needed.  */
3464
3465           if (isalpha (*opname) || *opname == '_' || *opname == '$')
3466             {
3467               /* There should 1 space between 'operator' and 'TYPENAME'.  */
3468               if (opname[-1] != ' ' || opname[-2] == ' ')
3469                 fix = 1;
3470             }
3471           else
3472             {
3473               /* There should 0 spaces between 'operator' and 'OPERATOR'.  */
3474               if (opname[-1] == ' ')
3475                 fix = 0;
3476             }
3477           /* If wrong number of spaces, fix it.  */
3478           if (fix >= 0)
3479             {
3480               char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
3481
3482               sprintf (tmp, "operator%.*s%s", fix, " ", opname);
3483               regexp = tmp;
3484             }
3485         }
3486
3487       errcode = regcomp (&datum.preg, regexp,
3488                          REG_NOSUB | (case_sensitivity == case_sensitive_off
3489                                       ? REG_ICASE : 0));
3490       if (errcode != 0)
3491         {
3492           char *err = get_regcomp_error (errcode, &datum.preg);
3493
3494           make_cleanup (xfree, err);
3495           error (_("Invalid regexp (%s): %s"), err, regexp);
3496         }
3497       datum.preg_p = 1;
3498       make_regfree_cleanup (&datum.preg);
3499     }
3500
3501   /* Search through the partial symtabs *first* for all symbols
3502      matching the regexp.  That way we don't have to reproduce all of
3503      the machinery below.  */
3504
3505   datum.nfiles = nfiles;
3506   datum.files = files;
3507   ALL_OBJFILES (objfile)
3508   {
3509     if (objfile->sf)
3510       objfile->sf->qf->expand_symtabs_matching (objfile,
3511                                                 (nfiles == 0
3512                                                  ? NULL
3513                                                  : search_symbols_file_matches),
3514                                                 search_symbols_name_matches,
3515                                                 kind,
3516                                                 &datum);
3517   }
3518
3519   retval_chain = old_chain;
3520
3521   /* Here, we search through the minimal symbol tables for functions
3522      and variables that match, and force their symbols to be read.
3523      This is in particular necessary for demangled variable names,
3524      which are no longer put into the partial symbol tables.
3525      The symbol will then be found during the scan of symtabs below.
3526
3527      For functions, find_pc_symtab should succeed if we have debug info
3528      for the function, for variables we have to call
3529      lookup_symbol_in_objfile_from_linkage_name to determine if the variable
3530      has debug info.
3531      If the lookup fails, set found_misc so that we will rescan to print
3532      any matching symbols without debug info.
3533      We only search the objfile the msymbol came from, we no longer search
3534      all objfiles.  In large programs (1000s of shared libs) searching all
3535      objfiles is not worth the pain.  */
3536
3537   if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
3538     {
3539       ALL_MSYMBOLS (objfile, msymbol)
3540       {
3541         QUIT;
3542
3543         if (msymbol->created_by_gdb)
3544           continue;
3545
3546         if (MSYMBOL_TYPE (msymbol) == ourtype
3547             || MSYMBOL_TYPE (msymbol) == ourtype2
3548             || MSYMBOL_TYPE (msymbol) == ourtype3
3549             || MSYMBOL_TYPE (msymbol) == ourtype4)
3550           {
3551             if (!datum.preg_p
3552                 || regexec (&datum.preg, SYMBOL_NATURAL_NAME (msymbol), 0,
3553                             NULL, 0) == 0)
3554               {
3555                 /* Note: An important side-effect of these lookup functions
3556                    is to expand the symbol table if msymbol is found, for the
3557                    benefit of the next loop on ALL_PRIMARY_SYMTABS.  */
3558                 if (kind == FUNCTIONS_DOMAIN
3559                     ? find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)) == NULL
3560                     : (lookup_symbol_in_objfile_from_linkage_name
3561                        (objfile, SYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
3562                        == NULL))
3563                   found_misc = 1;
3564               }
3565           }
3566       }
3567     }
3568
3569   ALL_PRIMARY_SYMTABS (objfile, s)
3570   {
3571     bv = BLOCKVECTOR (s);
3572     for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
3573       {
3574         struct symbol_search *prevtail = tail;
3575         int nfound = 0;
3576
3577         b = BLOCKVECTOR_BLOCK (bv, i);
3578         ALL_BLOCK_SYMBOLS (b, iter, sym)
3579           {
3580             struct symtab *real_symtab = SYMBOL_SYMTAB (sym);
3581
3582             QUIT;
3583
3584             if (file_matches (real_symtab->filename, files, nfiles)
3585                 && ((!datum.preg_p
3586                      || regexec (&datum.preg, SYMBOL_NATURAL_NAME (sym), 0,
3587                                  NULL, 0) == 0)
3588                     && ((kind == VARIABLES_DOMAIN
3589                          && SYMBOL_CLASS (sym) != LOC_TYPEDEF
3590                          && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
3591                          && SYMBOL_CLASS (sym) != LOC_BLOCK
3592                          /* LOC_CONST can be used for more than just enums,
3593                             e.g., c++ static const members.
3594                             We only want to skip enums here.  */
3595                          && !(SYMBOL_CLASS (sym) == LOC_CONST
3596                               && TYPE_CODE (SYMBOL_TYPE (sym))
3597                               == TYPE_CODE_ENUM))
3598                         || (kind == FUNCTIONS_DOMAIN 
3599                             && SYMBOL_CLASS (sym) == LOC_BLOCK)
3600                         || (kind == TYPES_DOMAIN
3601                             && SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
3602               {
3603                 /* match */
3604                 psr = (struct symbol_search *)
3605                   xmalloc (sizeof (struct symbol_search));
3606                 psr->block = i;
3607                 psr->symtab = real_symtab;
3608                 psr->symbol = sym;
3609                 psr->msymbol = NULL;
3610                 psr->next = NULL;
3611                 if (tail == NULL)
3612                   sr = psr;
3613                 else
3614                   tail->next = psr;
3615                 tail = psr;
3616                 nfound ++;
3617               }
3618           }
3619         if (nfound > 0)
3620           {
3621             if (prevtail == NULL)
3622               {
3623                 struct symbol_search dummy;
3624
3625                 dummy.next = sr;
3626                 tail = sort_search_symbols (&dummy, nfound);
3627                 sr = dummy.next;
3628
3629                 make_cleanup_free_search_symbols (sr);
3630               }
3631             else
3632               tail = sort_search_symbols (prevtail, nfound);
3633           }
3634       }
3635   }
3636
3637   /* If there are no eyes, avoid all contact.  I mean, if there are
3638      no debug symbols, then print directly from the msymbol_vector.  */
3639
3640   if (found_misc || (nfiles == 0 && kind != FUNCTIONS_DOMAIN))
3641     {
3642       ALL_MSYMBOLS (objfile, msymbol)
3643       {
3644         QUIT;
3645
3646         if (msymbol->created_by_gdb)
3647           continue;
3648
3649         if (MSYMBOL_TYPE (msymbol) == ourtype
3650             || MSYMBOL_TYPE (msymbol) == ourtype2
3651             || MSYMBOL_TYPE (msymbol) == ourtype3
3652             || MSYMBOL_TYPE (msymbol) == ourtype4)
3653           {
3654             if (!datum.preg_p
3655                 || regexec (&datum.preg, SYMBOL_NATURAL_NAME (msymbol), 0,
3656                             NULL, 0) == 0)
3657               {
3658                 /* For functions we can do a quick check of whether the
3659                    symbol might be found via find_pc_symtab.  */
3660                 if (kind != FUNCTIONS_DOMAIN
3661                     || find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)) == NULL)
3662                   {
3663                     if (lookup_symbol_in_objfile_from_linkage_name
3664                         (objfile, SYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
3665                         == NULL)
3666                       {
3667                         /* match */
3668                         psr = (struct symbol_search *)
3669                           xmalloc (sizeof (struct symbol_search));
3670                         psr->block = i;
3671                         psr->msymbol = msymbol;
3672                         psr->symtab = NULL;
3673                         psr->symbol = NULL;
3674                         psr->next = NULL;
3675                         if (tail == NULL)
3676                           {
3677                             sr = psr;
3678                             make_cleanup_free_search_symbols (sr);
3679                           }
3680                         else
3681                           tail->next = psr;
3682                         tail = psr;
3683                       }
3684                   }
3685               }
3686           }
3687       }
3688     }
3689
3690   discard_cleanups (retval_chain);
3691   do_cleanups (old_chain);
3692   *matches = sr;
3693 }
3694
3695 /* Helper function for symtab_symbol_info, this function uses
3696    the data returned from search_symbols() to print information
3697    regarding the match to gdb_stdout.  */
3698
3699 static void
3700 print_symbol_info (enum search_domain kind,
3701                    struct symtab *s, struct symbol *sym,
3702                    int block, char *last)
3703 {
3704   if (last == NULL || filename_cmp (last, s->filename) != 0)
3705     {
3706       fputs_filtered ("\nFile ", gdb_stdout);
3707       fputs_filtered (s->filename, gdb_stdout);
3708       fputs_filtered (":\n", gdb_stdout);
3709     }
3710
3711   if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
3712     printf_filtered ("static ");
3713
3714   /* Typedef that is not a C++ class.  */
3715   if (kind == TYPES_DOMAIN
3716       && SYMBOL_DOMAIN (sym) != STRUCT_DOMAIN)
3717     typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
3718   /* variable, func, or typedef-that-is-c++-class.  */
3719   else if (kind < TYPES_DOMAIN
3720            || (kind == TYPES_DOMAIN
3721                && SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN))
3722     {
3723       type_print (SYMBOL_TYPE (sym),
3724                   (SYMBOL_CLASS (sym) == LOC_TYPEDEF
3725                    ? "" : SYMBOL_PRINT_NAME (sym)),
3726                   gdb_stdout, 0);
3727
3728       printf_filtered (";\n");
3729     }
3730 }
3731
3732 /* This help function for symtab_symbol_info() prints information
3733    for non-debugging symbols to gdb_stdout.  */
3734
3735 static void
3736 print_msymbol_info (struct minimal_symbol *msymbol)
3737 {
3738   struct gdbarch *gdbarch = get_objfile_arch (msymbol_objfile (msymbol));
3739   char *tmp;
3740
3741   if (gdbarch_addr_bit (gdbarch) <= 32)
3742     tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol)
3743                              & (CORE_ADDR) 0xffffffff,
3744                              8);
3745   else
3746     tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol),
3747                              16);
3748   printf_filtered ("%s  %s\n",
3749                    tmp, SYMBOL_PRINT_NAME (msymbol));
3750 }
3751
3752 /* This is the guts of the commands "info functions", "info types", and
3753    "info variables".  It calls search_symbols to find all matches and then
3754    print_[m]symbol_info to print out some useful information about the
3755    matches.  */
3756
3757 static void
3758 symtab_symbol_info (char *regexp, enum search_domain kind, int from_tty)
3759 {
3760   static const char * const classnames[] =
3761     {"variable", "function", "type"};
3762   struct symbol_search *symbols;
3763   struct symbol_search *p;
3764   struct cleanup *old_chain;
3765   char *last_filename = NULL;
3766   int first = 1;
3767
3768   gdb_assert (kind <= TYPES_DOMAIN);
3769
3770   /* Must make sure that if we're interrupted, symbols gets freed.  */
3771   search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
3772   old_chain = make_cleanup_free_search_symbols (symbols);
3773
3774   if (regexp != NULL)
3775     printf_filtered (_("All %ss matching regular expression \"%s\":\n"),
3776                      classnames[kind], regexp);
3777   else
3778     printf_filtered (_("All defined %ss:\n"), classnames[kind]);
3779
3780   for (p = symbols; p != NULL; p = p->next)
3781     {
3782       QUIT;
3783
3784       if (p->msymbol != NULL)
3785         {
3786           if (first)
3787             {
3788               printf_filtered (_("\nNon-debugging symbols:\n"));
3789               first = 0;
3790             }
3791           print_msymbol_info (p->msymbol);
3792         }
3793       else
3794         {
3795           print_symbol_info (kind,
3796                              p->symtab,
3797                              p->symbol,
3798                              p->block,
3799                              last_filename);
3800           last_filename = p->symtab->filename;
3801         }
3802     }
3803
3804   do_cleanups (old_chain);
3805 }
3806
3807 static void
3808 variables_info (char *regexp, int from_tty)
3809 {
3810   symtab_symbol_info (regexp, VARIABLES_DOMAIN, from_tty);
3811 }
3812
3813 static void
3814 functions_info (char *regexp, int from_tty)
3815 {
3816   symtab_symbol_info (regexp, FUNCTIONS_DOMAIN, from_tty);
3817 }
3818
3819
3820 static void
3821 types_info (char *regexp, int from_tty)
3822 {
3823   symtab_symbol_info (regexp, TYPES_DOMAIN, from_tty);
3824 }
3825
3826 /* Breakpoint all functions matching regular expression.  */
3827
3828 void
3829 rbreak_command_wrapper (char *regexp, int from_tty)
3830 {
3831   rbreak_command (regexp, from_tty);
3832 }
3833
3834 /* A cleanup function that calls end_rbreak_breakpoints.  */
3835
3836 static void
3837 do_end_rbreak_breakpoints (void *ignore)
3838 {
3839   end_rbreak_breakpoints ();
3840 }
3841
3842 static void
3843 rbreak_command (char *regexp, int from_tty)
3844 {
3845   struct symbol_search *ss;
3846   struct symbol_search *p;
3847   struct cleanup *old_chain;
3848   char *string = NULL;
3849   int len = 0;
3850   char **files = NULL, *file_name;
3851   int nfiles = 0;
3852
3853   if (regexp)
3854     {
3855       char *colon = strchr (regexp, ':');
3856
3857       if (colon && *(colon + 1) != ':')
3858         {
3859           int colon_index;
3860
3861           colon_index = colon - regexp;
3862           file_name = alloca (colon_index + 1);
3863           memcpy (file_name, regexp, colon_index);
3864           file_name[colon_index--] = 0;
3865           while (isspace (file_name[colon_index]))
3866             file_name[colon_index--] = 0; 
3867           files = &file_name;
3868           nfiles = 1;
3869           regexp = colon + 1;
3870           while (isspace (*regexp))  regexp++; 
3871         }
3872     }
3873
3874   search_symbols (regexp, FUNCTIONS_DOMAIN, nfiles, files, &ss);
3875   old_chain = make_cleanup_free_search_symbols (ss);
3876   make_cleanup (free_current_contents, &string);
3877
3878   start_rbreak_breakpoints ();
3879   make_cleanup (do_end_rbreak_breakpoints, NULL);
3880   for (p = ss; p != NULL; p = p->next)
3881     {
3882       if (p->msymbol == NULL)
3883         {
3884           int newlen = (strlen (p->symtab->filename)
3885                         + strlen (SYMBOL_LINKAGE_NAME (p->symbol))
3886                         + 4);
3887
3888           if (newlen > len)
3889             {
3890               string = xrealloc (string, newlen);
3891               len = newlen;
3892             }
3893           strcpy (string, p->symtab->filename);
3894           strcat (string, ":'");
3895           strcat (string, SYMBOL_LINKAGE_NAME (p->symbol));
3896           strcat (string, "'");
3897           break_command (string, from_tty);
3898           print_symbol_info (FUNCTIONS_DOMAIN,
3899                              p->symtab,
3900                              p->symbol,
3901                              p->block,
3902                              p->symtab->filename);
3903         }
3904       else
3905         {
3906           int newlen = (strlen (SYMBOL_LINKAGE_NAME (p->msymbol)) + 3);
3907
3908           if (newlen > len)
3909             {
3910               string = xrealloc (string, newlen);
3911               len = newlen;
3912             }
3913           strcpy (string, "'");
3914           strcat (string, SYMBOL_LINKAGE_NAME (p->msymbol));
3915           strcat (string, "'");
3916
3917           break_command (string, from_tty);
3918           printf_filtered ("<function, no debug info> %s;\n",
3919                            SYMBOL_PRINT_NAME (p->msymbol));
3920         }
3921     }
3922
3923   do_cleanups (old_chain);
3924 }
3925 \f
3926
3927 /* Evaluate if NAME matches SYM_TEXT and SYM_TEXT_LEN.
3928
3929    Either sym_text[sym_text_len] != '(' and then we search for any
3930    symbol starting with SYM_TEXT text.
3931
3932    Otherwise sym_text[sym_text_len] == '(' and then we require symbol name to
3933    be terminated at that point.  Partial symbol tables do not have parameters
3934    information.  */
3935
3936 static int
3937 compare_symbol_name (const char *name, const char *sym_text, int sym_text_len)
3938 {
3939   int (*ncmp) (const char *, const char *, size_t);
3940
3941   ncmp = (case_sensitivity == case_sensitive_on ? strncmp : strncasecmp);
3942
3943   if (ncmp (name, sym_text, sym_text_len) != 0)
3944     return 0;
3945
3946   if (sym_text[sym_text_len] == '(')
3947     {
3948       /* User searches for `name(someth...'.  Require NAME to be terminated.
3949          Normally psymtabs and gdbindex have no parameter types so '\0' will be
3950          present but accept even parameters presence.  In this case this
3951          function is in fact strcmp_iw but whitespace skipping is not supported
3952          for tab completion.  */
3953
3954       if (name[sym_text_len] != '\0' && name[sym_text_len] != '(')
3955         return 0;
3956     }
3957
3958   return 1;
3959 }
3960
3961 /* Free any memory associated with a completion list.  */
3962
3963 static void
3964 free_completion_list (VEC (char_ptr) **list_ptr)
3965 {
3966   int i;
3967   char *p;
3968
3969   for (i = 0; VEC_iterate (char_ptr, *list_ptr, i, p); ++i)
3970     xfree (p);
3971   VEC_free (char_ptr, *list_ptr);
3972 }
3973
3974 /* Callback for make_cleanup.  */
3975
3976 static void
3977 do_free_completion_list (void *list)
3978 {
3979   free_completion_list (list);
3980 }
3981
3982 /* Helper routine for make_symbol_completion_list.  */
3983
3984 static VEC (char_ptr) *return_val;
3985
3986 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
3987       completion_list_add_name \
3988         (SYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))
3989
3990 /*  Test to see if the symbol specified by SYMNAME (which is already
3991    demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3992    characters.  If so, add it to the current completion list.  */
3993
3994 static void
3995 completion_list_add_name (const char *symname,
3996                           const char *sym_text, int sym_text_len,
3997                           const char *text, const char *word)
3998 {
3999   /* Clip symbols that cannot match.  */
4000   if (!compare_symbol_name (symname, sym_text, sym_text_len))
4001     return;
4002
4003   /* We have a match for a completion, so add SYMNAME to the current list
4004      of matches.  Note that the name is moved to freshly malloc'd space.  */
4005
4006   {
4007     char *new;
4008
4009     if (word == sym_text)
4010       {
4011         new = xmalloc (strlen (symname) + 5);
4012         strcpy (new, symname);
4013       }
4014     else if (word > sym_text)
4015       {
4016         /* Return some portion of symname.  */
4017         new = xmalloc (strlen (symname) + 5);
4018         strcpy (new, symname + (word - sym_text));
4019       }
4020     else
4021       {
4022         /* Return some of SYM_TEXT plus symname.  */
4023         new = xmalloc (strlen (symname) + (sym_text - word) + 5);
4024         strncpy (new, word, sym_text - word);
4025         new[sym_text - word] = '\0';
4026         strcat (new, symname);
4027       }
4028
4029     VEC_safe_push (char_ptr, return_val, new);
4030   }
4031 }
4032
4033 /* ObjC: In case we are completing on a selector, look as the msymbol
4034    again and feed all the selectors into the mill.  */
4035
4036 static void
4037 completion_list_objc_symbol (struct minimal_symbol *msymbol,
4038                              const char *sym_text, int sym_text_len,
4039                              const char *text, const char *word)
4040 {
4041   static char *tmp = NULL;
4042   static unsigned int tmplen = 0;
4043
4044   const char *method, *category, *selector;
4045   char *tmp2 = NULL;
4046
4047   method = SYMBOL_NATURAL_NAME (msymbol);
4048
4049   /* Is it a method?  */
4050   if ((method[0] != '-') && (method[0] != '+'))
4051     return;
4052
4053   if (sym_text[0] == '[')
4054     /* Complete on shortened method method.  */
4055     completion_list_add_name (method + 1, sym_text, sym_text_len, text, word);
4056
4057   while ((strlen (method) + 1) >= tmplen)
4058     {
4059       if (tmplen == 0)
4060         tmplen = 1024;
4061       else
4062         tmplen *= 2;
4063       tmp = xrealloc (tmp, tmplen);
4064     }
4065   selector = strchr (method, ' ');
4066   if (selector != NULL)
4067     selector++;
4068
4069   category = strchr (method, '(');
4070
4071   if ((category != NULL) && (selector != NULL))
4072     {
4073       memcpy (tmp, method, (category - method));
4074       tmp[category - method] = ' ';
4075       memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
4076       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
4077       if (sym_text[0] == '[')
4078         completion_list_add_name (tmp + 1, sym_text, sym_text_len, text, word);
4079     }
4080
4081   if (selector != NULL)
4082     {
4083       /* Complete on selector only.  */
4084       strcpy (tmp, selector);
4085       tmp2 = strchr (tmp, ']');
4086       if (tmp2 != NULL)
4087         *tmp2 = '\0';
4088
4089       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
4090     }
4091 }
4092
4093 /* Break the non-quoted text based on the characters which are in
4094    symbols.  FIXME: This should probably be language-specific.  */
4095
4096 static char *
4097 language_search_unquoted_string (char *text, char *p)
4098 {
4099   for (; p > text; --p)
4100     {
4101       if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
4102         continue;
4103       else
4104         {
4105           if ((current_language->la_language == language_objc))
4106             {
4107               if (p[-1] == ':')     /* Might be part of a method name.  */
4108                 continue;
4109               else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
4110                 p -= 2;             /* Beginning of a method name.  */
4111               else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
4112                 {                   /* Might be part of a method name.  */
4113                   char *t = p;
4114
4115                   /* Seeing a ' ' or a '(' is not conclusive evidence
4116                      that we are in the middle of a method name.  However,
4117                      finding "-[" or "+[" should be pretty un-ambiguous.
4118                      Unfortunately we have to find it now to decide.  */
4119
4120                   while (t > text)
4121                     if (isalnum (t[-1]) || t[-1] == '_' ||
4122                         t[-1] == ' '    || t[-1] == ':' ||
4123                         t[-1] == '('    || t[-1] == ')')
4124                       --t;
4125                     else
4126                       break;
4127
4128                   if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
4129                     p = t - 2;      /* Method name detected.  */
4130                   /* Else we leave with p unchanged.  */
4131                 }
4132             }
4133           break;
4134         }
4135     }
4136   return p;
4137 }
4138
4139 static void
4140 completion_list_add_fields (struct symbol *sym, char *sym_text,
4141                             int sym_text_len, char *text, char *word)
4142 {
4143   if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4144     {
4145       struct type *t = SYMBOL_TYPE (sym);
4146       enum type_code c = TYPE_CODE (t);
4147       int j;
4148
4149       if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
4150         for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
4151           if (TYPE_FIELD_NAME (t, j))
4152             completion_list_add_name (TYPE_FIELD_NAME (t, j),
4153                                       sym_text, sym_text_len, text, word);
4154     }
4155 }
4156
4157 /* Type of the user_data argument passed to add_macro_name or
4158    expand_partial_symbol_name.  The contents are simply whatever is
4159    needed by completion_list_add_name.  */
4160 struct add_name_data
4161 {
4162   char *sym_text;
4163   int sym_text_len;
4164   char *text;
4165   char *word;
4166 };
4167
4168 /* A callback used with macro_for_each and macro_for_each_in_scope.
4169    This adds a macro's name to the current completion list.  */
4170
4171 static void
4172 add_macro_name (const char *name, const struct macro_definition *ignore,
4173                 struct macro_source_file *ignore2, int ignore3,
4174                 void *user_data)
4175 {
4176   struct add_name_data *datum = (struct add_name_data *) user_data;
4177
4178   completion_list_add_name ((char *) name,
4179                             datum->sym_text, datum->sym_text_len,
4180                             datum->text, datum->word);
4181 }
4182
4183 /* A callback for expand_partial_symbol_names.  */
4184
4185 static int
4186 expand_partial_symbol_name (const char *name, void *user_data)
4187 {
4188   struct add_name_data *datum = (struct add_name_data *) user_data;
4189
4190   return compare_symbol_name (name, datum->sym_text, datum->sym_text_len);
4191 }
4192
4193 VEC (char_ptr) *
4194 default_make_symbol_completion_list_break_on (char *text, char *word,
4195                                               const char *break_on,
4196                                               enum type_code code)
4197 {
4198   /* Problem: All of the symbols have to be copied because readline
4199      frees them.  I'm not going to worry about this; hopefully there
4200      won't be that many.  */
4201
4202   struct symbol *sym;
4203   struct symtab *s;
4204   struct minimal_symbol *msymbol;
4205   struct objfile *objfile;
4206   struct block *b;
4207   const struct block *surrounding_static_block, *surrounding_global_block;
4208   struct block_iterator iter;
4209   /* The symbol we are completing on.  Points in same buffer as text.  */
4210   char *sym_text;
4211   /* Length of sym_text.  */
4212   int sym_text_len;
4213   struct add_name_data datum;
4214   struct cleanup *back_to;
4215
4216   /* Now look for the symbol we are supposed to complete on.  */
4217   {
4218     char *p;
4219     char quote_found;
4220     char *quote_pos = NULL;
4221
4222     /* First see if this is a quoted string.  */
4223     quote_found = '\0';
4224     for (p = text; *p != '\0'; ++p)
4225       {
4226         if (quote_found != '\0')
4227           {
4228             if (*p == quote_found)
4229               /* Found close quote.  */
4230               quote_found = '\0';
4231             else if (*p == '\\' && p[1] == quote_found)
4232               /* A backslash followed by the quote character
4233                  doesn't end the string.  */
4234               ++p;
4235           }
4236         else if (*p == '\'' || *p == '"')
4237           {
4238             quote_found = *p;
4239             quote_pos = p;
4240           }
4241       }
4242     if (quote_found == '\'')
4243       /* A string within single quotes can be a symbol, so complete on it.  */
4244       sym_text = quote_pos + 1;
4245     else if (quote_found == '"')
4246       /* A double-quoted string is never a symbol, nor does it make sense
4247          to complete it any other way.  */
4248       {
4249         return NULL;
4250       }
4251     else
4252       {
4253         /* It is not a quoted string.  Break it based on the characters
4254            which are in symbols.  */
4255         while (p > text)
4256           {
4257             if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0'
4258                 || p[-1] == ':' || strchr (break_on, p[-1]) != NULL)
4259               --p;
4260             else
4261               break;
4262           }
4263         sym_text = p;
4264       }
4265   }
4266
4267   sym_text_len = strlen (sym_text);
4268
4269   /* Prepare SYM_TEXT_LEN for compare_symbol_name.  */
4270
4271   if (current_language->la_language == language_cplus
4272       || current_language->la_language == language_java
4273       || current_language->la_language == language_fortran)
4274     {
4275       /* These languages may have parameters entered by user but they are never
4276          present in the partial symbol tables.  */
4277
4278       const char *cs = memchr (sym_text, '(', sym_text_len);
4279
4280       if (cs)
4281         sym_text_len = cs - sym_text;
4282     }
4283   gdb_assert (sym_text[sym_text_len] == '\0' || sym_text[sym_text_len] == '(');
4284
4285   return_val = NULL;
4286   back_to = make_cleanup (do_free_completion_list, &return_val);
4287
4288   datum.sym_text = sym_text;
4289   datum.sym_text_len = sym_text_len;
4290   datum.text = text;
4291   datum.word = word;
4292
4293   /* Look through the partial symtabs for all symbols which begin
4294      by matching SYM_TEXT.  Expand all CUs that you find to the list.
4295      The real names will get added by COMPLETION_LIST_ADD_SYMBOL below.  */
4296   expand_partial_symbol_names (expand_partial_symbol_name, &datum);
4297
4298   /* At this point scan through the misc symbol vectors and add each
4299      symbol you find to the list.  Eventually we want to ignore
4300      anything that isn't a text symbol (everything else will be
4301      handled by the psymtab code above).  */
4302
4303   if (code == TYPE_CODE_UNDEF)
4304     {
4305       ALL_MSYMBOLS (objfile, msymbol)
4306         {
4307           QUIT;
4308           COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text,
4309                                       word);
4310
4311           completion_list_objc_symbol (msymbol, sym_text, sym_text_len, text,
4312                                        word);
4313         }
4314     }
4315
4316   /* Search upwards from currently selected frame (so that we can
4317      complete on local vars).  Also catch fields of types defined in
4318      this places which match our text string.  Only complete on types
4319      visible from current context.  */
4320
4321   b = get_selected_block (0);
4322   surrounding_static_block = block_static_block (b);
4323   surrounding_global_block = block_global_block (b);
4324   if (surrounding_static_block != NULL)
4325     while (b != surrounding_static_block)
4326       {
4327         QUIT;
4328
4329         ALL_BLOCK_SYMBOLS (b, iter, sym)
4330           {
4331             if (code == TYPE_CODE_UNDEF)
4332               {
4333                 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
4334                                             word);
4335                 completion_list_add_fields (sym, sym_text, sym_text_len, text,
4336                                             word);
4337               }
4338             else if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
4339                      && TYPE_CODE (SYMBOL_TYPE (sym)) == code)
4340               COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
4341                                           word);
4342           }
4343
4344         /* Stop when we encounter an enclosing function.  Do not stop for
4345            non-inlined functions - the locals of the enclosing function
4346            are in scope for a nested function.  */
4347         if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
4348           break;
4349         b = BLOCK_SUPERBLOCK (b);
4350       }
4351
4352   /* Add fields from the file's types; symbols will be added below.  */
4353
4354   if (code == TYPE_CODE_UNDEF)
4355     {
4356       if (surrounding_static_block != NULL)
4357         ALL_BLOCK_SYMBOLS (surrounding_static_block, iter, sym)
4358           completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
4359
4360       if (surrounding_global_block != NULL)
4361         ALL_BLOCK_SYMBOLS (surrounding_global_block, iter, sym)
4362           completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
4363     }
4364
4365   /* Go through the symtabs and check the externs and statics for
4366      symbols which match.  */
4367
4368   ALL_PRIMARY_SYMTABS (objfile, s)
4369   {
4370     QUIT;
4371     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4372     ALL_BLOCK_SYMBOLS (b, iter, sym)
4373       {
4374         if (code == TYPE_CODE_UNDEF
4375             || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
4376                 && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
4377           COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4378       }
4379   }
4380
4381   ALL_PRIMARY_SYMTABS (objfile, s)
4382   {
4383     QUIT;
4384     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4385     ALL_BLOCK_SYMBOLS (b, iter, sym)
4386       {
4387         if (code == TYPE_CODE_UNDEF
4388             || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
4389                 && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
4390           COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4391       }
4392   }
4393
4394   /* Skip macros if we are completing a struct tag -- arguable but
4395      usually what is expected.  */
4396   if (current_language->la_macro_expansion == macro_expansion_c
4397       && code == TYPE_CODE_UNDEF)
4398     {
4399       struct macro_scope *scope;
4400
4401       /* Add any macros visible in the default scope.  Note that this
4402          may yield the occasional wrong result, because an expression
4403          might be evaluated in a scope other than the default.  For
4404          example, if the user types "break file:line if <TAB>", the
4405          resulting expression will be evaluated at "file:line" -- but
4406          at there does not seem to be a way to detect this at
4407          completion time.  */
4408       scope = default_macro_scope ();
4409       if (scope)
4410         {
4411           macro_for_each_in_scope (scope->file, scope->line,
4412                                    add_macro_name, &datum);
4413           xfree (scope);
4414         }
4415
4416       /* User-defined macros are always visible.  */
4417       macro_for_each (macro_user_macros, add_macro_name, &datum);
4418     }
4419
4420   discard_cleanups (back_to);
4421   return (return_val);
4422 }
4423
4424 VEC (char_ptr) *
4425 default_make_symbol_completion_list (char *text, char *word,
4426                                      enum type_code code)
4427 {
4428   return default_make_symbol_completion_list_break_on (text, word, "", code);
4429 }
4430
4431 /* Return a vector of all symbols (regardless of class) which begin by
4432    matching TEXT.  If the answer is no symbols, then the return value
4433    is NULL.  */
4434
4435 VEC (char_ptr) *
4436 make_symbol_completion_list (char *text, char *word)
4437 {
4438   return current_language->la_make_symbol_completion_list (text, word,
4439                                                            TYPE_CODE_UNDEF);
4440 }
4441
4442 /* Like make_symbol_completion_list, but only return STRUCT_DOMAIN
4443    symbols whose type code is CODE.  */
4444
4445 VEC (char_ptr) *
4446 make_symbol_completion_type (char *text, char *word, enum type_code code)
4447 {
4448   gdb_assert (code == TYPE_CODE_UNION
4449               || code == TYPE_CODE_STRUCT
4450               || code == TYPE_CODE_CLASS
4451               || code == TYPE_CODE_ENUM);
4452   return current_language->la_make_symbol_completion_list (text, word, code);
4453 }
4454
4455 /* Like make_symbol_completion_list, but suitable for use as a
4456    completion function.  */
4457
4458 VEC (char_ptr) *
4459 make_symbol_completion_list_fn (struct cmd_list_element *ignore,
4460                                 char *text, char *word)
4461 {
4462   return make_symbol_completion_list (text, word);
4463 }
4464
4465 /* Like make_symbol_completion_list, but returns a list of symbols
4466    defined in a source file FILE.  */
4467
4468 VEC (char_ptr) *
4469 make_file_symbol_completion_list (char *text, char *word, char *srcfile)
4470 {
4471   struct symbol *sym;
4472   struct symtab *s;
4473   struct block *b;
4474   struct block_iterator iter;
4475   /* The symbol we are completing on.  Points in same buffer as text.  */
4476   char *sym_text;
4477   /* Length of sym_text.  */
4478   int sym_text_len;
4479
4480   /* Now look for the symbol we are supposed to complete on.
4481      FIXME: This should be language-specific.  */
4482   {
4483     char *p;
4484     char quote_found;
4485     char *quote_pos = NULL;
4486
4487     /* First see if this is a quoted string.  */
4488     quote_found = '\0';
4489     for (p = text; *p != '\0'; ++p)
4490       {
4491         if (quote_found != '\0')
4492           {
4493             if (*p == quote_found)
4494               /* Found close quote.  */
4495               quote_found = '\0';
4496             else if (*p == '\\' && p[1] == quote_found)
4497               /* A backslash followed by the quote character
4498                  doesn't end the string.  */
4499               ++p;
4500           }
4501         else if (*p == '\'' || *p == '"')
4502           {
4503             quote_found = *p;
4504             quote_pos = p;
4505           }
4506       }
4507     if (quote_found == '\'')
4508       /* A string within single quotes can be a symbol, so complete on it.  */
4509       sym_text = quote_pos + 1;
4510     else if (quote_found == '"')
4511       /* A double-quoted string is never a symbol, nor does it make sense
4512          to complete it any other way.  */
4513       {
4514         return NULL;
4515       }
4516     else
4517       {
4518         /* Not a quoted string.  */
4519         sym_text = language_search_unquoted_string (text, p);
4520       }
4521   }
4522
4523   sym_text_len = strlen (sym_text);
4524
4525   return_val = NULL;
4526
4527   /* Find the symtab for SRCFILE (this loads it if it was not yet read
4528      in).  */
4529   s = lookup_symtab (srcfile);
4530   if (s == NULL)
4531     {
4532       /* Maybe they typed the file with leading directories, while the
4533          symbol tables record only its basename.  */
4534       const char *tail = lbasename (srcfile);
4535
4536       if (tail > srcfile)
4537         s = lookup_symtab (tail);
4538     }
4539
4540   /* If we have no symtab for that file, return an empty list.  */
4541   if (s == NULL)
4542     return (return_val);
4543
4544   /* Go through this symtab and check the externs and statics for
4545      symbols which match.  */
4546
4547   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4548   ALL_BLOCK_SYMBOLS (b, iter, sym)
4549     {
4550       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4551     }
4552
4553   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4554   ALL_BLOCK_SYMBOLS (b, iter, sym)
4555     {
4556       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4557     }
4558
4559   return (return_val);
4560 }
4561
4562 /* A helper function for make_source_files_completion_list.  It adds
4563    another file name to a list of possible completions, growing the
4564    list as necessary.  */
4565
4566 static void
4567 add_filename_to_list (const char *fname, char *text, char *word,
4568                       VEC (char_ptr) **list)
4569 {
4570   char *new;
4571   size_t fnlen = strlen (fname);
4572
4573   if (word == text)
4574     {
4575       /* Return exactly fname.  */
4576       new = xmalloc (fnlen + 5);
4577       strcpy (new, fname);
4578     }
4579   else if (word > text)
4580     {
4581       /* Return some portion of fname.  */
4582       new = xmalloc (fnlen + 5);
4583       strcpy (new, fname + (word - text));
4584     }
4585   else
4586     {
4587       /* Return some of TEXT plus fname.  */
4588       new = xmalloc (fnlen + (text - word) + 5);
4589       strncpy (new, word, text - word);
4590       new[text - word] = '\0';
4591       strcat (new, fname);
4592     }
4593   VEC_safe_push (char_ptr, *list, new);
4594 }
4595
4596 static int
4597 not_interesting_fname (const char *fname)
4598 {
4599   static const char *illegal_aliens[] = {
4600     "_globals_",        /* inserted by coff_symtab_read */
4601     NULL
4602   };
4603   int i;
4604
4605   for (i = 0; illegal_aliens[i]; i++)
4606     {
4607       if (filename_cmp (fname, illegal_aliens[i]) == 0)
4608         return 1;
4609     }
4610   return 0;
4611 }
4612
4613 /* An object of this type is passed as the user_data argument to
4614    map_partial_symbol_filenames.  */
4615 struct add_partial_filename_data
4616 {
4617   struct filename_seen_cache *filename_seen_cache;
4618   char *text;
4619   char *word;
4620   int text_len;
4621   VEC (char_ptr) **list;
4622 };
4623
4624 /* A callback for map_partial_symbol_filenames.  */
4625
4626 static void
4627 maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
4628                                    void *user_data)
4629 {
4630   struct add_partial_filename_data *data = user_data;
4631
4632   if (not_interesting_fname (filename))
4633     return;
4634   if (!filename_seen (data->filename_seen_cache, filename, 1)
4635       && filename_ncmp (filename, data->text, data->text_len) == 0)
4636     {
4637       /* This file matches for a completion; add it to the
4638          current list of matches.  */
4639       add_filename_to_list (filename, data->text, data->word, data->list);
4640     }
4641   else
4642     {
4643       const char *base_name = lbasename (filename);
4644
4645       if (base_name != filename
4646           && !filename_seen (data->filename_seen_cache, base_name, 1)
4647           && filename_ncmp (base_name, data->text, data->text_len) == 0)
4648         add_filename_to_list (base_name, data->text, data->word, data->list);
4649     }
4650 }
4651
4652 /* Return a vector of all source files whose names begin with matching
4653    TEXT.  The file names are looked up in the symbol tables of this
4654    program.  If the answer is no matchess, then the return value is
4655    NULL.  */
4656
4657 VEC (char_ptr) *
4658 make_source_files_completion_list (char *text, char *word)
4659 {
4660   struct symtab *s;
4661   struct objfile *objfile;
4662   size_t text_len = strlen (text);
4663   VEC (char_ptr) *list = NULL;
4664   const char *base_name;
4665   struct add_partial_filename_data datum;
4666   struct filename_seen_cache *filename_seen_cache;
4667   struct cleanup *back_to, *cache_cleanup;
4668
4669   if (!have_full_symbols () && !have_partial_symbols ())
4670     return list;
4671
4672   back_to = make_cleanup (do_free_completion_list, &list);
4673
4674   filename_seen_cache = create_filename_seen_cache ();
4675   cache_cleanup = make_cleanup (delete_filename_seen_cache,
4676                                 filename_seen_cache);
4677
4678   ALL_SYMTABS (objfile, s)
4679     {
4680       if (not_interesting_fname (s->filename))
4681         continue;
4682       if (!filename_seen (filename_seen_cache, s->filename, 1)
4683           && filename_ncmp (s->filename, text, text_len) == 0)
4684         {
4685           /* This file matches for a completion; add it to the current
4686              list of matches.  */
4687           add_filename_to_list (s->filename, text, word, &list);
4688         }
4689       else
4690         {
4691           /* NOTE: We allow the user to type a base name when the
4692              debug info records leading directories, but not the other
4693              way around.  This is what subroutines of breakpoint
4694              command do when they parse file names.  */
4695           base_name = lbasename (s->filename);
4696           if (base_name != s->filename
4697               && !filename_seen (filename_seen_cache, base_name, 1)
4698               && filename_ncmp (base_name, text, text_len) == 0)
4699             add_filename_to_list (base_name, text, word, &list);
4700         }
4701     }
4702
4703   datum.filename_seen_cache = filename_seen_cache;
4704   datum.text = text;
4705   datum.word = word;
4706   datum.text_len = text_len;
4707   datum.list = &list;
4708   map_partial_symbol_filenames (maybe_add_partial_symtab_filename, &datum,
4709                                 0 /*need_fullname*/);
4710
4711   do_cleanups (cache_cleanup);
4712   discard_cleanups (back_to);
4713
4714   return list;
4715 }
4716
4717 /* Determine if PC is in the prologue of a function.  The prologue is the area
4718    between the first instruction of a function, and the first executable line.
4719    Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
4720
4721    If non-zero, func_start is where we think the prologue starts, possibly
4722    by previous examination of symbol table information.  */
4723
4724 int
4725 in_prologue (struct gdbarch *gdbarch, CORE_ADDR pc, CORE_ADDR func_start)
4726 {
4727   struct symtab_and_line sal;
4728   CORE_ADDR func_addr, func_end;
4729
4730   /* We have several sources of information we can consult to figure
4731      this out.
4732      - Compilers usually emit line number info that marks the prologue
4733        as its own "source line".  So the ending address of that "line"
4734        is the end of the prologue.  If available, this is the most
4735        reliable method.
4736      - The minimal symbols and partial symbols, which can usually tell
4737        us the starting and ending addresses of a function.
4738      - If we know the function's start address, we can call the
4739        architecture-defined gdbarch_skip_prologue function to analyze the
4740        instruction stream and guess where the prologue ends.
4741      - Our `func_start' argument; if non-zero, this is the caller's
4742        best guess as to the function's entry point.  At the time of
4743        this writing, handle_inferior_event doesn't get this right, so
4744        it should be our last resort.  */
4745
4746   /* Consult the partial symbol table, to find which function
4747      the PC is in.  */
4748   if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
4749     {
4750       CORE_ADDR prologue_end;
4751
4752       /* We don't even have minsym information, so fall back to using
4753          func_start, if given.  */
4754       if (! func_start)
4755         return 1;               /* We *might* be in a prologue.  */
4756
4757       prologue_end = gdbarch_skip_prologue (gdbarch, func_start);
4758
4759       return func_start <= pc && pc < prologue_end;
4760     }
4761
4762   /* If we have line number information for the function, that's
4763      usually pretty reliable.  */
4764   sal = find_pc_line (func_addr, 0);
4765
4766   /* Now sal describes the source line at the function's entry point,
4767      which (by convention) is the prologue.  The end of that "line",
4768      sal.end, is the end of the prologue.
4769
4770      Note that, for functions whose source code is all on a single
4771      line, the line number information doesn't always end up this way.
4772      So we must verify that our purported end-of-prologue address is
4773      *within* the function, not at its start or end.  */
4774   if (sal.line == 0
4775       || sal.end <= func_addr
4776       || func_end <= sal.end)
4777     {
4778       /* We don't have any good line number info, so use the minsym
4779          information, together with the architecture-specific prologue
4780          scanning code.  */
4781       CORE_ADDR prologue_end = gdbarch_skip_prologue (gdbarch, func_addr);
4782
4783       return func_addr <= pc && pc < prologue_end;
4784     }
4785
4786   /* We have line number info, and it looks good.  */
4787   return func_addr <= pc && pc < sal.end;
4788 }
4789
4790 /* Given PC at the function's start address, attempt to find the
4791    prologue end using SAL information.  Return zero if the skip fails.
4792
4793    A non-optimized prologue traditionally has one SAL for the function
4794    and a second for the function body.  A single line function has
4795    them both pointing at the same line.
4796
4797    An optimized prologue is similar but the prologue may contain
4798    instructions (SALs) from the instruction body.  Need to skip those
4799    while not getting into the function body.
4800
4801    The functions end point and an increasing SAL line are used as
4802    indicators of the prologue's endpoint.
4803
4804    This code is based on the function refine_prologue_limit
4805    (found in ia64).  */
4806
4807 CORE_ADDR
4808 skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
4809 {
4810   struct symtab_and_line prologue_sal;
4811   CORE_ADDR start_pc;
4812   CORE_ADDR end_pc;
4813   struct block *bl;
4814
4815   /* Get an initial range for the function.  */
4816   find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
4817   start_pc += gdbarch_deprecated_function_start_offset (gdbarch);
4818
4819   prologue_sal = find_pc_line (start_pc, 0);
4820   if (prologue_sal.line != 0)
4821     {
4822       /* For languages other than assembly, treat two consecutive line
4823          entries at the same address as a zero-instruction prologue.
4824          The GNU assembler emits separate line notes for each instruction
4825          in a multi-instruction macro, but compilers generally will not
4826          do this.  */
4827       if (prologue_sal.symtab->language != language_asm)
4828         {
4829           struct linetable *linetable = LINETABLE (prologue_sal.symtab);
4830           int idx = 0;
4831
4832           /* Skip any earlier lines, and any end-of-sequence marker
4833              from a previous function.  */
4834           while (linetable->item[idx].pc != prologue_sal.pc
4835                  || linetable->item[idx].line == 0)
4836             idx++;
4837
4838           if (idx+1 < linetable->nitems
4839               && linetable->item[idx+1].line != 0
4840               && linetable->item[idx+1].pc == start_pc)
4841             return start_pc;
4842         }
4843
4844       /* If there is only one sal that covers the entire function,
4845          then it is probably a single line function, like
4846          "foo(){}".  */
4847       if (prologue_sal.end >= end_pc)
4848         return 0;
4849
4850       while (prologue_sal.end < end_pc)
4851         {
4852           struct symtab_and_line sal;
4853
4854           sal = find_pc_line (prologue_sal.end, 0);
4855           if (sal.line == 0)
4856             break;
4857           /* Assume that a consecutive SAL for the same (or larger)
4858              line mark the prologue -> body transition.  */
4859           if (sal.line >= prologue_sal.line)
4860             break;
4861           /* Likewise if we are in a different symtab altogether
4862              (e.g. within a file included via #include).  */
4863           if (sal.symtab != prologue_sal.symtab)
4864             break;
4865
4866           /* The line number is smaller.  Check that it's from the
4867              same function, not something inlined.  If it's inlined,
4868              then there is no point comparing the line numbers.  */
4869           bl = block_for_pc (prologue_sal.end);
4870           while (bl)
4871             {
4872               if (block_inlined_p (bl))
4873                 break;
4874               if (BLOCK_FUNCTION (bl))
4875                 {
4876                   bl = NULL;
4877                   break;
4878                 }
4879               bl = BLOCK_SUPERBLOCK (bl);
4880             }
4881           if (bl != NULL)
4882             break;
4883
4884           /* The case in which compiler's optimizer/scheduler has
4885              moved instructions into the prologue.  We look ahead in
4886              the function looking for address ranges whose
4887              corresponding line number is less the first one that we
4888              found for the function.  This is more conservative then
4889              refine_prologue_limit which scans a large number of SALs
4890              looking for any in the prologue.  */
4891           prologue_sal = sal;
4892         }
4893     }
4894
4895   if (prologue_sal.end < end_pc)
4896     /* Return the end of this line, or zero if we could not find a
4897        line.  */
4898     return prologue_sal.end;
4899   else
4900     /* Don't return END_PC, which is past the end of the function.  */
4901     return prologue_sal.pc;
4902 }
4903 \f
4904 /* Track MAIN */
4905 static char *name_of_main;
4906 enum language language_of_main = language_unknown;
4907
4908 void
4909 set_main_name (const char *name)
4910 {
4911   if (name_of_main != NULL)
4912     {
4913       xfree (name_of_main);
4914       name_of_main = NULL;
4915       language_of_main = language_unknown;
4916     }
4917   if (name != NULL)
4918     {
4919       name_of_main = xstrdup (name);
4920       language_of_main = language_unknown;
4921     }
4922 }
4923
4924 /* Deduce the name of the main procedure, and set NAME_OF_MAIN
4925    accordingly.  */
4926
4927 static void
4928 find_main_name (void)
4929 {
4930   const char *new_main_name;
4931
4932   /* Try to see if the main procedure is in Ada.  */
4933   /* FIXME: brobecker/2005-03-07: Another way of doing this would
4934      be to add a new method in the language vector, and call this
4935      method for each language until one of them returns a non-empty
4936      name.  This would allow us to remove this hard-coded call to
4937      an Ada function.  It is not clear that this is a better approach
4938      at this point, because all methods need to be written in a way
4939      such that false positives never be returned.  For instance, it is
4940      important that a method does not return a wrong name for the main
4941      procedure if the main procedure is actually written in a different
4942      language.  It is easy to guaranty this with Ada, since we use a
4943      special symbol generated only when the main in Ada to find the name
4944      of the main procedure.  It is difficult however to see how this can
4945      be guarantied for languages such as C, for instance.  This suggests
4946      that order of call for these methods becomes important, which means
4947      a more complicated approach.  */
4948   new_main_name = ada_main_name ();
4949   if (new_main_name != NULL)
4950     {
4951       set_main_name (new_main_name);
4952       return;
4953     }
4954
4955   new_main_name = go_main_name ();
4956   if (new_main_name != NULL)
4957     {
4958       set_main_name (new_main_name);
4959       return;
4960     }
4961
4962   new_main_name = pascal_main_name ();
4963   if (new_main_name != NULL)
4964     {
4965       set_main_name (new_main_name);
4966       return;
4967     }
4968
4969   /* The languages above didn't identify the name of the main procedure.
4970      Fallback to "main".  */
4971   set_main_name ("main");
4972 }
4973
4974 char *
4975 main_name (void)
4976 {
4977   if (name_of_main == NULL)
4978     find_main_name ();
4979
4980   return name_of_main;
4981 }
4982
4983 /* Handle ``executable_changed'' events for the symtab module.  */
4984
4985 static void
4986 symtab_observer_executable_changed (void)
4987 {
4988   /* NAME_OF_MAIN may no longer be the same, so reset it for now.  */
4989   set_main_name (NULL);
4990 }
4991
4992 /* Return 1 if the supplied producer string matches the ARM RealView
4993    compiler (armcc).  */
4994
4995 int
4996 producer_is_realview (const char *producer)
4997 {
4998   static const char *const arm_idents[] = {
4999     "ARM C Compiler, ADS",
5000     "Thumb C Compiler, ADS",
5001     "ARM C++ Compiler, ADS",
5002     "Thumb C++ Compiler, ADS",
5003     "ARM/Thumb C/C++ Compiler, RVCT",
5004     "ARM C/C++ Compiler, RVCT"
5005   };
5006   int i;
5007
5008   if (producer == NULL)
5009     return 0;
5010
5011   for (i = 0; i < ARRAY_SIZE (arm_idents); i++)
5012     if (strncmp (producer, arm_idents[i], strlen (arm_idents[i])) == 0)
5013       return 1;
5014
5015   return 0;
5016 }
5017
5018 void
5019 _initialize_symtab (void)
5020 {
5021   add_info ("variables", variables_info, _("\
5022 All global and static variable names, or those matching REGEXP."));
5023   if (dbx_commands)
5024     add_com ("whereis", class_info, variables_info, _("\
5025 All global and static variable names, or those matching REGEXP."));
5026
5027   add_info ("functions", functions_info,
5028             _("All function names, or those matching REGEXP."));
5029
5030   /* FIXME:  This command has at least the following problems:
5031      1.  It prints builtin types (in a very strange and confusing fashion).
5032      2.  It doesn't print right, e.g. with
5033      typedef struct foo *FOO
5034      type_print prints "FOO" when we want to make it (in this situation)
5035      print "struct foo *".
5036      I also think "ptype" or "whatis" is more likely to be useful (but if
5037      there is much disagreement "info types" can be fixed).  */
5038   add_info ("types", types_info,
5039             _("All type names, or those matching REGEXP."));
5040
5041   add_info ("sources", sources_info,
5042             _("Source files in the program."));
5043
5044   add_com ("rbreak", class_breakpoint, rbreak_command,
5045            _("Set a breakpoint for all functions matching REGEXP."));
5046
5047   if (xdb_commands)
5048     {
5049       add_com ("lf", class_info, sources_info,
5050                _("Source files in the program"));
5051       add_com ("lg", class_info, variables_info, _("\
5052 All global and static variable names, or those matching REGEXP."));
5053     }
5054
5055   add_setshow_enum_cmd ("multiple-symbols", no_class,
5056                         multiple_symbols_modes, &multiple_symbols_mode,
5057                         _("\
5058 Set the debugger behavior when more than one symbol are possible matches\n\
5059 in an expression."), _("\
5060 Show how the debugger handles ambiguities in expressions."), _("\
5061 Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
5062                         NULL, NULL, &setlist, &showlist);
5063
5064   add_setshow_boolean_cmd ("basenames-may-differ", class_obscure,
5065                            &basenames_may_differ, _("\
5066 Set whether a source file may have multiple base names."), _("\
5067 Show whether a source file may have multiple base names."), _("\
5068 (A \"base name\" is the name of a file with the directory part removed.\n\
5069 Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
5070 If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
5071 before comparing them.  Canonicalization is an expensive operation,\n\
5072 but it allows the same file be known by more than one base name.\n\
5073 If not set (the default), all source files are assumed to have just\n\
5074 one base name, and gdb will do file name comparisons more efficiently."),
5075                            NULL, NULL,
5076                            &setlist, &showlist);
5077
5078   add_setshow_boolean_cmd ("symtab-create", no_class, &symtab_create_debug,
5079                            _("Set debugging of symbol table creation."),
5080                            _("Show debugging of symbol table creation."), _("\
5081 When enabled, debugging messages are printed when building symbol tables."),
5082                             NULL,
5083                             NULL,
5084                             &setdebuglist, &showdebuglist);
5085
5086   observer_attach_executable_changed (symtab_observer_executable_changed);
5087 }