Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / mc / inspect / LocationList.cpp
1 /* Copyright (c) 2004-2021. 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/sysdep.h"
12
13 #include <cstddef>
14 #include <cstdint>
15 #include <libunwind.h>
16 #include <utility>
17
18 namespace simgrid {
19 namespace dwarf {
20
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)
24 {
25   simgrid::dwarf::ExpressionContext context;
26   context.frame_base    = frame_pointer_address;
27   context.cursor        = c;
28   context.address_space = address_space;
29   context.object_info   = object_info;
30
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));
35   }
36
37   simgrid::dwarf::ExpressionStack stack;
38   simgrid::dwarf::execute(expression, context, stack);
39   return Location((void*)stack.top());
40 }
41
42 // TODO, move this in a method of LocationList
43 static simgrid::dwarf::DwarfExpression const* find_expression(simgrid::dwarf::LocationList const& locations,
44                                                               unw_word_t ip)
45 {
46   for (simgrid::dwarf::LocationListEntry const& entry : locations)
47     if (entry.valid_for_ip(ip))
48       return &entry.expression();
49   return nullptr;
50 }
51
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)
54 {
55   unw_word_t ip = 0;
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);
59   if (not expression)
60     xbt_die("Could not resolve location");
61   return simgrid::dwarf::resolve(*expression, object_info, c, frame_pointer_address, address_space);
62 }
63
64 LocationList location_list(const simgrid::mc::ObjectInformation& info, Dwarf_Attribute& attr)
65 {
66   LocationList locations;
67   std::ptrdiff_t offset = 0;
68   while (true) {
69     Dwarf_Addr base;
70     Dwarf_Addr start;
71     Dwarf_Addr end;
72     Dwarf_Op* ops;
73     std::size_t len;
74
75     offset = dwarf_getlocations(&attr, offset, &base, &start, &end, &ops, &len);
76
77     if (offset == 0)
78       break;
79     else if (offset == -1)
80       xbt_die("Error while loading location list");
81
82     auto base_address = reinterpret_cast<std::uint64_t>(info.base_address());
83
84     LocationListEntry::range_type range;
85     if (start == 0)
86       // If start == 0, this is not a location list:
87       range = {0, UINT64_MAX};
88     else
89       range = {base_address + start, base_address + end};
90
91     locations.emplace_back(DwarfExpression(ops, ops + len), range);
92   }
93
94   return locations;
95 }
96 } // namespace dwarf
97 } // namespace simgrid