1 /* Copyright (c) 2014-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include <elfutils/libdw.h>
12 #include <libunwind.h>
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"
22 using simgrid::mc::remote;
28 const Dwarf_Op* ops, std::size_t n,
29 const ExpressionContext& context, ExpressionStack& stack)
31 for (size_t i = 0; i != n; ++i) {
32 const Dwarf_Op *op = ops + i;
33 std::uint8_t atom = op->atom;
71 // Push register + constant:
72 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
73 op->atom - DW_OP_breg0);
75 if (not context.cursor)
76 throw evaluation_error("Missing stack context");
77 unw_get_reg(context.cursor, register_id, &res);
78 stack.push(res + op->number);
82 // Push the CFA (Canonical Frame Address):
83 case DW_OP_call_frame_cfa:
85 /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
87 * > Typically, the CFA is defined to be the value of the stack
88 * > pointer at the call site in the previous frame (which may be
89 * > different from its value on entry to the current frame).
91 * We need to unwind the frame in order to get the SP of the parent
94 * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
95 * is the SP of the *current* frame. */
97 if (not context.cursor)
98 throw evaluation_error("Missint cursor");
101 unw_cursor_t cursor = *(context.cursor);
105 unw_get_reg(&cursor, UNW_REG_SP, &res);
113 stack.push((std::uintptr_t) context.frame_base + op->number);
118 // Short constant literals:
151 // Push a literal/constant on the stack:
152 stack.push(atom - DW_OP_lit0);
155 // Address from the base address of this ELF object.
156 // Push the address on the stack (base_address + argument).
158 if (not context.object_info)
159 throw evaluation_error("No base address");
160 Dwarf_Off addr = (Dwarf_Off) (std::uintptr_t)
161 context.object_info->base_address() + op->number;
166 // General constants:
167 // Push the constant argument on the stack.
178 stack.push(op->number);
181 // ***** Stack manipulation:
183 // Push another copy/duplicate the value at the top of the stack:
188 // Pop/drop the top of the stack:
197 // Duplicate the value under the top of the stack:
199 stack.push(stack.top(1));
203 // Those usually take the top of the stack and the next value as argument
204 // and replace the top of the stack with the computed value
205 // (stack.top() += stack.before_top()).
208 intptr_t first = stack.pop();
209 intptr_t second = stack.pop();
210 stack.push(first + second);
215 intptr_t first = stack.pop();
216 intptr_t second = stack.pop();
217 stack.push(first * second);
221 case DW_OP_plus_uconst:
222 stack.top() += op->number;
226 stack.top() = ~stack.top();
230 stack.top() = - (intptr_t) stack.top();
234 intptr_t first = stack.pop();
235 intptr_t second = stack.pop();
236 stack.push(second - first);
241 intptr_t first = stack.pop();
242 intptr_t second = stack.pop();
243 stack.push(first & second);
248 intptr_t first = stack.pop();
249 intptr_t second = stack.pop();
250 stack.push(first | second);
255 intptr_t first = stack.pop();
256 intptr_t second = stack.pop();
257 stack.push(first ^ second);
264 // ***** Deference (memory fetch)
266 case DW_OP_deref_size:
267 throw evaluation_error("Unsupported operation");
271 if (not context.address_space)
272 throw evaluation_error("Missing address space");
273 context.address_space->read_bytes(
274 &stack.top(), sizeof(uintptr_t), remote(stack.top()),
275 context.process_index);
280 throw evaluation_error("Unsupported operation");