xref: /dflybsd-src/contrib/gdb-7/gdb/dwarf2expr.h (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 2001-2003, 2005, 2007-2012 Free Software Foundation,
4*a45ae5f8SJohn Marino    Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    Contributed by Daniel Berlin <dan@dberlin.org>.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This file is part of GDB.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
115796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
125796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
135796c8dcSSimon Schubert    (at your option) any later version.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
165796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
175796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185796c8dcSSimon Schubert    GNU General Public License for more details.
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
215796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert #if !defined (DWARF2EXPR_H)
245796c8dcSSimon Schubert #define DWARF2EXPR_H
255796c8dcSSimon Schubert 
26*a45ae5f8SJohn Marino struct dwarf_expr_context;
27*a45ae5f8SJohn Marino 
28*a45ae5f8SJohn Marino /* Virtual method table for struct dwarf_expr_context below.  */
29*a45ae5f8SJohn Marino 
30*a45ae5f8SJohn Marino struct dwarf_expr_context_funcs
31*a45ae5f8SJohn Marino {
32*a45ae5f8SJohn Marino   /* Return the value of register number REGNUM.  */
33*a45ae5f8SJohn Marino   CORE_ADDR (*read_reg) (void *baton, int regnum);
34*a45ae5f8SJohn Marino 
35*a45ae5f8SJohn Marino   /* Read LENGTH bytes at ADDR into BUF.  */
36*a45ae5f8SJohn Marino   void (*read_mem) (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t length);
37*a45ae5f8SJohn Marino 
38*a45ae5f8SJohn Marino   /* Return the location expression for the frame base attribute, in
39*a45ae5f8SJohn Marino      START and LENGTH.  The result must be live until the current
40*a45ae5f8SJohn Marino      expression evaluation is complete.  */
41*a45ae5f8SJohn Marino   void (*get_frame_base) (void *baton, const gdb_byte **start, size_t *length);
42*a45ae5f8SJohn Marino 
43*a45ae5f8SJohn Marino   /* Return the CFA for the frame.  */
44*a45ae5f8SJohn Marino   CORE_ADDR (*get_frame_cfa) (void *baton);
45*a45ae5f8SJohn Marino 
46*a45ae5f8SJohn Marino   /* Return the PC for the frame.  */
47*a45ae5f8SJohn Marino   CORE_ADDR (*get_frame_pc) (void *baton);
48*a45ae5f8SJohn Marino 
49*a45ae5f8SJohn Marino   /* Return the thread-local storage address for
50*a45ae5f8SJohn Marino      DW_OP_GNU_push_tls_address.  */
51*a45ae5f8SJohn Marino   CORE_ADDR (*get_tls_address) (void *baton, CORE_ADDR offset);
52*a45ae5f8SJohn Marino 
53*a45ae5f8SJohn Marino   /* Execute DW_AT_location expression for the DWARF expression subroutine in
54*a45ae5f8SJohn Marino      the DIE at DIE_OFFSET in the CU from CTX.  Do not touch STACK while it
55*a45ae5f8SJohn Marino      being passed to and returned from the called DWARF subroutine.  */
56*a45ae5f8SJohn Marino   void (*dwarf_call) (struct dwarf_expr_context *ctx, size_t die_offset);
57*a45ae5f8SJohn Marino 
58*a45ae5f8SJohn Marino   /* Return the base type given by the indicated DIE.  This can throw
59*a45ae5f8SJohn Marino      an exception if the DIE is invalid or does not represent a base
60*a45ae5f8SJohn Marino      type.  If can also be NULL in the special case where the
61*a45ae5f8SJohn Marino      callbacks are not performing evaluation, and thus it is
62*a45ae5f8SJohn Marino      meaningful to substitute a stub type of the correct size.  */
63*a45ae5f8SJohn Marino   struct type *(*get_base_type) (struct dwarf_expr_context *ctx, size_t die);
64*a45ae5f8SJohn Marino 
65*a45ae5f8SJohn Marino   /* Push on DWARF stack an entry evaluated for DW_TAG_GNU_call_site's
66*a45ae5f8SJohn Marino      DWARF_REG/FB_OFFSET at the caller of specified BATON.  If DWARF register
67*a45ae5f8SJohn Marino      number DWARF_REG specifying the push_dwarf_reg_entry_value parameter is
68*a45ae5f8SJohn Marino      not -1 FB_OFFSET is ignored.  Otherwise FB_OFFSET specifies stack
69*a45ae5f8SJohn Marino      parameter offset against caller's stack pointer (which equals the callee's
70*a45ae5f8SJohn Marino      frame base).  If DEREF_SIZE is not -1 then use
71*a45ae5f8SJohn Marino      DW_AT_GNU_call_site_data_value instead of DW_AT_GNU_call_site_value.  */
72*a45ae5f8SJohn Marino   void (*push_dwarf_reg_entry_value) (struct dwarf_expr_context *ctx,
73*a45ae5f8SJohn Marino 				      int dwarf_reg, CORE_ADDR fb_offset,
74*a45ae5f8SJohn Marino 				      int deref_size);
75*a45ae5f8SJohn Marino 
76*a45ae5f8SJohn Marino #if 0
77*a45ae5f8SJohn Marino   /* Not yet implemented.  */
78*a45ae5f8SJohn Marino 
79*a45ae5f8SJohn Marino   /* Return the `object address' for DW_OP_push_object_address.  */
80*a45ae5f8SJohn Marino   CORE_ADDR (*get_object_address) (void *baton);
81*a45ae5f8SJohn Marino #endif
82*a45ae5f8SJohn Marino };
83*a45ae5f8SJohn Marino 
845796c8dcSSimon Schubert /* The location of a value.  */
855796c8dcSSimon Schubert enum dwarf_value_location
865796c8dcSSimon Schubert {
875796c8dcSSimon Schubert   /* The piece is in memory.
885796c8dcSSimon Schubert      The value on the dwarf stack is its address.  */
895796c8dcSSimon Schubert   DWARF_VALUE_MEMORY,
905796c8dcSSimon Schubert 
915796c8dcSSimon Schubert   /* The piece is in a register.
925796c8dcSSimon Schubert      The value on the dwarf stack is the register number.  */
935796c8dcSSimon Schubert   DWARF_VALUE_REGISTER,
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert   /* The piece is on the dwarf stack.  */
965796c8dcSSimon Schubert   DWARF_VALUE_STACK,
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert   /* The piece is a literal.  */
99cf7f2e2dSJohn Marino   DWARF_VALUE_LITERAL,
100cf7f2e2dSJohn Marino 
101cf7f2e2dSJohn Marino   /* The piece was optimized out.  */
102c50c785cSJohn Marino   DWARF_VALUE_OPTIMIZED_OUT,
103c50c785cSJohn Marino 
104c50c785cSJohn Marino   /* The piece is an implicit pointer.  */
105c50c785cSJohn Marino   DWARF_VALUE_IMPLICIT_POINTER
1065796c8dcSSimon Schubert };
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert /* The dwarf expression stack.  */
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert struct dwarf_stack_value
1115796c8dcSSimon Schubert {
112*a45ae5f8SJohn Marino   struct value *value;
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert   /* Non-zero if the piece is in memory and is known to be
1155796c8dcSSimon Schubert      on the program's stack.  It is always ok to set this to zero.
1165796c8dcSSimon Schubert      This is used, for example, to optimize memory access from the target.
1175796c8dcSSimon Schubert      It can vastly speed up backtraces on long latency connections when
1185796c8dcSSimon Schubert      "set stack-cache on".  */
1195796c8dcSSimon Schubert   int in_stack_memory;
1205796c8dcSSimon Schubert };
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert /* The expression evaluator works with a dwarf_expr_context, describing
1235796c8dcSSimon Schubert    its current state and its callbacks.  */
1245796c8dcSSimon Schubert struct dwarf_expr_context
1255796c8dcSSimon Schubert {
1265796c8dcSSimon Schubert   /* The stack of values, allocated with xmalloc.  */
1275796c8dcSSimon Schubert   struct dwarf_stack_value *stack;
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert   /* The number of values currently pushed on the stack, and the
1305796c8dcSSimon Schubert      number of elements allocated to the stack.  */
1315796c8dcSSimon Schubert   int stack_len, stack_allocated;
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert   /* Target architecture to use for address operations.  */
1345796c8dcSSimon Schubert   struct gdbarch *gdbarch;
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert   /* Target address size in bytes.  */
1375796c8dcSSimon Schubert   int addr_size;
1385796c8dcSSimon Schubert 
139*a45ae5f8SJohn Marino   /* DW_FORM_ref_addr size in bytes.  If -1 DWARF is executed from a frame
140*a45ae5f8SJohn Marino      context and operations depending on DW_FORM_ref_addr are not allowed.  */
141*a45ae5f8SJohn Marino   int ref_addr_size;
142*a45ae5f8SJohn Marino 
143cf7f2e2dSJohn Marino   /* Offset used to relocate DW_OP_addr argument.  */
144cf7f2e2dSJohn Marino   CORE_ADDR offset;
145cf7f2e2dSJohn Marino 
1465796c8dcSSimon Schubert   /* An opaque argument provided by the caller, which will be passed
1475796c8dcSSimon Schubert      to all of the callback functions.  */
1485796c8dcSSimon Schubert   void *baton;
1495796c8dcSSimon Schubert 
150*a45ae5f8SJohn Marino   /* Callback functions.  */
151*a45ae5f8SJohn Marino   const struct dwarf_expr_context_funcs *funcs;
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   /* The current depth of dwarf expression recursion, via DW_OP_call*,
1545796c8dcSSimon Schubert      DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum
1555796c8dcSSimon Schubert      depth we'll tolerate before raising an error.  */
1565796c8dcSSimon Schubert   int recursion_depth, max_recursion_depth;
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert   /* Location of the value.  */
1595796c8dcSSimon Schubert   enum dwarf_value_location location;
1605796c8dcSSimon Schubert 
161c50c785cSJohn Marino   /* For DWARF_VALUE_LITERAL, the current literal value's length and
162c50c785cSJohn Marino      data.  For DWARF_VALUE_IMPLICIT_POINTER, LEN is the offset of the
163c50c785cSJohn Marino      target DIE.  */
1645796c8dcSSimon Schubert   ULONGEST len;
165cf7f2e2dSJohn Marino   const gdb_byte *data;
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert   /* Initialization status of variable: Non-zero if variable has been
1685796c8dcSSimon Schubert      initialized; zero otherwise.  */
1695796c8dcSSimon Schubert   int initialized;
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert   /* An array of pieces.  PIECES points to its first element;
1725796c8dcSSimon Schubert      NUM_PIECES is its length.
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert      Each time DW_OP_piece is executed, we add a new element to the
1755796c8dcSSimon Schubert      end of this array, recording the current top of the stack, the
1765796c8dcSSimon Schubert      current location, and the size given as the operand to
1775796c8dcSSimon Schubert      DW_OP_piece.  We then pop the top value from the stack, reset the
1785796c8dcSSimon Schubert      location, and resume evaluation.
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert      The Dwarf spec doesn't say whether DW_OP_piece pops the top value
1815796c8dcSSimon Schubert      from the stack.  We do, ensuring that clients of this interface
1825796c8dcSSimon Schubert      expecting to see a value left on the top of the stack (say, code
1835796c8dcSSimon Schubert      evaluating frame base expressions or CFA's specified with
1845796c8dcSSimon Schubert      DW_CFA_def_cfa_expression) will get an error if the expression
1855796c8dcSSimon Schubert      actually marks all the values it computes as pieces.
1865796c8dcSSimon Schubert 
1875796c8dcSSimon Schubert      If an expression never uses DW_OP_piece, num_pieces will be zero.
1885796c8dcSSimon Schubert      (It would be nice to present these cases as expressions yielding
1895796c8dcSSimon Schubert      a single piece, so that callers need not distinguish between the
1905796c8dcSSimon Schubert      no-DW_OP_piece and one-DW_OP_piece cases.  But expressions with
1915796c8dcSSimon Schubert      no DW_OP_piece operations have no value to place in a piece's
1925796c8dcSSimon Schubert      'size' field; the size comes from the surrounding data.  So the
1935796c8dcSSimon Schubert      two cases need to be handled separately.)  */
1945796c8dcSSimon Schubert   int num_pieces;
1955796c8dcSSimon Schubert   struct dwarf_expr_piece *pieces;
1965796c8dcSSimon Schubert };
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert 
199cf7f2e2dSJohn Marino /* A piece of an object, as recorded by DW_OP_piece or DW_OP_bit_piece.  */
2005796c8dcSSimon Schubert struct dwarf_expr_piece
2015796c8dcSSimon Schubert {
2025796c8dcSSimon Schubert   enum dwarf_value_location location;
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert   union
2055796c8dcSSimon Schubert   {
2065796c8dcSSimon Schubert     struct
2075796c8dcSSimon Schubert     {
208cf7f2e2dSJohn Marino       /* This piece's address, for DWARF_VALUE_MEMORY pieces.  */
209cf7f2e2dSJohn Marino       CORE_ADDR addr;
2105796c8dcSSimon Schubert       /* Non-zero if the piece is known to be in memory and on
2115796c8dcSSimon Schubert 	 the program's stack.  */
2125796c8dcSSimon Schubert       int in_stack_memory;
213cf7f2e2dSJohn Marino     } mem;
214cf7f2e2dSJohn Marino 
215*a45ae5f8SJohn Marino     /* The piece's register number, for DWARF_VALUE_REGISTER pieces.  */
216*a45ae5f8SJohn Marino     int regno;
217*a45ae5f8SJohn Marino 
218*a45ae5f8SJohn Marino     /* The piece's literal value, for DWARF_VALUE_STACK pieces.  */
219*a45ae5f8SJohn Marino     struct value *value;
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert     struct
2225796c8dcSSimon Schubert     {
223cf7f2e2dSJohn Marino       /* A pointer to the data making up this piece,
224cf7f2e2dSJohn Marino 	 for DWARF_VALUE_LITERAL pieces.  */
225cf7f2e2dSJohn Marino       const gdb_byte *data;
2265796c8dcSSimon Schubert       /* The length of the available data.  */
2275796c8dcSSimon Schubert       ULONGEST length;
2285796c8dcSSimon Schubert     } literal;
229c50c785cSJohn Marino 
230c50c785cSJohn Marino     /* Used for DWARF_VALUE_IMPLICIT_POINTER.  */
231c50c785cSJohn Marino     struct
232c50c785cSJohn Marino     {
233c50c785cSJohn Marino       /* The referent DIE from DW_OP_GNU_implicit_pointer.  */
234c50c785cSJohn Marino       ULONGEST die;
235c50c785cSJohn Marino       /* The byte offset into the resulting data.  */
236c50c785cSJohn Marino       LONGEST offset;
237c50c785cSJohn Marino     } ptr;
2385796c8dcSSimon Schubert   } v;
2395796c8dcSSimon Schubert 
240cf7f2e2dSJohn Marino   /* The length of the piece, in bits.  */
2415796c8dcSSimon Schubert   ULONGEST size;
242cf7f2e2dSJohn Marino   /* The piece offset, in bits.  */
243cf7f2e2dSJohn Marino   ULONGEST offset;
2445796c8dcSSimon Schubert };
2455796c8dcSSimon Schubert 
2465796c8dcSSimon Schubert struct dwarf_expr_context *new_dwarf_expr_context (void);
2475796c8dcSSimon Schubert void free_dwarf_expr_context (struct dwarf_expr_context *ctx);
2485796c8dcSSimon Schubert struct cleanup *
2495796c8dcSSimon Schubert     make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx);
2505796c8dcSSimon Schubert 
251*a45ae5f8SJohn Marino void dwarf_expr_push_address (struct dwarf_expr_context *ctx,
252*a45ae5f8SJohn Marino 			      CORE_ADDR value,
2535796c8dcSSimon Schubert 			      int in_stack_memory);
254cf7f2e2dSJohn Marino void dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
2555796c8dcSSimon Schubert 		      size_t len);
256*a45ae5f8SJohn Marino struct value *dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n);
257cf7f2e2dSJohn Marino CORE_ADDR dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n);
2585796c8dcSSimon Schubert int dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n);
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert 
261cf7f2e2dSJohn Marino const gdb_byte *read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
262cf7f2e2dSJohn Marino 			      ULONGEST * r);
263cf7f2e2dSJohn Marino const gdb_byte *read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
264cf7f2e2dSJohn Marino 			      LONGEST * r);
265cf7f2e2dSJohn Marino 
266c50c785cSJohn Marino const char *dwarf_stack_op_name (unsigned int);
267cf7f2e2dSJohn Marino 
268cf7f2e2dSJohn Marino void dwarf_expr_require_composition (const gdb_byte *, const gdb_byte *,
269cf7f2e2dSJohn Marino 				     const char *);
2705796c8dcSSimon Schubert 
271*a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs implementations.  */
272*a45ae5f8SJohn Marino 
273*a45ae5f8SJohn Marino void ctx_no_get_frame_base (void *baton, const gdb_byte **start,
274*a45ae5f8SJohn Marino 			    size_t *length);
275*a45ae5f8SJohn Marino CORE_ADDR ctx_no_get_frame_cfa (void *baton);
276*a45ae5f8SJohn Marino CORE_ADDR ctx_no_get_frame_pc (void *baton);
277*a45ae5f8SJohn Marino CORE_ADDR ctx_no_get_tls_address (void *baton, CORE_ADDR offset);
278*a45ae5f8SJohn Marino void ctx_no_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset);
279*a45ae5f8SJohn Marino struct type *ctx_no_get_base_type (struct dwarf_expr_context *ctx, size_t die);
280*a45ae5f8SJohn Marino void ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
281*a45ae5f8SJohn Marino 					int dwarf_reg, CORE_ADDR fb_offset,
282*a45ae5f8SJohn Marino 					int deref_size);
283*a45ae5f8SJohn Marino 
284*a45ae5f8SJohn Marino int dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end);
285*a45ae5f8SJohn Marino 
286*a45ae5f8SJohn Marino int dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf,
287*a45ae5f8SJohn Marino 				    const gdb_byte *buf_end,
288*a45ae5f8SJohn Marino 				    CORE_ADDR *deref_size_return);
289*a45ae5f8SJohn Marino 
290*a45ae5f8SJohn Marino int dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
291*a45ae5f8SJohn Marino 			      CORE_ADDR *fb_offset_return);
292*a45ae5f8SJohn Marino 
293*a45ae5f8SJohn Marino int dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
294*a45ae5f8SJohn Marino 			      const gdb_byte *buf_end,
295*a45ae5f8SJohn Marino 			      CORE_ADDR *sp_offset_return);
296*a45ae5f8SJohn Marino 
2975796c8dcSSimon Schubert #endif /* dwarf2expr.h */
298