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 *) ((xbt_mheap_t) std_heap)->heapbase) / BLOCKSIZE + 1;
78 if (((xbt_mheap_t) std_heap)->heapinfo[region->block].type == 0) {
79 region->fragment = -1;
80 ((xbt_mheap_t) std_heap)->heapinfo[region->block].busy_block.ignore++;
83 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> ((xbt_mheap_t)
85 heapinfo[region->block].type;
86 ((xbt_mheap_t) std_heap)->heapinfo[region->block].busy_frag.ignore[region->
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 == NULL || strcmp(subprogram_name, subprogram->name) == 0) {
231 // Try to find the variable and remove it:
233 int end = xbt_dynar_length(scope->variables) - 1;
235 // Dichotomic search:
236 while (start <= end) {
237 int cursor = (start + end) / 2;
238 dw_variable_t current_var =
239 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
242 int compare = strcmp(current_var->name, var_name);
244 // Variable found, remove it:
245 xbt_dynar_remove_at(scope->variables, cursor, NULL);
249 end = xbt_dynar_length(scope->variables) - 1;
250 } else if (compare < 0) {
258 // And recursive processing in nested scopes:
260 dw_frame_t nested_scope = NULL;
261 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
262 // The new scope may be an inlined subroutine, in this case we want to use its
263 // namespaced name in recursive calls:
264 dw_frame_t nested_subprogram =
266 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
268 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
269 nested_subprogram, nested_scope);
273 static void MC_ignore_local_variable_in_object(const char *var_name,
274 const char *subprogram_name,
275 mc_object_info_t info)
277 xbt_dict_cursor_t cursor2;
280 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
281 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
285 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
288 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
290 if (strcmp(frame_name, "*") == 0)
295 MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
296 if (frame_name != NULL)
297 MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
304 /** @brief Register a stack in the model checker
306 * The stacks are allocated in the heap. The MC handle them especially
307 * when we analyse/compare the content of theap so it must be told where
308 * they are with this function.
311 * @param process Process owning the stack
313 * @param size Size of the stack
315 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
318 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
322 if (stacks_areas == NULL)
323 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
325 stack_region_t region = NULL;
326 region = xbt_new0(s_stack_region_t, 1);
327 region->address = stack;
328 region->process_name = process && process->name ? strdup(process->name) : NULL;
329 region->context = context;
333 (char *) ((xbt_mheap_t) std_heap)->heapbase) / BLOCKSIZE + 1;
335 if (smpi_privatize_global_variables && process) {
336 region->process_index = smpi_process_index_of_smx_process(process);
339 region->process_index = -1;
341 xbt_dynar_push(stacks_areas, ®ion);
347 void MC_ignore(void *addr, size_t size)
350 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
354 if (mc_checkpoint_ignore == NULL)
355 mc_checkpoint_ignore =
356 xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
357 checkpoint_ignore_region_free_voidp);
359 mc_checkpoint_ignore_region_t region =
360 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
364 if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
365 xbt_dynar_push(mc_checkpoint_ignore, ®ion);
368 unsigned int cursor = 0;
370 int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
371 mc_checkpoint_ignore_region_t current_region = NULL;
373 while (start <= end) {
374 cursor = (start + end) / 2;
376 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
378 mc_checkpoint_ignore_region_t);
379 if (current_region->addr == addr) {
380 if (current_region->size == size) {
381 checkpoint_ignore_region_free(region);
385 } else if (current_region->size < size) {
390 } else if (current_region->addr < addr) {
397 if (current_region->addr == addr) {
398 if (current_region->size < size) {
399 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
401 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
403 } else if (current_region->addr < addr) {
404 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
406 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);