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)
67 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
71 mc_heap_ignore_region_t region = NULL;
72 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
73 region->address = address;
78 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
80 if (std_heap->heapinfo[region->block].type == 0) {
81 region->fragment = -1;
82 std_heap->heapinfo[region->block].busy_block.ignore++;
85 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
86 heapinfo[region->block].type;
87 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
90 if (mc_heap_comparison_ignore == NULL) {
91 mc_heap_comparison_ignore =
92 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
93 heap_ignore_region_free_voidp);
94 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
100 unsigned int cursor = 0;
101 mc_heap_ignore_region_t current_region = NULL;
103 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
105 while (start <= end) {
106 cursor = (start + end) / 2;
108 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
110 mc_heap_ignore_region_t);
111 if (current_region->address == address) {
112 heap_ignore_region_free(region);
116 } else if (current_region->address < address) {
123 if (current_region->address < address)
124 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
126 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
132 void MC_remove_ignore_heap(void *address, size_t size)
135 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
139 unsigned int cursor = 0;
141 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
142 mc_heap_ignore_region_t region;
143 int ignore_found = 0;
145 while (start <= end) {
146 cursor = (start + end) / 2;
148 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
150 mc_heap_ignore_region_t);
151 if (region->address == address) {
154 } else if (region->address < address) {
157 if ((char *) region->address <= ((char *) address + size)) {
166 if (ignore_found == 1) {
167 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
168 MC_remove_ignore_heap(address, size);
176 void MC_ignore_global_variable(const char *name)
179 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
183 xbt_assert(mc_libsimgrid_info, "MC subsystem not initialized");
185 unsigned int cursor = 0;
186 dw_variable_t current_var;
188 int end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
190 while (start <= end) {
191 cursor = (start + end) / 2;
193 (dw_variable_t) xbt_dynar_get_as(mc_libsimgrid_info->global_variables,
194 cursor, dw_variable_t);
195 if (strcmp(current_var->name, name) == 0) {
196 xbt_dynar_remove_at(mc_libsimgrid_info->global_variables, cursor, NULL);
198 end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
199 } else if (strcmp(current_var->name, name) < 0) {
210 /** \brief Ignore a local variable in a scope
212 * Ignore all instances of variables with a given name in
213 * any (possibly inlined) subprogram with a given namespaced
216 * \param var_name Name of the local variable (or parameter to ignore)
217 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
218 * \param subprogram (possibly inlined) Subprogram of the scope
219 * \param scope Current scope
221 static void mc_ignore_local_variable_in_scope(const char *var_name,
222 const char *subprogram_name,
223 dw_frame_t subprogram,
226 // Processing of direct variables:
228 // If the current subprogram matche the given name:
229 if (!subprogram_name ||
230 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
232 // Try to find the variable and remove it:
234 int end = xbt_dynar_length(scope->variables) - 1;
236 // Dichotomic search:
237 while (start <= end) {
238 int cursor = (start + end) / 2;
239 dw_variable_t current_var =
240 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
243 int compare = strcmp(current_var->name, var_name);
245 // Variable found, remove it:
246 xbt_dynar_remove_at(scope->variables, cursor, NULL);
250 end = xbt_dynar_length(scope->variables) - 1;
251 } else if (compare < 0) {
259 // And recursive processing in nested scopes:
261 dw_frame_t nested_scope = NULL;
262 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
263 // The new scope may be an inlined subroutine, in this case we want to use its
264 // namespaced name in recursive calls:
265 dw_frame_t nested_subprogram =
267 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
269 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
270 nested_subprogram, nested_scope);
274 static void MC_ignore_local_variable_in_object(const char *var_name,
275 const char *subprogram_name,
276 mc_object_info_t info)
278 xbt_dict_cursor_t cursor2;
281 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
282 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
286 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
289 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
291 if (strcmp(frame_name, "*") == 0)
296 MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
297 if (frame_name != NULL)
298 MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
305 /** @brief Register a stack in the model checker
307 * The stacks are allocated in the heap. The MC handle them especially
308 * when we analyse/compare the content of theap so it must be told where
309 * they are with this function.
312 * @param process Process owning the stack
314 * @param size Size of the stack
316 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
319 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
323 if (stacks_areas == NULL)
324 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
326 stack_region_t region = NULL;
327 region = xbt_new0(s_stack_region_t, 1);
328 region->address = stack;
329 region->process_name = process && process->name ? strdup(process->name) : NULL;
330 region->context = context;
334 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
336 if (smpi_privatize_global_variables && process) {
337 region->process_index = smpi_process_index_of_smx_process(process);
340 region->process_index = -1;
342 xbt_dynar_push(stacks_areas, ®ion);
348 void MC_ignore(void *addr, size_t size)
351 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
355 if (mc_checkpoint_ignore == NULL)
356 mc_checkpoint_ignore =
357 xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
358 checkpoint_ignore_region_free_voidp);
360 mc_checkpoint_ignore_region_t region =
361 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
365 if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
366 xbt_dynar_push(mc_checkpoint_ignore, ®ion);
369 unsigned int cursor = 0;
371 int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
372 mc_checkpoint_ignore_region_t current_region = NULL;
374 while (start <= end) {
375 cursor = (start + end) / 2;
377 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
379 mc_checkpoint_ignore_region_t);
380 if (current_region->addr == addr) {
381 if (current_region->size == size) {
382 checkpoint_ignore_region_free(region);
386 } else if (current_region->size < size) {
391 } else if (current_region->addr < addr) {
398 if (current_region->addr == addr) {
399 if (current_region->size < size) {
400 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
402 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
404 } else if (current_region->addr < addr) {
405 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
407 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);