binutils-gdb/gdb/expression.h
Pedro Alves 74ea4be48e Introduce OP_VAR_MSYM_VALUE
The previous patch left GDB with an inconsistency.  While with normal
expression evaluation the "unknown return type" error shows the name
of the function that misses debug info:

  (gdb) p getenv ("PATH")
  'getenv' has unknown return type; cast the call to its declared return type
   ^^^^^^

which can by handy in more complicated expressions, "ptype" does not:

  (gdb) ptype getenv ("PATH")
  function has unknown return type; cast the call to its declared return type
  ^^^^^^^^

This commit is a step toward fixing it.

The problem is that while evaluating the expression above, we have no
reference to the minimal symbol where we could extract the name from.
This is because the resulting expression tree has no reference to the
minsym at all.  During parsing, the type and address of the minsym are
extracted and an UNOP_MEMVAL / UNOP_MEMVAL_TLS operator is generated
(see write_exp_elt_msym).  With "set debug expression", here's what
you see:

            0  OP_FUNCALL            Number of args: 0
            3    UNOP_MEMVAL           Type @0x565334a51930 (<text variable, no debug info>)
            6      OP_LONG               Type @0x565334a51c60 (__CORE_ADDR), value 140737345035648 (0x7ffff7751d80)

The "print" case finds the function name, because
call_function_by_hand looks up the function by address again.
However, for "ptype", we don't reach that code, because obviously we
don't really call the function.

Unlike minsym references, references to variables with debug info have
a pointer to the variable's symbol in the expression tree, with
OP_VAR_VALUE:

  (gdb) ptype main()
  ...
            0  OP_FUNCALL            Number of args: 0
            3    OP_VAR_VALUE          Block @0x0, symbol @0x559bbbd9b358 (main(int, char**))
  ...

so I don't see why do minsyms need to be different.  So to prepare for
fixing the missing function name issue, this commit adds a new
OP_VAR_MSYM_VALUE operator that mimics OP_VAR_VALUE, except that it's
for minsyms instead of debug symbols.  For infcalls, we now get
expressions like these:

            0  OP_FUNCALL            Number of args: 0
            3    OP_VAR_MSYM_VALUE     Objfile @0x1e41bf0, msymbol @0x7fffe599b000 (getenv)

In the following patch, we'll make OP_FUNCALL extract the function
name from the symbol stored in OP_VAR_VALUE/OP_VAR_MSYM_VALUE.

OP_VAR_MSYM_VALUE will be used more in a later patch in the series
too.

gdb/ChangeLog:
2017-09-04  Pedro Alves  <palves@redhat.com>

	* ada-lang.c (resolve_subexp): Handle OP_VAR_MSYM_VALUE.
	* ax-gdb.c (gen_msym_var_ref): New function.
	(gen_expr): Handle OP_VAR_MSYM_VALUE.
	* eval.c (evaluate_var_msym_value): New function.
	* eval.c (evaluate_subexp_standard): Handle OP_VAR_MSYM_VALUE.
	<OP_FUNCALL>: Extract function name from symbol/minsym and pass it
	to call_function_by_hand.
	* expprint.c (print_subexp_standard, dump_subexp_body_standard):
	Handle OP_VAR_MSYM_VALUE.
	(union exp_element) <msymbol>: New field.
	* minsyms.h (struct type): Forward declare.
	(find_minsym_type_and_address): Declare.
	* parse.c (write_exp_elt_msym): New function.
	(write_exp_msymbol): Delete, refactored as ...
	(find_minsym_type_and_address): ... this new function.
	(write_exp_msymbol): Reimplement using OP_VAR_MSYM_VALUE.
	(operator_length_standard, operator_check_standard): Handle
	OP_VAR_MSYM_VALUE.
	* std-operator.def (OP_VAR_MSYM_VALUE): New.
2017-09-04 20:21:13 +01:00

172 lines
5.4 KiB
C

/* Definitions for expressions stored in reversed prefix form, for GDB.
Copyright (C) 1986-2017 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#if !defined (EXPRESSION_H)
#define EXPRESSION_H 1
#include "symtab.h" /* Needed for "struct block" type. */
#include "doublest.h" /* Needed for DOUBLEST. */
/* Definitions for saved C expressions. */
/* An expression is represented as a vector of union exp_element's.
Each exp_element is an opcode, except that some opcodes cause
the following exp_element to be treated as a long or double constant
or as a variable. The opcodes are obeyed, using a stack for temporaries.
The value is left on the temporary stack at the end. */
/* When it is necessary to include a string,
it can occupy as many exp_elements as it needs.
We find the length of the string using strlen,
divide to find out how many exp_elements are used up,
and skip that many. Strings, like numbers, are indicated
by the preceding opcode. */
enum exp_opcode
{
#define OP(name) name ,
#include "std-operator.def"
/* First extension operator. Individual language modules define extra
operators in *.def include files below with numbers higher than
OP_EXTENDED0. */
OP (OP_EXTENDED0)
/* Language specific operators. */
#include "ada-operator.def"
#undef OP
/* Existing only to swallow the last comma (',') from last .inc file. */
OP_UNUSED_LAST
};
union exp_element
{
enum exp_opcode opcode;
struct symbol *symbol;
struct minimal_symbol *msymbol;
LONGEST longconst;
DOUBLEST doubleconst;
gdb_byte decfloatconst[16];
/* Really sizeof (union exp_element) characters (or less for the last
element of a string). */
char string;
struct type *type;
struct internalvar *internalvar;
const struct block *block;
struct objfile *objfile;
};
struct expression
{
const struct language_defn *language_defn; /* language it was
entered in. */
struct gdbarch *gdbarch; /* architecture it was parsed in. */
int nelts;
union exp_element elts[1];
};
typedef gdb::unique_xmalloc_ptr<expression> expression_up;
/* Macros for converting between number of expression elements and bytes
to store that many expression elements. */
#define EXP_ELEM_TO_BYTES(elements) \
((elements) * sizeof (union exp_element))
#define BYTES_TO_EXP_ELEM(bytes) \
(((bytes) + sizeof (union exp_element) - 1) / sizeof (union exp_element))
/* From parse.c */
extern expression_up parse_expression (const char *);
extern expression_up parse_expression_with_language (const char *string,
enum language lang);
extern struct type *parse_expression_for_completion (const char *, char **,
enum type_code *);
extern expression_up parse_exp_1 (const char **, CORE_ADDR pc,
const struct block *, int);
/* For use by parsers; set if we want to parse an expression and
attempt completion. */
extern int parse_completion;
/* The innermost context required by the stack and register variables
we've encountered so far. To use this, set it to NULL, then call
parse_<whatever>, then look at it. */
extern const struct block *innermost_block;
/* From eval.c */
/* Values of NOSIDE argument to eval_subexp. */
enum noside
{
EVAL_NORMAL,
EVAL_SKIP, /* Only effect is to increment pos. */
EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or
call any functions. The value
returned will have the correct
type, and will have an
approximately correct lvalue
type (inaccuracy: anything that is
listed as being in a register in
the function in which it was
declared will be lval_register).
Ideally this would not even read
target memory, but currently it
does in many situations. */
};
extern struct value *evaluate_subexp_standard
(struct type *, struct expression *, int *, enum noside);
/* From expprint.c */
extern void print_expression (struct expression *, struct ui_file *);
extern const char *op_name (struct expression *exp, enum exp_opcode opcode);
extern const char *op_string (enum exp_opcode);
extern void dump_raw_expression (struct expression *,
struct ui_file *, const char *);
extern void dump_prefix_expression (struct expression *, struct ui_file *);
/* In an OP_RANGE expression, either bound could be empty, indicating
that its value is by default that of the corresponding bound of the
array or string. So we have four sorts of subrange. This
enumeration type is to identify this. */
enum range_type
{
BOTH_BOUND_DEFAULT, /* "(:)" */
LOW_BOUND_DEFAULT, /* "(:high)" */
HIGH_BOUND_DEFAULT, /* "(low:)" */
NONE_BOUND_DEFAULT /* "(low:high)" */
};
#endif /* !defined (EXPRESSION_H) */