Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make ObjectInformation::function_index a std::vector
[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.h"
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   mc_type_t subtype; // DW_AT_type
63   mc_type_t 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   mc_type_t type;
109
110   // Use either of:
111   simgrid::mc::LocationList location_list;
112   void* address;
113
114   size_t start_scope;
115   mc_object_info_t 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   mc_object_info_t 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   mc_frame_t function;
141 };
142
143 class ObjectInformation {
144 public:
145   ObjectInformation();
146   ObjectInformation(ObjectInformation const&) = delete;
147   ObjectInformation& operator=(ObjectInformation const&) = delete;
148
149   mc_object_info_flags flags;
150   std::string file_name;
151   const void* start;
152   const void *end;
153   char *start_exec;
154   char *end_exec; // Executable segment
155   char *start_rw;
156   char *end_rw; // Read-write segment
157   char *start_ro;
158   char *end_ro; // read-only segment
159   std::unordered_map<std::uint64_t, simgrid::mc::Frame> subprograms;
160   // TODO, remove the mutable (to remove it we'll have to add a lot of const everywhere)
161   mutable std::vector<simgrid::mc::Variable> global_variables;
162   std::unordered_map<std::uint64_t, simgrid::mc::Type> types;
163   std::unordered_map<std::string, simgrid::mc::Type*> full_types_by_name;
164
165   /** Index of functions by IP
166    *
167    * The entries are sorted by low_pc and a binary search can be used to look
168    * them up. Should we used a binary tree instead?
169    */
170   std::vector<FunctionIndexEntry> functions_index;
171
172   bool executable() const
173   {
174     return this->flags & MC_OBJECT_INFO_EXECUTABLE;
175   }
176
177   bool privatized() const
178   {
179     return this->executable() && smpi_privatize_global_variables;
180   }
181
182   void* base_address() const;
183
184   mc_frame_t find_function(const void *ip) const;
185   // TODO, should be simgrid::mc::Variable*
186   simgrid::mc::Variable* find_variable(const char* name) const;
187
188 };
189
190 }
191 }
192
193
194 XBT_INTERNAL std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
195   std::vector<simgrid::mc::VmMap> const& maps, const char* name, int executable);
196 XBT_INTERNAL void MC_post_process_object_info(mc_process_t process, mc_object_info_t info);
197
198 XBT_INTERNAL void MC_dwarf_get_variables(mc_object_info_t info);
199 XBT_INTERNAL void MC_dwarf_get_variables_libdw(mc_object_info_t info);
200 XBT_INTERNAL const char* MC_dwarf_attrname(int attr);
201 XBT_INTERNAL const char* MC_dwarf_tagname(int tag);
202
203 XBT_INTERNAL void* mc_member_resolve(
204   const void* base, mc_type_t type, mc_type_t member,
205   mc_address_space_t snapshot, int process_index);
206
207 #endif