GDB (API)
/home/stan/gdb/src/gdb/dwarf2expr.h
Go to the documentation of this file.
00001 /* DWARF 2 Expression Evaluator.
00002 
00003    Copyright (C) 2001-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Daniel Berlin <dan@dberlin.org>.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #if !defined (DWARF2EXPR_H)
00023 #define DWARF2EXPR_H
00024 
00025 #include "leb128.h"
00026 #include "gdbtypes.h"
00027 
00028 struct dwarf_expr_context;
00029 
00030 /* Virtual method table for struct dwarf_expr_context below.  */
00031 
00032 struct dwarf_expr_context_funcs
00033 {
00034   /* Return the value of register number REGNUM.  */
00035   CORE_ADDR (*read_reg) (void *baton, int regnum);
00036 
00037   /* Read LENGTH bytes at ADDR into BUF.  */
00038   void (*read_mem) (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t length);
00039 
00040   /* Return the location expression for the frame base attribute, in
00041      START and LENGTH.  The result must be live until the current
00042      expression evaluation is complete.  */
00043   void (*get_frame_base) (void *baton, const gdb_byte **start, size_t *length);
00044 
00045   /* Return the CFA for the frame.  */
00046   CORE_ADDR (*get_frame_cfa) (void *baton);
00047 
00048   /* Return the PC for the frame.  */
00049   CORE_ADDR (*get_frame_pc) (void *baton);
00050 
00051   /* Return the thread-local storage address for
00052      DW_OP_GNU_push_tls_address.  */
00053   CORE_ADDR (*get_tls_address) (void *baton, CORE_ADDR offset);
00054 
00055   /* Execute DW_AT_location expression for the DWARF expression subroutine in
00056      the DIE at DIE_OFFSET in the CU from CTX.  Do not touch STACK while it
00057      being passed to and returned from the called DWARF subroutine.  */
00058   void (*dwarf_call) (struct dwarf_expr_context *ctx, cu_offset die_offset);
00059 
00060   /* Return the base type given by the indicated DIE.  This can throw
00061      an exception if the DIE is invalid or does not represent a base
00062      type.  If can also be NULL in the special case where the
00063      callbacks are not performing evaluation, and thus it is
00064      meaningful to substitute a stub type of the correct size.  */
00065   struct type *(*get_base_type) (struct dwarf_expr_context *ctx, cu_offset die);
00066 
00067   /* Push on DWARF stack an entry evaluated for DW_TAG_GNU_call_site's
00068      parameter matching KIND and KIND_U at the caller of specified BATON.
00069      If DEREF_SIZE is not -1 then use DW_AT_GNU_call_site_data_value instead of
00070      DW_AT_GNU_call_site_value.  */
00071   void (*push_dwarf_reg_entry_value) (struct dwarf_expr_context *ctx,
00072                                       enum call_site_parameter_kind kind,
00073                                       union call_site_parameter_u kind_u,
00074                                       int deref_size);
00075 
00076   /* Return the address indexed by DW_OP_GNU_addr_index.
00077      This can throw an exception if the index is out of range.  */
00078   CORE_ADDR (*get_addr_index) (void *baton, unsigned int index);
00079 
00080 #if 0
00081   /* Not yet implemented.  */
00082 
00083   /* Return the `object address' for DW_OP_push_object_address.  */
00084   CORE_ADDR (*get_object_address) (void *baton);
00085 #endif
00086 };
00087 
00088 /* The location of a value.  */
00089 enum dwarf_value_location
00090 {
00091   /* The piece is in memory.
00092      The value on the dwarf stack is its address.  */
00093   DWARF_VALUE_MEMORY,
00094 
00095   /* The piece is in a register.
00096      The value on the dwarf stack is the register number.  */
00097   DWARF_VALUE_REGISTER,
00098 
00099   /* The piece is on the dwarf stack.  */
00100   DWARF_VALUE_STACK,
00101 
00102   /* The piece is a literal.  */
00103   DWARF_VALUE_LITERAL,
00104 
00105   /* The piece was optimized out.  */
00106   DWARF_VALUE_OPTIMIZED_OUT,
00107 
00108   /* The piece is an implicit pointer.  */
00109   DWARF_VALUE_IMPLICIT_POINTER
00110 };
00111 
00112 /* The dwarf expression stack.  */
00113 
00114 struct dwarf_stack_value
00115 {
00116   struct value *value;
00117 
00118   /* Non-zero if the piece is in memory and is known to be
00119      on the program's stack.  It is always ok to set this to zero.
00120      This is used, for example, to optimize memory access from the target.
00121      It can vastly speed up backtraces on long latency connections when
00122      "set stack-cache on".  */
00123   int in_stack_memory;
00124 };
00125 
00126 /* The expression evaluator works with a dwarf_expr_context, describing
00127    its current state and its callbacks.  */
00128 struct dwarf_expr_context
00129 {
00130   /* The stack of values, allocated with xmalloc.  */
00131   struct dwarf_stack_value *stack;
00132 
00133   /* The number of values currently pushed on the stack, and the
00134      number of elements allocated to the stack.  */
00135   int stack_len, stack_allocated;
00136 
00137   /* Target architecture to use for address operations.  */
00138   struct gdbarch *gdbarch;
00139 
00140   /* Target address size in bytes.  */
00141   int addr_size;
00142 
00143   /* DW_FORM_ref_addr size in bytes.  If -1 DWARF is executed from a frame
00144      context and operations depending on DW_FORM_ref_addr are not allowed.  */
00145   int ref_addr_size;
00146 
00147   /* Offset used to relocate DW_OP_addr and DW_OP_GNU_addr_index arguments.  */
00148   CORE_ADDR offset;
00149 
00150   /* An opaque argument provided by the caller, which will be passed
00151      to all of the callback functions.  */
00152   void *baton;
00153 
00154   /* Callback functions.  */
00155   const struct dwarf_expr_context_funcs *funcs;
00156 
00157   /* The current depth of dwarf expression recursion, via DW_OP_call*,
00158      DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum
00159      depth we'll tolerate before raising an error.  */
00160   int recursion_depth, max_recursion_depth;
00161 
00162   /* Location of the value.  */
00163   enum dwarf_value_location location;
00164 
00165   /* For DWARF_VALUE_LITERAL, the current literal value's length and
00166      data.  For DWARF_VALUE_IMPLICIT_POINTER, LEN is the offset of the
00167      target DIE of sect_offset kind.  */
00168   ULONGEST len;
00169   const gdb_byte *data;
00170 
00171   /* Initialization status of variable: Non-zero if variable has been
00172      initialized; zero otherwise.  */
00173   int initialized;
00174 
00175   /* An array of pieces.  PIECES points to its first element;
00176      NUM_PIECES is its length.
00177 
00178      Each time DW_OP_piece is executed, we add a new element to the
00179      end of this array, recording the current top of the stack, the
00180      current location, and the size given as the operand to
00181      DW_OP_piece.  We then pop the top value from the stack, reset the
00182      location, and resume evaluation.
00183 
00184      The Dwarf spec doesn't say whether DW_OP_piece pops the top value
00185      from the stack.  We do, ensuring that clients of this interface
00186      expecting to see a value left on the top of the stack (say, code
00187      evaluating frame base expressions or CFA's specified with
00188      DW_CFA_def_cfa_expression) will get an error if the expression
00189      actually marks all the values it computes as pieces.
00190 
00191      If an expression never uses DW_OP_piece, num_pieces will be zero.
00192      (It would be nice to present these cases as expressions yielding
00193      a single piece, so that callers need not distinguish between the
00194      no-DW_OP_piece and one-DW_OP_piece cases.  But expressions with
00195      no DW_OP_piece operations have no value to place in a piece's
00196      'size' field; the size comes from the surrounding data.  So the
00197      two cases need to be handled separately.)  */
00198   int num_pieces;
00199   struct dwarf_expr_piece *pieces;
00200 };
00201 
00202 
00203 /* A piece of an object, as recorded by DW_OP_piece or DW_OP_bit_piece.  */
00204 struct dwarf_expr_piece
00205 {
00206   enum dwarf_value_location location;
00207 
00208   union
00209   {
00210     struct
00211     {
00212       /* This piece's address, for DWARF_VALUE_MEMORY pieces.  */
00213       CORE_ADDR addr;
00214       /* Non-zero if the piece is known to be in memory and on
00215          the program's stack.  */
00216       int in_stack_memory;
00217     } mem;
00218 
00219     /* The piece's register number, for DWARF_VALUE_REGISTER pieces.  */
00220     int regno;
00221 
00222     /* The piece's literal value, for DWARF_VALUE_STACK pieces.  */
00223     struct value *value;
00224 
00225     struct
00226     {
00227       /* A pointer to the data making up this piece,
00228          for DWARF_VALUE_LITERAL pieces.  */
00229       const gdb_byte *data;
00230       /* The length of the available data.  */
00231       ULONGEST length;
00232     } literal;
00233 
00234     /* Used for DWARF_VALUE_IMPLICIT_POINTER.  */
00235     struct
00236     {
00237       /* The referent DIE from DW_OP_GNU_implicit_pointer.  */
00238       sect_offset die;
00239       /* The byte offset into the resulting data.  */
00240       LONGEST offset;
00241     } ptr;
00242   } v;
00243 
00244   /* The length of the piece, in bits.  */
00245   ULONGEST size;
00246   /* The piece offset, in bits.  */
00247   ULONGEST offset;
00248 };
00249 
00250 struct dwarf_expr_context *new_dwarf_expr_context (void);
00251 void free_dwarf_expr_context (struct dwarf_expr_context *ctx);
00252 struct cleanup *
00253     make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx);
00254 
00255 void dwarf_expr_push_address (struct dwarf_expr_context *ctx,
00256                               CORE_ADDR value,
00257                               int in_stack_memory);
00258 void dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
00259                       size_t len);
00260 struct value *dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n);
00261 CORE_ADDR dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n);
00262 int dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n);
00263 
00264 void dwarf_expr_require_composition (const gdb_byte *, const gdb_byte *,
00265                                      const char *);
00266 
00267 /* Stub dwarf_expr_context_funcs implementations.  */
00268 
00269 void ctx_no_get_frame_base (void *baton, const gdb_byte **start,
00270                             size_t *length);
00271 CORE_ADDR ctx_no_get_frame_cfa (void *baton);
00272 CORE_ADDR ctx_no_get_frame_pc (void *baton);
00273 CORE_ADDR ctx_no_get_tls_address (void *baton, CORE_ADDR offset);
00274 void ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset);
00275 struct type *ctx_no_get_base_type (struct dwarf_expr_context *ctx,
00276                                    cu_offset die);
00277 void ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
00278                                         enum call_site_parameter_kind kind,
00279                                         union call_site_parameter_u kind_u,
00280                                         int deref_size);
00281 CORE_ADDR ctx_no_get_addr_index (void *baton, unsigned int index);
00282 
00283 int dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end);
00284 
00285 int dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf,
00286                                     const gdb_byte *buf_end,
00287                                     CORE_ADDR *deref_size_return);
00288 
00289 int dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
00290                               CORE_ADDR *fb_offset_return);
00291 
00292 int dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
00293                               const gdb_byte *buf_end,
00294                               CORE_ADDR *sp_offset_return);
00295 
00296 /* Wrappers around the leb128 reader routines to simplify them for our
00297    purposes.  */
00298 
00299 static inline const gdb_byte *
00300 gdb_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
00301                   uint64_t *r)
00302 {
00303   size_t bytes_read = read_uleb128_to_uint64 (buf, buf_end, r);
00304 
00305   if (bytes_read == 0)
00306     return NULL;
00307   return buf + bytes_read;
00308 }
00309 
00310 static inline const gdb_byte *
00311 gdb_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
00312                   int64_t *r)
00313 {
00314   size_t bytes_read = read_sleb128_to_int64 (buf, buf_end, r);
00315 
00316   if (bytes_read == 0)
00317     return NULL;
00318   return buf + bytes_read;
00319 }
00320 
00321 static inline const gdb_byte *
00322 gdb_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
00323 {
00324   size_t bytes_read = skip_leb128 (buf, buf_end);
00325 
00326   if (bytes_read == 0)
00327     return NULL;
00328   return buf + bytes_read;
00329 }
00330 
00331 extern const gdb_byte *safe_read_uleb128 (const gdb_byte *buf,
00332                                           const gdb_byte *buf_end,
00333                                           uint64_t *r);
00334 
00335 extern const gdb_byte *safe_read_sleb128 (const gdb_byte *buf,
00336                                           const gdb_byte *buf_end,
00337                                           int64_t *r);
00338 
00339 extern const gdb_byte *safe_skip_leb128 (const gdb_byte *buf,
00340                                          const gdb_byte *buf_end);
00341 
00342 #endif /* dwarf2expr.h */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines