Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
another bunch of codefactor style issues
[simgrid.git] / src / mc / inspect / mc_dwarf.cpp
index 81f61f1..e3a6d25 100644 (file)
@@ -3,33 +3,31 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "src/simgrid/util.hpp"
+#include "xbt/log.h"
+#include "xbt/string.hpp"
+#include "xbt/sysdep.h"
+#include <simgrid/config.h>
+
+#include "src/mc/inspect/ObjectInformation.hpp"
+#include "src/mc/inspect/Variable.hpp"
+#include "src/mc/inspect/mc_dwarf.hpp"
+#include "src/mc/mc_private.hpp"
+#include "src/mc/remote/RemoteClient.hpp"
+
 #include <cinttypes>
 #include <cstdint>
-
+#include <cstdlib>
+#include <fcntl.h>
 #include <memory>
 #include <utility>
 
 #include <boost/range/algorithm.hpp>
 
-#include <cstdlib>
 #include <elfutils/libdw.h>
-#include <fcntl.h>
 
 #include <boost/algorithm/string/predicate.hpp>
 
-#include "src/simgrid/util.hpp"
-#include "xbt/log.h"
-#include "xbt/string.hpp"
-#include "xbt/sysdep.h"
-#include <simgrid/config.h>
-
-#include "src/mc/mc_dwarf.hpp"
-#include "src/mc/mc_private.hpp"
-
-#include "src/mc/ObjectInformation.hpp"
-#include "src/mc/Variable.hpp"
-#include "src/mc/remote/RemoteClient.hpp"
-
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
 
 /** @brief The default DW_TAG_lower_bound for a given DW_AT_language.
@@ -256,8 +254,7 @@ static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die* die, int attribute)
     return 0;
   dwarf_attr_integrate(die, attribute, &attr);
   Dwarf_Die subtype_die;
-  if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
-    xbt_die("Could not find DIE");
+  xbt_assert(dwarf_formref_die(&attr, &subtype_die) != nullptr, "Could not find DIE");
   return dwarf_dieoffset(&subtype_die);
 }
 
@@ -268,8 +265,7 @@ static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die* die, int attribute
     return 0;
   dwarf_attr_integrate(die, DW_AT_type, &attr);
   Dwarf_Die subtype_die;
-  if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
-    xbt_die("Could not find DIE");
+  xbt_assert(dwarf_formref_die(&attr, &subtype_die) != nullptr, "Could not find DIE");
   return dwarf_dieoffset(&subtype_die);
 }
 
@@ -311,8 +307,8 @@ static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, bool integrate)
     return false;
 
   bool result;
-  if (dwarf_formflag(&attr, &result))
-    xbt_die("Unexpected form for attribute %s", simgrid::dwarf::attrname(attribute));
+  xbt_assert(not dwarf_formflag(&attr, &result), "Unexpected form for attribute %s",
+             simgrid::dwarf::attrname(attribute));
   return result;
 }
 
@@ -438,8 +434,7 @@ static bool MC_compare_variable(simgrid::mc::Variable const& a, simgrid::mc::Var
  */
 static void MC_dwarf_fill_member_location(simgrid::mc::Type* type, simgrid::mc::Member* member, Dwarf_Die* child)
 {
-  if (dwarf_hasattr(child, DW_AT_data_bit_offset))
-    xbt_die("Can't groke DW_AT_data_bit_offset.");
+  xbt_assert(not dwarf_hasattr(child, DW_AT_data_bit_offset), "Can't groke DW_AT_data_bit_offset.");
 
   if (not dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
     if (type->type == DW_TAG_union_type)
@@ -459,10 +454,10 @@ static void MC_dwarf_fill_member_location(simgrid::mc::Type* type, simgrid::mc::
       {
         Dwarf_Op* expr;
         size_t len;
-        if (dwarf_getlocation(&attr, &expr, &len))
-          xbt_die("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%" PRIx64
-                  ">%s",
-                  MC_dwarf_attr_integrate_string(child, DW_AT_name), (uint64_t)type->id, type->name.c_str());
+        xbt_assert(not dwarf_getlocation(&attr, &expr, &len),
+                   "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%" PRIx64
+                   ">%s",
+                   MC_dwarf_attr_integrate_string(child, DW_AT_name), (uint64_t)type->id, type->name.c_str());
         member->location_expression = simgrid::dwarf::DwarfExpression(expr, expr + len);
         break;
       }
@@ -470,11 +465,9 @@ static void MC_dwarf_fill_member_location(simgrid::mc::Type* type, simgrid::mc::
       // Offset from the base address of the object:
       {
         Dwarf_Word offset;
-        if (not dwarf_formudata(&attr, &offset))
-          member->offset(offset);
-        else
-          xbt_die("Cannot get %s location <%" PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
-                  (uint64_t)type->id, type->name.c_str());
+        xbt_assert(not dwarf_formudata(&attr, &offset), "Cannot get %s location <%" PRIx64 ">%s",
+                   MC_dwarf_attr_integrate_string(child, DW_AT_name), (uint64_t)type->id, type->name.c_str());
+        member->offset(offset);
         break;
       }
 
@@ -501,7 +494,6 @@ static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* /*info*/, Dwarf
   for (res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) {
     int tag = dwarf_tag(&child);
     if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
-
       // Skip declarations:
       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
         continue;
@@ -544,14 +536,12 @@ static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* /*info*/, Dwarf
       member.byte_size = MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
       member.type_id   = MC_dwarf_at_type(&child);
 
-      if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
-        xbt_die("Can't groke DW_AT_data_bit_offset.");
+      xbt_assert(not dwarf_hasattr(&child, DW_AT_data_bit_offset), "Can't groke DW_AT_data_bit_offset.");
 
       MC_dwarf_fill_member_location(type, &member, &child);
 
-      if (not member.type_id)
-        xbt_die("Missing type for member %s of <%" PRIx64 ">%s", member.name.c_str(), (uint64_t)type->id,
-                type->name.c_str());
+      xbt_assert(member.type_id, "Missing type for member %s of <%" PRIx64 ">%s", member.name.c_str(),
+                 (uint64_t)type->id, type->name.c_str());
 
       type->members.push_back(std::move(member));
     }
@@ -694,11 +684,10 @@ static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(simgrid::mc::Ob
       {
         Dwarf_Op* expr;
         size_t len;
-        if (dwarf_getlocation(&attr_location, &expr, &len)) {
-          xbt_die("Could not read location expression in DW_AT_location "
-                  "of variable <%" PRIx64 ">%s",
-                  (uint64_t)variable->id, variable->name.c_str());
-        }
+        xbt_assert(not dwarf_getlocation(&attr_location, &expr, &len),
+                   "Could not read location expression in DW_AT_location "
+                   "of variable <%" PRIx64 ">%s",
+                   (uint64_t)variable->id, variable->name.c_str());
 
         if (len == 1 && expr[0].atom == DW_OP_addr) {
           variable->global  = true;
@@ -806,26 +795,22 @@ static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwar
   if (low_pc) {
     // DW_AT_high_pc:
     Dwarf_Attribute attr;
-    if (not dwarf_attr_integrate(die, DW_AT_high_pc, &attr))
-      xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
+    xbt_assert(dwarf_attr_integrate(die, DW_AT_high_pc, &attr), "Missing DW_AT_high_pc matching with DW_AT_low_pc");
 
     Dwarf_Sword offset;
     Dwarf_Addr high_pc;
 
     switch (simgrid::dwarf::classify_form(dwarf_whatform(&attr))) {
-
-        // DW_AT_high_pc if an offset from the low_pc:
+      // DW_AT_high_pc if an offset from the low_pc:
       case simgrid::dwarf::FormClass::Constant:
 
-        if (dwarf_formsdata(&attr, &offset) != 0)
-          xbt_die("Could not read constant");
+        xbt_assert(dwarf_formsdata(&attr, &offset) == 0, "Could not read constant");
         frame.range.end() = frame.range.begin() + offset;
         break;
 
         // DW_AT_high_pc is a relocatable address:
       case simgrid::dwarf::FormClass::Address:
-        if (dwarf_formaddr(&attr, &high_pc) != 0)
-          xbt_die("Could not read address");
+        xbt_assert(dwarf_formaddr(&attr, &high_pc) == 0, "Could not read address");
         frame.range.end() = base + high_pc;
         break;
 
@@ -858,8 +843,7 @@ static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
                                           simgrid::mc::Frame* frame, const char* ns)
 {
   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
-  if (frame)
-    xbt_die("Unexpected namespace in a subprogram");
+  xbt_assert(not frame, "Unexpected namespace in a subprogram");
   char* new_ns = ns == nullptr ? xbt_strdup(name) : bprintf("%s::%s", ns, name);
   MC_dwarf_handle_children(info, die, unit, frame, new_ns);
   xbt_free(new_ns);
@@ -881,8 +865,7 @@ static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die*
   int tag                        = dwarf_tag(die);
   simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
   switch (klass) {
-
-      // Type:
+    // Type:
     case simgrid::dwarf::TagClass::Type:
       MC_dwarf_handle_type_die(info, die, unit, frame, ns);
       break;
@@ -948,8 +931,7 @@ static std::vector<char> get_build_id(Elf* elf)
   // found in a PT_NOTE entry in the program header table.
 
   size_t phnum;
-  if (elf_getphdrnum(elf, &phnum) != 0)
-    xbt_die("Could not read program headers");
+  xbt_assert(elf_getphdrnum(elf, &phnum) == 0, "Could not read program headers");
 
   // Iterate over the program headers and find the PT_NOTE ones:
   for (size_t i = 0; i < phnum; ++i) {
@@ -1050,19 +1032,15 @@ static std::string find_by_build_id(std::vector<char> id)
  */
 static void MC_load_dwarf(simgrid::mc::ObjectInformation* info)
 {
-  if (elf_version(EV_CURRENT) == EV_NONE)
-    xbt_die("libelf initialization error");
+  xbt_assert(elf_version(EV_CURRENT) != EV_NONE, "libelf initialization error");
 
   // Open the ELF file:
   int fd = open(info->file_name.c_str(), O_RDONLY);
-  if (fd < 0)
-    xbt_die("Could not open file %s", info->file_name.c_str());
+  xbt_assert(fd >= 0, "Could not open file %s", info->file_name.c_str());
   Elf* elf = elf_begin(fd, ELF_C_READ, nullptr);
-  if (elf == nullptr)
-    xbt_die("Not an ELF file");
+  xbt_assert(elf != nullptr, "Not an ELF file");
   Elf_Kind kind = elf_kind(elf);
-  if (kind != ELF_K_ELF)
-    xbt_die("Not an ELF file");
+  xbt_assert(kind == ELF_K_ELF, "Not an ELF file");
 
   // Remember if this is a `ET_EXEC` (fixed location) or `ET_DYN`:
   Elf64_Half type = get_type(elf);
@@ -1096,21 +1074,17 @@ static void MC_load_dwarf(simgrid::mc::ObjectInformation* info)
 
     // Find the debug file using the build id:
     std::string debug_file = find_by_build_id(build_id);
-    if (debug_file.empty()) {
-      std::string hex = to_hex(build_id);
-      xbt_die("Missing debug info for %s with build-id %s\n"
-              "You might want to install the suitable debugging package.\n",
-              info->file_name.c_str(), hex.c_str());
-    }
+    xbt_assert(not debug_file.empty(),
+               "Missing debug info for %s with build-id %s\n"
+               "You might want to install the suitable debugging package.\n",
+               info->file_name.c_str(), to_hex(build_id).c_str());
 
     // Load the DWARF info from this file:
     XBT_DEBUG("Load DWARF for %s from %s", info->file_name.c_str(), debug_file.c_str());
     fd = open(debug_file.c_str(), O_RDONLY);
-    if (fd < 0)
-      xbt_die("Could not open file %s", debug_file.c_str());
+    xbt_assert(fd >= 0, "Could not open file %s", debug_file.c_str());
     dwarf = dwarf_begin(fd, DWARF_C_READ);
-    if (dwarf == nullptr)
-      xbt_die("No DWARF info in %s for %s", debug_file.c_str(), info->file_name.c_str());
+    xbt_assert(dwarf != nullptr, "No DWARF info in %s for %s", debug_file.c_str(), info->file_name.c_str());
     read_dwarf_info(info, dwarf);
     dwarf_end(dwarf);
     close(fd);
@@ -1162,7 +1136,6 @@ static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
 
 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* scope)
 {
-
   if (scope->tag == DW_TAG_inlined_subroutine) {
     // Attach correct namespaced name in inlined subroutine:
     auto i = info->subprograms.find(scope->abstract_origin_id);
@@ -1239,7 +1212,6 @@ std::shared_ptr<simgrid::mc::ObjectInformation> createObjectInformation(std::vec
 void postProcessObjectInformation(simgrid::mc::RemoteClient* process, simgrid::mc::ObjectInformation* info)
 {
   for (auto& t : info->types) {
-
     simgrid::mc::Type* type    = &(t.second);
     simgrid::mc::Type* subtype = type;
     while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type ||