1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <elfutils/libdw.h>
13 #include "mc_private.h"
15 static int mc_dwarf_push_value(mc_expression_state_t state, Dwarf_Off value) {
16 if(state->stack_size>=MC_EXPRESSION_STACK_SIZE)
17 return MC_EXPRESSION_E_STACK_OVERFLOW;
19 state->stack[state->stack_size++] = value;
23 int mc_dwarf_execute_expression(
24 size_t n, const Dwarf_Op* ops, mc_expression_state_t state) {
25 for(int i=0; i!=n; ++i) {
27 const Dwarf_Op* op = ops + i;
28 uint8_t atom = op->atom;
34 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
35 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
36 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
37 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
38 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
39 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
40 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
41 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:{
42 int register_id = op->atom - DW_OP_breg0;
45 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
46 unw_get_reg(state->cursor, register_id, &res);
47 error = mc_dwarf_push_value(state, res + op->number);
51 // Push the CFA (Canonical Frame Addresse):
52 case DW_OP_call_frame_cfa:
54 // UNW_X86_64_CFA does not return the CFA DWARF expects
55 // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
56 // unwind it once in order to find the parent SP:
59 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
62 unw_cursor_t cursor = *(state->cursor);
66 unw_get_reg(&cursor, UNW_TDEP_SP, &res);
67 error = mc_dwarf_push_value(state, res);
75 if(!state->frame_base)
76 return MC_EXPRESSION_E_MISSING_FRAME_BASE;
77 error = mc_dwarf_push_value(state, ((uintptr_t)state->frame_base) + op->number);
84 case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3:
85 case DW_OP_lit4: case DW_OP_lit5: case DW_OP_lit6: case DW_OP_lit7:
86 case DW_OP_lit8: case DW_OP_lit9: case DW_OP_lit10: case DW_OP_lit11:
87 case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14: case DW_OP_lit15:
88 case DW_OP_lit16: case DW_OP_lit17: case DW_OP_lit18: case DW_OP_lit19:
89 case DW_OP_lit20: case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23:
90 case DW_OP_lit24: case DW_OP_lit25: case DW_OP_lit26: case DW_OP_lit27:
91 case DW_OP_lit28: case DW_OP_lit29: case DW_OP_lit30: case DW_OP_lit31:
92 error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
96 if(!state->object_info)
97 return MC_EXPRESSION_E_NO_BASE_ADDRESS;
98 if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
99 return MC_EXPRESSION_E_STACK_OVERFLOW;
100 error = mc_dwarf_push_value(state,
101 (Dwarf_Off)(uintptr_t)MC_object_base_address(state->object_info) + op->number);
114 if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
115 return MC_EXPRESSION_E_STACK_OVERFLOW;
116 error = mc_dwarf_push_value(state, op->number);
119 // Stack manipulation:
121 // Push the value at the top of the stack:
123 if(state->stack_size==0)
124 return MC_EXPRESSION_E_STACK_UNDERFLOW;
126 error = mc_dwarf_push_value(state, state->stack[state->stack_size-1]);
130 if(state->stack_size==0)
131 return MC_EXPRESSION_E_STACK_UNDERFLOW;
137 if(state->stack_size<2)
138 return MC_EXPRESSION_E_STACK_UNDERFLOW;
140 uintptr_t temp = state->stack[state->stack_size-2];
141 state->stack[state->stack_size-2] = state->stack[state->stack_size-1];
142 state->stack[state->stack_size-1] = temp;
147 if(state->stack_size<2)
148 return MC_EXPRESSION_E_STACK_UNDERFLOW;
149 error = mc_dwarf_push_value(state, state->stack[state->stack_size-2]);
155 if(state->stack_size<2)
156 return MC_EXPRESSION_E_STACK_UNDERFLOW;
158 uintptr_t result = state->stack[state->stack_size-2] + state->stack[state->stack_size-1];
159 state->stack[state->stack_size-2] = result;
165 if(state->stack_size<2)
166 return MC_EXPRESSION_E_STACK_UNDERFLOW;
168 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
169 state->stack[state->stack_size-2] = result;
174 case DW_OP_plus_uconst:
175 if(state->stack_size==0)
176 return MC_EXPRESSION_E_STACK_UNDERFLOW;
177 state->stack[state->stack_size-1] += op->number;
181 if(state->stack_size==0)
182 return MC_EXPRESSION_E_STACK_UNDERFLOW;
183 state->stack[state->stack_size-1] = ~state->stack[state->stack_size-1];
187 if(state->stack_size==0)
188 return MC_EXPRESSION_E_STACK_UNDERFLOW;
190 intptr_t value = state->stack[state->stack_size-1];
191 if(value<0) value = -value;
192 state->stack[state->stack_size-1] = value;
197 if(state->stack_size<2)
198 return MC_EXPRESSION_E_STACK_UNDERFLOW;
200 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
201 state->stack[state->stack_size-2] = result;
207 if(state->stack_size<2)
208 return MC_EXPRESSION_E_STACK_UNDERFLOW;
210 uintptr_t result = state->stack[state->stack_size-2] & state->stack[state->stack_size-1];
211 state->stack[state->stack_size-2] = result;
217 if(state->stack_size<2)
218 return MC_EXPRESSION_E_STACK_UNDERFLOW;
220 uintptr_t result = state->stack[state->stack_size-2] | state->stack[state->stack_size-1];
221 state->stack[state->stack_size-2] = result;
227 if(state->stack_size<2)
228 return MC_EXPRESSION_E_STACK_UNDERFLOW;
230 uintptr_t result = state->stack[state->stack_size-2] ^ state->stack[state->stack_size-1];
231 state->stack[state->stack_size-2] = result;
240 case DW_OP_deref_size:
241 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
244 if(state->stack_size==0)
245 return MC_EXPRESSION_E_STACK_UNDERFLOW;
248 uintptr_t address = (uintptr_t) state->stack[state->stack_size-1];
249 uintptr_t* p = (uintptr_t*)mc_translate_address(address, state->snapshot);
250 state->stack[state->stack_size-1] = *p;
256 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
259 if(error) return error;
266 /** \brief Resolve a location expression
267 * \deprecated Use mc_dwarf_resolve_expression
269 uintptr_t mc_dwarf_resolve_location(mc_expression_t expression, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
270 s_mc_expression_state_t state;
271 memset(&state, 0, sizeof(s_mc_expression_state_t));
272 state.frame_base = frame_pointer_address;
274 state.snapshot = snapshot;
275 state.object_info = object_info;
277 if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
278 xbt_die("Error evaluating DWARF expression");
279 if(state.stack_size==0)
280 xbt_die("No value on the stack");
282 return state.stack[state.stack_size-1];
285 uintptr_t mc_dwarf_resolve_locations(mc_location_list_t locations, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
289 if(unw_get_reg(c, UNW_REG_IP, &ip))
290 xbt_die("Could not resolve IP");
293 for(size_t i=0; i!=locations->size; ++i) {
294 mc_expression_t expression = locations->locations + i;
295 if( (expression->lowpc==NULL && expression->highpc==NULL)
296 || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
297 return mc_dwarf_resolve_location(expression, object_info, c, frame_pointer_address, snapshot);
300 xbt_die("Could not resolve location");
303 /** \brief Find the frame base of a given frame
308 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor) {
309 return (void*) mc_dwarf_resolve_locations(&frame->frame_base, object_info, unw_cursor, NULL, NULL);
312 void mc_dwarf_expression_clear(mc_expression_t expression) {
313 free(expression->ops);
314 expression->ops = NULL;
315 expression->size = 0;
316 expression->lowpc = NULL;
317 expression->highpc = NULL;
320 void mc_dwarf_location_list_clear(mc_location_list_t list) {
321 for(size_t i=0; i!=list->size; ++i) {
322 mc_dwarf_expression_clear(list->locations + i);
324 free(list->locations);
325 list->locations = NULL;
329 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
330 if(expression->ops) {
331 free(expression->ops);
333 expression->lowpc = NULL;
334 expression->highpc = NULL;
335 expression->size = len;
336 expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
337 memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
340 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
341 if(target->locations) {
342 mc_dwarf_location_list_clear(target);
345 target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
346 mc_dwarf_expression_init(target->locations, len, ops);
349 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
350 if(list->locations) {
351 mc_dwarf_location_list_clear(list);
355 ptrdiff_t offset = 0;
356 Dwarf_Addr base, start, end;
362 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
366 xbt_die("Error while loading location list");
370 list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
371 mc_expression_t expression = list->locations + i;
373 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
374 mc_dwarf_expression_init(expression, len, ops);
376 // If start == 0, this is not a location list:
377 expression->lowpc = start == 0 ? NULL : (char*) base + start;
378 expression->highpc = start == 0 ? NULL : (char*) base + end;