From: Gabriel Corona Date: Mon, 20 Jul 2015 12:18:28 +0000 (+0200) Subject: [mc] Make Frame::variables, Frame::scopes and ObjectInformation::global_variables... X-Git-Tag: v3_12~438^2~32 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/440ced834dc98975c18cf7130ae1d6d9018438db [mc] Make Frame::variables, Frame::scopes and ObjectInformation::global_variables std::vectors --- diff --git a/src/mc/mc_checkpoint.cpp b/src/mc/mc_checkpoint.cpp index 13c9397b9a..98f3ec78c4 100644 --- a/src/mc/mc_checkpoint.cpp +++ b/src/mc/mc_checkpoint.cpp @@ -280,11 +280,10 @@ static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame, if (ip < scope->low_pc || ip >= scope->high_pc) return; - unsigned cursor = 0; - mc_variable_t current_variable; - xbt_dynar_foreach(scope->variables, cursor, current_variable) { + for(simgrid::mc::Variable& current_variable : + scope->variables) { - if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip)) + if (!mc_valid_variable(¤t_variable, scope, (void *) stack_frame->ip)) continue; int region_type; @@ -297,18 +296,18 @@ static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame, s_local_variable_t new_var; new_var.subprogram = stack_frame->frame; new_var.ip = stack_frame->ip; - new_var.name = current_variable->name; - new_var.type = current_variable->type; + new_var.name = current_variable.name; + new_var.type = current_variable.type; new_var.region = region_type; new_var.address = nullptr; - if (current_variable->address != NULL) { - new_var.address = current_variable->address; - } else if (!current_variable->location_list.empty()) { + if (current_variable.address != NULL) { + new_var.address = current_variable.address; + } else if (!current_variable.location_list.empty()) { s_mc_location_t location; mc_dwarf_resolve_locations( - &location, ¤t_variable->location_list, - current_variable->object_info, + &location, ¤t_variable.location_list, + current_variable.object_info, &(stack_frame->unw_cursor), (void *) stack_frame->frame_base, &mc_model_checker->process(), process_index); @@ -330,10 +329,9 @@ static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame, } // Recursive processing of nested scopes: - mc_frame_t nested_scope = nullptr; - xbt_dynar_foreach(scope->scopes, cursor, nested_scope) { - mc_fill_local_variables_values(stack_frame, nested_scope, process_index, result); - } + for(simgrid::mc::Frame& nested_scope : scope->scopes) + mc_fill_local_variables_values( + stack_frame, &nested_scope, process_index, result); } static std::vector MC_get_local_variables_values( diff --git a/src/mc/mc_compare.cpp b/src/mc/mc_compare.cpp index 0314dfca71..837b416293 100644 --- a/src/mc/mc_compare.cpp +++ b/src/mc/mc_compare.cpp @@ -294,33 +294,28 @@ static int compare_global_variables(mc_object_info_t object_info, struct mc_compare_state state; - xbt_dynar_t variables; - int res; - unsigned int cursor = 0; - mc_variable_t current_var; + std::vector& variables = object_info->global_variables; - variables = object_info->global_variables; - - xbt_dynar_foreach(variables, cursor, current_var) { + for (simgrid::mc::Variable& current_var : variables) { // If the variable is not in this object, skip it: // We do not expect to find a pointer to something which is not reachable // by the global variables. - if ((char *) current_var->address < (char *) object_info->start_rw - || (char *) current_var->address > (char *) object_info->end_rw) + if ((char *) current_var.address < (char *) object_info->start_rw + || (char *) current_var.address > (char *) object_info->end_rw) continue; - mc_type_t bvariable_type = current_var->type; - res = + mc_type_t bvariable_type = current_var.type; + int res = compare_areas_with_type(state, process_index, - (char *) current_var->address, snapshot1, r1, - (char *) current_var->address, snapshot2, r2, + (char *) current_var.address, snapshot1, r1, + (char *) current_var.address, snapshot2, r2, bvariable_type, 0); if (res == 1) { XBT_TRACE3(mc, global_diff, -1, -1, current_var->name); XBT_VERB("Global variable %s (%p) is different between snapshots", - current_var->name.c_str(), - (char *) current_var->address); + current_var.name.c_str(), + (char *) current_var.address); return 1; } diff --git a/src/mc/mc_dwarf.cpp b/src/mc/mc_dwarf.cpp index ee1b4ee363..4e86738aa3 100644 --- a/src/mc/mc_dwarf.cpp +++ b/src/mc/mc_dwarf.cpp @@ -6,6 +6,8 @@ #include +#include + #include #define DW_LANG_Objc DW_LANG_ObjC /* fix spelling error in older dwarf.h */ #include @@ -28,10 +30,14 @@ static void mc_frame_free(void* frame) delete (simgrid::mc::Frame*)frame; } -static void MC_dwarf_register_global_variable(mc_object_info_t info, mc_variable_t variable); -static void MC_register_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable); +static void MC_dwarf_register_global_variable( + mc_object_info_t info, std::unique_ptr variable); +static void MC_register_variable( + mc_object_info_t info, mc_frame_t frame, std::unique_ptr variable); static void MC_dwarf_register_non_global_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable); -static void MC_dwarf_register_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable); +static void MC_dwarf_register_variable( + mc_object_info_t info, mc_frame_t frame, + std::unique_ptr variable); XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing"); @@ -708,25 +714,27 @@ static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die, static int mc_anonymous_variable_index = 0; -static mc_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die, - Dwarf_Die * unit, mc_frame_t frame, - const char *ns) +static std::unique_ptr MC_die_to_variable( + mc_object_info_t info, Dwarf_Die * die, + Dwarf_Die * unit, mc_frame_t frame, + const char *ns) { // Skip declarations: if (MC_dwarf_attr_flag(die, DW_AT_declaration, false)) - return NULL; + return nullptr; // Skip compile time constants: if (dwarf_hasattr(die, DW_AT_const_value)) - return NULL; + return nullptr; Dwarf_Attribute attr_location; if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) { // No location: do not add it ? - return NULL; + return nullptr; } - simgrid::mc::Variable* variable = new simgrid::mc::Variable(); + std::unique_ptr variable = + std::unique_ptr(new simgrid::mc::Variable()); variable->dwarf_offset = dwarf_dieoffset(die); variable->global = frame == NULL; // Can be override base on DW_AT_location variable->object_info = info; @@ -815,18 +823,15 @@ static mc_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die, variable->name = "@anonymous#" + std::to_string(mc_anonymous_variable_index++); - return variable; + return std::move(variable); } static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die, Dwarf_Die * unit, mc_frame_t frame, const char *ns) { - mc_variable_t variable = - MC_die_to_variable(info, die, unit, frame, ns); - if (variable == NULL) - return; - MC_dwarf_register_variable(info, frame, variable); + MC_dwarf_register_variable(info, frame, + MC_die_to_variable(info, die, unit, frame, ns)); } static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die, @@ -845,22 +850,22 @@ static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die, if (klass == mc_tag_scope) xbt_assert(parent_frame, "No parent scope for this scope"); - mc_frame_t frame = new simgrid::mc::Frame(); + simgrid::mc::Frame frame; - frame->tag = tag; - frame->id = dwarf_dieoffset(die); - frame->object_info = info; + frame.tag = tag; + frame.id = dwarf_dieoffset(die); + frame.object_info = info; if (klass == mc_tag_subprogram) { const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name); if(ns) - frame->name = std::string(ns) + "::" + name; + frame.name = std::string(ns) + "::" + name; else - frame->name = name; + frame.name = name; } - frame->abstract_origin_id = - MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin); + frame.abstract_origin_id = + MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin); // This is the base address for DWARF addresses. // Relocated addresses are offset from this base address. @@ -869,7 +874,7 @@ static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die, // TODO, support DW_AT_ranges uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc); - frame->low_pc = low_pc ? ((char *) base) + low_pc : 0; + frame.low_pc = low_pc ? ((char *) base) + low_pc : 0; if (low_pc) { // DW_AT_high_pc: Dwarf_Attribute attr; @@ -887,14 +892,14 @@ static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die, if (dwarf_formsdata(&attr, &offset) != 0) xbt_die("Could not read constant"); - frame->high_pc = (void *) ((char *) frame->low_pc + offset); + frame.high_pc = (void *) ((char *) frame.low_pc + offset); break; // DW_AT_high_pc is a relocatable address: case MC_DW_CLASS_ADDRESS: if (dwarf_formaddr(&attr, &high_pc) != 0) xbt_die("Could not read address"); - frame->high_pc = ((char *) base) + high_pc; + frame.high_pc = ((char *) base) + high_pc; break; default: @@ -906,20 +911,22 @@ static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die, if (klass == mc_tag_subprogram) { Dwarf_Attribute attr_frame_base; if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base)) - mc_dwarf_location_list_init(&frame->frame_base, info, die, + mc_dwarf_location_list_init(&frame.frame_base, info, die, &attr_frame_base); } + // Handle children: + MC_dwarf_handle_children(info, die, unit, &frame, ns); + // Register it: if (klass == mc_tag_subprogram) { - char *key = bprintf("%" PRIx64, (uint64_t) frame->id); - xbt_dict_set(info->subprograms, key, frame, NULL); + char *key = bprintf("%" PRIx64, (uint64_t) frame.id); + + xbt_dict_set(info->subprograms, key, + new simgrid::mc::Frame(std::move(frame)), NULL); xbt_free(key); - } else if (klass == mc_tag_scope) { - xbt_dynar_push(parent_frame->scopes, &frame); - } - // Handle children: - MC_dwarf_handle_children(info, die, unit, frame, ns); + } else if (klass == mc_tag_scope) + parent_frame->scopes.push_back(std::move(frame)); } static void mc_dwarf_handle_namespace_die(mc_object_info_t info, @@ -1048,8 +1055,6 @@ ObjectInformation::ObjectInformation() this->start_ro = nullptr; this->end_ro = nullptr; this->subprograms = xbt_dict_new_homogeneous(mc_frame_free); - this->global_variables = - xbt_dynar_new(sizeof(mc_variable_t), mc_variable_free_voidp); this->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free); this->full_types_by_name = xbt_dict_new_homogeneous(NULL); this->functions_index = nullptr; @@ -1059,7 +1064,6 @@ ObjectInformation::~ObjectInformation() { xbt_free(this->file_name); xbt_dict_free(&this->subprograms); - xbt_dynar_free(&this->global_variables); xbt_dict_free(&this->types); xbt_dict_free(&this->full_types_by_name); xbt_dynar_free(&this->functions_index); @@ -1142,12 +1146,10 @@ static void MC_make_functions_index(mc_object_info_t info) static void MC_post_process_variables(mc_object_info_t info) { - unsigned cursor = 0; - mc_variable_t variable = nullptr; - xbt_dynar_foreach(info->global_variables, cursor, variable) - if (!variable->type_id.empty()) - variable->type = (mc_type_t) xbt_dict_get_or_null( - info->types, variable->type_id.c_str()); + for(simgrid::mc::Variable& variable : info->global_variables) + if (!variable.type_id.empty()) + variable.type = (mc_type_t) xbt_dict_get_or_null( + info->types, variable.type_id.c_str()); } static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope) @@ -1164,17 +1166,14 @@ static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope) } // Direct: - unsigned cursor = 0; - mc_variable_t variable = nullptr; - xbt_dynar_foreach(scope->variables, cursor, variable) - if (!variable->type_id.empty()) - variable->type = (mc_type_t) xbt_dict_get_or_null( - info->types, variable->type_id.c_str()); + for (simgrid::mc::Variable& variable : scope->variables) + if (!variable.type_id.empty()) + variable.type = (mc_type_t) xbt_dict_get_or_null( + info->types, variable.type_id.c_str()); // Recursive post-processing of nested-scopes: - mc_frame_t nested_scope = nullptr; - xbt_dynar_foreach(scope->scopes, cursor, nested_scope) - mc_post_process_scope(info, nested_scope); + for (simgrid::mc::Frame& nested_scope : scope->scopes) + mc_post_process_scope(info, &nested_scope); } @@ -1250,22 +1249,21 @@ std::shared_ptr MC_find_object_info( /*************************************************************************/ -static int MC_dwarf_get_variable_index(xbt_dynar_t variables, const char *var, - void *address) +static int MC_dwarf_get_variable_index( + std::vector variables, const char *var, void *address) { - if (xbt_dynar_is_empty(variables)) + if (variables.empty()) return 0; unsigned int cursor = 0; int start = 0; - int end = xbt_dynar_length(variables) - 1; - mc_variable_t var_test = NULL; + int end = variables.size() - 1; + mc_variable_t var_test = nullptr; while (start <= end) { cursor = (start + end) / 2; - var_test = - (mc_variable_t) xbt_dynar_get_as(variables, cursor, mc_variable_t); + var_test = &variables[cursor]; if (strcmp(var_test->name.c_str(), var) < 0) { start = cursor + 1; } else if (strcmp(var_test->name.c_str(), var) > 0) { @@ -1296,40 +1294,47 @@ static int MC_dwarf_get_variable_index(xbt_dynar_t variables, const char *var, } -void MC_dwarf_register_global_variable(mc_object_info_t info, - mc_variable_t variable) +void MC_dwarf_register_global_variable( + mc_object_info_t info, + std::unique_ptr variable) { int index = MC_dwarf_get_variable_index(info->global_variables, variable->name.c_str(), variable->address); if (index != -1) - xbt_dynar_insert_at(info->global_variables, index, &variable); + info->global_variables.insert( + info->global_variables.begin() + index, std::move(*variable)); // TODO, else ? } -void MC_dwarf_register_non_global_variable(mc_object_info_t info, - mc_frame_t frame, - mc_variable_t variable) +void MC_dwarf_register_non_global_variable( + mc_object_info_t info, + mc_frame_t frame, + std::unique_ptr variable) { xbt_assert(frame, "Frame is NULL"); int index = MC_dwarf_get_variable_index( frame->variables, variable->name.c_str(), NULL); if (index != -1) - xbt_dynar_insert_at(frame->variables, index, &variable); + frame->variables.insert( + frame->variables.begin() + index, std::move(*variable)); // TODO, else ? } -void MC_dwarf_register_variable(mc_object_info_t info, mc_frame_t frame, - mc_variable_t variable) +void MC_dwarf_register_variable( + mc_object_info_t info, mc_frame_t frame, + std::unique_ptr variable) { + if (!variable) + return; if (variable->global) - MC_dwarf_register_global_variable(info, variable); - else if (frame == NULL) - xbt_die("No frame for this local variable"); + MC_dwarf_register_global_variable(info, std::move(variable)); + else if (frame != nullptr) + MC_dwarf_register_non_global_variable(info, frame, std::move(variable)); else - MC_dwarf_register_non_global_variable(info, frame, variable); + xbt_die("No frame for this local variable"); } void MC_post_process_object_info(mc_process_t process, mc_object_info_t info) diff --git a/src/mc/mc_object_info.cpp b/src/mc/mc_object_info.cpp index 5222260291..7fc980821e 100644 --- a/src/mc/mc_object_info.cpp +++ b/src/mc/mc_object_info.cpp @@ -14,19 +14,6 @@ namespace simgrid { namespace mc { -// Free functions - -static void mc_variable_free_voidp(void *t) -{ - delete *(simgrid::mc::Variable**)t; -} - -static void mc_frame_free_voipd(void** p) -{ - delete *(mc_frame_t**)p; - *p = nullptr; -} - // Type Type::Type() @@ -59,21 +46,11 @@ Frame::Frame() this->tag = 0; this->low_pc = nullptr; this->high_pc = nullptr; - this->variables = xbt_dynar_new( - sizeof(mc_variable_t), mc_variable_free_voidp); this->id = 0; - this->scopes = xbt_dynar_new( - sizeof(mc_frame_t), (void_f_pvoid_t) mc_frame_free_voipd); this->abstract_origin_id = 0; this->object_info = nullptr; } -Frame::~Frame() -{ - xbt_dynar_free(&(this->variables)); - xbt_dynar_free(&(this->scopes)); -} - // ObjectInformations mc_frame_t ObjectInformation::find_function(const void *ip) const @@ -96,13 +73,11 @@ mc_frame_t ObjectInformation::find_function(const void *ip) const return nullptr; } -mc_variable_t ObjectInformation::find_variable(const char* name) const +simgrid::mc::Variable* ObjectInformation::find_variable(const char* name) const { - unsigned int cursor = 0; - mc_variable_t variable; - xbt_dynar_foreach(this->global_variables, cursor, variable) - if(variable->name == name) - return variable; + for (simgrid::mc::Variable& variable : this->global_variables) + if(variable.name == name) + return &variable; return nullptr; } diff --git a/src/mc/mc_object_info.h b/src/mc/mc_object_info.h index e7a5a1bce1..9ba2378353 100644 --- a/src/mc/mc_object_info.h +++ b/src/mc/mc_object_info.h @@ -113,7 +113,8 @@ public: char *start_ro; char *end_ro; // read-only segment xbt_dict_t subprograms; // xbt_dict_t - xbt_dynar_t global_variables; // xbt_dynar_t + // TODO, remove the mutable (to remove it we'll have to add a lot of const everywhere) + mutable std::vector global_variables; xbt_dict_t types; // xbt_dict_t xbt_dict_t full_types_by_name; // xbt_dict_t (full defined type only) @@ -135,7 +136,8 @@ public: void* base_address() const; mc_frame_t find_function(const void *ip) const; - mc_variable_t find_variable(const char* name) const; + // TODO, should be simgrid::mc::Variable* + simgrid::mc::Variable* find_variable(const char* name) const; }; @@ -159,8 +161,6 @@ namespace mc { class Variable { public: Variable(); - Variable(Variable const&) = delete; - Variable& operator=(Variable const&) = delete; Dwarf_Off dwarf_offset; /* Global offset of the field. */ int global; @@ -180,18 +180,15 @@ public: class Frame { public: Frame(); - ~Frame(); - Frame(Frame const&) = delete; - Frame& operator=(Frame&) = delete; int tag; std::string name; void *low_pc; void *high_pc; simgrid::mc::LocationList frame_base; - xbt_dynar_t /* */ variables; /* Cannot use dict, there may be several variables with the same name (in different lexical blocks)*/ + std::vector variables; unsigned long int id; /* DWARF offset of the subprogram */ - xbt_dynar_t /* */ scopes; + std::vector scopes; Dwarf_Off abstract_origin_id; mc_object_info_t object_info; }; diff --git a/src/mc/mcer_ignore.cpp b/src/mc/mcer_ignore.cpp index bb509d1d8a..bf75f7d2cb 100644 --- a/src/mc/mcer_ignore.cpp +++ b/src/mc/mcer_ignore.cpp @@ -118,17 +118,16 @@ void MCer_ignore_global_variable(const char *name) // Binary search: int start = 0; - int end = xbt_dynar_length(info->global_variables) - 1; + int end = info->global_variables.size() - 1; while (start <= end) { unsigned int cursor = (start + end) / 2; - mc_variable_t current_var = - (mc_variable_t) xbt_dynar_get_as(info->global_variables, - cursor, mc_variable_t); + mc_variable_t current_var = &info->global_variables[cursor]; int cmp = strcmp(current_var->name.c_str(), name); if (cmp == 0) { - xbt_dynar_remove_at(info->global_variables, cursor, NULL); + info->global_variables.erase( + info->global_variables.begin() + cursor); start = 0; - end = xbt_dynar_length(info->global_variables) - 1; + end = info->global_variables.size() - 1; } else if (cmp < 0) { start = cursor + 1; } else { @@ -195,23 +194,21 @@ static void mc_ignore_local_variable_in_scope(const char *var_name, // Try to find the variable and remove it: int start = 0; - int end = xbt_dynar_length(scope->variables) - 1; + int end = scope->variables.size() - 1; // Dichotomic search: while (start <= end) { int cursor = (start + end) / 2; - mc_variable_t current_var = - (mc_variable_t) xbt_dynar_get_as(scope->variables, cursor, - mc_variable_t); + mc_variable_t current_var = &scope->variables[cursor]; int compare = strcmp(current_var->name.c_str(), var_name); if (compare == 0) { // Variable found, remove it: - xbt_dynar_remove_at(scope->variables, cursor, NULL); + scope->variables.erase(scope->variables.begin() + cursor); // and start again: start = 0; - end = xbt_dynar_length(scope->variables) - 1; + end = scope->variables.size() - 1; } else if (compare < 0) { start = cursor + 1; } else { @@ -221,17 +218,15 @@ static void mc_ignore_local_variable_in_scope(const char *var_name, } // And recursive processing in nested scopes: - unsigned cursor = 0; - mc_frame_t nested_scope = NULL; - xbt_dynar_foreach(scope->scopes, cursor, nested_scope) { + for (simgrid::mc::Frame& nested_scope : scope->scopes) { // The new scope may be an inlined subroutine, in this case we want to use its // namespaced name in recursive calls: mc_frame_t nested_subprogram = - nested_scope->tag == - DW_TAG_inlined_subroutine ? nested_scope : 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); + nested_subprogram, &nested_scope); } } diff --git a/teshsuite/mc/dwarf/dwarf.cpp b/teshsuite/mc/dwarf/dwarf.cpp index 0412e2f4eb..2f463b5ecc 100644 --- a/teshsuite/mc/dwarf/dwarf.cpp +++ b/teshsuite/mc/dwarf/dwarf.cpp @@ -44,16 +44,15 @@ static mc_frame_t find_function_by_name( } static mc_variable_t find_local_variable( - mc_frame_t frame, const char* argument_name) { - unsigned int cursor = 0; - mc_variable_t variable; - xbt_dynar_foreach(frame->variables, cursor, variable) - if(argument_name == variable->name) - return variable; + mc_frame_t frame, const char* argument_name) +{ + for (simgrid::mc::Variable& variable : frame->variables) + if(argument_name == variable.name) + return &variable; - mc_frame_t scope = nullptr; - xbt_dynar_foreach(frame->scopes, cursor, scope) { - variable = find_local_variable(scope, argument_name); + for (simgrid::mc::Frame& scope : frame->scopes) { + simgrid::mc::Variable* variable = find_local_variable( + &scope, argument_name); if(variable) return variable; }