Logo AND Algorithmique Numérique Distribuée

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