1 /* Copyright (c) 2014-2020. 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/inspect/DwarfExpression.hpp"
11 #include "src/mc/inspect/Frame.hpp"
12 #include "src/mc/inspect/LocationList.hpp"
13 #include "src/mc/inspect/ObjectInformation.hpp"
14 #include "src/mc/inspect/mc_dwarf.hpp"
15 #include "src/mc/mc_private.hpp"
17 using simgrid::mc::remote;
22 void execute(const Dwarf_Op* ops, std::size_t n, const ExpressionContext& context, ExpressionStack& stack)
24 for (size_t i = 0; i != n; ++i) {
25 const Dwarf_Op* op = ops + i;
26 std::uint8_t atom = op->atom;
64 // Push register + constant:
65 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(op->atom - DW_OP_breg0);
67 if (not context.cursor)
68 throw evaluation_error("Missing stack context");
69 unw_get_reg(context.cursor, register_id, &res);
70 stack.push(res + op->number);
74 // Push the CFA (Canonical Frame Address):
75 case DW_OP_call_frame_cfa: {
76 /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
78 * > Typically, the CFA is defined to be the value of the stack
79 * > pointer at the call site in the previous frame (which may be
80 * > different from its value on entry to the current frame).
82 * We need to unwind the frame in order to get the SP of the parent
85 * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
86 * is the SP of the *current* frame. */
88 if (not context.cursor)
89 throw evaluation_error("Missint cursor");
92 unw_cursor_t cursor = *(context.cursor);
96 unw_get_reg(&cursor, UNW_REG_SP, &res);
104 stack.push((std::uintptr_t)context.frame_base + op->number);
109 // Short constant literals:
142 // Push a literal/constant on the stack:
143 stack.push(atom - DW_OP_lit0);
146 // Address from the base address of this ELF object.
147 // Push the address on the stack (base_address + argument).
149 if (not context.object_info)
150 throw evaluation_error("No base address");
151 Dwarf_Off addr = (Dwarf_Off)(std::uintptr_t)context.object_info->base_address() + op->number;
156 // General constants:
157 // Push the constant argument on the stack.
168 stack.push(op->number);
171 // ***** Stack manipulation:
173 // Push another copy/duplicate the value at the top of the stack:
178 // Pop/drop the top of the stack:
187 // Duplicate the value under the top of the stack:
189 stack.push(stack.top(1));
193 // Those usually take the top of the stack and the next value as argument
194 // and replace the top of the stack with the computed value
195 // (stack.top() += stack.before_top()).
199 second = stack.pop();
200 stack.push(first + second);
205 second = stack.pop();
206 stack.push(first * second);
209 case DW_OP_plus_uconst:
210 stack.top() += op->number;
214 stack.top() = ~stack.top();
218 stack.top() = -(intptr_t)stack.top();
223 second = stack.pop();
224 stack.push(second - first);
229 second = stack.pop();
230 stack.push(first & second);
235 second = stack.pop();
236 stack.push(first | second);
241 second = stack.pop();
242 stack.push(first ^ second);
248 // ***** Deference (memory fetch)
250 case DW_OP_deref_size:
251 throw evaluation_error("Unsupported operation");
255 if (not context.address_space)
256 throw evaluation_error("Missing address space");
257 context.address_space->read_bytes(&stack.top(), sizeof(uintptr_t), remote(stack.top()));
262 throw evaluation_error("Unsupported operation");
268 } // namespace simgrid