xref: /dflybsd-src/contrib/gdb-7/gdb/dwarf2expr.h (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009
4*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    Contributed by Daniel Berlin <dan@dberlin.org>.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This file is part of GDB.
9*5796c8dcSSimon Schubert 
10*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
11*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
12*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
13*5796c8dcSSimon Schubert    (at your option) any later version.
14*5796c8dcSSimon Schubert 
15*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
16*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
17*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*5796c8dcSSimon Schubert    GNU General Public License for more details.
19*5796c8dcSSimon Schubert 
20*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
21*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22*5796c8dcSSimon Schubert 
23*5796c8dcSSimon Schubert #if !defined (DWARF2EXPR_H)
24*5796c8dcSSimon Schubert #define DWARF2EXPR_H
25*5796c8dcSSimon Schubert 
26*5796c8dcSSimon Schubert /* The location of a value.  */
27*5796c8dcSSimon Schubert enum dwarf_value_location
28*5796c8dcSSimon Schubert {
29*5796c8dcSSimon Schubert   /* The piece is in memory.
30*5796c8dcSSimon Schubert      The value on the dwarf stack is its address.  */
31*5796c8dcSSimon Schubert   DWARF_VALUE_MEMORY,
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert   /* The piece is in a register.
34*5796c8dcSSimon Schubert      The value on the dwarf stack is the register number.  */
35*5796c8dcSSimon Schubert   DWARF_VALUE_REGISTER,
36*5796c8dcSSimon Schubert 
37*5796c8dcSSimon Schubert   /* The piece is on the dwarf stack.  */
38*5796c8dcSSimon Schubert   DWARF_VALUE_STACK,
39*5796c8dcSSimon Schubert 
40*5796c8dcSSimon Schubert   /* The piece is a literal.  */
41*5796c8dcSSimon Schubert   DWARF_VALUE_LITERAL
42*5796c8dcSSimon Schubert };
43*5796c8dcSSimon Schubert 
44*5796c8dcSSimon Schubert /* The dwarf expression stack.  */
45*5796c8dcSSimon Schubert 
46*5796c8dcSSimon Schubert struct dwarf_stack_value
47*5796c8dcSSimon Schubert {
48*5796c8dcSSimon Schubert   CORE_ADDR value;
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert   /* Non-zero if the piece is in memory and is known to be
51*5796c8dcSSimon Schubert      on the program's stack.  It is always ok to set this to zero.
52*5796c8dcSSimon Schubert      This is used, for example, to optimize memory access from the target.
53*5796c8dcSSimon Schubert      It can vastly speed up backtraces on long latency connections when
54*5796c8dcSSimon Schubert      "set stack-cache on".  */
55*5796c8dcSSimon Schubert   int in_stack_memory;
56*5796c8dcSSimon Schubert };
57*5796c8dcSSimon Schubert 
58*5796c8dcSSimon Schubert /* The expression evaluator works with a dwarf_expr_context, describing
59*5796c8dcSSimon Schubert    its current state and its callbacks.  */
60*5796c8dcSSimon Schubert struct dwarf_expr_context
61*5796c8dcSSimon Schubert {
62*5796c8dcSSimon Schubert   /* The stack of values, allocated with xmalloc.  */
63*5796c8dcSSimon Schubert   struct dwarf_stack_value *stack;
64*5796c8dcSSimon Schubert 
65*5796c8dcSSimon Schubert   /* The number of values currently pushed on the stack, and the
66*5796c8dcSSimon Schubert      number of elements allocated to the stack.  */
67*5796c8dcSSimon Schubert   int stack_len, stack_allocated;
68*5796c8dcSSimon Schubert 
69*5796c8dcSSimon Schubert   /* Target architecture to use for address operations.  */
70*5796c8dcSSimon Schubert   struct gdbarch *gdbarch;
71*5796c8dcSSimon Schubert 
72*5796c8dcSSimon Schubert   /* Target address size in bytes.  */
73*5796c8dcSSimon Schubert   int addr_size;
74*5796c8dcSSimon Schubert 
75*5796c8dcSSimon Schubert   /* An opaque argument provided by the caller, which will be passed
76*5796c8dcSSimon Schubert      to all of the callback functions.  */
77*5796c8dcSSimon Schubert   void *baton;
78*5796c8dcSSimon Schubert 
79*5796c8dcSSimon Schubert   /* Return the value of register number REGNUM.  */
80*5796c8dcSSimon Schubert   CORE_ADDR (*read_reg) (void *baton, int regnum);
81*5796c8dcSSimon Schubert 
82*5796c8dcSSimon Schubert   /* Read LENGTH bytes at ADDR into BUF.  */
83*5796c8dcSSimon Schubert   void (*read_mem) (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t length);
84*5796c8dcSSimon Schubert 
85*5796c8dcSSimon Schubert   /* Return the location expression for the frame base attribute, in
86*5796c8dcSSimon Schubert      START and LENGTH.  The result must be live until the current
87*5796c8dcSSimon Schubert      expression evaluation is complete.  */
88*5796c8dcSSimon Schubert   void (*get_frame_base) (void *baton, gdb_byte **start, size_t *length);
89*5796c8dcSSimon Schubert 
90*5796c8dcSSimon Schubert   /* Return the CFA for the frame.  */
91*5796c8dcSSimon Schubert   CORE_ADDR (*get_frame_cfa) (void *baton);
92*5796c8dcSSimon Schubert 
93*5796c8dcSSimon Schubert   /* Return the thread-local storage address for
94*5796c8dcSSimon Schubert      DW_OP_GNU_push_tls_address.  */
95*5796c8dcSSimon Schubert   CORE_ADDR (*get_tls_address) (void *baton, CORE_ADDR offset);
96*5796c8dcSSimon Schubert 
97*5796c8dcSSimon Schubert #if 0
98*5796c8dcSSimon Schubert   /* Not yet implemented.  */
99*5796c8dcSSimon Schubert 
100*5796c8dcSSimon Schubert   /* Return the location expression for the dwarf expression
101*5796c8dcSSimon Schubert      subroutine in the die at OFFSET in the current compilation unit.
102*5796c8dcSSimon Schubert      The result must be live until the current expression evaluation
103*5796c8dcSSimon Schubert      is complete.  */
104*5796c8dcSSimon Schubert   unsigned char *(*get_subr) (void *baton, off_t offset, size_t *length);
105*5796c8dcSSimon Schubert 
106*5796c8dcSSimon Schubert   /* Return the `object address' for DW_OP_push_object_address.  */
107*5796c8dcSSimon Schubert   CORE_ADDR (*get_object_address) (void *baton);
108*5796c8dcSSimon Schubert #endif
109*5796c8dcSSimon Schubert 
110*5796c8dcSSimon Schubert   /* The current depth of dwarf expression recursion, via DW_OP_call*,
111*5796c8dcSSimon Schubert      DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum
112*5796c8dcSSimon Schubert      depth we'll tolerate before raising an error.  */
113*5796c8dcSSimon Schubert   int recursion_depth, max_recursion_depth;
114*5796c8dcSSimon Schubert 
115*5796c8dcSSimon Schubert   /* Location of the value.  */
116*5796c8dcSSimon Schubert   enum dwarf_value_location location;
117*5796c8dcSSimon Schubert 
118*5796c8dcSSimon Schubert   /* For VALUE_LITERAL, a the current literal value's length and
119*5796c8dcSSimon Schubert      data.  */
120*5796c8dcSSimon Schubert   ULONGEST len;
121*5796c8dcSSimon Schubert   gdb_byte *data;
122*5796c8dcSSimon Schubert 
123*5796c8dcSSimon Schubert   /* Initialization status of variable: Non-zero if variable has been
124*5796c8dcSSimon Schubert      initialized; zero otherwise.  */
125*5796c8dcSSimon Schubert   int initialized;
126*5796c8dcSSimon Schubert 
127*5796c8dcSSimon Schubert   /* An array of pieces.  PIECES points to its first element;
128*5796c8dcSSimon Schubert      NUM_PIECES is its length.
129*5796c8dcSSimon Schubert 
130*5796c8dcSSimon Schubert      Each time DW_OP_piece is executed, we add a new element to the
131*5796c8dcSSimon Schubert      end of this array, recording the current top of the stack, the
132*5796c8dcSSimon Schubert      current location, and the size given as the operand to
133*5796c8dcSSimon Schubert      DW_OP_piece.  We then pop the top value from the stack, reset the
134*5796c8dcSSimon Schubert      location, and resume evaluation.
135*5796c8dcSSimon Schubert 
136*5796c8dcSSimon Schubert      The Dwarf spec doesn't say whether DW_OP_piece pops the top value
137*5796c8dcSSimon Schubert      from the stack.  We do, ensuring that clients of this interface
138*5796c8dcSSimon Schubert      expecting to see a value left on the top of the stack (say, code
139*5796c8dcSSimon Schubert      evaluating frame base expressions or CFA's specified with
140*5796c8dcSSimon Schubert      DW_CFA_def_cfa_expression) will get an error if the expression
141*5796c8dcSSimon Schubert      actually marks all the values it computes as pieces.
142*5796c8dcSSimon Schubert 
143*5796c8dcSSimon Schubert      If an expression never uses DW_OP_piece, num_pieces will be zero.
144*5796c8dcSSimon Schubert      (It would be nice to present these cases as expressions yielding
145*5796c8dcSSimon Schubert      a single piece, so that callers need not distinguish between the
146*5796c8dcSSimon Schubert      no-DW_OP_piece and one-DW_OP_piece cases.  But expressions with
147*5796c8dcSSimon Schubert      no DW_OP_piece operations have no value to place in a piece's
148*5796c8dcSSimon Schubert      'size' field; the size comes from the surrounding data.  So the
149*5796c8dcSSimon Schubert      two cases need to be handled separately.)  */
150*5796c8dcSSimon Schubert   int num_pieces;
151*5796c8dcSSimon Schubert   struct dwarf_expr_piece *pieces;
152*5796c8dcSSimon Schubert };
153*5796c8dcSSimon Schubert 
154*5796c8dcSSimon Schubert 
155*5796c8dcSSimon Schubert /* A piece of an object, as recorded by DW_OP_piece.  */
156*5796c8dcSSimon Schubert struct dwarf_expr_piece
157*5796c8dcSSimon Schubert {
158*5796c8dcSSimon Schubert   enum dwarf_value_location location;
159*5796c8dcSSimon Schubert 
160*5796c8dcSSimon Schubert   union
161*5796c8dcSSimon Schubert   {
162*5796c8dcSSimon Schubert     struct
163*5796c8dcSSimon Schubert     {
164*5796c8dcSSimon Schubert       /* This piece's address or register number.  */
165*5796c8dcSSimon Schubert       CORE_ADDR value;
166*5796c8dcSSimon Schubert       /* Non-zero if the piece is known to be in memory and on
167*5796c8dcSSimon Schubert 	 the program's stack.  */
168*5796c8dcSSimon Schubert       int in_stack_memory;
169*5796c8dcSSimon Schubert     } expr;
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert     struct
172*5796c8dcSSimon Schubert     {
173*5796c8dcSSimon Schubert       /* A pointer to the data making up this piece, for literal
174*5796c8dcSSimon Schubert 	 pieces.  */
175*5796c8dcSSimon Schubert       gdb_byte *data;
176*5796c8dcSSimon Schubert       /* The length of the available data.  */
177*5796c8dcSSimon Schubert       ULONGEST length;
178*5796c8dcSSimon Schubert     } literal;
179*5796c8dcSSimon Schubert   } v;
180*5796c8dcSSimon Schubert 
181*5796c8dcSSimon Schubert   /* The length of the piece, in bytes.  */
182*5796c8dcSSimon Schubert   ULONGEST size;
183*5796c8dcSSimon Schubert };
184*5796c8dcSSimon Schubert 
185*5796c8dcSSimon Schubert struct dwarf_expr_context *new_dwarf_expr_context (void);
186*5796c8dcSSimon Schubert void free_dwarf_expr_context (struct dwarf_expr_context *ctx);
187*5796c8dcSSimon Schubert struct cleanup *
188*5796c8dcSSimon Schubert     make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx);
189*5796c8dcSSimon Schubert 
190*5796c8dcSSimon Schubert void dwarf_expr_push (struct dwarf_expr_context *ctx, CORE_ADDR value,
191*5796c8dcSSimon Schubert 		      int in_stack_memory);
192*5796c8dcSSimon Schubert void dwarf_expr_pop (struct dwarf_expr_context *ctx);
193*5796c8dcSSimon Schubert void dwarf_expr_eval (struct dwarf_expr_context *ctx, unsigned char *addr,
194*5796c8dcSSimon Schubert 		      size_t len);
195*5796c8dcSSimon Schubert CORE_ADDR dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n);
196*5796c8dcSSimon Schubert int dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n);
197*5796c8dcSSimon Schubert 
198*5796c8dcSSimon Schubert 
199*5796c8dcSSimon Schubert gdb_byte *read_uleb128 (gdb_byte *buf, gdb_byte *buf_end, ULONGEST * r);
200*5796c8dcSSimon Schubert gdb_byte *read_sleb128 (gdb_byte *buf, gdb_byte *buf_end, LONGEST * r);
201*5796c8dcSSimon Schubert CORE_ADDR dwarf2_read_address (struct gdbarch *gdbarch, gdb_byte *buf,
202*5796c8dcSSimon Schubert 			       gdb_byte *buf_end, int addr_size);
203*5796c8dcSSimon Schubert 
204*5796c8dcSSimon Schubert #endif /* dwarf2expr.h */
205