1 /* Parse expressions for GDB.
2 
3    Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 
5    Modified from expread.y by the Department of Computer Science at the
6    State University of New York at Buffalo, 1991.
7 
8    This file is part of GDB.
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22 
23 /* Parse an expression from text in a string,
24    and return the result as a struct expression pointer.
25    That structure contains arithmetic operations in reverse polish,
26    with constants represented by operations that are followed by special data.
27    See expression.h for the details of the format.
28    What is important here is that it can be built up sequentially
29    during the process of parsing; the lower levels of the tree always
30    come first in the result.  */
31 
32 #include <ctype.h>
33 #include "arch-utils.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "frame.h"
37 #include "expression.h"
38 #include "value.h"
39 #include "command.h"
40 #include "language.h"
41 #include "parser-defs.h"
42 #include "cli/cli-cmds.h"
43 #include "symfile.h"
44 #include "inferior.h"
45 #include "target-float.h"
46 #include "block.h"
47 #include "source.h"
48 #include "objfiles.h"
49 #include "user-regs.h"
50 #include <algorithm>
51 #include <optional>
52 #include "c-exp.h"
53 
54 static unsigned int expressiondebug = 0;
55 static void
show_expressiondebug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)56 show_expressiondebug (struct ui_file *file, int from_tty,
57                           struct cmd_list_element *c, const char *value)
58 {
59   gdb_printf (file, _("Expression debugging is %s.\n"), value);
60 }
61 
62 
63 /* True if an expression parser should set yydebug.  */
64 static bool parser_debug;
65 
66 static void
show_parserdebug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)67 show_parserdebug (struct ui_file *file, int from_tty,
68                       struct cmd_list_element *c, const char *value)
69 {
70   gdb_printf (file, _("Parser debugging is %s.\n"), value);
71 }
72 
73 
74 /* Documented at it's declaration.  */
75 
76 void
update(const struct block * b,innermost_block_tracker_types t)77 innermost_block_tracker::update (const struct block *b,
78                                          innermost_block_tracker_types t)
79 {
80   if ((m_types & t) != 0
81       && (m_innermost_block == NULL
82             || m_innermost_block->contains (b)))
83     m_innermost_block = b;
84 }
85 
86 
87 
88 bool
complete(struct expression * exp,completion_tracker & tracker)89 expr_complete_tag::complete (struct expression *exp,
90                                    completion_tracker &tracker)
91 {
92   collect_symbol_completion_matches_type (tracker, m_name.get (),
93                                                     m_name.get (), m_code);
94   return true;
95 }
96 
97 /* See parser-defs.h.  */
98 
99 void
mark_struct_expression(expr::structop_base_operation * op)100 parser_state::mark_struct_expression (expr::structop_base_operation *op)
101 {
102   gdb_assert (parse_completion && m_completion_state == nullptr);
103   m_completion_state.reset (new expr_complete_structop (op));
104 }
105 
106 /* Indicate that the current parser invocation is completing a tag.
107    TAG is the type code of the tag, and PTR and LENGTH represent the
108    start of the tag name.  */
109 
110 void
mark_completion_tag(enum type_code tag,const char * ptr,int length)111 parser_state::mark_completion_tag (enum type_code tag, const char *ptr,
112                                            int length)
113 {
114   gdb_assert (parse_completion && m_completion_state == nullptr);
115   gdb_assert (tag == TYPE_CODE_UNION
116                 || tag == TYPE_CODE_STRUCT
117                 || tag == TYPE_CODE_ENUM);
118   m_completion_state.reset
119     (new expr_complete_tag (tag, make_unique_xstrndup (ptr, length)));
120 }
121 
122 /* See parser-defs.h.  */
123 
124 void
push_c_string(int kind,struct stoken_vector * vec)125 parser_state::push_c_string (int kind, struct stoken_vector *vec)
126 {
127   std::vector<std::string> data (vec->len);
128   for (int i = 0; i < vec->len; ++i)
129     data[i] = std::string (vec->tokens[i].ptr, vec->tokens[i].length);
130 
131   push_new<expr::c_string_operation> ((enum c_string_type_values) kind,
132                                               std::move (data));
133 }
134 
135 /* See parser-defs.h.  */
136 
137 void
push_symbol(const char * name,block_symbol sym)138 parser_state::push_symbol (const char *name, block_symbol sym)
139 {
140   if (sym.symbol != nullptr)
141     {
142       if (symbol_read_needs_frame (sym.symbol))
143           block_tracker->update (sym);
144       push_new<expr::var_value_operation> (sym);
145     }
146   else
147     {
148       struct bound_minimal_symbol msymbol = lookup_bound_minimal_symbol (name);
149       if (msymbol.minsym != NULL)
150           push_new<expr::var_msym_value_operation> (msymbol);
151       else if (!have_full_symbols () && !have_partial_symbols ())
152           error (_("No symbol table is loaded.  Use the \"file\" command."));
153       else
154           error (_("No symbol \"%s\" in current context."), name);
155     }
156 }
157 
158 /* See parser-defs.h.  */
159 
160 void
push_dollar(struct stoken str)161 parser_state::push_dollar (struct stoken str)
162 {
163   struct block_symbol sym;
164   struct bound_minimal_symbol msym;
165   struct internalvar *isym = NULL;
166   std::string copy;
167 
168   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
169      and $$digits (equivalent to $<-digits> if you could type that).  */
170 
171   int negate = 0;
172   int i = 1;
173   /* Double dollar means negate the number and add -1 as well.
174      Thus $$ alone means -1.  */
175   if (str.length >= 2 && str.ptr[1] == '$')
176     {
177       negate = 1;
178       i = 2;
179     }
180   if (i == str.length)
181     {
182       /* Just dollars (one or two).  */
183       i = -negate;
184       push_new<expr::last_operation> (i);
185       return;
186     }
187   /* Is the rest of the token digits?  */
188   for (; i < str.length; i++)
189     if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
190       break;
191   if (i == str.length)
192     {
193       i = atoi (str.ptr + 1 + negate);
194       if (negate)
195           i = -i;
196       push_new<expr::last_operation> (i);
197       return;
198     }
199 
200   /* Handle tokens that refer to machine registers:
201      $ followed by a register name.  */
202   i = user_reg_map_name_to_regnum (gdbarch (),
203                                            str.ptr + 1, str.length - 1);
204   if (i >= 0)
205     {
206       str.length--;
207       str.ptr++;
208       push_new<expr::register_operation> (copy_name (str));
209       block_tracker->update (expression_context_block,
210                                    INNERMOST_BLOCK_FOR_REGISTERS);
211       return;
212     }
213 
214   /* Any names starting with $ are probably debugger internal variables.  */
215 
216   copy = copy_name (str);
217   isym = lookup_only_internalvar (copy.c_str () + 1);
218   if (isym)
219     {
220       push_new<expr::internalvar_operation> (isym);
221       return;
222     }
223 
224   /* On some systems, such as HP-UX and hppa-linux, certain system routines
225      have names beginning with $ or $$.  Check for those, first.  */
226 
227   sym = lookup_symbol (copy.c_str (), nullptr,
228                            SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN, nullptr);
229   if (sym.symbol)
230     {
231       push_new<expr::var_value_operation> (sym);
232       return;
233     }
234   msym = lookup_bound_minimal_symbol (copy.c_str ());
235   if (msym.minsym)
236     {
237       push_new<expr::var_msym_value_operation> (msym);
238       return;
239     }
240 
241   /* Any other names are assumed to be debugger internal variables.  */
242 
243   push_new<expr::internalvar_operation>
244     (create_internalvar (copy.c_str () + 1));
245 }
246 
247 /* See parser-defs.h.  */
248 
249 void
parse_error(const char * msg)250 parser_state::parse_error (const char *msg)
251 {
252   if (this->prev_lexptr)
253     this->lexptr = this->prev_lexptr;
254 
255   if (*this->lexptr == '\0')
256     error (_("A %s in expression, near the end of `%s'."),
257              msg, this->start_of_input);
258   else
259     error (_("A %s in expression, near `%s'."), msg, this->lexptr);
260 }
261 
262 
263 
264 const char *
find_template_name_end(const char * p)265 find_template_name_end (const char *p)
266 {
267   int depth = 1;
268   int just_seen_right = 0;
269   int just_seen_colon = 0;
270   int just_seen_space = 0;
271 
272   if (!p || (*p != '<'))
273     return 0;
274 
275   while (*++p)
276     {
277       switch (*p)
278           {
279           case '\'':
280           case '\"':
281           case '{':
282           case '}':
283             /* In future, may want to allow these??  */
284             return 0;
285           case '<':
286             depth++;                    /* start nested template */
287             if (just_seen_colon || just_seen_right || just_seen_space)
288               return 0;                 /* but not after : or :: or > or space */
289             break;
290           case '>':
291             if (just_seen_colon || just_seen_right)
292               return 0;                 /* end a (nested?) template */
293             just_seen_right = 1;        /* but not after : or :: */
294             if (--depth == 0) /* also disallow >>, insist on > > */
295               return ++p;               /* if outermost ended, return */
296             break;
297           case ':':
298             if (just_seen_space || (just_seen_colon > 1))
299               return 0;                 /* nested class spec coming up */
300             just_seen_colon++;          /* we allow :: but not :::: */
301             break;
302           case ' ':
303             break;
304           default:
305             if (!((*p >= 'a' && *p <= 'z') ||     /* allow token chars */
306                     (*p >= 'A' && *p <= 'Z') ||
307                     (*p >= '0' && *p <= '9') ||
308                     (*p == '_') || (*p == ',') || /* commas for template args */
309                     (*p == '&') || (*p == '*') || /* pointer and ref types */
310                     (*p == '(') || (*p == ')') || /* function types */
311                     (*p == '[') || (*p == ']')))  /* array types */
312               return 0;
313           }
314       if (*p != ' ')
315           just_seen_space = 0;
316       if (*p != ':')
317           just_seen_colon = 0;
318       if (*p != '>')
319           just_seen_right = 0;
320     }
321   return 0;
322 }
323 
324 
325 /* Return a null-terminated temporary copy of the name of a string token.
326 
327    Tokens that refer to names do so with explicit pointer and length,
328    so they can share the storage that lexptr is parsing.
329    When it is necessary to pass a name to a function that expects
330    a null-terminated string, the substring is copied out
331    into a separate block of storage.  */
332 
333 std::string
copy_name(struct stoken token)334 copy_name (struct stoken token)
335 {
336   return std::string (token.ptr, token.length);
337 }
338 
339 
340 /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
341    no value is expected from the expression.  */
342 
343 static expression_up
parse_exp_in_context(const char ** stringptr,CORE_ADDR pc,const struct block * block,parser_flags flags,innermost_block_tracker * tracker,std::unique_ptr<expr_completion_base> * completer)344 parse_exp_in_context (const char **stringptr, CORE_ADDR pc,
345                           const struct block *block,
346                           parser_flags flags,
347                           innermost_block_tracker *tracker,
348                           std::unique_ptr<expr_completion_base> *completer)
349 {
350   const struct language_defn *lang = NULL;
351 
352   if (*stringptr == 0 || **stringptr == 0)
353     error_no_arg (_("expression to compute"));
354 
355   const struct block *expression_context_block = block;
356   CORE_ADDR expression_context_pc = 0;
357 
358   innermost_block_tracker local_tracker;
359   if (tracker == nullptr)
360     tracker = &local_tracker;
361 
362   if ((flags & PARSER_LEAVE_BLOCK_ALONE) == 0)
363     {
364       /* If no context specified, try using the current frame, if any.  */
365       if (!expression_context_block)
366           expression_context_block
367             = get_selected_block (&expression_context_pc);
368       else if (pc == 0)
369           expression_context_pc = expression_context_block->entry_pc ();
370       else
371           expression_context_pc = pc;
372 
373       /* Fall back to using the current source static context, if any.  */
374 
375       if (!expression_context_block)
376           {
377             struct symtab_and_line cursal
378               = get_current_source_symtab_and_line ();
379 
380             if (cursal.symtab)
381               expression_context_block
382                 = cursal.symtab->compunit ()->blockvector ()->static_block ();
383 
384             if (expression_context_block)
385               expression_context_pc = expression_context_block->entry_pc ();
386           }
387     }
388 
389   if (language_mode == language_mode_auto && block != NULL)
390     {
391       /* Find the language associated to the given context block.
392            Default to the current language if it can not be determined.
393 
394            Note that using the language corresponding to the current frame
395            can sometimes give unexpected results.  For instance, this
396            routine is often called several times during the inferior
397            startup phase to re-parse breakpoint expressions after
398            a new shared library has been loaded.  The language associated
399            to the current frame at this moment is not relevant for
400            the breakpoint.  Using it would therefore be silly, so it seems
401            better to rely on the current language rather than relying on
402            the current frame language to parse the expression.  That's why
403            we do the following language detection only if the context block
404            has been specifically provided.  */
405       struct symbol *func = block->linkage_function ();
406 
407       if (func != NULL)
408           lang = language_def (func->language ());
409       if (lang == NULL || lang->la_language == language_unknown)
410           lang = current_language;
411     }
412   else
413     lang = current_language;
414 
415   /* get_current_arch may reset CURRENT_LANGUAGE via select_frame.
416      While we need CURRENT_LANGUAGE to be set to LANG (for lookup_symbol
417      and others called from *.y) ensure CURRENT_LANGUAGE gets restored
418      to the value matching SELECTED_FRAME as set by get_current_arch.  */
419 
420   parser_state ps (lang, get_current_arch (), expression_context_block,
421                        expression_context_pc, flags, *stringptr,
422                        completer != nullptr, tracker);
423 
424   scoped_restore_current_language lang_saver;
425   set_language (lang->la_language);
426 
427   try
428     {
429       lang->parser (&ps);
430     }
431   catch (const gdb_exception_error &except)
432     {
433       /* If parsing for completion, allow this to succeed; but if no
434            expression elements have been written, then there's nothing
435            to do, so fail.  */
436       if (! ps.parse_completion || ps.expout->op == nullptr)
437           throw;
438     }
439 
440   expression_up result = ps.release ();
441   result->op->set_outermost ();
442 
443   if (expressiondebug)
444     result->dump (gdb_stdlog);
445 
446   if (completer != nullptr)
447     *completer = std::move (ps.m_completion_state);
448   *stringptr = ps.lexptr;
449   return result;
450 }
451 
452 /* Read an expression from the string *STRINGPTR points to,
453    parse it, and return a pointer to a struct expression that we malloc.
454    Use block BLOCK as the lexical context for variable names;
455    if BLOCK is zero, use the block of the selected stack frame.
456    Meanwhile, advance *STRINGPTR to point after the expression,
457    at the first nonwhite character that is not part of the expression
458    (possibly a null character).  FLAGS are passed to the parser.  */
459 
460 expression_up
parse_exp_1(const char ** stringptr,CORE_ADDR pc,const struct block * block,parser_flags flags,innermost_block_tracker * tracker)461 parse_exp_1 (const char **stringptr, CORE_ADDR pc, const struct block *block,
462                parser_flags flags, innermost_block_tracker *tracker)
463 {
464   return parse_exp_in_context (stringptr, pc, block, flags,
465                                      tracker, nullptr);
466 }
467 
468 /* Parse STRING as an expression, and complain if this fails to use up
469    all of the contents of STRING.  TRACKER, if non-null, will be
470    updated by the parser.  FLAGS are passed to the parser.  */
471 
472 expression_up
parse_expression(const char * string,innermost_block_tracker * tracker,parser_flags flags)473 parse_expression (const char *string, innermost_block_tracker *tracker,
474                       parser_flags flags)
475 {
476   expression_up exp = parse_exp_in_context (&string, 0, nullptr, flags,
477                                                       tracker, nullptr);
478   if (*string)
479     error (_("Junk after end of expression."));
480   return exp;
481 }
482 
483 /* Same as parse_expression, but using the given language (LANG)
484    to parse the expression.  */
485 
486 expression_up
parse_expression_with_language(const char * string,enum language lang)487 parse_expression_with_language (const char *string, enum language lang)
488 {
489   std::optional<scoped_restore_current_language> lang_saver;
490   if (current_language->la_language != lang)
491     {
492       lang_saver.emplace ();
493       set_language (lang);
494     }
495 
496   return parse_expression (string);
497 }
498 
499 /* Parse STRING as an expression.  If the parse is marked for
500    completion, set COMPLETER and return the expression.  In all other
501    cases, return NULL.  */
502 
503 expression_up
parse_expression_for_completion(const char * string,std::unique_ptr<expr_completion_base> * completer)504 parse_expression_for_completion
505      (const char *string,
506       std::unique_ptr<expr_completion_base> *completer)
507 {
508   expression_up exp;
509 
510   try
511     {
512       exp = parse_exp_in_context (&string, 0, 0, 0, nullptr, completer);
513     }
514   catch (const gdb_exception_error &except)
515     {
516       /* Nothing, EXP remains NULL.  */
517     }
518 
519   /* If we didn't get a completion result, be sure to also not return
520      an expression to our caller.  */
521   if (*completer == nullptr)
522     return nullptr;
523 
524   return exp;
525 }
526 
527 /* Parse floating point value P of length LEN.
528    Return false if invalid, true if valid.
529    The successfully parsed number is stored in DATA in
530    target format for floating-point type TYPE.
531 
532    NOTE: This accepts the floating point syntax that sscanf accepts.  */
533 
534 bool
parse_float(const char * p,int len,const struct type * type,gdb_byte * data)535 parse_float (const char *p, int len,
536                const struct type *type, gdb_byte *data)
537 {
538   return target_float_from_string (data, type, std::string (p, len));
539 }
540 
541 /* Return true if the number N_SIGN * N fits in a type with TYPE_BITS and
542    TYPE_SIGNED_P.  N_SIGNED is either 1 or -1.  */
543 
544 bool
fits_in_type(int n_sign,ULONGEST n,int type_bits,bool type_signed_p)545 fits_in_type (int n_sign, ULONGEST n, int type_bits, bool type_signed_p)
546 {
547   /* Normalize -0.  */
548   if (n == 0 && n_sign == -1)
549     n_sign = 1;
550 
551   if (n_sign == -1 && !type_signed_p)
552     /* Can't fit a negative number in an unsigned type.  */
553     return false;
554 
555   if (type_bits > sizeof (ULONGEST) * 8)
556     return true;
557 
558   ULONGEST smax = (ULONGEST)1 << (type_bits - 1);
559   if (n_sign == -1)
560     {
561       /* Negative number, signed type.  */
562       return (n <= smax);
563     }
564   else if (n_sign == 1 && type_signed_p)
565     {
566       /* Positive number, signed type.  */
567       return (n < smax);
568     }
569   else if (n_sign == 1 && !type_signed_p)
570     {
571       /* Positive number, unsigned type.  */
572       return ((n >> 1) >> (type_bits - 1)) == 0;
573     }
574   else
575     gdb_assert_not_reached ("");
576 }
577 
578 /* Return true if the number N_SIGN * N fits in a type with TYPE_BITS and
579    TYPE_SIGNED_P.  N_SIGNED is either 1 or -1.  */
580 
581 bool
fits_in_type(int n_sign,const gdb_mpz & n,int type_bits,bool type_signed_p)582 fits_in_type (int n_sign, const gdb_mpz &n, int type_bits, bool type_signed_p)
583 {
584   /* N must be nonnegative.  */
585   gdb_assert (n.sgn () >= 0);
586 
587   /* Zero always fits.  */
588   /* Normalize -0.  */
589   if (n.sgn () == 0)
590     return true;
591 
592   if (n_sign == -1 && !type_signed_p)
593     /* Can't fit a negative number in an unsigned type.  */
594     return false;
595 
596   gdb_mpz max = gdb_mpz::pow (2, (type_signed_p
597                                           ? type_bits - 1
598                                           : type_bits));
599   if (n_sign == -1)
600     return n <= max;
601   return n < max;
602 }
603 
604 /* This function avoids direct calls to fprintf
605    in the parser generated debug code.  */
606 void
parser_fprintf(FILE * x,const char * y,...)607 parser_fprintf (FILE *x, const char *y, ...)
608 {
609   va_list args;
610 
611   va_start (args, y);
612   if (x == stderr)
613     gdb_vprintf (gdb_stderr, y, args);
614   else
615     {
616       gdb_printf (gdb_stderr, " Unknown FILE used.\n");
617       gdb_vprintf (gdb_stderr, y, args);
618     }
619   va_end (args);
620 }
621 
622 void _initialize_parse ();
623 void
_initialize_parse()624 _initialize_parse ()
625 {
626   add_setshow_zuinteger_cmd ("expression", class_maintenance,
627                                    &expressiondebug,
628                                    _("Set expression debugging."),
629                                    _("Show expression debugging."),
630                                    _("When non-zero, the internal representation "
631                                      "of expressions will be printed."),
632                                    NULL,
633                                    show_expressiondebug,
634                                    &setdebuglist, &showdebuglist);
635   add_setshow_boolean_cmd ("parser", class_maintenance,
636                                   &parser_debug,
637                                  _("Set parser debugging."),
638                                  _("Show parser debugging."),
639                                  _("When non-zero, expression parser "
640                                    "tracing will be enabled."),
641                                   NULL,
642                                   show_parserdebug,
643                                   &setdebuglist, &showdebuglist);
644 }
645