Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / teshsuite / mc / dwarf / dwarf.cpp
1 /* Copyright (c) 2014-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifdef NDEBUG
7 #undef NDEBUG
8 #endif
9
10 #include <mc/mc.h>
11
12 #include "mc/datatypes.h"
13 #include "src/mc/mc_private.hpp"
14
15 #include "src/mc/inspect/ObjectInformation.hpp"
16 #include "src/mc/inspect/Type.hpp"
17 #include "src/mc/inspect/Variable.hpp"
18 #include "src/mc/remote/RemoteProcess.hpp"
19
20 #include <cassert>
21 #include <cstring>
22
23 #include <elfutils/version.h>
24 #if _ELFUTILS_VERSION < 171
25 // Elder elfutils/libdw broken with multi-dimensional arrays. See https://sourceware.org/bugzilla/show_bug.cgi?id=22546
26 int test_some_array[4 * 5 * 6];
27 #else
28 int test_some_array[4][5][6];
29 #endif
30
31 struct some_struct {
32   int first;
33   int second[4][5];
34 };
35 some_struct test_some_struct;
36
37 static simgrid::mc::Frame* find_function_by_name(
38     simgrid::mc::ObjectInformation* info, const char* name)
39 {
40   for (auto& entry : info->subprograms)
41     if(entry.second.name == name)
42       return &entry.second;
43   return nullptr;
44 }
45
46 static simgrid::mc::Variable* find_local_variable(
47     simgrid::mc::Frame* frame, const char* argument_name)
48 {
49   for (simgrid::mc::Variable& variable : frame->variables)
50     if(argument_name == variable.name)
51       return &variable;
52
53   for (simgrid::mc::Frame& scope : frame->scopes) {
54     simgrid::mc::Variable* variable = find_local_variable(
55       &scope, argument_name);
56     if(variable)
57       return variable;
58   }
59
60   return nullptr;
61 }
62
63 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable,
64                                 const void* address, unw_cursor_t* cursor)
65 {
66   simgrid::mc::Frame* subprogram = find_function_by_name(info, function);
67   assert(subprogram);
68   // TODO, Lookup frame by IP and test against name instead
69
70   const simgrid::mc::Variable* var = find_local_variable(subprogram, variable);
71   assert(var);
72
73   void* frame_base = subprogram->frame_base(*cursor);
74   simgrid::dwarf::Location location = simgrid::dwarf::resolve(var->location_list, info, cursor, frame_base, nullptr);
75
76   xbt_assert(location.in_memory(), "Expected the variable %s of function %s to be in memory", variable, function);
77   xbt_assert(location.address() == address, "Bad resolution of local variable %s of %s", variable, function);
78 }
79
80 static const simgrid::mc::Variable* test_global_variable(const simgrid::mc::RemoteProcess& process,
81                                                          simgrid::mc::ObjectInformation* info, const char* name,
82                                                          void* address, long byte_size)
83 {
84   const simgrid::mc::Variable* variable = info->find_variable(name);
85   xbt_assert(variable, "Global variable %s was not found", name);
86   xbt_assert(variable->name == name, "Name mismatch for %s", name);
87   xbt_assert(variable->global, "Variable %s is not global", name);
88   xbt_assert(variable->address == address, "Address mismatch for %s : %p expected but %p found", name, address,
89              variable->address);
90
91   auto i = process.binary_info->types.find(variable->type_id);
92   xbt_assert(i != process.binary_info->types.end(), "Missing type for %s", name);
93   const simgrid::mc::Type* type = &i->second;
94   xbt_assert(type->byte_size == byte_size, "Byte size mismatch for %s", name);
95   return variable;
96 }
97
98 static simgrid::mc::Member* find_member(simgrid::mc::Type& type, const char* name)
99 {
100   for (simgrid::mc::Member& member : type.members)
101     if(member.name == name)
102       return &member;
103   return nullptr;
104 }
105
106 int some_local_variable = 0;
107
108 struct s_foo {
109   int i;
110 };
111
112 static void test_type_by_name(const simgrid::mc::RemoteProcess& process, s_foo /*my_foo*/)
113 {
114   assert(process.binary_info->full_types_by_name.find("struct s_foo") != process.binary_info->full_types_by_name.end());
115 }
116
117 int main(int argc, char** argv)
118 {
119   SIMIX_global_init(&argc, argv);
120
121   const simgrid::mc::Variable* var;
122   simgrid::mc::Type* type;
123
124   simgrid::mc::RemoteProcess process(getpid());
125   process.init(nullptr, nullptr, nullptr, nullptr);
126
127   test_global_variable(process, process.binary_info.get(), "some_local_variable", &some_local_variable, sizeof(int));
128
129   var = test_global_variable(process, process.binary_info.get(), "test_some_array", &test_some_array,
130                              sizeof(test_some_array));
131   auto i = process.binary_info->types.find(var->type_id);
132   xbt_assert(i != process.binary_info->types.end(), "Missing type");
133   type = &i->second;
134   xbt_assert(type->element_count == 6 * 5 * 4, "element_count mismatch in test_some_array : %i / %i",
135              type->element_count, 6 * 5 * 4);
136
137   var = test_global_variable(process, process.binary_info.get(), "test_some_struct", &test_some_struct,
138                              sizeof(test_some_struct));
139   i = process.binary_info->types.find(var->type_id);
140   xbt_assert(i != process.binary_info->types.end(), "Missing type");
141   type = &i->second;
142
143   assert(type);
144   assert(find_member(*type, "first")->offset() == 0);
145   assert(find_member(*type, "second")->offset() ==
146          ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
147
148   unw_context_t context;
149   unw_cursor_t cursor;
150   unw_getcontext(&context);
151   unw_init_local(&cursor, &context);
152
153   test_local_variable(process.binary_info.get(), "main", "argc", &argc, &cursor);
154
155   int lexical_block_variable = 50;
156   test_local_variable(process.binary_info.get(), "main", "lexical_block_variable", &lexical_block_variable, &cursor);
157
158   s_foo my_foo = {0};
159   test_type_by_name(process, my_foo);
160
161   return 0;
162 }