1 /* Copyright (c) 2004-2019. 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. */
10 #include "xbt/asserts.h"
11 #include "xbt/sysdep.h"
13 #include <libunwind.h>
15 #include "src/mc/LocationList.hpp"
16 #include "src/mc/ObjectInformation.hpp"
17 #include "src/mc/mc_dwarf.hpp"
22 /** Resolve a location expression */
23 Location resolve(simgrid::dwarf::DwarfExpression const& expression, simgrid::mc::ObjectInformation* object_info,
24 unw_cursor_t* c, void* frame_pointer_address, 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;
32 context.process_index = process_index;
34 if (not expression.empty() && expression[0].atom >= DW_OP_reg0 && expression[0].atom <= DW_OP_reg31) {
35 int dwarf_register = expression[0].atom - DW_OP_reg0;
36 xbt_assert(c, "Missing frame context for register operation DW_OP_reg%i", dwarf_register);
37 return Location(dwarf_register_to_libunwind(dwarf_register));
40 simgrid::dwarf::ExpressionStack stack;
41 simgrid::dwarf::execute(expression, context, stack);
42 return Location((void*)stack.top());
45 // TODO, move this in a method of LocationList
46 static simgrid::dwarf::DwarfExpression const* find_expression(simgrid::dwarf::LocationList const& locations,
49 for (simgrid::dwarf::LocationListEntry const& entry : locations)
50 if (entry.valid_for_ip(ip))
51 return &entry.expression();
55 Location resolve(simgrid::dwarf::LocationList const& locations, simgrid::mc::ObjectInformation* object_info,
56 unw_cursor_t* c, void* frame_pointer_address, simgrid::mc::AddressSpace* address_space,
60 if (c && unw_get_reg(c, UNW_REG_IP, &ip))
61 xbt_die("Could not resolve IP");
62 simgrid::dwarf::DwarfExpression const* expression = find_expression(locations, ip);
64 xbt_die("Could not resolve location");
65 return simgrid::dwarf::resolve(*expression, object_info, c, frame_pointer_address, address_space, process_index);
68 LocationList location_list(simgrid::mc::ObjectInformation& info, Dwarf_Attribute& attr)
70 LocationList locations;
71 std::ptrdiff_t offset = 0;
79 offset = dwarf_getlocations(&attr, offset, &base, &start, &end, &ops, &len);
83 else if (offset == -1)
84 xbt_die("Error while loading location list");
86 std::uint64_t base_address = (std::uint64_t)info.base_address();
88 LocationListEntry::range_type range;
90 // If start == 0, this is not a location list:
91 range = {0, UINT64_MAX};
93 range = {base_address + start, base_address + end};
95 locations.push_back({DwarfExpression(ops, ops + len), range});
101 } // namespace simgrid