1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_MC_OBJECT_INFORMATION_HPP
7 #define SIMGRID_MC_OBJECT_INFORMATION_HPP
11 #include <unordered_map>
14 #include "src/mc/inspect/Frame.hpp"
15 #include "src/mc/inspect/Type.hpp"
16 #include "src/mc/mc_forward.hpp"
17 #include "src/xbt/memory_map.hpp"
19 #include "src/smpi/include/private.hpp"
24 /** An entry in the functions index
26 * See the code of ObjectInformation::find_function.
28 struct FunctionIndexEntry {
30 simgrid::mc::Frame* function;
33 /** Information about an ELF module (executable or shared object)
35 * This contains all the information we need about an executable or
36 * shared-object in the model-checked process:
38 * - where it is located in the virtual address space;
40 * - where are located its different memory mappings in the the
41 * virtual address space;
43 * - all the debugging (DWARF) information
45 * - location of the functions and their local variables,
50 class ObjectInformation {
51 bool dwarf_loaded = false; // Lazily loads the dwarf info
54 void ensure_dwarf_loaded(); // Used by functions that need the dwarf
55 ObjectInformation() = default;
58 ObjectInformation(ObjectInformation const&) = delete;
59 ObjectInformation& operator=(ObjectInformation const&) = delete;
62 static const int Executable = 1;
64 /** Bitfield of flags */
66 std::string file_name;
67 const void* start = nullptr;
68 const void* end = nullptr;
69 // Location of its text segment:
70 char* start_exec = nullptr;
71 char* end_exec = nullptr;
72 // Location of the read-only part of its data segment:
73 char* start_rw = nullptr;
74 char* end_rw = nullptr;
75 // Location of the read/write part of its data segment:
76 char* start_ro = nullptr;
77 char* end_ro = nullptr;
79 /** All of its subprograms indexed by their address */
80 std::unordered_map<std::uint64_t, simgrid::mc::Frame> subprograms;
82 /** Index of functions by instruction address
84 * We need to efficiently find the function from any given instruction
85 * address inside its range. This index is sorted by low_pc
87 * The entries are sorted by low_pc and a binary search can be used to look
88 * them up. In order to have a better cache locality, we only keep the
89 * information we need for the lookup in this vector. We could probably
90 * replace subprograms by an ordered vector of Frame and replace this one b
91 * a parallel `std::vector<void*>`.
93 std::vector<FunctionIndexEntry> functions_index;
95 std::vector<simgrid::mc::Variable> global_variables;
97 /** Types indexed by DWARF ID */
98 std::unordered_map<std::uint64_t, simgrid::mc::Type> types;
100 /** Types indexed by name
102 * Different compilation units have their separate type definitions
103 * (for the same type). When we find an opaque type in one compilation unit,
104 * we use this in order to try to find its definition in another compilation
107 std::unordered_map<std::string, simgrid::mc::Type*> full_types_by_name;
109 /** Whether this module is an executable
111 * More precisely we check if this is an ET_EXE ELF. These ELF files
112 * use fixed addresses instead of base-address relative addresses.
113 * Position independent executables are in fact ET_DYN.
115 bool executable() const { return this->flags & simgrid::mc::ObjectInformation::Executable; }
117 /** Base address of the module
119 * All the location information in ELF and DWARF are expressed as an offsets
120 * from this base address:
122 * - location of the functions and global variables
124 * - the DWARF instruction `OP_addr` pushes this on the DWARF stack.
126 void* base_address() const;
128 /** Find a function by instruction address
130 * Loads the dwarf information on need.
132 * @param ip instruction address
133 * @return corresponding function (if any) or nullptr
135 simgrid::mc::Frame* find_function(const void* ip);
137 /** Find a global variable by name
139 * Loads the dwarf information on need.
141 * This is used to ignore global variables and to find well-known variables
142 * (`__mmalloc_default_mdp`).
144 * @param name scopes name of the global variable (`myproject::Foo::count`)
145 * @return corresponding variable (if any) or nullptr
147 const simgrid::mc::Variable* find_variable(const char* name);
149 /** Remove a global variable (in order to ignore it)
151 * This is used to ignore a global variable for the snapshot comparison.
153 void remove_global_variable(const char* name);
155 /** Remove a local variables (in order to ignore it)
157 * @param name Name of the local variable
158 * @param scope scopes name name of the function (myproject::Foo::count) or null for all functions
160 void remove_local_variable(const char* name, const char* scope);
163 XBT_PRIVATE std::shared_ptr<ObjectInformation> createObjectInformation(std::vector<simgrid::xbt::VmMap> const& maps,
166 /** Augment the current module with information about the other ones */
167 XBT_PRIVATE void postProcessObjectInformation(const simgrid::mc::RemoteProcess* process,
168 simgrid::mc::ObjectInformation* info);
170 } // namespace simgrid