15796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator. 25796c8dcSSimon Schubert 3*c50c785cSJohn Marino Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011 45796c8dcSSimon Schubert Free Software Foundation, 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 265796c8dcSSimon Schubert /* The location of a value. */ 275796c8dcSSimon Schubert enum dwarf_value_location 285796c8dcSSimon Schubert { 295796c8dcSSimon Schubert /* The piece is in memory. 305796c8dcSSimon Schubert The value on the dwarf stack is its address. */ 315796c8dcSSimon Schubert DWARF_VALUE_MEMORY, 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert /* The piece is in a register. 345796c8dcSSimon Schubert The value on the dwarf stack is the register number. */ 355796c8dcSSimon Schubert DWARF_VALUE_REGISTER, 365796c8dcSSimon Schubert 375796c8dcSSimon Schubert /* The piece is on the dwarf stack. */ 385796c8dcSSimon Schubert DWARF_VALUE_STACK, 395796c8dcSSimon Schubert 405796c8dcSSimon Schubert /* The piece is a literal. */ 41cf7f2e2dSJohn Marino DWARF_VALUE_LITERAL, 42cf7f2e2dSJohn Marino 43cf7f2e2dSJohn Marino /* The piece was optimized out. */ 44*c50c785cSJohn Marino DWARF_VALUE_OPTIMIZED_OUT, 45*c50c785cSJohn Marino 46*c50c785cSJohn Marino /* The piece is an implicit pointer. */ 47*c50c785cSJohn Marino DWARF_VALUE_IMPLICIT_POINTER 485796c8dcSSimon Schubert }; 495796c8dcSSimon Schubert 505796c8dcSSimon Schubert /* The dwarf expression stack. */ 515796c8dcSSimon Schubert 525796c8dcSSimon Schubert struct dwarf_stack_value 535796c8dcSSimon Schubert { 54cf7f2e2dSJohn Marino ULONGEST value; 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert /* Non-zero if the piece is in memory and is known to be 575796c8dcSSimon Schubert on the program's stack. It is always ok to set this to zero. 585796c8dcSSimon Schubert This is used, for example, to optimize memory access from the target. 595796c8dcSSimon Schubert It can vastly speed up backtraces on long latency connections when 605796c8dcSSimon Schubert "set stack-cache on". */ 615796c8dcSSimon Schubert int in_stack_memory; 625796c8dcSSimon Schubert }; 635796c8dcSSimon Schubert 645796c8dcSSimon Schubert /* The expression evaluator works with a dwarf_expr_context, describing 655796c8dcSSimon Schubert its current state and its callbacks. */ 665796c8dcSSimon Schubert struct dwarf_expr_context 675796c8dcSSimon Schubert { 685796c8dcSSimon Schubert /* The stack of values, allocated with xmalloc. */ 695796c8dcSSimon Schubert struct dwarf_stack_value *stack; 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert /* The number of values currently pushed on the stack, and the 725796c8dcSSimon Schubert number of elements allocated to the stack. */ 735796c8dcSSimon Schubert int stack_len, stack_allocated; 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert /* Target architecture to use for address operations. */ 765796c8dcSSimon Schubert struct gdbarch *gdbarch; 775796c8dcSSimon Schubert 785796c8dcSSimon Schubert /* Target address size in bytes. */ 795796c8dcSSimon Schubert int addr_size; 805796c8dcSSimon Schubert 81cf7f2e2dSJohn Marino /* Offset used to relocate DW_OP_addr argument. */ 82cf7f2e2dSJohn Marino CORE_ADDR offset; 83cf7f2e2dSJohn Marino 845796c8dcSSimon Schubert /* An opaque argument provided by the caller, which will be passed 855796c8dcSSimon Schubert to all of the callback functions. */ 865796c8dcSSimon Schubert void *baton; 875796c8dcSSimon Schubert 885796c8dcSSimon Schubert /* Return the value of register number REGNUM. */ 895796c8dcSSimon Schubert CORE_ADDR (*read_reg) (void *baton, int regnum); 905796c8dcSSimon Schubert 915796c8dcSSimon Schubert /* Read LENGTH bytes at ADDR into BUF. */ 925796c8dcSSimon Schubert void (*read_mem) (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t length); 935796c8dcSSimon Schubert 945796c8dcSSimon Schubert /* Return the location expression for the frame base attribute, in 955796c8dcSSimon Schubert START and LENGTH. The result must be live until the current 965796c8dcSSimon Schubert expression evaluation is complete. */ 97cf7f2e2dSJohn Marino void (*get_frame_base) (void *baton, const gdb_byte **start, size_t *length); 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert /* Return the CFA for the frame. */ 1005796c8dcSSimon Schubert CORE_ADDR (*get_frame_cfa) (void *baton); 1015796c8dcSSimon Schubert 102*c50c785cSJohn Marino /* Return the PC for the frame. */ 103*c50c785cSJohn Marino CORE_ADDR (*get_frame_pc) (void *baton); 104*c50c785cSJohn Marino 1055796c8dcSSimon Schubert /* Return the thread-local storage address for 1065796c8dcSSimon Schubert DW_OP_GNU_push_tls_address. */ 1075796c8dcSSimon Schubert CORE_ADDR (*get_tls_address) (void *baton, CORE_ADDR offset); 1085796c8dcSSimon Schubert 109cf7f2e2dSJohn Marino /* Execute DW_AT_location expression for the DWARF expression subroutine in 110cf7f2e2dSJohn Marino the DIE at DIE_OFFSET in the CU from CTX. Do not touch STACK while it 111cf7f2e2dSJohn Marino being passed to and returned from the called DWARF subroutine. */ 112cf7f2e2dSJohn Marino void (*dwarf_call) (struct dwarf_expr_context *ctx, size_t die_offset); 113cf7f2e2dSJohn Marino 1145796c8dcSSimon Schubert #if 0 1155796c8dcSSimon Schubert /* Not yet implemented. */ 1165796c8dcSSimon Schubert 1175796c8dcSSimon Schubert /* Return the `object address' for DW_OP_push_object_address. */ 1185796c8dcSSimon Schubert CORE_ADDR (*get_object_address) (void *baton); 1195796c8dcSSimon Schubert #endif 1205796c8dcSSimon Schubert 1215796c8dcSSimon Schubert /* The current depth of dwarf expression recursion, via DW_OP_call*, 1225796c8dcSSimon Schubert DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum 1235796c8dcSSimon Schubert depth we'll tolerate before raising an error. */ 1245796c8dcSSimon Schubert int recursion_depth, max_recursion_depth; 1255796c8dcSSimon Schubert 1265796c8dcSSimon Schubert /* Location of the value. */ 1275796c8dcSSimon Schubert enum dwarf_value_location location; 1285796c8dcSSimon Schubert 129*c50c785cSJohn Marino /* For DWARF_VALUE_LITERAL, the current literal value's length and 130*c50c785cSJohn Marino data. For DWARF_VALUE_IMPLICIT_POINTER, LEN is the offset of the 131*c50c785cSJohn Marino target DIE. */ 1325796c8dcSSimon Schubert ULONGEST len; 133cf7f2e2dSJohn Marino const gdb_byte *data; 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert /* Initialization status of variable: Non-zero if variable has been 1365796c8dcSSimon Schubert initialized; zero otherwise. */ 1375796c8dcSSimon Schubert int initialized; 1385796c8dcSSimon Schubert 1395796c8dcSSimon Schubert /* An array of pieces. PIECES points to its first element; 1405796c8dcSSimon Schubert NUM_PIECES is its length. 1415796c8dcSSimon Schubert 1425796c8dcSSimon Schubert Each time DW_OP_piece is executed, we add a new element to the 1435796c8dcSSimon Schubert end of this array, recording the current top of the stack, the 1445796c8dcSSimon Schubert current location, and the size given as the operand to 1455796c8dcSSimon Schubert DW_OP_piece. We then pop the top value from the stack, reset the 1465796c8dcSSimon Schubert location, and resume evaluation. 1475796c8dcSSimon Schubert 1485796c8dcSSimon Schubert The Dwarf spec doesn't say whether DW_OP_piece pops the top value 1495796c8dcSSimon Schubert from the stack. We do, ensuring that clients of this interface 1505796c8dcSSimon Schubert expecting to see a value left on the top of the stack (say, code 1515796c8dcSSimon Schubert evaluating frame base expressions or CFA's specified with 1525796c8dcSSimon Schubert DW_CFA_def_cfa_expression) will get an error if the expression 1535796c8dcSSimon Schubert actually marks all the values it computes as pieces. 1545796c8dcSSimon Schubert 1555796c8dcSSimon Schubert If an expression never uses DW_OP_piece, num_pieces will be zero. 1565796c8dcSSimon Schubert (It would be nice to present these cases as expressions yielding 1575796c8dcSSimon Schubert a single piece, so that callers need not distinguish between the 1585796c8dcSSimon Schubert no-DW_OP_piece and one-DW_OP_piece cases. But expressions with 1595796c8dcSSimon Schubert no DW_OP_piece operations have no value to place in a piece's 1605796c8dcSSimon Schubert 'size' field; the size comes from the surrounding data. So the 1615796c8dcSSimon Schubert two cases need to be handled separately.) */ 1625796c8dcSSimon Schubert int num_pieces; 1635796c8dcSSimon Schubert struct dwarf_expr_piece *pieces; 1645796c8dcSSimon Schubert }; 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert 167cf7f2e2dSJohn Marino /* A piece of an object, as recorded by DW_OP_piece or DW_OP_bit_piece. */ 1685796c8dcSSimon Schubert struct dwarf_expr_piece 1695796c8dcSSimon Schubert { 1705796c8dcSSimon Schubert enum dwarf_value_location location; 1715796c8dcSSimon Schubert 1725796c8dcSSimon Schubert union 1735796c8dcSSimon Schubert { 1745796c8dcSSimon Schubert struct 1755796c8dcSSimon Schubert { 176cf7f2e2dSJohn Marino /* This piece's address, for DWARF_VALUE_MEMORY pieces. */ 177cf7f2e2dSJohn Marino CORE_ADDR addr; 1785796c8dcSSimon Schubert /* Non-zero if the piece is known to be in memory and on 1795796c8dcSSimon Schubert the program's stack. */ 1805796c8dcSSimon Schubert int in_stack_memory; 181cf7f2e2dSJohn Marino } mem; 182cf7f2e2dSJohn Marino 183cf7f2e2dSJohn Marino /* The piece's register number or literal value, for 184cf7f2e2dSJohn Marino DWARF_VALUE_REGISTER or DWARF_VALUE_STACK pieces. */ 185cf7f2e2dSJohn Marino ULONGEST value; 1865796c8dcSSimon Schubert 1875796c8dcSSimon Schubert struct 1885796c8dcSSimon Schubert { 189cf7f2e2dSJohn Marino /* A pointer to the data making up this piece, 190cf7f2e2dSJohn Marino for DWARF_VALUE_LITERAL pieces. */ 191cf7f2e2dSJohn Marino const gdb_byte *data; 1925796c8dcSSimon Schubert /* The length of the available data. */ 1935796c8dcSSimon Schubert ULONGEST length; 1945796c8dcSSimon Schubert } literal; 195*c50c785cSJohn Marino 196*c50c785cSJohn Marino /* Used for DWARF_VALUE_IMPLICIT_POINTER. */ 197*c50c785cSJohn Marino struct 198*c50c785cSJohn Marino { 199*c50c785cSJohn Marino /* The referent DIE from DW_OP_GNU_implicit_pointer. */ 200*c50c785cSJohn Marino ULONGEST die; 201*c50c785cSJohn Marino /* The byte offset into the resulting data. */ 202*c50c785cSJohn Marino LONGEST offset; 203*c50c785cSJohn Marino } ptr; 2045796c8dcSSimon Schubert } v; 2055796c8dcSSimon Schubert 206cf7f2e2dSJohn Marino /* The length of the piece, in bits. */ 2075796c8dcSSimon Schubert ULONGEST size; 208cf7f2e2dSJohn Marino /* The piece offset, in bits. */ 209cf7f2e2dSJohn Marino ULONGEST offset; 2105796c8dcSSimon Schubert }; 2115796c8dcSSimon Schubert 2125796c8dcSSimon Schubert struct dwarf_expr_context *new_dwarf_expr_context (void); 2135796c8dcSSimon Schubert void free_dwarf_expr_context (struct dwarf_expr_context *ctx); 2145796c8dcSSimon Schubert struct cleanup * 2155796c8dcSSimon Schubert make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx); 2165796c8dcSSimon Schubert 217cf7f2e2dSJohn Marino void dwarf_expr_push (struct dwarf_expr_context *ctx, ULONGEST value, 2185796c8dcSSimon Schubert int in_stack_memory); 2195796c8dcSSimon Schubert void dwarf_expr_pop (struct dwarf_expr_context *ctx); 220cf7f2e2dSJohn Marino void dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr, 2215796c8dcSSimon Schubert size_t len); 222cf7f2e2dSJohn Marino ULONGEST dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n); 223cf7f2e2dSJohn Marino CORE_ADDR dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n); 2245796c8dcSSimon Schubert int dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n); 2255796c8dcSSimon Schubert 2265796c8dcSSimon Schubert 227cf7f2e2dSJohn Marino const gdb_byte *read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 228cf7f2e2dSJohn Marino ULONGEST * r); 229cf7f2e2dSJohn Marino const gdb_byte *read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 230cf7f2e2dSJohn Marino LONGEST * r); 231cf7f2e2dSJohn Marino 232*c50c785cSJohn Marino const char *dwarf_stack_op_name (unsigned int); 233cf7f2e2dSJohn Marino 234cf7f2e2dSJohn Marino void dwarf_expr_require_composition (const gdb_byte *, const gdb_byte *, 235cf7f2e2dSJohn Marino const char *); 2365796c8dcSSimon Schubert 2375796c8dcSSimon Schubert #endif /* dwarf2expr.h */ 238