~ubuntu-branches/ubuntu/utopic/crash/utopic-proposed

« back to all changes in this revision

Viewing changes to gdb-6.1/gdb-6.1/gdb-6.1/gdb-6.1/gdb-6.1/gdb-6.1/gdb/symtab.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-05-05 12:54:24 UTC
  • mfrom: (0.1.10 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20080505125424-5q3qi10b5t8f1hc1
Tags: 4.0-6.3-1ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/patches/01_spu_commands.dpatch:
    + SPU extension support
  - debian/rules:
    + Build SPU on powerpc

Show diffs side-by-side

added added

removed removed

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