Logo AND Algorithmique Numérique Distribuée

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