Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / mc / inspect / LocationList.cpp
1 /* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/mc/inspect/LocationList.hpp"
7 #include "src/mc/inspect/ObjectInformation.hpp"
8 #include "src/mc/inspect/mc_dwarf.hpp"
9
10 #include "xbt/asserts.h"
11 #include "xbt/log.h"
12 #include "xbt/sysdep.h"
13
14 #include <cstddef>
15 #include <cstdint>
16 #include <libunwind.h>
17 #include <utility>
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(mc_dwarf);
20
21 namespace simgrid {
22 namespace dwarf {
23
24 /** Resolve a location expression */
25 Location resolve(simgrid::dwarf::DwarfExpression const& expression, simgrid::mc::ObjectInformation* object_info,
26                  unw_cursor_t* c, void* frame_pointer_address, const simgrid::mc::AddressSpace* address_space)
27 {
28   simgrid::dwarf::ExpressionContext context;
29   context.frame_base    = frame_pointer_address;
30   context.cursor        = c;
31   context.address_space = address_space;
32   context.object_info   = object_info;
33
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));
38   }
39
40   simgrid::dwarf::ExpressionStack stack;
41   simgrid::dwarf::execute(expression, context, stack);
42   return Location((void*)stack.top());
43 }
44
45 // TODO, move this in a method of LocationList
46 static simgrid::dwarf::DwarfExpression const* find_expression(simgrid::dwarf::LocationList const& locations,
47                                                               unw_word_t ip)
48 {
49   for (simgrid::dwarf::LocationListEntry const& entry : locations)
50     if (entry.valid_for_ip(ip))
51       return &entry.expression();
52   return nullptr;
53 }
54
55 Location resolve(simgrid::dwarf::LocationList const& locations, simgrid::mc::ObjectInformation* object_info,
56                  unw_cursor_t* c, void* frame_pointer_address, const simgrid::mc::AddressSpace* address_space)
57 {
58   unw_word_t ip = 0;
59   if (c)
60     xbt_assert(unw_get_reg(c, UNW_REG_IP, &ip) == 0, "Could not resolve IP");
61   simgrid::dwarf::DwarfExpression const* expression = find_expression(locations, ip);
62   xbt_assert(expression != nullptr, "Could not resolve location");
63   return simgrid::dwarf::resolve(*expression, object_info, c, frame_pointer_address, address_space);
64 }
65
66 LocationList location_list(const simgrid::mc::ObjectInformation& info, Dwarf_Attribute& attr)
67 {
68   LocationList locations;
69   std::ptrdiff_t offset = 0;
70   while (true) {
71     Dwarf_Addr base;
72     Dwarf_Addr start;
73     Dwarf_Addr end;
74     Dwarf_Op* ops;
75     std::size_t len;
76
77     offset = dwarf_getlocations(&attr, offset, &base, &start, &end, &ops, &len);
78
79     if (offset == -1)
80       XBT_WARN("Error while loading location list: %s", dwarf_errmsg(-1));
81     if (offset <= 0)
82       break;
83
84     auto base_address = reinterpret_cast<std::uint64_t>(info.base_address());
85
86     LocationListEntry::range_type range;
87     if (start == 0)
88       // If start == 0, this is not a location list:
89       range = {0, UINT64_MAX};
90     else
91       range = {base_address + start, base_address + end};
92
93     locations.emplace_back(DwarfExpression(ops, ops + len), range);
94   }
95
96   return locations;
97 }
98 } // namespace dwarf
99 } // namespace simgrid