1 /* Copyright (c) 2014-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "src/mc/Frame.hpp"
12 #include "src/mc/ObjectInformation.hpp"
13 #include "src/mc/Variable.hpp"
18 ObjectInformation::ObjectInformation() {}
20 /* For an executable object, addresses are virtual address
21 * (there is no offset) i.e.
22 * \f$\text{virtual address} = \{dwarf address}\f$;
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$.
30 void *ObjectInformation::base_address() const
32 // For an executable (more precisely for a ET_EXEC) the base it 0:
33 if (this->executable())
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;
46 simgrid::mc::Frame* ObjectInformation::find_function(const void *ip) const
48 /* This is implemented by binary search on a sorted array.
50 * We do quite a lot ot 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.
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.
60 const simgrid::mc::FunctionIndexEntry* base =
61 this->functions_index.data();
63 int j = this->functions_index.size() - 1;
65 int k = i + ((j - i) / 2);
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)
71 else if (k < j && ip >= base[k + 1].low_pc)
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;
86 simgrid::mc::Variable* ObjectInformation::find_variable(const char* name) const
88 for (simgrid::mc::Variable& variable : this->global_variables)
89 if(variable.name == name)
94 void ObjectInformation::remove_global_variable(const char* name)
96 typedef std::vector<Variable>::size_type size_type;
98 if (this->global_variables.empty())
103 size_type last = this->global_variables.size() - 1;
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);
112 // Find the whole range:
113 size_type first = cursor;
114 while (first != 0 && this->global_variables[first - 1].name == name)
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)
121 // Remove the whole range:
122 this->global_variables.erase(
123 this->global_variables.begin() + first,
124 this->global_variables.begin() + last + 1);
129 else if (cursor != 0)
136 /** Ignore a local variable in a scope
138 * Ignore all instances of variables with a given name in
139 * any (possibly inlined) subprogram with a given namespaced
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
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)
152 typedef std::vector<Variable>::size_type size_type;
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()) {
160 // Try to find the variable and remove it:
162 size_type end = scope.variables.size() - 1;
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);
170 // Variable found, remove it:
171 scope.variables.erase(scope.variables.begin() + cursor);
173 } else if (compare < 0)
175 else if (cursor != 0)
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 =
188 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
189 remove_local_variable(nested_scope, var_name, subprogram_name,
194 void ObjectInformation::remove_local_variable(
195 const char* var_name, const char* subprogram_name)
197 for (auto& entry : this->subprograms)
198 simgrid::mc::remove_local_variable(entry.second,
199 var_name, subprogram_name, entry.second);