Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
019e5e7eab8f70ed2f270229c046e8cd77904a27
[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
119 class Frame {
120 public:
121   Frame();
122
123   int tag;
124   std::string name;
125   void *low_pc;
126   void *high_pc;
127   simgrid::mc::LocationList frame_base;
128   std::vector<Variable> variables;
129   unsigned long int id; /* DWARF offset of the subprogram */
130   std::vector<Frame> scopes;
131   Dwarf_Off abstract_origin_id;
132   mc_object_info_t object_info;
133 };
134
135 class ObjectInformation {
136 public:
137   ObjectInformation();
138   ~ObjectInformation();
139   ObjectInformation(ObjectInformation const&) = delete;
140   ObjectInformation& operator=(ObjectInformation const&) = delete;
141
142   mc_object_info_flags flags;
143   char* file_name;
144   const void* start;
145   const void *end;
146   char *start_exec;
147   char *end_exec; // Executable segment
148   char *start_rw;
149   char *end_rw; // Read-write segment
150   char *start_ro;
151   char *end_ro; // read-only segment
152   std::unordered_map<std::uint64_t, simgrid::mc::Frame> subprograms;
153   // TODO, remove the mutable (to remove it we'll have to add a lot of const everywhere)
154   mutable std::vector<simgrid::mc::Variable> global_variables;
155   std::unordered_map<std::uint64_t, simgrid::mc::Type> types;
156   std::unordered_map<std::string, simgrid::mc::Type*> full_types_by_name;
157
158   // Here we sort the minimal information for an efficient (and cache-efficient)
159   // lookup of a function given an instruction pointer.
160   // The entries are sorted by low_pc and a binary search can be used to look them up.
161   xbt_dynar_t functions_index;
162
163   bool executable() const
164   {
165     return this->flags & MC_OBJECT_INFO_EXECUTABLE;
166   }
167
168   bool privatized() const
169   {
170     return this->executable() && smpi_privatize_global_variables;
171   }
172
173   void* base_address() const;
174
175   mc_frame_t find_function(const void *ip) const;
176   // TODO, should be simgrid::mc::Variable*
177   simgrid::mc::Variable* find_variable(const char* name) const;
178
179 };
180
181 }
182 }
183
184
185 XBT_INTERNAL std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
186   std::vector<simgrid::mc::VmMap> const& maps, const char* name, int executable);
187 XBT_INTERNAL void MC_post_process_object_info(mc_process_t process, mc_object_info_t info);
188
189 XBT_INTERNAL void MC_dwarf_get_variables(mc_object_info_t info);
190 XBT_INTERNAL void MC_dwarf_get_variables_libdw(mc_object_info_t info);
191 XBT_INTERNAL const char* MC_dwarf_attrname(int attr);
192 XBT_INTERNAL const char* MC_dwarf_tagname(int tag);
193
194 XBT_INTERNAL void* mc_member_resolve(
195   const void* base, mc_type_t type, mc_type_t member,
196   mc_address_space_t snapshot, int process_index);
197
198
199 struct s_mc_function_index_item {
200   void* low_pc, *high_pc;
201   mc_frame_t function;
202 };
203
204 #endif