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)
178 mc_process_t process = &mc_model_checker->process;
179 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
183 xbt_assert(process->object_infos, "MC subsystem not initialized");
185 size_t n = process->object_infos_size;
186 for (size_t i=0; i!=n; ++i) {
187 mc_object_info_t info = process->object_infos[i];
191 int end = xbt_dynar_length(info->global_variables) - 1;
192 while (start <= end) {
193 unsigned int cursor = (start + end) / 2;
194 dw_variable_t current_var =
195 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
196 cursor, dw_variable_t);
197 if (strcmp(current_var->name, name) == 0) {
198 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
200 end = xbt_dynar_length(info->global_variables) - 1;
201 } else if (strcmp(current_var->name, name) < 0) {
213 /** \brief Ignore a local variable in a scope
215 * Ignore all instances of variables with a given name in
216 * any (possibly inlined) subprogram with a given namespaced
219 * \param var_name Name of the local variable (or parameter to ignore)
220 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
221 * \param subprogram (possibly inlined) Subprogram of the scope
222 * \param scope Current scope
224 static void mc_ignore_local_variable_in_scope(const char *var_name,
225 const char *subprogram_name,
226 dw_frame_t subprogram,
229 // Processing of direct variables:
231 // If the current subprogram matche the given name:
232 if (!subprogram_name ||
233 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
235 // Try to find the variable and remove it:
237 int end = xbt_dynar_length(scope->variables) - 1;
239 // Dichotomic search:
240 while (start <= end) {
241 int cursor = (start + end) / 2;
242 dw_variable_t current_var =
243 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
246 int compare = strcmp(current_var->name, var_name);
248 // Variable found, remove it:
249 xbt_dynar_remove_at(scope->variables, cursor, NULL);
253 end = xbt_dynar_length(scope->variables) - 1;
254 } else if (compare < 0) {
262 // And recursive processing in nested scopes:
264 dw_frame_t nested_scope = NULL;
265 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
266 // The new scope may be an inlined subroutine, in this case we want to use its
267 // namespaced name in recursive calls:
268 dw_frame_t nested_subprogram =
270 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
272 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
273 nested_subprogram, nested_scope);
277 static void MC_ignore_local_variable_in_object(const char *var_name,
278 const char *subprogram_name,
279 mc_object_info_t info)
281 xbt_dict_cursor_t cursor2;
284 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
285 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
289 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
291 mc_process_t process = &mc_model_checker->process;
294 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
296 if (strcmp(frame_name, "*") == 0)
301 size_t n = process->object_infos_size;
303 for (i=0; i!=n; ++i) {
304 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
312 /** @brief Register a stack in the model checker
314 * The stacks are allocated in the heap. The MC handle them especially
315 * when we analyse/compare the content of theap so it must be told where
316 * they are with this function.
319 * @param process Process owning the stack
321 * @param size Size of the stack
323 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
326 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
330 if (stacks_areas == NULL)
331 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
333 stack_region_t region = NULL;
334 region = xbt_new0(s_stack_region_t, 1);
335 region->address = stack;
336 region->process_name = process && process->name ? strdup(process->name) : NULL;
337 region->context = context;
341 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
343 if (smpi_privatize_global_variables && process) {
344 region->process_index = smpi_process_index_of_smx_process(process);
347 region->process_index = -1;
349 xbt_dynar_push(stacks_areas, ®ion);
355 void MC_ignore(void *addr, size_t size)
358 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
362 if (mc_checkpoint_ignore == NULL)
363 mc_checkpoint_ignore =
364 xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
365 checkpoint_ignore_region_free_voidp);
367 mc_checkpoint_ignore_region_t region =
368 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
372 if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
373 xbt_dynar_push(mc_checkpoint_ignore, ®ion);
376 unsigned int cursor = 0;
378 int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
379 mc_checkpoint_ignore_region_t current_region = NULL;
381 while (start <= end) {
382 cursor = (start + end) / 2;
384 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
386 mc_checkpoint_ignore_region_t);
387 if (current_region->addr == addr) {
388 if (current_region->size == size) {
389 checkpoint_ignore_region_free(region);
393 } else if (current_region->size < size) {
398 } else if (current_region->addr < addr) {
405 if (current_region->addr == addr) {
406 if (current_region->size < size) {
407 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
409 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
411 } else if (current_region->addr < addr) {
412 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
414 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);