1 /* Copyright (c) 2008-2014. 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. */
7 #include "internal_config.h"
8 #include "mc_object_info.h"
9 #include "mc_private.h"
10 #include "smpi/private.h"
11 #include "mc/mc_snapshot.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
14 "Logging specific to MC ignore mechanism");
17 /**************************** Global variables ******************************/
18 xbt_dynar_t mc_checkpoint_ignore;
19 extern xbt_dynar_t mc_heap_comparison_ignore;
20 extern xbt_dynar_t stacks_areas;
22 /**************************** Structures ******************************/
23 typedef struct s_mc_stack_ignore_variable {
26 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
28 /**************************** Free functions ******************************/
30 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
32 xbt_free(v->var_name);
37 static void stack_ignore_variable_free_voidp(void *v)
39 stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
42 void heap_ignore_region_free(mc_heap_ignore_region_t r)
47 void heap_ignore_region_free_voidp(void *r)
49 heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
52 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
57 static void checkpoint_ignore_region_free_voidp(void *r)
59 checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
62 /***********************************************************************/
64 void MC_ignore_heap(void *address, size_t size)
69 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
73 mc_heap_ignore_region_t region = NULL;
74 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
75 region->address = address;
80 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
82 if (std_heap->heapinfo[region->block].type == 0) {
83 region->fragment = -1;
84 std_heap->heapinfo[region->block].busy_block.ignore++;
87 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
88 heapinfo[region->block].type;
89 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
92 if (mc_heap_comparison_ignore == NULL) {
93 mc_heap_comparison_ignore =
94 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
95 heap_ignore_region_free_voidp);
96 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
102 unsigned int cursor = 0;
103 mc_heap_ignore_region_t current_region = NULL;
105 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
107 while (start <= end) {
108 cursor = (start + end) / 2;
110 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
112 mc_heap_ignore_region_t);
113 if (current_region->address == address) {
114 heap_ignore_region_free(region);
118 } else if (current_region->address < address) {
125 if (current_region->address < address)
126 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
128 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
134 void MC_remove_ignore_heap(void *address, size_t size)
137 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
141 unsigned int cursor = 0;
143 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
144 mc_heap_ignore_region_t region;
145 int ignore_found = 0;
147 while (start <= end) {
148 cursor = (start + end) / 2;
150 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
152 mc_heap_ignore_region_t);
153 if (region->address == address) {
156 } else if (region->address < address) {
159 if ((char *) region->address <= ((char *) address + size)) {
168 if (ignore_found == 1) {
169 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
170 MC_remove_ignore_heap(address, size);
178 void MC_ignore_global_variable(const char *name)
180 mc_process_t process = &mc_model_checker->process;
181 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
185 xbt_assert(process->object_infos, "MC subsystem not initialized");
187 size_t n = process->object_infos_size;
188 for (size_t i=0; i!=n; ++i) {
189 mc_object_info_t info = process->object_infos[i];
193 int end = xbt_dynar_length(info->global_variables) - 1;
194 while (start <= end) {
195 unsigned int cursor = (start + end) / 2;
196 dw_variable_t current_var =
197 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
198 cursor, dw_variable_t);
199 if (strcmp(current_var->name, name) == 0) {
200 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
202 end = xbt_dynar_length(info->global_variables) - 1;
203 } else if (strcmp(current_var->name, name) < 0) {
215 /** \brief Ignore a local variable in a scope
217 * Ignore all instances of variables with a given name in
218 * any (possibly inlined) subprogram with a given namespaced
221 * \param var_name Name of the local variable (or parameter to ignore)
222 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
223 * \param subprogram (possibly inlined) Subprogram of the scope
224 * \param scope Current scope
226 static void mc_ignore_local_variable_in_scope(const char *var_name,
227 const char *subprogram_name,
228 dw_frame_t subprogram,
231 // Processing of direct variables:
233 // If the current subprogram matche the given name:
234 if (!subprogram_name ||
235 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
237 // Try to find the variable and remove it:
239 int end = xbt_dynar_length(scope->variables) - 1;
241 // Dichotomic search:
242 while (start <= end) {
243 int cursor = (start + end) / 2;
244 dw_variable_t current_var =
245 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
248 int compare = strcmp(current_var->name, var_name);
250 // Variable found, remove it:
251 xbt_dynar_remove_at(scope->variables, cursor, NULL);
255 end = xbt_dynar_length(scope->variables) - 1;
256 } else if (compare < 0) {
264 // And recursive processing in nested scopes:
266 dw_frame_t nested_scope = NULL;
267 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
268 // The new scope may be an inlined subroutine, in this case we want to use its
269 // namespaced name in recursive calls:
270 dw_frame_t nested_subprogram =
272 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
274 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
275 nested_subprogram, nested_scope);
279 static void MC_ignore_local_variable_in_object(const char *var_name,
280 const char *subprogram_name,
281 mc_object_info_t info)
283 xbt_dict_cursor_t cursor2;
286 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
287 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
291 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
293 mc_process_t process = &mc_model_checker->process;
296 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
298 if (strcmp(frame_name, "*") == 0)
303 size_t n = process->object_infos_size;
305 for (i=0; i!=n; ++i) {
306 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
314 /** @brief Register a stack in the model checker
316 * The stacks are allocated in the heap. The MC handle them especially
317 * when we analyse/compare the content of theap so it must be told where
318 * they are with this function.
321 * @param process Process owning the stack
323 * @param size Size of the stack
325 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
328 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
332 if (stacks_areas == NULL)
333 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
335 stack_region_t region = NULL;
336 region = xbt_new0(s_stack_region_t, 1);
337 region->address = stack;
338 region->process_name = process && process->name ? strdup(process->name) : NULL;
339 region->context = context;
343 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
345 if (smpi_privatize_global_variables && process) {
346 region->process_index = smpi_process_index_of_smx_process(process);
349 region->process_index = -1;
351 xbt_dynar_push(stacks_areas, ®ion);
357 void MC_ignore(void *addr, size_t size)
360 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
364 if (mc_checkpoint_ignore == NULL)
365 mc_checkpoint_ignore =
366 xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
367 checkpoint_ignore_region_free_voidp);
369 mc_checkpoint_ignore_region_t region =
370 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
374 if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
375 xbt_dynar_push(mc_checkpoint_ignore, ®ion);
378 unsigned int cursor = 0;
380 int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
381 mc_checkpoint_ignore_region_t current_region = NULL;
383 while (start <= end) {
384 cursor = (start + end) / 2;
386 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
388 mc_checkpoint_ignore_region_t);
389 if (current_region->addr == addr) {
390 if (current_region->size == size) {
391 checkpoint_ignore_region_free(region);
395 } else if (current_region->size < size) {
400 } else if (current_region->addr < addr) {
407 if (current_region->addr == addr) {
408 if (current_region->size < size) {
409 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
411 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
413 } else if (current_region->addr < addr) {
414 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
416 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);