Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / mc / ObjectInformation.cpp
1 /* Copyright (c) 2014-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <cstdint>
8
9 #include <vector>
10
11 #include "src/mc/Frame.hpp"
12 #include "src/mc/ObjectInformation.hpp"
13 #include "src/mc/Variable.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 addreses 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-independant 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 =
59     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 simgrid::mc::Variable* ObjectInformation::find_variable(const char* name) const
85 {
86   for (simgrid::mc::Variable& variable : this->global_variables)
87     if(variable.name == name)
88       return &variable;
89   return nullptr;
90 }
91
92 void ObjectInformation::remove_global_variable(const char* name)
93 {
94   typedef std::vector<Variable>::size_type size_type;
95
96   if (this->global_variables.empty())
97     return;
98
99   // Binary search:
100   size_type first = 0;
101   size_type last = this->global_variables.size() - 1;
102
103   while (first <= last) {
104     size_type cursor = first + (last - first) / 2;
105     simgrid::mc::Variable& current_var = this->global_variables[cursor];
106     int cmp = current_var.name.compare(name);
107
108     if (cmp == 0) {
109
110       // Find the whole range:
111       size_type first = cursor;
112       while (first != 0 && this->global_variables[first - 1].name == name)
113         first--;
114       size_type size = this->global_variables.size();
115       size_type last = cursor;
116       while (last != size - 1 && this->global_variables[last + 1].name == name)
117         last++;
118
119       // Remove the whole range:
120       this->global_variables.erase(
121         this->global_variables.begin() + first,
122         this->global_variables.begin() + last + 1);
123
124       return;
125     } else if (cmp < 0)
126       first = cursor + 1;
127     else if (cursor != 0)
128       last = cursor - 1;
129     else
130       break;
131   }
132 }
133
134 /** Ignore a local variable in a scope
135  *
136  *  Ignore all instances of variables with a given name in
137  *  any (possibly inlined) subprogram with a given namespaced
138  *  name.
139  *
140  *  @param var_name        Name of the local variable to ignore
141  *  @param subprogram_name Name of the subprogram to ignore (nullptr for any)
142  *  @param subprogram      (possibly inlined) Subprogram of the scope current scope
143  *  @param scope           Current scope
144  */
145 static void remove_local_variable(simgrid::mc::Frame& scope,
146                             const char *var_name,
147                             const char *subprogram_name,
148                             simgrid::mc::Frame const& subprogram)
149 {
150   typedef std::vector<Variable>::size_type size_type;
151
152   // If the current subprogram matches the given name:
153   if ((subprogram_name == nullptr || (not subprogram.name.empty() && subprogram.name == subprogram_name)) &&
154       not scope.variables.empty()) {
155
156     // Try to find the variable and remove it:
157     size_type start = 0;
158     size_type end = scope.variables.size() - 1;
159
160     // Binary search:
161     while (start <= end) {
162       size_type cursor = start + (end - start) / 2;
163       simgrid::mc::Variable& current_var = scope.variables[cursor];
164       int compare = current_var.name.compare(var_name);
165       if (compare == 0) {
166         // Variable found, remove it:
167         scope.variables.erase(scope.variables.begin() + cursor);
168         break;
169       } else if (compare < 0)
170         start = cursor + 1;
171       else if (cursor != 0)
172         end = cursor - 1;
173       else
174         break;
175     }
176   }
177
178   // And recursive processing in nested scopes:
179   for (simgrid::mc::Frame& nested_scope : scope.scopes) {
180     // The new scope may be an inlined subroutine, in this case we want to use its
181     // namespaced name in recursive calls:
182     simgrid::mc::Frame const& nested_subprogram =
183         nested_scope.tag ==
184         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
185     remove_local_variable(nested_scope, var_name, subprogram_name,
186                           nested_subprogram);
187   }
188 }
189
190 void ObjectInformation::remove_local_variable(
191   const char* var_name, const char* subprogram_name)
192 {
193   for (auto& entry : this->subprograms)
194     simgrid::mc::remove_local_variable(entry.second,
195       var_name, subprogram_name, entry.second);
196 }
197
198 }
199 }