Logo AND Algorithmique Numérique Distribuée

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