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_private.h"
9 #include "smpi/private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
12 "Logging specific to MC ignore mechanism");
15 /**************************** Global variables ******************************/
16 xbt_dynar_t mc_checkpoint_ignore;
17 extern xbt_dynar_t mc_heap_comparison_ignore;
18 extern xbt_dynar_t stacks_areas;
20 /**************************** Structures ******************************/
21 typedef struct s_mc_stack_ignore_variable {
24 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
26 /**************************** Free functions ******************************/
28 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
30 xbt_free(v->var_name);
35 static void stack_ignore_variable_free_voidp(void *v)
37 stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
40 void heap_ignore_region_free(mc_heap_ignore_region_t r)
45 void heap_ignore_region_free_voidp(void *r)
47 heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
50 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
55 static void checkpoint_ignore_region_free_voidp(void *r)
57 checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
60 /***********************************************************************/
62 void MC_ignore_heap(void *address, size_t size)
65 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
69 mc_heap_ignore_region_t region = NULL;
70 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
71 region->address = address;
76 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
78 if (std_heap->heapinfo[region->block].type == 0) {
79 region->fragment = -1;
80 std_heap->heapinfo[region->block].busy_block.ignore++;
83 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
84 heapinfo[region->block].type;
85 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
88 if (mc_heap_comparison_ignore == NULL) {
89 mc_heap_comparison_ignore =
90 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
91 heap_ignore_region_free_voidp);
92 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
98 unsigned int cursor = 0;
99 mc_heap_ignore_region_t current_region = NULL;
101 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
103 while (start <= end) {
104 cursor = (start + end) / 2;
106 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
108 mc_heap_ignore_region_t);
109 if (current_region->address == address) {
110 heap_ignore_region_free(region);
114 } else if (current_region->address < address) {
121 if (current_region->address < address)
122 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
124 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
130 void MC_remove_ignore_heap(void *address, size_t size)
133 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
137 unsigned int cursor = 0;
139 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
140 mc_heap_ignore_region_t region;
141 int ignore_found = 0;
143 while (start <= end) {
144 cursor = (start + end) / 2;
146 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
148 mc_heap_ignore_region_t);
149 if (region->address == address) {
152 } else if (region->address < address) {
155 if ((char *) region->address <= ((char *) address + size)) {
164 if (ignore_found == 1) {
165 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
166 MC_remove_ignore_heap(address, size);
174 void MC_ignore_global_variable(const char *name)
177 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
181 xbt_assert(mc_libsimgrid_info, "MC subsystem not initialized");
183 unsigned int cursor = 0;
184 dw_variable_t current_var;
186 int end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
188 while (start <= end) {
189 cursor = (start + end) / 2;
191 (dw_variable_t) xbt_dynar_get_as(mc_libsimgrid_info->global_variables,
192 cursor, dw_variable_t);
193 if (strcmp(current_var->name, name) == 0) {
194 xbt_dynar_remove_at(mc_libsimgrid_info->global_variables, cursor, NULL);
196 end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
197 } else if (strcmp(current_var->name, name) < 0) {
208 /** \brief Ignore a local variable in a scope
210 * Ignore all instances of variables with a given name in
211 * any (possibly inlined) subprogram with a given namespaced
214 * \param var_name Name of the local variable (or parameter to ignore)
215 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
216 * \param subprogram (possibly inlined) Subprogram of the scope
217 * \param scope Current scope
219 static void mc_ignore_local_variable_in_scope(const char *var_name,
220 const char *subprogram_name,
221 dw_frame_t subprogram,
224 // Processing of direct variables:
226 // If the current subprogram matche the given name:
227 if (!subprogram_name ||
228 subprogram->name && strcmp(subprogram_name, subprogram->name) == 0) {
230 // Try to find the variable and remove it:
232 int end = xbt_dynar_length(scope->variables) - 1;
234 // Dichotomic search:
235 while (start <= end) {
236 int cursor = (start + end) / 2;
237 dw_variable_t current_var =
238 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
241 int compare = strcmp(current_var->name, var_name);
243 // Variable found, remove it:
244 xbt_dynar_remove_at(scope->variables, cursor, NULL);
248 end = xbt_dynar_length(scope->variables) - 1;
249 } else if (compare < 0) {
257 // And recursive processing in nested scopes:
259 dw_frame_t nested_scope = NULL;
260 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
261 // The new scope may be an inlined subroutine, in this case we want to use its
262 // namespaced name in recursive calls:
263 dw_frame_t nested_subprogram =
265 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
267 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
268 nested_subprogram, nested_scope);
272 static void MC_ignore_local_variable_in_object(const char *var_name,
273 const char *subprogram_name,
274 mc_object_info_t info)
276 xbt_dict_cursor_t cursor2;
279 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
280 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
284 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
287 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
289 if (strcmp(frame_name, "*") == 0)
294 MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
295 if (frame_name != NULL)
296 MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
303 /** @brief Register a stack in the model checker
305 * The stacks are allocated in the heap. The MC handle them especially
306 * when we analyse/compare the content of theap so it must be told where
307 * they are with this function.
310 * @param process Process owning the stack
312 * @param size Size of the stack
314 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
317 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
321 if (stacks_areas == NULL)
322 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
324 stack_region_t region = NULL;
325 region = xbt_new0(s_stack_region_t, 1);
326 region->address = stack;
327 region->process_name = process && process->name ? strdup(process->name) : NULL;
328 region->context = context;
332 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
334 if (smpi_privatize_global_variables && process) {
335 region->process_index = smpi_process_index_of_smx_process(process);
338 region->process_index = -1;
340 xbt_dynar_push(stacks_areas, ®ion);
346 void MC_ignore(void *addr, size_t size)
349 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
353 if (mc_checkpoint_ignore == NULL)
354 mc_checkpoint_ignore =
355 xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
356 checkpoint_ignore_region_free_voidp);
358 mc_checkpoint_ignore_region_t region =
359 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
363 if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
364 xbt_dynar_push(mc_checkpoint_ignore, ®ion);
367 unsigned int cursor = 0;
369 int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
370 mc_checkpoint_ignore_region_t current_region = NULL;
372 while (start <= end) {
373 cursor = (start + end) / 2;
375 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
377 mc_checkpoint_ignore_region_t);
378 if (current_region->addr == addr) {
379 if (current_region->size == size) {
380 checkpoint_ignore_region_free(region);
384 } else if (current_region->size < size) {
389 } else if (current_region->addr < addr) {
396 if (current_region->addr == addr) {
397 if (current_region->size < size) {
398 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
400 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
402 } else if (current_region->addr < addr) {
403 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
405 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);