Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a compilation error regarding logging and strings in fat trees
[simgrid.git] / src / mc / mc_dwarf_expression.c
1 /* Copyright (c) 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdint.h>
8 #include <stdarg.h>
9
10 #include <dwarf.h>
11 #include <elfutils/libdw.h>
12
13 #include "mc_private.h"
14
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;
18
19   state->stack[state->stack_size++] = value;
20   return 0;
21 }
22
23 static int mc_dwarf_register_to_libunwind(int dwarf_register) {
24   #if defined(UNW_TARGET_X86_64)
25   // It seems for this arch, DWARF and libunwind agree in the numbering:
26   return dwarf_register;
27   #elif defined(UNW_TARGET_X86)
28   // Could't find the authoritative source of information for this.
29   // This is inspired from http://source.winehq.org/source/dlls/dbghelp/cpu_i386.c#L517.
30   switch(dwarf_register) {
31   case 0:  return UNW_X86_EAX;
32   case 1:  return UNW_X86_ECX;
33   case 2:  return UNW_X86_EDX;
34   case 3:  return UNW_X86_EBX;
35   case 4:  return UNW_X86_ESP;
36   case 5:  return UNW_X86_EBP;
37   case 6:  return UNW_X86_ESI;
38   case 7:  return UNW_X86_EDI;
39   case 8:  return UNW_X86_EIP;
40   case 9:  return UNW_X86_EFLAGS;
41   case 10: return UNW_X86_CS;
42   case 11: return UNW_X86_SS;
43   case 12: return UNW_X86_DS;
44   case 13: return UNW_X86_ES;
45   case 14: return UNW_X86_FS;
46   case 15: return UNW_X86_GS;
47   case 16: return UNW_X86_ST0;
48   case 17: return UNW_X86_ST1;
49   case 18: return UNW_X86_ST2;
50   case 19: return UNW_X86_ST3;
51   case 20: return UNW_X86_ST4;
52   case 21: return UNW_X86_ST5;
53   case 22: return UNW_X86_ST6;
54   case 23: return UNW_X86_ST7;
55   default: xbt_die("Bad/unknown register number.");
56   }
57   #else
58   #error This architecture is not supported yet.
59   #endif
60 }
61
62 int mc_dwarf_execute_expression(
63   size_t n, const Dwarf_Op* ops, mc_expression_state_t state) {
64   for(int i=0; i!=n; ++i) {
65     int error = 0;
66     const Dwarf_Op* op = ops + i;
67     uint8_t atom = op->atom;
68
69     switch (atom) {
70
71     // Registers:
72
73     case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
74     case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
75     case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
76     case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
77     case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
78     case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
79     case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
80     case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:{
81         int register_id = mc_dwarf_register_to_libunwind(op->atom - DW_OP_breg0);
82         unw_word_t res;
83         if(!state->cursor)
84           return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
85         unw_get_reg(state->cursor, register_id, &res);
86         error = mc_dwarf_push_value(state, res + op->number);
87         break;
88       }
89
90     // Push the CFA (Canonical Frame Addresse):
91     case DW_OP_call_frame_cfa:
92     {
93       // UNW_X86_64_CFA does not return the CFA DWARF expects
94       // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
95       // unwind it once in order to find the parent SP:
96
97       if(!state->cursor)
98         return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
99
100       // Get frame:
101       unw_cursor_t cursor = *(state->cursor);
102       unw_step(&cursor);
103
104       unw_word_t res;
105       unw_get_reg(&cursor, UNW_TDEP_SP, &res);
106       error = mc_dwarf_push_value(state, res);
107       break;
108     }
109
110     // Frame base:
111
112     case DW_OP_fbreg:
113       {
114         if(!state->frame_base)
115           return MC_EXPRESSION_E_MISSING_FRAME_BASE;
116         error = mc_dwarf_push_value(state, ((uintptr_t)state->frame_base) + op->number);
117         break;
118       }
119
120
121     // Constants:
122
123     case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3:
124     case DW_OP_lit4: case DW_OP_lit5: case DW_OP_lit6: case DW_OP_lit7:
125     case DW_OP_lit8: case DW_OP_lit9: case DW_OP_lit10: case DW_OP_lit11:
126     case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14: case DW_OP_lit15:
127     case DW_OP_lit16: case DW_OP_lit17: case DW_OP_lit18: case DW_OP_lit19:
128     case DW_OP_lit20: case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23:
129     case DW_OP_lit24: case DW_OP_lit25: case DW_OP_lit26: case DW_OP_lit27:
130     case DW_OP_lit28: case DW_OP_lit29: case DW_OP_lit30: case DW_OP_lit31:
131       error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
132       break;
133
134     case DW_OP_addr:
135       if(!state->object_info)
136         return MC_EXPRESSION_E_NO_BASE_ADDRESS;
137       if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
138         return MC_EXPRESSION_E_STACK_OVERFLOW;
139       error = mc_dwarf_push_value(state,
140         (Dwarf_Off)(uintptr_t)MC_object_base_address(state->object_info) + op->number);
141       break;
142
143     case DW_OP_const1u:
144     case DW_OP_const2u:
145     case DW_OP_const4u:
146     case DW_OP_const8u:
147     case DW_OP_const1s:
148     case DW_OP_const2s:
149     case DW_OP_const4s:
150     case DW_OP_const8s:
151     case DW_OP_constu:
152     case DW_OP_consts:
153       if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
154         return MC_EXPRESSION_E_STACK_OVERFLOW;
155       error = mc_dwarf_push_value(state, op->number);
156       break;
157
158     // Stack manipulation:
159
160     // Push the value at the top of the stack:
161     case DW_OP_dup:
162       if(state->stack_size==0)
163         return MC_EXPRESSION_E_STACK_UNDERFLOW;
164       else
165         error = mc_dwarf_push_value(state, state->stack[state->stack_size-1]);
166       break;
167
168     case DW_OP_drop:
169       if(state->stack_size==0)
170         return MC_EXPRESSION_E_STACK_UNDERFLOW;
171       else
172         state->stack_size--;
173       break;
174
175     case DW_OP_swap:
176       if(state->stack_size<2)
177         return MC_EXPRESSION_E_STACK_UNDERFLOW;
178       {
179         uintptr_t temp = state->stack[state->stack_size-2];
180         state->stack[state->stack_size-2] = state->stack[state->stack_size-1];
181         state->stack[state->stack_size-1] = temp;
182       }
183       break;
184
185     case DW_OP_over:
186       if(state->stack_size<2)
187         return MC_EXPRESSION_E_STACK_UNDERFLOW;
188       error = mc_dwarf_push_value(state, state->stack[state->stack_size-2]);
189       break;
190
191     // Operations:
192
193     case DW_OP_plus:
194       if(state->stack_size<2)
195         return MC_EXPRESSION_E_STACK_UNDERFLOW;
196       {
197         uintptr_t result = state->stack[state->stack_size-2] + state->stack[state->stack_size-1];
198         state->stack[state->stack_size-2] = result;
199         state->stack_size--;
200       }
201       break;
202
203     case DW_OP_mul:
204       if(state->stack_size<2)
205         return MC_EXPRESSION_E_STACK_UNDERFLOW;
206       {
207         uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
208         state->stack[state->stack_size-2] = result;
209         state->stack_size--;
210       }
211       break;
212
213     case DW_OP_plus_uconst:
214       if(state->stack_size==0)
215         return MC_EXPRESSION_E_STACK_UNDERFLOW;
216       state->stack[state->stack_size-1] += op->number;
217       break;
218
219     case DW_OP_not:
220       if(state->stack_size==0)
221         return MC_EXPRESSION_E_STACK_UNDERFLOW;
222       state->stack[state->stack_size-1] = ~state->stack[state->stack_size-1];
223       break;
224
225     case DW_OP_neg:
226       if(state->stack_size==0)
227         return MC_EXPRESSION_E_STACK_UNDERFLOW;
228       {
229         intptr_t value = state->stack[state->stack_size-1];
230         if(value<0) value = -value;
231         state->stack[state->stack_size-1] = value;
232       }
233       break;
234
235     case DW_OP_minus:
236       if(state->stack_size<2)
237         return MC_EXPRESSION_E_STACK_UNDERFLOW;
238       {
239         uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
240         state->stack[state->stack_size-2] = result;
241         state->stack_size--;
242       }
243       break;
244
245     case DW_OP_and:
246       if(state->stack_size<2)
247         return MC_EXPRESSION_E_STACK_UNDERFLOW;
248       {
249         uintptr_t result = state->stack[state->stack_size-2] & state->stack[state->stack_size-1];
250         state->stack[state->stack_size-2] = result;
251         state->stack_size--;
252       }
253       break;
254
255     case DW_OP_or:
256       if(state->stack_size<2)
257         return MC_EXPRESSION_E_STACK_UNDERFLOW;
258       {
259         uintptr_t result = state->stack[state->stack_size-2] | state->stack[state->stack_size-1];
260         state->stack[state->stack_size-2] = result;
261         state->stack_size--;
262       }
263       break;
264
265     case DW_OP_xor:
266       if(state->stack_size<2)
267         return MC_EXPRESSION_E_STACK_UNDERFLOW;
268       {
269         uintptr_t result = state->stack[state->stack_size-2] ^ state->stack[state->stack_size-1];
270         state->stack[state->stack_size-2] = result;
271         state->stack_size--;
272       }
273       break;
274
275     case DW_OP_nop:
276       break;
277
278     // Dereference:
279     case DW_OP_deref_size:
280       return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
281
282     case DW_OP_deref:
283       if(state->stack_size==0)
284         return MC_EXPRESSION_E_STACK_UNDERFLOW;
285       {
286         // Computed address:
287         uintptr_t address = (uintptr_t) state->stack[state->stack_size-1];
288         uintptr_t* p = (uintptr_t*)mc_translate_address(address, state->snapshot);
289         state->stack[state->stack_size-1] = *p;
290       }
291       break;
292
293     // Not handled:
294     default:
295      return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
296     }
297
298     if(error) return error;
299   }
300   return 0;
301 }
302
303 // ***** Location
304
305 /** \brief Resolve a location expression
306  *  \deprecated Use mc_dwarf_resolve_expression
307  */
308 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) {
309   s_mc_expression_state_t state;
310   memset(&state, 0, sizeof(s_mc_expression_state_t));
311   state.frame_base = frame_pointer_address;
312   state.cursor = c;
313   state.snapshot = snapshot;
314   state.object_info = object_info;
315
316   if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
317     xbt_die("Error evaluating DWARF expression");
318   if(state.stack_size==0)
319     xbt_die("No value on the stack");
320   else
321     return state.stack[state.stack_size-1];
322 }
323
324 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) {
325
326   unw_word_t ip;
327   if(c) {
328     if(unw_get_reg(c, UNW_REG_IP, &ip))
329       xbt_die("Could not resolve IP");
330   }
331
332   for(size_t i=0; i!=locations->size; ++i) {
333     mc_expression_t expression = locations->locations + i;
334     if( (expression->lowpc==NULL && expression->highpc==NULL)
335       || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
336       return mc_dwarf_resolve_location(expression, object_info, c, frame_pointer_address, snapshot);
337     }
338   }
339   xbt_die("Could not resolve location");
340 }
341
342 /** \brief Find the frame base of a given frame
343  *
344  *  \param frame
345  *  \param unw_cursor
346  */
347 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor) {
348   return (void*) mc_dwarf_resolve_locations(&frame->frame_base, object_info, unw_cursor, NULL, NULL);
349 }
350
351 void mc_dwarf_expression_clear(mc_expression_t expression) {
352   free(expression->ops);
353   expression->ops = NULL;
354   expression->size = 0;
355   expression->lowpc = NULL;
356   expression->highpc = NULL;
357 }
358
359 void mc_dwarf_location_list_clear(mc_location_list_t list) {
360   for(size_t i=0; i!=list->size; ++i) {
361     mc_dwarf_expression_clear(list->locations + i);
362   }
363   free(list->locations);
364   list->locations = NULL;
365   list->size = 0;
366 }
367
368 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
369   if(expression->ops) {
370     free(expression->ops);
371   }
372   expression->lowpc = NULL;
373   expression->highpc = NULL;
374   expression->size = len;
375   expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
376   memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
377 }
378
379 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
380   if(target->locations) {
381     mc_dwarf_location_list_clear(target);
382   }
383   target->size = 1;
384   target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
385   mc_dwarf_expression_init(target->locations, len, ops);
386 }
387
388 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
389   if(list->locations) {
390     mc_dwarf_location_list_clear(list);
391   }
392   list->size = 0;
393
394   ptrdiff_t offset = 0;
395   Dwarf_Addr base, start, end;
396   Dwarf_Op *ops;
397   size_t len;
398
399   while (1) {
400
401     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
402     if (offset==0)
403       return;
404     else if (offset==-1)
405       xbt_die("Error while loading location list");
406
407     int i = list->size;
408     list->size++;
409     list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
410     mc_expression_t expression = list->locations + i;
411     expression->ops = NULL;
412     mc_dwarf_expression_init(expression, len, ops);
413
414     void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
415     // If start == 0, this is not a location list:
416     expression->lowpc = start == 0 ? NULL : (char*) base + start;
417     expression->highpc = start == 0 ? NULL : (char*) base + end;
418   }
419
420 }