-// ***** Helpers
-
-void* MC_object_base_address(mc_object_info_t info) {
- if(info->flags & MC_OBJECT_INFO_EXECUTABLE)
- return 0;
- void* result = info->start_exec;
- if(info->start_rw!=NULL && result > (void*) info->start_rw) result = info->start_rw;
- if(info->start_ro!=NULL && result > (void*) info->start_ro) result = info->start_ro;
- return result;
-}
-
-// ***** Functions index
-
-static int MC_compare_frame_index_items(mc_function_index_item_t a, mc_function_index_item_t b) {
- if(a->low_pc < b->low_pc)
- return -1;
- else if(a->low_pc == b->low_pc)
- return 0;
- else
- return 1;
-}
-
-static void MC_make_functions_index(mc_object_info_t info) {
- xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
-
- // Populate the array:
- dw_frame_t frame = NULL;
- xbt_dict_cursor_t cursor;
- char* key;
- xbt_dict_foreach(info->subprograms, cursor, key, frame) {
- if(frame->low_pc==NULL)
- continue;
- s_mc_function_index_item_t entry;
- entry.low_pc = frame->low_pc;
- entry.high_pc = frame->high_pc;
- entry.function = frame;
- xbt_dynar_push(index, &entry);
- }
-
- mc_function_index_item_t base = (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
-
- // Sort the array by low_pc:
- qsort(base,
- xbt_dynar_length(index),
- sizeof(s_mc_function_index_item_t),
- (int (*)(const void *, const void *))MC_compare_frame_index_items);
-
- info->functions_index = index;
-}
-
-mc_object_info_t MC_ip_find_object_info(void* ip) {
- size_t i;
- for(i=0; i!=mc_object_infos_size; ++i) {
- if(ip >= (void*)mc_object_infos[i]->start_exec && ip <= (void*)mc_object_infos[i]->end_exec) {
- return mc_object_infos[i];
- }
- }
- return NULL;
-}
-
-static dw_frame_t MC_find_function_by_ip_and_object(void* ip, mc_object_info_t info) {
- xbt_dynar_t dynar = info->functions_index;
- mc_function_index_item_t base = (mc_function_index_item_t) xbt_dynar_get_ptr(dynar, 0);
- int i = 0;
- int j = xbt_dynar_length(dynar) - 1;
- while(j>=i) {
- int k = i + ((j-i)/2);
- if(ip < base[k].low_pc) {
- j = k-1;
- } else if(ip >= base[k].high_pc) {
- i = k+1;
- } else {
- return base[k].function;
- }
- }
- return NULL;
-}
-
-dw_frame_t MC_find_function_by_ip(void* ip) {
- mc_object_info_t info = MC_ip_find_object_info(ip);
- if(info==NULL)
- return NULL;
- else
- return MC_find_function_by_ip_and_object(ip, info);
-}
-
-static void MC_post_process_variables(mc_object_info_t info) {
- unsigned cursor = 0;
- dw_variable_t variable = NULL;
- xbt_dynar_foreach(info->global_variables, cursor, variable) {
- if(variable->type_origin) {
- variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
- }
- }
-}
-
-static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope) {
-
- if(scope->tag == DW_TAG_inlined_subroutine) {
-
- // Attach correct namespaced name in inlined subroutine:
- char* key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
- dw_frame_t abstract_origin = xbt_dict_get_or_null(info->subprograms, key);
- xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
- xbt_free(key);
- scope->name = xbt_strdup(abstract_origin->name);
-
- }
-
- // Direct:
- unsigned cursor = 0;
- dw_variable_t variable = NULL;
- xbt_dynar_foreach(scope->variables, cursor, variable) {
- if(variable->type_origin) {
- variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
- }
- }
-
- // Recursive post-processing of nested-scopes:
- dw_frame_t nested_scope = NULL;
- xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
- mc_post_process_scope(info, nested_scope);
-
-}
-
-static void MC_post_process_functions(mc_object_info_t info) {
- xbt_dict_cursor_t cursor;
- char* key;
- dw_frame_t subprogram = NULL;
- xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
- mc_post_process_scope(info, subprogram);
- }
-}
-
-/** \brief Finds informations about a given shared object/executable */
-mc_object_info_t MC_find_object_info(memory_map_t maps, char* name, int executable) {
- mc_object_info_t result = MC_new_object_info();
- if(executable)
- result->flags |= MC_OBJECT_INFO_EXECUTABLE;
- result->file_name = xbt_strdup(name);
- MC_find_object_address(maps, result);
- MC_dwarf_get_variables(result);
- MC_post_process_types(result);
- MC_post_process_variables(result);
- MC_post_process_functions(result);
- MC_make_functions_index(result);
- return result;
-}
-
-/*************************************************************************/
-
-static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char* var, void *address){
-
- if(xbt_dynar_is_empty(variables))
- return 0;
-
- unsigned int cursor = 0;
- int start = 0;
- int end = xbt_dynar_length(variables) - 1;
- dw_variable_t var_test = NULL;
-
- while(start <= end){
- cursor = (start + end) / 2;
- var_test = (dw_variable_t)xbt_dynar_get_as(variables, cursor, dw_variable_t);
- if(strcmp(var_test->name, var) < 0){
- start = cursor + 1;
- }else if(strcmp(var_test->name, var) > 0){
- end = cursor - 1;
- }else{
- if(address){ /* global variable */
- if(var_test->address == address)
- return -1;
- if(var_test->address > address)
- end = cursor - 1;
- else
- start = cursor + 1;
- }else{ /* local variable */
- return -1;
- }
- }
- }
-
- if(strcmp(var_test->name, var) == 0){
- if(address && var_test->address < address)
- return cursor+1;
- else
- return cursor;
- }else if(strcmp(var_test->name, var) < 0)
- return cursor+1;
- else
- return cursor;
-
-}
-
-void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable) {
- int index = MC_dwarf_get_variable_index(info->global_variables, variable->name, variable->address);
- if (index != -1)
- xbt_dynar_insert_at(info->global_variables, index, &variable);
- // TODO, else ?
-}
-
-void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable) {
- xbt_assert(frame, "Frame is NULL");
- int index = MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
- if (index != -1)
- xbt_dynar_insert_at(frame->variables, index, &variable);
- // TODO, else ?
-}
-
-void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable) {
- if(variable->global)
- MC_dwarf_register_global_variable(info, variable);
- else if(frame==NULL)
- xbt_die("No frame for this local variable");
- else
- MC_dwarf_register_non_global_variable(info, frame, variable);
-}
-
-
-/******************************* Ignore mechanism *******************************/
-/*********************************************************************************/
-
-xbt_dynar_t mc_checkpoint_ignore;
-
-typedef struct s_mc_stack_ignore_variable{
- char *var_name;
- char *frame;
-}s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
-
-/**************************** Free functions ******************************/
-
-static void stack_ignore_variable_free(mc_stack_ignore_variable_t v){
- xbt_free(v->var_name);
- xbt_free(v->frame);
- xbt_free(v);
-}
-
-static void stack_ignore_variable_free_voidp(void *v){
- stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
-}
-
-void heap_ignore_region_free(mc_heap_ignore_region_t r){
- xbt_free(r);
-}
-
-void heap_ignore_region_free_voidp(void *r){
- heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
-}
-
-static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r){
- xbt_free(r);
-}
-
-static void checkpoint_ignore_region_free_voidp(void *r){
- checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
-}
-
-/***********************************************************************/
-
-void MC_ignore_heap(void *address, size_t size){
-
- int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
-
- MC_SET_RAW_MEM;
-
- mc_heap_ignore_region_t region = NULL;
- region = xbt_new0(s_mc_heap_ignore_region_t, 1);
- region->address = address;
- region->size = size;
-
- region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
-
- if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
- region->fragment = -1;
- ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_block.ignore++;
- }else{
- region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
- ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
- }
-
- if(mc_heap_comparison_ignore == NULL){
- mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
- xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
- return;
- }
-
- unsigned int cursor = 0;
- mc_heap_ignore_region_t current_region = NULL;
- int start = 0;
- int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
-
- while(start <= end){
- cursor = (start + end) / 2;
- current_region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
- if(current_region->address == address){
- heap_ignore_region_free(region);
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
- return;
- }else if(current_region->address < address){
- start = cursor + 1;
- }else{
- end = cursor - 1;
- }
- }
-
- if(current_region->address < address)
- xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
- else
- xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
-
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
-}
-
-void MC_remove_ignore_heap(void *address, size_t size){
-
- int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
-
- MC_SET_RAW_MEM;
-
- unsigned int cursor = 0;
- int start = 0;
- int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
- mc_heap_ignore_region_t region;
- int ignore_found = 0;
-
- while(start <= end){
- cursor = (start + end) / 2;
- region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
- if(region->address == address){
- ignore_found = 1;
- break;
- }else if(region->address < address){
- start = cursor + 1;
- }else{
- if((char * )region->address <= ((char *)address + size)){
- ignore_found = 1;
- break;
- }else{
- end = cursor - 1;
- }
- }
- }
-
- if(ignore_found == 1){
- xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
- MC_remove_ignore_heap(address, size);
- }
-
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
-
-}
-
-void MC_ignore_global_variable(const char *name){
-
- int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
-
- MC_SET_RAW_MEM;
-
- xbt_assert(mc_libsimgrid_info, "MC subsystem not initialized");
-
- unsigned int cursor = 0;
- dw_variable_t current_var;
- int start = 0;
- int end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
-
- while(start <= end){
- cursor = (start + end) /2;
- current_var = (dw_variable_t)xbt_dynar_get_as(mc_libsimgrid_info->global_variables, cursor, dw_variable_t);
- if(strcmp(current_var->name, name) == 0){
- xbt_dynar_remove_at(mc_libsimgrid_info->global_variables, cursor, NULL);
- start = 0;
- end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
- }else if(strcmp(current_var->name, name) < 0){
- start = cursor + 1;
- }else{
- end = cursor - 1;
- }
- }
-
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
-}
-
-/** \brief Ignore a local variable in a scope
- *
- * Ignore all instances of variables with a given name in
- * any (possibly inlined) subprogram with a given namespaced
- * name.
- *
- * \param var_name Name of the local variable (or parameter to ignore)
- * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
- * \param subprogram (possibly inlined) Subprogram of the scope
- * \param scope Current scope
- */
-static void mc_ignore_local_variable_in_scope(
- const char *var_name, const char *subprogram_name,
- dw_frame_t subprogram, dw_frame_t scope) {
- // Processing of direct variables:
-
- // If the current subprogram matche the given name:
- if(subprogram_name==NULL || strcmp(subprogram_name, subprogram->name)==0) {
-
- // Try to find the variable and remove it:
- int start = 0;
- int end = xbt_dynar_length(scope->variables) - 1;
-
- // Dichotomic search:
- while(start <= end){
- int cursor = (start + end) / 2;
- dw_variable_t current_var = (dw_variable_t)xbt_dynar_get_as(scope->variables, cursor, dw_variable_t);
-
- int compare = strcmp(current_var->name, var_name);
- if(compare == 0){
- // Variable found, remove it:
- xbt_dynar_remove_at(scope->variables, cursor, NULL);
-
- // and start again:
- start = 0;
- end = xbt_dynar_length(scope->variables) - 1;
- }else if(compare < 0){
- start = cursor + 1;
- }else{
- end = cursor - 1;
- }
- }
-
- }
-
- // And recursive processing in nested scopes:
- unsigned cursor = 0;
- dw_frame_t nested_scope = NULL;
- xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
- // The new scope may be an inlined subroutine, in this case we want to use its
- // namespaced name in recursive calls:
- dw_frame_t nested_subprogram = nested_scope->tag == DW_TAG_inlined_subroutine ? nested_scope : subprogram;
-
- mc_ignore_local_variable_in_scope(var_name, subprogram_name, nested_subprogram, nested_scope);
- }
-}
-
-static void MC_ignore_local_variable_in_object(const char *var_name, const char *subprogram_name, mc_object_info_t info) {
- xbt_dict_cursor_t cursor2;
- dw_frame_t frame;
- char* key;
- xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
- mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
- }
-}
-
-void MC_ignore_local_variable(const char *var_name, const char *frame_name){
-
- int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
-
- if(strcmp(frame_name, "*") == 0)
- frame_name = NULL;
-
- MC_SET_RAW_MEM;
-
- MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
- if(frame_name!=NULL)
- MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
-
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
-
-}
-
-void MC_new_stack_area(void *stack, char *name, void* context, size_t size){
-
- int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
-
- MC_SET_RAW_MEM;
-
- if(stacks_areas == NULL)
- stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
-
- stack_region_t region = NULL;
- region = xbt_new0(s_stack_region_t, 1);
- region->address = stack;
- region->process_name = strdup(name);
- region->context = context;
- region->size = size;
- region->block = ((char*)stack - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
- xbt_dynar_push(stacks_areas, ®ion);
-
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
-}
-
-void MC_ignore(void *addr, size_t size){
-
- int raw_mem_set= (mmalloc_get_current_heap() == raw_heap);
-
- MC_SET_RAW_MEM;
-
- if(mc_checkpoint_ignore == NULL)
- mc_checkpoint_ignore = xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t), checkpoint_ignore_region_free_voidp);
-
- mc_checkpoint_ignore_region_t region = xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
- region->addr = addr;
- region->size = size;
-
- if(xbt_dynar_is_empty(mc_checkpoint_ignore)){
- xbt_dynar_push(mc_checkpoint_ignore, ®ion);
- }else{
-
- unsigned int cursor = 0;
- int start = 0;
- int end = xbt_dynar_length(mc_checkpoint_ignore) -1;
- mc_checkpoint_ignore_region_t current_region = NULL;
-
- while(start <= end){
- cursor = (start + end) / 2;
- current_region = (mc_checkpoint_ignore_region_t)xbt_dynar_get_as(mc_checkpoint_ignore, cursor, mc_checkpoint_ignore_region_t);
- if(current_region->addr == addr){
- if(current_region->size == size){
- checkpoint_ignore_region_free(region);
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
- return;
- }else if(current_region->size < size){
- start = cursor + 1;
- }else{
- end = cursor - 1;
- }
- }else if(current_region->addr < addr){
- start = cursor + 1;
- }else{
- end = cursor - 1;
- }
- }
-
- if(current_region->addr == addr){
- if(current_region->size < size){
- xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
- }else{
- xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
- }
- }else if(current_region->addr < addr){
- xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
- }else{
- xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
- }
- }
-
- if(!raw_mem_set)
- MC_UNSET_RAW_MEM;
-}