1 /* Copyright (c) 2004-2021. 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. */
6 #include "src/mc/inspect/LocationList.hpp"
7 #include "src/mc/inspect/ObjectInformation.hpp"
8 #include "src/mc/inspect/mc_dwarf.hpp"
10 #include "xbt/asserts.h"
11 #include "xbt/sysdep.h"
15 #include <libunwind.h>
21 /** Resolve a location expression */
22 Location resolve(simgrid::dwarf::DwarfExpression const& expression, simgrid::mc::ObjectInformation* object_info,
23 unw_cursor_t* c, void* frame_pointer_address, const simgrid::mc::AddressSpace* address_space)
25 simgrid::dwarf::ExpressionContext context;
26 context.frame_base = frame_pointer_address;
28 context.address_space = address_space;
29 context.object_info = object_info;
31 if (not expression.empty() && expression[0].atom >= DW_OP_reg0 && expression[0].atom <= DW_OP_reg31) {
32 int dwarf_register = expression[0].atom - DW_OP_reg0;
33 xbt_assert(c, "Missing frame context for register operation DW_OP_reg%i", dwarf_register);
34 return Location(dwarf_register_to_libunwind(dwarf_register));
37 simgrid::dwarf::ExpressionStack stack;
38 simgrid::dwarf::execute(expression, context, stack);
39 return Location((void*)stack.top());
42 // TODO, move this in a method of LocationList
43 static simgrid::dwarf::DwarfExpression const* find_expression(simgrid::dwarf::LocationList const& locations,
46 for (simgrid::dwarf::LocationListEntry const& entry : locations)
47 if (entry.valid_for_ip(ip))
48 return &entry.expression();
52 Location resolve(simgrid::dwarf::LocationList const& locations, simgrid::mc::ObjectInformation* object_info,
53 unw_cursor_t* c, void* frame_pointer_address, const simgrid::mc::AddressSpace* address_space)
56 if (c && unw_get_reg(c, UNW_REG_IP, &ip))
57 xbt_die("Could not resolve IP");
58 simgrid::dwarf::DwarfExpression const* expression = find_expression(locations, ip);
60 xbt_die("Could not resolve location");
61 return simgrid::dwarf::resolve(*expression, object_info, c, frame_pointer_address, address_space);
64 LocationList location_list(const simgrid::mc::ObjectInformation& info, Dwarf_Attribute& attr)
66 LocationList locations;
67 std::ptrdiff_t offset = 0;
75 offset = dwarf_getlocations(&attr, offset, &base, &start, &end, &ops, &len);
79 else if (offset == -1)
80 xbt_die("Error while loading location list");
82 auto base_address = reinterpret_cast<std::uint64_t>(info.base_address());
84 LocationListEntry::range_type range;
86 // If start == 0, this is not a location list:
87 range = {0, UINT64_MAX};
89 range = {base_address + start, base_address + end};
91 locations.emplace_back(DwarfExpression(ops, ops + len), range);
97 } // namespace simgrid