1 /* Copyright (c) 2014-2023. 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. */
8 #include <sys/mman.h> // PROT_READ and friends
11 #include "src/mc/inspect/Frame.hpp"
12 #include "src/mc/inspect/ObjectInformation.hpp"
13 #include "src/mc/inspect/Variable.hpp"
14 #include "src/mc/mc_private.hpp"
15 #include "xbt/file.hpp"
17 namespace simgrid::mc {
19 /* For an executable object, addresses are virtual address (there is no offset) i.e.
20 * \f$\text{virtual address} = \{dwarf address}\f$
22 * For a shared object, the addresses are offset from the beginning of the shared object (the base address of the
23 * mapped shared object must be used as offset
24 * i.e. \f$\text{virtual address} = \text{shared object base address}
25 * + \text{dwarf address}\f$.
27 void* ObjectInformation::base_address() const
29 // For an executable (more precisely for an ET_EXEC) the base it 0:
30 if (this->executable())
33 // For an a shared-object (ET_DYN, including position-independent executables) the base address is its lowest address:
34 void* result = this->start_exec;
35 if (this->start_rw != nullptr && result > (void*)this->start_rw)
36 result = this->start_rw;
37 if (this->start_ro != nullptr && result > (void*)this->start_ro)
38 result = this->start_ro;
42 Frame* ObjectInformation::find_function(const void* ip)
44 ensure_dwarf_loaded();
46 /* This is implemented by binary search on a sorted array.
48 * We do quite a lot of those so we want this to be cache efficient.
49 * We pack the only information we need in the index entries in order
50 * to successfully do the binary search. We do not need the high_pc
51 * during the binary search (only at the end) so it is not included
52 * in the index entry. We could use parallel arrays as well.
54 * Note the usage of reverse iterators to match the correct interval.
56 auto pos = std::lower_bound(this->functions_index.rbegin(), this->functions_index.rend(), ip,
57 [](auto const& func, auto const* addr) { return func.low_pc > addr; });
59 /* At this point, the search is over.
60 * Either we have found the correct function or we do not know
61 * any function corresponding to this instruction address.
62 * Only at the point do we dereference the function pointer. */
63 return (pos != this->functions_index.rend() && reinterpret_cast<std::uint64_t>(ip) < pos->function->range.end())
68 const Variable* ObjectInformation::find_variable(const char* var_name)
70 ensure_dwarf_loaded();
72 auto pos = std::lower_bound(this->global_variables.begin(), this->global_variables.end(), var_name,
73 [](auto const& var, const char* name) { return var.name < name; });
74 return (pos != this->global_variables.end() && pos->name == var_name) ? &(*pos) : nullptr;
77 void ObjectInformation::remove_global_variable(const char* var_name)
80 auto pos1 = std::lower_bound(this->global_variables.begin(), this->global_variables.end(), var_name,
81 [](auto const& var, const char* name) { return var.name < name; });
82 // Find the whole range:
83 auto pos2 = std::upper_bound(pos1, this->global_variables.end(), var_name,
84 [](const char* name, auto const& var) { return name < var.name; });
85 // Remove the whole range:
86 this->global_variables.erase(pos1, pos2);
89 /** Ignore a local variable in a scope
91 * Ignore all instances of variables with a given name in any (possibly inlined) subprogram with a given namespaced
94 * @param var_name Name of the local variable to ignore
95 * @param subprogram_name Name of the subprogram to ignore (nullptr for any)
96 * @param subprogram (possibly inlined) Subprogram of the scope current scope
97 * @param scope Current scope
99 static void remove_local_variable(Frame& scope, const char* var_name, const char* subprogram_name,
100 Frame const& subprogram)
102 // If the current subprogram matches the given name:
103 if (subprogram_name == nullptr || (not subprogram.name.empty() && subprogram.name == subprogram_name)) {
104 // Try to find the variable and remove it:
107 auto pos = std::lower_bound(scope.variables.begin(), scope.variables.end(), var_name,
108 [](auto const& var, const char* name) { return var.name < name; });
109 if (pos != scope.variables.end() && pos->name == var_name) {
110 // Variable found, remove it:
111 scope.variables.erase(pos);
115 // And recursive processing in nested scopes:
116 for (Frame& nested_scope : scope.scopes) {
117 // The new scope may be an inlined subroutine, in this case we want to use its
118 // namespaced name in recursive calls:
119 Frame const& nested_subprogram = nested_scope.tag == DW_TAG_inlined_subroutine ? nested_scope : subprogram;
120 remove_local_variable(nested_scope, var_name, subprogram_name, nested_subprogram);
124 void ObjectInformation::remove_local_variable(const char* var_name, const char* subprogram_name)
126 for (auto& [_, entry] : this->subprograms)
127 mc::remove_local_variable(entry, var_name, subprogram_name, entry);
130 /** @brief Fills the position of the segments (executable, read-only, read/write) */
131 // TODO, use the ELF segment information for more robustness
132 void find_object_address(std::vector<xbt::VmMap> const& maps, ObjectInformation* result)
134 const int PROT_RW = PROT_READ | PROT_WRITE;
135 const int PROT_RX = PROT_READ | PROT_EXEC;
137 std::string name = xbt::Path(result->file_name).get_base_name();
139 for (size_t i = 0; i < maps.size(); ++i) {
140 simgrid::xbt::VmMap const& reg = maps[i];
141 if (reg.pathname.empty() || name != simgrid::xbt::Path(reg.pathname).get_base_name())
144 // This is the non-GNU_RELRO-part of the data segment:
145 if (reg.prot == PROT_RW) {
146 xbt_assert(not result->start_rw, "Multiple read-write segments for %s, not supported", maps[i].pathname.c_str());
147 result->start_rw = (char*)reg.start_addr;
148 result->end_rw = (char*)reg.end_addr;
150 // The next VMA might be end of the data segment:
151 if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
152 maps[i + 1].start_addr == reg.end_addr)
153 result->end_rw = (char*)maps[i + 1].end_addr;
156 // This is the text segment:
157 else if (reg.prot == PROT_RX) {
158 xbt_assert(not result->start_exec, "Multiple executable segments for %s, not supported",
159 maps[i].pathname.c_str());
160 result->start_exec = (char*)reg.start_addr;
161 result->end_exec = (char*)reg.end_addr;
163 // The next VMA might be end of the data segment:
164 if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
165 maps[i + 1].start_addr == reg.end_addr) {
166 result->start_rw = (char*)maps[i + 1].start_addr;
167 result->end_rw = (char*)maps[i + 1].end_addr;
171 // This is the GNU_RELRO-part of the data segment:
172 else if (reg.prot == PROT_READ) {
173 xbt_assert(not result->start_ro,
174 "Multiple read-only segments for %s, not supported. Compiling with the following may help: "
175 "-g -Wl,-znorelro -Wl,-znoseparate-code",
176 maps[i].pathname.c_str());
177 result->start_ro = (char*)reg.start_addr;
178 result->end_ro = (char*)reg.end_addr;
182 result->start = result->start_rw;
183 if ((const void*)result->start_ro < result->start)
184 result->start = result->start_ro;
185 if ((const void*)result->start_exec < result->start)
186 result->start = result->start_exec;
188 result->end = result->end_rw;
189 if (result->end_ro && (const void*)result->end_ro > result->end)
190 result->end = result->end_ro;
191 if (result->end_exec && (const void*)result->end_exec > result->end)
192 result->end = result->end_exec;
194 xbt_assert(result->start_exec || result->start_rw || result->start_ro);
197 } // namespace simgrid::mc