Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove old code (mc_location_t)
[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     case DW_OP_deref:
233       return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
234
235     // Not handled:
236     default:
237      return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
238     }
239
240     if(error) return error;
241   }
242   return 0;
243 }
244
245 // ***** Location
246
247 /** \brief Resolve a location expression
248  *  \deprecated Use mc_dwarf_resolve_expression
249  */
250 Dwarf_Off mc_dwarf_resolve_location(mc_expression_t expression, unw_cursor_t* c, void* frame_pointer_address) {
251   s_mc_expression_state_t state;
252   memset(&state, 0, sizeof(s_mc_expression_state_t));
253   state.frame_base = frame_pointer_address;
254   state.cursor = c;
255
256   if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
257     xbt_die("Error evaluating DWARF expression");
258   if(state.stack_size==0)
259     xbt_die("No value on the stack");
260   else
261     return (Dwarf_Off) state.stack[state.stack_size-1];
262 }
263
264 Dwarf_Off mc_dwarf_resolve_locations(mc_location_list_t locations, unw_cursor_t* c, void* frame_pointer_address) {
265
266   unw_word_t ip;
267   if(c) {
268     if(unw_get_reg(c, UNW_REG_IP, &ip))
269       xbt_die("Could not resolve IP");
270   }
271
272   for(size_t i=0; i!=locations->size; ++i) {
273     mc_expression_t expression = locations->locations + i;
274     if( (expression->lowpc==NULL && expression->highpc==NULL)
275       || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
276       return mc_dwarf_resolve_location(expression, c, frame_pointer_address);
277     }
278   }
279   xbt_die("Could not resolve location");
280 }
281
282 /** \brief Find the frame base of a given frame
283  *
284  *  \param frame
285  *  \param unw_cursor
286  */
287 void* mc_find_frame_base(dw_frame_t frame, unw_cursor_t* unw_cursor) {
288   return (void*) mc_dwarf_resolve_locations(&frame->frame_base, unw_cursor, NULL);
289 }
290
291 static
292 void mc_dwarf_expression_clear(mc_expression_t expression) {
293   free(expression->ops);
294   expression->ops = NULL;
295   expression->size = 0;
296   expression->lowpc = NULL;
297   expression->highpc = NULL;
298 }
299
300 void mc_dwarf_location_list_clear(mc_location_list_t list) {
301   for(size_t i=0; i!=list->size; ++i) {
302     mc_dwarf_expression_clear(list->locations + i);
303   }
304   free(list->locations);
305   list->locations = NULL;
306   list->size = 0;
307 }
308
309 static
310 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
311   if(expression->ops) {
312     free(expression->ops);
313   }
314   expression->lowpc = NULL;
315   expression->highpc = NULL;
316   expression->size = len;
317   expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
318   memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
319 }
320
321 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
322   if(target->locations) {
323     mc_dwarf_location_list_clear(target);
324   }
325   target->size = 1;
326   target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
327   mc_dwarf_expression_init(target->locations, len, ops);
328 }
329
330 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
331   if(list->locations) {
332     mc_dwarf_location_list_clear(list);
333   }
334   list->size = 0;
335
336   ptrdiff_t offset = 0;
337   Dwarf_Addr base, start, end;
338   Dwarf_Op *ops;
339   size_t len;
340
341   while (1) {
342
343     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
344     if (offset==0)
345       return;
346     else if (offset==-1)
347       xbt_die("Error while loading location list");
348
349     int i = list->size;
350     list->size++;
351     list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
352     mc_expression_t expression = list->locations + i;
353
354     void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
355     mc_dwarf_expression_init(expression, len, ops);
356
357     // If start == 0, this is not a location list:
358     expression->lowpc = start == 0 ? NULL : (char*) base + start;
359     expression->highpc = start == 0 ? NULL : (char*) base + end;
360   }
361
362 }