1 /* Copyright (c) 2014-2015. 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>
13 #include "src/mc/mc_object_info.h"
14 #include "src/mc/mc_private.h"
15 #include "src/mc/LocationList.hpp"
16 #include "src/mc/AddressSpace.hpp"
17 #include "src/mc/Frame.hpp"
18 #include "src/mc/ObjectInformation.hpp"
19 #include "src/mc/DwarfExpression.hpp"
20 #include "src/mc/mc_dwarf.hpp"
22 using simgrid::mc::remote;
27 evaluation_error::~evaluation_error() noexcept(true) {}
30 const Dwarf_Op* ops, std::size_t n,
31 const ExpressionContext& context, ExpressionStack& stack)
33 for (size_t i = 0; i != n; ++i) {
34 const Dwarf_Op *op = ops + i;
35 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);
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 Addresse):
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. */
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 (!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()).
210 stack.push(stack.pop() + stack.pop());
214 stack.push(stack.pop() * stack.pop());
217 case DW_OP_plus_uconst:
218 stack.top() += op->number;
222 stack.top() = ~stack.top();
226 stack.top() = - (intptr_t) stack.top();
230 stack.push(stack.pop() - stack.pop());
234 stack.push(stack.pop() & stack.pop());
238 stack.push(stack.pop() | stack.pop());
242 stack.push(stack.pop() ^ stack.pop());
248 // ***** Deference (memory fetch)
250 case DW_OP_deref_size:
251 throw evaluation_error("Unsupported operation");
255 if (!context.address_space)
256 throw evaluation_error("Missing address space");
257 context.address_space->read_bytes(
258 &stack.top(), sizeof(uintptr_t), remote(stack.top()),
259 context.process_index);
264 throw evaluation_error("Unsupported operation");