1 /* Copyright (c) 2014-2022. 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. */
8 #include <unordered_set>
10 #include "src/mc/AddressSpace.hpp"
11 #include "src/mc/inspect/DwarfExpression.hpp"
12 #include "src/mc/inspect/Frame.hpp"
13 #include "src/mc/inspect/LocationList.hpp"
14 #include "src/mc/inspect/ObjectInformation.hpp"
15 #include "src/mc/inspect/mc_dwarf.hpp"
16 #include "src/mc/mc_private.hpp"
18 using simgrid::mc::remote;
23 void execute(const Dwarf_Op* ops, std::size_t n, const ExpressionContext& context, ExpressionStack& stack)
25 for (size_t i = 0; i != n; ++i) {
26 const Dwarf_Op* op = ops + i;
27 std::uint8_t atom = op->atom;
32 // Push the CFA (Canonical Frame Address):
33 case DW_OP_call_frame_cfa:
34 /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
36 * > Typically, the CFA is defined to be the value of the stack
37 * > pointer at the call site in the previous frame (which may be
38 * > different from its value on entry to the current frame).
40 * We need to unwind the frame in order to get the SP of the parent
43 * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
44 * is the SP of the *current* frame. */
47 unw_cursor_t cursor = *(context.cursor);
51 unw_get_reg(&cursor, UNW_REG_SP, &res);
55 throw evaluation_error("Missing cursor");
59 stack.push((std::uintptr_t)context.frame_base + op->number);
62 // Address from the base address of this ELF object.
63 // Push the address on the stack (base_address + argument).
65 if (context.object_info) {
66 Dwarf_Off addr = (Dwarf_Off)(std::uintptr_t)context.object_info->base_address() + op->number;
70 throw evaluation_error("No base address");
72 // ***** Stack manipulation:
74 // Push another copy/duplicate the value at the top of the stack:
79 // Pop/drop the top of the stack:
88 // Duplicate the value under the top of the stack:
90 stack.push(stack.top(1));
94 // Those usually take the top of the stack and the next value as argument
95 // and replace the top of the stack with the computed value
96 // (stack.top() += stack.before_top()).
100 second = stack.pop();
101 stack.push(first + second);
106 second = stack.pop();
107 stack.push(first * second);
110 case DW_OP_plus_uconst:
111 stack.top() += op->number;
115 stack.top() = ~stack.top();
119 stack.top() = -(intptr_t)stack.top();
124 second = stack.pop();
125 stack.push(second - first);
130 second = stack.pop();
131 stack.push(first & second);
136 second = stack.pop();
137 stack.push(first | second);
142 second = stack.pop();
143 stack.push(first ^ second);
149 // ***** Deference (memory fetch)
151 case DW_OP_deref_size:
152 throw evaluation_error("Unsupported operation");
156 if (not context.address_space)
157 throw evaluation_error("Missing address space");
158 context.address_space->read_bytes(&stack.top(), sizeof(uintptr_t), remote(stack.top()));
164 static const std::unordered_set<uint8_t> registers = {
165 DW_OP_breg0, DW_OP_breg1, DW_OP_breg2, DW_OP_breg3, DW_OP_breg4, DW_OP_breg5, DW_OP_breg6,
166 DW_OP_breg7, DW_OP_breg8, DW_OP_breg9, DW_OP_breg10, DW_OP_breg11, DW_OP_breg12, DW_OP_breg13,
167 DW_OP_breg14, DW_OP_breg15, DW_OP_breg16, DW_OP_breg17, DW_OP_breg18, DW_OP_breg19, DW_OP_breg20,
168 DW_OP_breg21, DW_OP_breg22, DW_OP_breg23, DW_OP_breg24, DW_OP_breg25, DW_OP_breg26, DW_OP_breg27,
169 DW_OP_breg28, DW_OP_breg29, DW_OP_breg30, DW_OP_breg31};
170 if (registers.count(atom) > 0) {
171 // Push register + constant:
172 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(op->atom - DW_OP_breg0);
174 if (not context.cursor)
175 throw evaluation_error("Missing stack context");
176 unw_get_reg(context.cursor, register_id, &res);
177 stack.push(res + op->number);
183 // Short constant literals:
184 static const std::unordered_set<uint8_t> literals = {
185 DW_OP_lit0, DW_OP_lit1, DW_OP_lit2, DW_OP_lit3, DW_OP_lit4, DW_OP_lit5, DW_OP_lit6, DW_OP_lit7,
186 DW_OP_lit8, DW_OP_lit9, DW_OP_lit10, DW_OP_lit11, DW_OP_lit12, DW_OP_lit13, DW_OP_lit14, DW_OP_lit15,
187 DW_OP_lit16, DW_OP_lit17, DW_OP_lit18, DW_OP_lit19, DW_OP_lit20, DW_OP_lit21, DW_OP_lit22, DW_OP_lit23,
188 DW_OP_lit24, DW_OP_lit25, DW_OP_lit26, DW_OP_lit27, DW_OP_lit28, DW_OP_lit29, DW_OP_lit30, DW_OP_lit31};
189 if (literals.count(atom) > 0) {
190 // Push a literal/constant on the stack:
191 stack.push(atom - DW_OP_lit0);
195 // General constants:
196 static const std::unordered_set<uint8_t> constants = {
197 DW_OP_const1u, DW_OP_const2u, DW_OP_const4u, DW_OP_const8u, DW_OP_const1s,
198 DW_OP_const2s, DW_OP_const4s, DW_OP_const8s, DW_OP_constu, DW_OP_consts};
199 if (constants.count(atom) > 0) {
200 // Push the constant argument on the stack.
201 stack.push(op->number);
206 throw evaluation_error("Unsupported operation");
212 } // namespace simgrid