Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / mc_dwarf_expression.c
1
2 #include <stdint.h>
3 #include <stdarg.h>
4
5 #include <dwarf.h>
6 #include <elfutils/libdw.h>
7
8 #include "mc_private.h"
9
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;
13
14   state->stack[state->stack_size++] = value;
15   return 0;
16 }
17
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) {
21     int error = 0;
22     const Dwarf_Op* op = ops + i;
23     uint8_t atom = op->atom;
24
25     switch (atom) {
26
27     // Registers:
28
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;
38         unw_word_t res;
39         if(!state->cursor)
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);
43         break;
44       }
45
46     // Push the CFA (Call Frame Addresse):
47     case DW_OP_call_frame_cfa:
48     {
49       unw_word_t res;
50
51       int register_id =
52 #if defined(UNW_TARGET_X86_64)
53           UNW_X86_64_CFA
54 #elif defined(UNW_TARGET_X86)
55           UNW_X86_CFA
56 #else
57           -1;
58 #endif
59         ;
60       if(register_id<0)
61         xbt_die("Support for CFA not implemented for this achitecture.");
62
63       if(!state->cursor)
64         return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
65
66       unw_get_reg(state->cursor, register_id, &res);
67       error = mc_dwarf_push_value(state, res + op->number);
68       break;
69     }
70
71     // Frame base:
72
73     case DW_OP_fbreg:
74       {
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);
78         break;
79       }
80
81     // Constants:
82
83     case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3:
84     case DW_OP_lit4: case DW_OP_lit5: case DW_OP_lit6: case DW_OP_lit7:
85     case DW_OP_lit8: case DW_OP_lit9: case DW_OP_lit10: case DW_OP_lit11:
86     case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14: case DW_OP_lit15:
87     case DW_OP_lit16: case DW_OP_lit17: case DW_OP_lit18: case DW_OP_lit19:
88     case DW_OP_lit20: case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23:
89     case DW_OP_lit24: case DW_OP_lit25: case DW_OP_lit26: case DW_OP_lit27:
90     case DW_OP_lit28: case DW_OP_lit29: case DW_OP_lit30: case DW_OP_lit31:
91       error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
92       break;
93
94     case DW_OP_addr:
95     case DW_OP_const1u:
96     case DW_OP_const2u:
97     case DW_OP_const4u:
98     case DW_OP_const8u:
99     case DW_OP_const1s:
100     case DW_OP_const2s:
101     case DW_OP_const4s:
102     case DW_OP_const8s:
103     case DW_OP_constu:
104     case DW_OP_consts:
105       if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
106         return MC_EXPRESSION_E_STACK_OVERFLOW;
107       error = mc_dwarf_push_value(state, op->number);
108       break;
109
110     // Stack manipulation:
111
112     // Push the value at the top of the stack:
113     case DW_OP_dup:
114       if(state->stack_size==0)
115         return MC_EXPRESSION_E_STACK_UNDERFLOW;
116       else
117         error = mc_dwarf_push_value(state, state->stack[state->stack_size-1]);
118       break;
119
120     case DW_OP_drop:
121       if(state->stack_size==0)
122         return MC_EXPRESSION_E_STACK_UNDERFLOW;
123       else
124         state->stack_size--;
125       break;
126
127     case DW_OP_swap:
128       if(state->stack_size<2)
129         return MC_EXPRESSION_E_STACK_UNDERFLOW;
130       {
131         uintptr_t temp = state->stack[state->stack_size-2];
132         state->stack[state->stack_size-2] = state->stack[state->stack_size-1];
133         state->stack[state->stack_size-1] = temp;
134       }
135       break;
136
137     case DW_OP_over:
138       if(state->stack_size<2)
139         return MC_EXPRESSION_E_STACK_UNDERFLOW;
140       error = mc_dwarf_push_value(state, state->stack[state->stack_size-2]);
141       break;
142
143     // Operations:
144
145     case DW_OP_plus:
146       if(state->stack_size<2)
147         return MC_EXPRESSION_E_STACK_UNDERFLOW;
148       {
149         uintptr_t result = state->stack[state->stack_size-2] + state->stack[state->stack_size-1];
150         state->stack[state->stack_size-2] = result;
151         state->stack_size--;
152       }
153       break;
154
155     case DW_OP_mul:
156       if(state->stack_size<2)
157         return MC_EXPRESSION_E_STACK_UNDERFLOW;
158       {
159         uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
160         state->stack[state->stack_size-2] = result;
161         state->stack_size--;
162       }
163       break;
164
165     case DW_OP_plus_uconst:
166       if(state->stack_size==0)
167         return MC_EXPRESSION_E_STACK_UNDERFLOW;
168       state->stack[state->stack_size-1] += op->number;
169       break;
170
171     case DW_OP_not:
172       if(state->stack_size==0)
173         return MC_EXPRESSION_E_STACK_UNDERFLOW;
174       state->stack[state->stack_size-1] = ~state->stack[state->stack_size-1];
175       break;
176
177     case DW_OP_neg:
178       if(state->stack_size==0)
179         return MC_EXPRESSION_E_STACK_UNDERFLOW;
180       {
181         intptr_t value = state->stack[state->stack_size-1];
182         if(value<0) value = -value;
183         state->stack[state->stack_size-1] = value;
184       }
185       break;
186
187     case DW_OP_minus:
188       if(state->stack_size<2)
189         return MC_EXPRESSION_E_STACK_UNDERFLOW;
190       {
191         uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
192         state->stack[state->stack_size-2] = result;
193         state->stack_size--;
194       }
195       break;
196
197     case DW_OP_and:
198       if(state->stack_size<2)
199         return MC_EXPRESSION_E_STACK_UNDERFLOW;
200       {
201         uintptr_t result = state->stack[state->stack_size-2] & state->stack[state->stack_size-1];
202         state->stack[state->stack_size-2] = result;
203         state->stack_size--;
204       }
205       break;
206
207     case DW_OP_or:
208       if(state->stack_size<2)
209         return MC_EXPRESSION_E_STACK_UNDERFLOW;
210       {
211         uintptr_t result = state->stack[state->stack_size-2] | state->stack[state->stack_size-1];
212         state->stack[state->stack_size-2] = result;
213         state->stack_size--;
214       }
215       break;
216
217     case DW_OP_xor:
218       if(state->stack_size<2)
219         return MC_EXPRESSION_E_STACK_UNDERFLOW;
220       {
221         uintptr_t result = state->stack[state->stack_size-2] ^ state->stack[state->stack_size-1];
222         state->stack[state->stack_size-2] = result;
223         state->stack_size--;
224       }
225       break;
226
227     case DW_OP_nop:
228       break;
229
230     // Dereference:
231     case DW_OP_deref_size:
232       return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
233
234     case DW_OP_deref:
235       if(state->stack_size==0)
236         return MC_EXPRESSION_E_STACK_UNDERFLOW;
237       {
238         // Computed address:
239         uintptr_t address = (uintptr_t) state->stack[state->stack_size-1];
240         uintptr_t* p = (uintptr_t*)mc_translate_address(address, state->snapshot);
241         state->stack[state->stack_size-1] = *p;
242       }
243       break;
244
245     // Not handled:
246     default:
247      return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
248     }
249
250     if(error) return error;
251   }
252   return 0;
253 }
254
255 // ***** Location
256
257 /** \brief Resolve a location expression
258  *  \deprecated Use mc_dwarf_resolve_expression
259  */
260 Dwarf_Off mc_dwarf_resolve_location(mc_expression_t expression, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
261   s_mc_expression_state_t state;
262   memset(&state, 0, sizeof(s_mc_expression_state_t));
263   state.frame_base = frame_pointer_address;
264   state.cursor = c;
265   state.snapshot = snapshot;
266
267   if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
268     xbt_die("Error evaluating DWARF expression");
269   if(state.stack_size==0)
270     xbt_die("No value on the stack");
271   else
272     return (Dwarf_Off) state.stack[state.stack_size-1];
273 }
274
275 Dwarf_Off mc_dwarf_resolve_locations(mc_location_list_t locations, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
276
277   unw_word_t ip;
278   if(c) {
279     if(unw_get_reg(c, UNW_REG_IP, &ip))
280       xbt_die("Could not resolve IP");
281   }
282
283   for(size_t i=0; i!=locations->size; ++i) {
284     mc_expression_t expression = locations->locations + i;
285     if( (expression->lowpc==NULL && expression->highpc==NULL)
286       || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
287       return mc_dwarf_resolve_location(expression, c, frame_pointer_address, snapshot);
288     }
289   }
290   xbt_die("Could not resolve location");
291 }
292
293 /** \brief Find the frame base of a given frame
294  *
295  *  \param frame
296  *  \param unw_cursor
297  */
298 void* mc_find_frame_base(dw_frame_t frame, unw_cursor_t* unw_cursor) {
299   return (void*) mc_dwarf_resolve_locations(&frame->frame_base, unw_cursor, NULL, NULL);
300 }
301
302 void mc_dwarf_expression_clear(mc_expression_t expression) {
303   free(expression->ops);
304   expression->ops = NULL;
305   expression->size = 0;
306   expression->lowpc = NULL;
307   expression->highpc = NULL;
308 }
309
310 void mc_dwarf_location_list_clear(mc_location_list_t list) {
311   for(size_t i=0; i!=list->size; ++i) {
312     mc_dwarf_expression_clear(list->locations + i);
313   }
314   free(list->locations);
315   list->locations = NULL;
316   list->size = 0;
317 }
318
319 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
320   if(expression->ops) {
321     free(expression->ops);
322   }
323   expression->lowpc = NULL;
324   expression->highpc = NULL;
325   expression->size = len;
326   expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
327   memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
328 }
329
330 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
331   if(target->locations) {
332     mc_dwarf_location_list_clear(target);
333   }
334   target->size = 1;
335   target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
336   mc_dwarf_expression_init(target->locations, len, ops);
337 }
338
339 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
340   if(list->locations) {
341     mc_dwarf_location_list_clear(list);
342   }
343   list->size = 0;
344
345   ptrdiff_t offset = 0;
346   Dwarf_Addr base, start, end;
347   Dwarf_Op *ops;
348   size_t len;
349
350   while (1) {
351
352     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
353     if (offset==0)
354       return;
355     else if (offset==-1)
356       xbt_die("Error while loading location list");
357
358     int i = list->size;
359     list->size++;
360     list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
361     mc_expression_t expression = list->locations + i;
362
363     void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
364     mc_dwarf_expression_init(expression, len, ops);
365
366     // If start == 0, this is not a location list:
367     expression->lowpc = start == 0 ? NULL : (char*) base + start;
368     expression->highpc = start == 0 ? NULL : (char*) base + end;
369   }
370
371 }