Logo AND Algorithmique Numérique Distribuée

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