Logo AND Algorithmique Numérique Distribuée

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