1 /* Copyright (c) 2014-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "src/mc/AddressSpace.hpp"
10 #include "src/mc/DwarfExpression.hpp"
11 #include "src/mc/Frame.hpp"
12 #include "src/mc/LocationList.hpp"
13 #include "src/mc/ObjectInformation.hpp"
14 #include "src/mc/mc_dwarf.hpp"
15 #include "src/mc/mc_private.hpp"
17 using simgrid::mc::remote;
23 const Dwarf_Op* ops, std::size_t n,
24 const ExpressionContext& context, ExpressionStack& stack)
26 for (size_t i = 0; i != n; ++i) {
27 const Dwarf_Op *op = ops + i;
28 std::uint8_t atom = op->atom;
68 // Push register + constant:
69 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
70 op->atom - DW_OP_breg0);
72 if (not context.cursor)
73 throw evaluation_error("Missing stack context");
74 unw_get_reg(context.cursor, register_id, &res);
75 stack.push(res + op->number);
79 // Push the CFA (Canonical Frame Address):
80 case DW_OP_call_frame_cfa:
82 /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
84 * > Typically, the CFA is defined to be the value of the stack
85 * > pointer at the call site in the previous frame (which may be
86 * > different from its value on entry to the current frame).
88 * We need to unwind the frame in order to get the SP of the parent
91 * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
92 * is the SP of the *current* frame. */
94 if (not context.cursor)
95 throw evaluation_error("Missint cursor");
98 unw_cursor_t cursor = *(context.cursor);
102 unw_get_reg(&cursor, UNW_REG_SP, &res);
110 stack.push((std::uintptr_t) context.frame_base + op->number);
115 // Short constant literals:
148 // Push a literal/constant on the stack:
149 stack.push(atom - DW_OP_lit0);
152 // Address from the base address of this ELF object.
153 // Push the address on the stack (base_address + argument).
155 if (not context.object_info)
156 throw evaluation_error("No base address");
157 Dwarf_Off addr = (Dwarf_Off) (std::uintptr_t)
158 context.object_info->base_address() + op->number;
163 // General constants:
164 // Push the constant argument on the stack.
175 stack.push(op->number);
178 // ***** Stack manipulation:
180 // Push another copy/duplicate the value at the top of the stack:
185 // Pop/drop the top of the stack:
194 // Duplicate the value under the top of the stack:
196 stack.push(stack.top(1));
200 // Those usually take the top of the stack and the next value as argument
201 // and replace the top of the stack with the computed value
202 // (stack.top() += stack.before_top()).
206 second = stack.pop();
207 stack.push(first + second);
212 second = stack.pop();
213 stack.push(first * second);
216 case DW_OP_plus_uconst:
217 stack.top() += op->number;
221 stack.top() = ~stack.top();
225 stack.top() = - (intptr_t) stack.top();
230 second = stack.pop();
231 stack.push(second - first);
236 second = stack.pop();
237 stack.push(first & second);
242 second = stack.pop();
243 stack.push(first | second);
248 second = stack.pop();
249 stack.push(first ^ second);
255 // ***** Deference (memory fetch)
257 case DW_OP_deref_size:
258 throw evaluation_error("Unsupported operation");
262 if (not context.address_space)
263 throw evaluation_error("Missing address space");
264 context.address_space->read_bytes(
265 &stack.top(), sizeof(uintptr_t), remote(stack.top()),
266 context.process_index);
271 throw evaluation_error("Unsupported operation");