Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
increase the timeout to that test
[simgrid.git] / src / mc / mc_dwarf.hpp
1 /* Copyright (c) 2008-2015. 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 #if !defined(SIMGRID_MC_DWARF_HPP)
8 #define SIMGRID_MC_DWARF_HPP
9
10 #include <memory>
11
12 #include <string.h>
13
14 #include <xbt/sysdep.h>
15
16 #define DW_LANG_Objc DW_LANG_ObjC       /* fix spelling error in older dwarf.h */
17 #include <dwarf.h>
18
19 #include "mc/Variable.hpp"
20 #include "mc/mc_memory_map.h"
21
22 /** \brief A class of DWARF tags (DW_TAG_*)
23  */
24 typedef enum mc_tag_class {
25   mc_tag_unknown,
26   mc_tag_type,
27   mc_tag_subprogram,
28   mc_tag_variable,
29   mc_tag_scope,
30   mc_tag_namespace
31 } mc_tag_class;
32
33 static mc_tag_class MC_dwarf_tag_classify(int tag)
34 {
35   switch (tag) {
36
37   case DW_TAG_array_type:
38   case DW_TAG_class_type:
39   case DW_TAG_enumeration_type:
40   case DW_TAG_typedef:
41   case DW_TAG_pointer_type:
42   case DW_TAG_reference_type:
43   case DW_TAG_rvalue_reference_type:
44   case DW_TAG_string_type:
45   case DW_TAG_structure_type:
46   case DW_TAG_subroutine_type:
47   case DW_TAG_union_type:
48   case DW_TAG_ptr_to_member_type:
49   case DW_TAG_set_type:
50   case DW_TAG_subrange_type:
51   case DW_TAG_base_type:
52   case DW_TAG_const_type:
53   case DW_TAG_file_type:
54   case DW_TAG_packed_type:
55   case DW_TAG_volatile_type:
56   case DW_TAG_restrict_type:
57   case DW_TAG_interface_type:
58   case DW_TAG_unspecified_type:
59   case DW_TAG_shared_type:
60     return mc_tag_type;
61
62   case DW_TAG_subprogram:
63     return mc_tag_subprogram;
64
65   case DW_TAG_variable:
66   case DW_TAG_formal_parameter:
67     return mc_tag_variable;
68
69   case DW_TAG_lexical_block:
70   case DW_TAG_try_block:
71   case DW_TAG_catch_block:
72   case DW_TAG_inlined_subroutine:
73   case DW_TAG_with_stmt:
74     return mc_tag_scope;
75
76   case DW_TAG_namespace:
77     return mc_tag_namespace;
78
79   default:
80     return mc_tag_unknown;
81
82   }
83 }
84
85 #define MC_DW_CLASS_UNKNOWN 0
86 #define MC_DW_CLASS_ADDRESS 1   // Location in the address space of the program
87 #define MC_DW_CLASS_BLOCK 2     // Arbitrary block of bytes
88 #define MC_DW_CLASS_CONSTANT 3
89 #define MC_DW_CLASS_STRING 3    // String
90 #define MC_DW_CLASS_FLAG 4      // Boolean
91 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
92 #define MC_DW_CLASS_EXPRLOC 6   // DWARF expression/location description
93 #define MC_DW_CLASS_LINEPTR 7
94 #define MC_DW_CLASS_LOCLISTPTR 8
95 #define MC_DW_CLASS_MACPTR 9
96 #define MC_DW_CLASS_RANGELISTPTR 10
97
98 /** \brief Find the DWARF data class for a given DWARF data form
99  *
100  *  This mapping is defined in the DWARF spec.
101  *
102  *  \param form The form (values taken from the DWARF spec)
103  *  \return An internal representation for the corresponding class
104  * */
105 static int MC_dwarf_form_get_class(int form)
106 {
107   switch (form) {
108   case DW_FORM_addr:
109     return MC_DW_CLASS_ADDRESS;
110   case DW_FORM_block2:
111   case DW_FORM_block4:
112   case DW_FORM_block:
113   case DW_FORM_block1:
114     return MC_DW_CLASS_BLOCK;
115   case DW_FORM_data1:
116   case DW_FORM_data2:
117   case DW_FORM_data4:
118   case DW_FORM_data8:
119   case DW_FORM_udata:
120   case DW_FORM_sdata:
121     return MC_DW_CLASS_CONSTANT;
122   case DW_FORM_string:
123   case DW_FORM_strp:
124     return MC_DW_CLASS_STRING;
125   case DW_FORM_ref_addr:
126   case DW_FORM_ref1:
127   case DW_FORM_ref2:
128   case DW_FORM_ref4:
129   case DW_FORM_ref8:
130   case DW_FORM_ref_udata:
131     return MC_DW_CLASS_REFERENCE;
132   case DW_FORM_flag:
133   case DW_FORM_flag_present:
134     return MC_DW_CLASS_FLAG;
135   case DW_FORM_exprloc:
136     return MC_DW_CLASS_EXPRLOC;
137     // TODO sec offset
138     // TODO indirect
139   default:
140     return MC_DW_CLASS_UNKNOWN;
141   }
142 }
143
144 /** \brief Find the default lower bound for a given language
145  *
146  *  The default lower bound of an array (when DW_TAG_lower_bound
147  *  is missing) depends on the language of the compilation unit.
148  *
149  *  \param lang Language of the compilation unit (values defined in the DWARF spec)
150  *  \return     Default lower bound of an array in this compilation unit
151  * */
152 static inline
153 uint64_t MC_dwarf_default_lower_bound(int lang)
154 {
155   switch (lang) {
156   case DW_LANG_C:
157   case DW_LANG_C89:
158   case DW_LANG_C99:
159   case DW_LANG_C_plus_plus:
160   case DW_LANG_D:
161   case DW_LANG_Java:
162   case DW_LANG_ObjC:
163   case DW_LANG_ObjC_plus_plus:
164   case DW_LANG_Python:
165   case DW_LANG_UPC:
166     return 0;
167   case DW_LANG_Ada83:
168   case DW_LANG_Ada95:
169   case DW_LANG_Fortran77:
170   case DW_LANG_Fortran90:
171   case DW_LANG_Fortran95:
172   case DW_LANG_Modula2:
173   case DW_LANG_Pascal83:
174   case DW_LANG_PL1:
175   case DW_LANG_Cobol74:
176   case DW_LANG_Cobol85:
177     return 1;
178   default:
179     xbt_die("No default DW_TAG_lower_bound for language %i and none given",
180             lang);
181     return 0;
182   }
183 }
184
185 /** Sort the variable by name and address.
186  *
187  *  We could use boost::container::flat_set instead.
188  */
189 static inline
190 bool MC_compare_variable(
191   simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
192 {
193   int cmp = a.name.compare(b.name);
194   if (cmp < 0)
195     return true;
196   else if (cmp > 0)
197     return false;
198   else
199     return a.address < b.address;
200 }
201
202 XBT_INTERNAL std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
203   std::vector<simgrid::mc::VmMap> const& maps, const char* name, int executable);
204 XBT_INTERNAL void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info);
205
206 XBT_INTERNAL void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info);
207 XBT_INTERNAL void MC_dwarf_get_variables_libdw(simgrid::mc::ObjectInformation* info);
208
209 XBT_INTERNAL const char* MC_dwarf_attrname(int attr);
210 XBT_INTERNAL const char* MC_dwarf_tagname(int tag);
211
212 XBT_INTERNAL void* mc_member_resolve(
213   const void* base, simgrid::mc::Type* type, simgrid::mc::Type* member,
214   simgrid::mc::AddressSpace* snapshot, int process_index);
215
216 #endif