Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Spell check.
[simgrid.git] / src / mc / inspect / ObjectInformation.cpp
1 /* Copyright (c) 2014-2019. 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 <cstdint>
7 #include <sys/mman.h> // PROT_READ and friends
8 #include <vector>
9
10 #include "src/mc/inspect/Frame.hpp"
11 #include "src/mc/inspect/ObjectInformation.hpp"
12 #include "src/mc/inspect/Variable.hpp"
13 #include "src/mc/mc_private.hpp"
14 #include "xbt/file.hpp"
15
16 namespace simgrid {
17 namespace mc {
18
19 /* For an executable object, addresses are virtual address
20  * (there is no offset) i.e.
21  * \f$\text{virtual address} = \{dwarf address}\f$
22  *
23  * For a shared object, the addresses are offset from the beginning
24  * of the shared object (the base address of the mapped shared
25  * object must be used as offset
26  * i.e. \f$\text{virtual address} = \text{shared object base address}
27  *             + \text{dwarf address}\f$.
28  */
29 void* ObjectInformation::base_address() const
30 {
31   // For an executable (more precisely for a ET_EXEC) the base it 0:
32   if (this->executable())
33     return nullptr;
34
35   // For an a shared-object (ET_DYN, including position-independent executables)
36   // the base address is its lowest address:
37   void* result = this->start_exec;
38   if (this->start_rw != nullptr && result > (void*)this->start_rw)
39     result = this->start_rw;
40   if (this->start_ro != nullptr && result > (void*)this->start_ro)
41     result = this->start_ro;
42   return result;
43 }
44
45 simgrid::mc::Frame* ObjectInformation::find_function(const void* ip) const
46 {
47   /* This is implemented by binary search on a sorted array.
48    *
49    * We do quite a lot of those so we want this to be cache efficient.
50    * We pack the only information we need in the index entries in order
51    * to successfully do the binary search. We do not need the high_pc
52    * during the binary search (only at the end) so it is not included
53    * in the index entry. We could use parallel arrays as well.
54    *
55    * We cannot really use the std:: algorithm for this.
56    * We could use std::binary_search by including the high_pc inside
57    * the FunctionIndexEntry.
58    */
59   const simgrid::mc::FunctionIndexEntry* base = this->functions_index.data();
60   int i                                       = 0;
61   int j                                       = this->functions_index.size() - 1;
62   while (j >= i) {
63     int k = i + ((j - i) / 2);
64
65     /* In most of the search, we do not dereference the base[k].function.
66      * This way the memory accesses are located in the base[k] array. */
67     if (ip < base[k].low_pc)
68       j = k - 1;
69     else if (k < j && ip >= base[k + 1].low_pc)
70       i = k + 1;
71
72     /* At this point, the search is over.
73      * Either we have found the correct function or we do not know
74      * any function corresponding to this instruction address.
75      * Only at the point do we dereference the function pointer. */
76     else if ((std::uint64_t)ip < base[k].function->range.end())
77       return base[k].function;
78     else
79       return nullptr;
80   }
81   return nullptr;
82 }
83
84 const simgrid::mc::Variable* ObjectInformation::find_variable(const char* name) const
85 {
86   for (simgrid::mc::Variable const& variable : this->global_variables) {
87     if (variable.name == name)
88       return &variable;
89   }
90   return nullptr;
91 }
92
93 void ObjectInformation::remove_global_variable(const char* name)
94 {
95   typedef std::vector<Variable>::size_type size_type;
96
97   if (this->global_variables.empty())
98     return;
99
100   // Binary search:
101   size_type first = 0;
102   size_type last  = this->global_variables.size() - 1;
103
104   while (first <= last) {
105     size_type cursor                   = first + (last - first) / 2;
106     simgrid::mc::Variable& current_var = this->global_variables[cursor];
107     int cmp                            = current_var.name.compare(name);
108
109     if (cmp == 0) {
110
111       // Find the whole range:
112       first = cursor;
113       while (first != 0 && this->global_variables[first - 1].name == name)
114         first--;
115       size_type size = this->global_variables.size();
116       last           = cursor;
117       while (last != size - 1 && this->global_variables[last + 1].name == name)
118         last++;
119
120       // Remove the whole range:
121       this->global_variables.erase(this->global_variables.begin() + first, this->global_variables.begin() + last + 1);
122
123       return;
124     } else if (cmp < 0)
125       first = cursor + 1;
126     else if (cursor != 0)
127       last = cursor - 1;
128     else
129       break;
130   }
131 }
132
133 /** Ignore a local variable in a scope
134  *
135  *  Ignore all instances of variables with a given name in
136  *  any (possibly inlined) subprogram with a given namespaced
137  *  name.
138  *
139  *  @param var_name        Name of the local variable to ignore
140  *  @param subprogram_name Name of the subprogram to ignore (nullptr for any)
141  *  @param subprogram      (possibly inlined) Subprogram of the scope current scope
142  *  @param scope           Current scope
143  */
144 static void remove_local_variable(simgrid::mc::Frame& scope, const char* var_name, const char* subprogram_name,
145                                   simgrid::mc::Frame const& subprogram)
146 {
147   typedef std::vector<Variable>::size_type size_type;
148
149   // If the current subprogram matches the given name:
150   if ((subprogram_name == nullptr || (not subprogram.name.empty() && subprogram.name == subprogram_name)) &&
151       not scope.variables.empty()) {
152
153     // Try to find the variable and remove it:
154     size_type start = 0;
155     size_type end   = scope.variables.size() - 1;
156
157     // Binary search:
158     while (start <= end) {
159       size_type cursor                   = start + (end - start) / 2;
160       simgrid::mc::Variable& current_var = scope.variables[cursor];
161       int compare                        = current_var.name.compare(var_name);
162       if (compare == 0) {
163         // Variable found, remove it:
164         scope.variables.erase(scope.variables.begin() + cursor);
165         break;
166       } else if (compare < 0)
167         start = cursor + 1;
168       else if (cursor != 0)
169         end = cursor - 1;
170       else
171         break;
172     }
173   }
174
175   // And recursive processing in nested scopes:
176   for (simgrid::mc::Frame& nested_scope : scope.scopes) {
177     // The new scope may be an inlined subroutine, in this case we want to use its
178     // namespaced name in recursive calls:
179     simgrid::mc::Frame const& nested_subprogram =
180         nested_scope.tag == DW_TAG_inlined_subroutine ? nested_scope : subprogram;
181     remove_local_variable(nested_scope, var_name, subprogram_name, nested_subprogram);
182   }
183 }
184
185 void ObjectInformation::remove_local_variable(const char* var_name, const char* subprogram_name)
186 {
187   for (auto& entry : this->subprograms)
188     simgrid::mc::remove_local_variable(entry.second, var_name, subprogram_name, entry.second);
189 }
190
191 /** @brief Fills the position of the segments (executable, read-only, read/write) */
192 // TODO, use the ELF segment information for more robustness
193 void find_object_address(std::vector<simgrid::xbt::VmMap> const& maps, simgrid::mc::ObjectInformation* result)
194 {
195   const int PROT_RW = PROT_READ | PROT_WRITE;
196   const int PROT_RX = PROT_READ | PROT_EXEC;
197
198   std::string name = simgrid::xbt::Path(result->file_name).get_base_name();
199
200   for (size_t i = 0; i < maps.size(); ++i) {
201     simgrid::xbt::VmMap const& reg = maps[i];
202     if (maps[i].pathname.empty())
203       continue;
204     std::string map_basename = simgrid::xbt::Path(maps[i].pathname).get_base_name();
205     if (map_basename != name)
206       continue;
207
208     // This is the non-GNU_RELRO-part of the data segment:
209     if (reg.prot == PROT_RW) {
210       xbt_assert(not result->start_rw, "Multiple read-write segments for %s, not supported", maps[i].pathname.c_str());
211       result->start_rw = (char*)reg.start_addr;
212       result->end_rw   = (char*)reg.end_addr;
213
214       // The next VMA might be end of the data segment:
215       if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
216           maps[i + 1].start_addr == reg.end_addr)
217         result->end_rw = (char*)maps[i + 1].end_addr;
218     }
219
220     // This is the text segment:
221     else if (reg.prot == PROT_RX) {
222       xbt_assert(not result->start_exec, "Multiple executable segments for %s, not supported",
223                  maps[i].pathname.c_str());
224       result->start_exec = (char*)reg.start_addr;
225       result->end_exec   = (char*)reg.end_addr;
226
227       // The next VMA might be end of the data segment:
228       if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
229           maps[i + 1].start_addr == reg.end_addr) {
230         result->start_rw = (char*)maps[i + 1].start_addr;
231         result->end_rw   = (char*)maps[i + 1].end_addr;
232       }
233     }
234
235     // This is the GNU_RELRO-part of the data segment:
236     else if (reg.prot == PROT_READ) {
237       xbt_assert(not result->start_ro,
238                  "Multiple read-only segments for %s, not supported. Compiling with the following may help: "
239                  "-Wl,-znorelro -Wl,-znoseparate-code",
240                  maps[i].pathname.c_str());
241       result->start_ro = (char*)reg.start_addr;
242       result->end_ro   = (char*)reg.end_addr;
243     }
244   }
245
246   result->start = result->start_rw;
247   if ((const void*)result->start_ro < result->start)
248     result->start = result->start_ro;
249   if ((const void*)result->start_exec < result->start)
250     result->start = result->start_exec;
251
252   result->end = result->end_rw;
253   if (result->end_ro && (const void*)result->end_ro > result->end)
254     result->end = result->end_ro;
255   if (result->end_exec && (const void*)result->end_exec > result->end)
256     result->end = result->end_exec;
257
258   xbt_assert(result->start_exec || result->start_rw || result->start_ro);
259 }
260
261 } // namespace mc
262 } // namespace simgrid