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);
46 // Push the CFA (Canonical Frame Addresse):
47 case DW_OP_call_frame_cfa:
49 // UNW_X86_64_CFA does not return the CFA DWARF expects
50 // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
51 // unwind it once in order to find the parent SP:
54 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
57 unw_cursor_t cursor = *(state->cursor);
61 unw_get_reg(&cursor, UNW_TDEP_SP, &res);
62 error = mc_dwarf_push_value(state, res);
70 if(!state->frame_base)
71 return MC_EXPRESSION_E_MISSING_FRAME_BASE;
72 error = mc_dwarf_push_value(state, ((uintptr_t)state->frame_base) + op->number);
79 case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3:
80 case DW_OP_lit4: case DW_OP_lit5: case DW_OP_lit6: case DW_OP_lit7:
81 case DW_OP_lit8: case DW_OP_lit9: case DW_OP_lit10: case DW_OP_lit11:
82 case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14: case DW_OP_lit15:
83 case DW_OP_lit16: case DW_OP_lit17: case DW_OP_lit18: case DW_OP_lit19:
84 case DW_OP_lit20: case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23:
85 case DW_OP_lit24: case DW_OP_lit25: case DW_OP_lit26: case DW_OP_lit27:
86 case DW_OP_lit28: case DW_OP_lit29: case DW_OP_lit30: case DW_OP_lit31:
87 error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
101 if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
102 return MC_EXPRESSION_E_STACK_OVERFLOW;
103 error = mc_dwarf_push_value(state, op->number);
106 // Stack manipulation:
108 // Push the value at the top of the stack:
110 if(state->stack_size==0)
111 return MC_EXPRESSION_E_STACK_UNDERFLOW;
113 error = mc_dwarf_push_value(state, state->stack[state->stack_size-1]);
117 if(state->stack_size==0)
118 return MC_EXPRESSION_E_STACK_UNDERFLOW;
124 if(state->stack_size<2)
125 return MC_EXPRESSION_E_STACK_UNDERFLOW;
127 uintptr_t temp = state->stack[state->stack_size-2];
128 state->stack[state->stack_size-2] = state->stack[state->stack_size-1];
129 state->stack[state->stack_size-1] = temp;
134 if(state->stack_size<2)
135 return MC_EXPRESSION_E_STACK_UNDERFLOW;
136 error = mc_dwarf_push_value(state, state->stack[state->stack_size-2]);
142 if(state->stack_size<2)
143 return MC_EXPRESSION_E_STACK_UNDERFLOW;
145 uintptr_t result = state->stack[state->stack_size-2] + state->stack[state->stack_size-1];
146 state->stack[state->stack_size-2] = result;
152 if(state->stack_size<2)
153 return MC_EXPRESSION_E_STACK_UNDERFLOW;
155 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
156 state->stack[state->stack_size-2] = result;
161 case DW_OP_plus_uconst:
162 if(state->stack_size==0)
163 return MC_EXPRESSION_E_STACK_UNDERFLOW;
164 state->stack[state->stack_size-1] += op->number;
168 if(state->stack_size==0)
169 return MC_EXPRESSION_E_STACK_UNDERFLOW;
170 state->stack[state->stack_size-1] = ~state->stack[state->stack_size-1];
174 if(state->stack_size==0)
175 return MC_EXPRESSION_E_STACK_UNDERFLOW;
177 intptr_t value = state->stack[state->stack_size-1];
178 if(value<0) value = -value;
179 state->stack[state->stack_size-1] = value;
184 if(state->stack_size<2)
185 return MC_EXPRESSION_E_STACK_UNDERFLOW;
187 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
188 state->stack[state->stack_size-2] = result;
194 if(state->stack_size<2)
195 return MC_EXPRESSION_E_STACK_UNDERFLOW;
197 uintptr_t result = state->stack[state->stack_size-2] & state->stack[state->stack_size-1];
198 state->stack[state->stack_size-2] = result;
204 if(state->stack_size<2)
205 return MC_EXPRESSION_E_STACK_UNDERFLOW;
207 uintptr_t result = state->stack[state->stack_size-2] | state->stack[state->stack_size-1];
208 state->stack[state->stack_size-2] = result;
214 if(state->stack_size<2)
215 return MC_EXPRESSION_E_STACK_UNDERFLOW;
217 uintptr_t result = state->stack[state->stack_size-2] ^ state->stack[state->stack_size-1];
218 state->stack[state->stack_size-2] = result;
227 case DW_OP_deref_size:
228 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
231 if(state->stack_size==0)
232 return MC_EXPRESSION_E_STACK_UNDERFLOW;
235 uintptr_t address = (uintptr_t) state->stack[state->stack_size-1];
236 uintptr_t* p = (uintptr_t*)mc_translate_address(address, state->snapshot);
237 state->stack[state->stack_size-1] = *p;
243 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
246 if(error) return error;
253 /** \brief Resolve a location expression
254 * \deprecated Use mc_dwarf_resolve_expression
256 Dwarf_Off mc_dwarf_resolve_location(mc_expression_t expression, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
257 s_mc_expression_state_t state;
258 memset(&state, 0, sizeof(s_mc_expression_state_t));
259 state.frame_base = frame_pointer_address;
261 state.snapshot = snapshot;
263 if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
264 xbt_die("Error evaluating DWARF expression");
265 if(state.stack_size==0)
266 xbt_die("No value on the stack");
268 return (Dwarf_Off) state.stack[state.stack_size-1];
271 Dwarf_Off mc_dwarf_resolve_locations(mc_location_list_t locations, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
275 if(unw_get_reg(c, UNW_REG_IP, &ip))
276 xbt_die("Could not resolve IP");
279 for(size_t i=0; i!=locations->size; ++i) {
280 mc_expression_t expression = locations->locations + i;
281 if( (expression->lowpc==NULL && expression->highpc==NULL)
282 || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
283 return mc_dwarf_resolve_location(expression, c, frame_pointer_address, snapshot);
286 xbt_die("Could not resolve location");
289 /** \brief Find the frame base of a given frame
294 void* mc_find_frame_base(dw_frame_t frame, unw_cursor_t* unw_cursor) {
295 return (void*) mc_dwarf_resolve_locations(&frame->frame_base, unw_cursor, NULL, NULL);
298 void mc_dwarf_expression_clear(mc_expression_t expression) {
299 free(expression->ops);
300 expression->ops = NULL;
301 expression->size = 0;
302 expression->lowpc = NULL;
303 expression->highpc = NULL;
306 void mc_dwarf_location_list_clear(mc_location_list_t list) {
307 for(size_t i=0; i!=list->size; ++i) {
308 mc_dwarf_expression_clear(list->locations + i);
310 free(list->locations);
311 list->locations = NULL;
315 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
316 if(expression->ops) {
317 free(expression->ops);
319 expression->lowpc = NULL;
320 expression->highpc = NULL;
321 expression->size = len;
322 expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
323 memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
326 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
327 if(target->locations) {
328 mc_dwarf_location_list_clear(target);
331 target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
332 mc_dwarf_expression_init(target->locations, len, ops);
335 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
336 if(list->locations) {
337 mc_dwarf_location_list_clear(list);
341 ptrdiff_t offset = 0;
342 Dwarf_Addr base, start, end;
348 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
352 xbt_die("Error while loading location list");
356 list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
357 mc_expression_t expression = list->locations + i;
359 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
360 mc_dwarf_expression_init(expression, len, ops);
362 // If start == 0, this is not a location list:
363 expression->lowpc = start == 0 ? NULL : (char*) base + start;
364 expression->highpc = start == 0 ? NULL : (char*) base + end;