Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] OOPify/C++ify ObjectInformation
[simgrid.git] / src / mc / mc_dwarf.cpp
index 7c1bcd4..faba98c 100644 (file)
@@ -18,8 +18,6 @@
 #include "mc_object_info.h"
 #include "mc_private.h"
 
-extern "C" {
-
 static void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
 static void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
@@ -101,37 +99,6 @@ static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
  */
 static char *MC_dwarf_at_type(Dwarf_Die * die);
 
-/** \brief Get the name of an attribute (DW_AT_*) from its code
- *
- *  \param attr attribute code (see the DWARF specification)
- *  \return name of the attribute
- */
-const char *MC_dwarf_attrname(int attr)
-{
-  switch (attr) {
-#include "mc_dwarf_attrnames.h"
-  default:
-    return "DW_AT_unknown";
-  }
-}
-
-/** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
- *
- *  \param tag tag code (see the DWARF specification)
- *  \return name of the tag
- */
-XBT_INTERNAL
-const char *MC_dwarf_tagname(int tag)
-{
-  switch (tag) {
-#include "mc_dwarf_tagnames.h"
-  case DW_TAG_invalid:
-    return "DW_TAG_invalid";
-  default:
-    return "DW_TAG_unknown";
-  }
-}
-
 /** \brief A class of DWARF tags (DW_TAG_*)
  */
 typedef enum mc_tag_class {
@@ -559,7 +526,7 @@ static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member,
 
 static void dw_type_free_voidp(void *t)
 {
-  dw_type_free((dw_type_t) * (void **) t);
+  delete *(dw_type_t*)t;
 }
 
 /** \brief Populate the list of members of a type
@@ -639,7 +606,7 @@ static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
                                       const char *ns)
 {
 
-  dw_type_t type = xbt_new0(s_dw_type_t, 1);
+  dw_type_t type = new s_dw_type();
   type->type = -1;
   type->id = 0;
   type->name = NULL;
@@ -788,7 +755,7 @@ static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
       if (len == 1 && expr[0].atom == DW_OP_addr) {
         variable->global = 1;
         uintptr_t offset = (uintptr_t) expr[0].number;
-        uintptr_t base = (uintptr_t) MC_object_base_address(info);
+        uintptr_t base = (uintptr_t) info->base_address();
         variable->address = (void *) (base + offset);
       } else {
         mc_dwarf_location_list_init_from_expression(&variable->locations, len,
@@ -896,7 +863,7 @@ static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
   // This is the base address for DWARF addresses.
   // Relocated addresses are offset from this base address.
   // See DWARF4 spec 7.5
-  void *base = MC_object_base_address(info);
+  void *base = info->base_address();
 
   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
   frame->variables =
@@ -1072,13 +1039,33 @@ void mc_frame_free(dw_frame_t frame)
   xbt_free(frame);
 }
 
-void dw_type_free(dw_type_t t)
+s_dw_type::s_dw_type()
+{
+  this->type = 0;
+  this->id = 0;
+  this->name = nullptr;
+  this->byte_size = 0;
+  this->element_count = 0;
+  this->dw_type_id = nullptr;
+  this->members = nullptr;
+  this->is_pointer_type = 0;
+  this->location = { 0, 0, 0, 0};
+  this->offset = 0;
+  this->subtype = nullptr;
+  this->full_type = nullptr;
+}
+
+s_dw_type::~s_dw_type()
 {
-  xbt_free(t->name);
-  xbt_free(t->dw_type_id);
-  xbt_dynar_free(&(t->members));
-  mc_dwarf_expression_clear(&t->location);
-  xbt_free(t);
+  xbt_free(this->name);
+  xbt_free(this->dw_type_id);
+  xbt_dynar_free(&this->members);
+  mc_dwarf_expression_clear(&this->location);
+}
+
+static void dw_type_free(dw_type_t t)
+{
+  delete t;
 }
 
 void dw_variable_free(dw_variable_t v)
@@ -1100,45 +1087,71 @@ void dw_variable_free_voidp(void *t)
 
 // ***** object_info
 
+namespace simgrid {
+namespace mc {
 
-
-mc_object_info_t MC_new_object_info(void)
+ObjectInformation::ObjectInformation()
 {
-  mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
-  res->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
-  res->global_variables =
+  this->flags = 0;
+  this->file_name = nullptr;
+  this->start = nullptr;
+  this->end = nullptr;
+  this->start_exec = nullptr;
+  this->end_exec = nullptr;
+  this->start_rw = nullptr;
+  this->end_rw = nullptr;
+  this->start_ro = nullptr;
+  this->end_ro = nullptr;
+  this->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
+  this->global_variables =
       xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
-  res->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
-  res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
-  return res;
+  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;
 }
 
-void MC_free_object_info(mc_object_info_t * info)
+ObjectInformation::~ObjectInformation()
 {
-  xbt_free((*info)->file_name);
-  xbt_dict_free(&(*info)->subprograms);
-  xbt_dynar_free(&(*info)->global_variables);
-  xbt_dict_free(&(*info)->types);
-  xbt_dict_free(&(*info)->full_types_by_name);
-  xbt_free(*info);
-  xbt_dynar_free(&(*info)->functions_index);
-  *info = NULL;
+  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);
 }
 
-// ***** Helpers
-
-void *MC_object_base_address(mc_object_info_t info)
+/** Find the DWARF offset for this ELF object
+ *
+ *  An offset is applied to address found in DWARF:
+ *
+ *  <ul>
+ *    <li>for an executable obejct, addresses are virtual address
+ *        (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
+ *    <li>for a shared object, the addreses are offset from the begining
+ *        of the shared object (the base address of the mapped shared
+ *        object must be used as offset
+ *        i.e. \f$\text{virtual address} = \text{shared object base address}
+ *             + \text{dwarf address}\f$.</li>
+ *
+ */
+void *ObjectInformation::base_address() const
 {
-  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;
+  if (this->executable())
+    return nullptr;
+
+  void *result = this->start_exec;
+  if (this->start_rw != NULL && result > (void *) this->start_rw)
+    result = this->start_rw;
+  if (this->start_ro != NULL && result > (void *) this->start_ro)
+    result = this->start_ro;
   return result;
 }
 
+}
+}
+
+// ***** Helpers
+
 // ***** Functions index
 
 static int MC_compare_frame_index_items(mc_function_index_item_t a,
@@ -1278,20 +1291,21 @@ static void MC_post_process_types(mc_object_info_t info)
 }
 
 /** \brief Finds informations about a given shared object/executable */
-mc_object_info_t MC_find_object_info(memory_map_t maps, const char *name,
-                                     int executable)
+std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
+  std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
 {
-  mc_object_info_t result = MC_new_object_info();
+  std::shared_ptr<s_mc_object_info_t> result =
+    std::make_shared<s_mc_object_info_t>();
   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;
+  MC_find_object_address(maps, result.get());
+  MC_dwarf_get_variables(result.get());
+  MC_post_process_types(result.get());
+  MC_post_process_variables(result.get());
+  MC_post_process_functions(result.get());
+  MC_make_functions_index(result.get());
+  return std::move(result);
 }
 
 /*************************************************************************/
@@ -1394,9 +1408,9 @@ void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
 
     // Resolve full_type:
     if (subtype->name && subtype->byte_size == 0) {
-      for (size_t i = 0; i != process->object_infos_size; ++i) {
+      for (auto const& object_info : process->object_infos) {
         dw_type_t same_type = (dw_type_t)
-            xbt_dict_get_or_null(process->object_infos[i]->full_types_by_name,
+            xbt_dict_get_or_null(object_info->full_types_by_name,
                                  subtype->name);
         if (same_type && same_type->name && same_type->byte_size) {
           type->full_type = same_type;
@@ -1407,5 +1421,3 @@ void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
 
   }
 }
-
-}