Logo AND Algorithmique Numérique Distribuée

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