OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [linespec.c] - Blame information for rev 1782

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* Parser for linespec for the GNU debugger, GDB.
2
   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3
   1996, 1997, 1998, 1999, 2000, 2001
4
   Free Software Foundation, Inc.
5
 
6
   This file is part of GDB.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 2 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 59 Temple Place - Suite 330,
21
   Boston, MA 02111-1307, USA.  */
22
 
23
#include "defs.h"
24
#include "symtab.h"
25
#include "frame.h"
26
#include "command.h"
27
#include "symfile.h"
28
#include "objfiles.h"
29
#include "demangle.h"
30
#include "value.h"
31
#include "completer.h"
32
#include "cp-abi.h"
33
 
34
/* Prototype for one function in parser-defs.h,
35
   instead of including that entire file. */
36
 
37
extern char *find_template_name_end (char *);
38
 
39
/* We share this one with symtab.c, but it is not exported widely. */
40
 
41
extern char *operator_chars (char *, char **);
42
 
43
/* Prototypes for local functions */
44
 
45
static void cplusplus_hint (char *name);
46
 
47
static int total_number_of_methods (struct type *type);
48
 
49
static int find_methods (struct type *, char *, struct symbol **);
50
 
51
static void build_canonical_line_spec (struct symtab_and_line *,
52
                                       char *, char ***);
53
 
54
static char *find_toplevel_char (char *s, char c);
55
 
56
static struct symtabs_and_lines decode_line_2 (struct symbol *[],
57
                                               int, int, char ***);
58
 
59
/* Helper functions. */
60
 
61
/* While the C++ support is still in flux, issue a possibly helpful hint on
62
   using the new command completion feature on single quoted demangled C++
63
   symbols.  Remove when loose ends are cleaned up.   FIXME -fnf */
64
 
65
static void
66
cplusplus_hint (char *name)
67
{
68
  while (*name == '\'')
69
    name++;
70
  printf_filtered ("Hint: try '%s<TAB> or '%s<ESC-?>\n", name, name);
71
  printf_filtered ("(Note leading single quote.)\n");
72
}
73
 
74
/* Return the number of methods described for TYPE, including the
75
   methods from types it derives from. This can't be done in the symbol
76
   reader because the type of the baseclass might still be stubbed
77
   when the definition of the derived class is parsed.  */
78
 
79
static int
80
total_number_of_methods (struct type *type)
81
{
82
  int n;
83
  int count;
84
 
85
  CHECK_TYPEDEF (type);
86
  if (TYPE_CPLUS_SPECIFIC (type) == NULL)
87
    return 0;
88
  count = TYPE_NFN_FIELDS_TOTAL (type);
89
 
90
  for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
91
    count += total_number_of_methods (TYPE_BASECLASS (type, n));
92
 
93
  return count;
94
}
95
 
96
/* Recursive helper function for decode_line_1.
97
   Look for methods named NAME in type T.
98
   Return number of matches.
99
   Put matches in SYM_ARR, which should have been allocated with
100
   a size of total_number_of_methods (T) * sizeof (struct symbol *).
101
   Note that this function is g++ specific.  */
102
 
103
static int
104
find_methods (struct type *t, char *name, struct symbol **sym_arr)
105
{
106
  int i1 = 0;
107
  int ibase;
108
  char *class_name = type_name_no_tag (t);
109
 
110
  /* Ignore this class if it doesn't have a name.  This is ugly, but
111
     unless we figure out how to get the physname without the name of
112
     the class, then the loop can't do any good.  */
113
  if (class_name
114
      && (lookup_symbol (class_name, (struct block *) NULL,
115
                         STRUCT_NAMESPACE, (int *) NULL,
116
                         (struct symtab **) NULL)))
117
    {
118
      int method_counter;
119
 
120
      CHECK_TYPEDEF (t);
121
 
122
      /* Loop over each method name.  At this level, all overloads of a name
123
         are counted as a single name.  There is an inner loop which loops over
124
         each overload.  */
125
 
126
      for (method_counter = TYPE_NFN_FIELDS (t) - 1;
127
           method_counter >= 0;
128
           --method_counter)
129
        {
130
          int field_counter;
131
          char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
132
          char dem_opname[64];
133
 
134
          if (strncmp (method_name, "__", 2) == 0 ||
135
              strncmp (method_name, "op", 2) == 0 ||
136
              strncmp (method_name, "type", 4) == 0)
137
            {
138
              if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
139
                method_name = dem_opname;
140
              else if (cplus_demangle_opname (method_name, dem_opname, 0))
141
                method_name = dem_opname;
142
            }
143
 
144
          if (strcmp_iw (name, method_name) == 0)
145
            /* Find all the overloaded methods with that name.  */
146
            for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
147
                 field_counter >= 0;
148
                 --field_counter)
149
              {
150
                struct fn_field *f;
151
                char *phys_name;
152
 
153
                f = TYPE_FN_FIELDLIST1 (t, method_counter);
154
 
155
                if (TYPE_FN_FIELD_STUB (f, field_counter))
156
                  {
157
                    char *tmp_name;
158
 
159
                    tmp_name = gdb_mangle_name (t,
160
                                                method_counter,
161
                                                field_counter);
162
                    phys_name = alloca (strlen (tmp_name) + 1);
163
                    strcpy (phys_name, tmp_name);
164
                    xfree (tmp_name);
165
                  }
166
                else
167
                  phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
168
 
169
                /* Destructor is handled by caller, dont add it to the list */
170
                if (is_destructor_name (phys_name) != 0)
171
                  continue;
172
 
173
                sym_arr[i1] = lookup_symbol (phys_name,
174
                                             NULL, VAR_NAMESPACE,
175
                                             (int *) NULL,
176
                                             (struct symtab **) NULL);
177
                if (sym_arr[i1])
178
                  i1++;
179
                else
180
                  {
181
                    /* This error message gets printed, but the method
182
                       still seems to be found
183
                       fputs_filtered("(Cannot find method ", gdb_stdout);
184
                       fprintf_symbol_filtered (gdb_stdout, phys_name,
185
                       language_cplus,
186
                       DMGL_PARAMS | DMGL_ANSI);
187
                       fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
188
                     */
189
                  }
190
              }
191
        }
192
    }
193
 
194
  /* Only search baseclasses if there is no match yet, since names in
195
     derived classes override those in baseclasses.
196
 
197
     FIXME: The above is not true; it is only true of member functions
198
     if they have the same number of arguments (??? - section 13.1 of the
199
     ARM says the function members are not in the same scope but doesn't
200
     really spell out the rules in a way I understand.  In any case, if
201
     the number of arguments differ this is a case in which we can overload
202
     rather than hiding without any problem, and gcc 2.4.5 does overload
203
     rather than hiding in this case).  */
204
 
205
  if (i1 == 0)
206
    for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
207
      i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
208
 
209
  return i1;
210
}
211
 
212
/* Helper function for decode_line_1.
213
   Build a canonical line spec in CANONICAL if it is non-NULL and if
214
   the SAL has a symtab.
215
   If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
216
   If SYMNAME is NULL the line number from SAL is used and the canonical
217
   line spec is `filename:linenum'.  */
218
 
219
static void
220
build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
221
                           char ***canonical)
222
{
223
  char **canonical_arr;
224
  char *canonical_name;
225
  char *filename;
226
  struct symtab *s = sal->symtab;
227
 
228
  if (s == (struct symtab *) NULL
229
      || s->filename == (char *) NULL
230
      || canonical == (char ***) NULL)
231
    return;
232
 
233
  canonical_arr = (char **) xmalloc (sizeof (char *));
234
  *canonical = canonical_arr;
235
 
236
  filename = s->filename;
237
  if (symname != NULL)
238
    {
239
      canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
240
      sprintf (canonical_name, "%s:%s", filename, symname);
241
    }
242
  else
243
    {
244
      canonical_name = xmalloc (strlen (filename) + 30);
245
      sprintf (canonical_name, "%s:%d", filename, sal->line);
246
    }
247
  canonical_arr[0] = canonical_name;
248
}
249
 
250
 
251
 
252
/* Find an instance of the character C in the string S that is outside
253
   of all parenthesis pairs, single-quoted strings, and double-quoted
254
   strings.  */
255
static char *
256
find_toplevel_char (char *s, char c)
257
{
258
  int quoted = 0;                /* zero if we're not in quotes;
259
                                   '"' if we're in a double-quoted string;
260
                                   '\'' if we're in a single-quoted string.  */
261
  int depth = 0;         /* number of unclosed parens we've seen */
262
  char *scan;
263
 
264
  for (scan = s; *scan; scan++)
265
    {
266
      if (quoted)
267
        {
268
          if (*scan == quoted)
269
            quoted = 0;
270
          else if (*scan == '\\' && *(scan + 1))
271
            scan++;
272
        }
273
      else if (*scan == c && ! quoted && depth == 0)
274
        return scan;
275
      else if (*scan == '"' || *scan == '\'')
276
        quoted = *scan;
277
      else if (*scan == '(')
278
        depth++;
279
      else if (*scan == ')' && depth > 0)
280
        depth--;
281
    }
282
 
283
  return 0;
284
}
285
 
286
/* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
287
   operate on (ask user if necessary).
288
   If CANONICAL is non-NULL return a corresponding array of mangled names
289
   as canonical line specs there.  */
290
 
291
static struct symtabs_and_lines
292
decode_line_2 (struct symbol *sym_arr[], int nelts, int funfirstline,
293
               char ***canonical)
294
{
295
  struct symtabs_and_lines values, return_values;
296
  char *args, *arg1;
297
  int i;
298
  char *prompt;
299
  char *symname;
300
  struct cleanup *old_chain;
301
  char **canonical_arr = (char **) NULL;
302
 
303
  values.sals = (struct symtab_and_line *)
304
    alloca (nelts * sizeof (struct symtab_and_line));
305
  return_values.sals = (struct symtab_and_line *)
306
    xmalloc (nelts * sizeof (struct symtab_and_line));
307
  old_chain = make_cleanup (xfree, return_values.sals);
308
 
309
  if (canonical)
310
    {
311
      canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
312
      make_cleanup (xfree, canonical_arr);
313
      memset (canonical_arr, 0, nelts * sizeof (char *));
314
      *canonical = canonical_arr;
315
    }
316
 
317
  i = 0;
318
  printf_unfiltered ("[0] cancel\n[1] all\n");
319
  while (i < nelts)
320
    {
321
      INIT_SAL (&return_values.sals[i]);        /* initialize to zeroes */
322
      INIT_SAL (&values.sals[i]);
323
      if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
324
        {
325
          values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
326
          printf_unfiltered ("[%d] %s at %s:%d\n",
327
                             (i + 2),
328
                             SYMBOL_SOURCE_NAME (sym_arr[i]),
329
                             values.sals[i].symtab->filename,
330
                             values.sals[i].line);
331
        }
332
      else
333
        printf_unfiltered ("?HERE\n");
334
      i++;
335
    }
336
 
337
  if ((prompt = getenv ("PS2")) == NULL)
338
    {
339
      prompt = "> ";
340
    }
341
  args = command_line_input (prompt, 0, "overload-choice");
342
 
343
  if (args == 0 || *args == 0)
344
    error_no_arg ("one or more choice numbers");
345
 
346
  i = 0;
347
  while (*args)
348
    {
349
      int num;
350
 
351
      arg1 = args;
352
      while (*arg1 >= '0' && *arg1 <= '9')
353
        arg1++;
354
      if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
355
        error ("Arguments must be choice numbers.");
356
 
357
      num = atoi (args);
358
 
359
      if (num == 0)
360
        error ("canceled");
361
      else if (num == 1)
362
        {
363
          if (canonical_arr)
364
            {
365
              for (i = 0; i < nelts; i++)
366
                {
367
                  if (canonical_arr[i] == NULL)
368
                    {
369
                      symname = SYMBOL_NAME (sym_arr[i]);
370
                      canonical_arr[i] = savestring (symname, strlen (symname));
371
                    }
372
                }
373
            }
374
          memcpy (return_values.sals, values.sals,
375
                  (nelts * sizeof (struct symtab_and_line)));
376
          return_values.nelts = nelts;
377
          discard_cleanups (old_chain);
378
          return return_values;
379
        }
380
 
381
      if (num >= nelts + 2)
382
        {
383
          printf_unfiltered ("No choice number %d.\n", num);
384
        }
385
      else
386
        {
387
          num -= 2;
388
          if (values.sals[num].pc)
389
            {
390
              if (canonical_arr)
391
                {
392
                  symname = SYMBOL_NAME (sym_arr[num]);
393
                  make_cleanup (xfree, symname);
394
                  canonical_arr[i] = savestring (symname, strlen (symname));
395
                }
396
              return_values.sals[i++] = values.sals[num];
397
              values.sals[num].pc = 0;
398
            }
399
          else
400
            {
401
              printf_unfiltered ("duplicate request for %d ignored.\n", num);
402
            }
403
        }
404
 
405
      args = arg1;
406
      while (*args == ' ' || *args == '\t')
407
        args++;
408
    }
409
  return_values.nelts = i;
410
  discard_cleanups (old_chain);
411
  return return_values;
412
}
413
 
414
/* The parser of linespec itself. */
415
 
416
/* Parse a string that specifies a line number.
417
   Pass the address of a char * variable; that variable will be
418
   advanced over the characters actually parsed.
419
 
420
   The string can be:
421
 
422
   LINENUM -- that line number in current file.  PC returned is 0.
423
   FILE:LINENUM -- that line in that file.  PC returned is 0.
424
   FUNCTION -- line number of openbrace of that function.
425
   PC returned is the start of the function.
426
   VARIABLE -- line number of definition of that variable.
427
   PC returned is 0.
428
   FILE:FUNCTION -- likewise, but prefer functions in that file.
429
   *EXPR -- line in which address EXPR appears.
430
 
431
   This may all be followed by an "if EXPR", which we ignore.
432
 
433
   FUNCTION may be an undebuggable function found in minimal symbol table.
434
 
435
   If the argument FUNFIRSTLINE is nonzero, we want the first line
436
   of real code inside a function when a function is specified, and it is
437
   not OK to specify a variable or type to get its line number.
438
 
439
   DEFAULT_SYMTAB specifies the file to use if none is specified.
440
   It defaults to current_source_symtab.
441
   DEFAULT_LINE specifies the line number to use for relative
442
   line numbers (that start with signs).  Defaults to current_source_line.
443
   If CANONICAL is non-NULL, store an array of strings containing the canonical
444
   line specs there if necessary. Currently overloaded member functions and
445
   line numbers or static functions without a filename yield a canonical
446
   line spec. The array and the line spec strings are allocated on the heap,
447
   it is the callers responsibility to free them.
448
 
449
   Note that it is possible to return zero for the symtab
450
   if no file is validly specified.  Callers must check that.
451
   Also, the line number returned may be invalid.  */
452
 
453
/* We allow single quotes in various places.  This is a hideous
454
   kludge, which exists because the completer can't yet deal with the
455
   lack of single quotes.  FIXME: write a linespec_completer which we
456
   can use as appropriate instead of make_symbol_completion_list.  */
457
 
458
struct symtabs_and_lines
459
decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
460
               int default_line, char ***canonical)
461
{
462
  struct symtabs_and_lines values;
463
  struct symtab_and_line val;
464
  register char *p, *p1;
465
  char *q, *pp, *ii, *p2;
466
#if 0
467
  char *q1;
468
#endif
469
  register struct symtab *s;
470
 
471
  register struct symbol *sym;
472
  /* The symtab that SYM was found in.  */
473
  struct symtab *sym_symtab;
474
 
475
  register CORE_ADDR pc;
476
  register struct minimal_symbol *msymbol;
477
  char *copy;
478
  struct symbol *sym_class;
479
  int i1;
480
  int is_quoted;
481
  int is_quote_enclosed;
482
  int has_parens;
483
  int has_if = 0;
484
  int has_comma = 0;
485
  struct symbol **sym_arr;
486
  struct type *t;
487
  char *saved_arg = *argptr;
488
  extern char *gdb_completer_quote_characters;
489
 
490
  INIT_SAL (&val);              /* initialize to zeroes */
491
 
492
  /* Defaults have defaults.  */
493
 
494
  if (default_symtab == 0)
495
    {
496
      default_symtab = current_source_symtab;
497
      default_line = current_source_line;
498
    }
499
 
500
  /* See if arg is *PC */
501
 
502
  if (**argptr == '*')
503
    {
504
      (*argptr)++;
505
      pc = parse_and_eval_address_1 (argptr);
506
 
507
      values.sals = (struct symtab_and_line *)
508
        xmalloc (sizeof (struct symtab_and_line));
509
 
510
      values.nelts = 1;
511
      values.sals[0] = find_pc_line (pc, 0);
512
      values.sals[0].pc = pc;
513
      values.sals[0].section = find_pc_overlay (pc);
514
 
515
      return values;
516
    }
517
 
518
  /* 'has_if' is for the syntax:
519
   *     (gdb) break foo if (a==b)
520
   */
521
  if ((ii = strstr (*argptr, " if ")) != NULL ||
522
      (ii = strstr (*argptr, "\tif ")) != NULL ||
523
      (ii = strstr (*argptr, " if\t")) != NULL ||
524
      (ii = strstr (*argptr, "\tif\t")) != NULL ||
525
      (ii = strstr (*argptr, " if(")) != NULL ||
526
      (ii = strstr (*argptr, "\tif( ")) != NULL)
527
    has_if = 1;
528
  /* Temporarily zap out "if (condition)" to not
529
   * confuse the parenthesis-checking code below.
530
   * This is undone below. Do not change ii!!
531
   */
532
  if (has_if)
533
    {
534
      *ii = '\0';
535
    }
536
 
537
  /* Set various flags.
538
   * 'has_parens' is important for overload checking, where
539
   * we allow things like:
540
   *     (gdb) break c::f(int)
541
   */
542
 
543
  /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
544
 
545
  is_quoted = (**argptr
546
               && strchr (get_gdb_completer_quote_characters (),
547
                          **argptr) != NULL);
548
 
549
  has_parens = ((pp = strchr (*argptr, '(')) != NULL
550
                && (pp = strrchr (pp, ')')) != NULL);
551
 
552
  /* Now that we're safely past the has_parens check,
553
   * put back " if (condition)" so outer layers can see it
554
   */
555
  if (has_if)
556
    *ii = ' ';
557
 
558
  /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
559
     and we must isolate the first half.  Outer layers will call again later
560
     for the second half.
561
 
562
     Don't count commas that appear in argument lists of overloaded
563
     functions, or in quoted strings.  It's stupid to go to this much
564
     trouble when the rest of the function is such an obvious roach hotel.  */
565
  ii = find_toplevel_char (*argptr, ',');
566
  has_comma = (ii != 0);
567
 
568
  /* Temporarily zap out second half to not
569
   * confuse the code below.
570
   * This is undone below. Do not change ii!!
571
   */
572
  if (has_comma)
573
    {
574
      *ii = '\0';
575
    }
576
 
577
  /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
578
  /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
579
  /* Look for ':', but ignore inside of <> */
580
 
581
  s = NULL;
582
  p = *argptr;
583
  if (p[0] == '"')
584
    {
585
      is_quote_enclosed = 1;
586
      (*argptr)++;
587
      p++;
588
    }
589
  else
590
    is_quote_enclosed = 0;
591
  for (; *p; p++)
592
    {
593
      if (p[0] == '<')
594
        {
595
          char *temp_end = find_template_name_end (p);
596
          if (!temp_end)
597
            error ("malformed template specification in command");
598
          p = temp_end;
599
        }
600
      /* Check for the end of the first half of the linespec.  End of line,
601
         a tab, a double colon or the last single colon, or a space.  But
602
         if enclosed in double quotes we do not break on enclosed spaces */
603
      if (!*p
604
          || p[0] == '\t'
605
          || ((p[0] == ':')
606
              && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
607
          || ((p[0] == ' ') && !is_quote_enclosed))
608
        break;
609
      if (p[0] == '.' && strchr (p, ':') == NULL)        /* Java qualified method. */
610
        {
611
          /* Find the *last* '.', since the others are package qualifiers. */
612
          for (p1 = p; *p1; p1++)
613
            {
614
              if (*p1 == '.')
615
                p = p1;
616
            }
617
          break;
618
        }
619
    }
620
  while (p[0] == ' ' || p[0] == '\t')
621
    p++;
622
 
623
  /* if the closing double quote was left at the end, remove it */
624
  if (is_quote_enclosed)
625
    {
626
      char *closing_quote = strchr (p - 1, '"');
627
      if (closing_quote && closing_quote[1] == '\0')
628
        *closing_quote = '\0';
629
    }
630
 
631
  /* Now that we've safely parsed the first half,
632
   * put back ',' so outer layers can see it
633
   */
634
  if (has_comma)
635
    *ii = ',';
636
 
637
  if ((p[0] == ':' || p[0] == '.') && !has_parens)
638
    {
639
      /*  C++ */
640
      /*  ... or Java */
641
      if (is_quoted)
642
        *argptr = *argptr + 1;
643
      if (p[0] == '.' || p[1] == ':')
644
        {
645
          char *saved_arg2 = *argptr;
646
          char *temp_end;
647
          /* First check for "global" namespace specification,
648
             of the form "::foo". If found, skip over the colons
649
             and jump to normal symbol processing */
650
          if (p[0] == ':'
651
              && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
652
            saved_arg2 += 2;
653
 
654
          /* We have what looks like a class or namespace
655
             scope specification (A::B), possibly with many
656
             levels of namespaces or classes (A::B::C::D).
657
 
658
             Some versions of the HP ANSI C++ compiler (as also possibly
659
             other compilers) generate class/function/member names with
660
             embedded double-colons if they are inside namespaces. To
661
             handle this, we loop a few times, considering larger and
662
             larger prefixes of the string as though they were single
663
             symbols.  So, if the initially supplied string is
664
             A::B::C::D::foo, we have to look up "A", then "A::B",
665
             then "A::B::C", then "A::B::C::D", and finally
666
             "A::B::C::D::foo" as single, monolithic symbols, because
667
             A, B, C or D may be namespaces.
668
 
669
             Note that namespaces can nest only inside other
670
             namespaces, and not inside classes.  So we need only
671
             consider *prefixes* of the string; there is no need to look up
672
             "B::C" separately as a symbol in the previous example. */
673
 
674
          p2 = p;               /* save for restart */
675
          while (1)
676
            {
677
              /* Extract the class name.  */
678
              p1 = p;
679
              while (p != *argptr && p[-1] == ' ')
680
                --p;
681
              copy = (char *) alloca (p - *argptr + 1);
682
              memcpy (copy, *argptr, p - *argptr);
683
              copy[p - *argptr] = 0;
684
 
685
              /* Discard the class name from the arg.  */
686
              p = p1 + (p1[0] == ':' ? 2 : 1);
687
              while (*p == ' ' || *p == '\t')
688
                p++;
689
              *argptr = p;
690
 
691
              sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
692
                                         (struct symtab **) NULL);
693
 
694
              if (sym_class &&
695
                  (t = check_typedef (SYMBOL_TYPE (sym_class)),
696
                   (TYPE_CODE (t) == TYPE_CODE_STRUCT
697
                    || TYPE_CODE (t) == TYPE_CODE_UNION)))
698
                {
699
                  /* Arg token is not digits => try it as a function name
700
                     Find the next token(everything up to end or next blank). */
701
                  if (**argptr
702
                      && strchr (get_gdb_completer_quote_characters (),
703
                                 **argptr) != NULL)
704
                    {
705
                      p = skip_quoted (*argptr);
706
                      *argptr = *argptr + 1;
707
                    }
708
                  else
709
                    {
710
                      p = *argptr;
711
                      while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
712
                        p++;
713
                    }
714
/*
715
   q = operator_chars (*argptr, &q1);
716
   if (q1 - q)
717
   {
718
   char *opname;
719
   char *tmp = alloca (q1 - q + 1);
720
   memcpy (tmp, q, q1 - q);
721
   tmp[q1 - q] = '\0';
722
   opname = cplus_mangle_opname (tmp, DMGL_ANSI);
723
   if (opname == NULL)
724
   {
725
   error_begin ();
726
   printf_filtered ("no mangling for \"%s\"\n", tmp);
727
   cplusplus_hint (saved_arg);
728
   return_to_top_level (RETURN_ERROR);
729
   }
730
   copy = (char*) alloca (3 + strlen(opname));
731
   sprintf (copy, "__%s", opname);
732
   p = q1;
733
   }
734
   else
735
 */
736
                  {
737
                    copy = (char *) alloca (p - *argptr + 1);
738
                    memcpy (copy, *argptr, p - *argptr);
739
                    copy[p - *argptr] = '\0';
740
                    if (p != *argptr
741
                        && copy[p - *argptr - 1]
742
                        && strchr (get_gdb_completer_quote_characters (),
743
                                   copy[p - *argptr - 1]) != NULL)
744
                      copy[p - *argptr - 1] = '\0';
745
                  }
746
 
747
                  /* no line number may be specified */
748
                  while (*p == ' ' || *p == '\t')
749
                    p++;
750
                  *argptr = p;
751
 
752
                  sym = 0;
753
                  i1 = 0;        /*  counter for the symbol array */
754
                  sym_arr = (struct symbol **) alloca (total_number_of_methods (t)
755
                                                * sizeof (struct symbol *));
756
 
757
                  if (destructor_name_p (copy, t))
758
                    {
759
                      /* Destructors are a special case.  */
760
                      int m_index, f_index;
761
 
762
                      if (get_destructor_fn_field (t, &m_index, &f_index))
763
                        {
764
                          struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
765
 
766
                          sym_arr[i1] =
767
                            lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
768
                                           NULL, VAR_NAMESPACE, (int *) NULL,
769
                                           (struct symtab **) NULL);
770
                          if (sym_arr[i1])
771
                            i1++;
772
                        }
773
                    }
774
                  else
775
                    i1 = find_methods (t, copy, sym_arr);
776
                  if (i1 == 1)
777
                    {
778
                      /* There is exactly one field with that name.  */
779
                      sym = sym_arr[0];
780
 
781
                      if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
782
                        {
783
                          values.sals = (struct symtab_and_line *)
784
                            xmalloc (sizeof (struct symtab_and_line));
785
                          values.nelts = 1;
786
                          values.sals[0] = find_function_start_sal (sym,
787
                                                              funfirstline);
788
                        }
789
                      else
790
                        {
791
                          values.nelts = 0;
792
                        }
793
                      return values;
794
                    }
795
                  if (i1 > 0)
796
                    {
797
                      /* There is more than one field with that name
798
                         (overloaded).  Ask the user which one to use.  */
799
                      return decode_line_2 (sym_arr, i1, funfirstline, canonical);
800
                    }
801
                  else
802
                    {
803
                      char *tmp;
804
 
805
                      if (is_operator_name (copy))
806
                        {
807
                          tmp = (char *) alloca (strlen (copy + 3) + 9);
808
                          strcpy (tmp, "operator ");
809
                          strcat (tmp, copy + 3);
810
                        }
811
                      else
812
                        tmp = copy;
813
                      error_begin ();
814
                      if (tmp[0] == '~')
815
                        printf_filtered
816
                          ("the class `%s' does not have destructor defined\n",
817
                           SYMBOL_SOURCE_NAME (sym_class));
818
                      else
819
                        printf_filtered
820
                          ("the class %s does not have any method named %s\n",
821
                           SYMBOL_SOURCE_NAME (sym_class), tmp);
822
                      cplusplus_hint (saved_arg);
823
                      return_to_top_level (RETURN_ERROR);
824
                    }
825
                }
826
 
827
              /* Move pointer up to next possible class/namespace token */
828
              p = p2 + 1;       /* restart with old value +1 */
829
              /* Move pointer ahead to next double-colon */
830
              while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
831
                {
832
                  if (p[0] == '<')
833
                    {
834
                      temp_end = find_template_name_end (p);
835
                      if (!temp_end)
836
                        error ("malformed template specification in command");
837
                      p = temp_end;
838
                    }
839
                  else if ((p[0] == ':') && (p[1] == ':'))
840
                    break;      /* found double-colon */
841
                  else
842
                    p++;
843
                }
844
 
845
              if (*p != ':')
846
                break;          /* out of the while (1) */
847
 
848
              p2 = p;           /* save restart for next time around */
849
              *argptr = saved_arg2;     /* restore argptr */
850
            }                   /* while (1) */
851
 
852
          /* Last chance attempt -- check entire name as a symbol */
853
          /* Use "copy" in preparation for jumping out of this block,
854
             to be consistent with usage following the jump target */
855
          copy = (char *) alloca (p - saved_arg2 + 1);
856
          memcpy (copy, saved_arg2, p - saved_arg2);
857
          /* Note: if is_quoted should be true, we snuff out quote here anyway */
858
          copy[p - saved_arg2] = '\000';
859
          /* Set argptr to skip over the name */
860
          *argptr = (*p == '\'') ? p + 1 : p;
861
          /* Look up entire name */
862
          sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
863
          s = (struct symtab *) 0;
864
          /* Prepare to jump: restore the " if (condition)" so outer layers see it */
865
          /* Symbol was found --> jump to normal symbol processing.
866
             Code following "symbol_found" expects "copy" to have the
867
             symbol name, "sym" to have the symbol pointer, "s" to be
868
             a specified file's symtab, and sym_symtab to be the symbol's
869
             symtab. */
870
          /* By jumping there we avoid falling through the FILE:LINE and
871
             FILE:FUNC processing stuff below */
872
          if (sym)
873
            goto symbol_found;
874
 
875
          /* Couldn't find any interpretation as classes/namespaces, so give up */
876
          error_begin ();
877
          /* The quotes are important if copy is empty.  */
878
          printf_filtered
879
            ("Can't find member of namespace, class, struct, or union named \"%s\"\n", copy);
880
          cplusplus_hint (saved_arg);
881
          return_to_top_level (RETURN_ERROR);
882
        }
883
      /*  end of C++  */
884
 
885
 
886
      /* Extract the file name.  */
887
      p1 = p;
888
      while (p != *argptr && p[-1] == ' ')
889
        --p;
890
      if ((*p == '"') && is_quote_enclosed)
891
        --p;
892
      copy = (char *) alloca (p - *argptr + 1);
893
      if ((**argptr == '"') && is_quote_enclosed)
894
        {
895
          memcpy (copy, *argptr + 1, p - *argptr - 1);
896
          /* It may have the ending quote right after the file name */
897
          if (copy[p - *argptr - 2] == '"')
898
            copy[p - *argptr - 2] = 0;
899
          else
900
            copy[p - *argptr - 1] = 0;
901
        }
902
      else
903
        {
904
          memcpy (copy, *argptr, p - *argptr);
905
          copy[p - *argptr] = 0;
906
        }
907
 
908
      /* Find that file's data.  */
909
      s = lookup_symtab (copy);
910
      if (s == 0)
911
        {
912
          if (!have_full_symbols () && !have_partial_symbols ())
913
            error ("No symbol table is loaded.  Use the \"file\" command.");
914
          error ("No source file named %s.", copy);
915
        }
916
 
917
      /* Discard the file name from the arg.  */
918
      p = p1 + 1;
919
      while (*p == ' ' || *p == '\t')
920
        p++;
921
      *argptr = p;
922
    }
923
#if 0
924
  /* No one really seems to know why this was added. It certainly
925
     breaks the command line, though, whenever the passed
926
     name is of the form ClassName::Method. This bit of code
927
     singles out the class name, and if funfirstline is set (for
928
     example, you are setting a breakpoint at this function),
929
     you get an error. This did not occur with earlier
930
     verions, so I am ifdef'ing this out. 3/29/99 */
931
  else
932
    {
933
      /* Check if what we have till now is a symbol name */
934
 
935
      /* We may be looking at a template instantiation such
936
         as "foo<int>".  Check here whether we know about it,
937
         instead of falling through to the code below which
938
         handles ordinary function names, because that code
939
         doesn't like seeing '<' and '>' in a name -- the
940
         skip_quoted call doesn't go past them.  So see if we
941
         can figure it out right now. */
942
 
943
      copy = (char *) alloca (p - *argptr + 1);
944
      memcpy (copy, *argptr, p - *argptr);
945
      copy[p - *argptr] = '\000';
946
      sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
947
      if (sym)
948
        {
949
          /* Yes, we have a symbol; jump to symbol processing */
950
          /* Code after symbol_found expects S, SYM_SYMTAB, SYM,
951
             and COPY to be set correctly */
952
          *argptr = (*p == '\'') ? p + 1 : p;
953
          s = (struct symtab *) 0;
954
          goto symbol_found;
955
        }
956
      /* Otherwise fall out from here and go to file/line spec
957
         processing, etc. */
958
    }
959
#endif
960
 
961
  /* S is specified file's symtab, or 0 if no file specified.
962
     arg no longer contains the file name.  */
963
 
964
  /* Check whether arg is all digits (and sign) */
965
 
966
  q = *argptr;
967
  if (*q == '-' || *q == '+')
968
    q++;
969
  while (*q >= '0' && *q <= '9')
970
    q++;
971
 
972
  if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
973
    {
974
      /* We found a token consisting of all digits -- at least one digit.  */
975
      enum sign
976
        {
977
          none, plus, minus
978
        }
979
      sign = none;
980
 
981
      /* We might need a canonical line spec if no file was specified.  */
982
      int need_canonical = (s == 0) ? 1 : 0;
983
 
984
      /* This is where we need to make sure that we have good defaults.
985
         We must guarantee that this section of code is never executed
986
         when we are called with just a function name, since
987
         select_source_symtab calls us with such an argument  */
988
 
989
      if (s == 0 && default_symtab == 0)
990
        {
991
          select_source_symtab (0);
992
          default_symtab = current_source_symtab;
993
          default_line = current_source_line;
994
        }
995
 
996
      if (**argptr == '+')
997
        sign = plus, (*argptr)++;
998
      else if (**argptr == '-')
999
        sign = minus, (*argptr)++;
1000
      val.line = atoi (*argptr);
1001
      switch (sign)
1002
        {
1003
        case plus:
1004
          if (q == *argptr)
1005
            val.line = 5;
1006
          if (s == 0)
1007
            val.line = default_line + val.line;
1008
          break;
1009
        case minus:
1010
          if (q == *argptr)
1011
            val.line = 15;
1012
          if (s == 0)
1013
            val.line = default_line - val.line;
1014
          else
1015
            val.line = 1;
1016
          break;
1017
        case none:
1018
          break;                /* No need to adjust val.line.  */
1019
        }
1020
 
1021
      while (*q == ' ' || *q == '\t')
1022
        q++;
1023
      *argptr = q;
1024
      if (s == 0)
1025
        s = default_symtab;
1026
 
1027
      /* It is possible that this source file has more than one symtab,
1028
         and that the new line number specification has moved us from the
1029
         default (in s) to a new one.  */
1030
      val.symtab = find_line_symtab (s, val.line, NULL, NULL);
1031
      if (val.symtab == 0)
1032
        val.symtab = s;
1033
 
1034
      val.pc = 0;
1035
      values.sals = (struct symtab_and_line *)
1036
        xmalloc (sizeof (struct symtab_and_line));
1037
      values.sals[0] = val;
1038
      values.nelts = 1;
1039
      if (need_canonical)
1040
        build_canonical_line_spec (values.sals, NULL, canonical);
1041
      return values;
1042
    }
1043
 
1044
  /* Arg token is not digits => try it as a variable name
1045
     Find the next token (everything up to end or next whitespace).  */
1046
 
1047
  if (**argptr == '$')          /* May be a convenience variable */
1048
    p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));        /* One or two $ chars possible */
1049
  else if (is_quoted)
1050
    {
1051
      p = skip_quoted (*argptr);
1052
      if (p[-1] != '\'')
1053
        error ("Unmatched single quote.");
1054
    }
1055
  else if (has_parens)
1056
    {
1057
      p = pp + 1;
1058
    }
1059
  else
1060
    {
1061
      p = skip_quoted (*argptr);
1062
    }
1063
 
1064
  copy = (char *) alloca (p - *argptr + 1);
1065
  memcpy (copy, *argptr, p - *argptr);
1066
  copy[p - *argptr] = '\0';
1067
  if (p != *argptr
1068
      && copy[0]
1069
      && copy[0] == copy[p - *argptr - 1]
1070
      && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
1071
    {
1072
      copy[p - *argptr - 1] = '\0';
1073
      copy++;
1074
    }
1075
  while (*p == ' ' || *p == '\t')
1076
    p++;
1077
  *argptr = p;
1078
 
1079
  /* If it starts with $: may be a legitimate variable or routine name
1080
     (e.g. HP-UX millicode routines such as $$dyncall), or it may
1081
     be history value, or it may be a convenience variable */
1082
 
1083
  if (*copy == '$')
1084
    {
1085
      value_ptr valx;
1086
      int index = 0;
1087
      int need_canonical = 0;
1088
 
1089
      p = (copy[1] == '$') ? copy + 2 : copy + 1;
1090
      while (*p >= '0' && *p <= '9')
1091
        p++;
1092
      if (!*p)                  /* reached end of token without hitting non-digit */
1093
        {
1094
          /* We have a value history reference */
1095
          sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
1096
          valx = access_value_history ((copy[1] == '$') ? -index : index);
1097
          if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1098
            error ("History values used in line specs must have integer values.");
1099
        }
1100
      else
1101
        {
1102
          /* Not all digits -- may be user variable/function or a
1103
             convenience variable */
1104
 
1105
          /* Look up entire name as a symbol first */
1106
          sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
1107
          s = (struct symtab *) 0;
1108
          need_canonical = 1;
1109
          /* Symbol was found --> jump to normal symbol processing.
1110
             Code following "symbol_found" expects "copy" to have the
1111
             symbol name, "sym" to have the symbol pointer, "s" to be
1112
             a specified file's symtab, and sym_symtab to be the symbol's
1113
             symtab. */
1114
          if (sym)
1115
            goto symbol_found;
1116
 
1117
          /* If symbol was not found, look in minimal symbol tables */
1118
          msymbol = lookup_minimal_symbol (copy, 0, 0);
1119
          /* Min symbol was found --> jump to minsym processing. */
1120
          if (msymbol)
1121
            goto minimal_symbol_found;
1122
 
1123
          /* Not a user variable or function -- must be convenience variable */
1124
          need_canonical = (s == 0) ? 1 : 0;
1125
          valx = value_of_internalvar (lookup_internalvar (copy + 1));
1126
          if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1127
            error ("Convenience variables used in line specs must have integer values.");
1128
        }
1129
 
1130
      /* Either history value or convenience value from above, in valx */
1131
      val.symtab = s ? s : default_symtab;
1132
      val.line = value_as_long (valx);
1133
      val.pc = 0;
1134
 
1135
      values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
1136
      values.sals[0] = val;
1137
      values.nelts = 1;
1138
 
1139
      if (need_canonical)
1140
        build_canonical_line_spec (values.sals, NULL, canonical);
1141
 
1142
      return values;
1143
    }
1144
 
1145
 
1146
  /* Look up that token as a variable.
1147
     If file specified, use that file's per-file block to start with.  */
1148
 
1149
  sym = lookup_symbol (copy,
1150
                       (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
1151
                        : get_selected_block ()),
1152
                       VAR_NAMESPACE, 0, &sym_symtab);
1153
 
1154
symbol_found:                   /* We also jump here from inside the C++ class/namespace
1155
                                   code on finding a symbol of the form "A::B::C" */
1156
 
1157
  if (sym != NULL)
1158
    {
1159
      if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1160
        {
1161
          /* Arg is the name of a function */
1162
          values.sals = (struct symtab_and_line *)
1163
            xmalloc (sizeof (struct symtab_and_line));
1164
          values.sals[0] = find_function_start_sal (sym, funfirstline);
1165
          values.nelts = 1;
1166
 
1167
          /* Don't use the SYMBOL_LINE; if used at all it points to
1168
             the line containing the parameters or thereabouts, not
1169
             the first line of code.  */
1170
 
1171
          /* We might need a canonical line spec if it is a static
1172
             function.  */
1173
          if (s == 0)
1174
            {
1175
              struct blockvector *bv = BLOCKVECTOR (sym_symtab);
1176
              struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1177
              if (lookup_block_symbol (b, copy, VAR_NAMESPACE) != NULL)
1178
                build_canonical_line_spec (values.sals, copy, canonical);
1179
            }
1180
          return values;
1181
        }
1182
      else
1183
        {
1184
          if (funfirstline)
1185
            error ("\"%s\" is not a function", copy);
1186
          else if (SYMBOL_LINE (sym) != 0)
1187
            {
1188
              /* We know its line number.  */
1189
              values.sals = (struct symtab_and_line *)
1190
                xmalloc (sizeof (struct symtab_and_line));
1191
              values.nelts = 1;
1192
              memset (&values.sals[0], 0, sizeof (values.sals[0]));
1193
              values.sals[0].symtab = sym_symtab;
1194
              values.sals[0].line = SYMBOL_LINE (sym);
1195
              return values;
1196
            }
1197
          else
1198
            /* This can happen if it is compiled with a compiler which doesn't
1199
               put out line numbers for variables.  */
1200
            /* FIXME: Shouldn't we just set .line and .symtab to zero
1201
               and return?  For example, "info line foo" could print
1202
               the address.  */
1203
            error ("Line number not known for symbol \"%s\"", copy);
1204
        }
1205
    }
1206
 
1207
  msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1208
 
1209
minimal_symbol_found:           /* We also jump here from the case for variables
1210
                                   that begin with '$' */
1211
 
1212
  if (msymbol != NULL)
1213
    {
1214
      values.sals = (struct symtab_and_line *)
1215
        xmalloc (sizeof (struct symtab_and_line));
1216
      values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
1217
                                          (struct sec *) 0, 0);
1218
      values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
1219
      if (funfirstline)
1220
        {
1221
          values.sals[0].pc += FUNCTION_START_OFFSET;
1222
          values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
1223
        }
1224
      values.nelts = 1;
1225
      return values;
1226
    }
1227
 
1228
  if (!have_full_symbols () &&
1229
      !have_partial_symbols () && !have_minimal_symbols ())
1230
    error ("No symbol table is loaded.  Use the \"file\" command.");
1231
 
1232
  error ("Function \"%s\" not defined.", copy);
1233
  return values;                /* for lint */
1234
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.