1 /* Copyright (c) 2004-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. */
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"
12 #include "xbt/sysdep.h"
16 #include <libunwind.h>
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(mc_dwarf);
21 namespace simgrid::dwarf {
23 /** Resolve a location expression */
24 Location resolve(simgrid::dwarf::DwarfExpression const& expression, simgrid::mc::ObjectInformation* object_info,
25 unw_cursor_t* c, void* frame_pointer_address, const simgrid::mc::AddressSpace* address_space)
27 simgrid::dwarf::ExpressionContext context;
28 context.frame_base = frame_pointer_address;
30 context.address_space = address_space;
31 context.object_info = object_info;
33 if (not expression.empty() && expression[0].atom >= DW_OP_reg0 && expression[0].atom <= DW_OP_reg31) {
34 int dwarf_register = expression[0].atom - DW_OP_reg0;
35 xbt_assert(c, "Missing frame context for register operation DW_OP_reg%i", dwarf_register);
36 return Location(dwarf_register_to_libunwind(dwarf_register));
39 simgrid::dwarf::ExpressionStack stack;
40 simgrid::dwarf::execute(expression, context, stack);
41 return Location((void*)stack.top());
44 // TODO, move this in a method of LocationList
45 static simgrid::dwarf::DwarfExpression const* find_expression(simgrid::dwarf::LocationList const& locations,
48 for (simgrid::dwarf::LocationListEntry const& entry : locations)
49 if (entry.valid_for_ip(ip))
50 return &entry.expression();
54 Location resolve(simgrid::dwarf::LocationList const& locations, simgrid::mc::ObjectInformation* object_info,
55 unw_cursor_t* c, void* frame_pointer_address, const simgrid::mc::AddressSpace* address_space)
59 xbt_assert(unw_get_reg(c, UNW_REG_IP, &ip) == 0, "Could not resolve IP");
60 simgrid::dwarf::DwarfExpression const* expression = find_expression(locations, ip);
61 xbt_assert(expression != nullptr, "Could not resolve location");
62 return simgrid::dwarf::resolve(*expression, object_info, c, frame_pointer_address, address_space);
65 LocationList location_list(const simgrid::mc::ObjectInformation& info, Dwarf_Attribute& attr)
67 LocationList locations;
68 std::ptrdiff_t offset = 0;
76 offset = dwarf_getlocations(&attr, offset, &base, &start, &end, &ops, &len);
79 XBT_WARN("Error while loading location list: %s", dwarf_errmsg(-1));
83 auto base_address = reinterpret_cast<std::uint64_t>(info.base_address());
85 LocationListEntry::range_type range;
87 // If start == 0, this is not a location list:
88 range = {0, UINT64_MAX};
90 range = {base_address + start, base_address + end};
92 locations.emplace_back(DwarfExpression(ops, ops + len), range);
97 } // namespace simgrid::dwarf