Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make Frame::variables, Frame::scopes and ObjectInformation::global_variables...
[simgrid.git] / src / mc / mcer_ignore.cpp
1 /* Copyright (c) 2008-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 "internal_config.h"
8 #include "mc_object_info.h"
9 #include "mc/mc_private.h"
10 #include "smpi/private.h"
11 #include "mc/mc_snapshot.h"
12 #include "mc/mc_ignore.h"
13 #include "mc/mc_protocol.h"
14 #include "mc/mc_client.h"
15
16 extern "C" {
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mcer_ignore, mc,
19                                 "Logging specific to MC ignore mechanism");
20
21 // ***** Ignore heap chunks
22
23 extern xbt_dynar_t mc_heap_comparison_ignore;
24
25 void heap_ignore_region_free(mc_heap_ignore_region_t r)
26 {
27   xbt_free(r);
28 }
29
30 void heap_ignore_region_free_voidp(void *r)
31 {
32   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
33 }
34
35
36 void MC_heap_region_ignore_insert(mc_heap_ignore_region_t region)
37 {
38   if (mc_heap_comparison_ignore == NULL) {
39     mc_heap_comparison_ignore =
40         xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
41                       heap_ignore_region_free_voidp);
42     xbt_dynar_push(mc_heap_comparison_ignore, &region);
43     return;
44   }
45
46   unsigned int cursor = 0;
47   mc_heap_ignore_region_t current_region = NULL;
48   int start = 0;
49   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
50
51   // Find the position where we want to insert the mc_heap_ignore_region_t:
52   while (start <= end) {
53     cursor = (start + end) / 2;
54     current_region =
55         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
56                                                    cursor,
57                                                    mc_heap_ignore_region_t);
58     if (current_region->address == region->address) {
59       heap_ignore_region_free(region);
60       return;
61     } else if (current_region->address < region->address) {
62       start = cursor + 1;
63     } else {
64       end = cursor - 1;
65     }
66   }
67
68   // Insert it mc_heap_ignore_region_t:
69   if (current_region->address < region->address)
70     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, &region);
71   else
72     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, &region);
73 }
74
75 void MC_heap_region_ignore_remove(void *address, size_t size)
76 {
77   unsigned int cursor = 0;
78   int start = 0;
79   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
80   mc_heap_ignore_region_t region;
81   int ignore_found = 0;
82
83   while (start <= end) {
84     cursor = (start + end) / 2;
85     region =
86         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
87                                                    cursor,
88                                                    mc_heap_ignore_region_t);
89     if (region->address == address) {
90       ignore_found = 1;
91       break;
92     } else if (region->address < address) {
93       start = cursor + 1;
94     } else {
95       if ((char *) region->address <= ((char *) address + size)) {
96         ignore_found = 1;
97         break;
98       } else {
99         end = cursor - 1;
100       }
101     }
102   }
103
104   if (ignore_found == 1) {
105     xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
106     MC_remove_ignore_heap(address, size);
107   }
108 }
109
110 // ***** Ignore global variables
111
112 void MCer_ignore_global_variable(const char *name)
113 {
114   mc_process_t process = &mc_model_checker->process();
115   xbt_assert(!process->object_infos.empty(), "MC subsystem not initialized");
116
117   for (std::shared_ptr<s_mc_object_info_t> const& info : process->object_infos) {
118
119     // Binary search:
120     int start = 0;
121     int end = info->global_variables.size() - 1;
122     while (start <= end) {
123       unsigned int cursor = (start + end) / 2;
124       mc_variable_t current_var = &info->global_variables[cursor];
125       int cmp = strcmp(current_var->name.c_str(), name);
126       if (cmp == 0) {
127         info->global_variables.erase(
128           info->global_variables.begin() + cursor);
129         start = 0;
130         end = info->global_variables.size() - 1;
131       } else if (cmp < 0) {
132         start = cursor + 1;
133       } else {
134         end = cursor - 1;
135       }
136     }
137   }
138 }
139
140 // ***** Ignore local variables
141
142 static void mc_ignore_local_variable_in_scope(const char *var_name,
143                                               const char *subprogram_name,
144                                               mc_frame_t subprogram,
145                                               mc_frame_t scope);
146 static void MC_ignore_local_variable_in_object(const char *var_name,
147                                                const char *subprogram_name,
148                                                mc_object_info_t info);
149
150 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
151 {
152   mc_process_t process = &mc_model_checker->process();
153   if (strcmp(frame_name, "*") == 0)
154     frame_name = NULL;
155
156   for (std::shared_ptr<s_mc_object_info_t> const& info : process->object_infos)
157     MC_ignore_local_variable_in_object(var_name, frame_name, info.get());
158 }
159
160 static void MC_ignore_local_variable_in_object(const char *var_name,
161                                                const char *subprogram_name,
162                                                mc_object_info_t info)
163 {
164   xbt_dict_cursor_t cursor2;
165   mc_frame_t frame;
166   char *key;
167   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
168     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
169   }
170 }
171
172 /** \brief Ignore a local variable in a scope
173  *
174  *  Ignore all instances of variables with a given name in
175  *  any (possibly inlined) subprogram with a given namespaced
176  *  name.
177  *
178  *  \param var_name        Name of the local variable (or parameter to ignore)
179  *  \param subprogram_name Name of the subprogram fo ignore (NULL for any)
180  *  \param subprogram      (possibly inlined) Subprogram of the scope
181  *  \param scope           Current scope
182  */
183 static void mc_ignore_local_variable_in_scope(const char *var_name,
184                                               const char *subprogram_name,
185                                               mc_frame_t subprogram,
186                                               mc_frame_t scope)
187 {
188   // Processing of direct variables:
189
190   // If the current subprogram matches the given name:
191   if (subprogram_name == nullptr ||
192       (!subprogram->name.empty()
193         && subprogram->name == subprogram_name)) {
194
195     // Try to find the variable and remove it:
196     int start = 0;
197     int end = scope->variables.size() - 1;
198
199     // Dichotomic search:
200     while (start <= end) {
201       int cursor = (start + end) / 2;
202       mc_variable_t current_var = &scope->variables[cursor];
203
204       int compare = strcmp(current_var->name.c_str(), var_name);
205       if (compare == 0) {
206         // Variable found, remove it:
207         scope->variables.erase(scope->variables.begin() + cursor);
208
209         // and start again:
210         start = 0;
211         end = scope->variables.size() - 1;
212       } else if (compare < 0) {
213         start = cursor + 1;
214       } else {
215         end = cursor - 1;
216       }
217     }
218
219   }
220   // And recursive processing in nested scopes:
221   for (simgrid::mc::Frame& nested_scope : scope->scopes) {
222     // The new scope may be an inlined subroutine, in this case we want to use its
223     // namespaced name in recursive calls:
224     mc_frame_t nested_subprogram =
225         nested_scope.tag ==
226         DW_TAG_inlined_subroutine ? &nested_scope : subprogram;
227
228     mc_ignore_local_variable_in_scope(var_name, subprogram_name,
229                                       nested_subprogram, &nested_scope);
230   }
231 }
232
233 extern xbt_dynar_t stacks_areas;
234
235 void MC_stack_area_add(stack_region_t stack_area)
236 {
237   if (stacks_areas == NULL)
238     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
239   xbt_dynar_push(stacks_areas, &stack_area);
240 }
241
242 }