15796c8dcSSimon Schubert /* Symbol table lookup for the GNU debugger, GDB.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "symtab.h"
225796c8dcSSimon Schubert #include "gdbtypes.h"
235796c8dcSSimon Schubert #include "gdbcore.h"
245796c8dcSSimon Schubert #include "frame.h"
255796c8dcSSimon Schubert #include "target.h"
265796c8dcSSimon Schubert #include "value.h"
275796c8dcSSimon Schubert #include "symfile.h"
285796c8dcSSimon Schubert #include "objfiles.h"
295796c8dcSSimon Schubert #include "gdbcmd.h"
305796c8dcSSimon Schubert #include "gdb_regex.h"
315796c8dcSSimon Schubert #include "expression.h"
325796c8dcSSimon Schubert #include "language.h"
335796c8dcSSimon Schubert #include "demangle.h"
345796c8dcSSimon Schubert #include "inferior.h"
355796c8dcSSimon Schubert #include "source.h"
365796c8dcSSimon Schubert #include "filenames.h" /* for FILENAME_CMP */
375796c8dcSSimon Schubert #include "objc-lang.h"
38cf7f2e2dSJohn Marino #include "d-lang.h"
395796c8dcSSimon Schubert #include "ada-lang.h"
40*ef5ccd6cSJohn Marino #include "go-lang.h"
415796c8dcSSimon Schubert #include "p-lang.h"
425796c8dcSSimon Schubert #include "addrmap.h"
43*ef5ccd6cSJohn Marino #include "cli/cli-utils.h"
445796c8dcSSimon Schubert
455796c8dcSSimon Schubert #include "hashtab.h"
465796c8dcSSimon Schubert
475796c8dcSSimon Schubert #include "gdb_obstack.h"
485796c8dcSSimon Schubert #include "block.h"
495796c8dcSSimon Schubert #include "dictionary.h"
505796c8dcSSimon Schubert
515796c8dcSSimon Schubert #include <sys/types.h>
525796c8dcSSimon Schubert #include <fcntl.h>
535796c8dcSSimon Schubert #include "gdb_string.h"
545796c8dcSSimon Schubert #include "gdb_stat.h"
555796c8dcSSimon Schubert #include <ctype.h>
565796c8dcSSimon Schubert #include "cp-abi.h"
575796c8dcSSimon Schubert #include "cp-support.h"
585796c8dcSSimon Schubert #include "observer.h"
595796c8dcSSimon Schubert #include "gdb_assert.h"
605796c8dcSSimon Schubert #include "solist.h"
615796c8dcSSimon Schubert #include "macrotab.h"
625796c8dcSSimon Schubert #include "macroscope.h"
635796c8dcSSimon Schubert
64cf7f2e2dSJohn Marino #include "psymtab.h"
65*ef5ccd6cSJohn Marino #include "parser-defs.h"
66cf7f2e2dSJohn Marino
675796c8dcSSimon Schubert /* Prototypes for local functions */
685796c8dcSSimon Schubert
695796c8dcSSimon Schubert static void rbreak_command (char *, int);
705796c8dcSSimon Schubert
715796c8dcSSimon Schubert static void types_info (char *, int);
725796c8dcSSimon Schubert
735796c8dcSSimon Schubert static void functions_info (char *, int);
745796c8dcSSimon Schubert
755796c8dcSSimon Schubert static void variables_info (char *, int);
765796c8dcSSimon Schubert
775796c8dcSSimon Schubert static void sources_info (char *, int);
785796c8dcSSimon Schubert
79a45ae5f8SJohn Marino static int find_line_common (struct linetable *, int, int *, int);
805796c8dcSSimon Schubert
815796c8dcSSimon Schubert static struct symbol *lookup_symbol_aux (const char *name,
825796c8dcSSimon Schubert const struct block *block,
835796c8dcSSimon Schubert const domain_enum domain,
845796c8dcSSimon Schubert enum language language,
85*ef5ccd6cSJohn Marino struct field_of_this_result *is_a_field_of_this);
865796c8dcSSimon Schubert
875796c8dcSSimon Schubert static
885796c8dcSSimon Schubert struct symbol *lookup_symbol_aux_local (const char *name,
895796c8dcSSimon Schubert const struct block *block,
90cf7f2e2dSJohn Marino const domain_enum domain,
91cf7f2e2dSJohn Marino enum language language);
925796c8dcSSimon Schubert
935796c8dcSSimon Schubert static
945796c8dcSSimon Schubert struct symbol *lookup_symbol_aux_symtabs (int block_index,
955796c8dcSSimon Schubert const char *name,
965796c8dcSSimon Schubert const domain_enum domain);
975796c8dcSSimon Schubert
985796c8dcSSimon Schubert static
99cf7f2e2dSJohn Marino struct symbol *lookup_symbol_aux_quick (struct objfile *objfile,
100cf7f2e2dSJohn Marino int block_index,
1015796c8dcSSimon Schubert const char *name,
1025796c8dcSSimon Schubert const domain_enum domain);
1035796c8dcSSimon Schubert
1045796c8dcSSimon Schubert static void print_msymbol_info (struct minimal_symbol *);
1055796c8dcSSimon Schubert
1065796c8dcSSimon Schubert void _initialize_symtab (void);
1075796c8dcSSimon Schubert
1085796c8dcSSimon Schubert /* */
1095796c8dcSSimon Schubert
110*ef5ccd6cSJohn Marino /* When non-zero, print debugging messages related to symtab creation. */
111*ef5ccd6cSJohn Marino int symtab_create_debug = 0;
112*ef5ccd6cSJohn Marino
113a45ae5f8SJohn Marino /* Non-zero if a file may be known by two different basenames.
114a45ae5f8SJohn Marino This is the uncommon case, and significantly slows down gdb.
115a45ae5f8SJohn Marino Default set to "off" to not slow down the common case. */
116a45ae5f8SJohn Marino int basenames_may_differ = 0;
117a45ae5f8SJohn Marino
1185796c8dcSSimon Schubert /* Allow the user to configure the debugger behavior with respect
1195796c8dcSSimon Schubert to multiple-choice menus when more than one symbol matches during
1205796c8dcSSimon Schubert a symbol lookup. */
1215796c8dcSSimon Schubert
1225796c8dcSSimon Schubert const char multiple_symbols_ask[] = "ask";
1235796c8dcSSimon Schubert const char multiple_symbols_all[] = "all";
1245796c8dcSSimon Schubert const char multiple_symbols_cancel[] = "cancel";
125*ef5ccd6cSJohn Marino static const char *const multiple_symbols_modes[] =
1265796c8dcSSimon Schubert {
1275796c8dcSSimon Schubert multiple_symbols_ask,
1285796c8dcSSimon Schubert multiple_symbols_all,
1295796c8dcSSimon Schubert multiple_symbols_cancel,
1305796c8dcSSimon Schubert NULL
1315796c8dcSSimon Schubert };
1325796c8dcSSimon Schubert static const char *multiple_symbols_mode = multiple_symbols_all;
1335796c8dcSSimon Schubert
1345796c8dcSSimon Schubert /* Read-only accessor to AUTO_SELECT_MODE. */
1355796c8dcSSimon Schubert
1365796c8dcSSimon Schubert const char *
multiple_symbols_select_mode(void)1375796c8dcSSimon Schubert multiple_symbols_select_mode (void)
1385796c8dcSSimon Schubert {
1395796c8dcSSimon Schubert return multiple_symbols_mode;
1405796c8dcSSimon Schubert }
1415796c8dcSSimon Schubert
1425796c8dcSSimon Schubert /* Block in which the most recently searched-for symbol was found.
1435796c8dcSSimon Schubert Might be better to make this a parameter to lookup_symbol and
1445796c8dcSSimon Schubert value_of_this. */
1455796c8dcSSimon Schubert
1465796c8dcSSimon Schubert const struct block *block_found;
1475796c8dcSSimon Schubert
148*ef5ccd6cSJohn Marino /* See whether FILENAME matches SEARCH_NAME using the rule that we
149*ef5ccd6cSJohn Marino advertise to the user. (The manual's description of linespecs
150*ef5ccd6cSJohn Marino describes what we advertise). Returns true if they match, false
151*ef5ccd6cSJohn Marino otherwise. */
152*ef5ccd6cSJohn Marino
153*ef5ccd6cSJohn Marino int
compare_filenames_for_search(const char * filename,const char * search_name)154*ef5ccd6cSJohn Marino compare_filenames_for_search (const char *filename, const char *search_name)
155*ef5ccd6cSJohn Marino {
156*ef5ccd6cSJohn Marino int len = strlen (filename);
157*ef5ccd6cSJohn Marino size_t search_len = strlen (search_name);
158*ef5ccd6cSJohn Marino
159*ef5ccd6cSJohn Marino if (len < search_len)
160*ef5ccd6cSJohn Marino return 0;
161*ef5ccd6cSJohn Marino
162*ef5ccd6cSJohn Marino /* The tail of FILENAME must match. */
163*ef5ccd6cSJohn Marino if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
164*ef5ccd6cSJohn Marino return 0;
165*ef5ccd6cSJohn Marino
166*ef5ccd6cSJohn Marino /* Either the names must completely match, or the character
167*ef5ccd6cSJohn Marino preceding the trailing SEARCH_NAME segment of FILENAME must be a
168*ef5ccd6cSJohn Marino directory separator.
169*ef5ccd6cSJohn Marino
170*ef5ccd6cSJohn Marino The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
171*ef5ccd6cSJohn Marino cannot match FILENAME "/path//dir/file.c" - as user has requested
172*ef5ccd6cSJohn Marino absolute path. The sama applies for "c:\file.c" possibly
173*ef5ccd6cSJohn Marino incorrectly hypothetically matching "d:\dir\c:\file.c".
174*ef5ccd6cSJohn Marino
175*ef5ccd6cSJohn Marino The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
176*ef5ccd6cSJohn Marino compatible with SEARCH_NAME "file.c". In such case a compiler had
177*ef5ccd6cSJohn Marino to put the "c:file.c" name into debug info. Such compatibility
178*ef5ccd6cSJohn Marino works only on GDB built for DOS host. */
179*ef5ccd6cSJohn Marino return (len == search_len
180*ef5ccd6cSJohn Marino || (!IS_ABSOLUTE_PATH (search_name)
181*ef5ccd6cSJohn Marino && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
182*ef5ccd6cSJohn Marino || (HAS_DRIVE_SPEC (filename)
183*ef5ccd6cSJohn Marino && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
184*ef5ccd6cSJohn Marino }
185*ef5ccd6cSJohn Marino
186a45ae5f8SJohn Marino /* Check for a symtab of a specific name by searching some symtabs.
187a45ae5f8SJohn Marino This is a helper function for callbacks of iterate_over_symtabs.
1885796c8dcSSimon Schubert
189*ef5ccd6cSJohn Marino The return value, NAME, REAL_PATH, CALLBACK, and DATA
190a45ae5f8SJohn Marino are identical to the `map_symtabs_matching_filename' method of
191a45ae5f8SJohn Marino quick_symbol_functions.
192a45ae5f8SJohn Marino
193a45ae5f8SJohn Marino FIRST and AFTER_LAST indicate the range of symtabs to search.
194a45ae5f8SJohn Marino AFTER_LAST is one past the last symtab to search; NULL means to
195a45ae5f8SJohn Marino search until the end of the list. */
196a45ae5f8SJohn Marino
197a45ae5f8SJohn Marino int
iterate_over_some_symtabs(const char * name,const char * real_path,int (* callback)(struct symtab * symtab,void * data),void * data,struct symtab * first,struct symtab * after_last)198a45ae5f8SJohn Marino iterate_over_some_symtabs (const char *name,
199a45ae5f8SJohn Marino const char *real_path,
200a45ae5f8SJohn Marino int (*callback) (struct symtab *symtab,
201a45ae5f8SJohn Marino void *data),
202a45ae5f8SJohn Marino void *data,
203a45ae5f8SJohn Marino struct symtab *first,
204a45ae5f8SJohn Marino struct symtab *after_last)
2055796c8dcSSimon Schubert {
206cf7f2e2dSJohn Marino struct symtab *s = NULL;
207a45ae5f8SJohn Marino const char* base_name = lbasename (name);
2085796c8dcSSimon Schubert
209a45ae5f8SJohn Marino for (s = first; s != NULL && s != after_last; s = s->next)
2105796c8dcSSimon Schubert {
211*ef5ccd6cSJohn Marino if (compare_filenames_for_search (s->filename, name))
2125796c8dcSSimon Schubert {
213a45ae5f8SJohn Marino if (callback (s, data))
214a45ae5f8SJohn Marino return 1;
2155796c8dcSSimon Schubert }
2165796c8dcSSimon Schubert
217a45ae5f8SJohn Marino /* Before we invoke realpath, which can get expensive when many
218a45ae5f8SJohn Marino files are involved, do a quick comparison of the basenames. */
219a45ae5f8SJohn Marino if (! basenames_may_differ
220a45ae5f8SJohn Marino && FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
221a45ae5f8SJohn Marino continue;
222a45ae5f8SJohn Marino
223*ef5ccd6cSJohn Marino if (compare_filenames_for_search (symtab_to_fullname (s), name))
224*ef5ccd6cSJohn Marino {
225*ef5ccd6cSJohn Marino if (callback (s, data))
226*ef5ccd6cSJohn Marino return 1;
227*ef5ccd6cSJohn Marino }
228*ef5ccd6cSJohn Marino
2295796c8dcSSimon Schubert /* If the user gave us an absolute path, try to find the file in
2305796c8dcSSimon Schubert this symtab and use its absolute path. */
2315796c8dcSSimon Schubert
2325796c8dcSSimon Schubert if (real_path != NULL)
2335796c8dcSSimon Schubert {
234*ef5ccd6cSJohn Marino const char *fullname = symtab_to_fullname (s);
235cf7f2e2dSJohn Marino
236*ef5ccd6cSJohn Marino gdb_assert (IS_ABSOLUTE_PATH (real_path));
237*ef5ccd6cSJohn Marino gdb_assert (IS_ABSOLUTE_PATH (name));
238*ef5ccd6cSJohn Marino if (FILENAME_CMP (real_path, fullname) == 0)
239a45ae5f8SJohn Marino {
240a45ae5f8SJohn Marino if (callback (s, data))
241a45ae5f8SJohn Marino return 1;
242a45ae5f8SJohn Marino }
243a45ae5f8SJohn Marino }
244a45ae5f8SJohn Marino }
245a45ae5f8SJohn Marino
246a45ae5f8SJohn Marino return 0;
247a45ae5f8SJohn Marino }
248a45ae5f8SJohn Marino
249a45ae5f8SJohn Marino /* Check for a symtab of a specific name; first in symtabs, then in
250a45ae5f8SJohn Marino psymtabs. *If* there is no '/' in the name, a match after a '/'
251a45ae5f8SJohn Marino in the symtab filename will also work.
252a45ae5f8SJohn Marino
253a45ae5f8SJohn Marino Calls CALLBACK with each symtab that is found and with the supplied
254a45ae5f8SJohn Marino DATA. If CALLBACK returns true, the search stops. */
255a45ae5f8SJohn Marino
256a45ae5f8SJohn Marino void
iterate_over_symtabs(const char * name,int (* callback)(struct symtab * symtab,void * data),void * data)257a45ae5f8SJohn Marino iterate_over_symtabs (const char *name,
258a45ae5f8SJohn Marino int (*callback) (struct symtab *symtab,
259a45ae5f8SJohn Marino void *data),
260a45ae5f8SJohn Marino void *data)
261a45ae5f8SJohn Marino {
262a45ae5f8SJohn Marino struct objfile *objfile;
263a45ae5f8SJohn Marino char *real_path = NULL;
264a45ae5f8SJohn Marino struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
265a45ae5f8SJohn Marino
266a45ae5f8SJohn Marino /* Here we are interested in canonicalizing an absolute path, not
267a45ae5f8SJohn Marino absolutizing a relative path. */
268a45ae5f8SJohn Marino if (IS_ABSOLUTE_PATH (name))
269a45ae5f8SJohn Marino {
270a45ae5f8SJohn Marino real_path = gdb_realpath (name);
271a45ae5f8SJohn Marino make_cleanup (xfree, real_path);
272*ef5ccd6cSJohn Marino gdb_assert (IS_ABSOLUTE_PATH (real_path));
273a45ae5f8SJohn Marino }
274a45ae5f8SJohn Marino
275a45ae5f8SJohn Marino ALL_OBJFILES (objfile)
276a45ae5f8SJohn Marino {
277*ef5ccd6cSJohn Marino if (iterate_over_some_symtabs (name, real_path, callback, data,
278a45ae5f8SJohn Marino objfile->symtabs, NULL))
279a45ae5f8SJohn Marino {
280a45ae5f8SJohn Marino do_cleanups (cleanups);
281a45ae5f8SJohn Marino return;
282a45ae5f8SJohn Marino }
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert
2855796c8dcSSimon Schubert /* Same search rules as above apply here, but now we look thru the
2865796c8dcSSimon Schubert psymtabs. */
2875796c8dcSSimon Schubert
288cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
289cf7f2e2dSJohn Marino {
290cf7f2e2dSJohn Marino if (objfile->sf
291a45ae5f8SJohn Marino && objfile->sf->qf->map_symtabs_matching_filename (objfile,
292a45ae5f8SJohn Marino name,
293a45ae5f8SJohn Marino real_path,
294a45ae5f8SJohn Marino callback,
295a45ae5f8SJohn Marino data))
296cf7f2e2dSJohn Marino {
297a45ae5f8SJohn Marino do_cleanups (cleanups);
298a45ae5f8SJohn Marino return;
299cf7f2e2dSJohn Marino }
300cf7f2e2dSJohn Marino }
3015796c8dcSSimon Schubert
302a45ae5f8SJohn Marino do_cleanups (cleanups);
3035796c8dcSSimon Schubert }
304a45ae5f8SJohn Marino
305a45ae5f8SJohn Marino /* The callback function used by lookup_symtab. */
306a45ae5f8SJohn Marino
307a45ae5f8SJohn Marino static int
lookup_symtab_callback(struct symtab * symtab,void * data)308a45ae5f8SJohn Marino lookup_symtab_callback (struct symtab *symtab, void *data)
309a45ae5f8SJohn Marino {
310a45ae5f8SJohn Marino struct symtab **result_ptr = data;
311a45ae5f8SJohn Marino
312a45ae5f8SJohn Marino *result_ptr = symtab;
313a45ae5f8SJohn Marino return 1;
314a45ae5f8SJohn Marino }
315a45ae5f8SJohn Marino
316a45ae5f8SJohn Marino /* A wrapper for iterate_over_symtabs that returns the first matching
317a45ae5f8SJohn Marino symtab, or NULL. */
318a45ae5f8SJohn Marino
319a45ae5f8SJohn Marino struct symtab *
lookup_symtab(const char * name)320a45ae5f8SJohn Marino lookup_symtab (const char *name)
321a45ae5f8SJohn Marino {
322a45ae5f8SJohn Marino struct symtab *result = NULL;
323a45ae5f8SJohn Marino
324a45ae5f8SJohn Marino iterate_over_symtabs (name, lookup_symtab_callback, &result);
325a45ae5f8SJohn Marino return result;
326a45ae5f8SJohn Marino }
327a45ae5f8SJohn Marino
3285796c8dcSSimon Schubert
3295796c8dcSSimon Schubert /* Mangle a GDB method stub type. This actually reassembles the pieces of the
3305796c8dcSSimon Schubert full method name, which consist of the class name (from T), the unadorned
3315796c8dcSSimon Schubert method name from METHOD_ID, and the signature for the specific overload,
3325796c8dcSSimon Schubert specified by SIGNATURE_ID. Note that this function is g++ specific. */
3335796c8dcSSimon Schubert
3345796c8dcSSimon Schubert char *
gdb_mangle_name(struct type * type,int method_id,int signature_id)3355796c8dcSSimon Schubert gdb_mangle_name (struct type *type, int method_id, int signature_id)
3365796c8dcSSimon Schubert {
3375796c8dcSSimon Schubert int mangled_name_len;
3385796c8dcSSimon Schubert char *mangled_name;
3395796c8dcSSimon Schubert struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
3405796c8dcSSimon Schubert struct fn_field *method = &f[signature_id];
341*ef5ccd6cSJohn Marino const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
342a45ae5f8SJohn Marino const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
343*ef5ccd6cSJohn Marino const char *newname = type_name_no_tag (type);
3445796c8dcSSimon Schubert
3455796c8dcSSimon Schubert /* Does the form of physname indicate that it is the full mangled name
3465796c8dcSSimon Schubert of a constructor (not just the args)? */
3475796c8dcSSimon Schubert int is_full_physname_constructor;
3485796c8dcSSimon Schubert
3495796c8dcSSimon Schubert int is_constructor;
3505796c8dcSSimon Schubert int is_destructor = is_destructor_name (physname);
3515796c8dcSSimon Schubert /* Need a new type prefix. */
3525796c8dcSSimon Schubert char *const_prefix = method->is_const ? "C" : "";
3535796c8dcSSimon Schubert char *volatile_prefix = method->is_volatile ? "V" : "";
3545796c8dcSSimon Schubert char buf[20];
3555796c8dcSSimon Schubert int len = (newname == NULL ? 0 : strlen (newname));
3565796c8dcSSimon Schubert
3575796c8dcSSimon Schubert /* Nothing to do if physname already contains a fully mangled v3 abi name
3585796c8dcSSimon Schubert or an operator name. */
3595796c8dcSSimon Schubert if ((physname[0] == '_' && physname[1] == 'Z')
3605796c8dcSSimon Schubert || is_operator_name (field_name))
3615796c8dcSSimon Schubert return xstrdup (physname);
3625796c8dcSSimon Schubert
3635796c8dcSSimon Schubert is_full_physname_constructor = is_constructor_name (physname);
3645796c8dcSSimon Schubert
365c50c785cSJohn Marino is_constructor = is_full_physname_constructor
366c50c785cSJohn Marino || (newname && strcmp (field_name, newname) == 0);
3675796c8dcSSimon Schubert
3685796c8dcSSimon Schubert if (!is_destructor)
3695796c8dcSSimon Schubert is_destructor = (strncmp (physname, "__dt", 4) == 0);
3705796c8dcSSimon Schubert
3715796c8dcSSimon Schubert if (is_destructor || is_full_physname_constructor)
3725796c8dcSSimon Schubert {
3735796c8dcSSimon Schubert mangled_name = (char *) xmalloc (strlen (physname) + 1);
3745796c8dcSSimon Schubert strcpy (mangled_name, physname);
3755796c8dcSSimon Schubert return mangled_name;
3765796c8dcSSimon Schubert }
3775796c8dcSSimon Schubert
3785796c8dcSSimon Schubert if (len == 0)
3795796c8dcSSimon Schubert {
380*ef5ccd6cSJohn Marino xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
3815796c8dcSSimon Schubert }
3825796c8dcSSimon Schubert else if (physname[0] == 't' || physname[0] == 'Q')
3835796c8dcSSimon Schubert {
3845796c8dcSSimon Schubert /* The physname for template and qualified methods already includes
3855796c8dcSSimon Schubert the class name. */
386*ef5ccd6cSJohn Marino xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
3875796c8dcSSimon Schubert newname = NULL;
3885796c8dcSSimon Schubert len = 0;
3895796c8dcSSimon Schubert }
3905796c8dcSSimon Schubert else
3915796c8dcSSimon Schubert {
392*ef5ccd6cSJohn Marino xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
393*ef5ccd6cSJohn Marino volatile_prefix, len);
3945796c8dcSSimon Schubert }
3955796c8dcSSimon Schubert mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
3965796c8dcSSimon Schubert + strlen (buf) + len + strlen (physname) + 1);
3975796c8dcSSimon Schubert
3985796c8dcSSimon Schubert mangled_name = (char *) xmalloc (mangled_name_len);
3995796c8dcSSimon Schubert if (is_constructor)
4005796c8dcSSimon Schubert mangled_name[0] = '\0';
4015796c8dcSSimon Schubert else
4025796c8dcSSimon Schubert strcpy (mangled_name, field_name);
403cf7f2e2dSJohn Marino
4045796c8dcSSimon Schubert strcat (mangled_name, buf);
4055796c8dcSSimon Schubert /* If the class doesn't have a name, i.e. newname NULL, then we just
4065796c8dcSSimon Schubert mangle it using 0 for the length of the class. Thus it gets mangled
4075796c8dcSSimon Schubert as something starting with `::' rather than `classname::'. */
4085796c8dcSSimon Schubert if (newname != NULL)
4095796c8dcSSimon Schubert strcat (mangled_name, newname);
4105796c8dcSSimon Schubert
4115796c8dcSSimon Schubert strcat (mangled_name, physname);
4125796c8dcSSimon Schubert return (mangled_name);
4135796c8dcSSimon Schubert }
4145796c8dcSSimon Schubert
415c50c785cSJohn Marino /* Initialize the cplus_specific structure. 'cplus_specific' should
416c50c785cSJohn Marino only be allocated for use with cplus symbols. */
417c50c785cSJohn Marino
418c50c785cSJohn Marino static void
symbol_init_cplus_specific(struct general_symbol_info * gsymbol,struct objfile * objfile)419c50c785cSJohn Marino symbol_init_cplus_specific (struct general_symbol_info *gsymbol,
420c50c785cSJohn Marino struct objfile *objfile)
421c50c785cSJohn Marino {
422c50c785cSJohn Marino /* A language_specific structure should not have been previously
423c50c785cSJohn Marino initialized. */
424c50c785cSJohn Marino gdb_assert (gsymbol->language_specific.cplus_specific == NULL);
425c50c785cSJohn Marino gdb_assert (objfile != NULL);
426c50c785cSJohn Marino
427c50c785cSJohn Marino gsymbol->language_specific.cplus_specific =
428c50c785cSJohn Marino OBSTACK_ZALLOC (&objfile->objfile_obstack, struct cplus_specific);
429c50c785cSJohn Marino }
430c50c785cSJohn Marino
431c50c785cSJohn Marino /* Set the demangled name of GSYMBOL to NAME. NAME must be already
432c50c785cSJohn Marino correctly allocated. For C++ symbols a cplus_specific struct is
433c50c785cSJohn Marino allocated so OBJFILE must not be NULL. If this is a non C++ symbol
434c50c785cSJohn Marino OBJFILE can be NULL. */
435*ef5ccd6cSJohn Marino
436c50c785cSJohn Marino void
symbol_set_demangled_name(struct general_symbol_info * gsymbol,const char * name,struct objfile * objfile)437c50c785cSJohn Marino symbol_set_demangled_name (struct general_symbol_info *gsymbol,
438*ef5ccd6cSJohn Marino const char *name,
439c50c785cSJohn Marino struct objfile *objfile)
440c50c785cSJohn Marino {
441c50c785cSJohn Marino if (gsymbol->language == language_cplus)
442c50c785cSJohn Marino {
443c50c785cSJohn Marino if (gsymbol->language_specific.cplus_specific == NULL)
444c50c785cSJohn Marino symbol_init_cplus_specific (gsymbol, objfile);
445c50c785cSJohn Marino
446c50c785cSJohn Marino gsymbol->language_specific.cplus_specific->demangled_name = name;
447c50c785cSJohn Marino }
448c50c785cSJohn Marino else
449c50c785cSJohn Marino gsymbol->language_specific.mangled_lang.demangled_name = name;
450c50c785cSJohn Marino }
451c50c785cSJohn Marino
452c50c785cSJohn Marino /* Return the demangled name of GSYMBOL. */
453*ef5ccd6cSJohn Marino
454*ef5ccd6cSJohn Marino const char *
symbol_get_demangled_name(const struct general_symbol_info * gsymbol)455c50c785cSJohn Marino symbol_get_demangled_name (const struct general_symbol_info *gsymbol)
456c50c785cSJohn Marino {
457c50c785cSJohn Marino if (gsymbol->language == language_cplus)
458c50c785cSJohn Marino {
459c50c785cSJohn Marino if (gsymbol->language_specific.cplus_specific != NULL)
460c50c785cSJohn Marino return gsymbol->language_specific.cplus_specific->demangled_name;
461c50c785cSJohn Marino else
462c50c785cSJohn Marino return NULL;
463c50c785cSJohn Marino }
464c50c785cSJohn Marino else
465c50c785cSJohn Marino return gsymbol->language_specific.mangled_lang.demangled_name;
466c50c785cSJohn Marino }
467c50c785cSJohn Marino
4685796c8dcSSimon Schubert
4695796c8dcSSimon Schubert /* Initialize the language dependent portion of a symbol
4705796c8dcSSimon Schubert depending upon the language for the symbol. */
471*ef5ccd6cSJohn Marino
4725796c8dcSSimon Schubert void
symbol_set_language(struct general_symbol_info * gsymbol,enum language language)473c50c785cSJohn Marino symbol_set_language (struct general_symbol_info *gsymbol,
4745796c8dcSSimon Schubert enum language language)
4755796c8dcSSimon Schubert {
4765796c8dcSSimon Schubert gsymbol->language = language;
477c50c785cSJohn Marino if (gsymbol->language == language_d
478*ef5ccd6cSJohn Marino || gsymbol->language == language_go
4795796c8dcSSimon Schubert || gsymbol->language == language_java
480cf7f2e2dSJohn Marino || gsymbol->language == language_objc
481cf7f2e2dSJohn Marino || gsymbol->language == language_fortran)
4825796c8dcSSimon Schubert {
483c50c785cSJohn Marino symbol_set_demangled_name (gsymbol, NULL, NULL);
4845796c8dcSSimon Schubert }
485c50c785cSJohn Marino else if (gsymbol->language == language_cplus)
486c50c785cSJohn Marino gsymbol->language_specific.cplus_specific = NULL;
4875796c8dcSSimon Schubert else
4885796c8dcSSimon Schubert {
4895796c8dcSSimon Schubert memset (&gsymbol->language_specific, 0,
4905796c8dcSSimon Schubert sizeof (gsymbol->language_specific));
4915796c8dcSSimon Schubert }
4925796c8dcSSimon Schubert }
4935796c8dcSSimon Schubert
4945796c8dcSSimon Schubert /* Functions to initialize a symbol's mangled name. */
4955796c8dcSSimon Schubert
496cf7f2e2dSJohn Marino /* Objects of this type are stored in the demangled name hash table. */
497cf7f2e2dSJohn Marino struct demangled_name_entry
498cf7f2e2dSJohn Marino {
499*ef5ccd6cSJohn Marino const char *mangled;
500cf7f2e2dSJohn Marino char demangled[1];
501cf7f2e2dSJohn Marino };
502cf7f2e2dSJohn Marino
503cf7f2e2dSJohn Marino /* Hash function for the demangled name hash. */
504*ef5ccd6cSJohn Marino
505cf7f2e2dSJohn Marino static hashval_t
hash_demangled_name_entry(const void * data)506cf7f2e2dSJohn Marino hash_demangled_name_entry (const void *data)
507cf7f2e2dSJohn Marino {
508cf7f2e2dSJohn Marino const struct demangled_name_entry *e = data;
509cf7f2e2dSJohn Marino
510cf7f2e2dSJohn Marino return htab_hash_string (e->mangled);
511cf7f2e2dSJohn Marino }
512cf7f2e2dSJohn Marino
513cf7f2e2dSJohn Marino /* Equality function for the demangled name hash. */
514*ef5ccd6cSJohn Marino
515cf7f2e2dSJohn Marino static int
eq_demangled_name_entry(const void * a,const void * b)516cf7f2e2dSJohn Marino eq_demangled_name_entry (const void *a, const void *b)
517cf7f2e2dSJohn Marino {
518cf7f2e2dSJohn Marino const struct demangled_name_entry *da = a;
519cf7f2e2dSJohn Marino const struct demangled_name_entry *db = b;
520cf7f2e2dSJohn Marino
521cf7f2e2dSJohn Marino return strcmp (da->mangled, db->mangled) == 0;
522cf7f2e2dSJohn Marino }
523cf7f2e2dSJohn Marino
5245796c8dcSSimon Schubert /* Create the hash table used for demangled names. Each hash entry is
5255796c8dcSSimon Schubert a pair of strings; one for the mangled name and one for the demangled
5265796c8dcSSimon Schubert name. The entry is hashed via just the mangled name. */
5275796c8dcSSimon Schubert
5285796c8dcSSimon Schubert static void
create_demangled_names_hash(struct objfile * objfile)5295796c8dcSSimon Schubert create_demangled_names_hash (struct objfile *objfile)
5305796c8dcSSimon Schubert {
5315796c8dcSSimon Schubert /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
5325796c8dcSSimon Schubert The hash table code will round this up to the next prime number.
5335796c8dcSSimon Schubert Choosing a much larger table size wastes memory, and saves only about
5345796c8dcSSimon Schubert 1% in symbol reading. */
5355796c8dcSSimon Schubert
5365796c8dcSSimon Schubert objfile->demangled_names_hash = htab_create_alloc
537cf7f2e2dSJohn Marino (256, hash_demangled_name_entry, eq_demangled_name_entry,
5385796c8dcSSimon Schubert NULL, xcalloc, xfree);
5395796c8dcSSimon Schubert }
5405796c8dcSSimon Schubert
5415796c8dcSSimon Schubert /* Try to determine the demangled name for a symbol, based on the
5425796c8dcSSimon Schubert language of that symbol. If the language is set to language_auto,
5435796c8dcSSimon Schubert it will attempt to find any demangling algorithm that works and
5445796c8dcSSimon Schubert then set the language appropriately. The returned name is allocated
5455796c8dcSSimon Schubert by the demangler and should be xfree'd. */
5465796c8dcSSimon Schubert
5475796c8dcSSimon Schubert static char *
symbol_find_demangled_name(struct general_symbol_info * gsymbol,const char * mangled)5485796c8dcSSimon Schubert symbol_find_demangled_name (struct general_symbol_info *gsymbol,
5495796c8dcSSimon Schubert const char *mangled)
5505796c8dcSSimon Schubert {
5515796c8dcSSimon Schubert char *demangled = NULL;
5525796c8dcSSimon Schubert
5535796c8dcSSimon Schubert if (gsymbol->language == language_unknown)
5545796c8dcSSimon Schubert gsymbol->language = language_auto;
5555796c8dcSSimon Schubert
5565796c8dcSSimon Schubert if (gsymbol->language == language_objc
5575796c8dcSSimon Schubert || gsymbol->language == language_auto)
5585796c8dcSSimon Schubert {
5595796c8dcSSimon Schubert demangled =
5605796c8dcSSimon Schubert objc_demangle (mangled, 0);
5615796c8dcSSimon Schubert if (demangled != NULL)
5625796c8dcSSimon Schubert {
5635796c8dcSSimon Schubert gsymbol->language = language_objc;
5645796c8dcSSimon Schubert return demangled;
5655796c8dcSSimon Schubert }
5665796c8dcSSimon Schubert }
5675796c8dcSSimon Schubert if (gsymbol->language == language_cplus
5685796c8dcSSimon Schubert || gsymbol->language == language_auto)
5695796c8dcSSimon Schubert {
5705796c8dcSSimon Schubert demangled =
571c50c785cSJohn Marino cplus_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
5725796c8dcSSimon Schubert if (demangled != NULL)
5735796c8dcSSimon Schubert {
5745796c8dcSSimon Schubert gsymbol->language = language_cplus;
5755796c8dcSSimon Schubert return demangled;
5765796c8dcSSimon Schubert }
5775796c8dcSSimon Schubert }
5785796c8dcSSimon Schubert if (gsymbol->language == language_java)
5795796c8dcSSimon Schubert {
5805796c8dcSSimon Schubert demangled =
5815796c8dcSSimon Schubert cplus_demangle (mangled,
5825796c8dcSSimon Schubert DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
5835796c8dcSSimon Schubert if (demangled != NULL)
5845796c8dcSSimon Schubert {
5855796c8dcSSimon Schubert gsymbol->language = language_java;
5865796c8dcSSimon Schubert return demangled;
5875796c8dcSSimon Schubert }
5885796c8dcSSimon Schubert }
589cf7f2e2dSJohn Marino if (gsymbol->language == language_d
590cf7f2e2dSJohn Marino || gsymbol->language == language_auto)
591cf7f2e2dSJohn Marino {
592cf7f2e2dSJohn Marino demangled = d_demangle(mangled, 0);
593cf7f2e2dSJohn Marino if (demangled != NULL)
594cf7f2e2dSJohn Marino {
595cf7f2e2dSJohn Marino gsymbol->language = language_d;
596cf7f2e2dSJohn Marino return demangled;
597cf7f2e2dSJohn Marino }
598cf7f2e2dSJohn Marino }
599*ef5ccd6cSJohn Marino /* FIXME(dje): Continually adding languages here is clumsy.
600*ef5ccd6cSJohn Marino Better to just call la_demangle if !auto, and if auto then call
601*ef5ccd6cSJohn Marino a utility routine that tries successive languages in turn and reports
602*ef5ccd6cSJohn Marino which one it finds. I realize the la_demangle options may be different
603*ef5ccd6cSJohn Marino for different languages but there's already a FIXME for that. */
604*ef5ccd6cSJohn Marino if (gsymbol->language == language_go
605*ef5ccd6cSJohn Marino || gsymbol->language == language_auto)
606*ef5ccd6cSJohn Marino {
607*ef5ccd6cSJohn Marino demangled = go_demangle (mangled, 0);
608*ef5ccd6cSJohn Marino if (demangled != NULL)
609*ef5ccd6cSJohn Marino {
610*ef5ccd6cSJohn Marino gsymbol->language = language_go;
611*ef5ccd6cSJohn Marino return demangled;
612*ef5ccd6cSJohn Marino }
613*ef5ccd6cSJohn Marino }
614*ef5ccd6cSJohn Marino
615cf7f2e2dSJohn Marino /* We could support `gsymbol->language == language_fortran' here to provide
616cf7f2e2dSJohn Marino module namespaces also for inferiors with only minimal symbol table (ELF
617cf7f2e2dSJohn Marino symbols). Just the mangling standard is not standardized across compilers
618cf7f2e2dSJohn Marino and there is no DW_AT_producer available for inferiors with only the ELF
619cf7f2e2dSJohn Marino symbols to check the mangling kind. */
6205796c8dcSSimon Schubert return NULL;
6215796c8dcSSimon Schubert }
6225796c8dcSSimon Schubert
6235796c8dcSSimon Schubert /* Set both the mangled and demangled (if any) names for GSYMBOL based
624cf7f2e2dSJohn Marino on LINKAGE_NAME and LEN. Ordinarily, NAME is copied onto the
625cf7f2e2dSJohn Marino objfile's obstack; but if COPY_NAME is 0 and if NAME is
626cf7f2e2dSJohn Marino NUL-terminated, then this function assumes that NAME is already
627cf7f2e2dSJohn Marino correctly saved (either permanently or with a lifetime tied to the
628cf7f2e2dSJohn Marino objfile), and it will not be copied.
629cf7f2e2dSJohn Marino
630cf7f2e2dSJohn Marino The hash table corresponding to OBJFILE is used, and the memory
631cf7f2e2dSJohn Marino comes from that objfile's objfile_obstack. LINKAGE_NAME is copied,
632cf7f2e2dSJohn Marino so the pointer can be discarded after calling this function. */
6335796c8dcSSimon Schubert
6345796c8dcSSimon Schubert /* We have to be careful when dealing with Java names: when we run
6355796c8dcSSimon Schubert into a Java minimal symbol, we don't know it's a Java symbol, so it
6365796c8dcSSimon Schubert gets demangled as a C++ name. This is unfortunate, but there's not
6375796c8dcSSimon Schubert much we can do about it: but when demangling partial symbols and
6385796c8dcSSimon Schubert regular symbols, we'd better not reuse the wrong demangled name.
6395796c8dcSSimon Schubert (See PR gdb/1039.) We solve this by putting a distinctive prefix
6405796c8dcSSimon Schubert on Java names when storing them in the hash table. */
6415796c8dcSSimon Schubert
6425796c8dcSSimon Schubert /* FIXME: carlton/2003-03-13: This is an unfortunate situation. I
6435796c8dcSSimon Schubert don't mind the Java prefix so much: different languages have
6445796c8dcSSimon Schubert different demangling requirements, so it's only natural that we
6455796c8dcSSimon Schubert need to keep language data around in our demangling cache. But
6465796c8dcSSimon Schubert it's not good that the minimal symbol has the wrong demangled name.
6475796c8dcSSimon Schubert Unfortunately, I can't think of any easy solution to that
6485796c8dcSSimon Schubert problem. */
6495796c8dcSSimon Schubert
6505796c8dcSSimon Schubert #define JAVA_PREFIX "##JAVA$$"
6515796c8dcSSimon Schubert #define JAVA_PREFIX_LEN 8
6525796c8dcSSimon Schubert
6535796c8dcSSimon Schubert void
symbol_set_names(struct general_symbol_info * gsymbol,const char * linkage_name,int len,int copy_name,struct objfile * objfile)6545796c8dcSSimon Schubert symbol_set_names (struct general_symbol_info *gsymbol,
655cf7f2e2dSJohn Marino const char *linkage_name, int len, int copy_name,
656cf7f2e2dSJohn Marino struct objfile *objfile)
6575796c8dcSSimon Schubert {
658cf7f2e2dSJohn Marino struct demangled_name_entry **slot;
6595796c8dcSSimon Schubert /* A 0-terminated copy of the linkage name. */
6605796c8dcSSimon Schubert const char *linkage_name_copy;
6615796c8dcSSimon Schubert /* A copy of the linkage name that might have a special Java prefix
6625796c8dcSSimon Schubert added to it, for use when looking names up in the hash table. */
6635796c8dcSSimon Schubert const char *lookup_name;
6645796c8dcSSimon Schubert /* The length of lookup_name. */
6655796c8dcSSimon Schubert int lookup_len;
666cf7f2e2dSJohn Marino struct demangled_name_entry entry;
6675796c8dcSSimon Schubert
6685796c8dcSSimon Schubert if (gsymbol->language == language_ada)
6695796c8dcSSimon Schubert {
6705796c8dcSSimon Schubert /* In Ada, we do the symbol lookups using the mangled name, so
6715796c8dcSSimon Schubert we can save some space by not storing the demangled name.
6725796c8dcSSimon Schubert
6735796c8dcSSimon Schubert As a side note, we have also observed some overlap between
6745796c8dcSSimon Schubert the C++ mangling and Ada mangling, similarly to what has
6755796c8dcSSimon Schubert been observed with Java. Because we don't store the demangled
6765796c8dcSSimon Schubert name with the symbol, we don't need to use the same trick
6775796c8dcSSimon Schubert as Java. */
678cf7f2e2dSJohn Marino if (!copy_name)
679*ef5ccd6cSJohn Marino gsymbol->name = linkage_name;
680cf7f2e2dSJohn Marino else
681cf7f2e2dSJohn Marino {
682*ef5ccd6cSJohn Marino char *name = obstack_alloc (&objfile->objfile_obstack, len + 1);
683*ef5ccd6cSJohn Marino
684*ef5ccd6cSJohn Marino memcpy (name, linkage_name, len);
685*ef5ccd6cSJohn Marino name[len] = '\0';
686*ef5ccd6cSJohn Marino gsymbol->name = name;
687cf7f2e2dSJohn Marino }
688c50c785cSJohn Marino symbol_set_demangled_name (gsymbol, NULL, NULL);
6895796c8dcSSimon Schubert
6905796c8dcSSimon Schubert return;
6915796c8dcSSimon Schubert }
6925796c8dcSSimon Schubert
693cf7f2e2dSJohn Marino if (objfile->demangled_names_hash == NULL)
694cf7f2e2dSJohn Marino create_demangled_names_hash (objfile);
695cf7f2e2dSJohn Marino
6965796c8dcSSimon Schubert /* The stabs reader generally provides names that are not
6975796c8dcSSimon Schubert NUL-terminated; most of the other readers don't do this, so we
6985796c8dcSSimon Schubert can just use the given copy, unless we're in the Java case. */
6995796c8dcSSimon Schubert if (gsymbol->language == language_java)
7005796c8dcSSimon Schubert {
7015796c8dcSSimon Schubert char *alloc_name;
7025796c8dcSSimon Schubert
703cf7f2e2dSJohn Marino lookup_len = len + JAVA_PREFIX_LEN;
7045796c8dcSSimon Schubert alloc_name = alloca (lookup_len + 1);
7055796c8dcSSimon Schubert memcpy (alloc_name, JAVA_PREFIX, JAVA_PREFIX_LEN);
7065796c8dcSSimon Schubert memcpy (alloc_name + JAVA_PREFIX_LEN, linkage_name, len);
7075796c8dcSSimon Schubert alloc_name[lookup_len] = '\0';
7085796c8dcSSimon Schubert
7095796c8dcSSimon Schubert lookup_name = alloc_name;
7105796c8dcSSimon Schubert linkage_name_copy = alloc_name + JAVA_PREFIX_LEN;
7115796c8dcSSimon Schubert }
7125796c8dcSSimon Schubert else if (linkage_name[len] != '\0')
7135796c8dcSSimon Schubert {
7145796c8dcSSimon Schubert char *alloc_name;
7155796c8dcSSimon Schubert
716cf7f2e2dSJohn Marino lookup_len = len;
7175796c8dcSSimon Schubert alloc_name = alloca (lookup_len + 1);
7185796c8dcSSimon Schubert memcpy (alloc_name, linkage_name, len);
7195796c8dcSSimon Schubert alloc_name[lookup_len] = '\0';
7205796c8dcSSimon Schubert
7215796c8dcSSimon Schubert lookup_name = alloc_name;
7225796c8dcSSimon Schubert linkage_name_copy = alloc_name;
7235796c8dcSSimon Schubert }
7245796c8dcSSimon Schubert else
7255796c8dcSSimon Schubert {
7265796c8dcSSimon Schubert lookup_len = len;
7275796c8dcSSimon Schubert lookup_name = linkage_name;
7285796c8dcSSimon Schubert linkage_name_copy = linkage_name;
7295796c8dcSSimon Schubert }
7305796c8dcSSimon Schubert
731*ef5ccd6cSJohn Marino entry.mangled = lookup_name;
732cf7f2e2dSJohn Marino slot = ((struct demangled_name_entry **)
733cf7f2e2dSJohn Marino htab_find_slot (objfile->demangled_names_hash,
734cf7f2e2dSJohn Marino &entry, INSERT));
7355796c8dcSSimon Schubert
7365796c8dcSSimon Schubert /* If this name is not in the hash table, add it. */
737*ef5ccd6cSJohn Marino if (*slot == NULL
738*ef5ccd6cSJohn Marino /* A C version of the symbol may have already snuck into the table.
739*ef5ccd6cSJohn Marino This happens to, e.g., main.init (__go_init_main). Cope. */
740*ef5ccd6cSJohn Marino || (gsymbol->language == language_go
741*ef5ccd6cSJohn Marino && (*slot)->demangled[0] == '\0'))
7425796c8dcSSimon Schubert {
7435796c8dcSSimon Schubert char *demangled_name = symbol_find_demangled_name (gsymbol,
7445796c8dcSSimon Schubert linkage_name_copy);
7455796c8dcSSimon Schubert int demangled_len = demangled_name ? strlen (demangled_name) : 0;
7465796c8dcSSimon Schubert
747cf7f2e2dSJohn Marino /* Suppose we have demangled_name==NULL, copy_name==0, and
748cf7f2e2dSJohn Marino lookup_name==linkage_name. In this case, we already have the
749cf7f2e2dSJohn Marino mangled name saved, and we don't have a demangled name. So,
750cf7f2e2dSJohn Marino you might think we could save a little space by not recording
751cf7f2e2dSJohn Marino this in the hash table at all.
752cf7f2e2dSJohn Marino
753cf7f2e2dSJohn Marino It turns out that it is actually important to still save such
754cf7f2e2dSJohn Marino an entry in the hash table, because storing this name gives
755cf7f2e2dSJohn Marino us better bcache hit rates for partial symbols. */
756cf7f2e2dSJohn Marino if (!copy_name && lookup_name == linkage_name)
757cf7f2e2dSJohn Marino {
7585796c8dcSSimon Schubert *slot = obstack_alloc (&objfile->objfile_obstack,
759cf7f2e2dSJohn Marino offsetof (struct demangled_name_entry,
760cf7f2e2dSJohn Marino demangled)
761cf7f2e2dSJohn Marino + demangled_len + 1);
762*ef5ccd6cSJohn Marino (*slot)->mangled = lookup_name;
763cf7f2e2dSJohn Marino }
764cf7f2e2dSJohn Marino else
765cf7f2e2dSJohn Marino {
766*ef5ccd6cSJohn Marino char *mangled_ptr;
767*ef5ccd6cSJohn Marino
768cf7f2e2dSJohn Marino /* If we must copy the mangled name, put it directly after
769cf7f2e2dSJohn Marino the demangled name so we can have a single
770cf7f2e2dSJohn Marino allocation. */
771cf7f2e2dSJohn Marino *slot = obstack_alloc (&objfile->objfile_obstack,
772cf7f2e2dSJohn Marino offsetof (struct demangled_name_entry,
773cf7f2e2dSJohn Marino demangled)
774cf7f2e2dSJohn Marino + lookup_len + demangled_len + 2);
775*ef5ccd6cSJohn Marino mangled_ptr = &((*slot)->demangled[demangled_len + 1]);
776*ef5ccd6cSJohn Marino strcpy (mangled_ptr, lookup_name);
777*ef5ccd6cSJohn Marino (*slot)->mangled = mangled_ptr;
778cf7f2e2dSJohn Marino }
779cf7f2e2dSJohn Marino
7805796c8dcSSimon Schubert if (demangled_name != NULL)
7815796c8dcSSimon Schubert {
782cf7f2e2dSJohn Marino strcpy ((*slot)->demangled, demangled_name);
7835796c8dcSSimon Schubert xfree (demangled_name);
7845796c8dcSSimon Schubert }
7855796c8dcSSimon Schubert else
786cf7f2e2dSJohn Marino (*slot)->demangled[0] = '\0';
7875796c8dcSSimon Schubert }
7885796c8dcSSimon Schubert
789cf7f2e2dSJohn Marino gsymbol->name = (*slot)->mangled + lookup_len - len;
790cf7f2e2dSJohn Marino if ((*slot)->demangled[0] != '\0')
791c50c785cSJohn Marino symbol_set_demangled_name (gsymbol, (*slot)->demangled, objfile);
7925796c8dcSSimon Schubert else
793c50c785cSJohn Marino symbol_set_demangled_name (gsymbol, NULL, objfile);
7945796c8dcSSimon Schubert }
7955796c8dcSSimon Schubert
7965796c8dcSSimon Schubert /* Return the source code name of a symbol. In languages where
7975796c8dcSSimon Schubert demangling is necessary, this is the demangled name. */
7985796c8dcSSimon Schubert
799*ef5ccd6cSJohn Marino const char *
symbol_natural_name(const struct general_symbol_info * gsymbol)8005796c8dcSSimon Schubert symbol_natural_name (const struct general_symbol_info *gsymbol)
8015796c8dcSSimon Schubert {
8025796c8dcSSimon Schubert switch (gsymbol->language)
8035796c8dcSSimon Schubert {
8045796c8dcSSimon Schubert case language_cplus:
805cf7f2e2dSJohn Marino case language_d:
806*ef5ccd6cSJohn Marino case language_go:
8075796c8dcSSimon Schubert case language_java:
8085796c8dcSSimon Schubert case language_objc:
809cf7f2e2dSJohn Marino case language_fortran:
810c50c785cSJohn Marino if (symbol_get_demangled_name (gsymbol) != NULL)
811c50c785cSJohn Marino return symbol_get_demangled_name (gsymbol);
8125796c8dcSSimon Schubert break;
8135796c8dcSSimon Schubert case language_ada:
814c50c785cSJohn Marino if (symbol_get_demangled_name (gsymbol) != NULL)
815c50c785cSJohn Marino return symbol_get_demangled_name (gsymbol);
8165796c8dcSSimon Schubert else
8175796c8dcSSimon Schubert return ada_decode_symbol (gsymbol);
8185796c8dcSSimon Schubert break;
8195796c8dcSSimon Schubert default:
8205796c8dcSSimon Schubert break;
8215796c8dcSSimon Schubert }
8225796c8dcSSimon Schubert return gsymbol->name;
8235796c8dcSSimon Schubert }
8245796c8dcSSimon Schubert
8255796c8dcSSimon Schubert /* Return the demangled name for a symbol based on the language for
8265796c8dcSSimon Schubert that symbol. If no demangled name exists, return NULL. */
827*ef5ccd6cSJohn Marino
828*ef5ccd6cSJohn Marino const char *
symbol_demangled_name(const struct general_symbol_info * gsymbol)8295796c8dcSSimon Schubert symbol_demangled_name (const struct general_symbol_info *gsymbol)
8305796c8dcSSimon Schubert {
831*ef5ccd6cSJohn Marino const char *dem_name = NULL;
832*ef5ccd6cSJohn Marino
8335796c8dcSSimon Schubert switch (gsymbol->language)
8345796c8dcSSimon Schubert {
8355796c8dcSSimon Schubert case language_cplus:
836cf7f2e2dSJohn Marino case language_d:
837*ef5ccd6cSJohn Marino case language_go:
8385796c8dcSSimon Schubert case language_java:
8395796c8dcSSimon Schubert case language_objc:
840cf7f2e2dSJohn Marino case language_fortran:
841*ef5ccd6cSJohn Marino dem_name = symbol_get_demangled_name (gsymbol);
8425796c8dcSSimon Schubert break;
8435796c8dcSSimon Schubert case language_ada:
844*ef5ccd6cSJohn Marino dem_name = symbol_get_demangled_name (gsymbol);
845*ef5ccd6cSJohn Marino if (dem_name == NULL)
846*ef5ccd6cSJohn Marino dem_name = ada_decode_symbol (gsymbol);
8475796c8dcSSimon Schubert break;
8485796c8dcSSimon Schubert default:
8495796c8dcSSimon Schubert break;
8505796c8dcSSimon Schubert }
851*ef5ccd6cSJohn Marino return dem_name;
8525796c8dcSSimon Schubert }
8535796c8dcSSimon Schubert
8545796c8dcSSimon Schubert /* Return the search name of a symbol---generally the demangled or
8555796c8dcSSimon Schubert linkage name of the symbol, depending on how it will be searched for.
8565796c8dcSSimon Schubert If there is no distinct demangled name, then returns the same value
8575796c8dcSSimon Schubert (same pointer) as SYMBOL_LINKAGE_NAME. */
858*ef5ccd6cSJohn Marino
859*ef5ccd6cSJohn Marino const char *
symbol_search_name(const struct general_symbol_info * gsymbol)8605796c8dcSSimon Schubert symbol_search_name (const struct general_symbol_info *gsymbol)
8615796c8dcSSimon Schubert {
8625796c8dcSSimon Schubert if (gsymbol->language == language_ada)
8635796c8dcSSimon Schubert return gsymbol->name;
8645796c8dcSSimon Schubert else
8655796c8dcSSimon Schubert return symbol_natural_name (gsymbol);
8665796c8dcSSimon Schubert }
8675796c8dcSSimon Schubert
8685796c8dcSSimon Schubert /* Initialize the structure fields to zero values. */
869*ef5ccd6cSJohn Marino
8705796c8dcSSimon Schubert void
init_sal(struct symtab_and_line * sal)8715796c8dcSSimon Schubert init_sal (struct symtab_and_line *sal)
8725796c8dcSSimon Schubert {
873cf7f2e2dSJohn Marino sal->pspace = NULL;
8745796c8dcSSimon Schubert sal->symtab = 0;
8755796c8dcSSimon Schubert sal->section = 0;
8765796c8dcSSimon Schubert sal->line = 0;
8775796c8dcSSimon Schubert sal->pc = 0;
8785796c8dcSSimon Schubert sal->end = 0;
8795796c8dcSSimon Schubert sal->explicit_pc = 0;
8805796c8dcSSimon Schubert sal->explicit_line = 0;
881*ef5ccd6cSJohn Marino sal->probe = NULL;
8825796c8dcSSimon Schubert }
8835796c8dcSSimon Schubert
8845796c8dcSSimon Schubert
8855796c8dcSSimon Schubert /* Return 1 if the two sections are the same, or if they could
8865796c8dcSSimon Schubert plausibly be copies of each other, one in an original object
8875796c8dcSSimon Schubert file and another in a separated debug file. */
8885796c8dcSSimon Schubert
8895796c8dcSSimon Schubert int
matching_obj_sections(struct obj_section * obj_first,struct obj_section * obj_second)8905796c8dcSSimon Schubert matching_obj_sections (struct obj_section *obj_first,
8915796c8dcSSimon Schubert struct obj_section *obj_second)
8925796c8dcSSimon Schubert {
8935796c8dcSSimon Schubert asection *first = obj_first? obj_first->the_bfd_section : NULL;
8945796c8dcSSimon Schubert asection *second = obj_second? obj_second->the_bfd_section : NULL;
8955796c8dcSSimon Schubert struct objfile *obj;
8965796c8dcSSimon Schubert
8975796c8dcSSimon Schubert /* If they're the same section, then they match. */
8985796c8dcSSimon Schubert if (first == second)
8995796c8dcSSimon Schubert return 1;
9005796c8dcSSimon Schubert
9015796c8dcSSimon Schubert /* If either is NULL, give up. */
9025796c8dcSSimon Schubert if (first == NULL || second == NULL)
9035796c8dcSSimon Schubert return 0;
9045796c8dcSSimon Schubert
9055796c8dcSSimon Schubert /* This doesn't apply to absolute symbols. */
9065796c8dcSSimon Schubert if (first->owner == NULL || second->owner == NULL)
9075796c8dcSSimon Schubert return 0;
9085796c8dcSSimon Schubert
9095796c8dcSSimon Schubert /* If they're in the same object file, they must be different sections. */
9105796c8dcSSimon Schubert if (first->owner == second->owner)
9115796c8dcSSimon Schubert return 0;
9125796c8dcSSimon Schubert
9135796c8dcSSimon Schubert /* Check whether the two sections are potentially corresponding. They must
9145796c8dcSSimon Schubert have the same size, address, and name. We can't compare section indexes,
9155796c8dcSSimon Schubert which would be more reliable, because some sections may have been
9165796c8dcSSimon Schubert stripped. */
9175796c8dcSSimon Schubert if (bfd_get_section_size (first) != bfd_get_section_size (second))
9185796c8dcSSimon Schubert return 0;
9195796c8dcSSimon Schubert
9205796c8dcSSimon Schubert /* In-memory addresses may start at a different offset, relativize them. */
9215796c8dcSSimon Schubert if (bfd_get_section_vma (first->owner, first)
9225796c8dcSSimon Schubert - bfd_get_start_address (first->owner)
9235796c8dcSSimon Schubert != bfd_get_section_vma (second->owner, second)
9245796c8dcSSimon Schubert - bfd_get_start_address (second->owner))
9255796c8dcSSimon Schubert return 0;
9265796c8dcSSimon Schubert
9275796c8dcSSimon Schubert if (bfd_get_section_name (first->owner, first) == NULL
9285796c8dcSSimon Schubert || bfd_get_section_name (second->owner, second) == NULL
9295796c8dcSSimon Schubert || strcmp (bfd_get_section_name (first->owner, first),
9305796c8dcSSimon Schubert bfd_get_section_name (second->owner, second)) != 0)
9315796c8dcSSimon Schubert return 0;
9325796c8dcSSimon Schubert
9335796c8dcSSimon Schubert /* Otherwise check that they are in corresponding objfiles. */
9345796c8dcSSimon Schubert
9355796c8dcSSimon Schubert ALL_OBJFILES (obj)
9365796c8dcSSimon Schubert if (obj->obfd == first->owner)
9375796c8dcSSimon Schubert break;
9385796c8dcSSimon Schubert gdb_assert (obj != NULL);
9395796c8dcSSimon Schubert
9405796c8dcSSimon Schubert if (obj->separate_debug_objfile != NULL
9415796c8dcSSimon Schubert && obj->separate_debug_objfile->obfd == second->owner)
9425796c8dcSSimon Schubert return 1;
9435796c8dcSSimon Schubert if (obj->separate_debug_objfile_backlink != NULL
9445796c8dcSSimon Schubert && obj->separate_debug_objfile_backlink->obfd == second->owner)
9455796c8dcSSimon Schubert return 1;
9465796c8dcSSimon Schubert
9475796c8dcSSimon Schubert return 0;
9485796c8dcSSimon Schubert }
9495796c8dcSSimon Schubert
950cf7f2e2dSJohn Marino struct symtab *
find_pc_sect_symtab_via_partial(CORE_ADDR pc,struct obj_section * section)951cf7f2e2dSJohn Marino find_pc_sect_symtab_via_partial (CORE_ADDR pc, struct obj_section *section)
9525796c8dcSSimon Schubert {
9535796c8dcSSimon Schubert struct objfile *objfile;
9545796c8dcSSimon Schubert struct minimal_symbol *msymbol;
9555796c8dcSSimon Schubert
9565796c8dcSSimon Schubert /* If we know that this is not a text address, return failure. This is
9575796c8dcSSimon Schubert necessary because we loop based on texthigh and textlow, which do
9585796c8dcSSimon Schubert not include the data ranges. */
9595796c8dcSSimon Schubert msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
9605796c8dcSSimon Schubert if (msymbol
9615796c8dcSSimon Schubert && (MSYMBOL_TYPE (msymbol) == mst_data
9625796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_bss
9635796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_abs
9645796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_file_data
9655796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_file_bss))
9665796c8dcSSimon Schubert return NULL;
9675796c8dcSSimon Schubert
9685796c8dcSSimon Schubert ALL_OBJFILES (objfile)
9695796c8dcSSimon Schubert {
970cf7f2e2dSJohn Marino struct symtab *result = NULL;
9715796c8dcSSimon Schubert
972cf7f2e2dSJohn Marino if (objfile->sf)
973cf7f2e2dSJohn Marino result = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
974cf7f2e2dSJohn Marino pc, section, 0);
975cf7f2e2dSJohn Marino if (result)
976cf7f2e2dSJohn Marino return result;
9775796c8dcSSimon Schubert }
9785796c8dcSSimon Schubert
9795796c8dcSSimon Schubert return NULL;
9805796c8dcSSimon Schubert }
9815796c8dcSSimon Schubert
9825796c8dcSSimon Schubert /* Debug symbols usually don't have section information. We need to dig that
9835796c8dcSSimon Schubert out of the minimal symbols and stash that in the debug symbol. */
9845796c8dcSSimon Schubert
985cf7f2e2dSJohn Marino void
fixup_section(struct general_symbol_info * ginfo,CORE_ADDR addr,struct objfile * objfile)9865796c8dcSSimon Schubert fixup_section (struct general_symbol_info *ginfo,
9875796c8dcSSimon Schubert CORE_ADDR addr, struct objfile *objfile)
9885796c8dcSSimon Schubert {
9895796c8dcSSimon Schubert struct minimal_symbol *msym;
9905796c8dcSSimon Schubert
9915796c8dcSSimon Schubert /* First, check whether a minimal symbol with the same name exists
9925796c8dcSSimon Schubert and points to the same address. The address check is required
9935796c8dcSSimon Schubert e.g. on PowerPC64, where the minimal symbol for a function will
9945796c8dcSSimon Schubert point to the function descriptor, while the debug symbol will
9955796c8dcSSimon Schubert point to the actual function code. */
9965796c8dcSSimon Schubert msym = lookup_minimal_symbol_by_pc_name (addr, ginfo->name, objfile);
9975796c8dcSSimon Schubert if (msym)
9985796c8dcSSimon Schubert {
9995796c8dcSSimon Schubert ginfo->obj_section = SYMBOL_OBJ_SECTION (msym);
10005796c8dcSSimon Schubert ginfo->section = SYMBOL_SECTION (msym);
10015796c8dcSSimon Schubert }
10025796c8dcSSimon Schubert else
10035796c8dcSSimon Schubert {
10045796c8dcSSimon Schubert /* Static, function-local variables do appear in the linker
10055796c8dcSSimon Schubert (minimal) symbols, but are frequently given names that won't
10065796c8dcSSimon Schubert be found via lookup_minimal_symbol(). E.g., it has been
10075796c8dcSSimon Schubert observed in frv-uclinux (ELF) executables that a static,
10085796c8dcSSimon Schubert function-local variable named "foo" might appear in the
10095796c8dcSSimon Schubert linker symbols as "foo.6" or "foo.3". Thus, there is no
10105796c8dcSSimon Schubert point in attempting to extend the lookup-by-name mechanism to
10115796c8dcSSimon Schubert handle this case due to the fact that there can be multiple
10125796c8dcSSimon Schubert names.
10135796c8dcSSimon Schubert
10145796c8dcSSimon Schubert So, instead, search the section table when lookup by name has
10155796c8dcSSimon Schubert failed. The ``addr'' and ``endaddr'' fields may have already
10165796c8dcSSimon Schubert been relocated. If so, the relocation offset (i.e. the
10175796c8dcSSimon Schubert ANOFFSET value) needs to be subtracted from these values when
10185796c8dcSSimon Schubert performing the comparison. We unconditionally subtract it,
10195796c8dcSSimon Schubert because, when no relocation has been performed, the ANOFFSET
10205796c8dcSSimon Schubert value will simply be zero.
10215796c8dcSSimon Schubert
10225796c8dcSSimon Schubert The address of the symbol whose section we're fixing up HAS
10235796c8dcSSimon Schubert NOT BEEN adjusted (relocated) yet. It can't have been since
10245796c8dcSSimon Schubert the section isn't yet known and knowing the section is
10255796c8dcSSimon Schubert necessary in order to add the correct relocation value. In
10265796c8dcSSimon Schubert other words, we wouldn't even be in this function (attempting
10275796c8dcSSimon Schubert to compute the section) if it were already known.
10285796c8dcSSimon Schubert
10295796c8dcSSimon Schubert Note that it is possible to search the minimal symbols
10305796c8dcSSimon Schubert (subtracting the relocation value if necessary) to find the
10315796c8dcSSimon Schubert matching minimal symbol, but this is overkill and much less
10325796c8dcSSimon Schubert efficient. It is not necessary to find the matching minimal
10335796c8dcSSimon Schubert symbol, only its section.
10345796c8dcSSimon Schubert
10355796c8dcSSimon Schubert Note that this technique (of doing a section table search)
10365796c8dcSSimon Schubert can fail when unrelocated section addresses overlap. For
10375796c8dcSSimon Schubert this reason, we still attempt a lookup by name prior to doing
10385796c8dcSSimon Schubert a search of the section table. */
10395796c8dcSSimon Schubert
10405796c8dcSSimon Schubert struct obj_section *s;
1041cf7f2e2dSJohn Marino
10425796c8dcSSimon Schubert ALL_OBJFILE_OSECTIONS (objfile, s)
10435796c8dcSSimon Schubert {
10445796c8dcSSimon Schubert int idx = s->the_bfd_section->index;
10455796c8dcSSimon Schubert CORE_ADDR offset = ANOFFSET (objfile->section_offsets, idx);
10465796c8dcSSimon Schubert
10475796c8dcSSimon Schubert if (obj_section_addr (s) - offset <= addr
10485796c8dcSSimon Schubert && addr < obj_section_endaddr (s) - offset)
10495796c8dcSSimon Schubert {
10505796c8dcSSimon Schubert ginfo->obj_section = s;
10515796c8dcSSimon Schubert ginfo->section = idx;
10525796c8dcSSimon Schubert return;
10535796c8dcSSimon Schubert }
10545796c8dcSSimon Schubert }
10555796c8dcSSimon Schubert }
10565796c8dcSSimon Schubert }
10575796c8dcSSimon Schubert
10585796c8dcSSimon Schubert struct symbol *
fixup_symbol_section(struct symbol * sym,struct objfile * objfile)10595796c8dcSSimon Schubert fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
10605796c8dcSSimon Schubert {
10615796c8dcSSimon Schubert CORE_ADDR addr;
10625796c8dcSSimon Schubert
10635796c8dcSSimon Schubert if (!sym)
10645796c8dcSSimon Schubert return NULL;
10655796c8dcSSimon Schubert
10665796c8dcSSimon Schubert if (SYMBOL_OBJ_SECTION (sym))
10675796c8dcSSimon Schubert return sym;
10685796c8dcSSimon Schubert
10695796c8dcSSimon Schubert /* We either have an OBJFILE, or we can get at it from the sym's
10705796c8dcSSimon Schubert symtab. Anything else is a bug. */
10715796c8dcSSimon Schubert gdb_assert (objfile || SYMBOL_SYMTAB (sym));
10725796c8dcSSimon Schubert
10735796c8dcSSimon Schubert if (objfile == NULL)
10745796c8dcSSimon Schubert objfile = SYMBOL_SYMTAB (sym)->objfile;
10755796c8dcSSimon Schubert
10765796c8dcSSimon Schubert /* We should have an objfile by now. */
10775796c8dcSSimon Schubert gdb_assert (objfile);
10785796c8dcSSimon Schubert
10795796c8dcSSimon Schubert switch (SYMBOL_CLASS (sym))
10805796c8dcSSimon Schubert {
10815796c8dcSSimon Schubert case LOC_STATIC:
10825796c8dcSSimon Schubert case LOC_LABEL:
10835796c8dcSSimon Schubert addr = SYMBOL_VALUE_ADDRESS (sym);
10845796c8dcSSimon Schubert break;
10855796c8dcSSimon Schubert case LOC_BLOCK:
10865796c8dcSSimon Schubert addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
10875796c8dcSSimon Schubert break;
10885796c8dcSSimon Schubert
10895796c8dcSSimon Schubert default:
10905796c8dcSSimon Schubert /* Nothing else will be listed in the minsyms -- no use looking
10915796c8dcSSimon Schubert it up. */
10925796c8dcSSimon Schubert return sym;
10935796c8dcSSimon Schubert }
10945796c8dcSSimon Schubert
10955796c8dcSSimon Schubert fixup_section (&sym->ginfo, addr, objfile);
10965796c8dcSSimon Schubert
10975796c8dcSSimon Schubert return sym;
10985796c8dcSSimon Schubert }
10995796c8dcSSimon Schubert
1100a45ae5f8SJohn Marino /* Compute the demangled form of NAME as used by the various symbol
1101a45ae5f8SJohn Marino lookup functions. The result is stored in *RESULT_NAME. Returns a
1102a45ae5f8SJohn Marino cleanup which can be used to clean up the result.
11035796c8dcSSimon Schubert
1104a45ae5f8SJohn Marino For Ada, this function just sets *RESULT_NAME to NAME, unmodified.
1105a45ae5f8SJohn Marino Normally, Ada symbol lookups are performed using the encoded name
1106a45ae5f8SJohn Marino rather than the demangled name, and so it might seem to make sense
1107a45ae5f8SJohn Marino for this function to return an encoded version of NAME.
1108a45ae5f8SJohn Marino Unfortunately, we cannot do this, because this function is used in
1109a45ae5f8SJohn Marino circumstances where it is not appropriate to try to encode NAME.
1110a45ae5f8SJohn Marino For instance, when displaying the frame info, we demangle the name
1111a45ae5f8SJohn Marino of each parameter, and then perform a symbol lookup inside our
1112a45ae5f8SJohn Marino function using that demangled name. In Ada, certain functions
1113a45ae5f8SJohn Marino have internally-generated parameters whose name contain uppercase
1114a45ae5f8SJohn Marino characters. Encoding those name would result in those uppercase
1115a45ae5f8SJohn Marino characters to become lowercase, and thus cause the symbol lookup
1116a45ae5f8SJohn Marino to fail. */
11175796c8dcSSimon Schubert
1118a45ae5f8SJohn Marino struct cleanup *
demangle_for_lookup(const char * name,enum language lang,const char ** result_name)1119a45ae5f8SJohn Marino demangle_for_lookup (const char *name, enum language lang,
1120a45ae5f8SJohn Marino const char **result_name)
11215796c8dcSSimon Schubert {
11225796c8dcSSimon Schubert char *demangled_name = NULL;
11235796c8dcSSimon Schubert const char *modified_name = NULL;
11245796c8dcSSimon Schubert struct cleanup *cleanup = make_cleanup (null_cleanup, 0);
11255796c8dcSSimon Schubert
11265796c8dcSSimon Schubert modified_name = name;
11275796c8dcSSimon Schubert
1128*ef5ccd6cSJohn Marino /* If we are using C++, D, Go, or Java, demangle the name before doing a
1129cf7f2e2dSJohn Marino lookup, so we can always binary search. */
11305796c8dcSSimon Schubert if (lang == language_cplus)
11315796c8dcSSimon Schubert {
11325796c8dcSSimon Schubert demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
11335796c8dcSSimon Schubert if (demangled_name)
11345796c8dcSSimon Schubert {
11355796c8dcSSimon Schubert modified_name = demangled_name;
11365796c8dcSSimon Schubert make_cleanup (xfree, demangled_name);
11375796c8dcSSimon Schubert }
11385796c8dcSSimon Schubert else
11395796c8dcSSimon Schubert {
11405796c8dcSSimon Schubert /* If we were given a non-mangled name, canonicalize it
11415796c8dcSSimon Schubert according to the language (so far only for C++). */
11425796c8dcSSimon Schubert demangled_name = cp_canonicalize_string (name);
11435796c8dcSSimon Schubert if (demangled_name)
11445796c8dcSSimon Schubert {
11455796c8dcSSimon Schubert modified_name = demangled_name;
11465796c8dcSSimon Schubert make_cleanup (xfree, demangled_name);
11475796c8dcSSimon Schubert }
11485796c8dcSSimon Schubert }
11495796c8dcSSimon Schubert }
11505796c8dcSSimon Schubert else if (lang == language_java)
11515796c8dcSSimon Schubert {
11525796c8dcSSimon Schubert demangled_name = cplus_demangle (name,
11535796c8dcSSimon Schubert DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
11545796c8dcSSimon Schubert if (demangled_name)
11555796c8dcSSimon Schubert {
1156cf7f2e2dSJohn Marino modified_name = demangled_name;
1157cf7f2e2dSJohn Marino make_cleanup (xfree, demangled_name);
1158cf7f2e2dSJohn Marino }
1159cf7f2e2dSJohn Marino }
1160cf7f2e2dSJohn Marino else if (lang == language_d)
1161cf7f2e2dSJohn Marino {
1162cf7f2e2dSJohn Marino demangled_name = d_demangle (name, 0);
1163cf7f2e2dSJohn Marino if (demangled_name)
1164cf7f2e2dSJohn Marino {
11655796c8dcSSimon Schubert modified_name = demangled_name;
11665796c8dcSSimon Schubert make_cleanup (xfree, demangled_name);
11675796c8dcSSimon Schubert }
11685796c8dcSSimon Schubert }
1169*ef5ccd6cSJohn Marino else if (lang == language_go)
1170*ef5ccd6cSJohn Marino {
1171*ef5ccd6cSJohn Marino demangled_name = go_demangle (name, 0);
1172*ef5ccd6cSJohn Marino if (demangled_name)
1173*ef5ccd6cSJohn Marino {
1174*ef5ccd6cSJohn Marino modified_name = demangled_name;
1175*ef5ccd6cSJohn Marino make_cleanup (xfree, demangled_name);
1176*ef5ccd6cSJohn Marino }
1177*ef5ccd6cSJohn Marino }
11785796c8dcSSimon Schubert
1179a45ae5f8SJohn Marino *result_name = modified_name;
1180a45ae5f8SJohn Marino return cleanup;
11815796c8dcSSimon Schubert }
11825796c8dcSSimon Schubert
1183a45ae5f8SJohn Marino /* Find the definition for a specified symbol name NAME
1184a45ae5f8SJohn Marino in domain DOMAIN, visible from lexical block BLOCK.
1185a45ae5f8SJohn Marino Returns the struct symbol pointer, or zero if no symbol is found.
1186a45ae5f8SJohn Marino C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
1187a45ae5f8SJohn Marino NAME is a field of the current implied argument `this'. If so set
1188a45ae5f8SJohn Marino *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
1189a45ae5f8SJohn Marino BLOCK_FOUND is set to the block in which NAME is found (in the case of
1190a45ae5f8SJohn Marino a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
1191a45ae5f8SJohn Marino
1192*ef5ccd6cSJohn Marino /* This function (or rather its subordinates) have a bunch of loops and
1193*ef5ccd6cSJohn Marino it would seem to be attractive to put in some QUIT's (though I'm not really
1194*ef5ccd6cSJohn Marino sure whether it can run long enough to be really important). But there
1195a45ae5f8SJohn Marino are a few calls for which it would appear to be bad news to quit
1196*ef5ccd6cSJohn Marino out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c. (Note
1197a45ae5f8SJohn Marino that there is C++ code below which can error(), but that probably
1198a45ae5f8SJohn Marino doesn't affect these calls since they are looking for a known
1199a45ae5f8SJohn Marino variable and thus can probably assume it will never hit the C++
1200a45ae5f8SJohn Marino code). */
1201a45ae5f8SJohn Marino
1202a45ae5f8SJohn Marino struct symbol *
lookup_symbol_in_language(const char * name,const struct block * block,const domain_enum domain,enum language lang,struct field_of_this_result * is_a_field_of_this)1203a45ae5f8SJohn Marino lookup_symbol_in_language (const char *name, const struct block *block,
1204a45ae5f8SJohn Marino const domain_enum domain, enum language lang,
1205*ef5ccd6cSJohn Marino struct field_of_this_result *is_a_field_of_this)
1206a45ae5f8SJohn Marino {
1207a45ae5f8SJohn Marino const char *modified_name;
1208a45ae5f8SJohn Marino struct symbol *returnval;
1209a45ae5f8SJohn Marino struct cleanup *cleanup = demangle_for_lookup (name, lang, &modified_name);
1210a45ae5f8SJohn Marino
1211cf7f2e2dSJohn Marino returnval = lookup_symbol_aux (modified_name, block, domain, lang,
1212cf7f2e2dSJohn Marino is_a_field_of_this);
12135796c8dcSSimon Schubert do_cleanups (cleanup);
12145796c8dcSSimon Schubert
12155796c8dcSSimon Schubert return returnval;
12165796c8dcSSimon Schubert }
12175796c8dcSSimon Schubert
12185796c8dcSSimon Schubert /* Behave like lookup_symbol_in_language, but performed with the
12195796c8dcSSimon Schubert current language. */
12205796c8dcSSimon Schubert
12215796c8dcSSimon Schubert struct symbol *
lookup_symbol(const char * name,const struct block * block,domain_enum domain,struct field_of_this_result * is_a_field_of_this)12225796c8dcSSimon Schubert lookup_symbol (const char *name, const struct block *block,
1223*ef5ccd6cSJohn Marino domain_enum domain,
1224*ef5ccd6cSJohn Marino struct field_of_this_result *is_a_field_of_this)
12255796c8dcSSimon Schubert {
12265796c8dcSSimon Schubert return lookup_symbol_in_language (name, block, domain,
12275796c8dcSSimon Schubert current_language->la_language,
12285796c8dcSSimon Schubert is_a_field_of_this);
12295796c8dcSSimon Schubert }
12305796c8dcSSimon Schubert
1231a45ae5f8SJohn Marino /* Look up the `this' symbol for LANG in BLOCK. Return the symbol if
1232a45ae5f8SJohn Marino found, or NULL if not found. */
1233a45ae5f8SJohn Marino
1234a45ae5f8SJohn Marino struct symbol *
lookup_language_this(const struct language_defn * lang,const struct block * block)1235a45ae5f8SJohn Marino lookup_language_this (const struct language_defn *lang,
1236a45ae5f8SJohn Marino const struct block *block)
1237a45ae5f8SJohn Marino {
1238a45ae5f8SJohn Marino if (lang->la_name_of_this == NULL || block == NULL)
1239a45ae5f8SJohn Marino return NULL;
1240a45ae5f8SJohn Marino
1241a45ae5f8SJohn Marino while (block)
1242a45ae5f8SJohn Marino {
1243a45ae5f8SJohn Marino struct symbol *sym;
1244a45ae5f8SJohn Marino
1245a45ae5f8SJohn Marino sym = lookup_block_symbol (block, lang->la_name_of_this, VAR_DOMAIN);
1246a45ae5f8SJohn Marino if (sym != NULL)
1247*ef5ccd6cSJohn Marino {
1248*ef5ccd6cSJohn Marino block_found = block;
1249a45ae5f8SJohn Marino return sym;
1250*ef5ccd6cSJohn Marino }
1251a45ae5f8SJohn Marino if (BLOCK_FUNCTION (block))
1252a45ae5f8SJohn Marino break;
1253a45ae5f8SJohn Marino block = BLOCK_SUPERBLOCK (block);
1254a45ae5f8SJohn Marino }
1255a45ae5f8SJohn Marino
1256a45ae5f8SJohn Marino return NULL;
1257a45ae5f8SJohn Marino }
1258a45ae5f8SJohn Marino
1259*ef5ccd6cSJohn Marino /* Given TYPE, a structure/union,
1260*ef5ccd6cSJohn Marino return 1 if the component named NAME from the ultimate target
1261*ef5ccd6cSJohn Marino structure/union is defined, otherwise, return 0. */
1262*ef5ccd6cSJohn Marino
1263*ef5ccd6cSJohn Marino static int
check_field(struct type * type,const char * name,struct field_of_this_result * is_a_field_of_this)1264*ef5ccd6cSJohn Marino check_field (struct type *type, const char *name,
1265*ef5ccd6cSJohn Marino struct field_of_this_result *is_a_field_of_this)
1266*ef5ccd6cSJohn Marino {
1267*ef5ccd6cSJohn Marino int i;
1268*ef5ccd6cSJohn Marino
1269*ef5ccd6cSJohn Marino /* The type may be a stub. */
1270*ef5ccd6cSJohn Marino CHECK_TYPEDEF (type);
1271*ef5ccd6cSJohn Marino
1272*ef5ccd6cSJohn Marino for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1273*ef5ccd6cSJohn Marino {
1274*ef5ccd6cSJohn Marino const char *t_field_name = TYPE_FIELD_NAME (type, i);
1275*ef5ccd6cSJohn Marino
1276*ef5ccd6cSJohn Marino if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1277*ef5ccd6cSJohn Marino {
1278*ef5ccd6cSJohn Marino is_a_field_of_this->type = type;
1279*ef5ccd6cSJohn Marino is_a_field_of_this->field = &TYPE_FIELD (type, i);
1280*ef5ccd6cSJohn Marino return 1;
1281*ef5ccd6cSJohn Marino }
1282*ef5ccd6cSJohn Marino }
1283*ef5ccd6cSJohn Marino
1284*ef5ccd6cSJohn Marino /* C++: If it was not found as a data field, then try to return it
1285*ef5ccd6cSJohn Marino as a pointer to a method. */
1286*ef5ccd6cSJohn Marino
1287*ef5ccd6cSJohn Marino for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1288*ef5ccd6cSJohn Marino {
1289*ef5ccd6cSJohn Marino if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
1290*ef5ccd6cSJohn Marino {
1291*ef5ccd6cSJohn Marino is_a_field_of_this->type = type;
1292*ef5ccd6cSJohn Marino is_a_field_of_this->fn_field = &TYPE_FN_FIELDLIST (type, i);
1293*ef5ccd6cSJohn Marino return 1;
1294*ef5ccd6cSJohn Marino }
1295*ef5ccd6cSJohn Marino }
1296*ef5ccd6cSJohn Marino
1297*ef5ccd6cSJohn Marino for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1298*ef5ccd6cSJohn Marino if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
1299*ef5ccd6cSJohn Marino return 1;
1300*ef5ccd6cSJohn Marino
1301*ef5ccd6cSJohn Marino return 0;
1302*ef5ccd6cSJohn Marino }
1303*ef5ccd6cSJohn Marino
13045796c8dcSSimon Schubert /* Behave like lookup_symbol except that NAME is the natural name
1305*ef5ccd6cSJohn Marino (e.g., demangled name) of the symbol that we're looking for. */
13065796c8dcSSimon Schubert
13075796c8dcSSimon Schubert static struct symbol *
lookup_symbol_aux(const char * name,const struct block * block,const domain_enum domain,enum language language,struct field_of_this_result * is_a_field_of_this)1308cf7f2e2dSJohn Marino lookup_symbol_aux (const char *name, const struct block *block,
1309cf7f2e2dSJohn Marino const domain_enum domain, enum language language,
1310*ef5ccd6cSJohn Marino struct field_of_this_result *is_a_field_of_this)
13115796c8dcSSimon Schubert {
13125796c8dcSSimon Schubert struct symbol *sym;
13135796c8dcSSimon Schubert const struct language_defn *langdef;
13145796c8dcSSimon Schubert
13155796c8dcSSimon Schubert /* Make sure we do something sensible with is_a_field_of_this, since
13165796c8dcSSimon Schubert the callers that set this parameter to some non-null value will
1317*ef5ccd6cSJohn Marino certainly use it later. If we don't set it, the contents of
1318*ef5ccd6cSJohn Marino is_a_field_of_this are undefined. */
13195796c8dcSSimon Schubert if (is_a_field_of_this != NULL)
1320*ef5ccd6cSJohn Marino memset (is_a_field_of_this, 0, sizeof (*is_a_field_of_this));
13215796c8dcSSimon Schubert
13225796c8dcSSimon Schubert /* Search specified block and its superiors. Don't search
13235796c8dcSSimon Schubert STATIC_BLOCK or GLOBAL_BLOCK. */
13245796c8dcSSimon Schubert
1325cf7f2e2dSJohn Marino sym = lookup_symbol_aux_local (name, block, domain, language);
13265796c8dcSSimon Schubert if (sym != NULL)
13275796c8dcSSimon Schubert return sym;
13285796c8dcSSimon Schubert
13295796c8dcSSimon Schubert /* If requested to do so by the caller and if appropriate for LANGUAGE,
13305796c8dcSSimon Schubert check to see if NAME is a field of `this'. */
13315796c8dcSSimon Schubert
13325796c8dcSSimon Schubert langdef = language_def (language);
13335796c8dcSSimon Schubert
1334*ef5ccd6cSJohn Marino /* Don't do this check if we are searching for a struct. It will
1335*ef5ccd6cSJohn Marino not be found by check_field, but will be found by other
1336*ef5ccd6cSJohn Marino means. */
1337*ef5ccd6cSJohn Marino if (is_a_field_of_this != NULL && domain != STRUCT_DOMAIN)
13385796c8dcSSimon Schubert {
1339a45ae5f8SJohn Marino struct symbol *sym = lookup_language_this (langdef, block);
1340cf7f2e2dSJohn Marino
13415796c8dcSSimon Schubert if (sym)
13425796c8dcSSimon Schubert {
13435796c8dcSSimon Schubert struct type *t = sym->type;
13445796c8dcSSimon Schubert
13455796c8dcSSimon Schubert /* I'm not really sure that type of this can ever
13465796c8dcSSimon Schubert be typedefed; just be safe. */
13475796c8dcSSimon Schubert CHECK_TYPEDEF (t);
13485796c8dcSSimon Schubert if (TYPE_CODE (t) == TYPE_CODE_PTR
13495796c8dcSSimon Schubert || TYPE_CODE (t) == TYPE_CODE_REF)
13505796c8dcSSimon Schubert t = TYPE_TARGET_TYPE (t);
13515796c8dcSSimon Schubert
13525796c8dcSSimon Schubert if (TYPE_CODE (t) != TYPE_CODE_STRUCT
13535796c8dcSSimon Schubert && TYPE_CODE (t) != TYPE_CODE_UNION)
13545796c8dcSSimon Schubert error (_("Internal error: `%s' is not an aggregate"),
13555796c8dcSSimon Schubert langdef->la_name_of_this);
13565796c8dcSSimon Schubert
1357*ef5ccd6cSJohn Marino if (check_field (t, name, is_a_field_of_this))
13585796c8dcSSimon Schubert return NULL;
13595796c8dcSSimon Schubert }
13605796c8dcSSimon Schubert }
13615796c8dcSSimon Schubert
13625796c8dcSSimon Schubert /* Now do whatever is appropriate for LANGUAGE to look
13635796c8dcSSimon Schubert up static and global variables. */
13645796c8dcSSimon Schubert
1365cf7f2e2dSJohn Marino sym = langdef->la_lookup_symbol_nonlocal (name, block, domain);
13665796c8dcSSimon Schubert if (sym != NULL)
13675796c8dcSSimon Schubert return sym;
13685796c8dcSSimon Schubert
13695796c8dcSSimon Schubert /* Now search all static file-level symbols. Not strictly correct,
1370cf7f2e2dSJohn Marino but more useful than an error. */
13715796c8dcSSimon Schubert
1372cf7f2e2dSJohn Marino return lookup_static_symbol_aux (name, domain);
1373cf7f2e2dSJohn Marino }
1374cf7f2e2dSJohn Marino
1375cf7f2e2dSJohn Marino /* Search all static file-level symbols for NAME from DOMAIN. Do the symtabs
1376cf7f2e2dSJohn Marino first, then check the psymtabs. If a psymtab indicates the existence of the
1377cf7f2e2dSJohn Marino desired name as a file-level static, then do psymtab-to-symtab conversion on
1378cf7f2e2dSJohn Marino the fly and return the found symbol. */
1379cf7f2e2dSJohn Marino
1380cf7f2e2dSJohn Marino struct symbol *
lookup_static_symbol_aux(const char * name,const domain_enum domain)1381cf7f2e2dSJohn Marino lookup_static_symbol_aux (const char *name, const domain_enum domain)
1382cf7f2e2dSJohn Marino {
1383cf7f2e2dSJohn Marino struct objfile *objfile;
1384cf7f2e2dSJohn Marino struct symbol *sym;
1385cf7f2e2dSJohn Marino
1386cf7f2e2dSJohn Marino sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, domain);
13875796c8dcSSimon Schubert if (sym != NULL)
13885796c8dcSSimon Schubert return sym;
13895796c8dcSSimon Schubert
1390cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
1391cf7f2e2dSJohn Marino {
1392cf7f2e2dSJohn Marino sym = lookup_symbol_aux_quick (objfile, STATIC_BLOCK, name, domain);
13935796c8dcSSimon Schubert if (sym != NULL)
13945796c8dcSSimon Schubert return sym;
1395cf7f2e2dSJohn Marino }
13965796c8dcSSimon Schubert
13975796c8dcSSimon Schubert return NULL;
13985796c8dcSSimon Schubert }
13995796c8dcSSimon Schubert
14005796c8dcSSimon Schubert /* Check to see if the symbol is defined in BLOCK or its superiors.
14015796c8dcSSimon Schubert Don't search STATIC_BLOCK or GLOBAL_BLOCK. */
14025796c8dcSSimon Schubert
14035796c8dcSSimon Schubert static struct symbol *
lookup_symbol_aux_local(const char * name,const struct block * block,const domain_enum domain,enum language language)1404cf7f2e2dSJohn Marino lookup_symbol_aux_local (const char *name, const struct block *block,
1405cf7f2e2dSJohn Marino const domain_enum domain,
1406cf7f2e2dSJohn Marino enum language language)
14075796c8dcSSimon Schubert {
14085796c8dcSSimon Schubert struct symbol *sym;
14095796c8dcSSimon Schubert const struct block *static_block = block_static_block (block);
1410cf7f2e2dSJohn Marino const char *scope = block_scope (block);
14115796c8dcSSimon Schubert
14125796c8dcSSimon Schubert /* Check if either no block is specified or it's a global block. */
14135796c8dcSSimon Schubert
14145796c8dcSSimon Schubert if (static_block == NULL)
14155796c8dcSSimon Schubert return NULL;
14165796c8dcSSimon Schubert
14175796c8dcSSimon Schubert while (block != static_block)
14185796c8dcSSimon Schubert {
1419cf7f2e2dSJohn Marino sym = lookup_symbol_aux_block (name, block, domain);
14205796c8dcSSimon Schubert if (sym != NULL)
14215796c8dcSSimon Schubert return sym;
14225796c8dcSSimon Schubert
1423cf7f2e2dSJohn Marino if (language == language_cplus || language == language_fortran)
1424cf7f2e2dSJohn Marino {
1425c50c785cSJohn Marino sym = cp_lookup_symbol_imports_or_template (scope, name, block,
1426c50c785cSJohn Marino domain);
1427cf7f2e2dSJohn Marino if (sym != NULL)
1428cf7f2e2dSJohn Marino return sym;
1429cf7f2e2dSJohn Marino }
1430cf7f2e2dSJohn Marino
14315796c8dcSSimon Schubert if (BLOCK_FUNCTION (block) != NULL && block_inlined_p (block))
14325796c8dcSSimon Schubert break;
14335796c8dcSSimon Schubert block = BLOCK_SUPERBLOCK (block);
14345796c8dcSSimon Schubert }
14355796c8dcSSimon Schubert
14365796c8dcSSimon Schubert /* We've reached the edge of the function without finding a result. */
14375796c8dcSSimon Schubert
14385796c8dcSSimon Schubert return NULL;
14395796c8dcSSimon Schubert }
14405796c8dcSSimon Schubert
14415796c8dcSSimon Schubert /* Look up OBJFILE to BLOCK. */
14425796c8dcSSimon Schubert
1443cf7f2e2dSJohn Marino struct objfile *
lookup_objfile_from_block(const struct block * block)14445796c8dcSSimon Schubert lookup_objfile_from_block (const struct block *block)
14455796c8dcSSimon Schubert {
14465796c8dcSSimon Schubert struct objfile *obj;
14475796c8dcSSimon Schubert struct symtab *s;
14485796c8dcSSimon Schubert
14495796c8dcSSimon Schubert if (block == NULL)
14505796c8dcSSimon Schubert return NULL;
14515796c8dcSSimon Schubert
14525796c8dcSSimon Schubert block = block_global_block (block);
14535796c8dcSSimon Schubert /* Go through SYMTABS. */
14545796c8dcSSimon Schubert ALL_SYMTABS (obj, s)
14555796c8dcSSimon Schubert if (block == BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK))
1456cf7f2e2dSJohn Marino {
1457cf7f2e2dSJohn Marino if (obj->separate_debug_objfile_backlink)
1458cf7f2e2dSJohn Marino obj = obj->separate_debug_objfile_backlink;
1459cf7f2e2dSJohn Marino
14605796c8dcSSimon Schubert return obj;
1461cf7f2e2dSJohn Marino }
14625796c8dcSSimon Schubert
14635796c8dcSSimon Schubert return NULL;
14645796c8dcSSimon Schubert }
14655796c8dcSSimon Schubert
14665796c8dcSSimon Schubert /* Look up a symbol in a block; if found, fixup the symbol, and set
14675796c8dcSSimon Schubert block_found appropriately. */
14685796c8dcSSimon Schubert
14695796c8dcSSimon Schubert struct symbol *
lookup_symbol_aux_block(const char * name,const struct block * block,const domain_enum domain)1470cf7f2e2dSJohn Marino lookup_symbol_aux_block (const char *name, const struct block *block,
14715796c8dcSSimon Schubert const domain_enum domain)
14725796c8dcSSimon Schubert {
14735796c8dcSSimon Schubert struct symbol *sym;
14745796c8dcSSimon Schubert
1475cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, domain);
14765796c8dcSSimon Schubert if (sym)
14775796c8dcSSimon Schubert {
14785796c8dcSSimon Schubert block_found = block;
14795796c8dcSSimon Schubert return fixup_symbol_section (sym, NULL);
14805796c8dcSSimon Schubert }
14815796c8dcSSimon Schubert
14825796c8dcSSimon Schubert return NULL;
14835796c8dcSSimon Schubert }
14845796c8dcSSimon Schubert
14855796c8dcSSimon Schubert /* Check all global symbols in OBJFILE in symtabs and
14865796c8dcSSimon Schubert psymtabs. */
14875796c8dcSSimon Schubert
14885796c8dcSSimon Schubert struct symbol *
lookup_global_symbol_from_objfile(const struct objfile * main_objfile,const char * name,const domain_enum domain)1489cf7f2e2dSJohn Marino lookup_global_symbol_from_objfile (const struct objfile *main_objfile,
14905796c8dcSSimon Schubert const char *name,
14915796c8dcSSimon Schubert const domain_enum domain)
14925796c8dcSSimon Schubert {
1493cf7f2e2dSJohn Marino const struct objfile *objfile;
14945796c8dcSSimon Schubert struct symbol *sym;
14955796c8dcSSimon Schubert struct blockvector *bv;
14965796c8dcSSimon Schubert const struct block *block;
14975796c8dcSSimon Schubert struct symtab *s;
14985796c8dcSSimon Schubert
1499cf7f2e2dSJohn Marino for (objfile = main_objfile;
1500cf7f2e2dSJohn Marino objfile;
1501cf7f2e2dSJohn Marino objfile = objfile_separate_debug_iterate (main_objfile, objfile))
1502cf7f2e2dSJohn Marino {
15035796c8dcSSimon Schubert /* Go through symtabs. */
1504*ef5ccd6cSJohn Marino ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
15055796c8dcSSimon Schubert {
15065796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
15075796c8dcSSimon Schubert block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1508cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, domain);
15095796c8dcSSimon Schubert if (sym)
15105796c8dcSSimon Schubert {
15115796c8dcSSimon Schubert block_found = block;
15125796c8dcSSimon Schubert return fixup_symbol_section (sym, (struct objfile *)objfile);
15135796c8dcSSimon Schubert }
15145796c8dcSSimon Schubert }
15155796c8dcSSimon Schubert
1516cf7f2e2dSJohn Marino sym = lookup_symbol_aux_quick ((struct objfile *) objfile, GLOBAL_BLOCK,
1517cf7f2e2dSJohn Marino name, domain);
1518cf7f2e2dSJohn Marino if (sym)
1519cf7f2e2dSJohn Marino return sym;
15205796c8dcSSimon Schubert }
15215796c8dcSSimon Schubert
15225796c8dcSSimon Schubert return NULL;
15235796c8dcSSimon Schubert }
15245796c8dcSSimon Schubert
1525*ef5ccd6cSJohn Marino /* Check to see if the symbol is defined in one of the OBJFILE's
1526*ef5ccd6cSJohn Marino symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
15275796c8dcSSimon Schubert depending on whether or not we want to search global symbols or
15285796c8dcSSimon Schubert static symbols. */
15295796c8dcSSimon Schubert
15305796c8dcSSimon Schubert static struct symbol *
lookup_symbol_aux_objfile(struct objfile * objfile,int block_index,const char * name,const domain_enum domain)1531*ef5ccd6cSJohn Marino lookup_symbol_aux_objfile (struct objfile *objfile, int block_index,
1532*ef5ccd6cSJohn Marino const char *name, const domain_enum domain)
15335796c8dcSSimon Schubert {
1534*ef5ccd6cSJohn Marino struct symbol *sym = NULL;
15355796c8dcSSimon Schubert struct blockvector *bv;
15365796c8dcSSimon Schubert const struct block *block;
15375796c8dcSSimon Schubert struct symtab *s;
15385796c8dcSSimon Schubert
1539*ef5ccd6cSJohn Marino ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
15405796c8dcSSimon Schubert {
15415796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
15425796c8dcSSimon Schubert block = BLOCKVECTOR_BLOCK (bv, block_index);
1543cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, domain);
15445796c8dcSSimon Schubert if (sym)
15455796c8dcSSimon Schubert {
15465796c8dcSSimon Schubert block_found = block;
15475796c8dcSSimon Schubert return fixup_symbol_section (sym, objfile);
15485796c8dcSSimon Schubert }
15495796c8dcSSimon Schubert }
1550*ef5ccd6cSJohn Marino
1551*ef5ccd6cSJohn Marino return NULL;
1552c50c785cSJohn Marino }
15535796c8dcSSimon Schubert
1554*ef5ccd6cSJohn Marino /* Same as lookup_symbol_aux_objfile, except that it searches all
1555*ef5ccd6cSJohn Marino objfiles. Return the first match found. */
1556*ef5ccd6cSJohn Marino
1557*ef5ccd6cSJohn Marino static struct symbol *
lookup_symbol_aux_symtabs(int block_index,const char * name,const domain_enum domain)1558*ef5ccd6cSJohn Marino lookup_symbol_aux_symtabs (int block_index, const char *name,
1559*ef5ccd6cSJohn Marino const domain_enum domain)
1560*ef5ccd6cSJohn Marino {
1561*ef5ccd6cSJohn Marino struct symbol *sym;
1562*ef5ccd6cSJohn Marino struct objfile *objfile;
1563*ef5ccd6cSJohn Marino
1564*ef5ccd6cSJohn Marino ALL_OBJFILES (objfile)
1565*ef5ccd6cSJohn Marino {
1566*ef5ccd6cSJohn Marino sym = lookup_symbol_aux_objfile (objfile, block_index, name, domain);
1567*ef5ccd6cSJohn Marino if (sym)
1568*ef5ccd6cSJohn Marino return sym;
1569*ef5ccd6cSJohn Marino }
1570*ef5ccd6cSJohn Marino
1571*ef5ccd6cSJohn Marino return NULL;
1572*ef5ccd6cSJohn Marino }
1573*ef5ccd6cSJohn Marino
1574*ef5ccd6cSJohn Marino /* Wrapper around lookup_symbol_aux_objfile for search_symbols.
1575*ef5ccd6cSJohn Marino Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
1576*ef5ccd6cSJohn Marino and all related objfiles. */
1577*ef5ccd6cSJohn Marino
1578*ef5ccd6cSJohn Marino static struct symbol *
lookup_symbol_in_objfile_from_linkage_name(struct objfile * objfile,const char * linkage_name,domain_enum domain)1579*ef5ccd6cSJohn Marino lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
1580*ef5ccd6cSJohn Marino const char *linkage_name,
1581*ef5ccd6cSJohn Marino domain_enum domain)
1582*ef5ccd6cSJohn Marino {
1583*ef5ccd6cSJohn Marino enum language lang = current_language->la_language;
1584*ef5ccd6cSJohn Marino const char *modified_name;
1585*ef5ccd6cSJohn Marino struct cleanup *cleanup = demangle_for_lookup (linkage_name, lang,
1586*ef5ccd6cSJohn Marino &modified_name);
1587*ef5ccd6cSJohn Marino struct objfile *main_objfile, *cur_objfile;
1588*ef5ccd6cSJohn Marino
1589*ef5ccd6cSJohn Marino if (objfile->separate_debug_objfile_backlink)
1590*ef5ccd6cSJohn Marino main_objfile = objfile->separate_debug_objfile_backlink;
1591*ef5ccd6cSJohn Marino else
1592*ef5ccd6cSJohn Marino main_objfile = objfile;
1593*ef5ccd6cSJohn Marino
1594*ef5ccd6cSJohn Marino for (cur_objfile = main_objfile;
1595*ef5ccd6cSJohn Marino cur_objfile;
1596*ef5ccd6cSJohn Marino cur_objfile = objfile_separate_debug_iterate (main_objfile, cur_objfile))
1597*ef5ccd6cSJohn Marino {
1598*ef5ccd6cSJohn Marino struct symbol *sym;
1599*ef5ccd6cSJohn Marino
1600*ef5ccd6cSJohn Marino sym = lookup_symbol_aux_objfile (cur_objfile, GLOBAL_BLOCK,
1601*ef5ccd6cSJohn Marino modified_name, domain);
1602*ef5ccd6cSJohn Marino if (sym == NULL)
1603*ef5ccd6cSJohn Marino sym = lookup_symbol_aux_objfile (cur_objfile, STATIC_BLOCK,
1604*ef5ccd6cSJohn Marino modified_name, domain);
1605*ef5ccd6cSJohn Marino if (sym != NULL)
1606*ef5ccd6cSJohn Marino {
1607*ef5ccd6cSJohn Marino do_cleanups (cleanup);
1608*ef5ccd6cSJohn Marino return sym;
1609*ef5ccd6cSJohn Marino }
1610*ef5ccd6cSJohn Marino }
1611*ef5ccd6cSJohn Marino
1612*ef5ccd6cSJohn Marino do_cleanups (cleanup);
16135796c8dcSSimon Schubert return NULL;
16145796c8dcSSimon Schubert }
16155796c8dcSSimon Schubert
1616cf7f2e2dSJohn Marino /* A helper function for lookup_symbol_aux that interfaces with the
1617cf7f2e2dSJohn Marino "quick" symbol table functions. */
16185796c8dcSSimon Schubert
16195796c8dcSSimon Schubert static struct symbol *
lookup_symbol_aux_quick(struct objfile * objfile,int kind,const char * name,const domain_enum domain)1620cf7f2e2dSJohn Marino lookup_symbol_aux_quick (struct objfile *objfile, int kind,
1621cf7f2e2dSJohn Marino const char *name, const domain_enum domain)
16225796c8dcSSimon Schubert {
1623cf7f2e2dSJohn Marino struct symtab *symtab;
16245796c8dcSSimon Schubert struct blockvector *bv;
16255796c8dcSSimon Schubert const struct block *block;
1626cf7f2e2dSJohn Marino struct symbol *sym;
16275796c8dcSSimon Schubert
1628cf7f2e2dSJohn Marino if (!objfile->sf)
1629cf7f2e2dSJohn Marino return NULL;
1630cf7f2e2dSJohn Marino symtab = objfile->sf->qf->lookup_symbol (objfile, kind, name, domain);
1631cf7f2e2dSJohn Marino if (!symtab)
1632cf7f2e2dSJohn Marino return NULL;
1633cf7f2e2dSJohn Marino
1634cf7f2e2dSJohn Marino bv = BLOCKVECTOR (symtab);
1635cf7f2e2dSJohn Marino block = BLOCKVECTOR_BLOCK (bv, kind);
1636cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, domain);
16375796c8dcSSimon Schubert if (!sym)
16385796c8dcSSimon Schubert {
16395796c8dcSSimon Schubert /* This shouldn't be necessary, but as a last resort try
16405796c8dcSSimon Schubert looking in the statics even though the psymtab claimed
16415796c8dcSSimon Schubert the symbol was global, or vice-versa. It's possible
16425796c8dcSSimon Schubert that the psymtab gets it wrong in some cases. */
16435796c8dcSSimon Schubert
16445796c8dcSSimon Schubert /* FIXME: carlton/2002-09-30: Should we really do that?
16455796c8dcSSimon Schubert If that happens, isn't it likely to be a GDB error, in
16465796c8dcSSimon Schubert which case we should fix the GDB error rather than
16475796c8dcSSimon Schubert silently dealing with it here? So I'd vote for
16485796c8dcSSimon Schubert removing the check for the symbol in the other
16495796c8dcSSimon Schubert block. */
16505796c8dcSSimon Schubert block = BLOCKVECTOR_BLOCK (bv,
1651cf7f2e2dSJohn Marino kind == GLOBAL_BLOCK ?
16525796c8dcSSimon Schubert STATIC_BLOCK : GLOBAL_BLOCK);
1653cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, domain);
16545796c8dcSSimon Schubert if (!sym)
1655c50c785cSJohn Marino error (_("\
1656c50c785cSJohn Marino Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
1657c50c785cSJohn Marino %s may be an inlined function, or may be a template function\n\
1658c50c785cSJohn Marino (if a template, try specifying an instantiation: %s<type>)."),
1659cf7f2e2dSJohn Marino kind == GLOBAL_BLOCK ? "global" : "static",
1660*ef5ccd6cSJohn Marino name, symtab_to_filename_for_display (symtab), name, name);
16615796c8dcSSimon Schubert }
16625796c8dcSSimon Schubert return fixup_symbol_section (sym, objfile);
16635796c8dcSSimon Schubert }
16645796c8dcSSimon Schubert
16655796c8dcSSimon Schubert /* A default version of lookup_symbol_nonlocal for use by languages
16665796c8dcSSimon Schubert that can't think of anything better to do. This implements the C
16675796c8dcSSimon Schubert lookup rules. */
16685796c8dcSSimon Schubert
16695796c8dcSSimon Schubert struct symbol *
basic_lookup_symbol_nonlocal(const char * name,const struct block * block,const domain_enum domain)16705796c8dcSSimon Schubert basic_lookup_symbol_nonlocal (const char *name,
16715796c8dcSSimon Schubert const struct block *block,
16725796c8dcSSimon Schubert const domain_enum domain)
16735796c8dcSSimon Schubert {
16745796c8dcSSimon Schubert struct symbol *sym;
16755796c8dcSSimon Schubert
16765796c8dcSSimon Schubert /* NOTE: carlton/2003-05-19: The comments below were written when
16775796c8dcSSimon Schubert this (or what turned into this) was part of lookup_symbol_aux;
16785796c8dcSSimon Schubert I'm much less worried about these questions now, since these
16795796c8dcSSimon Schubert decisions have turned out well, but I leave these comments here
16805796c8dcSSimon Schubert for posterity. */
16815796c8dcSSimon Schubert
16825796c8dcSSimon Schubert /* NOTE: carlton/2002-12-05: There is a question as to whether or
16835796c8dcSSimon Schubert not it would be appropriate to search the current global block
16845796c8dcSSimon Schubert here as well. (That's what this code used to do before the
16855796c8dcSSimon Schubert is_a_field_of_this check was moved up.) On the one hand, it's
16865796c8dcSSimon Schubert redundant with the lookup_symbol_aux_symtabs search that happens
16875796c8dcSSimon Schubert next. On the other hand, if decode_line_1 is passed an argument
16885796c8dcSSimon Schubert like filename:var, then the user presumably wants 'var' to be
16895796c8dcSSimon Schubert searched for in filename. On the third hand, there shouldn't be
16905796c8dcSSimon Schubert multiple global variables all of which are named 'var', and it's
16915796c8dcSSimon Schubert not like decode_line_1 has ever restricted its search to only
16925796c8dcSSimon Schubert global variables in a single filename. All in all, only
16935796c8dcSSimon Schubert searching the static block here seems best: it's correct and it's
16945796c8dcSSimon Schubert cleanest. */
16955796c8dcSSimon Schubert
16965796c8dcSSimon Schubert /* NOTE: carlton/2002-12-05: There's also a possible performance
16975796c8dcSSimon Schubert issue here: if you usually search for global symbols in the
16985796c8dcSSimon Schubert current file, then it would be slightly better to search the
16995796c8dcSSimon Schubert current global block before searching all the symtabs. But there
17005796c8dcSSimon Schubert are other factors that have a much greater effect on performance
17015796c8dcSSimon Schubert than that one, so I don't think we should worry about that for
17025796c8dcSSimon Schubert now. */
17035796c8dcSSimon Schubert
1704cf7f2e2dSJohn Marino sym = lookup_symbol_static (name, block, domain);
17055796c8dcSSimon Schubert if (sym != NULL)
17065796c8dcSSimon Schubert return sym;
17075796c8dcSSimon Schubert
1708cf7f2e2dSJohn Marino return lookup_symbol_global (name, block, domain);
17095796c8dcSSimon Schubert }
17105796c8dcSSimon Schubert
17115796c8dcSSimon Schubert /* Lookup a symbol in the static block associated to BLOCK, if there
17125796c8dcSSimon Schubert is one; do nothing if BLOCK is NULL or a global block. */
17135796c8dcSSimon Schubert
17145796c8dcSSimon Schubert struct symbol *
lookup_symbol_static(const char * name,const struct block * block,const domain_enum domain)17155796c8dcSSimon Schubert lookup_symbol_static (const char *name,
17165796c8dcSSimon Schubert const struct block *block,
17175796c8dcSSimon Schubert const domain_enum domain)
17185796c8dcSSimon Schubert {
17195796c8dcSSimon Schubert const struct block *static_block = block_static_block (block);
17205796c8dcSSimon Schubert
17215796c8dcSSimon Schubert if (static_block != NULL)
1722cf7f2e2dSJohn Marino return lookup_symbol_aux_block (name, static_block, domain);
17235796c8dcSSimon Schubert else
17245796c8dcSSimon Schubert return NULL;
17255796c8dcSSimon Schubert }
17265796c8dcSSimon Schubert
1727*ef5ccd6cSJohn Marino /* Private data to be used with lookup_symbol_global_iterator_cb. */
1728*ef5ccd6cSJohn Marino
1729*ef5ccd6cSJohn Marino struct global_sym_lookup_data
1730*ef5ccd6cSJohn Marino {
1731*ef5ccd6cSJohn Marino /* The name of the symbol we are searching for. */
1732*ef5ccd6cSJohn Marino const char *name;
1733*ef5ccd6cSJohn Marino
1734*ef5ccd6cSJohn Marino /* The domain to use for our search. */
1735*ef5ccd6cSJohn Marino domain_enum domain;
1736*ef5ccd6cSJohn Marino
1737*ef5ccd6cSJohn Marino /* The field where the callback should store the symbol if found.
1738*ef5ccd6cSJohn Marino It should be initialized to NULL before the search is started. */
1739*ef5ccd6cSJohn Marino struct symbol *result;
1740*ef5ccd6cSJohn Marino };
1741*ef5ccd6cSJohn Marino
1742*ef5ccd6cSJohn Marino /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
1743*ef5ccd6cSJohn Marino It searches by name for a symbol in the GLOBAL_BLOCK of the given
1744*ef5ccd6cSJohn Marino OBJFILE. The arguments for the search are passed via CB_DATA,
1745*ef5ccd6cSJohn Marino which in reality is a pointer to struct global_sym_lookup_data. */
1746*ef5ccd6cSJohn Marino
1747*ef5ccd6cSJohn Marino static int
lookup_symbol_global_iterator_cb(struct objfile * objfile,void * cb_data)1748*ef5ccd6cSJohn Marino lookup_symbol_global_iterator_cb (struct objfile *objfile,
1749*ef5ccd6cSJohn Marino void *cb_data)
1750*ef5ccd6cSJohn Marino {
1751*ef5ccd6cSJohn Marino struct global_sym_lookup_data *data =
1752*ef5ccd6cSJohn Marino (struct global_sym_lookup_data *) cb_data;
1753*ef5ccd6cSJohn Marino
1754*ef5ccd6cSJohn Marino gdb_assert (data->result == NULL);
1755*ef5ccd6cSJohn Marino
1756*ef5ccd6cSJohn Marino data->result = lookup_symbol_aux_objfile (objfile, GLOBAL_BLOCK,
1757*ef5ccd6cSJohn Marino data->name, data->domain);
1758*ef5ccd6cSJohn Marino if (data->result == NULL)
1759*ef5ccd6cSJohn Marino data->result = lookup_symbol_aux_quick (objfile, GLOBAL_BLOCK,
1760*ef5ccd6cSJohn Marino data->name, data->domain);
1761*ef5ccd6cSJohn Marino
1762*ef5ccd6cSJohn Marino /* If we found a match, tell the iterator to stop. Otherwise,
1763*ef5ccd6cSJohn Marino keep going. */
1764*ef5ccd6cSJohn Marino return (data->result != NULL);
1765*ef5ccd6cSJohn Marino }
1766*ef5ccd6cSJohn Marino
17675796c8dcSSimon Schubert /* Lookup a symbol in all files' global blocks (searching psymtabs if
17685796c8dcSSimon Schubert necessary). */
17695796c8dcSSimon Schubert
17705796c8dcSSimon Schubert struct symbol *
lookup_symbol_global(const char * name,const struct block * block,const domain_enum domain)17715796c8dcSSimon Schubert lookup_symbol_global (const char *name,
17725796c8dcSSimon Schubert const struct block *block,
17735796c8dcSSimon Schubert const domain_enum domain)
17745796c8dcSSimon Schubert {
17755796c8dcSSimon Schubert struct symbol *sym = NULL;
17765796c8dcSSimon Schubert struct objfile *objfile = NULL;
1777*ef5ccd6cSJohn Marino struct global_sym_lookup_data lookup_data;
17785796c8dcSSimon Schubert
17795796c8dcSSimon Schubert /* Call library-specific lookup procedure. */
17805796c8dcSSimon Schubert objfile = lookup_objfile_from_block (block);
17815796c8dcSSimon Schubert if (objfile != NULL)
1782cf7f2e2dSJohn Marino sym = solib_global_lookup (objfile, name, domain);
17835796c8dcSSimon Schubert if (sym != NULL)
17845796c8dcSSimon Schubert return sym;
17855796c8dcSSimon Schubert
1786*ef5ccd6cSJohn Marino memset (&lookup_data, 0, sizeof (lookup_data));
1787*ef5ccd6cSJohn Marino lookup_data.name = name;
1788*ef5ccd6cSJohn Marino lookup_data.domain = domain;
1789*ef5ccd6cSJohn Marino gdbarch_iterate_over_objfiles_in_search_order
1790*ef5ccd6cSJohn Marino (objfile != NULL ? get_objfile_arch (objfile) : target_gdbarch (),
1791*ef5ccd6cSJohn Marino lookup_symbol_global_iterator_cb, &lookup_data, objfile);
17925796c8dcSSimon Schubert
1793*ef5ccd6cSJohn Marino return lookup_data.result;
17945796c8dcSSimon Schubert }
17955796c8dcSSimon Schubert
17965796c8dcSSimon Schubert int
symbol_matches_domain(enum language symbol_language,domain_enum symbol_domain,domain_enum domain)17975796c8dcSSimon Schubert symbol_matches_domain (enum language symbol_language,
17985796c8dcSSimon Schubert domain_enum symbol_domain,
17995796c8dcSSimon Schubert domain_enum domain)
18005796c8dcSSimon Schubert {
18015796c8dcSSimon Schubert /* For C++ "struct foo { ... }" also defines a typedef for "foo".
18025796c8dcSSimon Schubert A Java class declaration also defines a typedef for the class.
18035796c8dcSSimon Schubert Similarly, any Ada type declaration implicitly defines a typedef. */
18045796c8dcSSimon Schubert if (symbol_language == language_cplus
1805cf7f2e2dSJohn Marino || symbol_language == language_d
18065796c8dcSSimon Schubert || symbol_language == language_java
18075796c8dcSSimon Schubert || symbol_language == language_ada)
18085796c8dcSSimon Schubert {
18095796c8dcSSimon Schubert if ((domain == VAR_DOMAIN || domain == STRUCT_DOMAIN)
18105796c8dcSSimon Schubert && symbol_domain == STRUCT_DOMAIN)
18115796c8dcSSimon Schubert return 1;
18125796c8dcSSimon Schubert }
18135796c8dcSSimon Schubert /* For all other languages, strict match is required. */
18145796c8dcSSimon Schubert return (symbol_domain == domain);
18155796c8dcSSimon Schubert }
18165796c8dcSSimon Schubert
18175796c8dcSSimon Schubert /* Look up a type named NAME in the struct_domain. The type returned
18185796c8dcSSimon Schubert must not be opaque -- i.e., must have at least one field
18195796c8dcSSimon Schubert defined. */
18205796c8dcSSimon Schubert
18215796c8dcSSimon Schubert struct type *
lookup_transparent_type(const char * name)18225796c8dcSSimon Schubert lookup_transparent_type (const char *name)
18235796c8dcSSimon Schubert {
18245796c8dcSSimon Schubert return current_language->la_lookup_transparent_type (name);
18255796c8dcSSimon Schubert }
18265796c8dcSSimon Schubert
1827cf7f2e2dSJohn Marino /* A helper for basic_lookup_transparent_type that interfaces with the
1828cf7f2e2dSJohn Marino "quick" symbol table functions. */
1829cf7f2e2dSJohn Marino
1830cf7f2e2dSJohn Marino static struct type *
basic_lookup_transparent_type_quick(struct objfile * objfile,int kind,const char * name)1831cf7f2e2dSJohn Marino basic_lookup_transparent_type_quick (struct objfile *objfile, int kind,
1832cf7f2e2dSJohn Marino const char *name)
1833cf7f2e2dSJohn Marino {
1834cf7f2e2dSJohn Marino struct symtab *symtab;
1835cf7f2e2dSJohn Marino struct blockvector *bv;
1836cf7f2e2dSJohn Marino struct block *block;
1837cf7f2e2dSJohn Marino struct symbol *sym;
1838cf7f2e2dSJohn Marino
1839cf7f2e2dSJohn Marino if (!objfile->sf)
1840cf7f2e2dSJohn Marino return NULL;
1841cf7f2e2dSJohn Marino symtab = objfile->sf->qf->lookup_symbol (objfile, kind, name, STRUCT_DOMAIN);
1842cf7f2e2dSJohn Marino if (!symtab)
1843cf7f2e2dSJohn Marino return NULL;
1844cf7f2e2dSJohn Marino
1845cf7f2e2dSJohn Marino bv = BLOCKVECTOR (symtab);
1846cf7f2e2dSJohn Marino block = BLOCKVECTOR_BLOCK (bv, kind);
1847cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
1848cf7f2e2dSJohn Marino if (!sym)
1849cf7f2e2dSJohn Marino {
1850cf7f2e2dSJohn Marino int other_kind = kind == GLOBAL_BLOCK ? STATIC_BLOCK : GLOBAL_BLOCK;
1851cf7f2e2dSJohn Marino
1852cf7f2e2dSJohn Marino /* This shouldn't be necessary, but as a last resort
1853cf7f2e2dSJohn Marino * try looking in the 'other kind' even though the psymtab
1854cf7f2e2dSJohn Marino * claimed the symbol was one thing. It's possible that
1855cf7f2e2dSJohn Marino * the psymtab gets it wrong in some cases.
1856cf7f2e2dSJohn Marino */
1857cf7f2e2dSJohn Marino block = BLOCKVECTOR_BLOCK (bv, other_kind);
1858cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
1859cf7f2e2dSJohn Marino if (!sym)
1860c50c785cSJohn Marino /* FIXME; error is wrong in one case. */
1861c50c785cSJohn Marino error (_("\
1862c50c785cSJohn Marino Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1863cf7f2e2dSJohn Marino %s may be an inlined function, or may be a template function\n\
1864cf7f2e2dSJohn Marino (if a template, try specifying an instantiation: %s<type>)."),
1865*ef5ccd6cSJohn Marino name, symtab_to_filename_for_display (symtab), name, name);
1866cf7f2e2dSJohn Marino }
1867cf7f2e2dSJohn Marino if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1868cf7f2e2dSJohn Marino return SYMBOL_TYPE (sym);
1869cf7f2e2dSJohn Marino
1870cf7f2e2dSJohn Marino return NULL;
1871cf7f2e2dSJohn Marino }
1872cf7f2e2dSJohn Marino
18735796c8dcSSimon Schubert /* The standard implementation of lookup_transparent_type. This code
18745796c8dcSSimon Schubert was modeled on lookup_symbol -- the parts not relevant to looking
18755796c8dcSSimon Schubert up types were just left out. In particular it's assumed here that
18765796c8dcSSimon Schubert types are available in struct_domain and only at file-static or
18775796c8dcSSimon Schubert global blocks. */
18785796c8dcSSimon Schubert
18795796c8dcSSimon Schubert struct type *
basic_lookup_transparent_type(const char * name)18805796c8dcSSimon Schubert basic_lookup_transparent_type (const char *name)
18815796c8dcSSimon Schubert {
18825796c8dcSSimon Schubert struct symbol *sym;
18835796c8dcSSimon Schubert struct symtab *s = NULL;
18845796c8dcSSimon Schubert struct blockvector *bv;
18855796c8dcSSimon Schubert struct objfile *objfile;
18865796c8dcSSimon Schubert struct block *block;
1887cf7f2e2dSJohn Marino struct type *t;
18885796c8dcSSimon Schubert
18895796c8dcSSimon Schubert /* Now search all the global symbols. Do the symtab's first, then
18905796c8dcSSimon Schubert check the psymtab's. If a psymtab indicates the existence
18915796c8dcSSimon Schubert of the desired name as a global, then do psymtab-to-symtab
18925796c8dcSSimon Schubert conversion on the fly and return the found symbol. */
18935796c8dcSSimon Schubert
1894c50c785cSJohn Marino ALL_OBJFILES (objfile)
1895c50c785cSJohn Marino {
1896*ef5ccd6cSJohn Marino ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
18975796c8dcSSimon Schubert {
18985796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
18995796c8dcSSimon Schubert block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1900cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
19015796c8dcSSimon Schubert if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
19025796c8dcSSimon Schubert {
19035796c8dcSSimon Schubert return SYMBOL_TYPE (sym);
19045796c8dcSSimon Schubert }
19055796c8dcSSimon Schubert }
1906c50c785cSJohn Marino }
19075796c8dcSSimon Schubert
1908cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
19095796c8dcSSimon Schubert {
1910cf7f2e2dSJohn Marino t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
1911cf7f2e2dSJohn Marino if (t)
1912cf7f2e2dSJohn Marino return t;
19135796c8dcSSimon Schubert }
19145796c8dcSSimon Schubert
19155796c8dcSSimon Schubert /* Now search the static file-level symbols.
19165796c8dcSSimon Schubert Not strictly correct, but more useful than an error.
19175796c8dcSSimon Schubert Do the symtab's first, then
19185796c8dcSSimon Schubert check the psymtab's. If a psymtab indicates the existence
19195796c8dcSSimon Schubert of the desired name as a file-level static, then do psymtab-to-symtab
1920c50c785cSJohn Marino conversion on the fly and return the found symbol. */
19215796c8dcSSimon Schubert
1922c50c785cSJohn Marino ALL_OBJFILES (objfile)
1923c50c785cSJohn Marino {
1924*ef5ccd6cSJohn Marino ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
19255796c8dcSSimon Schubert {
19265796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
19275796c8dcSSimon Schubert block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1928cf7f2e2dSJohn Marino sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
19295796c8dcSSimon Schubert if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
19305796c8dcSSimon Schubert {
19315796c8dcSSimon Schubert return SYMBOL_TYPE (sym);
19325796c8dcSSimon Schubert }
19335796c8dcSSimon Schubert }
1934c50c785cSJohn Marino }
19355796c8dcSSimon Schubert
1936cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
19375796c8dcSSimon Schubert {
1938cf7f2e2dSJohn Marino t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
1939cf7f2e2dSJohn Marino if (t)
1940cf7f2e2dSJohn Marino return t;
19415796c8dcSSimon Schubert }
1942cf7f2e2dSJohn Marino
19435796c8dcSSimon Schubert return (struct type *) 0;
19445796c8dcSSimon Schubert }
19455796c8dcSSimon Schubert
1946cf7f2e2dSJohn Marino /* Find the name of the file containing main(). */
19475796c8dcSSimon Schubert /* FIXME: What about languages without main() or specially linked
19485796c8dcSSimon Schubert executables that have no main() ? */
19495796c8dcSSimon Schubert
1950c50c785cSJohn Marino const char *
find_main_filename(void)1951cf7f2e2dSJohn Marino find_main_filename (void)
19525796c8dcSSimon Schubert {
19535796c8dcSSimon Schubert struct objfile *objfile;
1954c50c785cSJohn Marino char *name = main_name ();
19555796c8dcSSimon Schubert
1956cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
19575796c8dcSSimon Schubert {
1958c50c785cSJohn Marino const char *result;
1959c50c785cSJohn Marino
1960cf7f2e2dSJohn Marino if (!objfile->sf)
1961cf7f2e2dSJohn Marino continue;
1962cf7f2e2dSJohn Marino result = objfile->sf->qf->find_symbol_file (objfile, name);
1963cf7f2e2dSJohn Marino if (result)
1964cf7f2e2dSJohn Marino return result;
19655796c8dcSSimon Schubert }
19665796c8dcSSimon Schubert return (NULL);
19675796c8dcSSimon Schubert }
19685796c8dcSSimon Schubert
19695796c8dcSSimon Schubert /* Search BLOCK for symbol NAME in DOMAIN.
19705796c8dcSSimon Schubert
19715796c8dcSSimon Schubert Note that if NAME is the demangled form of a C++ symbol, we will fail
19725796c8dcSSimon Schubert to find a match during the binary search of the non-encoded names, but
19735796c8dcSSimon Schubert for now we don't worry about the slight inefficiency of looking for
19745796c8dcSSimon Schubert a match we'll never find, since it will go pretty quick. Once the
19755796c8dcSSimon Schubert binary search terminates, we drop through and do a straight linear
19765796c8dcSSimon Schubert search on the symbols. Each symbol which is marked as being a ObjC/C++
19775796c8dcSSimon Schubert symbol (language_cplus or language_objc set) has both the encoded and
1978c50c785cSJohn Marino non-encoded names tested for a match. */
19795796c8dcSSimon Schubert
19805796c8dcSSimon Schubert struct symbol *
lookup_block_symbol(const struct block * block,const char * name,const domain_enum domain)19815796c8dcSSimon Schubert lookup_block_symbol (const struct block *block, const char *name,
19825796c8dcSSimon Schubert const domain_enum domain)
19835796c8dcSSimon Schubert {
1984*ef5ccd6cSJohn Marino struct block_iterator iter;
19855796c8dcSSimon Schubert struct symbol *sym;
19865796c8dcSSimon Schubert
19875796c8dcSSimon Schubert if (!BLOCK_FUNCTION (block))
19885796c8dcSSimon Schubert {
1989*ef5ccd6cSJohn Marino for (sym = block_iter_name_first (block, name, &iter);
19905796c8dcSSimon Schubert sym != NULL;
1991*ef5ccd6cSJohn Marino sym = block_iter_name_next (name, &iter))
19925796c8dcSSimon Schubert {
19935796c8dcSSimon Schubert if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1994cf7f2e2dSJohn Marino SYMBOL_DOMAIN (sym), domain))
19955796c8dcSSimon Schubert return sym;
19965796c8dcSSimon Schubert }
19975796c8dcSSimon Schubert return NULL;
19985796c8dcSSimon Schubert }
19995796c8dcSSimon Schubert else
20005796c8dcSSimon Schubert {
20015796c8dcSSimon Schubert /* Note that parameter symbols do not always show up last in the
20025796c8dcSSimon Schubert list; this loop makes sure to take anything else other than
20035796c8dcSSimon Schubert parameter symbols first; it only uses parameter symbols as a
20045796c8dcSSimon Schubert last resort. Note that this only takes up extra computation
20055796c8dcSSimon Schubert time on a match. */
20065796c8dcSSimon Schubert
20075796c8dcSSimon Schubert struct symbol *sym_found = NULL;
20085796c8dcSSimon Schubert
2009*ef5ccd6cSJohn Marino for (sym = block_iter_name_first (block, name, &iter);
20105796c8dcSSimon Schubert sym != NULL;
2011*ef5ccd6cSJohn Marino sym = block_iter_name_next (name, &iter))
20125796c8dcSSimon Schubert {
20135796c8dcSSimon Schubert if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
2014cf7f2e2dSJohn Marino SYMBOL_DOMAIN (sym), domain))
20155796c8dcSSimon Schubert {
20165796c8dcSSimon Schubert sym_found = sym;
20175796c8dcSSimon Schubert if (!SYMBOL_IS_ARGUMENT (sym))
20185796c8dcSSimon Schubert {
20195796c8dcSSimon Schubert break;
20205796c8dcSSimon Schubert }
20215796c8dcSSimon Schubert }
20225796c8dcSSimon Schubert }
20235796c8dcSSimon Schubert return (sym_found); /* Will be NULL if not found. */
20245796c8dcSSimon Schubert }
20255796c8dcSSimon Schubert }
20265796c8dcSSimon Schubert
2027*ef5ccd6cSJohn Marino /* Iterate over the symbols named NAME, matching DOMAIN, in BLOCK.
2028a45ae5f8SJohn Marino
2029a45ae5f8SJohn Marino For each symbol that matches, CALLBACK is called. The symbol and
2030a45ae5f8SJohn Marino DATA are passed to the callback.
2031a45ae5f8SJohn Marino
2032a45ae5f8SJohn Marino If CALLBACK returns zero, the iteration ends. Otherwise, the
2033*ef5ccd6cSJohn Marino search continues. */
2034a45ae5f8SJohn Marino
2035a45ae5f8SJohn Marino void
iterate_over_symbols(const struct block * block,const char * name,const domain_enum domain,symbol_found_callback_ftype * callback,void * data)2036a45ae5f8SJohn Marino iterate_over_symbols (const struct block *block, const char *name,
2037a45ae5f8SJohn Marino const domain_enum domain,
2038*ef5ccd6cSJohn Marino symbol_found_callback_ftype *callback,
2039a45ae5f8SJohn Marino void *data)
2040a45ae5f8SJohn Marino {
2041*ef5ccd6cSJohn Marino struct block_iterator iter;
2042a45ae5f8SJohn Marino struct symbol *sym;
2043a45ae5f8SJohn Marino
2044*ef5ccd6cSJohn Marino for (sym = block_iter_name_first (block, name, &iter);
2045a45ae5f8SJohn Marino sym != NULL;
2046*ef5ccd6cSJohn Marino sym = block_iter_name_next (name, &iter))
2047a45ae5f8SJohn Marino {
2048a45ae5f8SJohn Marino if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
2049a45ae5f8SJohn Marino SYMBOL_DOMAIN (sym), domain))
2050a45ae5f8SJohn Marino {
2051a45ae5f8SJohn Marino if (!callback (sym, data))
2052a45ae5f8SJohn Marino return;
2053a45ae5f8SJohn Marino }
2054a45ae5f8SJohn Marino }
2055a45ae5f8SJohn Marino }
2056a45ae5f8SJohn Marino
20575796c8dcSSimon Schubert /* Find the symtab associated with PC and SECTION. Look through the
20585796c8dcSSimon Schubert psymtabs and read in another symtab if necessary. */
20595796c8dcSSimon Schubert
20605796c8dcSSimon Schubert struct symtab *
find_pc_sect_symtab(CORE_ADDR pc,struct obj_section * section)20615796c8dcSSimon Schubert find_pc_sect_symtab (CORE_ADDR pc, struct obj_section *section)
20625796c8dcSSimon Schubert {
20635796c8dcSSimon Schubert struct block *b;
20645796c8dcSSimon Schubert struct blockvector *bv;
20655796c8dcSSimon Schubert struct symtab *s = NULL;
20665796c8dcSSimon Schubert struct symtab *best_s = NULL;
20675796c8dcSSimon Schubert struct objfile *objfile;
20685796c8dcSSimon Schubert CORE_ADDR distance = 0;
20695796c8dcSSimon Schubert struct minimal_symbol *msymbol;
20705796c8dcSSimon Schubert
20715796c8dcSSimon Schubert /* If we know that this is not a text address, return failure. This is
20725796c8dcSSimon Schubert necessary because we loop based on the block's high and low code
20735796c8dcSSimon Schubert addresses, which do not include the data ranges, and because
20745796c8dcSSimon Schubert we call find_pc_sect_psymtab which has a similar restriction based
20755796c8dcSSimon Schubert on the partial_symtab's texthigh and textlow. */
20765796c8dcSSimon Schubert msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
20775796c8dcSSimon Schubert if (msymbol
20785796c8dcSSimon Schubert && (MSYMBOL_TYPE (msymbol) == mst_data
20795796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_bss
20805796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_abs
20815796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_file_data
20825796c8dcSSimon Schubert || MSYMBOL_TYPE (msymbol) == mst_file_bss))
20835796c8dcSSimon Schubert return NULL;
20845796c8dcSSimon Schubert
20855796c8dcSSimon Schubert /* Search all symtabs for the one whose file contains our address, and which
20865796c8dcSSimon Schubert is the smallest of all the ones containing the address. This is designed
20875796c8dcSSimon Schubert to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
20885796c8dcSSimon Schubert and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
20895796c8dcSSimon Schubert 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
20905796c8dcSSimon Schubert
20915796c8dcSSimon Schubert This happens for native ecoff format, where code from included files
20925796c8dcSSimon Schubert gets its own symtab. The symtab for the included file should have
20935796c8dcSSimon Schubert been read in already via the dependency mechanism.
20945796c8dcSSimon Schubert It might be swifter to create several symtabs with the same name
20955796c8dcSSimon Schubert like xcoff does (I'm not sure).
20965796c8dcSSimon Schubert
20975796c8dcSSimon Schubert It also happens for objfiles that have their functions reordered.
20985796c8dcSSimon Schubert For these, the symtab we are looking for is not necessarily read in. */
20995796c8dcSSimon Schubert
21005796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s)
21015796c8dcSSimon Schubert {
21025796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
21035796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
21045796c8dcSSimon Schubert
21055796c8dcSSimon Schubert if (BLOCK_START (b) <= pc
21065796c8dcSSimon Schubert && BLOCK_END (b) > pc
21075796c8dcSSimon Schubert && (distance == 0
21085796c8dcSSimon Schubert || BLOCK_END (b) - BLOCK_START (b) < distance))
21095796c8dcSSimon Schubert {
21105796c8dcSSimon Schubert /* For an objfile that has its functions reordered,
21115796c8dcSSimon Schubert find_pc_psymtab will find the proper partial symbol table
21125796c8dcSSimon Schubert and we simply return its corresponding symtab. */
21135796c8dcSSimon Schubert /* In order to better support objfiles that contain both
21145796c8dcSSimon Schubert stabs and coff debugging info, we continue on if a psymtab
21155796c8dcSSimon Schubert can't be found. */
2116cf7f2e2dSJohn Marino if ((objfile->flags & OBJF_REORDERED) && objfile->sf)
21175796c8dcSSimon Schubert {
2118cf7f2e2dSJohn Marino struct symtab *result;
2119cf7f2e2dSJohn Marino
2120cf7f2e2dSJohn Marino result
2121cf7f2e2dSJohn Marino = objfile->sf->qf->find_pc_sect_symtab (objfile,
2122cf7f2e2dSJohn Marino msymbol,
2123cf7f2e2dSJohn Marino pc, section,
2124cf7f2e2dSJohn Marino 0);
2125cf7f2e2dSJohn Marino if (result)
2126cf7f2e2dSJohn Marino return result;
21275796c8dcSSimon Schubert }
21285796c8dcSSimon Schubert if (section != 0)
21295796c8dcSSimon Schubert {
2130*ef5ccd6cSJohn Marino struct block_iterator iter;
21315796c8dcSSimon Schubert struct symbol *sym = NULL;
21325796c8dcSSimon Schubert
21335796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (b, iter, sym)
21345796c8dcSSimon Schubert {
21355796c8dcSSimon Schubert fixup_symbol_section (sym, objfile);
21365796c8dcSSimon Schubert if (matching_obj_sections (SYMBOL_OBJ_SECTION (sym), section))
21375796c8dcSSimon Schubert break;
21385796c8dcSSimon Schubert }
21395796c8dcSSimon Schubert if (sym == NULL)
2140c50c785cSJohn Marino continue; /* No symbol in this symtab matches
2141c50c785cSJohn Marino section. */
21425796c8dcSSimon Schubert }
21435796c8dcSSimon Schubert distance = BLOCK_END (b) - BLOCK_START (b);
21445796c8dcSSimon Schubert best_s = s;
21455796c8dcSSimon Schubert }
21465796c8dcSSimon Schubert }
21475796c8dcSSimon Schubert
21485796c8dcSSimon Schubert if (best_s != NULL)
21495796c8dcSSimon Schubert return (best_s);
21505796c8dcSSimon Schubert
2151*ef5ccd6cSJohn Marino /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs). */
2152*ef5ccd6cSJohn Marino
2153cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
21545796c8dcSSimon Schubert {
2155cf7f2e2dSJohn Marino struct symtab *result;
2156cf7f2e2dSJohn Marino
2157cf7f2e2dSJohn Marino if (!objfile->sf)
2158cf7f2e2dSJohn Marino continue;
2159cf7f2e2dSJohn Marino result = objfile->sf->qf->find_pc_sect_symtab (objfile,
2160cf7f2e2dSJohn Marino msymbol,
2161cf7f2e2dSJohn Marino pc, section,
2162cf7f2e2dSJohn Marino 1);
2163cf7f2e2dSJohn Marino if (result)
2164cf7f2e2dSJohn Marino return result;
21655796c8dcSSimon Schubert }
2166cf7f2e2dSJohn Marino
2167cf7f2e2dSJohn Marino return NULL;
21685796c8dcSSimon Schubert }
21695796c8dcSSimon Schubert
2170c50c785cSJohn Marino /* Find the symtab associated with PC. Look through the psymtabs and read
2171c50c785cSJohn Marino in another symtab if necessary. Backward compatibility, no section. */
21725796c8dcSSimon Schubert
21735796c8dcSSimon Schubert struct symtab *
find_pc_symtab(CORE_ADDR pc)21745796c8dcSSimon Schubert find_pc_symtab (CORE_ADDR pc)
21755796c8dcSSimon Schubert {
21765796c8dcSSimon Schubert return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
21775796c8dcSSimon Schubert }
21785796c8dcSSimon Schubert
21795796c8dcSSimon Schubert
21805796c8dcSSimon Schubert /* Find the source file and line number for a given PC value and SECTION.
21815796c8dcSSimon Schubert Return a structure containing a symtab pointer, a line number,
21825796c8dcSSimon Schubert and a pc range for the entire source line.
21835796c8dcSSimon Schubert The value's .pc field is NOT the specified pc.
21845796c8dcSSimon Schubert NOTCURRENT nonzero means, if specified pc is on a line boundary,
21855796c8dcSSimon Schubert use the line that ends there. Otherwise, in that case, the line
21865796c8dcSSimon Schubert that begins there is used. */
21875796c8dcSSimon Schubert
21885796c8dcSSimon Schubert /* The big complication here is that a line may start in one file, and end just
21895796c8dcSSimon Schubert before the start of another file. This usually occurs when you #include
21905796c8dcSSimon Schubert code in the middle of a subroutine. To properly find the end of a line's PC
21915796c8dcSSimon Schubert range, we must search all symtabs associated with this compilation unit, and
21925796c8dcSSimon Schubert find the one whose first PC is closer than that of the next line in this
21935796c8dcSSimon Schubert symtab. */
21945796c8dcSSimon Schubert
21955796c8dcSSimon Schubert /* If it's worth the effort, we could be using a binary search. */
21965796c8dcSSimon Schubert
21975796c8dcSSimon Schubert struct symtab_and_line
find_pc_sect_line(CORE_ADDR pc,struct obj_section * section,int notcurrent)21985796c8dcSSimon Schubert find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
21995796c8dcSSimon Schubert {
22005796c8dcSSimon Schubert struct symtab *s;
22015796c8dcSSimon Schubert struct linetable *l;
22025796c8dcSSimon Schubert int len;
22035796c8dcSSimon Schubert int i;
22045796c8dcSSimon Schubert struct linetable_entry *item;
22055796c8dcSSimon Schubert struct symtab_and_line val;
22065796c8dcSSimon Schubert struct blockvector *bv;
22075796c8dcSSimon Schubert struct minimal_symbol *msymbol;
22085796c8dcSSimon Schubert struct minimal_symbol *mfunsym;
2209c50c785cSJohn Marino struct objfile *objfile;
22105796c8dcSSimon Schubert
22115796c8dcSSimon Schubert /* Info on best line seen so far, and where it starts, and its file. */
22125796c8dcSSimon Schubert
22135796c8dcSSimon Schubert struct linetable_entry *best = NULL;
22145796c8dcSSimon Schubert CORE_ADDR best_end = 0;
22155796c8dcSSimon Schubert struct symtab *best_symtab = 0;
22165796c8dcSSimon Schubert
22175796c8dcSSimon Schubert /* Store here the first line number
22185796c8dcSSimon Schubert of a file which contains the line at the smallest pc after PC.
22195796c8dcSSimon Schubert If we don't find a line whose range contains PC,
22205796c8dcSSimon Schubert we will use a line one less than this,
22215796c8dcSSimon Schubert with a range from the start of that file to the first line's pc. */
22225796c8dcSSimon Schubert struct linetable_entry *alt = NULL;
22235796c8dcSSimon Schubert
22245796c8dcSSimon Schubert /* Info on best line seen in this file. */
22255796c8dcSSimon Schubert
22265796c8dcSSimon Schubert struct linetable_entry *prev;
22275796c8dcSSimon Schubert
22285796c8dcSSimon Schubert /* If this pc is not from the current frame,
22295796c8dcSSimon Schubert it is the address of the end of a call instruction.
22305796c8dcSSimon Schubert Quite likely that is the start of the following statement.
22315796c8dcSSimon Schubert But what we want is the statement containing the instruction.
22325796c8dcSSimon Schubert Fudge the pc to make sure we get that. */
22335796c8dcSSimon Schubert
22345796c8dcSSimon Schubert init_sal (&val); /* initialize to zeroes */
22355796c8dcSSimon Schubert
2236cf7f2e2dSJohn Marino val.pspace = current_program_space;
2237cf7f2e2dSJohn Marino
22385796c8dcSSimon Schubert /* It's tempting to assume that, if we can't find debugging info for
22395796c8dcSSimon Schubert any function enclosing PC, that we shouldn't search for line
22405796c8dcSSimon Schubert number info, either. However, GAS can emit line number info for
22415796c8dcSSimon Schubert assembly files --- very helpful when debugging hand-written
22425796c8dcSSimon Schubert assembly code. In such a case, we'd have no debug info for the
22435796c8dcSSimon Schubert function, but we would have line info. */
22445796c8dcSSimon Schubert
22455796c8dcSSimon Schubert if (notcurrent)
22465796c8dcSSimon Schubert pc -= 1;
22475796c8dcSSimon Schubert
22485796c8dcSSimon Schubert /* elz: added this because this function returned the wrong
22495796c8dcSSimon Schubert information if the pc belongs to a stub (import/export)
22505796c8dcSSimon Schubert to call a shlib function. This stub would be anywhere between
22515796c8dcSSimon Schubert two functions in the target, and the line info was erroneously
2252c50c785cSJohn Marino taken to be the one of the line before the pc. */
2253c50c785cSJohn Marino
22545796c8dcSSimon Schubert /* RT: Further explanation:
22555796c8dcSSimon Schubert
22565796c8dcSSimon Schubert * We have stubs (trampolines) inserted between procedures.
22575796c8dcSSimon Schubert *
22585796c8dcSSimon Schubert * Example: "shr1" exists in a shared library, and a "shr1" stub also
22595796c8dcSSimon Schubert * exists in the main image.
22605796c8dcSSimon Schubert *
22615796c8dcSSimon Schubert * In the minimal symbol table, we have a bunch of symbols
22625796c8dcSSimon Schubert * sorted by start address. The stubs are marked as "trampoline",
22635796c8dcSSimon Schubert * the others appear as text. E.g.:
22645796c8dcSSimon Schubert *
22655796c8dcSSimon Schubert * Minimal symbol table for main image
22665796c8dcSSimon Schubert * main: code for main (text symbol)
22675796c8dcSSimon Schubert * shr1: stub (trampoline symbol)
22685796c8dcSSimon Schubert * foo: code for foo (text symbol)
22695796c8dcSSimon Schubert * ...
22705796c8dcSSimon Schubert * Minimal symbol table for "shr1" image:
22715796c8dcSSimon Schubert * ...
22725796c8dcSSimon Schubert * shr1: code for shr1 (text symbol)
22735796c8dcSSimon Schubert * ...
22745796c8dcSSimon Schubert *
22755796c8dcSSimon Schubert * So the code below is trying to detect if we are in the stub
22765796c8dcSSimon Schubert * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
22775796c8dcSSimon Schubert * and if found, do the symbolization from the real-code address
22785796c8dcSSimon Schubert * rather than the stub address.
22795796c8dcSSimon Schubert *
22805796c8dcSSimon Schubert * Assumptions being made about the minimal symbol table:
22815796c8dcSSimon Schubert * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
2282c50c785cSJohn Marino * if we're really in the trampoline.s If we're beyond it (say
22835796c8dcSSimon Schubert * we're in "foo" in the above example), it'll have a closer
22845796c8dcSSimon Schubert * symbol (the "foo" text symbol for example) and will not
22855796c8dcSSimon Schubert * return the trampoline.
22865796c8dcSSimon Schubert * 2. lookup_minimal_symbol_text() will find a real text symbol
22875796c8dcSSimon Schubert * corresponding to the trampoline, and whose address will
22885796c8dcSSimon Schubert * be different than the trampoline address. I put in a sanity
22895796c8dcSSimon Schubert * check for the address being the same, to avoid an
22905796c8dcSSimon Schubert * infinite recursion.
22915796c8dcSSimon Schubert */
22925796c8dcSSimon Schubert msymbol = lookup_minimal_symbol_by_pc (pc);
22935796c8dcSSimon Schubert if (msymbol != NULL)
22945796c8dcSSimon Schubert if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
22955796c8dcSSimon Schubert {
22965796c8dcSSimon Schubert mfunsym = lookup_minimal_symbol_text (SYMBOL_LINKAGE_NAME (msymbol),
22975796c8dcSSimon Schubert NULL);
22985796c8dcSSimon Schubert if (mfunsym == NULL)
22995796c8dcSSimon Schubert /* I eliminated this warning since it is coming out
23005796c8dcSSimon Schubert * in the following situation:
23015796c8dcSSimon Schubert * gdb shmain // test program with shared libraries
23025796c8dcSSimon Schubert * (gdb) break shr1 // function in shared lib
23035796c8dcSSimon Schubert * Warning: In stub for ...
23045796c8dcSSimon Schubert * In the above situation, the shared lib is not loaded yet,
23055796c8dcSSimon Schubert * so of course we can't find the real func/line info,
23065796c8dcSSimon Schubert * but the "break" still works, and the warning is annoying.
23075796c8dcSSimon Schubert * So I commented out the warning. RT */
2308c50c785cSJohn Marino /* warning ("In stub for %s; unable to find real function/line info",
2309c50c785cSJohn Marino SYMBOL_LINKAGE_NAME (msymbol)); */
2310c50c785cSJohn Marino ;
23115796c8dcSSimon Schubert /* fall through */
2312c50c785cSJohn Marino else if (SYMBOL_VALUE_ADDRESS (mfunsym)
2313c50c785cSJohn Marino == SYMBOL_VALUE_ADDRESS (msymbol))
23145796c8dcSSimon Schubert /* Avoid infinite recursion */
2315c50c785cSJohn Marino /* See above comment about why warning is commented out. */
2316c50c785cSJohn Marino /* warning ("In stub for %s; unable to find real function/line info",
2317c50c785cSJohn Marino SYMBOL_LINKAGE_NAME (msymbol)); */
2318c50c785cSJohn Marino ;
23195796c8dcSSimon Schubert /* fall through */
23205796c8dcSSimon Schubert else
23215796c8dcSSimon Schubert return find_pc_line (SYMBOL_VALUE_ADDRESS (mfunsym), 0);
23225796c8dcSSimon Schubert }
23235796c8dcSSimon Schubert
23245796c8dcSSimon Schubert
23255796c8dcSSimon Schubert s = find_pc_sect_symtab (pc, section);
23265796c8dcSSimon Schubert if (!s)
23275796c8dcSSimon Schubert {
2328c50c785cSJohn Marino /* If no symbol information, return previous pc. */
23295796c8dcSSimon Schubert if (notcurrent)
23305796c8dcSSimon Schubert pc++;
23315796c8dcSSimon Schubert val.pc = pc;
23325796c8dcSSimon Schubert return val;
23335796c8dcSSimon Schubert }
23345796c8dcSSimon Schubert
23355796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
2336c50c785cSJohn Marino objfile = s->objfile;
23375796c8dcSSimon Schubert
23385796c8dcSSimon Schubert /* Look at all the symtabs that share this blockvector.
23395796c8dcSSimon Schubert They all have the same apriori range, that we found was right;
23405796c8dcSSimon Schubert but they have different line tables. */
23415796c8dcSSimon Schubert
2342c50c785cSJohn Marino ALL_OBJFILE_SYMTABS (objfile, s)
23435796c8dcSSimon Schubert {
2344c50c785cSJohn Marino if (BLOCKVECTOR (s) != bv)
2345c50c785cSJohn Marino continue;
2346c50c785cSJohn Marino
23475796c8dcSSimon Schubert /* Find the best line in this symtab. */
23485796c8dcSSimon Schubert l = LINETABLE (s);
23495796c8dcSSimon Schubert if (!l)
23505796c8dcSSimon Schubert continue;
23515796c8dcSSimon Schubert len = l->nitems;
23525796c8dcSSimon Schubert if (len <= 0)
23535796c8dcSSimon Schubert {
23545796c8dcSSimon Schubert /* I think len can be zero if the symtab lacks line numbers
23555796c8dcSSimon Schubert (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
23565796c8dcSSimon Schubert I'm not sure which, and maybe it depends on the symbol
23575796c8dcSSimon Schubert reader). */
23585796c8dcSSimon Schubert continue;
23595796c8dcSSimon Schubert }
23605796c8dcSSimon Schubert
23615796c8dcSSimon Schubert prev = NULL;
2362c50c785cSJohn Marino item = l->item; /* Get first line info. */
23635796c8dcSSimon Schubert
23645796c8dcSSimon Schubert /* Is this file's first line closer than the first lines of other files?
23655796c8dcSSimon Schubert If so, record this file, and its first line, as best alternate. */
23665796c8dcSSimon Schubert if (item->pc > pc && (!alt || item->pc < alt->pc))
23675796c8dcSSimon Schubert alt = item;
23685796c8dcSSimon Schubert
23695796c8dcSSimon Schubert for (i = 0; i < len; i++, item++)
23705796c8dcSSimon Schubert {
23715796c8dcSSimon Schubert /* Leave prev pointing to the linetable entry for the last line
23725796c8dcSSimon Schubert that started at or before PC. */
23735796c8dcSSimon Schubert if (item->pc > pc)
23745796c8dcSSimon Schubert break;
23755796c8dcSSimon Schubert
23765796c8dcSSimon Schubert prev = item;
23775796c8dcSSimon Schubert }
23785796c8dcSSimon Schubert
23795796c8dcSSimon Schubert /* At this point, prev points at the line whose start addr is <= pc, and
23805796c8dcSSimon Schubert item points at the next line. If we ran off the end of the linetable
23815796c8dcSSimon Schubert (pc >= start of the last line), then prev == item. If pc < start of
23825796c8dcSSimon Schubert the first line, prev will not be set. */
23835796c8dcSSimon Schubert
23845796c8dcSSimon Schubert /* Is this file's best line closer than the best in the other files?
23855796c8dcSSimon Schubert If so, record this file, and its best line, as best so far. Don't
23865796c8dcSSimon Schubert save prev if it represents the end of a function (i.e. line number
23875796c8dcSSimon Schubert 0) instead of a real line. */
23885796c8dcSSimon Schubert
23895796c8dcSSimon Schubert if (prev && prev->line && (!best || prev->pc > best->pc))
23905796c8dcSSimon Schubert {
23915796c8dcSSimon Schubert best = prev;
23925796c8dcSSimon Schubert best_symtab = s;
23935796c8dcSSimon Schubert
23945796c8dcSSimon Schubert /* Discard BEST_END if it's before the PC of the current BEST. */
23955796c8dcSSimon Schubert if (best_end <= best->pc)
23965796c8dcSSimon Schubert best_end = 0;
23975796c8dcSSimon Schubert }
23985796c8dcSSimon Schubert
23995796c8dcSSimon Schubert /* If another line (denoted by ITEM) is in the linetable and its
24005796c8dcSSimon Schubert PC is after BEST's PC, but before the current BEST_END, then
24015796c8dcSSimon Schubert use ITEM's PC as the new best_end. */
24025796c8dcSSimon Schubert if (best && i < len && item->pc > best->pc
24035796c8dcSSimon Schubert && (best_end == 0 || best_end > item->pc))
24045796c8dcSSimon Schubert best_end = item->pc;
24055796c8dcSSimon Schubert }
24065796c8dcSSimon Schubert
24075796c8dcSSimon Schubert if (!best_symtab)
24085796c8dcSSimon Schubert {
24095796c8dcSSimon Schubert /* If we didn't find any line number info, just return zeros.
24105796c8dcSSimon Schubert We used to return alt->line - 1 here, but that could be
24115796c8dcSSimon Schubert anywhere; if we don't have line number info for this PC,
24125796c8dcSSimon Schubert don't make some up. */
24135796c8dcSSimon Schubert val.pc = pc;
24145796c8dcSSimon Schubert }
24155796c8dcSSimon Schubert else if (best->line == 0)
24165796c8dcSSimon Schubert {
24175796c8dcSSimon Schubert /* If our best fit is in a range of PC's for which no line
24185796c8dcSSimon Schubert number info is available (line number is zero) then we didn't
24195796c8dcSSimon Schubert find any valid line information. */
24205796c8dcSSimon Schubert val.pc = pc;
24215796c8dcSSimon Schubert }
24225796c8dcSSimon Schubert else
24235796c8dcSSimon Schubert {
24245796c8dcSSimon Schubert val.symtab = best_symtab;
24255796c8dcSSimon Schubert val.line = best->line;
24265796c8dcSSimon Schubert val.pc = best->pc;
24275796c8dcSSimon Schubert if (best_end && (!alt || best_end < alt->pc))
24285796c8dcSSimon Schubert val.end = best_end;
24295796c8dcSSimon Schubert else if (alt)
24305796c8dcSSimon Schubert val.end = alt->pc;
24315796c8dcSSimon Schubert else
24325796c8dcSSimon Schubert val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
24335796c8dcSSimon Schubert }
24345796c8dcSSimon Schubert val.section = section;
24355796c8dcSSimon Schubert return val;
24365796c8dcSSimon Schubert }
24375796c8dcSSimon Schubert
2438c50c785cSJohn Marino /* Backward compatibility (no section). */
24395796c8dcSSimon Schubert
24405796c8dcSSimon Schubert struct symtab_and_line
find_pc_line(CORE_ADDR pc,int notcurrent)24415796c8dcSSimon Schubert find_pc_line (CORE_ADDR pc, int notcurrent)
24425796c8dcSSimon Schubert {
24435796c8dcSSimon Schubert struct obj_section *section;
24445796c8dcSSimon Schubert
24455796c8dcSSimon Schubert section = find_pc_overlay (pc);
24465796c8dcSSimon Schubert if (pc_in_unmapped_range (pc, section))
24475796c8dcSSimon Schubert pc = overlay_mapped_address (pc, section);
24485796c8dcSSimon Schubert return find_pc_sect_line (pc, section, notcurrent);
24495796c8dcSSimon Schubert }
24505796c8dcSSimon Schubert
24515796c8dcSSimon Schubert /* Find line number LINE in any symtab whose name is the same as
24525796c8dcSSimon Schubert SYMTAB.
24535796c8dcSSimon Schubert
24545796c8dcSSimon Schubert If found, return the symtab that contains the linetable in which it was
24555796c8dcSSimon Schubert found, set *INDEX to the index in the linetable of the best entry
24565796c8dcSSimon Schubert found, and set *EXACT_MATCH nonzero if the value returned is an
24575796c8dcSSimon Schubert exact match.
24585796c8dcSSimon Schubert
24595796c8dcSSimon Schubert If not found, return NULL. */
24605796c8dcSSimon Schubert
24615796c8dcSSimon Schubert struct symtab *
find_line_symtab(struct symtab * symtab,int line,int * index,int * exact_match)2462cf7f2e2dSJohn Marino find_line_symtab (struct symtab *symtab, int line,
2463cf7f2e2dSJohn Marino int *index, int *exact_match)
24645796c8dcSSimon Schubert {
24655796c8dcSSimon Schubert int exact = 0; /* Initialized here to avoid a compiler warning. */
24665796c8dcSSimon Schubert
24675796c8dcSSimon Schubert /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
24685796c8dcSSimon Schubert so far seen. */
24695796c8dcSSimon Schubert
24705796c8dcSSimon Schubert int best_index;
24715796c8dcSSimon Schubert struct linetable *best_linetable;
24725796c8dcSSimon Schubert struct symtab *best_symtab;
24735796c8dcSSimon Schubert
24745796c8dcSSimon Schubert /* First try looking it up in the given symtab. */
24755796c8dcSSimon Schubert best_linetable = LINETABLE (symtab);
24765796c8dcSSimon Schubert best_symtab = symtab;
2477a45ae5f8SJohn Marino best_index = find_line_common (best_linetable, line, &exact, 0);
24785796c8dcSSimon Schubert if (best_index < 0 || !exact)
24795796c8dcSSimon Schubert {
24805796c8dcSSimon Schubert /* Didn't find an exact match. So we better keep looking for
24815796c8dcSSimon Schubert another symtab with the same name. In the case of xcoff,
24825796c8dcSSimon Schubert multiple csects for one source file (produced by IBM's FORTRAN
24835796c8dcSSimon Schubert compiler) produce multiple symtabs (this is unavoidable
24845796c8dcSSimon Schubert assuming csects can be at arbitrary places in memory and that
24855796c8dcSSimon Schubert the GLOBAL_BLOCK of a symtab has a begin and end address). */
24865796c8dcSSimon Schubert
24875796c8dcSSimon Schubert /* BEST is the smallest linenumber > LINE so far seen,
24885796c8dcSSimon Schubert or 0 if none has been seen so far.
24895796c8dcSSimon Schubert BEST_INDEX and BEST_LINETABLE identify the item for it. */
24905796c8dcSSimon Schubert int best;
24915796c8dcSSimon Schubert
24925796c8dcSSimon Schubert struct objfile *objfile;
24935796c8dcSSimon Schubert struct symtab *s;
24945796c8dcSSimon Schubert
24955796c8dcSSimon Schubert if (best_index >= 0)
24965796c8dcSSimon Schubert best = best_linetable->item[best_index].line;
24975796c8dcSSimon Schubert else
24985796c8dcSSimon Schubert best = 0;
24995796c8dcSSimon Schubert
2500cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
25015796c8dcSSimon Schubert {
2502cf7f2e2dSJohn Marino if (objfile->sf)
2503*ef5ccd6cSJohn Marino objfile->sf->qf->expand_symtabs_with_fullname (objfile,
2504*ef5ccd6cSJohn Marino symtab_to_fullname (symtab));
25055796c8dcSSimon Schubert }
25065796c8dcSSimon Schubert
25075796c8dcSSimon Schubert ALL_SYMTABS (objfile, s)
25085796c8dcSSimon Schubert {
25095796c8dcSSimon Schubert struct linetable *l;
25105796c8dcSSimon Schubert int ind;
25115796c8dcSSimon Schubert
2512cf7f2e2dSJohn Marino if (FILENAME_CMP (symtab->filename, s->filename) != 0)
2513cf7f2e2dSJohn Marino continue;
2514*ef5ccd6cSJohn Marino if (FILENAME_CMP (symtab_to_fullname (symtab),
2515*ef5ccd6cSJohn Marino symtab_to_fullname (s)) != 0)
25165796c8dcSSimon Schubert continue;
25175796c8dcSSimon Schubert l = LINETABLE (s);
2518a45ae5f8SJohn Marino ind = find_line_common (l, line, &exact, 0);
25195796c8dcSSimon Schubert if (ind >= 0)
25205796c8dcSSimon Schubert {
25215796c8dcSSimon Schubert if (exact)
25225796c8dcSSimon Schubert {
25235796c8dcSSimon Schubert best_index = ind;
25245796c8dcSSimon Schubert best_linetable = l;
25255796c8dcSSimon Schubert best_symtab = s;
25265796c8dcSSimon Schubert goto done;
25275796c8dcSSimon Schubert }
25285796c8dcSSimon Schubert if (best == 0 || l->item[ind].line < best)
25295796c8dcSSimon Schubert {
25305796c8dcSSimon Schubert best = l->item[ind].line;
25315796c8dcSSimon Schubert best_index = ind;
25325796c8dcSSimon Schubert best_linetable = l;
25335796c8dcSSimon Schubert best_symtab = s;
25345796c8dcSSimon Schubert }
25355796c8dcSSimon Schubert }
25365796c8dcSSimon Schubert }
25375796c8dcSSimon Schubert }
25385796c8dcSSimon Schubert done:
25395796c8dcSSimon Schubert if (best_index < 0)
25405796c8dcSSimon Schubert return NULL;
25415796c8dcSSimon Schubert
25425796c8dcSSimon Schubert if (index)
25435796c8dcSSimon Schubert *index = best_index;
25445796c8dcSSimon Schubert if (exact_match)
25455796c8dcSSimon Schubert *exact_match = exact;
25465796c8dcSSimon Schubert
25475796c8dcSSimon Schubert return best_symtab;
25485796c8dcSSimon Schubert }
2549a45ae5f8SJohn Marino
2550a45ae5f8SJohn Marino /* Given SYMTAB, returns all the PCs function in the symtab that
2551a45ae5f8SJohn Marino exactly match LINE. Returns NULL if there are no exact matches,
2552a45ae5f8SJohn Marino but updates BEST_ITEM in this case. */
2553a45ae5f8SJohn Marino
VEC(CORE_ADDR)2554a45ae5f8SJohn Marino VEC (CORE_ADDR) *
2555a45ae5f8SJohn Marino find_pcs_for_symtab_line (struct symtab *symtab, int line,
2556a45ae5f8SJohn Marino struct linetable_entry **best_item)
2557a45ae5f8SJohn Marino {
2558*ef5ccd6cSJohn Marino int start = 0;
2559a45ae5f8SJohn Marino VEC (CORE_ADDR) *result = NULL;
2560a45ae5f8SJohn Marino
2561a45ae5f8SJohn Marino /* First, collect all the PCs that are at this line. */
2562a45ae5f8SJohn Marino while (1)
2563a45ae5f8SJohn Marino {
2564a45ae5f8SJohn Marino int was_exact;
2565a45ae5f8SJohn Marino int idx;
2566a45ae5f8SJohn Marino
2567a45ae5f8SJohn Marino idx = find_line_common (LINETABLE (symtab), line, &was_exact, start);
2568a45ae5f8SJohn Marino if (idx < 0)
2569a45ae5f8SJohn Marino break;
2570a45ae5f8SJohn Marino
2571a45ae5f8SJohn Marino if (!was_exact)
2572a45ae5f8SJohn Marino {
2573a45ae5f8SJohn Marino struct linetable_entry *item = &LINETABLE (symtab)->item[idx];
2574a45ae5f8SJohn Marino
2575a45ae5f8SJohn Marino if (*best_item == NULL || item->line < (*best_item)->line)
2576a45ae5f8SJohn Marino *best_item = item;
2577a45ae5f8SJohn Marino
2578a45ae5f8SJohn Marino break;
2579a45ae5f8SJohn Marino }
2580a45ae5f8SJohn Marino
2581a45ae5f8SJohn Marino VEC_safe_push (CORE_ADDR, result, LINETABLE (symtab)->item[idx].pc);
2582a45ae5f8SJohn Marino start = idx + 1;
2583a45ae5f8SJohn Marino }
2584a45ae5f8SJohn Marino
2585a45ae5f8SJohn Marino return result;
2586a45ae5f8SJohn Marino }
2587a45ae5f8SJohn Marino
25885796c8dcSSimon Schubert
25895796c8dcSSimon Schubert /* Set the PC value for a given source file and line number and return true.
25905796c8dcSSimon Schubert Returns zero for invalid line number (and sets the PC to 0).
25915796c8dcSSimon Schubert The source file is specified with a struct symtab. */
25925796c8dcSSimon Schubert
25935796c8dcSSimon Schubert int
find_line_pc(struct symtab * symtab,int line,CORE_ADDR * pc)25945796c8dcSSimon Schubert find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
25955796c8dcSSimon Schubert {
25965796c8dcSSimon Schubert struct linetable *l;
25975796c8dcSSimon Schubert int ind;
25985796c8dcSSimon Schubert
25995796c8dcSSimon Schubert *pc = 0;
26005796c8dcSSimon Schubert if (symtab == 0)
26015796c8dcSSimon Schubert return 0;
26025796c8dcSSimon Schubert
26035796c8dcSSimon Schubert symtab = find_line_symtab (symtab, line, &ind, NULL);
26045796c8dcSSimon Schubert if (symtab != NULL)
26055796c8dcSSimon Schubert {
26065796c8dcSSimon Schubert l = LINETABLE (symtab);
26075796c8dcSSimon Schubert *pc = l->item[ind].pc;
26085796c8dcSSimon Schubert return 1;
26095796c8dcSSimon Schubert }
26105796c8dcSSimon Schubert else
26115796c8dcSSimon Schubert return 0;
26125796c8dcSSimon Schubert }
26135796c8dcSSimon Schubert
26145796c8dcSSimon Schubert /* Find the range of pc values in a line.
26155796c8dcSSimon Schubert Store the starting pc of the line into *STARTPTR
26165796c8dcSSimon Schubert and the ending pc (start of next line) into *ENDPTR.
26175796c8dcSSimon Schubert Returns 1 to indicate success.
26185796c8dcSSimon Schubert Returns 0 if could not find the specified line. */
26195796c8dcSSimon Schubert
26205796c8dcSSimon Schubert int
find_line_pc_range(struct symtab_and_line sal,CORE_ADDR * startptr,CORE_ADDR * endptr)26215796c8dcSSimon Schubert find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
26225796c8dcSSimon Schubert CORE_ADDR *endptr)
26235796c8dcSSimon Schubert {
26245796c8dcSSimon Schubert CORE_ADDR startaddr;
26255796c8dcSSimon Schubert struct symtab_and_line found_sal;
26265796c8dcSSimon Schubert
26275796c8dcSSimon Schubert startaddr = sal.pc;
26285796c8dcSSimon Schubert if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
26295796c8dcSSimon Schubert return 0;
26305796c8dcSSimon Schubert
26315796c8dcSSimon Schubert /* This whole function is based on address. For example, if line 10 has
26325796c8dcSSimon Schubert two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
26335796c8dcSSimon Schubert "info line *0x123" should say the line goes from 0x100 to 0x200
26345796c8dcSSimon Schubert and "info line *0x355" should say the line goes from 0x300 to 0x400.
26355796c8dcSSimon Schubert This also insures that we never give a range like "starts at 0x134
26365796c8dcSSimon Schubert and ends at 0x12c". */
26375796c8dcSSimon Schubert
26385796c8dcSSimon Schubert found_sal = find_pc_sect_line (startaddr, sal.section, 0);
26395796c8dcSSimon Schubert if (found_sal.line != sal.line)
26405796c8dcSSimon Schubert {
26415796c8dcSSimon Schubert /* The specified line (sal) has zero bytes. */
26425796c8dcSSimon Schubert *startptr = found_sal.pc;
26435796c8dcSSimon Schubert *endptr = found_sal.pc;
26445796c8dcSSimon Schubert }
26455796c8dcSSimon Schubert else
26465796c8dcSSimon Schubert {
26475796c8dcSSimon Schubert *startptr = found_sal.pc;
26485796c8dcSSimon Schubert *endptr = found_sal.end;
26495796c8dcSSimon Schubert }
26505796c8dcSSimon Schubert return 1;
26515796c8dcSSimon Schubert }
26525796c8dcSSimon Schubert
26535796c8dcSSimon Schubert /* Given a line table and a line number, return the index into the line
26545796c8dcSSimon Schubert table for the pc of the nearest line whose number is >= the specified one.
26555796c8dcSSimon Schubert Return -1 if none is found. The value is >= 0 if it is an index.
2656a45ae5f8SJohn Marino START is the index at which to start searching the line table.
26575796c8dcSSimon Schubert
26585796c8dcSSimon Schubert Set *EXACT_MATCH nonzero if the value returned is an exact match. */
26595796c8dcSSimon Schubert
26605796c8dcSSimon Schubert static int
find_line_common(struct linetable * l,int lineno,int * exact_match,int start)26615796c8dcSSimon Schubert find_line_common (struct linetable *l, int lineno,
2662a45ae5f8SJohn Marino int *exact_match, int start)
26635796c8dcSSimon Schubert {
26645796c8dcSSimon Schubert int i;
26655796c8dcSSimon Schubert int len;
26665796c8dcSSimon Schubert
26675796c8dcSSimon Schubert /* BEST is the smallest linenumber > LINENO so far seen,
26685796c8dcSSimon Schubert or 0 if none has been seen so far.
26695796c8dcSSimon Schubert BEST_INDEX identifies the item for it. */
26705796c8dcSSimon Schubert
26715796c8dcSSimon Schubert int best_index = -1;
26725796c8dcSSimon Schubert int best = 0;
26735796c8dcSSimon Schubert
26745796c8dcSSimon Schubert *exact_match = 0;
26755796c8dcSSimon Schubert
26765796c8dcSSimon Schubert if (lineno <= 0)
26775796c8dcSSimon Schubert return -1;
26785796c8dcSSimon Schubert if (l == 0)
26795796c8dcSSimon Schubert return -1;
26805796c8dcSSimon Schubert
26815796c8dcSSimon Schubert len = l->nitems;
2682a45ae5f8SJohn Marino for (i = start; i < len; i++)
26835796c8dcSSimon Schubert {
26845796c8dcSSimon Schubert struct linetable_entry *item = &(l->item[i]);
26855796c8dcSSimon Schubert
26865796c8dcSSimon Schubert if (item->line == lineno)
26875796c8dcSSimon Schubert {
26885796c8dcSSimon Schubert /* Return the first (lowest address) entry which matches. */
26895796c8dcSSimon Schubert *exact_match = 1;
26905796c8dcSSimon Schubert return i;
26915796c8dcSSimon Schubert }
26925796c8dcSSimon Schubert
26935796c8dcSSimon Schubert if (item->line > lineno && (best == 0 || item->line < best))
26945796c8dcSSimon Schubert {
26955796c8dcSSimon Schubert best = item->line;
26965796c8dcSSimon Schubert best_index = i;
26975796c8dcSSimon Schubert }
26985796c8dcSSimon Schubert }
26995796c8dcSSimon Schubert
27005796c8dcSSimon Schubert /* If we got here, we didn't get an exact match. */
27015796c8dcSSimon Schubert return best_index;
27025796c8dcSSimon Schubert }
27035796c8dcSSimon Schubert
27045796c8dcSSimon Schubert int
find_pc_line_pc_range(CORE_ADDR pc,CORE_ADDR * startptr,CORE_ADDR * endptr)27055796c8dcSSimon Schubert find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
27065796c8dcSSimon Schubert {
27075796c8dcSSimon Schubert struct symtab_and_line sal;
2708cf7f2e2dSJohn Marino
27095796c8dcSSimon Schubert sal = find_pc_line (pc, 0);
27105796c8dcSSimon Schubert *startptr = sal.pc;
27115796c8dcSSimon Schubert *endptr = sal.end;
27125796c8dcSSimon Schubert return sal.symtab != 0;
27135796c8dcSSimon Schubert }
27145796c8dcSSimon Schubert
27155796c8dcSSimon Schubert /* Given a function start address FUNC_ADDR and SYMTAB, find the first
27165796c8dcSSimon Schubert address for that function that has an entry in SYMTAB's line info
27175796c8dcSSimon Schubert table. If such an entry cannot be found, return FUNC_ADDR
27185796c8dcSSimon Schubert unaltered. */
2719*ef5ccd6cSJohn Marino
2720*ef5ccd6cSJohn Marino static CORE_ADDR
skip_prologue_using_lineinfo(CORE_ADDR func_addr,struct symtab * symtab)27215796c8dcSSimon Schubert skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
27225796c8dcSSimon Schubert {
27235796c8dcSSimon Schubert CORE_ADDR func_start, func_end;
27245796c8dcSSimon Schubert struct linetable *l;
2725cf7f2e2dSJohn Marino int i;
27265796c8dcSSimon Schubert
27275796c8dcSSimon Schubert /* Give up if this symbol has no lineinfo table. */
27285796c8dcSSimon Schubert l = LINETABLE (symtab);
27295796c8dcSSimon Schubert if (l == NULL)
27305796c8dcSSimon Schubert return func_addr;
27315796c8dcSSimon Schubert
27325796c8dcSSimon Schubert /* Get the range for the function's PC values, or give up if we
27335796c8dcSSimon Schubert cannot, for some reason. */
27345796c8dcSSimon Schubert if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
27355796c8dcSSimon Schubert return func_addr;
27365796c8dcSSimon Schubert
27375796c8dcSSimon Schubert /* Linetable entries are ordered by PC values, see the commentary in
27385796c8dcSSimon Schubert symtab.h where `struct linetable' is defined. Thus, the first
27395796c8dcSSimon Schubert entry whose PC is in the range [FUNC_START..FUNC_END[ is the
27405796c8dcSSimon Schubert address we are looking for. */
27415796c8dcSSimon Schubert for (i = 0; i < l->nitems; i++)
27425796c8dcSSimon Schubert {
27435796c8dcSSimon Schubert struct linetable_entry *item = &(l->item[i]);
27445796c8dcSSimon Schubert
27455796c8dcSSimon Schubert /* Don't use line numbers of zero, they mark special entries in
27465796c8dcSSimon Schubert the table. See the commentary on symtab.h before the
27475796c8dcSSimon Schubert definition of struct linetable. */
27485796c8dcSSimon Schubert if (item->line > 0 && func_start <= item->pc && item->pc < func_end)
27495796c8dcSSimon Schubert return item->pc;
27505796c8dcSSimon Schubert }
27515796c8dcSSimon Schubert
27525796c8dcSSimon Schubert return func_addr;
27535796c8dcSSimon Schubert }
27545796c8dcSSimon Schubert
27555796c8dcSSimon Schubert /* Given a function symbol SYM, find the symtab and line for the start
27565796c8dcSSimon Schubert of the function.
27575796c8dcSSimon Schubert If the argument FUNFIRSTLINE is nonzero, we want the first line
27585796c8dcSSimon Schubert of real code inside the function. */
27595796c8dcSSimon Schubert
27605796c8dcSSimon Schubert struct symtab_and_line
find_function_start_sal(struct symbol * sym,int funfirstline)27615796c8dcSSimon Schubert find_function_start_sal (struct symbol *sym, int funfirstline)
27625796c8dcSSimon Schubert {
27635796c8dcSSimon Schubert struct symtab_and_line sal;
2764cf7f2e2dSJohn Marino
2765cf7f2e2dSJohn Marino fixup_symbol_section (sym, NULL);
2766cf7f2e2dSJohn Marino sal = find_pc_sect_line (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)),
2767cf7f2e2dSJohn Marino SYMBOL_OBJ_SECTION (sym), 0);
2768cf7f2e2dSJohn Marino
2769cf7f2e2dSJohn Marino /* We always should have a line for the function start address.
2770cf7f2e2dSJohn Marino If we don't, something is odd. Create a plain SAL refering
2771cf7f2e2dSJohn Marino just the PC and hope that skip_prologue_sal (if requested)
2772cf7f2e2dSJohn Marino can find a line number for after the prologue. */
2773cf7f2e2dSJohn Marino if (sal.pc < BLOCK_START (SYMBOL_BLOCK_VALUE (sym)))
2774cf7f2e2dSJohn Marino {
2775cf7f2e2dSJohn Marino init_sal (&sal);
2776cf7f2e2dSJohn Marino sal.pspace = current_program_space;
2777cf7f2e2dSJohn Marino sal.pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2778cf7f2e2dSJohn Marino sal.section = SYMBOL_OBJ_SECTION (sym);
2779cf7f2e2dSJohn Marino }
2780cf7f2e2dSJohn Marino
2781cf7f2e2dSJohn Marino if (funfirstline)
2782cf7f2e2dSJohn Marino skip_prologue_sal (&sal);
2783cf7f2e2dSJohn Marino
2784cf7f2e2dSJohn Marino return sal;
2785cf7f2e2dSJohn Marino }
2786cf7f2e2dSJohn Marino
2787cf7f2e2dSJohn Marino /* Adjust SAL to the first instruction past the function prologue.
2788cf7f2e2dSJohn Marino If the PC was explicitly specified, the SAL is not changed.
2789cf7f2e2dSJohn Marino If the line number was explicitly specified, at most the SAL's PC
2790cf7f2e2dSJohn Marino is updated. If SAL is already past the prologue, then do nothing. */
2791*ef5ccd6cSJohn Marino
2792cf7f2e2dSJohn Marino void
skip_prologue_sal(struct symtab_and_line * sal)2793cf7f2e2dSJohn Marino skip_prologue_sal (struct symtab_and_line *sal)
2794cf7f2e2dSJohn Marino {
2795cf7f2e2dSJohn Marino struct symbol *sym;
2796cf7f2e2dSJohn Marino struct symtab_and_line start_sal;
2797cf7f2e2dSJohn Marino struct cleanup *old_chain;
2798c50c785cSJohn Marino CORE_ADDR pc, saved_pc;
2799cf7f2e2dSJohn Marino struct obj_section *section;
2800cf7f2e2dSJohn Marino const char *name;
2801cf7f2e2dSJohn Marino struct objfile *objfile;
2802cf7f2e2dSJohn Marino struct gdbarch *gdbarch;
28035796c8dcSSimon Schubert struct block *b, *function_block;
2804c50c785cSJohn Marino int force_skip, skip;
28055796c8dcSSimon Schubert
2806*ef5ccd6cSJohn Marino /* Do not change the SAL if PC was specified explicitly. */
2807cf7f2e2dSJohn Marino if (sal->explicit_pc)
2808cf7f2e2dSJohn Marino return;
2809cf7f2e2dSJohn Marino
2810cf7f2e2dSJohn Marino old_chain = save_current_space_and_thread ();
2811cf7f2e2dSJohn Marino switch_to_program_space_and_thread (sal->pspace);
2812cf7f2e2dSJohn Marino
2813cf7f2e2dSJohn Marino sym = find_pc_sect_function (sal->pc, sal->section);
2814cf7f2e2dSJohn Marino if (sym != NULL)
28155796c8dcSSimon Schubert {
2816cf7f2e2dSJohn Marino fixup_symbol_section (sym, NULL);
2817cf7f2e2dSJohn Marino
2818cf7f2e2dSJohn Marino pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2819cf7f2e2dSJohn Marino section = SYMBOL_OBJ_SECTION (sym);
2820cf7f2e2dSJohn Marino name = SYMBOL_LINKAGE_NAME (sym);
2821cf7f2e2dSJohn Marino objfile = SYMBOL_SYMTAB (sym)->objfile;
28225796c8dcSSimon Schubert }
2823cf7f2e2dSJohn Marino else
2824cf7f2e2dSJohn Marino {
2825cf7f2e2dSJohn Marino struct minimal_symbol *msymbol
2826cf7f2e2dSJohn Marino = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);
2827cf7f2e2dSJohn Marino
2828cf7f2e2dSJohn Marino if (msymbol == NULL)
2829cf7f2e2dSJohn Marino {
2830cf7f2e2dSJohn Marino do_cleanups (old_chain);
2831cf7f2e2dSJohn Marino return;
2832cf7f2e2dSJohn Marino }
2833cf7f2e2dSJohn Marino
2834cf7f2e2dSJohn Marino pc = SYMBOL_VALUE_ADDRESS (msymbol);
2835cf7f2e2dSJohn Marino section = SYMBOL_OBJ_SECTION (msymbol);
2836cf7f2e2dSJohn Marino name = SYMBOL_LINKAGE_NAME (msymbol);
2837cf7f2e2dSJohn Marino objfile = msymbol_objfile (msymbol);
2838cf7f2e2dSJohn Marino }
2839cf7f2e2dSJohn Marino
2840cf7f2e2dSJohn Marino gdbarch = get_objfile_arch (objfile);
2841cf7f2e2dSJohn Marino
2842c50c785cSJohn Marino /* Process the prologue in two passes. In the first pass try to skip the
2843c50c785cSJohn Marino prologue (SKIP is true) and verify there is a real need for it (indicated
2844c50c785cSJohn Marino by FORCE_SKIP). If no such reason was found run a second pass where the
2845c50c785cSJohn Marino prologue is not skipped (SKIP is false). */
2846c50c785cSJohn Marino
2847c50c785cSJohn Marino skip = 1;
2848c50c785cSJohn Marino force_skip = 1;
2849c50c785cSJohn Marino
2850c50c785cSJohn Marino /* Be conservative - allow direct PC (without skipping prologue) only if we
2851c50c785cSJohn Marino have proven the CU (Compilation Unit) supports it. sal->SYMTAB does not
2852c50c785cSJohn Marino have to be set by the caller so we use SYM instead. */
2853c50c785cSJohn Marino if (sym && SYMBOL_SYMTAB (sym)->locations_valid)
2854c50c785cSJohn Marino force_skip = 0;
2855c50c785cSJohn Marino
2856c50c785cSJohn Marino saved_pc = pc;
2857c50c785cSJohn Marino do
2858c50c785cSJohn Marino {
2859c50c785cSJohn Marino pc = saved_pc;
2860c50c785cSJohn Marino
2861cf7f2e2dSJohn Marino /* If the function is in an unmapped overlay, use its unmapped LMA address,
2862cf7f2e2dSJohn Marino so that gdbarch_skip_prologue has something unique to work on. */
2863cf7f2e2dSJohn Marino if (section_is_overlay (section) && !section_is_mapped (section))
2864cf7f2e2dSJohn Marino pc = overlay_unmapped_address (pc, section);
2865cf7f2e2dSJohn Marino
2866cf7f2e2dSJohn Marino /* Skip "first line" of function (which is actually its prologue). */
2867cf7f2e2dSJohn Marino pc += gdbarch_deprecated_function_start_offset (gdbarch);
2868c50c785cSJohn Marino if (skip)
2869cf7f2e2dSJohn Marino pc = gdbarch_skip_prologue (gdbarch, pc);
2870cf7f2e2dSJohn Marino
2871cf7f2e2dSJohn Marino /* For overlays, map pc back into its mapped VMA range. */
2872cf7f2e2dSJohn Marino pc = overlay_mapped_address (pc, section);
2873cf7f2e2dSJohn Marino
2874cf7f2e2dSJohn Marino /* Calculate line number. */
2875cf7f2e2dSJohn Marino start_sal = find_pc_sect_line (pc, section, 0);
28765796c8dcSSimon Schubert
28775796c8dcSSimon Schubert /* Check if gdbarch_skip_prologue left us in mid-line, and the next
28785796c8dcSSimon Schubert line is still part of the same function. */
2879c50c785cSJohn Marino if (skip && start_sal.pc != pc
2880cf7f2e2dSJohn Marino && (sym ? (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= start_sal.end
2881cf7f2e2dSJohn Marino && start_sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2882cf7f2e2dSJohn Marino : (lookup_minimal_symbol_by_pc_section (start_sal.end, section)
2883cf7f2e2dSJohn Marino == lookup_minimal_symbol_by_pc_section (pc, section))))
28845796c8dcSSimon Schubert {
28855796c8dcSSimon Schubert /* First pc of next line */
2886cf7f2e2dSJohn Marino pc = start_sal.end;
28875796c8dcSSimon Schubert /* Recalculate the line number (might not be N+1). */
2888cf7f2e2dSJohn Marino start_sal = find_pc_sect_line (pc, section, 0);
28895796c8dcSSimon Schubert }
28905796c8dcSSimon Schubert
28915796c8dcSSimon Schubert /* On targets with executable formats that don't have a concept of
28925796c8dcSSimon Schubert constructors (ELF with .init has, PE doesn't), gcc emits a call
28935796c8dcSSimon Schubert to `__main' in `main' between the prologue and before user
28945796c8dcSSimon Schubert code. */
2895cf7f2e2dSJohn Marino if (gdbarch_skip_main_prologue_p (gdbarch)
2896*ef5ccd6cSJohn Marino && name && strcmp_iw (name, "main") == 0)
28975796c8dcSSimon Schubert {
28985796c8dcSSimon Schubert pc = gdbarch_skip_main_prologue (gdbarch, pc);
28995796c8dcSSimon Schubert /* Recalculate the line number (might not be N+1). */
2900cf7f2e2dSJohn Marino start_sal = find_pc_sect_line (pc, section, 0);
2901c50c785cSJohn Marino force_skip = 1;
29025796c8dcSSimon Schubert }
2903c50c785cSJohn Marino }
2904c50c785cSJohn Marino while (!force_skip && skip--);
29055796c8dcSSimon Schubert
29065796c8dcSSimon Schubert /* If we still don't have a valid source line, try to find the first
29075796c8dcSSimon Schubert PC in the lineinfo table that belongs to the same function. This
29085796c8dcSSimon Schubert happens with COFF debug info, which does not seem to have an
29095796c8dcSSimon Schubert entry in lineinfo table for the code after the prologue which has
29105796c8dcSSimon Schubert no direct relation to source. For example, this was found to be
29115796c8dcSSimon Schubert the case with the DJGPP target using "gcc -gcoff" when the
29125796c8dcSSimon Schubert compiler inserted code after the prologue to make sure the stack
29135796c8dcSSimon Schubert is aligned. */
2914c50c785cSJohn Marino if (!force_skip && sym && start_sal.symtab == NULL)
29155796c8dcSSimon Schubert {
29165796c8dcSSimon Schubert pc = skip_prologue_using_lineinfo (pc, SYMBOL_SYMTAB (sym));
29175796c8dcSSimon Schubert /* Recalculate the line number. */
2918cf7f2e2dSJohn Marino start_sal = find_pc_sect_line (pc, section, 0);
29195796c8dcSSimon Schubert }
29205796c8dcSSimon Schubert
2921cf7f2e2dSJohn Marino do_cleanups (old_chain);
2922cf7f2e2dSJohn Marino
2923cf7f2e2dSJohn Marino /* If we're already past the prologue, leave SAL unchanged. Otherwise
2924cf7f2e2dSJohn Marino forward SAL to the end of the prologue. */
2925cf7f2e2dSJohn Marino if (sal->pc >= pc)
2926cf7f2e2dSJohn Marino return;
2927cf7f2e2dSJohn Marino
2928cf7f2e2dSJohn Marino sal->pc = pc;
2929cf7f2e2dSJohn Marino sal->section = section;
2930cf7f2e2dSJohn Marino
2931cf7f2e2dSJohn Marino /* Unless the explicit_line flag was set, update the SAL line
2932cf7f2e2dSJohn Marino and symtab to correspond to the modified PC location. */
2933cf7f2e2dSJohn Marino if (sal->explicit_line)
2934cf7f2e2dSJohn Marino return;
2935cf7f2e2dSJohn Marino
2936cf7f2e2dSJohn Marino sal->symtab = start_sal.symtab;
2937cf7f2e2dSJohn Marino sal->line = start_sal.line;
2938cf7f2e2dSJohn Marino sal->end = start_sal.end;
29395796c8dcSSimon Schubert
29405796c8dcSSimon Schubert /* Check if we are now inside an inlined function. If we can,
29415796c8dcSSimon Schubert use the call site of the function instead. */
2942cf7f2e2dSJohn Marino b = block_for_pc_sect (sal->pc, sal->section);
29435796c8dcSSimon Schubert function_block = NULL;
29445796c8dcSSimon Schubert while (b != NULL)
29455796c8dcSSimon Schubert {
29465796c8dcSSimon Schubert if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
29475796c8dcSSimon Schubert function_block = b;
29485796c8dcSSimon Schubert else if (BLOCK_FUNCTION (b) != NULL)
29495796c8dcSSimon Schubert break;
29505796c8dcSSimon Schubert b = BLOCK_SUPERBLOCK (b);
29515796c8dcSSimon Schubert }
29525796c8dcSSimon Schubert if (function_block != NULL
29535796c8dcSSimon Schubert && SYMBOL_LINE (BLOCK_FUNCTION (function_block)) != 0)
29545796c8dcSSimon Schubert {
2955cf7f2e2dSJohn Marino sal->line = SYMBOL_LINE (BLOCK_FUNCTION (function_block));
2956cf7f2e2dSJohn Marino sal->symtab = SYMBOL_SYMTAB (BLOCK_FUNCTION (function_block));
29575796c8dcSSimon Schubert }
29585796c8dcSSimon Schubert }
29595796c8dcSSimon Schubert
29605796c8dcSSimon Schubert /* If P is of the form "operator[ \t]+..." where `...' is
29615796c8dcSSimon Schubert some legitimate operator text, return a pointer to the
29625796c8dcSSimon Schubert beginning of the substring of the operator text.
29635796c8dcSSimon Schubert Otherwise, return "". */
2964*ef5ccd6cSJohn Marino
2965a45ae5f8SJohn Marino static char *
operator_chars(char * p,char ** end)29665796c8dcSSimon Schubert operator_chars (char *p, char **end)
29675796c8dcSSimon Schubert {
29685796c8dcSSimon Schubert *end = "";
29695796c8dcSSimon Schubert if (strncmp (p, "operator", 8))
29705796c8dcSSimon Schubert return *end;
29715796c8dcSSimon Schubert p += 8;
29725796c8dcSSimon Schubert
29735796c8dcSSimon Schubert /* Don't get faked out by `operator' being part of a longer
29745796c8dcSSimon Schubert identifier. */
29755796c8dcSSimon Schubert if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
29765796c8dcSSimon Schubert return *end;
29775796c8dcSSimon Schubert
29785796c8dcSSimon Schubert /* Allow some whitespace between `operator' and the operator symbol. */
29795796c8dcSSimon Schubert while (*p == ' ' || *p == '\t')
29805796c8dcSSimon Schubert p++;
29815796c8dcSSimon Schubert
29825796c8dcSSimon Schubert /* Recognize 'operator TYPENAME'. */
29835796c8dcSSimon Schubert
29845796c8dcSSimon Schubert if (isalpha (*p) || *p == '_' || *p == '$')
29855796c8dcSSimon Schubert {
29865796c8dcSSimon Schubert char *q = p + 1;
2987cf7f2e2dSJohn Marino
29885796c8dcSSimon Schubert while (isalnum (*q) || *q == '_' || *q == '$')
29895796c8dcSSimon Schubert q++;
29905796c8dcSSimon Schubert *end = q;
29915796c8dcSSimon Schubert return p;
29925796c8dcSSimon Schubert }
29935796c8dcSSimon Schubert
29945796c8dcSSimon Schubert while (*p)
29955796c8dcSSimon Schubert switch (*p)
29965796c8dcSSimon Schubert {
29975796c8dcSSimon Schubert case '\\': /* regexp quoting */
29985796c8dcSSimon Schubert if (p[1] == '*')
29995796c8dcSSimon Schubert {
30005796c8dcSSimon Schubert if (p[2] == '=') /* 'operator\*=' */
30015796c8dcSSimon Schubert *end = p + 3;
30025796c8dcSSimon Schubert else /* 'operator\*' */
30035796c8dcSSimon Schubert *end = p + 2;
30045796c8dcSSimon Schubert return p;
30055796c8dcSSimon Schubert }
30065796c8dcSSimon Schubert else if (p[1] == '[')
30075796c8dcSSimon Schubert {
30085796c8dcSSimon Schubert if (p[2] == ']')
3009c50c785cSJohn Marino error (_("mismatched quoting on brackets, "
3010c50c785cSJohn Marino "try 'operator\\[\\]'"));
30115796c8dcSSimon Schubert else if (p[2] == '\\' && p[3] == ']')
30125796c8dcSSimon Schubert {
30135796c8dcSSimon Schubert *end = p + 4; /* 'operator\[\]' */
30145796c8dcSSimon Schubert return p;
30155796c8dcSSimon Schubert }
30165796c8dcSSimon Schubert else
30175796c8dcSSimon Schubert error (_("nothing is allowed between '[' and ']'"));
30185796c8dcSSimon Schubert }
30195796c8dcSSimon Schubert else
30205796c8dcSSimon Schubert {
30215796c8dcSSimon Schubert /* Gratuitous qoute: skip it and move on. */
30225796c8dcSSimon Schubert p++;
30235796c8dcSSimon Schubert continue;
30245796c8dcSSimon Schubert }
30255796c8dcSSimon Schubert break;
30265796c8dcSSimon Schubert case '!':
30275796c8dcSSimon Schubert case '=':
30285796c8dcSSimon Schubert case '*':
30295796c8dcSSimon Schubert case '/':
30305796c8dcSSimon Schubert case '%':
30315796c8dcSSimon Schubert case '^':
30325796c8dcSSimon Schubert if (p[1] == '=')
30335796c8dcSSimon Schubert *end = p + 2;
30345796c8dcSSimon Schubert else
30355796c8dcSSimon Schubert *end = p + 1;
30365796c8dcSSimon Schubert return p;
30375796c8dcSSimon Schubert case '<':
30385796c8dcSSimon Schubert case '>':
30395796c8dcSSimon Schubert case '+':
30405796c8dcSSimon Schubert case '-':
30415796c8dcSSimon Schubert case '&':
30425796c8dcSSimon Schubert case '|':
30435796c8dcSSimon Schubert if (p[0] == '-' && p[1] == '>')
30445796c8dcSSimon Schubert {
30455796c8dcSSimon Schubert /* Struct pointer member operator 'operator->'. */
30465796c8dcSSimon Schubert if (p[2] == '*')
30475796c8dcSSimon Schubert {
30485796c8dcSSimon Schubert *end = p + 3; /* 'operator->*' */
30495796c8dcSSimon Schubert return p;
30505796c8dcSSimon Schubert }
30515796c8dcSSimon Schubert else if (p[2] == '\\')
30525796c8dcSSimon Schubert {
30535796c8dcSSimon Schubert *end = p + 4; /* Hopefully 'operator->\*' */
30545796c8dcSSimon Schubert return p;
30555796c8dcSSimon Schubert }
30565796c8dcSSimon Schubert else
30575796c8dcSSimon Schubert {
30585796c8dcSSimon Schubert *end = p + 2; /* 'operator->' */
30595796c8dcSSimon Schubert return p;
30605796c8dcSSimon Schubert }
30615796c8dcSSimon Schubert }
30625796c8dcSSimon Schubert if (p[1] == '=' || p[1] == p[0])
30635796c8dcSSimon Schubert *end = p + 2;
30645796c8dcSSimon Schubert else
30655796c8dcSSimon Schubert *end = p + 1;
30665796c8dcSSimon Schubert return p;
30675796c8dcSSimon Schubert case '~':
30685796c8dcSSimon Schubert case ',':
30695796c8dcSSimon Schubert *end = p + 1;
30705796c8dcSSimon Schubert return p;
30715796c8dcSSimon Schubert case '(':
30725796c8dcSSimon Schubert if (p[1] != ')')
3073c50c785cSJohn Marino error (_("`operator ()' must be specified "
3074c50c785cSJohn Marino "without whitespace in `()'"));
30755796c8dcSSimon Schubert *end = p + 2;
30765796c8dcSSimon Schubert return p;
30775796c8dcSSimon Schubert case '?':
30785796c8dcSSimon Schubert if (p[1] != ':')
3079c50c785cSJohn Marino error (_("`operator ?:' must be specified "
3080c50c785cSJohn Marino "without whitespace in `?:'"));
30815796c8dcSSimon Schubert *end = p + 2;
30825796c8dcSSimon Schubert return p;
30835796c8dcSSimon Schubert case '[':
30845796c8dcSSimon Schubert if (p[1] != ']')
3085c50c785cSJohn Marino error (_("`operator []' must be specified "
3086c50c785cSJohn Marino "without whitespace in `[]'"));
30875796c8dcSSimon Schubert *end = p + 2;
30885796c8dcSSimon Schubert return p;
30895796c8dcSSimon Schubert default:
30905796c8dcSSimon Schubert error (_("`operator %s' not supported"), p);
30915796c8dcSSimon Schubert break;
30925796c8dcSSimon Schubert }
30935796c8dcSSimon Schubert
30945796c8dcSSimon Schubert *end = "";
30955796c8dcSSimon Schubert return *end;
30965796c8dcSSimon Schubert }
30975796c8dcSSimon Schubert
30985796c8dcSSimon Schubert
3099*ef5ccd6cSJohn Marino /* Cache to watch for file names already seen by filename_seen. */
3100*ef5ccd6cSJohn Marino
3101*ef5ccd6cSJohn Marino struct filename_seen_cache
31025796c8dcSSimon Schubert {
31035796c8dcSSimon Schubert /* Table of files seen so far. */
3104*ef5ccd6cSJohn Marino htab_t tab;
3105*ef5ccd6cSJohn Marino /* Initial size of the table. It automagically grows from here. */
3106*ef5ccd6cSJohn Marino #define INITIAL_FILENAME_SEEN_CACHE_SIZE 100
3107*ef5ccd6cSJohn Marino };
31085796c8dcSSimon Schubert
3109*ef5ccd6cSJohn Marino /* filename_seen_cache constructor. */
3110*ef5ccd6cSJohn Marino
3111*ef5ccd6cSJohn Marino static struct filename_seen_cache *
create_filename_seen_cache(void)3112*ef5ccd6cSJohn Marino create_filename_seen_cache (void)
31135796c8dcSSimon Schubert {
3114*ef5ccd6cSJohn Marino struct filename_seen_cache *cache;
3115*ef5ccd6cSJohn Marino
3116*ef5ccd6cSJohn Marino cache = XNEW (struct filename_seen_cache);
3117*ef5ccd6cSJohn Marino cache->tab = htab_create_alloc (INITIAL_FILENAME_SEEN_CACHE_SIZE,
3118*ef5ccd6cSJohn Marino filename_hash, filename_eq,
3119*ef5ccd6cSJohn Marino NULL, xcalloc, xfree);
3120*ef5ccd6cSJohn Marino
3121*ef5ccd6cSJohn Marino return cache;
31225796c8dcSSimon Schubert }
31235796c8dcSSimon Schubert
3124*ef5ccd6cSJohn Marino /* Empty the cache, but do not delete it. */
3125*ef5ccd6cSJohn Marino
3126*ef5ccd6cSJohn Marino static void
clear_filename_seen_cache(struct filename_seen_cache * cache)3127*ef5ccd6cSJohn Marino clear_filename_seen_cache (struct filename_seen_cache *cache)
3128*ef5ccd6cSJohn Marino {
3129*ef5ccd6cSJohn Marino htab_empty (cache->tab);
3130*ef5ccd6cSJohn Marino }
3131*ef5ccd6cSJohn Marino
3132*ef5ccd6cSJohn Marino /* filename_seen_cache destructor.
3133*ef5ccd6cSJohn Marino This takes a void * argument as it is generally used as a cleanup. */
3134*ef5ccd6cSJohn Marino
3135*ef5ccd6cSJohn Marino static void
delete_filename_seen_cache(void * ptr)3136*ef5ccd6cSJohn Marino delete_filename_seen_cache (void *ptr)
3137*ef5ccd6cSJohn Marino {
3138*ef5ccd6cSJohn Marino struct filename_seen_cache *cache = ptr;
3139*ef5ccd6cSJohn Marino
3140*ef5ccd6cSJohn Marino htab_delete (cache->tab);
3141*ef5ccd6cSJohn Marino xfree (cache);
3142*ef5ccd6cSJohn Marino }
3143*ef5ccd6cSJohn Marino
3144*ef5ccd6cSJohn Marino /* If FILE is not already in the table of files in CACHE, return zero;
3145*ef5ccd6cSJohn Marino otherwise return non-zero. Optionally add FILE to the table if ADD
3146*ef5ccd6cSJohn Marino is non-zero.
3147*ef5ccd6cSJohn Marino
3148*ef5ccd6cSJohn Marino NOTE: We don't manage space for FILE, we assume FILE lives as long
3149*ef5ccd6cSJohn Marino as the caller needs. */
3150*ef5ccd6cSJohn Marino
3151*ef5ccd6cSJohn Marino static int
filename_seen(struct filename_seen_cache * cache,const char * file,int add)3152*ef5ccd6cSJohn Marino filename_seen (struct filename_seen_cache *cache, const char *file, int add)
3153*ef5ccd6cSJohn Marino {
3154*ef5ccd6cSJohn Marino void **slot;
3155*ef5ccd6cSJohn Marino
31565796c8dcSSimon Schubert /* Is FILE in tab? */
3157*ef5ccd6cSJohn Marino slot = htab_find_slot (cache->tab, file, add ? INSERT : NO_INSERT);
3158*ef5ccd6cSJohn Marino if (*slot != NULL)
31595796c8dcSSimon Schubert return 1;
31605796c8dcSSimon Schubert
31615796c8dcSSimon Schubert /* No; maybe add it to tab. */
31625796c8dcSSimon Schubert if (add)
3163*ef5ccd6cSJohn Marino *slot = (char *) file;
31645796c8dcSSimon Schubert
31655796c8dcSSimon Schubert return 0;
31665796c8dcSSimon Schubert }
31675796c8dcSSimon Schubert
3168*ef5ccd6cSJohn Marino /* Data structure to maintain printing state for output_source_filename. */
3169*ef5ccd6cSJohn Marino
3170*ef5ccd6cSJohn Marino struct output_source_filename_data
3171*ef5ccd6cSJohn Marino {
3172*ef5ccd6cSJohn Marino /* Cache of what we've seen so far. */
3173*ef5ccd6cSJohn Marino struct filename_seen_cache *filename_seen_cache;
3174*ef5ccd6cSJohn Marino
3175*ef5ccd6cSJohn Marino /* Flag of whether we're printing the first one. */
3176*ef5ccd6cSJohn Marino int first;
3177*ef5ccd6cSJohn Marino };
3178*ef5ccd6cSJohn Marino
31795796c8dcSSimon Schubert /* Slave routine for sources_info. Force line breaks at ,'s.
3180*ef5ccd6cSJohn Marino NAME is the name to print.
3181*ef5ccd6cSJohn Marino DATA contains the state for printing and watching for duplicates. */
3182*ef5ccd6cSJohn Marino
31835796c8dcSSimon Schubert static void
output_source_filename(const char * name,struct output_source_filename_data * data)3184*ef5ccd6cSJohn Marino output_source_filename (const char *name,
3185*ef5ccd6cSJohn Marino struct output_source_filename_data *data)
31865796c8dcSSimon Schubert {
31875796c8dcSSimon Schubert /* Since a single source file can result in several partial symbol
31885796c8dcSSimon Schubert tables, we need to avoid printing it more than once. Note: if
31895796c8dcSSimon Schubert some of the psymtabs are read in and some are not, it gets
31905796c8dcSSimon Schubert printed both under "Source files for which symbols have been
31915796c8dcSSimon Schubert read" and "Source files for which symbols will be read in on
31925796c8dcSSimon Schubert demand". I consider this a reasonable way to deal with the
31935796c8dcSSimon Schubert situation. I'm not sure whether this can also happen for
31945796c8dcSSimon Schubert symtabs; it doesn't hurt to check. */
31955796c8dcSSimon Schubert
31965796c8dcSSimon Schubert /* Was NAME already seen? */
3197*ef5ccd6cSJohn Marino if (filename_seen (data->filename_seen_cache, name, 1))
31985796c8dcSSimon Schubert {
31995796c8dcSSimon Schubert /* Yes; don't print it again. */
32005796c8dcSSimon Schubert return;
32015796c8dcSSimon Schubert }
3202*ef5ccd6cSJohn Marino
32035796c8dcSSimon Schubert /* No; print it and reset *FIRST. */
3204*ef5ccd6cSJohn Marino if (! data->first)
32055796c8dcSSimon Schubert printf_filtered (", ");
3206*ef5ccd6cSJohn Marino data->first = 0;
32075796c8dcSSimon Schubert
32085796c8dcSSimon Schubert wrap_here ("");
32095796c8dcSSimon Schubert fputs_filtered (name, gdb_stdout);
32105796c8dcSSimon Schubert }
32115796c8dcSSimon Schubert
3212cf7f2e2dSJohn Marino /* A callback for map_partial_symbol_filenames. */
3213*ef5ccd6cSJohn Marino
3214cf7f2e2dSJohn Marino static void
output_partial_symbol_filename(const char * filename,const char * fullname,void * data)3215c50c785cSJohn Marino output_partial_symbol_filename (const char *filename, const char *fullname,
3216cf7f2e2dSJohn Marino void *data)
3217cf7f2e2dSJohn Marino {
3218cf7f2e2dSJohn Marino output_source_filename (fullname ? fullname : filename, data);
3219cf7f2e2dSJohn Marino }
3220cf7f2e2dSJohn Marino
32215796c8dcSSimon Schubert static void
sources_info(char * ignore,int from_tty)32225796c8dcSSimon Schubert sources_info (char *ignore, int from_tty)
32235796c8dcSSimon Schubert {
32245796c8dcSSimon Schubert struct symtab *s;
32255796c8dcSSimon Schubert struct objfile *objfile;
3226*ef5ccd6cSJohn Marino struct output_source_filename_data data;
3227*ef5ccd6cSJohn Marino struct cleanup *cleanups;
32285796c8dcSSimon Schubert
32295796c8dcSSimon Schubert if (!have_full_symbols () && !have_partial_symbols ())
32305796c8dcSSimon Schubert {
32315796c8dcSSimon Schubert error (_("No symbol table is loaded. Use the \"file\" command."));
32325796c8dcSSimon Schubert }
32335796c8dcSSimon Schubert
3234*ef5ccd6cSJohn Marino data.filename_seen_cache = create_filename_seen_cache ();
3235*ef5ccd6cSJohn Marino cleanups = make_cleanup (delete_filename_seen_cache,
3236*ef5ccd6cSJohn Marino data.filename_seen_cache);
3237*ef5ccd6cSJohn Marino
32385796c8dcSSimon Schubert printf_filtered ("Source files for which symbols have been read in:\n\n");
32395796c8dcSSimon Schubert
3240*ef5ccd6cSJohn Marino data.first = 1;
32415796c8dcSSimon Schubert ALL_SYMTABS (objfile, s)
32425796c8dcSSimon Schubert {
32435796c8dcSSimon Schubert const char *fullname = symtab_to_fullname (s);
3244cf7f2e2dSJohn Marino
3245*ef5ccd6cSJohn Marino output_source_filename (fullname, &data);
32465796c8dcSSimon Schubert }
32475796c8dcSSimon Schubert printf_filtered ("\n\n");
32485796c8dcSSimon Schubert
3249c50c785cSJohn Marino printf_filtered ("Source files for which symbols "
3250c50c785cSJohn Marino "will be read in on demand:\n\n");
32515796c8dcSSimon Schubert
3252*ef5ccd6cSJohn Marino clear_filename_seen_cache (data.filename_seen_cache);
3253*ef5ccd6cSJohn Marino data.first = 1;
3254*ef5ccd6cSJohn Marino map_partial_symbol_filenames (output_partial_symbol_filename, &data,
3255a45ae5f8SJohn Marino 1 /*need_fullname*/);
32565796c8dcSSimon Schubert printf_filtered ("\n");
3257*ef5ccd6cSJohn Marino
3258*ef5ccd6cSJohn Marino do_cleanups (cleanups);
32595796c8dcSSimon Schubert }
32605796c8dcSSimon Schubert
3261*ef5ccd6cSJohn Marino /* Compare FILE against all the NFILES entries of FILES. If BASENAMES is
3262*ef5ccd6cSJohn Marino non-zero compare only lbasename of FILES. */
3263*ef5ccd6cSJohn Marino
32645796c8dcSSimon Schubert static int
file_matches(const char * file,char * files[],int nfiles,int basenames)3265*ef5ccd6cSJohn Marino file_matches (const char *file, char *files[], int nfiles, int basenames)
32665796c8dcSSimon Schubert {
32675796c8dcSSimon Schubert int i;
32685796c8dcSSimon Schubert
32695796c8dcSSimon Schubert if (file != NULL && nfiles != 0)
32705796c8dcSSimon Schubert {
32715796c8dcSSimon Schubert for (i = 0; i < nfiles; i++)
32725796c8dcSSimon Schubert {
3273*ef5ccd6cSJohn Marino if (compare_filenames_for_search (file, (basenames
3274*ef5ccd6cSJohn Marino ? lbasename (files[i])
3275*ef5ccd6cSJohn Marino : files[i])))
32765796c8dcSSimon Schubert return 1;
32775796c8dcSSimon Schubert }
32785796c8dcSSimon Schubert }
32795796c8dcSSimon Schubert else if (nfiles == 0)
32805796c8dcSSimon Schubert return 1;
32815796c8dcSSimon Schubert return 0;
32825796c8dcSSimon Schubert }
32835796c8dcSSimon Schubert
32845796c8dcSSimon Schubert /* Free any memory associated with a search. */
3285*ef5ccd6cSJohn Marino
32865796c8dcSSimon Schubert void
free_search_symbols(struct symbol_search * symbols)32875796c8dcSSimon Schubert free_search_symbols (struct symbol_search *symbols)
32885796c8dcSSimon Schubert {
32895796c8dcSSimon Schubert struct symbol_search *p;
32905796c8dcSSimon Schubert struct symbol_search *next;
32915796c8dcSSimon Schubert
32925796c8dcSSimon Schubert for (p = symbols; p != NULL; p = next)
32935796c8dcSSimon Schubert {
32945796c8dcSSimon Schubert next = p->next;
32955796c8dcSSimon Schubert xfree (p);
32965796c8dcSSimon Schubert }
32975796c8dcSSimon Schubert }
32985796c8dcSSimon Schubert
32995796c8dcSSimon Schubert static void
do_free_search_symbols_cleanup(void * symbols)33005796c8dcSSimon Schubert do_free_search_symbols_cleanup (void *symbols)
33015796c8dcSSimon Schubert {
33025796c8dcSSimon Schubert free_search_symbols (symbols);
33035796c8dcSSimon Schubert }
33045796c8dcSSimon Schubert
33055796c8dcSSimon Schubert struct cleanup *
make_cleanup_free_search_symbols(struct symbol_search * symbols)33065796c8dcSSimon Schubert make_cleanup_free_search_symbols (struct symbol_search *symbols)
33075796c8dcSSimon Schubert {
33085796c8dcSSimon Schubert return make_cleanup (do_free_search_symbols_cleanup, symbols);
33095796c8dcSSimon Schubert }
33105796c8dcSSimon Schubert
33115796c8dcSSimon Schubert /* Helper function for sort_search_symbols and qsort. Can only
33125796c8dcSSimon Schubert sort symbols, not minimal symbols. */
3313*ef5ccd6cSJohn Marino
33145796c8dcSSimon Schubert static int
compare_search_syms(const void * sa,const void * sb)33155796c8dcSSimon Schubert compare_search_syms (const void *sa, const void *sb)
33165796c8dcSSimon Schubert {
33175796c8dcSSimon Schubert struct symbol_search **sym_a = (struct symbol_search **) sa;
33185796c8dcSSimon Schubert struct symbol_search **sym_b = (struct symbol_search **) sb;
33195796c8dcSSimon Schubert
33205796c8dcSSimon Schubert return strcmp (SYMBOL_PRINT_NAME ((*sym_a)->symbol),
33215796c8dcSSimon Schubert SYMBOL_PRINT_NAME ((*sym_b)->symbol));
33225796c8dcSSimon Schubert }
33235796c8dcSSimon Schubert
33245796c8dcSSimon Schubert /* Sort the ``nfound'' symbols in the list after prevtail. Leave
33255796c8dcSSimon Schubert prevtail where it is, but update its next pointer to point to
33265796c8dcSSimon Schubert the first of the sorted symbols. */
3327*ef5ccd6cSJohn Marino
33285796c8dcSSimon Schubert static struct symbol_search *
sort_search_symbols(struct symbol_search * prevtail,int nfound)33295796c8dcSSimon Schubert sort_search_symbols (struct symbol_search *prevtail, int nfound)
33305796c8dcSSimon Schubert {
33315796c8dcSSimon Schubert struct symbol_search **symbols, *symp, *old_next;
33325796c8dcSSimon Schubert int i;
33335796c8dcSSimon Schubert
33345796c8dcSSimon Schubert symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
33355796c8dcSSimon Schubert * nfound);
33365796c8dcSSimon Schubert symp = prevtail->next;
33375796c8dcSSimon Schubert for (i = 0; i < nfound; i++)
33385796c8dcSSimon Schubert {
33395796c8dcSSimon Schubert symbols[i] = symp;
33405796c8dcSSimon Schubert symp = symp->next;
33415796c8dcSSimon Schubert }
33425796c8dcSSimon Schubert /* Generally NULL. */
33435796c8dcSSimon Schubert old_next = symp;
33445796c8dcSSimon Schubert
33455796c8dcSSimon Schubert qsort (symbols, nfound, sizeof (struct symbol_search *),
33465796c8dcSSimon Schubert compare_search_syms);
33475796c8dcSSimon Schubert
33485796c8dcSSimon Schubert symp = prevtail;
33495796c8dcSSimon Schubert for (i = 0; i < nfound; i++)
33505796c8dcSSimon Schubert {
33515796c8dcSSimon Schubert symp->next = symbols[i];
33525796c8dcSSimon Schubert symp = symp->next;
33535796c8dcSSimon Schubert }
33545796c8dcSSimon Schubert symp->next = old_next;
33555796c8dcSSimon Schubert
33565796c8dcSSimon Schubert xfree (symbols);
33575796c8dcSSimon Schubert return symp;
33585796c8dcSSimon Schubert }
33595796c8dcSSimon Schubert
3360cf7f2e2dSJohn Marino /* An object of this type is passed as the user_data to the
3361cf7f2e2dSJohn Marino expand_symtabs_matching method. */
3362cf7f2e2dSJohn Marino struct search_symbols_data
3363cf7f2e2dSJohn Marino {
3364cf7f2e2dSJohn Marino int nfiles;
3365cf7f2e2dSJohn Marino char **files;
3366a45ae5f8SJohn Marino
3367a45ae5f8SJohn Marino /* It is true if PREG contains valid data, false otherwise. */
3368a45ae5f8SJohn Marino unsigned preg_p : 1;
3369a45ae5f8SJohn Marino regex_t preg;
3370cf7f2e2dSJohn Marino };
3371cf7f2e2dSJohn Marino
3372cf7f2e2dSJohn Marino /* A callback for expand_symtabs_matching. */
3373*ef5ccd6cSJohn Marino
3374cf7f2e2dSJohn Marino static int
search_symbols_file_matches(const char * filename,void * user_data,int basenames)3375*ef5ccd6cSJohn Marino search_symbols_file_matches (const char *filename, void *user_data,
3376*ef5ccd6cSJohn Marino int basenames)
3377cf7f2e2dSJohn Marino {
3378cf7f2e2dSJohn Marino struct search_symbols_data *data = user_data;
3379cf7f2e2dSJohn Marino
3380*ef5ccd6cSJohn Marino return file_matches (filename, data->files, data->nfiles, basenames);
3381cf7f2e2dSJohn Marino }
3382cf7f2e2dSJohn Marino
3383cf7f2e2dSJohn Marino /* A callback for expand_symtabs_matching. */
3384*ef5ccd6cSJohn Marino
3385cf7f2e2dSJohn Marino static int
search_symbols_name_matches(const char * symname,void * user_data)3386*ef5ccd6cSJohn Marino search_symbols_name_matches (const char *symname, void *user_data)
3387cf7f2e2dSJohn Marino {
3388cf7f2e2dSJohn Marino struct search_symbols_data *data = user_data;
3389cf7f2e2dSJohn Marino
3390a45ae5f8SJohn Marino return !data->preg_p || regexec (&data->preg, symname, 0, NULL, 0) == 0;
3391cf7f2e2dSJohn Marino }
3392cf7f2e2dSJohn Marino
33935796c8dcSSimon Schubert /* Search the symbol table for matches to the regular expression REGEXP,
33945796c8dcSSimon Schubert returning the results in *MATCHES.
33955796c8dcSSimon Schubert
33965796c8dcSSimon Schubert Only symbols of KIND are searched:
33975796c8dcSSimon Schubert VARIABLES_DOMAIN - search all symbols, excluding functions, type names,
33985796c8dcSSimon Schubert and constants (enums)
3399a45ae5f8SJohn Marino FUNCTIONS_DOMAIN - search all functions
3400a45ae5f8SJohn Marino TYPES_DOMAIN - search all type names
3401c50c785cSJohn Marino ALL_DOMAIN - an internal error for this function
34025796c8dcSSimon Schubert
34035796c8dcSSimon Schubert free_search_symbols should be called when *MATCHES is no longer needed.
34045796c8dcSSimon Schubert
34055796c8dcSSimon Schubert The results are sorted locally; each symtab's global and static blocks are
3406c50c785cSJohn Marino separately alphabetized. */
3407c50c785cSJohn Marino
34085796c8dcSSimon Schubert void
search_symbols(char * regexp,enum search_domain kind,int nfiles,char * files[],struct symbol_search ** matches)3409a45ae5f8SJohn Marino search_symbols (char *regexp, enum search_domain kind,
3410a45ae5f8SJohn Marino int nfiles, char *files[],
34115796c8dcSSimon Schubert struct symbol_search **matches)
34125796c8dcSSimon Schubert {
34135796c8dcSSimon Schubert struct symtab *s;
34145796c8dcSSimon Schubert struct blockvector *bv;
34155796c8dcSSimon Schubert struct block *b;
34165796c8dcSSimon Schubert int i = 0;
3417*ef5ccd6cSJohn Marino struct block_iterator iter;
34185796c8dcSSimon Schubert struct symbol *sym;
34195796c8dcSSimon Schubert struct objfile *objfile;
34205796c8dcSSimon Schubert struct minimal_symbol *msymbol;
34215796c8dcSSimon Schubert int found_misc = 0;
3422c50c785cSJohn Marino static const enum minimal_symbol_type types[]
3423a45ae5f8SJohn Marino = {mst_data, mst_text, mst_abs};
3424c50c785cSJohn Marino static const enum minimal_symbol_type types2[]
3425a45ae5f8SJohn Marino = {mst_bss, mst_file_text, mst_abs};
3426c50c785cSJohn Marino static const enum minimal_symbol_type types3[]
3427a45ae5f8SJohn Marino = {mst_file_data, mst_solib_trampoline, mst_abs};
3428c50c785cSJohn Marino static const enum minimal_symbol_type types4[]
3429a45ae5f8SJohn Marino = {mst_file_bss, mst_text_gnu_ifunc, mst_abs};
34305796c8dcSSimon Schubert enum minimal_symbol_type ourtype;
34315796c8dcSSimon Schubert enum minimal_symbol_type ourtype2;
34325796c8dcSSimon Schubert enum minimal_symbol_type ourtype3;
34335796c8dcSSimon Schubert enum minimal_symbol_type ourtype4;
34345796c8dcSSimon Schubert struct symbol_search *sr;
34355796c8dcSSimon Schubert struct symbol_search *psr;
34365796c8dcSSimon Schubert struct symbol_search *tail;
3437cf7f2e2dSJohn Marino struct search_symbols_data datum;
34385796c8dcSSimon Schubert
3439a45ae5f8SJohn Marino /* OLD_CHAIN .. RETVAL_CHAIN is always freed, RETVAL_CHAIN .. current
3440a45ae5f8SJohn Marino CLEANUP_CHAIN is freed only in the case of an error. */
3441a45ae5f8SJohn Marino struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
3442a45ae5f8SJohn Marino struct cleanup *retval_chain;
34435796c8dcSSimon Schubert
3444a45ae5f8SJohn Marino gdb_assert (kind <= TYPES_DOMAIN);
3445a45ae5f8SJohn Marino
3446a45ae5f8SJohn Marino ourtype = types[kind];
3447a45ae5f8SJohn Marino ourtype2 = types2[kind];
3448a45ae5f8SJohn Marino ourtype3 = types3[kind];
3449a45ae5f8SJohn Marino ourtype4 = types4[kind];
34505796c8dcSSimon Schubert
34515796c8dcSSimon Schubert sr = *matches = NULL;
34525796c8dcSSimon Schubert tail = NULL;
3453a45ae5f8SJohn Marino datum.preg_p = 0;
34545796c8dcSSimon Schubert
34555796c8dcSSimon Schubert if (regexp != NULL)
34565796c8dcSSimon Schubert {
34575796c8dcSSimon Schubert /* Make sure spacing is right for C++ operators.
34585796c8dcSSimon Schubert This is just a courtesy to make the matching less sensitive
34595796c8dcSSimon Schubert to how many spaces the user leaves between 'operator'
34605796c8dcSSimon Schubert and <TYPENAME> or <OPERATOR>. */
34615796c8dcSSimon Schubert char *opend;
34625796c8dcSSimon Schubert char *opname = operator_chars (regexp, &opend);
3463a45ae5f8SJohn Marino int errcode;
3464cf7f2e2dSJohn Marino
34655796c8dcSSimon Schubert if (*opname)
34665796c8dcSSimon Schubert {
3467c50c785cSJohn Marino int fix = -1; /* -1 means ok; otherwise number of
3468c50c785cSJohn Marino spaces needed. */
3469cf7f2e2dSJohn Marino
34705796c8dcSSimon Schubert if (isalpha (*opname) || *opname == '_' || *opname == '$')
34715796c8dcSSimon Schubert {
34725796c8dcSSimon Schubert /* There should 1 space between 'operator' and 'TYPENAME'. */
34735796c8dcSSimon Schubert if (opname[-1] != ' ' || opname[-2] == ' ')
34745796c8dcSSimon Schubert fix = 1;
34755796c8dcSSimon Schubert }
34765796c8dcSSimon Schubert else
34775796c8dcSSimon Schubert {
34785796c8dcSSimon Schubert /* There should 0 spaces between 'operator' and 'OPERATOR'. */
34795796c8dcSSimon Schubert if (opname[-1] == ' ')
34805796c8dcSSimon Schubert fix = 0;
34815796c8dcSSimon Schubert }
34825796c8dcSSimon Schubert /* If wrong number of spaces, fix it. */
34835796c8dcSSimon Schubert if (fix >= 0)
34845796c8dcSSimon Schubert {
34855796c8dcSSimon Schubert char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
3486cf7f2e2dSJohn Marino
34875796c8dcSSimon Schubert sprintf (tmp, "operator%.*s%s", fix, " ", opname);
34885796c8dcSSimon Schubert regexp = tmp;
34895796c8dcSSimon Schubert }
34905796c8dcSSimon Schubert }
34915796c8dcSSimon Schubert
3492a45ae5f8SJohn Marino errcode = regcomp (&datum.preg, regexp,
3493a45ae5f8SJohn Marino REG_NOSUB | (case_sensitivity == case_sensitive_off
3494a45ae5f8SJohn Marino ? REG_ICASE : 0));
3495a45ae5f8SJohn Marino if (errcode != 0)
3496a45ae5f8SJohn Marino {
3497a45ae5f8SJohn Marino char *err = get_regcomp_error (errcode, &datum.preg);
3498a45ae5f8SJohn Marino
3499a45ae5f8SJohn Marino make_cleanup (xfree, err);
3500a45ae5f8SJohn Marino error (_("Invalid regexp (%s): %s"), err, regexp);
3501a45ae5f8SJohn Marino }
3502a45ae5f8SJohn Marino datum.preg_p = 1;
3503a45ae5f8SJohn Marino make_regfree_cleanup (&datum.preg);
35045796c8dcSSimon Schubert }
35055796c8dcSSimon Schubert
35065796c8dcSSimon Schubert /* Search through the partial symtabs *first* for all symbols
35075796c8dcSSimon Schubert matching the regexp. That way we don't have to reproduce all of
35085796c8dcSSimon Schubert the machinery below. */
35095796c8dcSSimon Schubert
3510cf7f2e2dSJohn Marino datum.nfiles = nfiles;
3511cf7f2e2dSJohn Marino datum.files = files;
3512cf7f2e2dSJohn Marino ALL_OBJFILES (objfile)
35135796c8dcSSimon Schubert {
3514cf7f2e2dSJohn Marino if (objfile->sf)
3515cf7f2e2dSJohn Marino objfile->sf->qf->expand_symtabs_matching (objfile,
3516*ef5ccd6cSJohn Marino (nfiles == 0
3517*ef5ccd6cSJohn Marino ? NULL
3518*ef5ccd6cSJohn Marino : search_symbols_file_matches),
3519cf7f2e2dSJohn Marino search_symbols_name_matches,
3520cf7f2e2dSJohn Marino kind,
3521cf7f2e2dSJohn Marino &datum);
35225796c8dcSSimon Schubert }
35235796c8dcSSimon Schubert
3524a45ae5f8SJohn Marino retval_chain = old_chain;
3525a45ae5f8SJohn Marino
35265796c8dcSSimon Schubert /* Here, we search through the minimal symbol tables for functions
35275796c8dcSSimon Schubert and variables that match, and force their symbols to be read.
35285796c8dcSSimon Schubert This is in particular necessary for demangled variable names,
35295796c8dcSSimon Schubert which are no longer put into the partial symbol tables.
35305796c8dcSSimon Schubert The symbol will then be found during the scan of symtabs below.
35315796c8dcSSimon Schubert
35325796c8dcSSimon Schubert For functions, find_pc_symtab should succeed if we have debug info
3533*ef5ccd6cSJohn Marino for the function, for variables we have to call
3534*ef5ccd6cSJohn Marino lookup_symbol_in_objfile_from_linkage_name to determine if the variable
3535*ef5ccd6cSJohn Marino has debug info.
35365796c8dcSSimon Schubert If the lookup fails, set found_misc so that we will rescan to print
3537*ef5ccd6cSJohn Marino any matching symbols without debug info.
3538*ef5ccd6cSJohn Marino We only search the objfile the msymbol came from, we no longer search
3539*ef5ccd6cSJohn Marino all objfiles. In large programs (1000s of shared libs) searching all
3540*ef5ccd6cSJohn Marino objfiles is not worth the pain. */
35415796c8dcSSimon Schubert
35425796c8dcSSimon Schubert if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
35435796c8dcSSimon Schubert {
35445796c8dcSSimon Schubert ALL_MSYMBOLS (objfile, msymbol)
35455796c8dcSSimon Schubert {
35465796c8dcSSimon Schubert QUIT;
35475796c8dcSSimon Schubert
3548*ef5ccd6cSJohn Marino if (msymbol->created_by_gdb)
3549*ef5ccd6cSJohn Marino continue;
3550*ef5ccd6cSJohn Marino
3551*ef5ccd6cSJohn Marino if (MSYMBOL_TYPE (msymbol) == ourtype
3552*ef5ccd6cSJohn Marino || MSYMBOL_TYPE (msymbol) == ourtype2
3553*ef5ccd6cSJohn Marino || MSYMBOL_TYPE (msymbol) == ourtype3
3554*ef5ccd6cSJohn Marino || MSYMBOL_TYPE (msymbol) == ourtype4)
35555796c8dcSSimon Schubert {
3556a45ae5f8SJohn Marino if (!datum.preg_p
3557a45ae5f8SJohn Marino || regexec (&datum.preg, SYMBOL_NATURAL_NAME (msymbol), 0,
3558a45ae5f8SJohn Marino NULL, 0) == 0)
35595796c8dcSSimon Schubert {
3560*ef5ccd6cSJohn Marino /* Note: An important side-effect of these lookup functions
3561*ef5ccd6cSJohn Marino is to expand the symbol table if msymbol is found, for the
3562*ef5ccd6cSJohn Marino benefit of the next loop on ALL_PRIMARY_SYMTABS. */
35635796c8dcSSimon Schubert if (kind == FUNCTIONS_DOMAIN
3564*ef5ccd6cSJohn Marino ? find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)) == NULL
3565*ef5ccd6cSJohn Marino : (lookup_symbol_in_objfile_from_linkage_name
3566*ef5ccd6cSJohn Marino (objfile, SYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
3567*ef5ccd6cSJohn Marino == NULL))
35685796c8dcSSimon Schubert found_misc = 1;
35695796c8dcSSimon Schubert }
35705796c8dcSSimon Schubert }
35715796c8dcSSimon Schubert }
35725796c8dcSSimon Schubert }
35735796c8dcSSimon Schubert
35745796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s)
35755796c8dcSSimon Schubert {
35765796c8dcSSimon Schubert bv = BLOCKVECTOR (s);
35775796c8dcSSimon Schubert for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
35785796c8dcSSimon Schubert {
35795796c8dcSSimon Schubert struct symbol_search *prevtail = tail;
35805796c8dcSSimon Schubert int nfound = 0;
3581cf7f2e2dSJohn Marino
35825796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (bv, i);
35835796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (b, iter, sym)
35845796c8dcSSimon Schubert {
35855796c8dcSSimon Schubert struct symtab *real_symtab = SYMBOL_SYMTAB (sym);
3586cf7f2e2dSJohn Marino
35875796c8dcSSimon Schubert QUIT;
35885796c8dcSSimon Schubert
3589*ef5ccd6cSJohn Marino /* Check first sole REAL_SYMTAB->FILENAME. It does not need to be
3590*ef5ccd6cSJohn Marino a substring of symtab_to_fullname as it may contain "./" etc. */
3591*ef5ccd6cSJohn Marino if ((file_matches (real_symtab->filename, files, nfiles, 0)
3592*ef5ccd6cSJohn Marino || ((basenames_may_differ
3593*ef5ccd6cSJohn Marino || file_matches (lbasename (real_symtab->filename),
3594*ef5ccd6cSJohn Marino files, nfiles, 1))
3595*ef5ccd6cSJohn Marino && file_matches (symtab_to_fullname (real_symtab),
3596*ef5ccd6cSJohn Marino files, nfiles, 0)))
3597a45ae5f8SJohn Marino && ((!datum.preg_p
3598a45ae5f8SJohn Marino || regexec (&datum.preg, SYMBOL_NATURAL_NAME (sym), 0,
3599a45ae5f8SJohn Marino NULL, 0) == 0)
3600cf7f2e2dSJohn Marino && ((kind == VARIABLES_DOMAIN
3601cf7f2e2dSJohn Marino && SYMBOL_CLASS (sym) != LOC_TYPEDEF
3602cf7f2e2dSJohn Marino && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
36035796c8dcSSimon Schubert && SYMBOL_CLASS (sym) != LOC_BLOCK
3604cf7f2e2dSJohn Marino /* LOC_CONST can be used for more than just enums,
3605cf7f2e2dSJohn Marino e.g., c++ static const members.
3606cf7f2e2dSJohn Marino We only want to skip enums here. */
3607cf7f2e2dSJohn Marino && !(SYMBOL_CLASS (sym) == LOC_CONST
3608c50c785cSJohn Marino && TYPE_CODE (SYMBOL_TYPE (sym))
3609c50c785cSJohn Marino == TYPE_CODE_ENUM))
3610c50c785cSJohn Marino || (kind == FUNCTIONS_DOMAIN
3611c50c785cSJohn Marino && SYMBOL_CLASS (sym) == LOC_BLOCK)
3612c50c785cSJohn Marino || (kind == TYPES_DOMAIN
3613c50c785cSJohn Marino && SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
36145796c8dcSSimon Schubert {
36155796c8dcSSimon Schubert /* match */
3616c50c785cSJohn Marino psr = (struct symbol_search *)
3617c50c785cSJohn Marino xmalloc (sizeof (struct symbol_search));
36185796c8dcSSimon Schubert psr->block = i;
36195796c8dcSSimon Schubert psr->symtab = real_symtab;
36205796c8dcSSimon Schubert psr->symbol = sym;
36215796c8dcSSimon Schubert psr->msymbol = NULL;
36225796c8dcSSimon Schubert psr->next = NULL;
36235796c8dcSSimon Schubert if (tail == NULL)
36245796c8dcSSimon Schubert sr = psr;
36255796c8dcSSimon Schubert else
36265796c8dcSSimon Schubert tail->next = psr;
36275796c8dcSSimon Schubert tail = psr;
36285796c8dcSSimon Schubert nfound ++;
36295796c8dcSSimon Schubert }
36305796c8dcSSimon Schubert }
36315796c8dcSSimon Schubert if (nfound > 0)
36325796c8dcSSimon Schubert {
36335796c8dcSSimon Schubert if (prevtail == NULL)
36345796c8dcSSimon Schubert {
36355796c8dcSSimon Schubert struct symbol_search dummy;
36365796c8dcSSimon Schubert
36375796c8dcSSimon Schubert dummy.next = sr;
36385796c8dcSSimon Schubert tail = sort_search_symbols (&dummy, nfound);
36395796c8dcSSimon Schubert sr = dummy.next;
36405796c8dcSSimon Schubert
3641a45ae5f8SJohn Marino make_cleanup_free_search_symbols (sr);
36425796c8dcSSimon Schubert }
36435796c8dcSSimon Schubert else
36445796c8dcSSimon Schubert tail = sort_search_symbols (prevtail, nfound);
36455796c8dcSSimon Schubert }
36465796c8dcSSimon Schubert }
36475796c8dcSSimon Schubert }
36485796c8dcSSimon Schubert
36495796c8dcSSimon Schubert /* If there are no eyes, avoid all contact. I mean, if there are
36505796c8dcSSimon Schubert no debug symbols, then print directly from the msymbol_vector. */
36515796c8dcSSimon Schubert
3652*ef5ccd6cSJohn Marino if (found_misc || (nfiles == 0 && kind != FUNCTIONS_DOMAIN))
36535796c8dcSSimon Schubert {
36545796c8dcSSimon Schubert ALL_MSYMBOLS (objfile, msymbol)
36555796c8dcSSimon Schubert {
36565796c8dcSSimon Schubert QUIT;
36575796c8dcSSimon Schubert
3658*ef5ccd6cSJohn Marino if (msymbol->created_by_gdb)
3659*ef5ccd6cSJohn Marino continue;
3660*ef5ccd6cSJohn Marino
3661*ef5ccd6cSJohn Marino if (MSYMBOL_TYPE (msymbol) == ourtype
3662*ef5ccd6cSJohn Marino || MSYMBOL_TYPE (msymbol) == ourtype2
3663*ef5ccd6cSJohn Marino || MSYMBOL_TYPE (msymbol) == ourtype3
3664*ef5ccd6cSJohn Marino || MSYMBOL_TYPE (msymbol) == ourtype4)
36655796c8dcSSimon Schubert {
3666a45ae5f8SJohn Marino if (!datum.preg_p
3667a45ae5f8SJohn Marino || regexec (&datum.preg, SYMBOL_NATURAL_NAME (msymbol), 0,
3668a45ae5f8SJohn Marino NULL, 0) == 0)
36695796c8dcSSimon Schubert {
3670*ef5ccd6cSJohn Marino /* For functions we can do a quick check of whether the
3671*ef5ccd6cSJohn Marino symbol might be found via find_pc_symtab. */
3672*ef5ccd6cSJohn Marino if (kind != FUNCTIONS_DOMAIN
3673*ef5ccd6cSJohn Marino || find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)) == NULL)
36745796c8dcSSimon Schubert {
3675*ef5ccd6cSJohn Marino if (lookup_symbol_in_objfile_from_linkage_name
3676*ef5ccd6cSJohn Marino (objfile, SYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
36775796c8dcSSimon Schubert == NULL)
36785796c8dcSSimon Schubert {
36795796c8dcSSimon Schubert /* match */
3680c50c785cSJohn Marino psr = (struct symbol_search *)
3681c50c785cSJohn Marino xmalloc (sizeof (struct symbol_search));
36825796c8dcSSimon Schubert psr->block = i;
36835796c8dcSSimon Schubert psr->msymbol = msymbol;
36845796c8dcSSimon Schubert psr->symtab = NULL;
36855796c8dcSSimon Schubert psr->symbol = NULL;
36865796c8dcSSimon Schubert psr->next = NULL;
36875796c8dcSSimon Schubert if (tail == NULL)
36885796c8dcSSimon Schubert {
36895796c8dcSSimon Schubert sr = psr;
3690a45ae5f8SJohn Marino make_cleanup_free_search_symbols (sr);
36915796c8dcSSimon Schubert }
36925796c8dcSSimon Schubert else
36935796c8dcSSimon Schubert tail->next = psr;
36945796c8dcSSimon Schubert tail = psr;
36955796c8dcSSimon Schubert }
36965796c8dcSSimon Schubert }
36975796c8dcSSimon Schubert }
36985796c8dcSSimon Schubert }
36995796c8dcSSimon Schubert }
37005796c8dcSSimon Schubert }
37015796c8dcSSimon Schubert
3702a45ae5f8SJohn Marino discard_cleanups (retval_chain);
3703a45ae5f8SJohn Marino do_cleanups (old_chain);
37045796c8dcSSimon Schubert *matches = sr;
37055796c8dcSSimon Schubert }
37065796c8dcSSimon Schubert
37075796c8dcSSimon Schubert /* Helper function for symtab_symbol_info, this function uses
37085796c8dcSSimon Schubert the data returned from search_symbols() to print information
3709c50c785cSJohn Marino regarding the match to gdb_stdout. */
3710c50c785cSJohn Marino
37115796c8dcSSimon Schubert static void
print_symbol_info(enum search_domain kind,struct symtab * s,struct symbol * sym,int block,const char * last)3712a45ae5f8SJohn Marino print_symbol_info (enum search_domain kind,
3713a45ae5f8SJohn Marino struct symtab *s, struct symbol *sym,
3714*ef5ccd6cSJohn Marino int block, const char *last)
37155796c8dcSSimon Schubert {
3716*ef5ccd6cSJohn Marino const char *s_filename = symtab_to_filename_for_display (s);
3717*ef5ccd6cSJohn Marino
3718*ef5ccd6cSJohn Marino if (last == NULL || filename_cmp (last, s_filename) != 0)
37195796c8dcSSimon Schubert {
37205796c8dcSSimon Schubert fputs_filtered ("\nFile ", gdb_stdout);
3721*ef5ccd6cSJohn Marino fputs_filtered (s_filename, gdb_stdout);
37225796c8dcSSimon Schubert fputs_filtered (":\n", gdb_stdout);
37235796c8dcSSimon Schubert }
37245796c8dcSSimon Schubert
37255796c8dcSSimon Schubert if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
37265796c8dcSSimon Schubert printf_filtered ("static ");
37275796c8dcSSimon Schubert
3728c50c785cSJohn Marino /* Typedef that is not a C++ class. */
37295796c8dcSSimon Schubert if (kind == TYPES_DOMAIN
37305796c8dcSSimon Schubert && SYMBOL_DOMAIN (sym) != STRUCT_DOMAIN)
37315796c8dcSSimon Schubert typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
3732c50c785cSJohn Marino /* variable, func, or typedef-that-is-c++-class. */
3733*ef5ccd6cSJohn Marino else if (kind < TYPES_DOMAIN
3734*ef5ccd6cSJohn Marino || (kind == TYPES_DOMAIN
3735*ef5ccd6cSJohn Marino && SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN))
37365796c8dcSSimon Schubert {
37375796c8dcSSimon Schubert type_print (SYMBOL_TYPE (sym),
37385796c8dcSSimon Schubert (SYMBOL_CLASS (sym) == LOC_TYPEDEF
37395796c8dcSSimon Schubert ? "" : SYMBOL_PRINT_NAME (sym)),
37405796c8dcSSimon Schubert gdb_stdout, 0);
37415796c8dcSSimon Schubert
37425796c8dcSSimon Schubert printf_filtered (";\n");
37435796c8dcSSimon Schubert }
37445796c8dcSSimon Schubert }
37455796c8dcSSimon Schubert
37465796c8dcSSimon Schubert /* This help function for symtab_symbol_info() prints information
3747c50c785cSJohn Marino for non-debugging symbols to gdb_stdout. */
3748c50c785cSJohn Marino
37495796c8dcSSimon Schubert static void
print_msymbol_info(struct minimal_symbol * msymbol)37505796c8dcSSimon Schubert print_msymbol_info (struct minimal_symbol *msymbol)
37515796c8dcSSimon Schubert {
37525796c8dcSSimon Schubert struct gdbarch *gdbarch = get_objfile_arch (msymbol_objfile (msymbol));
37535796c8dcSSimon Schubert char *tmp;
37545796c8dcSSimon Schubert
37555796c8dcSSimon Schubert if (gdbarch_addr_bit (gdbarch) <= 32)
37565796c8dcSSimon Schubert tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol)
37575796c8dcSSimon Schubert & (CORE_ADDR) 0xffffffff,
37585796c8dcSSimon Schubert 8);
37595796c8dcSSimon Schubert else
37605796c8dcSSimon Schubert tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol),
37615796c8dcSSimon Schubert 16);
37625796c8dcSSimon Schubert printf_filtered ("%s %s\n",
37635796c8dcSSimon Schubert tmp, SYMBOL_PRINT_NAME (msymbol));
37645796c8dcSSimon Schubert }
37655796c8dcSSimon Schubert
37665796c8dcSSimon Schubert /* This is the guts of the commands "info functions", "info types", and
37675796c8dcSSimon Schubert "info variables". It calls search_symbols to find all matches and then
37685796c8dcSSimon Schubert print_[m]symbol_info to print out some useful information about the
3769c50c785cSJohn Marino matches. */
3770c50c785cSJohn Marino
37715796c8dcSSimon Schubert static void
symtab_symbol_info(char * regexp,enum search_domain kind,int from_tty)3772a45ae5f8SJohn Marino symtab_symbol_info (char *regexp, enum search_domain kind, int from_tty)
37735796c8dcSSimon Schubert {
3774c50c785cSJohn Marino static const char * const classnames[] =
3775a45ae5f8SJohn Marino {"variable", "function", "type"};
37765796c8dcSSimon Schubert struct symbol_search *symbols;
37775796c8dcSSimon Schubert struct symbol_search *p;
37785796c8dcSSimon Schubert struct cleanup *old_chain;
3779*ef5ccd6cSJohn Marino const char *last_filename = NULL;
37805796c8dcSSimon Schubert int first = 1;
37815796c8dcSSimon Schubert
3782a45ae5f8SJohn Marino gdb_assert (kind <= TYPES_DOMAIN);
3783a45ae5f8SJohn Marino
3784c50c785cSJohn Marino /* Must make sure that if we're interrupted, symbols gets freed. */
37855796c8dcSSimon Schubert search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
37865796c8dcSSimon Schubert old_chain = make_cleanup_free_search_symbols (symbols);
37875796c8dcSSimon Schubert
3788*ef5ccd6cSJohn Marino if (regexp != NULL)
3789*ef5ccd6cSJohn Marino printf_filtered (_("All %ss matching regular expression \"%s\":\n"),
3790a45ae5f8SJohn Marino classnames[kind], regexp);
3791*ef5ccd6cSJohn Marino else
3792*ef5ccd6cSJohn Marino printf_filtered (_("All defined %ss:\n"), classnames[kind]);
37935796c8dcSSimon Schubert
37945796c8dcSSimon Schubert for (p = symbols; p != NULL; p = p->next)
37955796c8dcSSimon Schubert {
37965796c8dcSSimon Schubert QUIT;
37975796c8dcSSimon Schubert
37985796c8dcSSimon Schubert if (p->msymbol != NULL)
37995796c8dcSSimon Schubert {
38005796c8dcSSimon Schubert if (first)
38015796c8dcSSimon Schubert {
3802*ef5ccd6cSJohn Marino printf_filtered (_("\nNon-debugging symbols:\n"));
38035796c8dcSSimon Schubert first = 0;
38045796c8dcSSimon Schubert }
38055796c8dcSSimon Schubert print_msymbol_info (p->msymbol);
38065796c8dcSSimon Schubert }
38075796c8dcSSimon Schubert else
38085796c8dcSSimon Schubert {
38095796c8dcSSimon Schubert print_symbol_info (kind,
38105796c8dcSSimon Schubert p->symtab,
38115796c8dcSSimon Schubert p->symbol,
38125796c8dcSSimon Schubert p->block,
38135796c8dcSSimon Schubert last_filename);
3814*ef5ccd6cSJohn Marino last_filename = symtab_to_filename_for_display (p->symtab);
38155796c8dcSSimon Schubert }
38165796c8dcSSimon Schubert }
38175796c8dcSSimon Schubert
38185796c8dcSSimon Schubert do_cleanups (old_chain);
38195796c8dcSSimon Schubert }
38205796c8dcSSimon Schubert
38215796c8dcSSimon Schubert static void
variables_info(char * regexp,int from_tty)38225796c8dcSSimon Schubert variables_info (char *regexp, int from_tty)
38235796c8dcSSimon Schubert {
38245796c8dcSSimon Schubert symtab_symbol_info (regexp, VARIABLES_DOMAIN, from_tty);
38255796c8dcSSimon Schubert }
38265796c8dcSSimon Schubert
38275796c8dcSSimon Schubert static void
functions_info(char * regexp,int from_tty)38285796c8dcSSimon Schubert functions_info (char *regexp, int from_tty)
38295796c8dcSSimon Schubert {
38305796c8dcSSimon Schubert symtab_symbol_info (regexp, FUNCTIONS_DOMAIN, from_tty);
38315796c8dcSSimon Schubert }
38325796c8dcSSimon Schubert
38335796c8dcSSimon Schubert
38345796c8dcSSimon Schubert static void
types_info(char * regexp,int from_tty)38355796c8dcSSimon Schubert types_info (char *regexp, int from_tty)
38365796c8dcSSimon Schubert {
38375796c8dcSSimon Schubert symtab_symbol_info (regexp, TYPES_DOMAIN, from_tty);
38385796c8dcSSimon Schubert }
38395796c8dcSSimon Schubert
38405796c8dcSSimon Schubert /* Breakpoint all functions matching regular expression. */
38415796c8dcSSimon Schubert
38425796c8dcSSimon Schubert void
rbreak_command_wrapper(char * regexp,int from_tty)38435796c8dcSSimon Schubert rbreak_command_wrapper (char *regexp, int from_tty)
38445796c8dcSSimon Schubert {
38455796c8dcSSimon Schubert rbreak_command (regexp, from_tty);
38465796c8dcSSimon Schubert }
38475796c8dcSSimon Schubert
3848cf7f2e2dSJohn Marino /* A cleanup function that calls end_rbreak_breakpoints. */
3849cf7f2e2dSJohn Marino
3850cf7f2e2dSJohn Marino static void
do_end_rbreak_breakpoints(void * ignore)3851cf7f2e2dSJohn Marino do_end_rbreak_breakpoints (void *ignore)
3852cf7f2e2dSJohn Marino {
3853cf7f2e2dSJohn Marino end_rbreak_breakpoints ();
3854cf7f2e2dSJohn Marino }
3855cf7f2e2dSJohn Marino
38565796c8dcSSimon Schubert static void
rbreak_command(char * regexp,int from_tty)38575796c8dcSSimon Schubert rbreak_command (char *regexp, int from_tty)
38585796c8dcSSimon Schubert {
38595796c8dcSSimon Schubert struct symbol_search *ss;
38605796c8dcSSimon Schubert struct symbol_search *p;
38615796c8dcSSimon Schubert struct cleanup *old_chain;
3862cf7f2e2dSJohn Marino char *string = NULL;
3863cf7f2e2dSJohn Marino int len = 0;
3864c50c785cSJohn Marino char **files = NULL, *file_name;
3865cf7f2e2dSJohn Marino int nfiles = 0;
38665796c8dcSSimon Schubert
3867cf7f2e2dSJohn Marino if (regexp)
3868cf7f2e2dSJohn Marino {
3869cf7f2e2dSJohn Marino char *colon = strchr (regexp, ':');
3870cf7f2e2dSJohn Marino
3871cf7f2e2dSJohn Marino if (colon && *(colon + 1) != ':')
3872cf7f2e2dSJohn Marino {
3873cf7f2e2dSJohn Marino int colon_index;
3874cf7f2e2dSJohn Marino
3875cf7f2e2dSJohn Marino colon_index = colon - regexp;
3876cf7f2e2dSJohn Marino file_name = alloca (colon_index + 1);
3877cf7f2e2dSJohn Marino memcpy (file_name, regexp, colon_index);
3878cf7f2e2dSJohn Marino file_name[colon_index--] = 0;
3879cf7f2e2dSJohn Marino while (isspace (file_name[colon_index]))
3880cf7f2e2dSJohn Marino file_name[colon_index--] = 0;
3881cf7f2e2dSJohn Marino files = &file_name;
3882cf7f2e2dSJohn Marino nfiles = 1;
3883*ef5ccd6cSJohn Marino regexp = skip_spaces (colon + 1);
3884cf7f2e2dSJohn Marino }
3885cf7f2e2dSJohn Marino }
3886cf7f2e2dSJohn Marino
3887cf7f2e2dSJohn Marino search_symbols (regexp, FUNCTIONS_DOMAIN, nfiles, files, &ss);
38885796c8dcSSimon Schubert old_chain = make_cleanup_free_search_symbols (ss);
3889cf7f2e2dSJohn Marino make_cleanup (free_current_contents, &string);
38905796c8dcSSimon Schubert
3891cf7f2e2dSJohn Marino start_rbreak_breakpoints ();
3892cf7f2e2dSJohn Marino make_cleanup (do_end_rbreak_breakpoints, NULL);
38935796c8dcSSimon Schubert for (p = ss; p != NULL; p = p->next)
38945796c8dcSSimon Schubert {
38955796c8dcSSimon Schubert if (p->msymbol == NULL)
38965796c8dcSSimon Schubert {
3897*ef5ccd6cSJohn Marino const char *fullname = symtab_to_fullname (p->symtab);
3898*ef5ccd6cSJohn Marino
3899*ef5ccd6cSJohn Marino int newlen = (strlen (fullname)
39005796c8dcSSimon Schubert + strlen (SYMBOL_LINKAGE_NAME (p->symbol))
39015796c8dcSSimon Schubert + 4);
3902cf7f2e2dSJohn Marino
3903cf7f2e2dSJohn Marino if (newlen > len)
3904cf7f2e2dSJohn Marino {
3905cf7f2e2dSJohn Marino string = xrealloc (string, newlen);
3906cf7f2e2dSJohn Marino len = newlen;
3907cf7f2e2dSJohn Marino }
3908*ef5ccd6cSJohn Marino strcpy (string, fullname);
39095796c8dcSSimon Schubert strcat (string, ":'");
39105796c8dcSSimon Schubert strcat (string, SYMBOL_LINKAGE_NAME (p->symbol));
39115796c8dcSSimon Schubert strcat (string, "'");
39125796c8dcSSimon Schubert break_command (string, from_tty);
39135796c8dcSSimon Schubert print_symbol_info (FUNCTIONS_DOMAIN,
39145796c8dcSSimon Schubert p->symtab,
39155796c8dcSSimon Schubert p->symbol,
39165796c8dcSSimon Schubert p->block,
3917*ef5ccd6cSJohn Marino symtab_to_filename_for_display (p->symtab));
39185796c8dcSSimon Schubert }
39195796c8dcSSimon Schubert else
39205796c8dcSSimon Schubert {
3921cf7f2e2dSJohn Marino int newlen = (strlen (SYMBOL_LINKAGE_NAME (p->msymbol)) + 3);
3922cf7f2e2dSJohn Marino
3923cf7f2e2dSJohn Marino if (newlen > len)
3924cf7f2e2dSJohn Marino {
3925cf7f2e2dSJohn Marino string = xrealloc (string, newlen);
3926cf7f2e2dSJohn Marino len = newlen;
3927cf7f2e2dSJohn Marino }
39285796c8dcSSimon Schubert strcpy (string, "'");
39295796c8dcSSimon Schubert strcat (string, SYMBOL_LINKAGE_NAME (p->msymbol));
39305796c8dcSSimon Schubert strcat (string, "'");
39315796c8dcSSimon Schubert
39325796c8dcSSimon Schubert break_command (string, from_tty);
39335796c8dcSSimon Schubert printf_filtered ("<function, no debug info> %s;\n",
39345796c8dcSSimon Schubert SYMBOL_PRINT_NAME (p->msymbol));
39355796c8dcSSimon Schubert }
39365796c8dcSSimon Schubert }
39375796c8dcSSimon Schubert
39385796c8dcSSimon Schubert do_cleanups (old_chain);
39395796c8dcSSimon Schubert }
39405796c8dcSSimon Schubert
39415796c8dcSSimon Schubert
3942c50c785cSJohn Marino /* Evaluate if NAME matches SYM_TEXT and SYM_TEXT_LEN.
3943c50c785cSJohn Marino
3944c50c785cSJohn Marino Either sym_text[sym_text_len] != '(' and then we search for any
3945c50c785cSJohn Marino symbol starting with SYM_TEXT text.
3946c50c785cSJohn Marino
3947c50c785cSJohn Marino Otherwise sym_text[sym_text_len] == '(' and then we require symbol name to
3948c50c785cSJohn Marino be terminated at that point. Partial symbol tables do not have parameters
3949c50c785cSJohn Marino information. */
3950c50c785cSJohn Marino
3951c50c785cSJohn Marino static int
compare_symbol_name(const char * name,const char * sym_text,int sym_text_len)3952c50c785cSJohn Marino compare_symbol_name (const char *name, const char *sym_text, int sym_text_len)
3953c50c785cSJohn Marino {
3954a45ae5f8SJohn Marino int (*ncmp) (const char *, const char *, size_t);
3955a45ae5f8SJohn Marino
3956a45ae5f8SJohn Marino ncmp = (case_sensitivity == case_sensitive_on ? strncmp : strncasecmp);
3957a45ae5f8SJohn Marino
3958a45ae5f8SJohn Marino if (ncmp (name, sym_text, sym_text_len) != 0)
3959c50c785cSJohn Marino return 0;
3960c50c785cSJohn Marino
3961c50c785cSJohn Marino if (sym_text[sym_text_len] == '(')
3962c50c785cSJohn Marino {
3963c50c785cSJohn Marino /* User searches for `name(someth...'. Require NAME to be terminated.
3964c50c785cSJohn Marino Normally psymtabs and gdbindex have no parameter types so '\0' will be
3965c50c785cSJohn Marino present but accept even parameters presence. In this case this
3966c50c785cSJohn Marino function is in fact strcmp_iw but whitespace skipping is not supported
3967c50c785cSJohn Marino for tab completion. */
3968c50c785cSJohn Marino
3969c50c785cSJohn Marino if (name[sym_text_len] != '\0' && name[sym_text_len] != '(')
3970c50c785cSJohn Marino return 0;
3971c50c785cSJohn Marino }
3972c50c785cSJohn Marino
3973c50c785cSJohn Marino return 1;
3974c50c785cSJohn Marino }
3975c50c785cSJohn Marino
3976a45ae5f8SJohn Marino /* Free any memory associated with a completion list. */
3977a45ae5f8SJohn Marino
3978a45ae5f8SJohn Marino static void
free_completion_list(VEC (char_ptr)** list_ptr)3979*ef5ccd6cSJohn Marino free_completion_list (VEC (char_ptr) **list_ptr)
3980a45ae5f8SJohn Marino {
3981*ef5ccd6cSJohn Marino int i;
3982*ef5ccd6cSJohn Marino char *p;
3983a45ae5f8SJohn Marino
3984*ef5ccd6cSJohn Marino for (i = 0; VEC_iterate (char_ptr, *list_ptr, i, p); ++i)
3985*ef5ccd6cSJohn Marino xfree (p);
3986*ef5ccd6cSJohn Marino VEC_free (char_ptr, *list_ptr);
3987a45ae5f8SJohn Marino }
3988a45ae5f8SJohn Marino
3989a45ae5f8SJohn Marino /* Callback for make_cleanup. */
3990a45ae5f8SJohn Marino
3991a45ae5f8SJohn Marino static void
do_free_completion_list(void * list)3992a45ae5f8SJohn Marino do_free_completion_list (void *list)
3993a45ae5f8SJohn Marino {
3994a45ae5f8SJohn Marino free_completion_list (list);
3995a45ae5f8SJohn Marino }
3996a45ae5f8SJohn Marino
39975796c8dcSSimon Schubert /* Helper routine for make_symbol_completion_list. */
39985796c8dcSSimon Schubert
VEC(char_ptr)3999*ef5ccd6cSJohn Marino static VEC (char_ptr) *return_val;
40005796c8dcSSimon Schubert
40015796c8dcSSimon Schubert #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
40025796c8dcSSimon Schubert completion_list_add_name \
40035796c8dcSSimon Schubert (SYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))
40045796c8dcSSimon Schubert
40055796c8dcSSimon Schubert /* Test to see if the symbol specified by SYMNAME (which is already
40065796c8dcSSimon Schubert demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
40075796c8dcSSimon Schubert characters. If so, add it to the current completion list. */
40085796c8dcSSimon Schubert
40095796c8dcSSimon Schubert static void
4010*ef5ccd6cSJohn Marino completion_list_add_name (const char *symname,
4011*ef5ccd6cSJohn Marino const char *sym_text, int sym_text_len,
4012*ef5ccd6cSJohn Marino const char *text, const char *word)
40135796c8dcSSimon Schubert {
4014c50c785cSJohn Marino /* Clip symbols that cannot match. */
4015c50c785cSJohn Marino if (!compare_symbol_name (symname, sym_text, sym_text_len))
40165796c8dcSSimon Schubert return;
40175796c8dcSSimon Schubert
40185796c8dcSSimon Schubert /* We have a match for a completion, so add SYMNAME to the current list
40195796c8dcSSimon Schubert of matches. Note that the name is moved to freshly malloc'd space. */
40205796c8dcSSimon Schubert
40215796c8dcSSimon Schubert {
40225796c8dcSSimon Schubert char *new;
4023cf7f2e2dSJohn Marino
40245796c8dcSSimon Schubert if (word == sym_text)
40255796c8dcSSimon Schubert {
40265796c8dcSSimon Schubert new = xmalloc (strlen (symname) + 5);
40275796c8dcSSimon Schubert strcpy (new, symname);
40285796c8dcSSimon Schubert }
40295796c8dcSSimon Schubert else if (word > sym_text)
40305796c8dcSSimon Schubert {
40315796c8dcSSimon Schubert /* Return some portion of symname. */
40325796c8dcSSimon Schubert new = xmalloc (strlen (symname) + 5);
40335796c8dcSSimon Schubert strcpy (new, symname + (word - sym_text));
40345796c8dcSSimon Schubert }
40355796c8dcSSimon Schubert else
40365796c8dcSSimon Schubert {
40375796c8dcSSimon Schubert /* Return some of SYM_TEXT plus symname. */
40385796c8dcSSimon Schubert new = xmalloc (strlen (symname) + (sym_text - word) + 5);
40395796c8dcSSimon Schubert strncpy (new, word, sym_text - word);
40405796c8dcSSimon Schubert new[sym_text - word] = '\0';
40415796c8dcSSimon Schubert strcat (new, symname);
40425796c8dcSSimon Schubert }
40435796c8dcSSimon Schubert
4044*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, return_val, new);
40455796c8dcSSimon Schubert }
40465796c8dcSSimon Schubert }
40475796c8dcSSimon Schubert
40485796c8dcSSimon Schubert /* ObjC: In case we are completing on a selector, look as the msymbol
40495796c8dcSSimon Schubert again and feed all the selectors into the mill. */
40505796c8dcSSimon Schubert
40515796c8dcSSimon Schubert static void
completion_list_objc_symbol(struct minimal_symbol * msymbol,const char * sym_text,int sym_text_len,const char * text,const char * word)4052*ef5ccd6cSJohn Marino completion_list_objc_symbol (struct minimal_symbol *msymbol,
4053*ef5ccd6cSJohn Marino const char *sym_text, int sym_text_len,
4054*ef5ccd6cSJohn Marino const char *text, const char *word)
40555796c8dcSSimon Schubert {
40565796c8dcSSimon Schubert static char *tmp = NULL;
40575796c8dcSSimon Schubert static unsigned int tmplen = 0;
40585796c8dcSSimon Schubert
4059*ef5ccd6cSJohn Marino const char *method, *category, *selector;
40605796c8dcSSimon Schubert char *tmp2 = NULL;
40615796c8dcSSimon Schubert
40625796c8dcSSimon Schubert method = SYMBOL_NATURAL_NAME (msymbol);
40635796c8dcSSimon Schubert
40645796c8dcSSimon Schubert /* Is it a method? */
40655796c8dcSSimon Schubert if ((method[0] != '-') && (method[0] != '+'))
40665796c8dcSSimon Schubert return;
40675796c8dcSSimon Schubert
40685796c8dcSSimon Schubert if (sym_text[0] == '[')
40695796c8dcSSimon Schubert /* Complete on shortened method method. */
40705796c8dcSSimon Schubert completion_list_add_name (method + 1, sym_text, sym_text_len, text, word);
40715796c8dcSSimon Schubert
40725796c8dcSSimon Schubert while ((strlen (method) + 1) >= tmplen)
40735796c8dcSSimon Schubert {
40745796c8dcSSimon Schubert if (tmplen == 0)
40755796c8dcSSimon Schubert tmplen = 1024;
40765796c8dcSSimon Schubert else
40775796c8dcSSimon Schubert tmplen *= 2;
40785796c8dcSSimon Schubert tmp = xrealloc (tmp, tmplen);
40795796c8dcSSimon Schubert }
40805796c8dcSSimon Schubert selector = strchr (method, ' ');
40815796c8dcSSimon Schubert if (selector != NULL)
40825796c8dcSSimon Schubert selector++;
40835796c8dcSSimon Schubert
40845796c8dcSSimon Schubert category = strchr (method, '(');
40855796c8dcSSimon Schubert
40865796c8dcSSimon Schubert if ((category != NULL) && (selector != NULL))
40875796c8dcSSimon Schubert {
40885796c8dcSSimon Schubert memcpy (tmp, method, (category - method));
40895796c8dcSSimon Schubert tmp[category - method] = ' ';
40905796c8dcSSimon Schubert memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
40915796c8dcSSimon Schubert completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
40925796c8dcSSimon Schubert if (sym_text[0] == '[')
40935796c8dcSSimon Schubert completion_list_add_name (tmp + 1, sym_text, sym_text_len, text, word);
40945796c8dcSSimon Schubert }
40955796c8dcSSimon Schubert
40965796c8dcSSimon Schubert if (selector != NULL)
40975796c8dcSSimon Schubert {
40985796c8dcSSimon Schubert /* Complete on selector only. */
40995796c8dcSSimon Schubert strcpy (tmp, selector);
41005796c8dcSSimon Schubert tmp2 = strchr (tmp, ']');
41015796c8dcSSimon Schubert if (tmp2 != NULL)
41025796c8dcSSimon Schubert *tmp2 = '\0';
41035796c8dcSSimon Schubert
41045796c8dcSSimon Schubert completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
41055796c8dcSSimon Schubert }
41065796c8dcSSimon Schubert }
41075796c8dcSSimon Schubert
41085796c8dcSSimon Schubert /* Break the non-quoted text based on the characters which are in
41095796c8dcSSimon Schubert symbols. FIXME: This should probably be language-specific. */
41105796c8dcSSimon Schubert
41115796c8dcSSimon Schubert static char *
language_search_unquoted_string(char * text,char * p)41125796c8dcSSimon Schubert language_search_unquoted_string (char *text, char *p)
41135796c8dcSSimon Schubert {
41145796c8dcSSimon Schubert for (; p > text; --p)
41155796c8dcSSimon Schubert {
41165796c8dcSSimon Schubert if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
41175796c8dcSSimon Schubert continue;
41185796c8dcSSimon Schubert else
41195796c8dcSSimon Schubert {
41205796c8dcSSimon Schubert if ((current_language->la_language == language_objc))
41215796c8dcSSimon Schubert {
4122c50c785cSJohn Marino if (p[-1] == ':') /* Might be part of a method name. */
41235796c8dcSSimon Schubert continue;
41245796c8dcSSimon Schubert else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
4125c50c785cSJohn Marino p -= 2; /* Beginning of a method name. */
41265796c8dcSSimon Schubert else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
4127c50c785cSJohn Marino { /* Might be part of a method name. */
41285796c8dcSSimon Schubert char *t = p;
41295796c8dcSSimon Schubert
41305796c8dcSSimon Schubert /* Seeing a ' ' or a '(' is not conclusive evidence
41315796c8dcSSimon Schubert that we are in the middle of a method name. However,
41325796c8dcSSimon Schubert finding "-[" or "+[" should be pretty un-ambiguous.
41335796c8dcSSimon Schubert Unfortunately we have to find it now to decide. */
41345796c8dcSSimon Schubert
41355796c8dcSSimon Schubert while (t > text)
41365796c8dcSSimon Schubert if (isalnum (t[-1]) || t[-1] == '_' ||
41375796c8dcSSimon Schubert t[-1] == ' ' || t[-1] == ':' ||
41385796c8dcSSimon Schubert t[-1] == '(' || t[-1] == ')')
41395796c8dcSSimon Schubert --t;
41405796c8dcSSimon Schubert else
41415796c8dcSSimon Schubert break;
41425796c8dcSSimon Schubert
41435796c8dcSSimon Schubert if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
4144c50c785cSJohn Marino p = t - 2; /* Method name detected. */
4145c50c785cSJohn Marino /* Else we leave with p unchanged. */
41465796c8dcSSimon Schubert }
41475796c8dcSSimon Schubert }
41485796c8dcSSimon Schubert break;
41495796c8dcSSimon Schubert }
41505796c8dcSSimon Schubert }
41515796c8dcSSimon Schubert return p;
41525796c8dcSSimon Schubert }
41535796c8dcSSimon Schubert
41545796c8dcSSimon Schubert static void
completion_list_add_fields(struct symbol * sym,char * sym_text,int sym_text_len,char * text,char * word)41555796c8dcSSimon Schubert completion_list_add_fields (struct symbol *sym, char *sym_text,
41565796c8dcSSimon Schubert int sym_text_len, char *text, char *word)
41575796c8dcSSimon Schubert {
41585796c8dcSSimon Schubert if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
41595796c8dcSSimon Schubert {
41605796c8dcSSimon Schubert struct type *t = SYMBOL_TYPE (sym);
41615796c8dcSSimon Schubert enum type_code c = TYPE_CODE (t);
41625796c8dcSSimon Schubert int j;
41635796c8dcSSimon Schubert
41645796c8dcSSimon Schubert if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
41655796c8dcSSimon Schubert for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
41665796c8dcSSimon Schubert if (TYPE_FIELD_NAME (t, j))
41675796c8dcSSimon Schubert completion_list_add_name (TYPE_FIELD_NAME (t, j),
41685796c8dcSSimon Schubert sym_text, sym_text_len, text, word);
41695796c8dcSSimon Schubert }
41705796c8dcSSimon Schubert }
41715796c8dcSSimon Schubert
4172cf7f2e2dSJohn Marino /* Type of the user_data argument passed to add_macro_name or
4173c50c785cSJohn Marino expand_partial_symbol_name. The contents are simply whatever is
4174cf7f2e2dSJohn Marino needed by completion_list_add_name. */
4175cf7f2e2dSJohn Marino struct add_name_data
41765796c8dcSSimon Schubert {
41775796c8dcSSimon Schubert char *sym_text;
41785796c8dcSSimon Schubert int sym_text_len;
41795796c8dcSSimon Schubert char *text;
41805796c8dcSSimon Schubert char *word;
41815796c8dcSSimon Schubert };
41825796c8dcSSimon Schubert
41835796c8dcSSimon Schubert /* A callback used with macro_for_each and macro_for_each_in_scope.
41845796c8dcSSimon Schubert This adds a macro's name to the current completion list. */
4185*ef5ccd6cSJohn Marino
41865796c8dcSSimon Schubert static void
add_macro_name(const char * name,const struct macro_definition * ignore,struct macro_source_file * ignore2,int ignore3,void * user_data)41875796c8dcSSimon Schubert add_macro_name (const char *name, const struct macro_definition *ignore,
4188a45ae5f8SJohn Marino struct macro_source_file *ignore2, int ignore3,
41895796c8dcSSimon Schubert void *user_data)
41905796c8dcSSimon Schubert {
4191cf7f2e2dSJohn Marino struct add_name_data *datum = (struct add_name_data *) user_data;
4192cf7f2e2dSJohn Marino
4193cf7f2e2dSJohn Marino completion_list_add_name ((char *) name,
4194cf7f2e2dSJohn Marino datum->sym_text, datum->sym_text_len,
4195cf7f2e2dSJohn Marino datum->text, datum->word);
4196cf7f2e2dSJohn Marino }
4197cf7f2e2dSJohn Marino
4198c50c785cSJohn Marino /* A callback for expand_partial_symbol_names. */
4199*ef5ccd6cSJohn Marino
4200c50c785cSJohn Marino static int
expand_partial_symbol_name(const char * name,void * user_data)4201*ef5ccd6cSJohn Marino expand_partial_symbol_name (const char *name, void *user_data)
4202cf7f2e2dSJohn Marino {
4203cf7f2e2dSJohn Marino struct add_name_data *datum = (struct add_name_data *) user_data;
4204cf7f2e2dSJohn Marino
4205c50c785cSJohn Marino return compare_symbol_name (name, datum->sym_text, datum->sym_text_len);
42065796c8dcSSimon Schubert }
42075796c8dcSSimon Schubert
VEC(char_ptr)4208*ef5ccd6cSJohn Marino VEC (char_ptr) *
4209cf7f2e2dSJohn Marino default_make_symbol_completion_list_break_on (char *text, char *word,
4210*ef5ccd6cSJohn Marino const char *break_on,
4211*ef5ccd6cSJohn Marino enum type_code code)
42125796c8dcSSimon Schubert {
42135796c8dcSSimon Schubert /* Problem: All of the symbols have to be copied because readline
42145796c8dcSSimon Schubert frees them. I'm not going to worry about this; hopefully there
42155796c8dcSSimon Schubert won't be that many. */
42165796c8dcSSimon Schubert
42175796c8dcSSimon Schubert struct symbol *sym;
42185796c8dcSSimon Schubert struct symtab *s;
42195796c8dcSSimon Schubert struct minimal_symbol *msymbol;
42205796c8dcSSimon Schubert struct objfile *objfile;
42215796c8dcSSimon Schubert struct block *b;
42225796c8dcSSimon Schubert const struct block *surrounding_static_block, *surrounding_global_block;
4223*ef5ccd6cSJohn Marino struct block_iterator iter;
42245796c8dcSSimon Schubert /* The symbol we are completing on. Points in same buffer as text. */
42255796c8dcSSimon Schubert char *sym_text;
42265796c8dcSSimon Schubert /* Length of sym_text. */
42275796c8dcSSimon Schubert int sym_text_len;
4228cf7f2e2dSJohn Marino struct add_name_data datum;
4229a45ae5f8SJohn Marino struct cleanup *back_to;
42305796c8dcSSimon Schubert
42315796c8dcSSimon Schubert /* Now look for the symbol we are supposed to complete on. */
42325796c8dcSSimon Schubert {
42335796c8dcSSimon Schubert char *p;
42345796c8dcSSimon Schubert char quote_found;
42355796c8dcSSimon Schubert char *quote_pos = NULL;
42365796c8dcSSimon Schubert
42375796c8dcSSimon Schubert /* First see if this is a quoted string. */
42385796c8dcSSimon Schubert quote_found = '\0';
42395796c8dcSSimon Schubert for (p = text; *p != '\0'; ++p)
42405796c8dcSSimon Schubert {
42415796c8dcSSimon Schubert if (quote_found != '\0')
42425796c8dcSSimon Schubert {
42435796c8dcSSimon Schubert if (*p == quote_found)
42445796c8dcSSimon Schubert /* Found close quote. */
42455796c8dcSSimon Schubert quote_found = '\0';
42465796c8dcSSimon Schubert else if (*p == '\\' && p[1] == quote_found)
42475796c8dcSSimon Schubert /* A backslash followed by the quote character
42485796c8dcSSimon Schubert doesn't end the string. */
42495796c8dcSSimon Schubert ++p;
42505796c8dcSSimon Schubert }
42515796c8dcSSimon Schubert else if (*p == '\'' || *p == '"')
42525796c8dcSSimon Schubert {
42535796c8dcSSimon Schubert quote_found = *p;
42545796c8dcSSimon Schubert quote_pos = p;
42555796c8dcSSimon Schubert }
42565796c8dcSSimon Schubert }
42575796c8dcSSimon Schubert if (quote_found == '\'')
42585796c8dcSSimon Schubert /* A string within single quotes can be a symbol, so complete on it. */
42595796c8dcSSimon Schubert sym_text = quote_pos + 1;
42605796c8dcSSimon Schubert else if (quote_found == '"')
42615796c8dcSSimon Schubert /* A double-quoted string is never a symbol, nor does it make sense
42625796c8dcSSimon Schubert to complete it any other way. */
42635796c8dcSSimon Schubert {
4264*ef5ccd6cSJohn Marino return NULL;
42655796c8dcSSimon Schubert }
42665796c8dcSSimon Schubert else
42675796c8dcSSimon Schubert {
42685796c8dcSSimon Schubert /* It is not a quoted string. Break it based on the characters
42695796c8dcSSimon Schubert which are in symbols. */
42705796c8dcSSimon Schubert while (p > text)
42715796c8dcSSimon Schubert {
42725796c8dcSSimon Schubert if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0'
4273cf7f2e2dSJohn Marino || p[-1] == ':' || strchr (break_on, p[-1]) != NULL)
42745796c8dcSSimon Schubert --p;
42755796c8dcSSimon Schubert else
42765796c8dcSSimon Schubert break;
42775796c8dcSSimon Schubert }
42785796c8dcSSimon Schubert sym_text = p;
42795796c8dcSSimon Schubert }
42805796c8dcSSimon Schubert }
42815796c8dcSSimon Schubert
42825796c8dcSSimon Schubert sym_text_len = strlen (sym_text);
42835796c8dcSSimon Schubert
4284c50c785cSJohn Marino /* Prepare SYM_TEXT_LEN for compare_symbol_name. */
4285c50c785cSJohn Marino
4286c50c785cSJohn Marino if (current_language->la_language == language_cplus
4287c50c785cSJohn Marino || current_language->la_language == language_java
4288c50c785cSJohn Marino || current_language->la_language == language_fortran)
4289c50c785cSJohn Marino {
4290c50c785cSJohn Marino /* These languages may have parameters entered by user but they are never
4291c50c785cSJohn Marino present in the partial symbol tables. */
4292c50c785cSJohn Marino
4293c50c785cSJohn Marino const char *cs = memchr (sym_text, '(', sym_text_len);
4294c50c785cSJohn Marino
4295c50c785cSJohn Marino if (cs)
4296c50c785cSJohn Marino sym_text_len = cs - sym_text;
4297c50c785cSJohn Marino }
4298c50c785cSJohn Marino gdb_assert (sym_text[sym_text_len] == '\0' || sym_text[sym_text_len] == '(');
4299c50c785cSJohn Marino
4300*ef5ccd6cSJohn Marino return_val = NULL;
4301a45ae5f8SJohn Marino back_to = make_cleanup (do_free_completion_list, &return_val);
43025796c8dcSSimon Schubert
4303cf7f2e2dSJohn Marino datum.sym_text = sym_text;
4304cf7f2e2dSJohn Marino datum.sym_text_len = sym_text_len;
4305cf7f2e2dSJohn Marino datum.text = text;
4306cf7f2e2dSJohn Marino datum.word = word;
4307cf7f2e2dSJohn Marino
43085796c8dcSSimon Schubert /* Look through the partial symtabs for all symbols which begin
4309c50c785cSJohn Marino by matching SYM_TEXT. Expand all CUs that you find to the list.
4310c50c785cSJohn Marino The real names will get added by COMPLETION_LIST_ADD_SYMBOL below. */
4311c50c785cSJohn Marino expand_partial_symbol_names (expand_partial_symbol_name, &datum);
43125796c8dcSSimon Schubert
43135796c8dcSSimon Schubert /* At this point scan through the misc symbol vectors and add each
43145796c8dcSSimon Schubert symbol you find to the list. Eventually we want to ignore
43155796c8dcSSimon Schubert anything that isn't a text symbol (everything else will be
43165796c8dcSSimon Schubert handled by the psymtab code above). */
43175796c8dcSSimon Schubert
4318*ef5ccd6cSJohn Marino if (code == TYPE_CODE_UNDEF)
4319*ef5ccd6cSJohn Marino {
43205796c8dcSSimon Schubert ALL_MSYMBOLS (objfile, msymbol)
43215796c8dcSSimon Schubert {
43225796c8dcSSimon Schubert QUIT;
4323*ef5ccd6cSJohn Marino COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text,
4324*ef5ccd6cSJohn Marino word);
43255796c8dcSSimon Schubert
4326*ef5ccd6cSJohn Marino completion_list_objc_symbol (msymbol, sym_text, sym_text_len, text,
4327*ef5ccd6cSJohn Marino word);
4328*ef5ccd6cSJohn Marino }
43295796c8dcSSimon Schubert }
43305796c8dcSSimon Schubert
43315796c8dcSSimon Schubert /* Search upwards from currently selected frame (so that we can
43325796c8dcSSimon Schubert complete on local vars). Also catch fields of types defined in
43335796c8dcSSimon Schubert this places which match our text string. Only complete on types
43345796c8dcSSimon Schubert visible from current context. */
43355796c8dcSSimon Schubert
43365796c8dcSSimon Schubert b = get_selected_block (0);
43375796c8dcSSimon Schubert surrounding_static_block = block_static_block (b);
43385796c8dcSSimon Schubert surrounding_global_block = block_global_block (b);
43395796c8dcSSimon Schubert if (surrounding_static_block != NULL)
43405796c8dcSSimon Schubert while (b != surrounding_static_block)
43415796c8dcSSimon Schubert {
43425796c8dcSSimon Schubert QUIT;
43435796c8dcSSimon Schubert
43445796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (b, iter, sym)
43455796c8dcSSimon Schubert {
4346*ef5ccd6cSJohn Marino if (code == TYPE_CODE_UNDEF)
4347*ef5ccd6cSJohn Marino {
43485796c8dcSSimon Schubert COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
43495796c8dcSSimon Schubert word);
43505796c8dcSSimon Schubert completion_list_add_fields (sym, sym_text, sym_text_len, text,
43515796c8dcSSimon Schubert word);
43525796c8dcSSimon Schubert }
4353*ef5ccd6cSJohn Marino else if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
4354*ef5ccd6cSJohn Marino && TYPE_CODE (SYMBOL_TYPE (sym)) == code)
4355*ef5ccd6cSJohn Marino COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
4356*ef5ccd6cSJohn Marino word);
4357*ef5ccd6cSJohn Marino }
43585796c8dcSSimon Schubert
43595796c8dcSSimon Schubert /* Stop when we encounter an enclosing function. Do not stop for
43605796c8dcSSimon Schubert non-inlined functions - the locals of the enclosing function
43615796c8dcSSimon Schubert are in scope for a nested function. */
43625796c8dcSSimon Schubert if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
43635796c8dcSSimon Schubert break;
43645796c8dcSSimon Schubert b = BLOCK_SUPERBLOCK (b);
43655796c8dcSSimon Schubert }
43665796c8dcSSimon Schubert
43675796c8dcSSimon Schubert /* Add fields from the file's types; symbols will be added below. */
43685796c8dcSSimon Schubert
4369*ef5ccd6cSJohn Marino if (code == TYPE_CODE_UNDEF)
4370*ef5ccd6cSJohn Marino {
43715796c8dcSSimon Schubert if (surrounding_static_block != NULL)
43725796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (surrounding_static_block, iter, sym)
43735796c8dcSSimon Schubert completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
43745796c8dcSSimon Schubert
43755796c8dcSSimon Schubert if (surrounding_global_block != NULL)
43765796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (surrounding_global_block, iter, sym)
43775796c8dcSSimon Schubert completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
4378*ef5ccd6cSJohn Marino }
43795796c8dcSSimon Schubert
43805796c8dcSSimon Schubert /* Go through the symtabs and check the externs and statics for
43815796c8dcSSimon Schubert symbols which match. */
43825796c8dcSSimon Schubert
43835796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s)
43845796c8dcSSimon Schubert {
43855796c8dcSSimon Schubert QUIT;
43865796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
43875796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (b, iter, sym)
43885796c8dcSSimon Schubert {
4389*ef5ccd6cSJohn Marino if (code == TYPE_CODE_UNDEF
4390*ef5ccd6cSJohn Marino || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
4391*ef5ccd6cSJohn Marino && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
43925796c8dcSSimon Schubert COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
43935796c8dcSSimon Schubert }
43945796c8dcSSimon Schubert }
43955796c8dcSSimon Schubert
43965796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s)
43975796c8dcSSimon Schubert {
43985796c8dcSSimon Schubert QUIT;
43995796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
44005796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (b, iter, sym)
44015796c8dcSSimon Schubert {
4402*ef5ccd6cSJohn Marino if (code == TYPE_CODE_UNDEF
4403*ef5ccd6cSJohn Marino || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
4404*ef5ccd6cSJohn Marino && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
44055796c8dcSSimon Schubert COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
44065796c8dcSSimon Schubert }
44075796c8dcSSimon Schubert }
44085796c8dcSSimon Schubert
4409*ef5ccd6cSJohn Marino /* Skip macros if we are completing a struct tag -- arguable but
4410*ef5ccd6cSJohn Marino usually what is expected. */
4411*ef5ccd6cSJohn Marino if (current_language->la_macro_expansion == macro_expansion_c
4412*ef5ccd6cSJohn Marino && code == TYPE_CODE_UNDEF)
44135796c8dcSSimon Schubert {
44145796c8dcSSimon Schubert struct macro_scope *scope;
44155796c8dcSSimon Schubert
44165796c8dcSSimon Schubert /* Add any macros visible in the default scope. Note that this
44175796c8dcSSimon Schubert may yield the occasional wrong result, because an expression
44185796c8dcSSimon Schubert might be evaluated in a scope other than the default. For
44195796c8dcSSimon Schubert example, if the user types "break file:line if <TAB>", the
44205796c8dcSSimon Schubert resulting expression will be evaluated at "file:line" -- but
44215796c8dcSSimon Schubert at there does not seem to be a way to detect this at
44225796c8dcSSimon Schubert completion time. */
44235796c8dcSSimon Schubert scope = default_macro_scope ();
44245796c8dcSSimon Schubert if (scope)
44255796c8dcSSimon Schubert {
44265796c8dcSSimon Schubert macro_for_each_in_scope (scope->file, scope->line,
44275796c8dcSSimon Schubert add_macro_name, &datum);
44285796c8dcSSimon Schubert xfree (scope);
44295796c8dcSSimon Schubert }
44305796c8dcSSimon Schubert
44315796c8dcSSimon Schubert /* User-defined macros are always visible. */
44325796c8dcSSimon Schubert macro_for_each (macro_user_macros, add_macro_name, &datum);
44335796c8dcSSimon Schubert }
44345796c8dcSSimon Schubert
4435a45ae5f8SJohn Marino discard_cleanups (back_to);
44365796c8dcSSimon Schubert return (return_val);
44375796c8dcSSimon Schubert }
44385796c8dcSSimon Schubert
VEC(char_ptr)4439*ef5ccd6cSJohn Marino VEC (char_ptr) *
4440*ef5ccd6cSJohn Marino default_make_symbol_completion_list (char *text, char *word,
4441*ef5ccd6cSJohn Marino enum type_code code)
4442cf7f2e2dSJohn Marino {
4443*ef5ccd6cSJohn Marino return default_make_symbol_completion_list_break_on (text, word, "", code);
4444cf7f2e2dSJohn Marino }
4445cf7f2e2dSJohn Marino
4446*ef5ccd6cSJohn Marino /* Return a vector of all symbols (regardless of class) which begin by
4447*ef5ccd6cSJohn Marino matching TEXT. If the answer is no symbols, then the return value
4448*ef5ccd6cSJohn Marino is NULL. */
44495796c8dcSSimon Schubert
VEC(char_ptr)4450*ef5ccd6cSJohn Marino VEC (char_ptr) *
44515796c8dcSSimon Schubert make_symbol_completion_list (char *text, char *word)
44525796c8dcSSimon Schubert {
4453*ef5ccd6cSJohn Marino return current_language->la_make_symbol_completion_list (text, word,
4454*ef5ccd6cSJohn Marino TYPE_CODE_UNDEF);
4455*ef5ccd6cSJohn Marino }
4456*ef5ccd6cSJohn Marino
4457*ef5ccd6cSJohn Marino /* Like make_symbol_completion_list, but only return STRUCT_DOMAIN
4458*ef5ccd6cSJohn Marino symbols whose type code is CODE. */
4459*ef5ccd6cSJohn Marino
VEC(char_ptr)4460*ef5ccd6cSJohn Marino VEC (char_ptr) *
4461*ef5ccd6cSJohn Marino make_symbol_completion_type (char *text, char *word, enum type_code code)
4462*ef5ccd6cSJohn Marino {
4463*ef5ccd6cSJohn Marino gdb_assert (code == TYPE_CODE_UNION
4464*ef5ccd6cSJohn Marino || code == TYPE_CODE_STRUCT
4465*ef5ccd6cSJohn Marino || code == TYPE_CODE_CLASS
4466*ef5ccd6cSJohn Marino || code == TYPE_CODE_ENUM);
4467*ef5ccd6cSJohn Marino return current_language->la_make_symbol_completion_list (text, word, code);
44685796c8dcSSimon Schubert }
44695796c8dcSSimon Schubert
44705796c8dcSSimon Schubert /* Like make_symbol_completion_list, but suitable for use as a
44715796c8dcSSimon Schubert completion function. */
44725796c8dcSSimon Schubert
VEC(char_ptr)4473*ef5ccd6cSJohn Marino VEC (char_ptr) *
44745796c8dcSSimon Schubert make_symbol_completion_list_fn (struct cmd_list_element *ignore,
44755796c8dcSSimon Schubert char *text, char *word)
44765796c8dcSSimon Schubert {
44775796c8dcSSimon Schubert return make_symbol_completion_list (text, word);
44785796c8dcSSimon Schubert }
44795796c8dcSSimon Schubert
44805796c8dcSSimon Schubert /* Like make_symbol_completion_list, but returns a list of symbols
44815796c8dcSSimon Schubert defined in a source file FILE. */
44825796c8dcSSimon Schubert
VEC(char_ptr)4483*ef5ccd6cSJohn Marino VEC (char_ptr) *
44845796c8dcSSimon Schubert make_file_symbol_completion_list (char *text, char *word, char *srcfile)
44855796c8dcSSimon Schubert {
44865796c8dcSSimon Schubert struct symbol *sym;
44875796c8dcSSimon Schubert struct symtab *s;
44885796c8dcSSimon Schubert struct block *b;
4489*ef5ccd6cSJohn Marino struct block_iterator iter;
44905796c8dcSSimon Schubert /* The symbol we are completing on. Points in same buffer as text. */
44915796c8dcSSimon Schubert char *sym_text;
44925796c8dcSSimon Schubert /* Length of sym_text. */
44935796c8dcSSimon Schubert int sym_text_len;
44945796c8dcSSimon Schubert
44955796c8dcSSimon Schubert /* Now look for the symbol we are supposed to complete on.
44965796c8dcSSimon Schubert FIXME: This should be language-specific. */
44975796c8dcSSimon Schubert {
44985796c8dcSSimon Schubert char *p;
44995796c8dcSSimon Schubert char quote_found;
45005796c8dcSSimon Schubert char *quote_pos = NULL;
45015796c8dcSSimon Schubert
45025796c8dcSSimon Schubert /* First see if this is a quoted string. */
45035796c8dcSSimon Schubert quote_found = '\0';
45045796c8dcSSimon Schubert for (p = text; *p != '\0'; ++p)
45055796c8dcSSimon Schubert {
45065796c8dcSSimon Schubert if (quote_found != '\0')
45075796c8dcSSimon Schubert {
45085796c8dcSSimon Schubert if (*p == quote_found)
45095796c8dcSSimon Schubert /* Found close quote. */
45105796c8dcSSimon Schubert quote_found = '\0';
45115796c8dcSSimon Schubert else if (*p == '\\' && p[1] == quote_found)
45125796c8dcSSimon Schubert /* A backslash followed by the quote character
45135796c8dcSSimon Schubert doesn't end the string. */
45145796c8dcSSimon Schubert ++p;
45155796c8dcSSimon Schubert }
45165796c8dcSSimon Schubert else if (*p == '\'' || *p == '"')
45175796c8dcSSimon Schubert {
45185796c8dcSSimon Schubert quote_found = *p;
45195796c8dcSSimon Schubert quote_pos = p;
45205796c8dcSSimon Schubert }
45215796c8dcSSimon Schubert }
45225796c8dcSSimon Schubert if (quote_found == '\'')
45235796c8dcSSimon Schubert /* A string within single quotes can be a symbol, so complete on it. */
45245796c8dcSSimon Schubert sym_text = quote_pos + 1;
45255796c8dcSSimon Schubert else if (quote_found == '"')
45265796c8dcSSimon Schubert /* A double-quoted string is never a symbol, nor does it make sense
45275796c8dcSSimon Schubert to complete it any other way. */
45285796c8dcSSimon Schubert {
4529*ef5ccd6cSJohn Marino return NULL;
45305796c8dcSSimon Schubert }
45315796c8dcSSimon Schubert else
45325796c8dcSSimon Schubert {
45335796c8dcSSimon Schubert /* Not a quoted string. */
45345796c8dcSSimon Schubert sym_text = language_search_unquoted_string (text, p);
45355796c8dcSSimon Schubert }
45365796c8dcSSimon Schubert }
45375796c8dcSSimon Schubert
45385796c8dcSSimon Schubert sym_text_len = strlen (sym_text);
45395796c8dcSSimon Schubert
4540*ef5ccd6cSJohn Marino return_val = NULL;
45415796c8dcSSimon Schubert
45425796c8dcSSimon Schubert /* Find the symtab for SRCFILE (this loads it if it was not yet read
45435796c8dcSSimon Schubert in). */
45445796c8dcSSimon Schubert s = lookup_symtab (srcfile);
45455796c8dcSSimon Schubert if (s == NULL)
45465796c8dcSSimon Schubert {
45475796c8dcSSimon Schubert /* Maybe they typed the file with leading directories, while the
45485796c8dcSSimon Schubert symbol tables record only its basename. */
45495796c8dcSSimon Schubert const char *tail = lbasename (srcfile);
45505796c8dcSSimon Schubert
45515796c8dcSSimon Schubert if (tail > srcfile)
45525796c8dcSSimon Schubert s = lookup_symtab (tail);
45535796c8dcSSimon Schubert }
45545796c8dcSSimon Schubert
45555796c8dcSSimon Schubert /* If we have no symtab for that file, return an empty list. */
45565796c8dcSSimon Schubert if (s == NULL)
45575796c8dcSSimon Schubert return (return_val);
45585796c8dcSSimon Schubert
45595796c8dcSSimon Schubert /* Go through this symtab and check the externs and statics for
45605796c8dcSSimon Schubert symbols which match. */
45615796c8dcSSimon Schubert
45625796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
45635796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (b, iter, sym)
45645796c8dcSSimon Schubert {
45655796c8dcSSimon Schubert COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
45665796c8dcSSimon Schubert }
45675796c8dcSSimon Schubert
45685796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
45695796c8dcSSimon Schubert ALL_BLOCK_SYMBOLS (b, iter, sym)
45705796c8dcSSimon Schubert {
45715796c8dcSSimon Schubert COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
45725796c8dcSSimon Schubert }
45735796c8dcSSimon Schubert
45745796c8dcSSimon Schubert return (return_val);
45755796c8dcSSimon Schubert }
45765796c8dcSSimon Schubert
45775796c8dcSSimon Schubert /* A helper function for make_source_files_completion_list. It adds
45785796c8dcSSimon Schubert another file name to a list of possible completions, growing the
45795796c8dcSSimon Schubert list as necessary. */
45805796c8dcSSimon Schubert
45815796c8dcSSimon Schubert static void
add_filename_to_list(const char * fname,char * text,char * word,VEC (char_ptr)** list)45825796c8dcSSimon Schubert add_filename_to_list (const char *fname, char *text, char *word,
4583*ef5ccd6cSJohn Marino VEC (char_ptr) **list)
45845796c8dcSSimon Schubert {
45855796c8dcSSimon Schubert char *new;
45865796c8dcSSimon Schubert size_t fnlen = strlen (fname);
45875796c8dcSSimon Schubert
45885796c8dcSSimon Schubert if (word == text)
45895796c8dcSSimon Schubert {
45905796c8dcSSimon Schubert /* Return exactly fname. */
45915796c8dcSSimon Schubert new = xmalloc (fnlen + 5);
45925796c8dcSSimon Schubert strcpy (new, fname);
45935796c8dcSSimon Schubert }
45945796c8dcSSimon Schubert else if (word > text)
45955796c8dcSSimon Schubert {
45965796c8dcSSimon Schubert /* Return some portion of fname. */
45975796c8dcSSimon Schubert new = xmalloc (fnlen + 5);
45985796c8dcSSimon Schubert strcpy (new, fname + (word - text));
45995796c8dcSSimon Schubert }
46005796c8dcSSimon Schubert else
46015796c8dcSSimon Schubert {
46025796c8dcSSimon Schubert /* Return some of TEXT plus fname. */
46035796c8dcSSimon Schubert new = xmalloc (fnlen + (text - word) + 5);
46045796c8dcSSimon Schubert strncpy (new, word, text - word);
46055796c8dcSSimon Schubert new[text - word] = '\0';
46065796c8dcSSimon Schubert strcat (new, fname);
46075796c8dcSSimon Schubert }
4608*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, *list, new);
46095796c8dcSSimon Schubert }
46105796c8dcSSimon Schubert
46115796c8dcSSimon Schubert static int
not_interesting_fname(const char * fname)46125796c8dcSSimon Schubert not_interesting_fname (const char *fname)
46135796c8dcSSimon Schubert {
46145796c8dcSSimon Schubert static const char *illegal_aliens[] = {
46155796c8dcSSimon Schubert "_globals_", /* inserted by coff_symtab_read */
46165796c8dcSSimon Schubert NULL
46175796c8dcSSimon Schubert };
46185796c8dcSSimon Schubert int i;
46195796c8dcSSimon Schubert
46205796c8dcSSimon Schubert for (i = 0; illegal_aliens[i]; i++)
46215796c8dcSSimon Schubert {
4622c50c785cSJohn Marino if (filename_cmp (fname, illegal_aliens[i]) == 0)
46235796c8dcSSimon Schubert return 1;
46245796c8dcSSimon Schubert }
46255796c8dcSSimon Schubert return 0;
46265796c8dcSSimon Schubert }
46275796c8dcSSimon Schubert
4628cf7f2e2dSJohn Marino /* An object of this type is passed as the user_data argument to
4629cf7f2e2dSJohn Marino map_partial_symbol_filenames. */
4630cf7f2e2dSJohn Marino struct add_partial_filename_data
4631cf7f2e2dSJohn Marino {
4632*ef5ccd6cSJohn Marino struct filename_seen_cache *filename_seen_cache;
4633cf7f2e2dSJohn Marino char *text;
4634cf7f2e2dSJohn Marino char *word;
4635cf7f2e2dSJohn Marino int text_len;
4636*ef5ccd6cSJohn Marino VEC (char_ptr) **list;
4637cf7f2e2dSJohn Marino };
4638cf7f2e2dSJohn Marino
4639cf7f2e2dSJohn Marino /* A callback for map_partial_symbol_filenames. */
4640*ef5ccd6cSJohn Marino
4641cf7f2e2dSJohn Marino static void
maybe_add_partial_symtab_filename(const char * filename,const char * fullname,void * user_data)4642cf7f2e2dSJohn Marino maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
4643cf7f2e2dSJohn Marino void *user_data)
4644cf7f2e2dSJohn Marino {
4645cf7f2e2dSJohn Marino struct add_partial_filename_data *data = user_data;
4646cf7f2e2dSJohn Marino
4647cf7f2e2dSJohn Marino if (not_interesting_fname (filename))
4648cf7f2e2dSJohn Marino return;
4649*ef5ccd6cSJohn Marino if (!filename_seen (data->filename_seen_cache, filename, 1)
4650c50c785cSJohn Marino && filename_ncmp (filename, data->text, data->text_len) == 0)
4651cf7f2e2dSJohn Marino {
4652cf7f2e2dSJohn Marino /* This file matches for a completion; add it to the
4653cf7f2e2dSJohn Marino current list of matches. */
4654*ef5ccd6cSJohn Marino add_filename_to_list (filename, data->text, data->word, data->list);
4655cf7f2e2dSJohn Marino }
4656cf7f2e2dSJohn Marino else
4657cf7f2e2dSJohn Marino {
4658cf7f2e2dSJohn Marino const char *base_name = lbasename (filename);
4659cf7f2e2dSJohn Marino
4660cf7f2e2dSJohn Marino if (base_name != filename
4661*ef5ccd6cSJohn Marino && !filename_seen (data->filename_seen_cache, base_name, 1)
4662c50c785cSJohn Marino && filename_ncmp (base_name, data->text, data->text_len) == 0)
4663*ef5ccd6cSJohn Marino add_filename_to_list (base_name, data->text, data->word, data->list);
4664cf7f2e2dSJohn Marino }
4665cf7f2e2dSJohn Marino }
4666cf7f2e2dSJohn Marino
4667*ef5ccd6cSJohn Marino /* Return a vector of all source files whose names begin with matching
4668*ef5ccd6cSJohn Marino TEXT. The file names are looked up in the symbol tables of this
4669*ef5ccd6cSJohn Marino program. If the answer is no matchess, then the return value is
4670*ef5ccd6cSJohn Marino NULL. */
46715796c8dcSSimon Schubert
VEC(char_ptr)4672*ef5ccd6cSJohn Marino VEC (char_ptr) *
46735796c8dcSSimon Schubert make_source_files_completion_list (char *text, char *word)
46745796c8dcSSimon Schubert {
46755796c8dcSSimon Schubert struct symtab *s;
46765796c8dcSSimon Schubert struct objfile *objfile;
46775796c8dcSSimon Schubert size_t text_len = strlen (text);
4678*ef5ccd6cSJohn Marino VEC (char_ptr) *list = NULL;
46795796c8dcSSimon Schubert const char *base_name;
4680cf7f2e2dSJohn Marino struct add_partial_filename_data datum;
4681*ef5ccd6cSJohn Marino struct filename_seen_cache *filename_seen_cache;
4682*ef5ccd6cSJohn Marino struct cleanup *back_to, *cache_cleanup;
46835796c8dcSSimon Schubert
46845796c8dcSSimon Schubert if (!have_full_symbols () && !have_partial_symbols ())
46855796c8dcSSimon Schubert return list;
46865796c8dcSSimon Schubert
4687a45ae5f8SJohn Marino back_to = make_cleanup (do_free_completion_list, &list);
4688a45ae5f8SJohn Marino
4689*ef5ccd6cSJohn Marino filename_seen_cache = create_filename_seen_cache ();
4690*ef5ccd6cSJohn Marino cache_cleanup = make_cleanup (delete_filename_seen_cache,
4691*ef5ccd6cSJohn Marino filename_seen_cache);
4692*ef5ccd6cSJohn Marino
46935796c8dcSSimon Schubert ALL_SYMTABS (objfile, s)
46945796c8dcSSimon Schubert {
46955796c8dcSSimon Schubert if (not_interesting_fname (s->filename))
46965796c8dcSSimon Schubert continue;
4697*ef5ccd6cSJohn Marino if (!filename_seen (filename_seen_cache, s->filename, 1)
4698c50c785cSJohn Marino && filename_ncmp (s->filename, text, text_len) == 0)
46995796c8dcSSimon Schubert {
47005796c8dcSSimon Schubert /* This file matches for a completion; add it to the current
47015796c8dcSSimon Schubert list of matches. */
4702*ef5ccd6cSJohn Marino add_filename_to_list (s->filename, text, word, &list);
47035796c8dcSSimon Schubert }
47045796c8dcSSimon Schubert else
47055796c8dcSSimon Schubert {
47065796c8dcSSimon Schubert /* NOTE: We allow the user to type a base name when the
47075796c8dcSSimon Schubert debug info records leading directories, but not the other
47085796c8dcSSimon Schubert way around. This is what subroutines of breakpoint
47095796c8dcSSimon Schubert command do when they parse file names. */
47105796c8dcSSimon Schubert base_name = lbasename (s->filename);
47115796c8dcSSimon Schubert if (base_name != s->filename
4712*ef5ccd6cSJohn Marino && !filename_seen (filename_seen_cache, base_name, 1)
4713c50c785cSJohn Marino && filename_ncmp (base_name, text, text_len) == 0)
4714*ef5ccd6cSJohn Marino add_filename_to_list (base_name, text, word, &list);
47155796c8dcSSimon Schubert }
47165796c8dcSSimon Schubert }
47175796c8dcSSimon Schubert
4718*ef5ccd6cSJohn Marino datum.filename_seen_cache = filename_seen_cache;
4719cf7f2e2dSJohn Marino datum.text = text;
4720cf7f2e2dSJohn Marino datum.word = word;
4721cf7f2e2dSJohn Marino datum.text_len = text_len;
4722cf7f2e2dSJohn Marino datum.list = &list;
4723a45ae5f8SJohn Marino map_partial_symbol_filenames (maybe_add_partial_symtab_filename, &datum,
4724a45ae5f8SJohn Marino 0 /*need_fullname*/);
4725*ef5ccd6cSJohn Marino
4726*ef5ccd6cSJohn Marino do_cleanups (cache_cleanup);
4727a45ae5f8SJohn Marino discard_cleanups (back_to);
47285796c8dcSSimon Schubert
47295796c8dcSSimon Schubert return list;
47305796c8dcSSimon Schubert }
47315796c8dcSSimon Schubert
47325796c8dcSSimon Schubert /* Determine if PC is in the prologue of a function. The prologue is the area
47335796c8dcSSimon Schubert between the first instruction of a function, and the first executable line.
47345796c8dcSSimon Schubert Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
47355796c8dcSSimon Schubert
47365796c8dcSSimon Schubert If non-zero, func_start is where we think the prologue starts, possibly
4737c50c785cSJohn Marino by previous examination of symbol table information. */
47385796c8dcSSimon Schubert
47395796c8dcSSimon Schubert int
in_prologue(struct gdbarch * gdbarch,CORE_ADDR pc,CORE_ADDR func_start)47405796c8dcSSimon Schubert in_prologue (struct gdbarch *gdbarch, CORE_ADDR pc, CORE_ADDR func_start)
47415796c8dcSSimon Schubert {
47425796c8dcSSimon Schubert struct symtab_and_line sal;
47435796c8dcSSimon Schubert CORE_ADDR func_addr, func_end;
47445796c8dcSSimon Schubert
47455796c8dcSSimon Schubert /* We have several sources of information we can consult to figure
47465796c8dcSSimon Schubert this out.
47475796c8dcSSimon Schubert - Compilers usually emit line number info that marks the prologue
47485796c8dcSSimon Schubert as its own "source line". So the ending address of that "line"
47495796c8dcSSimon Schubert is the end of the prologue. If available, this is the most
47505796c8dcSSimon Schubert reliable method.
47515796c8dcSSimon Schubert - The minimal symbols and partial symbols, which can usually tell
47525796c8dcSSimon Schubert us the starting and ending addresses of a function.
47535796c8dcSSimon Schubert - If we know the function's start address, we can call the
47545796c8dcSSimon Schubert architecture-defined gdbarch_skip_prologue function to analyze the
47555796c8dcSSimon Schubert instruction stream and guess where the prologue ends.
47565796c8dcSSimon Schubert - Our `func_start' argument; if non-zero, this is the caller's
47575796c8dcSSimon Schubert best guess as to the function's entry point. At the time of
47585796c8dcSSimon Schubert this writing, handle_inferior_event doesn't get this right, so
47595796c8dcSSimon Schubert it should be our last resort. */
47605796c8dcSSimon Schubert
47615796c8dcSSimon Schubert /* Consult the partial symbol table, to find which function
47625796c8dcSSimon Schubert the PC is in. */
47635796c8dcSSimon Schubert if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
47645796c8dcSSimon Schubert {
47655796c8dcSSimon Schubert CORE_ADDR prologue_end;
47665796c8dcSSimon Schubert
47675796c8dcSSimon Schubert /* We don't even have minsym information, so fall back to using
47685796c8dcSSimon Schubert func_start, if given. */
47695796c8dcSSimon Schubert if (! func_start)
47705796c8dcSSimon Schubert return 1; /* We *might* be in a prologue. */
47715796c8dcSSimon Schubert
47725796c8dcSSimon Schubert prologue_end = gdbarch_skip_prologue (gdbarch, func_start);
47735796c8dcSSimon Schubert
47745796c8dcSSimon Schubert return func_start <= pc && pc < prologue_end;
47755796c8dcSSimon Schubert }
47765796c8dcSSimon Schubert
47775796c8dcSSimon Schubert /* If we have line number information for the function, that's
47785796c8dcSSimon Schubert usually pretty reliable. */
47795796c8dcSSimon Schubert sal = find_pc_line (func_addr, 0);
47805796c8dcSSimon Schubert
47815796c8dcSSimon Schubert /* Now sal describes the source line at the function's entry point,
47825796c8dcSSimon Schubert which (by convention) is the prologue. The end of that "line",
47835796c8dcSSimon Schubert sal.end, is the end of the prologue.
47845796c8dcSSimon Schubert
47855796c8dcSSimon Schubert Note that, for functions whose source code is all on a single
47865796c8dcSSimon Schubert line, the line number information doesn't always end up this way.
47875796c8dcSSimon Schubert So we must verify that our purported end-of-prologue address is
47885796c8dcSSimon Schubert *within* the function, not at its start or end. */
47895796c8dcSSimon Schubert if (sal.line == 0
47905796c8dcSSimon Schubert || sal.end <= func_addr
47915796c8dcSSimon Schubert || func_end <= sal.end)
47925796c8dcSSimon Schubert {
47935796c8dcSSimon Schubert /* We don't have any good line number info, so use the minsym
47945796c8dcSSimon Schubert information, together with the architecture-specific prologue
47955796c8dcSSimon Schubert scanning code. */
47965796c8dcSSimon Schubert CORE_ADDR prologue_end = gdbarch_skip_prologue (gdbarch, func_addr);
47975796c8dcSSimon Schubert
47985796c8dcSSimon Schubert return func_addr <= pc && pc < prologue_end;
47995796c8dcSSimon Schubert }
48005796c8dcSSimon Schubert
48015796c8dcSSimon Schubert /* We have line number info, and it looks good. */
48025796c8dcSSimon Schubert return func_addr <= pc && pc < sal.end;
48035796c8dcSSimon Schubert }
48045796c8dcSSimon Schubert
48055796c8dcSSimon Schubert /* Given PC at the function's start address, attempt to find the
48065796c8dcSSimon Schubert prologue end using SAL information. Return zero if the skip fails.
48075796c8dcSSimon Schubert
48085796c8dcSSimon Schubert A non-optimized prologue traditionally has one SAL for the function
48095796c8dcSSimon Schubert and a second for the function body. A single line function has
48105796c8dcSSimon Schubert them both pointing at the same line.
48115796c8dcSSimon Schubert
48125796c8dcSSimon Schubert An optimized prologue is similar but the prologue may contain
48135796c8dcSSimon Schubert instructions (SALs) from the instruction body. Need to skip those
48145796c8dcSSimon Schubert while not getting into the function body.
48155796c8dcSSimon Schubert
48165796c8dcSSimon Schubert The functions end point and an increasing SAL line are used as
48175796c8dcSSimon Schubert indicators of the prologue's endpoint.
48185796c8dcSSimon Schubert
4819a45ae5f8SJohn Marino This code is based on the function refine_prologue_limit
4820a45ae5f8SJohn Marino (found in ia64). */
48215796c8dcSSimon Schubert
48225796c8dcSSimon Schubert CORE_ADDR
skip_prologue_using_sal(struct gdbarch * gdbarch,CORE_ADDR func_addr)48235796c8dcSSimon Schubert skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
48245796c8dcSSimon Schubert {
48255796c8dcSSimon Schubert struct symtab_and_line prologue_sal;
48265796c8dcSSimon Schubert CORE_ADDR start_pc;
48275796c8dcSSimon Schubert CORE_ADDR end_pc;
48285796c8dcSSimon Schubert struct block *bl;
48295796c8dcSSimon Schubert
48305796c8dcSSimon Schubert /* Get an initial range for the function. */
48315796c8dcSSimon Schubert find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
48325796c8dcSSimon Schubert start_pc += gdbarch_deprecated_function_start_offset (gdbarch);
48335796c8dcSSimon Schubert
48345796c8dcSSimon Schubert prologue_sal = find_pc_line (start_pc, 0);
48355796c8dcSSimon Schubert if (prologue_sal.line != 0)
48365796c8dcSSimon Schubert {
4837a45ae5f8SJohn Marino /* For languages other than assembly, treat two consecutive line
48385796c8dcSSimon Schubert entries at the same address as a zero-instruction prologue.
48395796c8dcSSimon Schubert The GNU assembler emits separate line notes for each instruction
48405796c8dcSSimon Schubert in a multi-instruction macro, but compilers generally will not
48415796c8dcSSimon Schubert do this. */
48425796c8dcSSimon Schubert if (prologue_sal.symtab->language != language_asm)
48435796c8dcSSimon Schubert {
48445796c8dcSSimon Schubert struct linetable *linetable = LINETABLE (prologue_sal.symtab);
48455796c8dcSSimon Schubert int idx = 0;
48465796c8dcSSimon Schubert
48475796c8dcSSimon Schubert /* Skip any earlier lines, and any end-of-sequence marker
48485796c8dcSSimon Schubert from a previous function. */
48495796c8dcSSimon Schubert while (linetable->item[idx].pc != prologue_sal.pc
48505796c8dcSSimon Schubert || linetable->item[idx].line == 0)
48515796c8dcSSimon Schubert idx++;
48525796c8dcSSimon Schubert
48535796c8dcSSimon Schubert if (idx+1 < linetable->nitems
48545796c8dcSSimon Schubert && linetable->item[idx+1].line != 0
48555796c8dcSSimon Schubert && linetable->item[idx+1].pc == start_pc)
48565796c8dcSSimon Schubert return start_pc;
48575796c8dcSSimon Schubert }
48585796c8dcSSimon Schubert
48595796c8dcSSimon Schubert /* If there is only one sal that covers the entire function,
48605796c8dcSSimon Schubert then it is probably a single line function, like
48615796c8dcSSimon Schubert "foo(){}". */
48625796c8dcSSimon Schubert if (prologue_sal.end >= end_pc)
48635796c8dcSSimon Schubert return 0;
48645796c8dcSSimon Schubert
48655796c8dcSSimon Schubert while (prologue_sal.end < end_pc)
48665796c8dcSSimon Schubert {
48675796c8dcSSimon Schubert struct symtab_and_line sal;
48685796c8dcSSimon Schubert
48695796c8dcSSimon Schubert sal = find_pc_line (prologue_sal.end, 0);
48705796c8dcSSimon Schubert if (sal.line == 0)
48715796c8dcSSimon Schubert break;
48725796c8dcSSimon Schubert /* Assume that a consecutive SAL for the same (or larger)
48735796c8dcSSimon Schubert line mark the prologue -> body transition. */
48745796c8dcSSimon Schubert if (sal.line >= prologue_sal.line)
48755796c8dcSSimon Schubert break;
4876*ef5ccd6cSJohn Marino /* Likewise if we are in a different symtab altogether
4877*ef5ccd6cSJohn Marino (e.g. within a file included via #include). */
4878*ef5ccd6cSJohn Marino if (sal.symtab != prologue_sal.symtab)
4879*ef5ccd6cSJohn Marino break;
48805796c8dcSSimon Schubert
48815796c8dcSSimon Schubert /* The line number is smaller. Check that it's from the
48825796c8dcSSimon Schubert same function, not something inlined. If it's inlined,
48835796c8dcSSimon Schubert then there is no point comparing the line numbers. */
48845796c8dcSSimon Schubert bl = block_for_pc (prologue_sal.end);
48855796c8dcSSimon Schubert while (bl)
48865796c8dcSSimon Schubert {
48875796c8dcSSimon Schubert if (block_inlined_p (bl))
48885796c8dcSSimon Schubert break;
48895796c8dcSSimon Schubert if (BLOCK_FUNCTION (bl))
48905796c8dcSSimon Schubert {
48915796c8dcSSimon Schubert bl = NULL;
48925796c8dcSSimon Schubert break;
48935796c8dcSSimon Schubert }
48945796c8dcSSimon Schubert bl = BLOCK_SUPERBLOCK (bl);
48955796c8dcSSimon Schubert }
48965796c8dcSSimon Schubert if (bl != NULL)
48975796c8dcSSimon Schubert break;
48985796c8dcSSimon Schubert
48995796c8dcSSimon Schubert /* The case in which compiler's optimizer/scheduler has
49005796c8dcSSimon Schubert moved instructions into the prologue. We look ahead in
49015796c8dcSSimon Schubert the function looking for address ranges whose
49025796c8dcSSimon Schubert corresponding line number is less the first one that we
49035796c8dcSSimon Schubert found for the function. This is more conservative then
49045796c8dcSSimon Schubert refine_prologue_limit which scans a large number of SALs
4905c50c785cSJohn Marino looking for any in the prologue. */
49065796c8dcSSimon Schubert prologue_sal = sal;
49075796c8dcSSimon Schubert }
49085796c8dcSSimon Schubert }
49095796c8dcSSimon Schubert
49105796c8dcSSimon Schubert if (prologue_sal.end < end_pc)
49115796c8dcSSimon Schubert /* Return the end of this line, or zero if we could not find a
49125796c8dcSSimon Schubert line. */
49135796c8dcSSimon Schubert return prologue_sal.end;
49145796c8dcSSimon Schubert else
49155796c8dcSSimon Schubert /* Don't return END_PC, which is past the end of the function. */
49165796c8dcSSimon Schubert return prologue_sal.pc;
49175796c8dcSSimon Schubert }
49185796c8dcSSimon Schubert
49195796c8dcSSimon Schubert /* Track MAIN */
49205796c8dcSSimon Schubert static char *name_of_main;
4921c50c785cSJohn Marino enum language language_of_main = language_unknown;
49225796c8dcSSimon Schubert
49235796c8dcSSimon Schubert void
set_main_name(const char * name)49245796c8dcSSimon Schubert set_main_name (const char *name)
49255796c8dcSSimon Schubert {
49265796c8dcSSimon Schubert if (name_of_main != NULL)
49275796c8dcSSimon Schubert {
49285796c8dcSSimon Schubert xfree (name_of_main);
49295796c8dcSSimon Schubert name_of_main = NULL;
4930c50c785cSJohn Marino language_of_main = language_unknown;
49315796c8dcSSimon Schubert }
49325796c8dcSSimon Schubert if (name != NULL)
49335796c8dcSSimon Schubert {
49345796c8dcSSimon Schubert name_of_main = xstrdup (name);
4935c50c785cSJohn Marino language_of_main = language_unknown;
49365796c8dcSSimon Schubert }
49375796c8dcSSimon Schubert }
49385796c8dcSSimon Schubert
49395796c8dcSSimon Schubert /* Deduce the name of the main procedure, and set NAME_OF_MAIN
49405796c8dcSSimon Schubert accordingly. */
49415796c8dcSSimon Schubert
49425796c8dcSSimon Schubert static void
find_main_name(void)49435796c8dcSSimon Schubert find_main_name (void)
49445796c8dcSSimon Schubert {
49455796c8dcSSimon Schubert const char *new_main_name;
49465796c8dcSSimon Schubert
49475796c8dcSSimon Schubert /* Try to see if the main procedure is in Ada. */
49485796c8dcSSimon Schubert /* FIXME: brobecker/2005-03-07: Another way of doing this would
49495796c8dcSSimon Schubert be to add a new method in the language vector, and call this
49505796c8dcSSimon Schubert method for each language until one of them returns a non-empty
49515796c8dcSSimon Schubert name. This would allow us to remove this hard-coded call to
49525796c8dcSSimon Schubert an Ada function. It is not clear that this is a better approach
49535796c8dcSSimon Schubert at this point, because all methods need to be written in a way
49545796c8dcSSimon Schubert such that false positives never be returned. For instance, it is
49555796c8dcSSimon Schubert important that a method does not return a wrong name for the main
49565796c8dcSSimon Schubert procedure if the main procedure is actually written in a different
49575796c8dcSSimon Schubert language. It is easy to guaranty this with Ada, since we use a
49585796c8dcSSimon Schubert special symbol generated only when the main in Ada to find the name
49595796c8dcSSimon Schubert of the main procedure. It is difficult however to see how this can
49605796c8dcSSimon Schubert be guarantied for languages such as C, for instance. This suggests
49615796c8dcSSimon Schubert that order of call for these methods becomes important, which means
49625796c8dcSSimon Schubert a more complicated approach. */
49635796c8dcSSimon Schubert new_main_name = ada_main_name ();
49645796c8dcSSimon Schubert if (new_main_name != NULL)
49655796c8dcSSimon Schubert {
49665796c8dcSSimon Schubert set_main_name (new_main_name);
49675796c8dcSSimon Schubert return;
49685796c8dcSSimon Schubert }
49695796c8dcSSimon Schubert
4970*ef5ccd6cSJohn Marino new_main_name = go_main_name ();
4971*ef5ccd6cSJohn Marino if (new_main_name != NULL)
4972*ef5ccd6cSJohn Marino {
4973*ef5ccd6cSJohn Marino set_main_name (new_main_name);
4974*ef5ccd6cSJohn Marino return;
4975*ef5ccd6cSJohn Marino }
4976*ef5ccd6cSJohn Marino
49775796c8dcSSimon Schubert new_main_name = pascal_main_name ();
49785796c8dcSSimon Schubert if (new_main_name != NULL)
49795796c8dcSSimon Schubert {
49805796c8dcSSimon Schubert set_main_name (new_main_name);
49815796c8dcSSimon Schubert return;
49825796c8dcSSimon Schubert }
49835796c8dcSSimon Schubert
49845796c8dcSSimon Schubert /* The languages above didn't identify the name of the main procedure.
49855796c8dcSSimon Schubert Fallback to "main". */
49865796c8dcSSimon Schubert set_main_name ("main");
49875796c8dcSSimon Schubert }
49885796c8dcSSimon Schubert
49895796c8dcSSimon Schubert char *
main_name(void)49905796c8dcSSimon Schubert main_name (void)
49915796c8dcSSimon Schubert {
49925796c8dcSSimon Schubert if (name_of_main == NULL)
49935796c8dcSSimon Schubert find_main_name ();
49945796c8dcSSimon Schubert
49955796c8dcSSimon Schubert return name_of_main;
49965796c8dcSSimon Schubert }
49975796c8dcSSimon Schubert
49985796c8dcSSimon Schubert /* Handle ``executable_changed'' events for the symtab module. */
49995796c8dcSSimon Schubert
50005796c8dcSSimon Schubert static void
symtab_observer_executable_changed(void)50015796c8dcSSimon Schubert symtab_observer_executable_changed (void)
50025796c8dcSSimon Schubert {
50035796c8dcSSimon Schubert /* NAME_OF_MAIN may no longer be the same, so reset it for now. */
50045796c8dcSSimon Schubert set_main_name (NULL);
50055796c8dcSSimon Schubert }
50065796c8dcSSimon Schubert
5007cf7f2e2dSJohn Marino /* Return 1 if the supplied producer string matches the ARM RealView
5008cf7f2e2dSJohn Marino compiler (armcc). */
5009cf7f2e2dSJohn Marino
5010cf7f2e2dSJohn Marino int
producer_is_realview(const char * producer)5011cf7f2e2dSJohn Marino producer_is_realview (const char *producer)
5012cf7f2e2dSJohn Marino {
5013cf7f2e2dSJohn Marino static const char *const arm_idents[] = {
5014cf7f2e2dSJohn Marino "ARM C Compiler, ADS",
5015cf7f2e2dSJohn Marino "Thumb C Compiler, ADS",
5016cf7f2e2dSJohn Marino "ARM C++ Compiler, ADS",
5017cf7f2e2dSJohn Marino "Thumb C++ Compiler, ADS",
5018cf7f2e2dSJohn Marino "ARM/Thumb C/C++ Compiler, RVCT",
5019cf7f2e2dSJohn Marino "ARM C/C++ Compiler, RVCT"
5020cf7f2e2dSJohn Marino };
5021cf7f2e2dSJohn Marino int i;
5022cf7f2e2dSJohn Marino
5023cf7f2e2dSJohn Marino if (producer == NULL)
5024cf7f2e2dSJohn Marino return 0;
5025cf7f2e2dSJohn Marino
5026cf7f2e2dSJohn Marino for (i = 0; i < ARRAY_SIZE (arm_idents); i++)
5027cf7f2e2dSJohn Marino if (strncmp (producer, arm_idents[i], strlen (arm_idents[i])) == 0)
5028cf7f2e2dSJohn Marino return 1;
5029cf7f2e2dSJohn Marino
5030cf7f2e2dSJohn Marino return 0;
5031cf7f2e2dSJohn Marino }
50325796c8dcSSimon Schubert
50335796c8dcSSimon Schubert void
_initialize_symtab(void)50345796c8dcSSimon Schubert _initialize_symtab (void)
50355796c8dcSSimon Schubert {
50365796c8dcSSimon Schubert add_info ("variables", variables_info, _("\
50375796c8dcSSimon Schubert All global and static variable names, or those matching REGEXP."));
50385796c8dcSSimon Schubert if (dbx_commands)
50395796c8dcSSimon Schubert add_com ("whereis", class_info, variables_info, _("\
50405796c8dcSSimon Schubert All global and static variable names, or those matching REGEXP."));
50415796c8dcSSimon Schubert
50425796c8dcSSimon Schubert add_info ("functions", functions_info,
50435796c8dcSSimon Schubert _("All function names, or those matching REGEXP."));
50445796c8dcSSimon Schubert
50455796c8dcSSimon Schubert /* FIXME: This command has at least the following problems:
50465796c8dcSSimon Schubert 1. It prints builtin types (in a very strange and confusing fashion).
50475796c8dcSSimon Schubert 2. It doesn't print right, e.g. with
50485796c8dcSSimon Schubert typedef struct foo *FOO
50495796c8dcSSimon Schubert type_print prints "FOO" when we want to make it (in this situation)
50505796c8dcSSimon Schubert print "struct foo *".
50515796c8dcSSimon Schubert I also think "ptype" or "whatis" is more likely to be useful (but if
50525796c8dcSSimon Schubert there is much disagreement "info types" can be fixed). */
50535796c8dcSSimon Schubert add_info ("types", types_info,
50545796c8dcSSimon Schubert _("All type names, or those matching REGEXP."));
50555796c8dcSSimon Schubert
50565796c8dcSSimon Schubert add_info ("sources", sources_info,
50575796c8dcSSimon Schubert _("Source files in the program."));
50585796c8dcSSimon Schubert
50595796c8dcSSimon Schubert add_com ("rbreak", class_breakpoint, rbreak_command,
50605796c8dcSSimon Schubert _("Set a breakpoint for all functions matching REGEXP."));
50615796c8dcSSimon Schubert
50625796c8dcSSimon Schubert if (xdb_commands)
50635796c8dcSSimon Schubert {
50645796c8dcSSimon Schubert add_com ("lf", class_info, sources_info,
50655796c8dcSSimon Schubert _("Source files in the program"));
50665796c8dcSSimon Schubert add_com ("lg", class_info, variables_info, _("\
50675796c8dcSSimon Schubert All global and static variable names, or those matching REGEXP."));
50685796c8dcSSimon Schubert }
50695796c8dcSSimon Schubert
50705796c8dcSSimon Schubert add_setshow_enum_cmd ("multiple-symbols", no_class,
50715796c8dcSSimon Schubert multiple_symbols_modes, &multiple_symbols_mode,
50725796c8dcSSimon Schubert _("\
50735796c8dcSSimon Schubert Set the debugger behavior when more than one symbol are possible matches\n\
50745796c8dcSSimon Schubert in an expression."), _("\
50755796c8dcSSimon Schubert Show how the debugger handles ambiguities in expressions."), _("\
50765796c8dcSSimon Schubert Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
50775796c8dcSSimon Schubert NULL, NULL, &setlist, &showlist);
50785796c8dcSSimon Schubert
5079a45ae5f8SJohn Marino add_setshow_boolean_cmd ("basenames-may-differ", class_obscure,
5080a45ae5f8SJohn Marino &basenames_may_differ, _("\
5081a45ae5f8SJohn Marino Set whether a source file may have multiple base names."), _("\
5082a45ae5f8SJohn Marino Show whether a source file may have multiple base names."), _("\
5083a45ae5f8SJohn Marino (A \"base name\" is the name of a file with the directory part removed.\n\
5084a45ae5f8SJohn Marino Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
5085a45ae5f8SJohn Marino If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
5086a45ae5f8SJohn Marino before comparing them. Canonicalization is an expensive operation,\n\
5087a45ae5f8SJohn Marino but it allows the same file be known by more than one base name.\n\
5088a45ae5f8SJohn Marino If not set (the default), all source files are assumed to have just\n\
5089a45ae5f8SJohn Marino one base name, and gdb will do file name comparisons more efficiently."),
5090a45ae5f8SJohn Marino NULL, NULL,
5091a45ae5f8SJohn Marino &setlist, &showlist);
5092a45ae5f8SJohn Marino
5093*ef5ccd6cSJohn Marino add_setshow_boolean_cmd ("symtab-create", no_class, &symtab_create_debug,
5094*ef5ccd6cSJohn Marino _("Set debugging of symbol table creation."),
5095*ef5ccd6cSJohn Marino _("Show debugging of symbol table creation."), _("\
5096*ef5ccd6cSJohn Marino When enabled, debugging messages are printed when building symbol tables."),
5097*ef5ccd6cSJohn Marino NULL,
5098*ef5ccd6cSJohn Marino NULL,
5099*ef5ccd6cSJohn Marino &setdebuglist, &showdebuglist);
5100*ef5ccd6cSJohn Marino
51015796c8dcSSimon Schubert observer_attach_executable_changed (symtab_observer_executable_changed);
51025796c8dcSSimon Schubert }
5103