Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5e898641245b9bbb588406a88799d3da75d2e081
[simgrid.git] / src / mc / DwarfExpression.cpp
1 /* Copyright (c) 2014-2015. 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 <cstdint>
8 #include <cstdarg>
9
10 #include <dwarf.h>
11 #include <elfutils/libdw.h>
12
13 #include "mc_object_info.h"
14 #include "mc_private.h"
15 #include "mc/LocationList.hpp"
16 #include "mc/AddressSpace.hpp"
17 #include "mc/Frame.hpp"
18 #include "mc/ObjectInformation.hpp"
19 #include "mc/DwarfExpression.hpp"
20 #include "mc_dwarf.hpp"
21
22 using simgrid::mc::remote;
23
24 namespace simgrid {
25 namespace dwarf {
26
27 evaluation_error::~evaluation_error() {}
28
29 }
30 }
31
32 namespace simgrid {
33 namespace dwarf {
34
35 void execute(
36   const Dwarf_Op* ops, std::size_t n,
37   const ExpressionContext& context, ExpressionStack& stack)
38 {
39   for (size_t i = 0; i != n; ++i) {
40     const Dwarf_Op *op = ops + i;
41     std::uint8_t atom = op->atom;
42
43     switch (atom) {
44
45       // Registers:
46
47     case DW_OP_breg0:
48     case DW_OP_breg1:
49     case DW_OP_breg2:
50     case DW_OP_breg3:
51     case DW_OP_breg4:
52     case DW_OP_breg5:
53     case DW_OP_breg6:
54     case DW_OP_breg7:
55     case DW_OP_breg8:
56     case DW_OP_breg9:
57     case DW_OP_breg10:
58     case DW_OP_breg11:
59     case DW_OP_breg12:
60     case DW_OP_breg13:
61     case DW_OP_breg14:
62     case DW_OP_breg15:
63     case DW_OP_breg16:
64     case DW_OP_breg17:
65     case DW_OP_breg18:
66     case DW_OP_breg19:
67     case DW_OP_breg20:
68     case DW_OP_breg21:
69     case DW_OP_breg22:
70     case DW_OP_breg23:
71     case DW_OP_breg24:
72     case DW_OP_breg25:
73     case DW_OP_breg26:
74     case DW_OP_breg27:
75     case DW_OP_breg28:
76     case DW_OP_breg29:
77     case DW_OP_breg30:
78     case DW_OP_breg31:{
79         int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
80           op->atom - DW_OP_breg0);
81         unw_word_t res;
82         if (!context.cursor)
83           throw evaluation_error("Missin stack context");
84         unw_get_reg(context.cursor, register_id, &res);
85         stack.push(res + op->number);
86         break;
87       }
88
89       // Push the CFA (Canonical Frame Addresse):
90     case DW_OP_call_frame_cfa:
91       {
92         // UNW_X86_64_CFA does not return the CFA DWARF expects
93         // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
94         // unwind it once in order to find the parent SP:
95
96         if (!context.cursor)
97           throw evaluation_error("Missint cursor");
98
99         // Get frame:
100         unw_cursor_t cursor = *(context.cursor);
101         unw_step(&cursor);
102
103         unw_word_t res;
104         unw_get_reg(&cursor, UNW_REG_SP, &res);
105         stack.push(res);
106         break;
107       }
108
109       // Frame base:
110
111     case DW_OP_fbreg:
112       stack.push((std::uintptr_t) context.frame_base + op->number);
113       break;
114
115       // ***** Constants:
116
117       // Short constant literals:
118       // DW_OP_lit15 pushed the 15 on the stack.
119     case DW_OP_lit0:
120     case DW_OP_lit1:
121     case DW_OP_lit2:
122     case DW_OP_lit3:
123     case DW_OP_lit4:
124     case DW_OP_lit5:
125     case DW_OP_lit6:
126     case DW_OP_lit7:
127     case DW_OP_lit8:
128     case DW_OP_lit9:
129     case DW_OP_lit10:
130     case DW_OP_lit11:
131     case DW_OP_lit12:
132     case DW_OP_lit13:
133     case DW_OP_lit14:
134     case DW_OP_lit15:
135     case DW_OP_lit16:
136     case DW_OP_lit17:
137     case DW_OP_lit18:
138     case DW_OP_lit19:
139     case DW_OP_lit20:
140     case DW_OP_lit21:
141     case DW_OP_lit22:
142     case DW_OP_lit23:
143     case DW_OP_lit24:
144     case DW_OP_lit25:
145     case DW_OP_lit26:
146     case DW_OP_lit27:
147     case DW_OP_lit28:
148     case DW_OP_lit29:
149     case DW_OP_lit30:
150     case DW_OP_lit31:
151       stack.push(atom - DW_OP_lit0);
152       break;
153
154       // Address from the base address of this ELF object.
155       // Push the address on the stack (base_address + argument).
156     case DW_OP_addr: {
157       if (!context.object_info)
158         throw evaluation_error("No base address");
159       Dwarf_Off addr = (Dwarf_Off) (std::uintptr_t)
160         context.object_info->base_address() + op->number;
161       stack.push(addr);
162       break;
163     }
164
165       // General constants:
166       // Push the constant argument on the stack.
167     case DW_OP_const1u:
168     case DW_OP_const2u:
169     case DW_OP_const4u:
170     case DW_OP_const8u:
171     case DW_OP_const1s:
172     case DW_OP_const2s:
173     case DW_OP_const4s:
174     case DW_OP_const8s:
175     case DW_OP_constu:
176     case DW_OP_consts:
177       stack.push(op->number);
178       break;
179
180       // ***** Stack manipulation:
181
182       // Push another copy/duplicate the value at the top of the stack:
183     case DW_OP_dup:
184       stack.dup();
185       break;
186
187       // Pop/drop the top of the stack:
188     case DW_OP_drop:
189       stack.pop();
190       break;
191
192       // Swap the two top-most value of the stack:
193     case DW_OP_swap:
194       std::swap(stack.top(), stack.top(1));
195       break;
196
197       // Duplicate the value under the top of the stack:
198     case DW_OP_over:
199       stack.push(stack.top(1));
200       break;
201
202       // ***** Operations:
203       // Those usually take the top of the stack and the next value as argument
204       // and replace the top of the stack with the computed value
205       // (stack.top() += stack.before_top()).
206
207     case DW_OP_plus:
208       stack.push(stack.pop() + stack.pop());
209       break;
210
211     case DW_OP_mul:
212       stack.push(stack.pop() * stack.pop());
213       break;
214
215     case DW_OP_plus_uconst:
216       stack.top() += op->number;
217       break;
218
219     case DW_OP_not:
220       stack.top() = ~stack.top();
221       break;
222
223     case DW_OP_neg:
224       stack.top() = - (intptr_t) stack.top();
225       break;
226
227     case DW_OP_minus:
228       stack.push(stack.pop() - stack.pop());
229       break;
230
231     case DW_OP_and:
232       stack.push(stack.pop() & stack.pop());
233       break;
234
235     case DW_OP_or:
236       stack.push(stack.pop() | stack.pop());
237       break;
238
239     case DW_OP_xor:
240       stack.push(stack.pop() ^ stack.pop());
241       break;
242
243     case DW_OP_nop:
244       break;
245
246       // ***** Deference (memory fetch)
247
248     case DW_OP_deref_size:
249       throw evaluation_error("Unsupported operation");
250
251     case DW_OP_deref:
252       // Computed address:
253       if (!context.address_space)
254         throw evaluation_error("Missing address space");
255       context.address_space->read_bytes(
256         &stack.top(), sizeof(uintptr_t), remote(stack.top()),
257         context.process_index);
258       break;
259
260       // Not handled:
261     default:
262       throw evaluation_error("Unsupported operation");
263     }
264
265   }
266 }
267
268 }
269 }
270
271 extern "C" {
272
273 /** \brief Find the frame base of a given frame
274  *
275  *  \param frame
276  *  \param unw_cursor
277  */
278 void *mc_find_frame_base(simgrid::mc::Frame* frame, simgrid::mc::ObjectInformation* object_info,
279                          unw_cursor_t * unw_cursor)
280 {
281   simgrid::dwarf::Location location = simgrid::dwarf::resolve(
282                              frame->frame_base, object_info,
283                              unw_cursor, NULL, NULL, -1);
284   if (location.in_memory())
285     return location.address();
286   else if (location.in_register()) {
287     // This is a special case.
288     // The register if not the location of the frame base
289     // (a frame base cannot be located in a register)
290     // Instead, DWARF defines this to mean that the register
291     // contains the address of the frame base.
292     unw_word_t word;
293     unw_get_reg(unw_cursor, location.register_id(), &word);
294     return (void*) word;
295   }
296   else xbt_die("Unexpected location type");
297
298 }
299
300 void mc_dwarf_location_list_init(
301   simgrid::dwarf::LocationList* list, simgrid::mc::ObjectInformation* info,
302   Dwarf_Die * die, Dwarf_Attribute * attr)
303 {
304   list->clear();
305
306   std::ptrdiff_t offset = 0;
307   Dwarf_Addr base, start, end;
308   Dwarf_Op *ops;
309   std::size_t len;
310
311   while (1) {
312
313     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
314     if (offset == 0)
315       return;
316     else if (offset == -1)
317       xbt_die("Error while loading location list");
318
319     simgrid::dwarf::LocationListEntry entry;
320     entry.expression = simgrid::dwarf::DwarfExpression(ops, ops + len);
321
322     void *base = info->base_address();
323     // If start == 0, this is not a location list:
324     entry.lowpc = start == 0 ? NULL : (char *) base + start;
325     entry.highpc = start == 0 ? NULL : (char *) base + end;
326
327     list->push_back(std::move(entry));
328   }
329
330 }
331
332 }