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_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"
21 using simgrid::mc::remote;
26 evaluation_error::~evaluation_error() noexcept(true) {}
29 const Dwarf_Op* ops, std::size_t n,
30 const ExpressionContext& context, ExpressionStack& stack)
32 for (size_t i = 0; i != n; ++i) {
33 const Dwarf_Op *op = ops + i;
34 std::uint8_t atom = op->atom;
72 // Push register + constant:
73 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
74 op->atom - DW_OP_breg0);
77 throw evaluation_error("Missing stack context");
78 unw_get_reg(context.cursor, register_id, &res);
79 stack.push(res + op->number);
83 // Push the CFA (Canonical Frame Addresse):
84 case DW_OP_call_frame_cfa:
86 /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
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).
92 * We need to unwind the frame in order to get the SP of the parent
95 * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
96 * is the SP of the *current* frame. */
99 throw evaluation_error("Missint cursor");
102 unw_cursor_t cursor = *(context.cursor);
106 unw_get_reg(&cursor, UNW_REG_SP, &res);
114 stack.push((std::uintptr_t) context.frame_base + op->number);
119 // Short constant literals:
152 // Push a literal/constant on the stack:
153 stack.push(atom - DW_OP_lit0);
156 // Address from the base address of this ELF object.
157 // Push the address on the stack (base_address + argument).
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;
167 // General constants:
168 // Push the constant argument on the stack.
179 stack.push(op->number);
182 // ***** Stack manipulation:
184 // Push another copy/duplicate the value at the top of the stack:
189 // Pop/drop the top of the stack:
198 // Duplicate the value under the top of the stack:
200 stack.push(stack.top(1));
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()).
209 stack.push(stack.pop() + stack.pop());
213 stack.push(stack.pop() * stack.pop());
216 case DW_OP_plus_uconst:
217 stack.top() += op->number;
221 stack.top() = ~stack.top();
225 stack.top() = - (intptr_t) stack.top();
229 stack.push(stack.pop() - stack.pop());
233 stack.push(stack.pop() & stack.pop());
237 stack.push(stack.pop() | stack.pop());
241 stack.push(stack.pop() ^ stack.pop());
247 // ***** Deference (memory fetch)
249 case DW_OP_deref_size:
250 throw evaluation_error("Unsupported operation");
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);
263 throw evaluation_error("Unsupported operation");