1 /* Data structures and API for event locations in GDB.
2    Copyright (C) 2013-2021 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18 
19 #include "defs.h"
20 #include "gdbsupport/gdb_assert.h"
21 #include "location.h"
22 #include "symtab.h"
23 #include "language.h"
24 #include "linespec.h"
25 #include "cli/cli-utils.h"
26 #include "probe.h"
27 #include "cp-support.h"
28 
29 #include <ctype.h>
30 #include <string.h>
31 
32 /* An event location used to set a stop event in the inferior.
33    This structure is an amalgam of the various ways
34    to specify where a stop event should be set.  */
35 
36 struct event_location
37 {
38   /* The type of this breakpoint specification.  */
39   enum event_location_type type;
40 #define EL_TYPE(P) (P)->type
41 
42   union
43   {
44     /* A probe.  */
45     char *addr_string;
46 #define EL_PROBE(P) ((P)->u.addr_string)
47 
48     /* A "normal" linespec.  */
49     struct linespec_location linespec_location;
50 #define EL_LINESPEC(P) (&(P)->u.linespec_location)
51 
52     /* An address in the inferior.  */
53     CORE_ADDR address;
54 #define EL_ADDRESS(P) (P)->u.address
55 
56     /* An explicit location.  */
57     struct explicit_location explicit_loc;
58 #define EL_EXPLICIT(P) (&((P)->u.explicit_loc))
59   } u;
60 
61   /* Cached string representation of this location.  This is used, e.g., to
62      save stop event locations to file.  Malloc'd.  */
63   char *as_string;
64 #define EL_STRING(P) ((P)->as_string)
65 };
66 
67 /* See description in location.h.  */
68 
69 enum event_location_type
event_location_type(const struct event_location * location)70 event_location_type (const struct event_location *location)
71 {
72   return EL_TYPE (location);
73 }
74 
75 /* See description in location.h.  */
76 
77 void
initialize_explicit_location(struct explicit_location * explicit_loc)78 initialize_explicit_location (struct explicit_location *explicit_loc)
79 {
80   memset (explicit_loc, 0, sizeof (struct explicit_location));
81   explicit_loc->line_offset.sign = LINE_OFFSET_UNKNOWN;
82   explicit_loc->func_name_match_type = symbol_name_match_type::WILD;
83 }
84 
85 /* See description in location.h.  */
86 
87 event_location_up
new_linespec_location(const char ** linespec,symbol_name_match_type match_type)88 new_linespec_location (const char **linespec,
89 		       symbol_name_match_type match_type)
90 {
91   struct event_location *location;
92 
93   location = XCNEW (struct event_location);
94   EL_TYPE (location) = LINESPEC_LOCATION;
95   EL_LINESPEC (location)->match_type = match_type;
96   if (*linespec != NULL)
97     {
98       const char *p;
99       const char *orig = *linespec;
100 
101       linespec_lex_to_end (linespec);
102       p = remove_trailing_whitespace (orig, *linespec);
103       if ((p - orig) > 0)
104 	EL_LINESPEC (location)->spec_string = savestring (orig, p - orig);
105     }
106   return event_location_up (location);
107 }
108 
109 /* See description in location.h.  */
110 
111 const linespec_location *
get_linespec_location(const struct event_location * location)112 get_linespec_location (const struct event_location *location)
113 {
114   gdb_assert (EL_TYPE (location) == LINESPEC_LOCATION);
115   return EL_LINESPEC (location);
116 }
117 
118 /* See description in location.h.  */
119 
120 event_location_up
new_address_location(CORE_ADDR addr,const char * addr_string,int addr_string_len)121 new_address_location (CORE_ADDR addr, const char *addr_string,
122 		      int addr_string_len)
123 {
124   struct event_location *location;
125 
126   location = XCNEW (struct event_location);
127   EL_TYPE (location) = ADDRESS_LOCATION;
128   EL_ADDRESS (location) = addr;
129   if (addr_string != NULL)
130     EL_STRING (location) = xstrndup (addr_string, addr_string_len);
131   return event_location_up (location);
132 }
133 
134 /* See description in location.h.  */
135 
136 CORE_ADDR
get_address_location(const struct event_location * location)137 get_address_location (const struct event_location *location)
138 {
139   gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
140   return EL_ADDRESS (location);
141 }
142 
143 /* See description in location.h.  */
144 
145 const char *
get_address_string_location(const struct event_location * location)146 get_address_string_location (const struct event_location *location)
147 {
148   gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
149   return EL_STRING (location);
150 }
151 
152 /* See description in location.h.  */
153 
154 event_location_up
new_probe_location(const char * probe)155 new_probe_location (const char *probe)
156 {
157   struct event_location *location;
158 
159   location = XCNEW (struct event_location);
160   EL_TYPE (location) = PROBE_LOCATION;
161   if (probe != NULL)
162     EL_PROBE (location) = xstrdup (probe);
163   return event_location_up (location);
164 }
165 
166 /* See description in location.h.  */
167 
168 const char *
get_probe_location(const struct event_location * location)169 get_probe_location (const struct event_location *location)
170 {
171   gdb_assert (EL_TYPE (location) == PROBE_LOCATION);
172   return EL_PROBE (location);
173 }
174 
175 /* See description in location.h.  */
176 
177 event_location_up
new_explicit_location(const struct explicit_location * explicit_loc)178 new_explicit_location (const struct explicit_location *explicit_loc)
179 {
180   struct event_location tmp;
181 
182   memset (&tmp, 0, sizeof (struct event_location));
183   EL_TYPE (&tmp) = EXPLICIT_LOCATION;
184   initialize_explicit_location (EL_EXPLICIT (&tmp));
185   if (explicit_loc != NULL)
186     {
187       EL_EXPLICIT (&tmp)->func_name_match_type
188 	= explicit_loc->func_name_match_type;
189 
190       if (explicit_loc->source_filename != NULL)
191 	{
192 	  EL_EXPLICIT (&tmp)->source_filename
193 	    = explicit_loc->source_filename;
194 	}
195 
196       if (explicit_loc->function_name != NULL)
197 	EL_EXPLICIT (&tmp)->function_name
198 	  = explicit_loc->function_name;
199 
200       if (explicit_loc->label_name != NULL)
201 	EL_EXPLICIT (&tmp)->label_name = explicit_loc->label_name;
202 
203       if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
204 	EL_EXPLICIT (&tmp)->line_offset = explicit_loc->line_offset;
205     }
206 
207   return copy_event_location (&tmp);
208 }
209 
210 /* See description in location.h.  */
211 
212 struct explicit_location *
get_explicit_location(struct event_location * location)213 get_explicit_location (struct event_location *location)
214 {
215   gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
216   return EL_EXPLICIT (location);
217 }
218 
219 /* See description in location.h.  */
220 
221 const struct explicit_location *
get_explicit_location_const(const struct event_location * location)222 get_explicit_location_const (const struct event_location *location)
223 {
224   gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
225   return EL_EXPLICIT (location);
226 }
227 
228 /* This convenience function returns a malloc'd string which
229    represents the location in EXPLICIT_LOC.
230 
231    AS_LINESPEC is non-zero if this string should be a linespec.
232    Otherwise it will be output in explicit form.  */
233 
234 static char *
explicit_to_string_internal(int as_linespec,const struct explicit_location * explicit_loc)235 explicit_to_string_internal (int as_linespec,
236 			     const struct explicit_location *explicit_loc)
237 {
238   int need_space = 0;
239   char space = as_linespec ? ':' : ' ';
240   string_file buf;
241 
242   if (explicit_loc->source_filename != NULL)
243     {
244       if (!as_linespec)
245 	buf.puts ("-source ");
246       buf.puts (explicit_loc->source_filename);
247       need_space = 1;
248     }
249 
250   if (explicit_loc->function_name != NULL)
251     {
252       if (need_space)
253 	buf.putc (space);
254       if (explicit_loc->func_name_match_type == symbol_name_match_type::FULL)
255 	buf.puts ("-qualified ");
256       if (!as_linespec)
257 	buf.puts ("-function ");
258       buf.puts (explicit_loc->function_name);
259       need_space = 1;
260     }
261 
262   if (explicit_loc->label_name != NULL)
263     {
264       if (need_space)
265 	buf.putc (space);
266       if (!as_linespec)
267 	buf.puts ("-label ");
268       buf.puts (explicit_loc->label_name);
269       need_space = 1;
270     }
271 
272   if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
273     {
274       if (need_space)
275 	buf.putc (space);
276       if (!as_linespec)
277 	buf.puts ("-line ");
278       buf.printf ("%s%d",
279 		  (explicit_loc->line_offset.sign == LINE_OFFSET_NONE ? ""
280 		   : (explicit_loc->line_offset.sign
281 		      == LINE_OFFSET_PLUS ? "+" : "-")),
282 		  explicit_loc->line_offset.offset);
283     }
284 
285   return xstrdup (buf.c_str ());
286 }
287 
288 /* See description in location.h.  */
289 
290 char *
explicit_location_to_string(const struct explicit_location * explicit_loc)291 explicit_location_to_string (const struct explicit_location *explicit_loc)
292 {
293   return explicit_to_string_internal (0, explicit_loc);
294 }
295 
296 /* See description in location.h.  */
297 
298 char *
explicit_location_to_linespec(const struct explicit_location * explicit_loc)299 explicit_location_to_linespec (const struct explicit_location *explicit_loc)
300 {
301   return explicit_to_string_internal (1, explicit_loc);
302 }
303 
304 /* See description in location.h.  */
305 
306 event_location_up
copy_event_location(const struct event_location * src)307 copy_event_location (const struct event_location *src)
308 {
309   struct event_location *dst;
310 
311   dst = XCNEW (struct event_location);
312   EL_TYPE (dst) = EL_TYPE (src);
313   if (EL_STRING (src) != NULL)
314     EL_STRING (dst) = xstrdup (EL_STRING (src));
315 
316   switch (EL_TYPE (src))
317     {
318     case LINESPEC_LOCATION:
319       EL_LINESPEC (dst)->match_type = EL_LINESPEC (src)->match_type;
320       if (EL_LINESPEC (src)->spec_string != NULL)
321 	EL_LINESPEC (dst)->spec_string
322 	  = xstrdup (EL_LINESPEC (src)->spec_string);
323       break;
324 
325     case ADDRESS_LOCATION:
326       EL_ADDRESS (dst) = EL_ADDRESS (src);
327       break;
328 
329     case EXPLICIT_LOCATION:
330       EL_EXPLICIT (dst)->func_name_match_type
331 	= EL_EXPLICIT (src)->func_name_match_type;
332       if (EL_EXPLICIT (src)->source_filename != NULL)
333 	EL_EXPLICIT (dst)->source_filename
334 	  = xstrdup (EL_EXPLICIT (src)->source_filename);
335 
336       if (EL_EXPLICIT (src)->function_name != NULL)
337 	EL_EXPLICIT (dst)->function_name
338 	  = xstrdup (EL_EXPLICIT (src)->function_name);
339 
340       if (EL_EXPLICIT (src)->label_name != NULL)
341 	EL_EXPLICIT (dst)->label_name = xstrdup (EL_EXPLICIT (src)->label_name);
342 
343       EL_EXPLICIT (dst)->line_offset = EL_EXPLICIT (src)->line_offset;
344       break;
345 
346 
347     case PROBE_LOCATION:
348       if (EL_PROBE (src) != NULL)
349 	EL_PROBE (dst) = xstrdup (EL_PROBE (src));
350       break;
351 
352     default:
353       gdb_assert_not_reached ("unknown event location type");
354     }
355 
356   return event_location_up (dst);
357 }
358 
359 void
operator()360 event_location_deleter::operator() (event_location *location) const
361 {
362   if (location != NULL)
363     {
364       xfree (EL_STRING (location));
365 
366       switch (EL_TYPE (location))
367 	{
368 	case LINESPEC_LOCATION:
369 	  xfree (EL_LINESPEC (location)->spec_string);
370 	  break;
371 
372 	case ADDRESS_LOCATION:
373 	  /* Nothing to do.  */
374 	  break;
375 
376 	case EXPLICIT_LOCATION:
377 	  xfree (EL_EXPLICIT (location)->source_filename);
378 	  xfree (EL_EXPLICIT (location)->function_name);
379 	  xfree (EL_EXPLICIT (location)->label_name);
380 	  break;
381 
382 	case PROBE_LOCATION:
383 	  xfree (EL_PROBE (location));
384 	  break;
385 
386 	default:
387 	  gdb_assert_not_reached ("unknown event location type");
388 	}
389 
390       xfree (location);
391     }
392 }
393 
394 /* See description in location.h.  */
395 
396 const char *
event_location_to_string(struct event_location * location)397 event_location_to_string (struct event_location *location)
398 {
399   if (EL_STRING (location) == NULL)
400     {
401       switch (EL_TYPE (location))
402 	{
403 	case LINESPEC_LOCATION:
404 	  if (EL_LINESPEC (location)->spec_string != NULL)
405 	    {
406 	      linespec_location *ls = EL_LINESPEC (location);
407 	      if (ls->match_type == symbol_name_match_type::FULL)
408 		{
409 		  EL_STRING (location)
410 		    = concat ("-qualified ", ls->spec_string, (char *) NULL);
411 		}
412 	      else
413 		EL_STRING (location) = xstrdup (ls->spec_string);
414 	    }
415 	  break;
416 
417 	case ADDRESS_LOCATION:
418 	  EL_STRING (location)
419 	    = xstrprintf ("*%s",
420 			  core_addr_to_string (EL_ADDRESS (location)));
421 	  break;
422 
423 	case EXPLICIT_LOCATION:
424 	  EL_STRING (location)
425 	    = explicit_location_to_string (EL_EXPLICIT (location));
426 	  break;
427 
428 	case PROBE_LOCATION:
429 	  EL_STRING (location) = xstrdup (EL_PROBE (location));
430 	  break;
431 
432 	default:
433 	  gdb_assert_not_reached ("unknown event location type");
434 	}
435     }
436 
437   return EL_STRING (location);
438 }
439 
440 /* Find an instance of the quote character C in the string S that is
441    outside of all single- and double-quoted strings (i.e., any quoting
442    other than C).  */
443 
444 static const char *
find_end_quote(const char * s,char end_quote_char)445 find_end_quote (const char *s, char end_quote_char)
446 {
447   /* zero if we're not in quotes;
448      '"' if we're in a double-quoted string;
449      '\'' if we're in a single-quoted string.  */
450   char nested_quote_char = '\0';
451 
452   for (const char *scan = s; *scan != '\0'; scan++)
453     {
454       if (nested_quote_char != '\0')
455 	{
456 	  if (*scan == nested_quote_char)
457 	    nested_quote_char = '\0';
458 	  else if (scan[0] == '\\' && *(scan + 1) != '\0')
459 	    scan++;
460 	}
461       else if (*scan == end_quote_char && nested_quote_char == '\0')
462 	return scan;
463       else if (*scan == '"' || *scan == '\'')
464 	nested_quote_char = *scan;
465     }
466 
467   return 0;
468 }
469 
470 /* A lexer for explicit locations.  This function will advance INP
471    past any strings that it lexes.  Returns a malloc'd copy of the
472    lexed string or NULL if no lexing was done.  */
473 
474 static gdb::unique_xmalloc_ptr<char>
explicit_location_lex_one(const char ** inp,const struct language_defn * language,explicit_completion_info * completion_info)475 explicit_location_lex_one (const char **inp,
476 			   const struct language_defn *language,
477 			   explicit_completion_info *completion_info)
478 {
479   const char *start = *inp;
480 
481   if (*start == '\0')
482     return NULL;
483 
484   /* If quoted, skip to the ending quote.  */
485   if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
486     {
487       if (completion_info != NULL)
488 	completion_info->quoted_arg_start = start;
489 
490       const char *end = find_end_quote (start + 1, *start);
491 
492       if (end == NULL)
493 	{
494 	  if (completion_info == NULL)
495 	    error (_("Unmatched quote, %s."), start);
496 
497 	  end = start + strlen (start);
498 	  *inp = end;
499 	  return gdb::unique_xmalloc_ptr<char> (savestring (start + 1,
500 							    *inp - start - 1));
501 	}
502 
503       if (completion_info != NULL)
504 	completion_info->quoted_arg_end = end;
505       *inp = end + 1;
506       return gdb::unique_xmalloc_ptr<char> (savestring (start + 1,
507 							*inp - start - 2));
508     }
509 
510   /* If the input starts with '-' or '+', the string ends with the next
511      whitespace or comma.  */
512   if (*start == '-' || *start == '+')
513     {
514       while (*inp[0] != '\0' && *inp[0] != ',' && !isspace (*inp[0]))
515 	++(*inp);
516     }
517   else
518     {
519       /* Handle numbers first, stopping at the next whitespace or ','.  */
520       while (isdigit (*inp[0]))
521 	++(*inp);
522       if (*inp[0] == '\0' || isspace (*inp[0]) || *inp[0] == ',')
523 	return gdb::unique_xmalloc_ptr<char> (savestring (start,
524 							  *inp - start));
525 
526       /* Otherwise stop at the next occurrence of whitespace, '\0',
527 	 keyword, or ','.  */
528       *inp = start;
529       while ((*inp)[0]
530 	     && (*inp)[0] != ','
531 	     && !(isspace ((*inp)[0])
532 		  || linespec_lexer_lex_keyword (&(*inp)[1])))
533 	{
534 	  /* Special case: C++ operator,.  */
535 	  if (language->la_language == language_cplus
536 	      && startswith (*inp, CP_OPERATOR_STR))
537 	    (*inp) += CP_OPERATOR_LEN;
538 	  ++(*inp);
539 	}
540     }
541 
542   if (*inp - start > 0)
543     return gdb::unique_xmalloc_ptr<char> (savestring (start, *inp - start));
544 
545   return NULL;
546 }
547 
548 /* Return true if COMMA points past "operator".  START is the start of
549    the line that COMMAND points to, hence when reading backwards, we
550    must not read any character before START.  */
551 
552 static bool
is_cp_operator(const char * start,const char * comma)553 is_cp_operator (const char *start, const char *comma)
554 {
555   if (comma != NULL
556       && (comma - start) >= CP_OPERATOR_LEN)
557     {
558       const char *p = comma;
559 
560       while (p > start && isspace (p[-1]))
561 	p--;
562       if (p - start >= CP_OPERATOR_LEN)
563 	{
564 	  p -= CP_OPERATOR_LEN;
565 	  if (strncmp (p, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
566 	      && (p == start
567 		  || !(isalnum (p[-1]) || p[-1] == '_')))
568 	    {
569 	      return true;
570 	    }
571 	}
572     }
573   return false;
574 }
575 
576 /* When scanning the input string looking for the next explicit
577    location option/delimiter, we jump to the next option by looking
578    for ",", and "-".  Such a character can also appear in C++ symbols
579    like "operator," and "operator-".  So when we find such a
580    character, we call this function to check if we found such a
581    symbol, meaning we had a false positive for an option string.  In
582    that case, we keep looking for the next delimiter, until we find
583    one that is not a false positive, or we reach end of string.  FOUND
584    is the character that scanning found (either '-' or ','), and START
585    is the start of the line that FOUND points to, hence when reading
586    backwards, we must not read any character before START.  Returns a
587    pointer to the next non-false-positive delimiter character, or NULL
588    if none was found.  */
589 
590 static const char *
skip_op_false_positives(const char * start,const char * found)591 skip_op_false_positives (const char *start, const char *found)
592 {
593   while (found != NULL && is_cp_operator (start, found))
594     {
595       if (found[0] == '-' && found[1] == '-')
596 	start = found + 2;
597       else
598 	start = found + 1;
599       found = find_toplevel_char (start, *found);
600     }
601 
602   return found;
603 }
604 
605 /* Assuming both FIRST and NEW_TOK point into the same string, return
606    the pointer that is closer to the start of the string.  If FIRST is
607    NULL, returns NEW_TOK.  If NEW_TOK is NULL, returns FIRST.  */
608 
609 static const char *
first_of(const char * first,const char * new_tok)610 first_of (const char *first, const char *new_tok)
611 {
612   if (first == NULL)
613     return new_tok;
614   else if (new_tok != NULL && new_tok < first)
615     return new_tok;
616   else
617     return first;
618 }
619 
620 /* A lexer for functions in explicit locations.  This function will
621    advance INP past a function until the next option, or until end of
622    string.  Returns a malloc'd copy of the lexed string or NULL if no
623    lexing was done.  */
624 
625 static gdb::unique_xmalloc_ptr<char>
explicit_location_lex_one_function(const char ** inp,const struct language_defn * language,explicit_completion_info * completion_info)626 explicit_location_lex_one_function (const char **inp,
627 				    const struct language_defn *language,
628 				    explicit_completion_info *completion_info)
629 {
630   const char *start = *inp;
631 
632   if (*start == '\0')
633     return NULL;
634 
635   /* If quoted, skip to the ending quote.  */
636   if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
637     {
638       char quote_char = *start;
639 
640       /* If the input is not an Ada operator, skip to the matching
641 	 closing quote and return the string.  */
642       if (!(language->la_language == language_ada
643 	    && quote_char == '\"' && is_ada_operator (start)))
644 	{
645 	  if (completion_info != NULL)
646 	    completion_info->quoted_arg_start = start;
647 
648 	  const char *end = find_toplevel_char (start + 1, quote_char);
649 
650 	  if (end == NULL)
651 	    {
652 	      if (completion_info == NULL)
653 		error (_("Unmatched quote, %s."), start);
654 
655 	      end = start + strlen (start);
656 	      *inp = end;
657 	      char *saved = savestring (start + 1, *inp - start - 1);
658 	      return gdb::unique_xmalloc_ptr<char> (saved);
659 	    }
660 
661 	  if (completion_info != NULL)
662 	    completion_info->quoted_arg_end = end;
663 	  *inp = end + 1;
664 	  char *saved = savestring (start + 1, *inp - start - 2);
665 	  return gdb::unique_xmalloc_ptr<char> (saved);
666 	}
667     }
668 
669   const char *comma = find_toplevel_char (start, ',');
670 
671   /* If we have "-function -myfunction", or perhaps better example,
672      "-function -[BasicClass doIt]" (objc selector), treat
673      "-myfunction" as the function name.  I.e., skip the first char if
674      it is an hyphen.  Don't skip the first char always, because we
675      may have C++ "operator<", and find_toplevel_char needs to see the
676      'o' in that case.  */
677   const char *hyphen
678     = (*start == '-'
679        ? find_toplevel_char (start + 1, '-')
680        : find_toplevel_char (start, '-'));
681 
682   /* Check for C++ "operator," and "operator-".  */
683   comma = skip_op_false_positives (start, comma);
684   hyphen = skip_op_false_positives (start, hyphen);
685 
686   /* Pick the one that appears first.  */
687   const char *end = first_of (hyphen, comma);
688 
689   /* See if a linespec keyword appears first.  */
690   const char *s = start;
691   const char *ws = find_toplevel_char (start, ' ');
692   while (ws != NULL && linespec_lexer_lex_keyword (ws + 1) == NULL)
693     {
694       s = ws + 1;
695       ws = find_toplevel_char (s, ' ');
696     }
697   if (ws != NULL)
698     end = first_of (end, ws + 1);
699 
700   /* If we don't have any terminator, then take the whole string.  */
701   if (end == NULL)
702     end = start + strlen (start);
703 
704   /* Trim whitespace at the end.  */
705   while (end > start && end[-1] == ' ')
706     end--;
707 
708   *inp = end;
709 
710   if (*inp - start > 0)
711     return gdb::unique_xmalloc_ptr<char> (savestring (start, *inp - start));
712 
713   return NULL;
714 }
715 
716 /* See description in location.h.  */
717 
718 event_location_up
string_to_explicit_location(const char ** argp,const struct language_defn * language,explicit_completion_info * completion_info)719 string_to_explicit_location (const char **argp,
720 			     const struct language_defn *language,
721 			     explicit_completion_info *completion_info)
722 {
723   event_location_up location;
724 
725   /* It is assumed that input beginning with '-' and a non-digit
726      character is an explicit location.  "-p" is reserved, though,
727      for probe locations.  */
728   if (argp == NULL
729       || *argp == NULL
730       || *argp[0] != '-'
731       || !isalpha ((*argp)[1])
732       || ((*argp)[0] == '-' && (*argp)[1] == 'p'))
733     return NULL;
734 
735   location = new_explicit_location (NULL);
736 
737   /* Process option/argument pairs.  dprintf_command
738      requires that processing stop on ','.  */
739   while ((*argp)[0] != '\0' && (*argp)[0] != ',')
740     {
741       int len;
742       const char *start;
743 
744       /* Clear these on each iteration, since they should be filled
745 	 with info about the last option.  */
746       if (completion_info != NULL)
747 	{
748 	  completion_info->quoted_arg_start = NULL;
749 	  completion_info->quoted_arg_end = NULL;
750 	}
751 
752       /* If *ARGP starts with a keyword, stop processing
753 	 options.  */
754       if (linespec_lexer_lex_keyword (*argp) != NULL)
755 	break;
756 
757       /* Mark the start of the string in case we need to rewind.  */
758       start = *argp;
759 
760       if (completion_info != NULL)
761 	completion_info->last_option = start;
762 
763       /* Get the option string.  */
764       gdb::unique_xmalloc_ptr<char> opt
765 	= explicit_location_lex_one (argp, language, NULL);
766 
767       /* Use the length of the option to allow abbreviations.  */
768       len = strlen (opt.get ());
769 
770       /* Get the argument string.  */
771       *argp = skip_spaces (*argp);
772 
773       /* All options have a required argument.  Checking for this
774 	 required argument is deferred until later.  */
775       gdb::unique_xmalloc_ptr<char> oarg;
776       /* True if we have an argument.  This is required because we'll
777 	 move from OARG before checking whether we have an
778 	 argument.  */
779       bool have_oarg = false;
780 
781       /* True if the option needs an argument.  */
782       bool need_oarg = false;
783 
784       /* Convenience to consistently set both OARG/HAVE_OARG from
785 	 ARG.  */
786       auto set_oarg = [&] (gdb::unique_xmalloc_ptr<char> arg)
787 	{
788 	  if (completion_info != NULL)
789 	    {
790 	      /* We do this here because the set of options that take
791 		 arguments matches the set of explicit location
792 		 options.  */
793 	      completion_info->saw_explicit_location_option = true;
794 	    }
795 	  oarg = std::move (arg);
796 	  have_oarg = oarg != NULL;
797 	  need_oarg = true;
798 	};
799 
800       if (strncmp (opt.get (), "-source", len) == 0)
801 	{
802 	  set_oarg (explicit_location_lex_one (argp, language,
803 					       completion_info));
804 	  EL_EXPLICIT (location)->source_filename = oarg.release ();
805 	}
806       else if (strncmp (opt.get (), "-function", len) == 0)
807 	{
808 	  set_oarg (explicit_location_lex_one_function (argp, language,
809 							completion_info));
810 	  EL_EXPLICIT (location)->function_name = oarg.release ();
811 	}
812       else if (strncmp (opt.get (), "-qualified", len) == 0)
813 	{
814 	  EL_EXPLICIT (location)->func_name_match_type
815 	    = symbol_name_match_type::FULL;
816 	}
817       else if (strncmp (opt.get (), "-line", len) == 0)
818 	{
819 	  set_oarg (explicit_location_lex_one (argp, language, NULL));
820 	  *argp = skip_spaces (*argp);
821 	  if (have_oarg)
822 	    {
823 	      EL_EXPLICIT (location)->line_offset
824 		= linespec_parse_line_offset (oarg.get ());
825 	      continue;
826 	    }
827 	}
828       else if (strncmp (opt.get (), "-label", len) == 0)
829 	{
830 	  set_oarg (explicit_location_lex_one (argp, language, completion_info));
831 	  EL_EXPLICIT (location)->label_name = oarg.release ();
832 	}
833       /* Only emit an "invalid argument" error for options
834 	 that look like option strings.  */
835       else if (opt.get ()[0] == '-' && !isdigit (opt.get ()[1]))
836 	{
837 	  if (completion_info == NULL)
838 	    error (_("invalid explicit location argument, \"%s\""), opt.get ());
839 	}
840       else
841 	{
842 	  /* End of the explicit location specification.
843 	     Stop parsing and return whatever explicit location was
844 	     parsed.  */
845 	  *argp = start;
846 	  break;
847 	}
848 
849       *argp = skip_spaces (*argp);
850 
851       /* It's a little lame to error after the fact, but in this
852 	 case, it provides a much better user experience to issue
853 	 the "invalid argument" error before any missing
854 	 argument error.  */
855       if (need_oarg && !have_oarg && completion_info == NULL)
856 	error (_("missing argument for \"%s\""), opt.get ());
857     }
858 
859   /* One special error check:  If a source filename was given
860      without offset, function, or label, issue an error.  */
861   if (EL_EXPLICIT (location)->source_filename != NULL
862       && EL_EXPLICIT (location)->function_name == NULL
863       && EL_EXPLICIT (location)->label_name == NULL
864       && (EL_EXPLICIT (location)->line_offset.sign == LINE_OFFSET_UNKNOWN)
865       && completion_info == NULL)
866     {
867       error (_("Source filename requires function, label, or "
868 	       "line offset."));
869     }
870 
871   return location;
872 }
873 
874 /* See description in location.h.  */
875 
876 event_location_up
string_to_event_location_basic(const char ** stringp,const struct language_defn * language,symbol_name_match_type match_type)877 string_to_event_location_basic (const char **stringp,
878 				const struct language_defn *language,
879 				symbol_name_match_type match_type)
880 {
881   event_location_up location;
882   const char *cs;
883 
884   /* Try the input as a probe spec.  */
885   cs = *stringp;
886   if (cs != NULL && probe_linespec_to_static_ops (&cs) != NULL)
887     {
888       location = new_probe_location (*stringp);
889       *stringp += strlen (*stringp);
890     }
891   else
892     {
893       /* Try an address location.  */
894       if (*stringp != NULL && **stringp == '*')
895 	{
896 	  const char *arg, *orig;
897 	  CORE_ADDR addr;
898 
899 	  orig = arg = *stringp;
900 	  addr = linespec_expression_to_pc (&arg);
901 	  location = new_address_location (addr, orig, arg - orig);
902 	  *stringp += arg - orig;
903 	}
904       else
905 	{
906 	  /* Everything else is a linespec.  */
907 	  location = new_linespec_location (stringp, match_type);
908 	}
909     }
910 
911   return location;
912 }
913 
914 /* See description in location.h.  */
915 
916 event_location_up
string_to_event_location(const char ** stringp,const struct language_defn * language,symbol_name_match_type match_type)917 string_to_event_location (const char **stringp,
918 			  const struct language_defn *language,
919 			  symbol_name_match_type match_type)
920 {
921   const char *arg, *orig;
922 
923   /* Try an explicit location.  */
924   orig = arg = *stringp;
925   event_location_up location = string_to_explicit_location (&arg, language, NULL);
926   if (location != NULL)
927     {
928       /* It was a valid explicit location.  Advance STRINGP to
929 	 the end of input.  */
930       *stringp += arg - orig;
931 
932       /* If the user really specified a location, then we're done.  */
933       if (!event_location_empty_p (location.get ()))
934 	return location;
935 
936       /* Otherwise, the user _only_ specified optional flags like
937 	 "-qualified", otherwise string_to_explicit_location would
938 	 have thrown an error.  Save the flags for "basic" linespec
939 	 parsing below and discard the explicit location.  */
940       match_type = EL_EXPLICIT (location)->func_name_match_type;
941     }
942 
943   /* Everything else is a "basic" linespec, address, or probe
944      location.  */
945   return string_to_event_location_basic (stringp, language, match_type);
946 }
947 
948 /* See description in location.h.  */
949 
950 int
event_location_empty_p(const struct event_location * location)951 event_location_empty_p (const struct event_location *location)
952 {
953   switch (EL_TYPE (location))
954     {
955     case LINESPEC_LOCATION:
956       /* Linespecs are never "empty."  (NULL is a valid linespec)  */
957       return 0;
958 
959     case ADDRESS_LOCATION:
960       return 0;
961 
962     case EXPLICIT_LOCATION:
963       return (EL_EXPLICIT (location) == NULL
964 	      || (EL_EXPLICIT (location)->source_filename == NULL
965 		  && EL_EXPLICIT (location)->function_name == NULL
966 		  && EL_EXPLICIT (location)->label_name == NULL
967 		  && (EL_EXPLICIT (location)->line_offset.sign
968 		      == LINE_OFFSET_UNKNOWN)));
969 
970     case PROBE_LOCATION:
971       return EL_PROBE (location) == NULL;
972 
973     default:
974       gdb_assert_not_reached ("unknown event location type");
975     }
976 }
977 
978 /* See description in location.h.  */
979 
980 void
set_event_location_string(struct event_location * location,const char * string)981 set_event_location_string (struct event_location *location,
982 			   const char *string)
983 {
984   xfree (EL_STRING (location));
985   EL_STRING (location) = string == NULL ?  NULL : xstrdup (string);
986 }
987