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 "mc_object_info.h"
14 #include "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 "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 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
74 op->atom - DW_OP_breg0);
77 throw evaluation_error("Missin 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 // 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:
91 throw evaluation_error("Missint cursor");
94 unw_cursor_t cursor = *(context.cursor);
98 unw_get_reg(&cursor, UNW_REG_SP, &res);
106 stack.push((std::uintptr_t) context.frame_base + op->number);
111 // Short constant literals:
112 // DW_OP_lit15 pushed the 15 on the stack.
145 stack.push(atom - DW_OP_lit0);
148 // Address from the base address of this ELF object.
149 // Push the address on the stack (base_address + argument).
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;
159 // General constants:
160 // Push the constant argument on the stack.
171 stack.push(op->number);
174 // ***** Stack manipulation:
176 // Push another copy/duplicate the value at the top of the stack:
181 // Pop/drop the top of the stack:
186 // Swap the two top-most value of the stack:
188 std::swap(stack.top(), stack.top(1));
191 // Duplicate the value under the top of the stack:
193 stack.push(stack.top(1));
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()).
202 stack.push(stack.pop() + stack.pop());
206 stack.push(stack.pop() * stack.pop());
209 case DW_OP_plus_uconst:
210 stack.top() += op->number;
214 stack.top() = ~stack.top();
218 stack.top() = - (intptr_t) stack.top();
222 stack.push(stack.pop() - stack.pop());
226 stack.push(stack.pop() & stack.pop());
230 stack.push(stack.pop() | stack.pop());
234 stack.push(stack.pop() ^ stack.pop());
240 // ***** Deference (memory fetch)
242 case DW_OP_deref_size:
243 throw evaluation_error("Unsupported operation");
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);
256 throw evaluation_error("Unsupported operation");