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;
73 // Push register + constant:
74 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
75 op->atom - DW_OP_breg0);
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);
84 // Push the CFA (Canonical Frame Address):
85 case DW_OP_call_frame_cfa:
87 /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
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).
93 * We need to unwind the frame in order to get the SP of the parent
96 * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
97 * is the SP of the *current* frame. */
99 if (not context.cursor)
100 throw evaluation_error("Missint cursor");
103 unw_cursor_t cursor = *(context.cursor);
107 unw_get_reg(&cursor, UNW_REG_SP, &res);
115 stack.push((std::uintptr_t) context.frame_base + op->number);
120 // Short constant literals:
153 // Push a literal/constant on the stack:
154 stack.push(atom - DW_OP_lit0);
157 // Address from the base address of this ELF object.
158 // Push the address on the stack (base_address + argument).
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;
168 // General constants:
169 // Push the constant argument on the stack.
180 stack.push(op->number);
183 // ***** Stack manipulation:
185 // Push another copy/duplicate the value at the top of the stack:
190 // Pop/drop the top of the stack:
199 // Duplicate the value under the top of the stack:
201 stack.push(stack.top(1));
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()).
211 second = stack.pop();
212 stack.push(first + second);
217 second = stack.pop();
218 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();
235 second = stack.pop();
236 stack.push(second - first);
241 second = stack.pop();
242 stack.push(first & second);
247 second = stack.pop();
248 stack.push(first | second);
253 second = stack.pop();
254 stack.push(first ^ second);
260 // ***** Deference (memory fetch)
262 case DW_OP_deref_size:
263 throw evaluation_error("Unsupported operation");
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);
276 throw evaluation_error("Unsupported operation");