Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Declare function 'const'.
[simgrid.git] / src / mc / inspect / mc_dwarf.cpp
index eb5f346718b80e0c57e04edd02b5b74c67f81bce..83444e45b6d55d24ccaa7ee0fbd1e303deb10066 100644 (file)
 #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 "src/mc/remote/RemoteSimulation.hpp"
 
+#include <algorithm>
+#include <array>
 #include <cinttypes>
 #include <cstdint>
 #include <cstdlib>
@@ -59,7 +61,7 @@ static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
 
 /** @brief Process a DIE
  *
- *  @param info the resulting object fot the library/binary file (output)
+ *  @param info the resulting object for the library/binary file (output)
  *  @param die  the current DIE
  *  @param unit the DIE of the compile unit of the current DIE
  *  @param frame containing frame if any
@@ -74,7 +76,7 @@ static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf
 
 /** @brief Calls MC_dwarf_handle_die on all children of the given die
  *
- *  @param info the resulting object fot the library/binary file (output)
+ *  @param info the resulting object for the library/binary file (output)
  *  @param die  the current DIE
  *  @param unit the DIE of the compile unit of the current DIE
  *  @param frame containing frame if any
@@ -84,7 +86,7 @@ static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf
 
 /** @brief Handle a variable (DW_TAG_variable or other)
  *
- *  @param info the resulting object fot the library/binary file (output)
+ *  @param info the resulting object for the library/binary file (output)
  *  @param die  the current DIE
  *  @param unit the DIE of the compile unit of the current DIE
  *  @param frame containing frame if any
@@ -302,7 +304,7 @@ static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die* die, int attribute, uint
 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, bool integrate)
 {
   Dwarf_Attribute attr;
-  if ((integrate ? dwarf_attr_integrate(die, attribute, &attr) : dwarf_attr(die, attribute, &attr)) == 0)
+  if ((integrate ? dwarf_attr_integrate(die, attribute, &attr) : dwarf_attr(die, attribute, &attr)) == nullptr)
     return false;
 
   bool result;
@@ -380,7 +382,7 @@ static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit)
   return upper_bound - lower_bound + 1;
 }
 
-/** @brief Finds the number of elements in a array type (DW_TAG_array_type)
+/** @brief Finds the number of elements in an array type (DW_TAG_array_type)
  *
  *  The compilation unit might be needed because the default lower
  *  bound depends on the language of the compilation unit.
@@ -396,8 +398,7 @@ static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit)
 
   int result = 1;
   Dwarf_Die child;
-  int res;
-  for (res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) {
+  for (int res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) {
     int child_tag = dwarf_tag(&child);
     if (child_tag == DW_TAG_subrange_type || child_tag == DW_TAG_enumeration_type)
       result *= MC_dwarf_subrange_element_count(&child, unit);
@@ -487,10 +488,9 @@ static void MC_dwarf_fill_member_location(const simgrid::mc::Type* type, simgrid
 static void MC_dwarf_add_members(const simgrid::mc::ObjectInformation* /*info*/, Dwarf_Die* die,
                                  const Dwarf_Die* /*unit*/, simgrid::mc::Type* type)
 {
-  int res;
   Dwarf_Die child;
   xbt_assert(type->members.empty());
-  for (res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) {
+  for (int 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:
@@ -660,10 +660,10 @@ static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(simgrid::mc::Ob
     // No location: do not add it ?
     return nullptr;
 
-  std::unique_ptr<simgrid::mc::Variable> variable = std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
-  variable->id                                    = dwarf_dieoffset(die);
-  variable->global                                = frame == nullptr; // Can be override base on DW_AT_location
-  variable->object_info                           = info;
+  auto variable         = std::make_unique<simgrid::mc::Variable>();
+  variable->id          = dwarf_dieoffset(die);
+  variable->global      = frame == nullptr; // Can be override base on DW_AT_location
+  variable->object_info = info;
 
   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
   if (name)
@@ -690,9 +690,9 @@ static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(simgrid::mc::Ob
 
         if (len == 1 && expr[0].atom == DW_OP_addr) {
           variable->global  = true;
-          uintptr_t offset  = (uintptr_t)expr[0].number;
-          uintptr_t base    = (uintptr_t)info->base_address();
-          variable->address = (void*)(base + offset);
+          auto offset       = static_cast<uintptr_t>(expr[0].number);
+          auto base         = reinterpret_cast<uintptr_t>(info->base_address());
+          variable->address = reinterpret_cast<void*>(base + offset);
         } else
           variable->location_list = {
               simgrid::dwarf::LocationListEntry(simgrid::dwarf::DwarfExpression(expr, expr + len))};
@@ -786,11 +786,11 @@ static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwar
   // This is the base address for DWARF addresses.
   // Relocated addresses are offset from this base address.
   // See DWARF4 spec 7.5
-  std::uint64_t base = (std::uint64_t)info->base_address();
+  auto base = reinterpret_cast<std::uint64_t>(info->base_address());
 
   // TODO, support DW_AT_ranges
   uint64_t low_pc     = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
-  frame.range.begin() = low_pc ? (std::uint64_t)base + low_pc : 0;
+  frame.range.begin() = low_pc ? base + low_pc : 0;
   if (low_pc) {
     // DW_AT_high_pc:
     Dwarf_Attribute attr;
@@ -853,8 +853,7 @@ static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf
 {
   // For each child DIE:
   Dwarf_Die child;
-  int res;
-  for (res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child))
+  for (int res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child))
     MC_dwarf_handle_die(info, &child, unit, frame, ns);
 }
 
@@ -955,7 +954,7 @@ static std::vector<char> get_build_id(Elf* elf)
           memcmp((char*)data->d_buf + name_pos, "GNU", sizeof("GNU")) == 0) {
         XBT_DEBUG("Found GNU/NT_GNU_BUILD_ID note");
         char* start = (char*)data->d_buf + desc_pos;
-        char* end   = (char*)start + nhdr.n_descsz;
+        char* end   = start + nhdr.n_descsz;
         return std::vector<char>(start, end);
       }
     }
@@ -964,11 +963,11 @@ static std::vector<char> get_build_id(Elf* elf)
   return std::vector<char>();
 }
 
-static char hexdigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
-
 /** Binary data to hexadecimal */
 static inline std::array<char, 2> to_hex(std::uint8_t byte)
 {
+  constexpr std::array<char, 16> hexdigits{
+      {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}};
   // Horrid double braces!
   // Apparently, this is needed in C++11 (not in C++14).
   return {{hexdigits[byte >> 4], hexdigits[byte & 0xF]}};
@@ -979,11 +978,8 @@ static std::string to_hex(const char* data, std::size_t count)
 {
   std::string res;
   res.resize(2 * count);
-  for (std::size_t i = 0; i < count; i++) {
-    std::array<char, 2> hex_byte = to_hex(data[i]);
-    for (int j = 0; j < 2; ++j)
-      res[2 * i + j] = hex_byte[j];
-  }
+  for (std::size_t i = 0; i < count; i++)
+    std::copy_n(cbegin(to_hex(data[i])), 2, &res[2 * i]);
   return res;
 }
 
@@ -994,7 +990,7 @@ static std::string to_hex(std::vector<char> const& data)
 }
 
 /** Base directories for external debug files */
-static const char* debug_paths[] = {
+static constexpr auto debug_paths = {
     "/usr/lib/debug/",
     "/usr/local/lib/debug/",
 };
@@ -1020,11 +1016,11 @@ static std::string find_by_build_id(std::vector<char> id)
       return filename;
     }
   }
-  XBT_DEBUG("Not debuf info found for build ID %s\n", hex.data());
+  XBT_DEBUG("No debug info found for build ID %s\n", hex.data());
   return std::string();
 }
 
-/** @brief Populate the debugging informations of the given ELF object
+/** @brief Populate the debugging information of the given ELF object
  *
  *  Read the DWARf information of the EFFL object and populate the
  *  lists of types, variables, functions.
@@ -1058,7 +1054,7 @@ static void MC_load_dwarf(simgrid::mc::ObjectInformation* info)
   dwarf_end(dwarf);
 
   // If there was no DWARF in the file, try to find it in a separate file.
-  // Different methods might be used to store the DWARF informations:
+  // Different methods might be used to store the DWARF information:
   //  * GNU NT_GNU_BUILD_ID
   //  * .gnu_debuglink
   // See https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
@@ -1190,11 +1186,11 @@ static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
 namespace simgrid {
 namespace mc {
 
-/** @brief Finds informations about a given shared object/executable */
+/** @brief Finds information about a given shared object/executable */
 std::shared_ptr<ObjectInformation> createObjectInformation(std::vector<xbt::VmMap> const& maps, const char* name)
 {
-  std::shared_ptr<ObjectInformation> result = std::make_shared<ObjectInformation>();
-  result->file_name                         = name;
+  auto result       = std::make_shared<ObjectInformation>();
+  result->file_name = name;
   simgrid::mc::find_object_address(maps, result.get());
   MC_load_dwarf(result.get());
   MC_post_process_variables(result.get());
@@ -1207,7 +1203,7 @@ std::shared_ptr<ObjectInformation> createObjectInformation(std::vector<xbt::VmMa
 
 /*************************************************************************/
 
-void postProcessObjectInformation(const RemoteClient* process, ObjectInformation* info)
+void postProcessObjectInformation(const RemoteSimulation* process, ObjectInformation* info)
 {
   for (auto& t : info->types) {
     Type* type    = &(t.second);