15796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator. 25796c8dcSSimon Schubert 3*cf7f2e2dSJohn Marino Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010 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. */ 41*cf7f2e2dSJohn Marino DWARF_VALUE_LITERAL, 42*cf7f2e2dSJohn Marino 43*cf7f2e2dSJohn Marino /* The piece was optimized out. */ 44*cf7f2e2dSJohn Marino DWARF_VALUE_OPTIMIZED_OUT 455796c8dcSSimon Schubert }; 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert /* The dwarf expression stack. */ 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert struct dwarf_stack_value 505796c8dcSSimon Schubert { 51*cf7f2e2dSJohn Marino ULONGEST value; 525796c8dcSSimon Schubert 535796c8dcSSimon Schubert /* Non-zero if the piece is in memory and is known to be 545796c8dcSSimon Schubert on the program's stack. It is always ok to set this to zero. 555796c8dcSSimon Schubert This is used, for example, to optimize memory access from the target. 565796c8dcSSimon Schubert It can vastly speed up backtraces on long latency connections when 575796c8dcSSimon Schubert "set stack-cache on". */ 585796c8dcSSimon Schubert int in_stack_memory; 595796c8dcSSimon Schubert }; 605796c8dcSSimon Schubert 615796c8dcSSimon Schubert /* The expression evaluator works with a dwarf_expr_context, describing 625796c8dcSSimon Schubert its current state and its callbacks. */ 635796c8dcSSimon Schubert struct dwarf_expr_context 645796c8dcSSimon Schubert { 655796c8dcSSimon Schubert /* The stack of values, allocated with xmalloc. */ 665796c8dcSSimon Schubert struct dwarf_stack_value *stack; 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert /* The number of values currently pushed on the stack, and the 695796c8dcSSimon Schubert number of elements allocated to the stack. */ 705796c8dcSSimon Schubert int stack_len, stack_allocated; 715796c8dcSSimon Schubert 725796c8dcSSimon Schubert /* Target architecture to use for address operations. */ 735796c8dcSSimon Schubert struct gdbarch *gdbarch; 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert /* Target address size in bytes. */ 765796c8dcSSimon Schubert int addr_size; 775796c8dcSSimon Schubert 78*cf7f2e2dSJohn Marino /* Offset used to relocate DW_OP_addr argument. */ 79*cf7f2e2dSJohn Marino CORE_ADDR offset; 80*cf7f2e2dSJohn Marino 815796c8dcSSimon Schubert /* An opaque argument provided by the caller, which will be passed 825796c8dcSSimon Schubert to all of the callback functions. */ 835796c8dcSSimon Schubert void *baton; 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert /* Return the value of register number REGNUM. */ 865796c8dcSSimon Schubert CORE_ADDR (*read_reg) (void *baton, int regnum); 875796c8dcSSimon Schubert 885796c8dcSSimon Schubert /* Read LENGTH bytes at ADDR into BUF. */ 895796c8dcSSimon Schubert void (*read_mem) (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t length); 905796c8dcSSimon Schubert 915796c8dcSSimon Schubert /* Return the location expression for the frame base attribute, in 925796c8dcSSimon Schubert START and LENGTH. The result must be live until the current 935796c8dcSSimon Schubert expression evaluation is complete. */ 94*cf7f2e2dSJohn Marino void (*get_frame_base) (void *baton, const gdb_byte **start, size_t *length); 955796c8dcSSimon Schubert 965796c8dcSSimon Schubert /* Return the CFA for the frame. */ 975796c8dcSSimon Schubert CORE_ADDR (*get_frame_cfa) (void *baton); 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert /* Return the thread-local storage address for 1005796c8dcSSimon Schubert DW_OP_GNU_push_tls_address. */ 1015796c8dcSSimon Schubert CORE_ADDR (*get_tls_address) (void *baton, CORE_ADDR offset); 1025796c8dcSSimon Schubert 103*cf7f2e2dSJohn Marino /* Execute DW_AT_location expression for the DWARF expression subroutine in 104*cf7f2e2dSJohn Marino the DIE at DIE_OFFSET in the CU from CTX. Do not touch STACK while it 105*cf7f2e2dSJohn Marino being passed to and returned from the called DWARF subroutine. */ 106*cf7f2e2dSJohn Marino void (*dwarf_call) (struct dwarf_expr_context *ctx, size_t die_offset); 107*cf7f2e2dSJohn Marino 1085796c8dcSSimon Schubert #if 0 1095796c8dcSSimon Schubert /* Not yet implemented. */ 1105796c8dcSSimon Schubert 1115796c8dcSSimon Schubert /* Return the `object address' for DW_OP_push_object_address. */ 1125796c8dcSSimon Schubert CORE_ADDR (*get_object_address) (void *baton); 1135796c8dcSSimon Schubert #endif 1145796c8dcSSimon Schubert 1155796c8dcSSimon Schubert /* The current depth of dwarf expression recursion, via DW_OP_call*, 1165796c8dcSSimon Schubert DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum 1175796c8dcSSimon Schubert depth we'll tolerate before raising an error. */ 1185796c8dcSSimon Schubert int recursion_depth, max_recursion_depth; 1195796c8dcSSimon Schubert 1205796c8dcSSimon Schubert /* Location of the value. */ 1215796c8dcSSimon Schubert enum dwarf_value_location location; 1225796c8dcSSimon Schubert 1235796c8dcSSimon Schubert /* For VALUE_LITERAL, a the current literal value's length and 1245796c8dcSSimon Schubert data. */ 1255796c8dcSSimon Schubert ULONGEST len; 126*cf7f2e2dSJohn Marino const gdb_byte *data; 1275796c8dcSSimon Schubert 1285796c8dcSSimon Schubert /* Initialization status of variable: Non-zero if variable has been 1295796c8dcSSimon Schubert initialized; zero otherwise. */ 1305796c8dcSSimon Schubert int initialized; 1315796c8dcSSimon Schubert 1325796c8dcSSimon Schubert /* An array of pieces. PIECES points to its first element; 1335796c8dcSSimon Schubert NUM_PIECES is its length. 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert Each time DW_OP_piece is executed, we add a new element to the 1365796c8dcSSimon Schubert end of this array, recording the current top of the stack, the 1375796c8dcSSimon Schubert current location, and the size given as the operand to 1385796c8dcSSimon Schubert DW_OP_piece. We then pop the top value from the stack, reset the 1395796c8dcSSimon Schubert location, and resume evaluation. 1405796c8dcSSimon Schubert 1415796c8dcSSimon Schubert The Dwarf spec doesn't say whether DW_OP_piece pops the top value 1425796c8dcSSimon Schubert from the stack. We do, ensuring that clients of this interface 1435796c8dcSSimon Schubert expecting to see a value left on the top of the stack (say, code 1445796c8dcSSimon Schubert evaluating frame base expressions or CFA's specified with 1455796c8dcSSimon Schubert DW_CFA_def_cfa_expression) will get an error if the expression 1465796c8dcSSimon Schubert actually marks all the values it computes as pieces. 1475796c8dcSSimon Schubert 1485796c8dcSSimon Schubert If an expression never uses DW_OP_piece, num_pieces will be zero. 1495796c8dcSSimon Schubert (It would be nice to present these cases as expressions yielding 1505796c8dcSSimon Schubert a single piece, so that callers need not distinguish between the 1515796c8dcSSimon Schubert no-DW_OP_piece and one-DW_OP_piece cases. But expressions with 1525796c8dcSSimon Schubert no DW_OP_piece operations have no value to place in a piece's 1535796c8dcSSimon Schubert 'size' field; the size comes from the surrounding data. So the 1545796c8dcSSimon Schubert two cases need to be handled separately.) */ 1555796c8dcSSimon Schubert int num_pieces; 1565796c8dcSSimon Schubert struct dwarf_expr_piece *pieces; 1575796c8dcSSimon Schubert }; 1585796c8dcSSimon Schubert 1595796c8dcSSimon Schubert 160*cf7f2e2dSJohn Marino /* A piece of an object, as recorded by DW_OP_piece or DW_OP_bit_piece. */ 1615796c8dcSSimon Schubert struct dwarf_expr_piece 1625796c8dcSSimon Schubert { 1635796c8dcSSimon Schubert enum dwarf_value_location location; 1645796c8dcSSimon Schubert 1655796c8dcSSimon Schubert union 1665796c8dcSSimon Schubert { 1675796c8dcSSimon Schubert struct 1685796c8dcSSimon Schubert { 169*cf7f2e2dSJohn Marino /* This piece's address, for DWARF_VALUE_MEMORY pieces. */ 170*cf7f2e2dSJohn Marino CORE_ADDR addr; 1715796c8dcSSimon Schubert /* Non-zero if the piece is known to be in memory and on 1725796c8dcSSimon Schubert the program's stack. */ 1735796c8dcSSimon Schubert int in_stack_memory; 174*cf7f2e2dSJohn Marino } mem; 175*cf7f2e2dSJohn Marino 176*cf7f2e2dSJohn Marino /* The piece's register number or literal value, for 177*cf7f2e2dSJohn Marino DWARF_VALUE_REGISTER or DWARF_VALUE_STACK pieces. */ 178*cf7f2e2dSJohn Marino ULONGEST value; 1795796c8dcSSimon Schubert 1805796c8dcSSimon Schubert struct 1815796c8dcSSimon Schubert { 182*cf7f2e2dSJohn Marino /* A pointer to the data making up this piece, 183*cf7f2e2dSJohn Marino for DWARF_VALUE_LITERAL pieces. */ 184*cf7f2e2dSJohn Marino const gdb_byte *data; 1855796c8dcSSimon Schubert /* The length of the available data. */ 1865796c8dcSSimon Schubert ULONGEST length; 1875796c8dcSSimon Schubert } literal; 1885796c8dcSSimon Schubert } v; 1895796c8dcSSimon Schubert 190*cf7f2e2dSJohn Marino /* The length of the piece, in bits. */ 1915796c8dcSSimon Schubert ULONGEST size; 192*cf7f2e2dSJohn Marino /* The piece offset, in bits. */ 193*cf7f2e2dSJohn Marino ULONGEST offset; 1945796c8dcSSimon Schubert }; 1955796c8dcSSimon Schubert 1965796c8dcSSimon Schubert struct dwarf_expr_context *new_dwarf_expr_context (void); 1975796c8dcSSimon Schubert void free_dwarf_expr_context (struct dwarf_expr_context *ctx); 1985796c8dcSSimon Schubert struct cleanup * 1995796c8dcSSimon Schubert make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx); 2005796c8dcSSimon Schubert 201*cf7f2e2dSJohn Marino void dwarf_expr_push (struct dwarf_expr_context *ctx, ULONGEST value, 2025796c8dcSSimon Schubert int in_stack_memory); 2035796c8dcSSimon Schubert void dwarf_expr_pop (struct dwarf_expr_context *ctx); 204*cf7f2e2dSJohn Marino void dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr, 2055796c8dcSSimon Schubert size_t len); 206*cf7f2e2dSJohn Marino ULONGEST dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n); 207*cf7f2e2dSJohn Marino CORE_ADDR dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n); 2085796c8dcSSimon Schubert int dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n); 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert 211*cf7f2e2dSJohn Marino const gdb_byte *read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 212*cf7f2e2dSJohn Marino ULONGEST * r); 213*cf7f2e2dSJohn Marino const gdb_byte *read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 214*cf7f2e2dSJohn Marino LONGEST * r); 215*cf7f2e2dSJohn Marino 216*cf7f2e2dSJohn Marino const char *dwarf_stack_op_name (unsigned int, int); 217*cf7f2e2dSJohn Marino 218*cf7f2e2dSJohn Marino void dwarf_expr_require_composition (const gdb_byte *, const gdb_byte *, 219*cf7f2e2dSJohn Marino const char *); 2205796c8dcSSimon Schubert 2215796c8dcSSimon Schubert #endif /* dwarf2expr.h */ 222