Logo AND Algorithmique Numérique Distribuée

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