Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to fix some compilation erros
[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 <cstdint>
8 #include <cstdarg>
9
10 #include <dwarf.h>
11 #include <elfutils/libdw.h>
12
13 #include "mc_object_info.h"
14 #include "mc_private.h"
15 #include "mc/LocationList.hpp"
16 #include "mc/AddressSpace.hpp"
17 #include "mc/Frame.hpp"
18 #include "mc/ObjectInformation.hpp"
19 #include "mc/DwarfExpression.hpp"
20 #include "mc_dwarf.hpp"
21
22 using simgrid::mc::remote;
23
24 namespace simgrid {
25 namespace dwarf {
26
27 evaluation_error::~evaluation_error() {}
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         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("Missin 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 Addresse):
84     case DW_OP_call_frame_cfa:
85       {
86         // UNW_X86_64_CFA does not return the CFA DWARF expects
87         // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
88         // unwind it once in order to find the parent SP:
89
90         if (!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       // DW_OP_lit15 pushed the 15 on the stack.
113     case DW_OP_lit0:
114     case DW_OP_lit1:
115     case DW_OP_lit2:
116     case DW_OP_lit3:
117     case DW_OP_lit4:
118     case DW_OP_lit5:
119     case DW_OP_lit6:
120     case DW_OP_lit7:
121     case DW_OP_lit8:
122     case DW_OP_lit9:
123     case DW_OP_lit10:
124     case DW_OP_lit11:
125     case DW_OP_lit12:
126     case DW_OP_lit13:
127     case DW_OP_lit14:
128     case DW_OP_lit15:
129     case DW_OP_lit16:
130     case DW_OP_lit17:
131     case DW_OP_lit18:
132     case DW_OP_lit19:
133     case DW_OP_lit20:
134     case DW_OP_lit21:
135     case DW_OP_lit22:
136     case DW_OP_lit23:
137     case DW_OP_lit24:
138     case DW_OP_lit25:
139     case DW_OP_lit26:
140     case DW_OP_lit27:
141     case DW_OP_lit28:
142     case DW_OP_lit29:
143     case DW_OP_lit30:
144     case DW_OP_lit31:
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 (!context.object_info)
152         throw evaluation_error("No base address");
153       Dwarf_Off addr = (Dwarf_Off) (std::uintptr_t)
154         context.object_info->base_address() + op->number;
155       stack.push(addr);
156       break;
157     }
158
159       // General constants:
160       // Push the constant argument on the stack.
161     case DW_OP_const1u:
162     case DW_OP_const2u:
163     case DW_OP_const4u:
164     case DW_OP_const8u:
165     case DW_OP_const1s:
166     case DW_OP_const2s:
167     case DW_OP_const4s:
168     case DW_OP_const8s:
169     case DW_OP_constu:
170     case DW_OP_consts:
171       stack.push(op->number);
172       break;
173
174       // ***** Stack manipulation:
175
176       // Push another copy/duplicate the value at the top of the stack:
177     case DW_OP_dup:
178       stack.dup();
179       break;
180
181       // Pop/drop the top of the stack:
182     case DW_OP_drop:
183       stack.pop();
184       break;
185
186       // Swap the two top-most value of the stack:
187     case DW_OP_swap:
188       std::swap(stack.top(), stack.top(1));
189       break;
190
191       // Duplicate the value under the top of the stack:
192     case DW_OP_over:
193       stack.push(stack.top(1));
194       break;
195
196       // ***** Operations:
197       // Those usually take the top of the stack and the next value as argument
198       // and replace the top of the stack with the computed value
199       // (stack.top() += stack.before_top()).
200
201     case DW_OP_plus:
202       stack.push(stack.pop() + stack.pop());
203       break;
204
205     case DW_OP_mul:
206       stack.push(stack.pop() * stack.pop());
207       break;
208
209     case DW_OP_plus_uconst:
210       stack.top() += op->number;
211       break;
212
213     case DW_OP_not:
214       stack.top() = ~stack.top();
215       break;
216
217     case DW_OP_neg:
218       stack.top() = - (intptr_t) stack.top();
219       break;
220
221     case DW_OP_minus:
222       stack.push(stack.pop() - stack.pop());
223       break;
224
225     case DW_OP_and:
226       stack.push(stack.pop() & stack.pop());
227       break;
228
229     case DW_OP_or:
230       stack.push(stack.pop() | stack.pop());
231       break;
232
233     case DW_OP_xor:
234       stack.push(stack.pop() ^ stack.pop());
235       break;
236
237     case DW_OP_nop:
238       break;
239
240       // ***** Deference (memory fetch)
241
242     case DW_OP_deref_size:
243       throw evaluation_error("Unsupported operation");
244
245     case DW_OP_deref:
246       // Computed address:
247       if (!context.address_space)
248         throw evaluation_error("Missing address space");
249       context.address_space->read_bytes(
250         &stack.top(), sizeof(uintptr_t), remote(stack.top()),
251         context.process_index);
252       break;
253
254       // Not handled:
255     default:
256       throw evaluation_error("Unsupported operation");
257     }
258
259   }
260 }
261
262 }
263 }