Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add comments on ObjectInformation
[simgrid.git] / src / mc / mc_object_info.h
index 9914c56..b175daf 100644 (file)
  *  Debug information for the MC.
  */
 
-#ifndef MC_OBJECT_INFO_H
-#define MC_OBJECT_INFO_H
+#ifndef SIMGRID_MC_OBJECT_INFO_H
+#define SIMGRID_MC_OBJECT_INFO_H
 
-#include <stdint.h>
-#include <stdbool.h>
+#include <cstdint>
+
+#include <string>
+#include <vector>
+#include <unordered_map>
 
 #include <simgrid_config.h>
 #include <xbt/dict.h>
 #include <xbt/dynar.h>
 
+#include <elfutils/libdw.h>
+
 #include "mc_forward.h"
 #include "mc_location.h"
 #include "mc_process.h"
 #include "../smpi/private.h"
 
-SG_BEGIN_DECL();
-
 // ***** Type
 
-typedef int e_dw_type_type;
+typedef int e_mc_type_type;
 
-struct s_dw_type {
-  e_dw_type_type type;
+namespace simgrid {
+namespace mc {
+
+/** Represents a type in the program
+ *
+ *  It is currently used to represent members of structs and unions as well.
+ */
+class Type {
+public:
+  Type();
+  Type(Type const& type) = default;
+  Type& operator=(Type const&) = default;
+  Type(Type&& type) = default;
+  Type& operator=(Type&&) = default;
+
+  e_mc_type_type type;
   Dwarf_Off id; /* Offset in the section (in hexadecimal form) */
-  char *name; /* Name of the type */
+  std::string name; /* Name of the type */
   int byte_size; /* Size in bytes */
   int element_count; /* Number of elements for array type */
-  char *dw_type_id; /* DW_AT_type id */
-  xbt_dynar_t members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
+  std::uint64_t type_id; /* DW_AT_type id */
+  std::vector<Type> members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
   int is_pointer_type;
 
   // Location (for members) is either of:
-  struct s_mc_expression location;
-  int offset;
-
-  dw_type_t subtype; // DW_AT_type
-  dw_type_t full_type; // The same (but more complete) type
-};
-
-void dw_type_free(dw_type_t t);
-void dw_variable_free(dw_variable_t v);
-void dw_variable_free_voidp(void *t);
-
-// ***** Object info
-
-enum mc_object_info_flags {
-  MC_OBJECT_INFO_NONE = 0,
-  MC_OBJECT_INFO_EXECUTABLE = 1
+  simgrid::mc::DwarfExpression location_expression;
+
+  mc_type_t subtype; // DW_AT_type
+  mc_type_t full_type; // The same (but more complete) type
+
+  bool has_offset_location() const
+  {
+    return location_expression.size() == 1 &&
+      location_expression[0].atom == DW_OP_plus_uconst;
+  }
+
+  // TODO, check if this shortcut is really necessary
+  int offset() const
+  {
+    xbt_assert(this->has_offset_location());
+    return this->location_expression[0].number;
+  }
+
+  void offset(int new_offset)
+  {
+    Dwarf_Op op;
+    op.atom = DW_OP_plus_uconst;
+    op.number = new_offset;
+    this->location_expression = { op };
+  }
 };
 
-struct s_mc_object_info {
-  enum mc_object_info_flags flags;
-  char* file_name;
-  const void* start, *end;
-  char *start_exec, *end_exec; // Executable segment
-  char *start_rw, *end_rw; // Read-write segment
-  char *start_ro, *end_ro; // read-only segment
-  xbt_dict_t subprograms; // xbt_dict_t<origin as hexadecimal string, dw_frame_t>
-  xbt_dynar_t global_variables; // xbt_dynar_t<dw_variable_t>
-  xbt_dict_t types; // xbt_dict_t<origin as hexadecimal string, dw_type_t>
-  xbt_dict_t full_types_by_name; // xbt_dict_t<name, dw_type_t> (full defined type only)
-
-  // Here we sort the minimal information for an efficient (and cache-efficient)
-  // lookup of a function given an instruction pointer.
-  // The entries are sorted by low_pc and a binary search can be used to look them up.
-  xbt_dynar_t functions_index;
-};
-
-static inline __attribute__ ((always_inline))
-bool MC_object_info_executable(mc_object_info_t info)
-{
-  return info->flags & MC_OBJECT_INFO_EXECUTABLE;
 }
-
-static inline __attribute__ ((always_inline))
-bool MC_object_info_is_privatized(mc_object_info_t info)
-{
-  return info && MC_object_info_executable(info) && smpi_privatize_global_variables;
 }
 
-/** 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* MC_object_base_address(mc_object_info_t info);
-
-mc_object_info_t MC_new_object_info(void);
-mc_object_info_t MC_find_object_info(memory_map_t maps, const char* name, int executable);
-void MC_free_object_info(mc_object_info_t* p);
-
-dw_frame_t MC_file_object_info_find_function(mc_object_info_t info, const void *ip);
-dw_variable_t MC_file_object_info_find_variable_by_name(mc_object_info_t info, const char* name);
-
-void MC_post_process_object_info(mc_process_t process, mc_object_info_t info);
+// ***** Object info
 
-void MC_dwarf_get_variables(mc_object_info_t info);
-void MC_dwarf_get_variables_libdw(mc_object_info_t info);
-const char* MC_dwarf_attrname(int attr);
-const char* MC_dwarf_tagname(int tag);
+/** Bit field of options */
+typedef int mc_object_info_flags;
+#define MC_OBJECT_INFO_NONE 0
+#define MC_OBJECT_INFO_EXECUTABLE 1
 
-// Not used:
-char* get_type_description(mc_object_info_t info, char *type_name);
+namespace simgrid {
+namespace mc {
 
-void* mc_member_resolve(const void* base, dw_type_t type, dw_type_t member, mc_address_space_t snapshot, int process_index);
+class Variable {
+public:
+  Variable();
 
-struct s_dw_variable{
   Dwarf_Off dwarf_offset; /* Global offset of the field. */
   int global;
-  char *name;
-  char *type_origin;
-  dw_type_t type;
+  std::string name;
+  std::uint64_t type_id;
+  mc_type_t type;
 
   // Use either of:
-  s_mc_location_list_t locations;
+  simgrid::mc::LocationList location_list;
   void* address;
 
   size_t start_scope;
   mc_object_info_t object_info;
-
 };
 
-struct s_dw_frame{
+class Frame {
+public:
+  Frame();
+
   int tag;
-  char *name;
+  std::string name;
   void *low_pc;
   void *high_pc;
-  s_mc_location_list_t frame_base;
-  xbt_dynar_t /* <dw_variable_t> */ variables; /* Cannot use dict, there may be several variables with the same name (in different lexical blocks)*/
+  simgrid::mc::LocationList frame_base;
+  std::vector<Variable> variables;
   unsigned long int id; /* DWARF offset of the subprogram */
-  xbt_dynar_t /* <dw_frame_t> */ scopes;
+  std::vector<Frame> scopes;
   Dwarf_Off abstract_origin_id;
   mc_object_info_t object_info;
 };
 
-struct s_mc_function_index_item {
-  void* low_pc, *high_pc;
-  dw_frame_t function;
+/** An entry in the functions index
+ *
+ *  See the code of ObjectInformation::find_function.
+ */
+struct FunctionIndexEntry {
+  void* low_pc;
+  mc_frame_t function;
 };
 
-void mc_frame_free(dw_frame_t freme);
+/** Information about an (ELF) executable/sharedobject
+ *
+ *  This contain sall the information we have at runtime about an
+ *  executable/shared object in the target (modelchecked) process:
+ *  - where it is located in the virtual address space;
+ *  - where are located it's different memory mapping in the the
+ *    virtual address space ;
+ *  - all the debugging (DWARF) information,
+ *    - location of the functions,
+ *    - types
+ *  - etc.
+ *
+ *  It is not copyable because we are taking pointers to Types/Frames.
+ *  We'd have to update/rebuild some data structures in order to copy
+ *  successfully.
+ */
+
+class ObjectInformation {
+public:
+  ObjectInformation();
+
+  // Not copyable:
+  ObjectInformation(ObjectInformation const&) = delete;
+  ObjectInformation& operator=(ObjectInformation const&) = delete;
+
+  mc_object_info_flags flags;
+  std::string file_name;
+  const void* start;
+  const void *end;
+  char *start_exec;
+  char *end_exec; // Executable segment
+  char *start_rw;
+  char *end_rw; // Read-write segment
+  char *start_ro;
+  char *end_ro; // read-only segment
+  std::unordered_map<std::uint64_t, simgrid::mc::Frame> subprograms;
+  // TODO, remove the mutable (to remove it we'll have to add a lot of const everywhere)
+  mutable std::vector<simgrid::mc::Variable> global_variables;
+  std::unordered_map<std::uint64_t, simgrid::mc::Type> types;
+  std::unordered_map<std::string, simgrid::mc::Type*> full_types_by_name;
+
+  /** Index of functions by IP
+   *
+   * The entries are sorted by low_pc and a binary search can be used to look
+   * them up. Should we used a binary tree instead?
+   */
+  std::vector<FunctionIndexEntry> functions_index;
+
+  bool executable() const
+  {
+    return this->flags & MC_OBJECT_INFO_EXECUTABLE;
+  }
+
+  bool privatized() const
+  {
+    return this->executable() && smpi_privatize_global_variables;
+  }
+
+  void* base_address() const;
+
+  mc_frame_t find_function(const void *ip) const;
+  // TODO, should be simgrid::mc::Variable*
+  simgrid::mc::Variable* find_variable(const char* name) const;
+
+};
+
+}
+}
+
+
+XBT_INTERNAL std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
+  std::vector<simgrid::mc::VmMap> const& maps, const char* name, int executable);
+XBT_INTERNAL void MC_post_process_object_info(mc_process_t process, mc_object_info_t info);
+
+XBT_INTERNAL void MC_dwarf_get_variables(mc_object_info_t info);
+XBT_INTERNAL void MC_dwarf_get_variables_libdw(mc_object_info_t info);
+XBT_INTERNAL const char* MC_dwarf_attrname(int attr);
+XBT_INTERNAL const char* MC_dwarf_tagname(int tag);
 
-SG_END_DECL()
+XBT_INTERNAL void* mc_member_resolve(
+  const void* base, mc_type_t type, mc_type_t member,
+  mc_address_space_t snapshot, int process_index);
 
 #endif