Logo AND Algorithmique Numérique Distribuée

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