Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix
[simgrid.git] / src / mc / ObjectInformation.cpp
1 /* Copyright (c) 2014-2015. 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 ObjectInformation::ObjectInformation() {}
19
20 /* For an executable object, addresses are virtual address
21  * (there is no offset) i.e.
22  * \f$\text{virtual address} = \{dwarf address}\f$;
23  *
24  * For a shared object, the addreses are offset from the begining
25  * of the shared object (the base address of the mapped shared
26  * object must be used as offset
27  * i.e. \f$\text{virtual address} = \text{shared object base address}
28  *             + \text{dwarf address}\f$.
29  */
30 void *ObjectInformation::base_address() const
31 {
32   // For an executable (more precisely for a ET_EXEC) the base it 0:
33   if (this->executable())
34     return nullptr;
35
36   // For an a shared-object (ET_DYN, including position-independant executables)
37   // the base address is its lowest address:
38   void *result = this->start_exec;
39   if (this->start_rw != nullptr && result > (void *) this->start_rw)
40     result = this->start_rw;
41   if (this->start_ro != nullptr && result > (void *) this->start_ro)
42     result = this->start_ro;
43   return result;
44 }
45
46 simgrid::mc::Frame* ObjectInformation::find_function(const void *ip) const
47 {
48   /* This is implemented by binary search on a sorted array.
49    *
50    * We do quite a lot of those so we want this to be cache efficient.
51    * We pack the only information we need in the index entries in order
52    * to successfully do the binary search. We do not need the high_pc
53    * during the binary search (only at the end) so it is not included
54    * in the index entry. We could use parallel arrays as well.
55    *
56    * We cannot really use the std:: algorithm for this.
57    * We could use std::binary_search by including the high_pc inside
58    * the FunctionIndexEntry.
59    */
60   const simgrid::mc::FunctionIndexEntry* base =
61     this->functions_index.data();
62   int i = 0;
63   int j = this->functions_index.size() - 1;
64   while (j >= i) {
65     int k = i + ((j - i) / 2);
66
67     /* In most of the search, we do not dereference the base[k].function.
68      * This way the memory accesses are located in the base[k] array. */
69     if (ip < base[k].low_pc)
70       j = k - 1;
71     else if (k < j && ip >= base[k + 1].low_pc)
72       i = k + 1;
73
74     /* At this point, the search is over.
75      * Either we have found the correct function or we do not know
76      * any function corresponding to this instruction address.
77      * Only at the point do we dereference the function pointer. */
78     else if ((std::uint64_t) ip < base[k].function->range.end())
79       return base[k].function;
80     else
81       return nullptr;
82   }
83   return nullptr;
84 }
85
86 simgrid::mc::Variable* ObjectInformation::find_variable(const char* name) const
87 {
88   for (simgrid::mc::Variable& variable : this->global_variables)
89     if(variable.name == name)
90       return &variable;
91   return nullptr;
92 }
93
94 void ObjectInformation::remove_global_variable(const char* name)
95 {
96   typedef std::vector<Variable>::size_type size_type;
97
98   if (this->global_variables.empty())
99     return;
100
101   // Binary search:
102   size_type first = 0;
103   size_type last = this->global_variables.size() - 1;
104
105   while (first <= last) {
106     size_type cursor = first + (last - first) / 2;
107     simgrid::mc::Variable& current_var = this->global_variables[cursor];
108     int cmp = current_var.name.compare(name);
109
110     if (cmp == 0) {
111   
112       // Find the whole range:
113       size_type first = cursor;
114       while (first != 0 && this->global_variables[first - 1].name == name)
115         first--;
116       size_type size = this->global_variables.size();
117       size_type last = cursor;
118       while (last != size - 1 && this->global_variables[last + 1].name == name)
119         last++;
120   
121       // Remove the whole range:
122       this->global_variables.erase(
123         this->global_variables.begin() + first,
124         this->global_variables.begin() + last + 1);
125   
126       return;
127     } else if (cmp < 0)
128       first = cursor + 1;
129     else if (cursor != 0)
130       last = cursor - 1;
131     else
132       break;
133   }
134 }
135
136 /** Ignore a local variable in a scope
137  *
138  *  Ignore all instances of variables with a given name in
139  *  any (possibly inlined) subprogram with a given namespaced
140  *  name.
141  *
142  *  @param var_name        Name of the local variable to ignore
143  *  @param subprogram_name Name of the subprogram to ignore (nullptr for any)
144  *  @param subprogram      (possibly inlined) Subprogram of the scope current scope
145  *  @param scope           Current scope
146  */
147 static void remove_local_variable(simgrid::mc::Frame& scope,
148                             const char *var_name,
149                             const char *subprogram_name,
150                             simgrid::mc::Frame const& subprogram)
151 {
152   typedef std::vector<Variable>::size_type size_type;
153
154   // If the current subprogram matches the given name:
155   if ((subprogram_name == nullptr ||
156       (!subprogram.name.empty()
157         && subprogram.name == subprogram_name))
158       && !scope.variables.empty()) {
159
160     // Try to find the variable and remove it:
161     size_type start = 0;
162     size_type end = scope.variables.size() - 1;
163
164     // Binary search:
165     while (start <= end) {
166       size_type cursor = start + (end - start) / 2;
167       simgrid::mc::Variable& current_var = scope.variables[cursor];
168       int compare = current_var.name.compare(var_name);
169       if (compare == 0) {
170         // Variable found, remove it:
171         scope.variables.erase(scope.variables.begin() + cursor);
172         break;
173       } else if (compare < 0)
174         start = cursor + 1;
175       else if (cursor != 0)
176         end = cursor - 1;
177       else
178         break;
179     }
180   }
181
182   // And recursive processing in nested scopes:
183   for (simgrid::mc::Frame& nested_scope : scope.scopes) {
184     // The new scope may be an inlined subroutine, in this case we want to use its
185     // namespaced name in recursive calls:
186     simgrid::mc::Frame const& nested_subprogram =
187         nested_scope.tag ==
188         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
189     remove_local_variable(nested_scope, var_name, subprogram_name,
190                           nested_subprogram);
191   }
192 }
193
194 void ObjectInformation::remove_local_variable(
195   const char* var_name, const char* subprogram_name)
196 {
197   for (auto& entry : this->subprograms)
198     simgrid::mc::remove_local_variable(entry.second,
199       var_name, subprogram_name, entry.second);
200 }
201
202 }
203 }