6 #include <elfutils/libdw.h>
8 #include "mc_private.h"
10 static int mc_dwarf_push_value(mc_expression_state_t state, Dwarf_Off value) {
11 if(state->stack_size>=MC_EXPRESSION_STACK_SIZE)
12 return MC_EXPRESSION_E_STACK_OVERFLOW;
14 state->stack[state->stack_size++] = value;
18 int mc_dwarf_execute_expression(
19 size_t n, const Dwarf_Op* ops, mc_expression_state_t state) {
20 for(int i=0; i!=n; ++i) {
22 const Dwarf_Op* op = ops + i;
23 uint8_t atom = op->atom;
29 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
30 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
31 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
32 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
33 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
34 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
35 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
36 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:{
37 int register_id = op->atom - DW_OP_breg0;
40 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
41 unw_get_reg(state->cursor, register_id, &res);
42 error = mc_dwarf_push_value(state, res + op->number);
50 if(!state->frame_base)
51 return MC_EXPRESSION_E_MISSING_FRAME_BASE;
52 error = mc_dwarf_push_value(state, ((uintptr_t)state->frame_base) + op->number);
58 case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3:
59 case DW_OP_lit4: case DW_OP_lit5: case DW_OP_lit6: case DW_OP_lit7:
60 case DW_OP_lit8: case DW_OP_lit9: case DW_OP_lit10: case DW_OP_lit11:
61 case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14: case DW_OP_lit15:
62 case DW_OP_lit16: case DW_OP_lit17: case DW_OP_lit18: case DW_OP_lit19:
63 case DW_OP_lit20: case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23:
64 case DW_OP_lit24: case DW_OP_lit25: case DW_OP_lit26: case DW_OP_lit27:
65 case DW_OP_lit28: case DW_OP_lit29: case DW_OP_lit30: case DW_OP_lit31:
66 error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
80 if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
81 return MC_EXPRESSION_E_STACK_OVERFLOW;
82 error = mc_dwarf_push_value(state, op->number);
85 // Stack manipulation:
87 // Push the value at the top of the stack:
89 if(state->stack_size==0)
90 return MC_EXPRESSION_E_STACK_UNDERFLOW;
92 error = mc_dwarf_push_value(state, state->stack[state->stack_size-1]);
96 if(state->stack_size==0)
97 return MC_EXPRESSION_E_STACK_UNDERFLOW;
103 if(state->stack_size<2)
104 return MC_EXPRESSION_E_STACK_UNDERFLOW;
106 uintptr_t temp = state->stack[state->stack_size-2];
107 state->stack[state->stack_size-2] = state->stack[state->stack_size-1];
108 state->stack[state->stack_size-1] = temp;
113 if(state->stack_size<2)
114 return MC_EXPRESSION_E_STACK_UNDERFLOW;
115 error = mc_dwarf_push_value(state, state->stack[state->stack_size-2]);
121 if(state->stack_size<2)
122 return MC_EXPRESSION_E_STACK_UNDERFLOW;
124 uintptr_t result = state->stack[state->stack_size-2] + state->stack[state->stack_size-1];
125 state->stack[state->stack_size-2] = result;
131 if(state->stack_size<2)
132 return MC_EXPRESSION_E_STACK_UNDERFLOW;
134 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
135 state->stack[state->stack_size-2] = result;
140 case DW_OP_plus_uconst:
141 if(state->stack_size==0)
142 return MC_EXPRESSION_E_STACK_UNDERFLOW;
143 state->stack[state->stack_size-1] += op->number;
147 if(state->stack_size==0)
148 return MC_EXPRESSION_E_STACK_UNDERFLOW;
149 state->stack[state->stack_size-1] = ~state->stack[state->stack_size-1];
153 if(state->stack_size==0)
154 return MC_EXPRESSION_E_STACK_UNDERFLOW;
156 intptr_t value = state->stack[state->stack_size-1];
157 if(value<0) value = -value;
158 state->stack[state->stack_size-1] = value;
163 if(state->stack_size<2)
164 return MC_EXPRESSION_E_STACK_UNDERFLOW;
166 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
167 state->stack[state->stack_size-2] = result;
173 if(state->stack_size<2)
174 return MC_EXPRESSION_E_STACK_UNDERFLOW;
176 uintptr_t result = state->stack[state->stack_size-2] & state->stack[state->stack_size-1];
177 state->stack[state->stack_size-2] = result;
183 if(state->stack_size<2)
184 return MC_EXPRESSION_E_STACK_UNDERFLOW;
186 uintptr_t result = state->stack[state->stack_size-2] | state->stack[state->stack_size-1];
187 state->stack[state->stack_size-2] = result;
193 if(state->stack_size<2)
194 return MC_EXPRESSION_E_STACK_UNDERFLOW;
196 uintptr_t result = state->stack[state->stack_size-2] ^ state->stack[state->stack_size-1];
197 state->stack[state->stack_size-2] = result;
206 case DW_OP_deref_size:
208 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
212 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
215 if(error) return error;
222 /** \brief Resolve a location expression
223 * \deprecated Use mc_dwarf_resolve_expression
225 Dwarf_Off mc_dwarf_resolve_location(mc_expression_t expression, unw_cursor_t* c, void* frame_pointer_address) {
226 s_mc_expression_state_t state;
227 memset(&state, 0, sizeof(s_mc_expression_state_t));
228 state.frame_base = frame_pointer_address;
231 if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
232 xbt_die("Error evaluating DWARF expression");
233 if(state.stack_size==0)
234 xbt_die("No value on the stack");
236 return (Dwarf_Off) state.stack[state.stack_size-1];