Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0e3a3941f0197d71910774e9ca4517870463c187
[simgrid.git] / src / mc / inspect / ObjectInformation.hpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_MC_OBJECT_INFORMATION_HPP
7 #define SIMGRID_MC_OBJECT_INFORMATION_HPP
8
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13
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"
18
19 #include "src/smpi/include/private.hpp"
20
21 namespace simgrid {
22 namespace mc {
23
24 /** An entry in the functions index
25  *
26  *  See the code of ObjectInformation::find_function.
27  */
28 struct FunctionIndexEntry {
29   void* low_pc;
30   simgrid::mc::Frame* function;
31 };
32
33 /** Information about an ELF module (executable or shared object)
34  *
35  *  This contains all the information we need about an executable or
36  *  shared-object in the model-checked process:
37  *
38  *  - where it is located in the virtual address space;
39  *
40  *  - where are located its different memory mappings in the the
41  *    virtual address space;
42  *
43  *  - all the debugging (DWARF) information
44  *    - types,
45  *    - location of the functions and their local variables,
46  *    - global variables,
47  *
48  *  - etc.
49  */
50 class ObjectInformation {
51 public:
52   ObjectInformation() = default;
53
54   // Not copyable:
55   ObjectInformation(ObjectInformation const&) = delete;
56   ObjectInformation& operator=(ObjectInformation const&) = delete;
57
58   // Flag:
59   static const int Executable = 1;
60
61   /** Bitfield of flags */
62   int flags = 0;
63   std::string file_name;
64   const void* start = nullptr;
65   const void* end   = nullptr;
66   // Location of its text segment:
67   char* start_exec = nullptr;
68   char* end_exec   = nullptr;
69   // Location of the read-only part of its data segment:
70   char* start_rw = nullptr;
71   char* end_rw   = nullptr;
72   // Location of the read/write part of its data segment:
73   char* start_ro = nullptr;
74   char* end_ro   = nullptr;
75
76   /** All of its subprograms indexed by their address */
77   std::unordered_map<std::uint64_t, simgrid::mc::Frame> subprograms;
78
79   /** Index of functions by instruction address
80    *
81    * We need to efficiently find the function from any given instruction
82    * address inside its range. This index is sorted by low_pc
83    *
84    * The entries are sorted by low_pc and a binary search can be used to look
85    * them up. In order to have a better cache locality, we only keep the
86    * information we need for the lookup in this vector. We could probably
87    * replace subprograms by an ordered vector of Frame and replace this one b
88    * a parallel `std::vector<void*>`.
89    */
90   std::vector<FunctionIndexEntry> functions_index;
91
92   std::vector<simgrid::mc::Variable> global_variables;
93
94   /** Types indexed by DWARF ID */
95   std::unordered_map<std::uint64_t, simgrid::mc::Type> types;
96
97   /** Types indexed by name
98    *
99    *  Different compilation units have their separate type definitions
100    *  (for the same type). When we find an opaque type in one compilation unit,
101    *  we use this in order to try to find its definition in another compilation
102    *  unit.
103    */
104   std::unordered_map<std::string, simgrid::mc::Type*> full_types_by_name;
105
106   /** Whether this module is an executable
107    *
108    *  More precisely we check if this is an ET_EXE ELF. These ELF files
109    *  use fixed addresses instead of base-address relative addresses.
110    *  Position independent executables are in fact ET_DYN.
111    */
112   bool executable() const { return this->flags & simgrid::mc::ObjectInformation::Executable; }
113
114   /** Base address of the module
115    *
116    *  All the location information in ELF and DWARF are expressed as an offsets
117    *  from this base address:
118    *
119    *  - location of the functions and global variables
120    *
121    *  - the DWARF instruction `OP_addr` pushes this on the DWARF stack.
122    **/
123   void* base_address() const;
124
125   /** Find a function by instruction address
126    *
127    *  @param ip instruction address
128    *  @return corresponding function (if any) or nullptr
129    */
130   simgrid::mc::Frame* find_function(const void* ip) const;
131
132   /** Find a global variable by name
133    *
134    *  This is used to ignore global variables and to find well-known variables
135    *  (`__mmalloc_default_mdp`).
136    *
137    *  @param name scopes name of the global variable (`myproject::Foo::count`)
138    *  @return corresponding variable (if any) or nullptr
139    */
140   const simgrid::mc::Variable* find_variable(const char* name) const;
141
142   /** Remove a global variable (in order to ignore it)
143    *
144    *  This is used to ignore a global variable for the snapshot comparison.
145    */
146   void remove_global_variable(const char* name);
147
148   /** Remove a local variables (in order to ignore it)
149    *
150    *  @param name Name of the local variable
151    *  @param scope scopes name name of the function (myproject::Foo::count) or null for all functions
152    */
153   void remove_local_variable(const char* name, const char* scope);
154 };
155
156 XBT_PRIVATE std::shared_ptr<ObjectInformation> createObjectInformation(std::vector<simgrid::xbt::VmMap> const& maps,
157                                                                        const char* name);
158
159 /** Augment the current module with informations about the other ones */
160 XBT_PRIVATE void postProcessObjectInformation(simgrid::mc::RemoteClient* process, simgrid::mc::ObjectInformation* info);
161 } // namespace mc
162 } // namespace simgrid
163
164 #endif