Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
jedule: obey our coding standards
[simgrid.git] / src / mc / DwarfExpression.cpp
1 /* Copyright (c) 2014-2018. 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 void execute(
28   const Dwarf_Op* ops, std::size_t n,
29   const ExpressionContext& context, ExpressionStack& stack)
30 {
31   for (size_t i = 0; i != n; ++i) {
32     const Dwarf_Op *op = ops + i;
33     std::uint8_t atom = op->atom;
34     intptr_t first;
35     intptr_t second;
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       first  = stack.pop();
211       second = stack.pop();
212       stack.push(first + second);
213       break;
214
215     case DW_OP_mul:
216       first  = stack.pop();
217       second = stack.pop();
218       stack.push(first * second);
219       break;
220
221     case DW_OP_plus_uconst:
222       stack.top() += op->number;
223       break;
224
225     case DW_OP_not:
226       stack.top() = ~stack.top();
227       break;
228
229     case DW_OP_neg:
230       stack.top() = - (intptr_t) stack.top();
231       break;
232
233     case DW_OP_minus:
234       first  = stack.pop();
235       second = stack.pop();
236       stack.push(second - first);
237       break;
238
239     case DW_OP_and:
240       first  = stack.pop();
241       second = stack.pop();
242       stack.push(first & second);
243       break;
244
245     case DW_OP_or:
246       first  = stack.pop();
247       second = stack.pop();
248       stack.push(first | second);
249       break;
250
251     case DW_OP_xor:
252       first  = stack.pop();
253       second = stack.pop();
254       stack.push(first ^ second);
255       break;
256
257     case DW_OP_nop:
258       break;
259
260       // ***** Deference (memory fetch)
261
262     case DW_OP_deref_size:
263       throw evaluation_error("Unsupported operation");
264
265     case DW_OP_deref:
266       // Computed address:
267       if (not context.address_space)
268         throw evaluation_error("Missing address space");
269       context.address_space->read_bytes(
270         &stack.top(), sizeof(uintptr_t), remote(stack.top()),
271         context.process_index);
272       break;
273
274       // Not handled:
275     default:
276       throw evaluation_error("Unsupported operation");
277     }
278
279   }
280 }
281
282 }
283 }