Logo AND Algorithmique Numérique Distribuée

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