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 (Canonical Frame Addresse):
47     case DW_OP_call_frame_cfa:
48     {
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:
52
53       if(!state->cursor)
54         return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
55
56       // Get frame:
57       unw_cursor_t cursor = *(state->cursor);
58       unw_step(&cursor);
59
60       unw_word_t res;
61       unw_get_reg(&cursor, UNW_TDEP_SP, &res);
62       error = mc_dwarf_push_value(state, res);
63       break;
64     }
65
66     // Frame base:
67
68     case DW_OP_fbreg:
69       {
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);
73         break;
74       }
75
76
77     // Constants:
78
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);
88       break;
89
90     case DW_OP_addr:
91       if(!state->object_info)
92         return MC_EXPRESSION_E_NO_BASE_ADDRESS;
93       if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
94         return MC_EXPRESSION_E_STACK_OVERFLOW;
95       error = mc_dwarf_push_value(state,
96         (Dwarf_Off)(uintptr_t)MC_object_base_address(state->object_info) + op->number);
97       break;
98
99     case DW_OP_const1u:
100     case DW_OP_const2u:
101     case DW_OP_const4u:
102     case DW_OP_const8u:
103     case DW_OP_const1s:
104     case DW_OP_const2s:
105     case DW_OP_const4s:
106     case DW_OP_const8s:
107     case DW_OP_constu:
108     case DW_OP_consts:
109       if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
110         return MC_EXPRESSION_E_STACK_OVERFLOW;
111       error = mc_dwarf_push_value(state, op->number);
112       break;
113
114     // Stack manipulation:
115
116     // Push the value at the top of the stack:
117     case DW_OP_dup:
118       if(state->stack_size==0)
119         return MC_EXPRESSION_E_STACK_UNDERFLOW;
120       else
121         error = mc_dwarf_push_value(state, state->stack[state->stack_size-1]);
122       break;
123
124     case DW_OP_drop:
125       if(state->stack_size==0)
126         return MC_EXPRESSION_E_STACK_UNDERFLOW;
127       else
128         state->stack_size--;
129       break;
130
131     case DW_OP_swap:
132       if(state->stack_size<2)
133         return MC_EXPRESSION_E_STACK_UNDERFLOW;
134       {
135         uintptr_t temp = state->stack[state->stack_size-2];
136         state->stack[state->stack_size-2] = state->stack[state->stack_size-1];
137         state->stack[state->stack_size-1] = temp;
138       }
139       break;
140
141     case DW_OP_over:
142       if(state->stack_size<2)
143         return MC_EXPRESSION_E_STACK_UNDERFLOW;
144       error = mc_dwarf_push_value(state, state->stack[state->stack_size-2]);
145       break;
146
147     // Operations:
148
149     case DW_OP_plus:
150       if(state->stack_size<2)
151         return MC_EXPRESSION_E_STACK_UNDERFLOW;
152       {
153         uintptr_t result = state->stack[state->stack_size-2] + state->stack[state->stack_size-1];
154         state->stack[state->stack_size-2] = result;
155         state->stack_size--;
156       }
157       break;
158
159     case DW_OP_mul:
160       if(state->stack_size<2)
161         return MC_EXPRESSION_E_STACK_UNDERFLOW;
162       {
163         uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
164         state->stack[state->stack_size-2] = result;
165         state->stack_size--;
166       }
167       break;
168
169     case DW_OP_plus_uconst:
170       if(state->stack_size==0)
171         return MC_EXPRESSION_E_STACK_UNDERFLOW;
172       state->stack[state->stack_size-1] += op->number;
173       break;
174
175     case DW_OP_not:
176       if(state->stack_size==0)
177         return MC_EXPRESSION_E_STACK_UNDERFLOW;
178       state->stack[state->stack_size-1] = ~state->stack[state->stack_size-1];
179       break;
180
181     case DW_OP_neg:
182       if(state->stack_size==0)
183         return MC_EXPRESSION_E_STACK_UNDERFLOW;
184       {
185         intptr_t value = state->stack[state->stack_size-1];
186         if(value<0) value = -value;
187         state->stack[state->stack_size-1] = value;
188       }
189       break;
190
191     case DW_OP_minus:
192       if(state->stack_size<2)
193         return MC_EXPRESSION_E_STACK_UNDERFLOW;
194       {
195         uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
196         state->stack[state->stack_size-2] = result;
197         state->stack_size--;
198       }
199       break;
200
201     case DW_OP_and:
202       if(state->stack_size<2)
203         return MC_EXPRESSION_E_STACK_UNDERFLOW;
204       {
205         uintptr_t result = state->stack[state->stack_size-2] & state->stack[state->stack_size-1];
206         state->stack[state->stack_size-2] = result;
207         state->stack_size--;
208       }
209       break;
210
211     case DW_OP_or:
212       if(state->stack_size<2)
213         return MC_EXPRESSION_E_STACK_UNDERFLOW;
214       {
215         uintptr_t result = state->stack[state->stack_size-2] | state->stack[state->stack_size-1];
216         state->stack[state->stack_size-2] = result;
217         state->stack_size--;
218       }
219       break;
220
221     case DW_OP_xor:
222       if(state->stack_size<2)
223         return MC_EXPRESSION_E_STACK_UNDERFLOW;
224       {
225         uintptr_t result = state->stack[state->stack_size-2] ^ state->stack[state->stack_size-1];
226         state->stack[state->stack_size-2] = result;
227         state->stack_size--;
228       }
229       break;
230
231     case DW_OP_nop:
232       break;
233
234     // Dereference:
235     case DW_OP_deref_size:
236       return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
237
238     case DW_OP_deref:
239       if(state->stack_size==0)
240         return MC_EXPRESSION_E_STACK_UNDERFLOW;
241       {
242         // Computed address:
243         uintptr_t address = (uintptr_t) state->stack[state->stack_size-1];
244         uintptr_t* p = (uintptr_t*)mc_translate_address(address, state->snapshot);
245         state->stack[state->stack_size-1] = *p;
246       }
247       break;
248
249     // Not handled:
250     default:
251      return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
252     }
253
254     if(error) return error;
255   }
256   return 0;
257 }
258
259 // ***** Location
260
261 /** \brief Resolve a location expression
262  *  \deprecated Use mc_dwarf_resolve_expression
263  */
264 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) {
265   s_mc_expression_state_t state;
266   memset(&state, 0, sizeof(s_mc_expression_state_t));
267   state.frame_base = frame_pointer_address;
268   state.cursor = c;
269   state.snapshot = snapshot;
270   state.object_info = object_info;
271
272   if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
273     xbt_die("Error evaluating DWARF expression");
274   if(state.stack_size==0)
275     xbt_die("No value on the stack");
276   else
277     return state.stack[state.stack_size-1];
278 }
279
280 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) {
281
282   unw_word_t ip;
283   if(c) {
284     if(unw_get_reg(c, UNW_REG_IP, &ip))
285       xbt_die("Could not resolve IP");
286   }
287
288   for(size_t i=0; i!=locations->size; ++i) {
289     mc_expression_t expression = locations->locations + i;
290     if( (expression->lowpc==NULL && expression->highpc==NULL)
291       || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
292       return mc_dwarf_resolve_location(expression, object_info, c, frame_pointer_address, snapshot);
293     }
294   }
295   xbt_die("Could not resolve location");
296 }
297
298 /** \brief Find the frame base of a given frame
299  *
300  *  \param frame
301  *  \param unw_cursor
302  */
303 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor) {
304   return (void*) mc_dwarf_resolve_locations(&frame->frame_base, object_info, unw_cursor, NULL, NULL);
305 }
306
307 void mc_dwarf_expression_clear(mc_expression_t expression) {
308   free(expression->ops);
309   expression->ops = NULL;
310   expression->size = 0;
311   expression->lowpc = NULL;
312   expression->highpc = NULL;
313 }
314
315 void mc_dwarf_location_list_clear(mc_location_list_t list) {
316   for(size_t i=0; i!=list->size; ++i) {
317     mc_dwarf_expression_clear(list->locations + i);
318   }
319   free(list->locations);
320   list->locations = NULL;
321   list->size = 0;
322 }
323
324 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
325   if(expression->ops) {
326     free(expression->ops);
327   }
328   expression->lowpc = NULL;
329   expression->highpc = NULL;
330   expression->size = len;
331   expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
332   memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
333 }
334
335 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
336   if(target->locations) {
337     mc_dwarf_location_list_clear(target);
338   }
339   target->size = 1;
340   target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
341   mc_dwarf_expression_init(target->locations, len, ops);
342 }
343
344 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
345   if(list->locations) {
346     mc_dwarf_location_list_clear(list);
347   }
348   list->size = 0;
349
350   ptrdiff_t offset = 0;
351   Dwarf_Addr base, start, end;
352   Dwarf_Op *ops;
353   size_t len;
354
355   while (1) {
356
357     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
358     if (offset==0)
359       return;
360     else if (offset==-1)
361       xbt_die("Error while loading location list");
362
363     int i = list->size;
364     list->size++;
365     list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
366     mc_expression_t expression = list->locations + i;
367
368     void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
369     mc_dwarf_expression_init(expression, len, ops);
370
371     // If start == 0, this is not a location list:
372     expression->lowpc = start == 0 ? NULL : (char*) base + start;
373     expression->highpc = start == 0 ? NULL : (char*) base + end;
374   }
375
376 }