Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent.
[simgrid.git] / src / mc / DwarfExpression.cpp
1 /* Copyright (c) 2014-2017. 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 <cstddef>
8 #include <cstdint>
9
10 #include <dwarf.h>
11 #include <elfutils/libdw.h>
12 #include <libunwind.h>
13
14 #include "src/mc/AddressSpace.hpp"
15 #include "src/mc/DwarfExpression.hpp"
16 #include "src/mc/Frame.hpp"
17 #include "src/mc/LocationList.hpp"
18 #include "src/mc/ObjectInformation.hpp"
19 #include "src/mc/mc_dwarf.hpp"
20 #include "src/mc/mc_private.hpp"
21
22 using simgrid::mc::remote;
23
24 namespace simgrid {
25 namespace dwarf {
26
27 evaluation_error::~evaluation_error() noexcept(true) {}
28
29 void execute(
30   const Dwarf_Op* ops, std::size_t n,
31   const ExpressionContext& context, ExpressionStack& stack)
32 {
33   for (size_t i = 0; i != n; ++i) {
34     const Dwarf_Op *op = ops + i;
35     std::uint8_t atom = op->atom;
36
37     switch (atom) {
38
39       // Registers:
40
41     case DW_OP_breg0:
42     case DW_OP_breg1:
43     case DW_OP_breg2:
44     case DW_OP_breg3:
45     case DW_OP_breg4:
46     case DW_OP_breg5:
47     case DW_OP_breg6:
48     case DW_OP_breg7:
49     case DW_OP_breg8:
50     case DW_OP_breg9:
51     case DW_OP_breg10:
52     case DW_OP_breg11:
53     case DW_OP_breg12:
54     case DW_OP_breg13:
55     case DW_OP_breg14:
56     case DW_OP_breg15:
57     case DW_OP_breg16:
58     case DW_OP_breg17:
59     case DW_OP_breg18:
60     case DW_OP_breg19:
61     case DW_OP_breg20:
62     case DW_OP_breg21:
63     case DW_OP_breg22:
64     case DW_OP_breg23:
65     case DW_OP_breg24:
66     case DW_OP_breg25:
67     case DW_OP_breg26:
68     case DW_OP_breg27:
69     case DW_OP_breg28:
70     case DW_OP_breg29:
71     case DW_OP_breg30:
72     case DW_OP_breg31:{
73         // Push register + constant:
74         int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
75           op->atom - DW_OP_breg0);
76         unw_word_t res;
77         if (not context.cursor)
78           throw evaluation_error("Missing stack context");
79         unw_get_reg(context.cursor, register_id, &res);
80         stack.push(res + op->number);
81         break;
82       }
83
84       // Push the CFA (Canonical Frame Address):
85     case DW_OP_call_frame_cfa:
86       {
87         /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
88          *
89          * > Typically, the CFA is defined to be the value of the stack
90          * > pointer at the call site in the previous frame (which may be
91          * > different from its value on entry to the current frame).
92          *
93          * We need to unwind the frame in order to get the SP of the parent
94          * frame.
95          *
96          * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
97          * is the SP of the *current* frame. */
98
99         if (not context.cursor)
100           throw evaluation_error("Missint cursor");
101
102         // Get frame:
103         unw_cursor_t cursor = *(context.cursor);
104         unw_step(&cursor);
105
106         unw_word_t res;
107         unw_get_reg(&cursor, UNW_REG_SP, &res);
108         stack.push(res);
109         break;
110       }
111
112       // Frame base:
113
114     case DW_OP_fbreg:
115       stack.push((std::uintptr_t) context.frame_base + op->number);
116       break;
117
118       // ***** Constants:
119
120       // Short constant literals:
121     case DW_OP_lit0:
122     case DW_OP_lit1:
123     case DW_OP_lit2:
124     case DW_OP_lit3:
125     case DW_OP_lit4:
126     case DW_OP_lit5:
127     case DW_OP_lit6:
128     case DW_OP_lit7:
129     case DW_OP_lit8:
130     case DW_OP_lit9:
131     case DW_OP_lit10:
132     case DW_OP_lit11:
133     case DW_OP_lit12:
134     case DW_OP_lit13:
135     case DW_OP_lit14:
136     case DW_OP_lit15:
137     case DW_OP_lit16:
138     case DW_OP_lit17:
139     case DW_OP_lit18:
140     case DW_OP_lit19:
141     case DW_OP_lit20:
142     case DW_OP_lit21:
143     case DW_OP_lit22:
144     case DW_OP_lit23:
145     case DW_OP_lit24:
146     case DW_OP_lit25:
147     case DW_OP_lit26:
148     case DW_OP_lit27:
149     case DW_OP_lit28:
150     case DW_OP_lit29:
151     case DW_OP_lit30:
152     case DW_OP_lit31:
153       // Push a literal/constant on the stack:
154       stack.push(atom - DW_OP_lit0);
155       break;
156
157       // Address from the base address of this ELF object.
158       // Push the address on the stack (base_address + argument).
159     case DW_OP_addr: {
160       if (not context.object_info)
161         throw evaluation_error("No base address");
162       Dwarf_Off addr = (Dwarf_Off) (std::uintptr_t)
163         context.object_info->base_address() + op->number;
164       stack.push(addr);
165       break;
166     }
167
168       // General constants:
169       // Push the constant argument on the stack.
170     case DW_OP_const1u:
171     case DW_OP_const2u:
172     case DW_OP_const4u:
173     case DW_OP_const8u:
174     case DW_OP_const1s:
175     case DW_OP_const2s:
176     case DW_OP_const4s:
177     case DW_OP_const8s:
178     case DW_OP_constu:
179     case DW_OP_consts:
180       stack.push(op->number);
181       break;
182
183       // ***** Stack manipulation:
184
185       // Push another copy/duplicate the value at the top of the stack:
186     case DW_OP_dup:
187       stack.dup();
188       break;
189
190       // Pop/drop the top of the stack:
191     case DW_OP_drop:
192       stack.pop();
193       break;
194
195     case DW_OP_swap:
196       stack.swap();
197       break;
198
199       // Duplicate the value under the top of the stack:
200     case DW_OP_over:
201       stack.push(stack.top(1));
202       break;
203
204       // ***** Operations:
205       // Those usually take the top of the stack and the next value as argument
206       // and replace the top of the stack with the computed value
207       // (stack.top() += stack.before_top()).
208
209     case DW_OP_plus: {
210       intptr_t first = stack.pop();
211       intptr_t second = stack.pop();
212       stack.push(first + second);
213       break;
214     }
215
216     case DW_OP_mul: {
217       intptr_t first = stack.pop();
218       intptr_t second = stack.pop();
219       stack.push(first * second);
220       break;
221     }
222
223     case DW_OP_plus_uconst:
224       stack.top() += op->number;
225       break;
226
227     case DW_OP_not:
228       stack.top() = ~stack.top();
229       break;
230
231     case DW_OP_neg:
232       stack.top() = - (intptr_t) stack.top();
233       break;
234
235     case DW_OP_minus: {
236       intptr_t first = stack.pop();
237       intptr_t second = stack.pop();
238       stack.push(second - first);
239       break;
240     }
241
242     case DW_OP_and: {
243       intptr_t first = stack.pop();
244       intptr_t second = stack.pop();
245       stack.push(first & second);
246       break;
247     }
248
249     case DW_OP_or: {
250       intptr_t first = stack.pop();
251       intptr_t second = stack.pop();
252       stack.push(first | second);
253       break;
254     }
255
256     case DW_OP_xor: {
257       intptr_t first = stack.pop();
258       intptr_t second = stack.pop();
259       stack.push(first ^ second);
260       break;
261     }
262
263     case DW_OP_nop:
264       break;
265
266       // ***** Deference (memory fetch)
267
268     case DW_OP_deref_size:
269       throw evaluation_error("Unsupported operation");
270
271     case DW_OP_deref:
272       // Computed address:
273       if (not context.address_space)
274         throw evaluation_error("Missing address space");
275       context.address_space->read_bytes(
276         &stack.top(), sizeof(uintptr_t), remote(stack.top()),
277         context.process_index);
278       break;
279
280       // Not handled:
281     default:
282       throw evaluation_error("Unsupported operation");
283     }
284
285   }
286 }
287
288 }
289 }