Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove a lot of typedefs and typedef usage
[simgrid.git] / src / mc / mc_object_info.h
1 /* Copyright (c) 2007-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /** file
8  *  Debug information for the MC.
9  */
10
11 #ifndef SIMGRID_MC_OBJECT_INFO_H
12 #define SIMGRID_MC_OBJECT_INFO_H
13
14 #include <cstdint>
15
16 #include <string>
17 #include <vector>
18 #include <unordered_map>
19
20 #include <simgrid_config.h>
21 #include <xbt/dict.h>
22 #include <xbt/dynar.h>
23
24 #include <elfutils/libdw.h>
25
26 #include "mc_forward.hpp"
27 #include "mc_location.h"
28 #include "mc_process.h"
29 #include "../smpi/private.h"
30
31 // ***** Type
32
33 typedef int e_mc_type_type;
34
35 namespace simgrid {
36 namespace mc {
37
38 /** Represents a type in the program
39  *
40  *  It is currently used to represent members of structs and unions as well.
41  */
42 class Type {
43 public:
44   Type();
45   Type(Type const& type) = default;
46   Type& operator=(Type const&) = default;
47   Type(Type&& type) = default;
48   Type& operator=(Type&&) = default;
49
50   e_mc_type_type type;
51   Dwarf_Off id; /* Offset in the section (in hexadecimal form) */
52   std::string name; /* Name of the type */
53   int byte_size; /* Size in bytes */
54   int element_count; /* Number of elements for array type */
55   std::uint64_t type_id; /* DW_AT_type id */
56   std::vector<Type> members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
57   int is_pointer_type;
58
59   // Location (for members) is either of:
60   simgrid::mc::DwarfExpression location_expression;
61
62   simgrid::mc::Type* subtype; // DW_AT_type
63   simgrid::mc::Type* full_type; // The same (but more complete) type
64
65   bool has_offset_location() const
66   {
67     return location_expression.size() == 1 &&
68       location_expression[0].atom == DW_OP_plus_uconst;
69   }
70
71   // TODO, check if this shortcut is really necessary
72   int offset() const
73   {
74     xbt_assert(this->has_offset_location());
75     return this->location_expression[0].number;
76   }
77
78   void offset(int new_offset)
79   {
80     Dwarf_Op op;
81     op.atom = DW_OP_plus_uconst;
82     op.number = new_offset;
83     this->location_expression = { op };
84   }
85 };
86
87 }
88 }
89
90 // ***** Object info
91
92 /** Bit field of options */
93 typedef int mc_object_info_flags;
94 #define MC_OBJECT_INFO_NONE 0
95 #define MC_OBJECT_INFO_EXECUTABLE 1
96
97 namespace simgrid {
98 namespace mc {
99
100 class Variable {
101 public:
102   Variable();
103
104   Dwarf_Off dwarf_offset; /* Global offset of the field. */
105   int global;
106   std::string name;
107   std::uint64_t type_id;
108   simgrid::mc::Type* type;
109
110   // Use either of:
111   simgrid::mc::LocationList location_list;
112   void* address;
113
114   size_t start_scope;
115   simgrid::mc::ObjectInformation* object_info;
116 };
117
118 class Frame {
119 public:
120   Frame();
121
122   int tag;
123   std::string name;
124   void *low_pc;
125   void *high_pc;
126   simgrid::mc::LocationList frame_base;
127   std::vector<Variable> variables;
128   unsigned long int id; /* DWARF offset of the subprogram */
129   std::vector<Frame> scopes;
130   Dwarf_Off abstract_origin_id;
131   simgrid::mc::ObjectInformation* object_info;
132 };
133
134 /** An entry in the functions index
135  *
136  *  See the code of ObjectInformation::find_function.
137  */
138 struct FunctionIndexEntry {
139   void* low_pc;
140   simgrid::mc::Frame* function;
141 };
142
143 /** Information about an (ELF) executable/sharedobject
144  *
145  *  This contain sall the information we have at runtime about an
146  *  executable/shared object in the target (modelchecked) process:
147  *  - where it is located in the virtual address space;
148  *  - where are located it's different memory mapping in the the
149  *    virtual address space ;
150  *  - all the debugging (DWARF) information,
151  *    - location of the functions,
152  *    - types
153  *  - etc.
154  *
155  *  It is not copyable because we are taking pointers to Types/Frames.
156  *  We'd have to update/rebuild some data structures in order to copy
157  *  successfully.
158  */
159
160 class ObjectInformation {
161 public:
162   ObjectInformation();
163
164   // Not copyable:
165   ObjectInformation(ObjectInformation const&) = delete;
166   ObjectInformation& operator=(ObjectInformation const&) = delete;
167
168   mc_object_info_flags flags;
169   std::string file_name;
170   const void* start;
171   const void *end;
172   char *start_exec;
173   char *end_exec; // Executable segment
174   char *start_rw;
175   char *end_rw; // Read-write segment
176   char *start_ro;
177   char *end_ro; // read-only segment
178   std::unordered_map<std::uint64_t, simgrid::mc::Frame> subprograms;
179   // TODO, remove the mutable (to remove it we'll have to add a lot of const everywhere)
180   mutable std::vector<simgrid::mc::Variable> global_variables;
181   std::unordered_map<std::uint64_t, simgrid::mc::Type> types;
182   std::unordered_map<std::string, simgrid::mc::Type*> full_types_by_name;
183
184   /** Index of functions by IP
185    *
186    * The entries are sorted by low_pc and a binary search can be used to look
187    * them up. Should we used a binary tree instead?
188    */
189   std::vector<FunctionIndexEntry> functions_index;
190
191   bool executable() const
192   {
193     return this->flags & MC_OBJECT_INFO_EXECUTABLE;
194   }
195
196   bool privatized() const
197   {
198     return this->executable() && smpi_privatize_global_variables;
199   }
200
201   void* base_address() const;
202
203   simgrid::mc::Frame* find_function(const void *ip) const;
204   // TODO, should be simgrid::mc::Variable*
205   simgrid::mc::Variable* find_variable(const char* name) const;
206
207 };
208
209 }
210 }
211
212
213 XBT_INTERNAL std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
214   std::vector<simgrid::mc::VmMap> const& maps, const char* name, int executable);
215 XBT_INTERNAL void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info);
216
217 XBT_INTERNAL void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info);
218 XBT_INTERNAL void MC_dwarf_get_variables_libdw(simgrid::mc::ObjectInformation* info);
219 XBT_INTERNAL const char* MC_dwarf_attrname(int attr);
220 XBT_INTERNAL const char* MC_dwarf_tagname(int tag);
221
222 XBT_INTERNAL void* mc_member_resolve(
223   const void* base, simgrid::mc::Type* type, simgrid::mc::Type* member,
224   simgrid::mc::AddressSpace* snapshot, int process_index);
225
226 #endif