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 "mc/LocationList.hpp"
16 #include "mc/AddressSpace.hpp"
17 #include "mc/Frame.hpp"
18 #include "mc/ObjectInformation.hpp"
19 #include "mc/DwarfExpression.hpp"
20 #include "mc_dwarf.hpp"
22 using simgrid::mc::remote;
27 evaluation_error::~evaluation_error() {}
36 const Dwarf_Op* ops, std::size_t n,
37 const ExpressionContext& context, ExpressionStack& stack)
39 for (size_t i = 0; i != n; ++i) {
40 const Dwarf_Op *op = ops + i;
41 std::uint8_t atom = op->atom;
79 int register_id = simgrid::dwarf::dwarf_register_to_libunwind(
80 op->atom - DW_OP_breg0);
83 throw evaluation_error("Missin stack context");
84 unw_get_reg(context.cursor, register_id, &res);
85 stack.push(res + op->number);
89 // Push the CFA (Canonical Frame Addresse):
90 case DW_OP_call_frame_cfa:
92 // UNW_X86_64_CFA does not return the CFA DWARF expects
93 // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
94 // unwind it once in order to find the parent SP:
97 throw evaluation_error("Missint cursor");
100 unw_cursor_t cursor = *(context.cursor);
104 unw_get_reg(&cursor, UNW_REG_SP, &res);
112 stack.push((std::uintptr_t) context.frame_base + op->number);
117 // Short constant literals:
118 // DW_OP_lit15 pushed the 15 on the stack.
151 stack.push(atom - DW_OP_lit0);
154 // Address from the base address of this ELF object.
155 // Push the address on the stack (base_address + argument).
157 if (!context.object_info)
158 throw evaluation_error("No base address");
159 Dwarf_Off addr = (Dwarf_Off) (std::uintptr_t)
160 context.object_info->base_address() + op->number;
165 // General constants:
166 // Push the constant argument on the stack.
177 stack.push(op->number);
180 // ***** Stack manipulation:
182 // Push another copy/duplicate the value at the top of the stack:
187 // Pop/drop the top of the stack:
192 // Swap the two top-most value of the stack:
194 std::swap(stack.top(), stack.top(1));
197 // Duplicate the value under the top of the stack:
199 stack.push(stack.top(1));
203 // Those usually take the top of the stack and the next value as argument
204 // and replace the top of the stack with the computed value
205 // (stack.top() += stack.before_top()).
208 stack.push(stack.pop() + stack.pop());
212 stack.push(stack.pop() * stack.pop());
215 case DW_OP_plus_uconst:
216 stack.top() += op->number;
220 stack.top() = ~stack.top();
224 stack.top() = - (intptr_t) stack.top();
228 stack.push(stack.pop() - stack.pop());
232 stack.push(stack.pop() & stack.pop());
236 stack.push(stack.pop() | stack.pop());
240 stack.push(stack.pop() ^ stack.pop());
246 // ***** Deference (memory fetch)
248 case DW_OP_deref_size:
249 throw evaluation_error("Unsupported operation");
253 if (!context.address_space)
254 throw evaluation_error("Missing address space");
255 context.address_space->read_bytes(
256 &stack.top(), sizeof(uintptr_t), remote(stack.top()),
257 context.process_index);
262 throw evaluation_error("Unsupported operation");
273 void mc_dwarf_location_list_init(
274 simgrid::dwarf::LocationList* list, simgrid::mc::ObjectInformation* info,
275 Dwarf_Die * die, Dwarf_Attribute * attr)
279 std::ptrdiff_t offset = 0;
280 Dwarf_Addr base, start, end;
286 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
289 else if (offset == -1)
290 xbt_die("Error while loading location list");
292 simgrid::dwarf::LocationListEntry entry;
293 entry.expression = simgrid::dwarf::DwarfExpression(ops, ops + len);
295 void *base = info->base_address();
296 // If start == 0, this is not a location list:
297 entry.lowpc = start == 0 ? NULL : (char *) base + start;
298 entry.highpc = start == 0 ? NULL : (char *) base + end;
300 list->push_back(std::move(entry));