Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d4d513e51d9f25a3347cd84789eecec61d6d7c79
[simgrid.git] / teshsuite / mc / dwarf / dwarf.cpp
1 /* Copyright (c) 2014-2023. 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 <simgrid/s4u/Engine.hpp>
11
12 #include "src/mc/datatypes.h"
13 #include "src/mc/mc.h"
14 #include "src/mc/mc_private.hpp"
15
16 #include "src/mc/inspect/ObjectInformation.hpp"
17 #include "src/mc/inspect/Type.hpp"
18 #include "src/mc/inspect/Variable.hpp"
19 #include "src/mc/sosp/RemoteProcessMemory.hpp"
20
21 #include <cassert>
22 #include <cstring>
23
24 #include <elfutils/version.h>
25 #if _ELFUTILS_VERSION < 171
26 // Elder elfutils/libdw broken with multi-dimensional arrays. See https://sourceware.org/bugzilla/show_bug.cgi?id=22546
27 int test_some_array[4 * 5 * 6];
28 #else
29 int test_some_array[4][5][6];
30 #endif
31
32 struct some_struct {
33   int first;
34   int second[4][5];
35 };
36 some_struct test_some_struct;
37
38 static simgrid::mc::Frame* find_function_by_name(
39     simgrid::mc::ObjectInformation* info, const char* name)
40 {
41   for (auto& [_, entry] : info->subprograms)
42     if (entry.name == name)
43       return &entry;
44   return nullptr;
45 }
46
47 static simgrid::mc::Variable* find_local_variable(
48     simgrid::mc::Frame* frame, const char* argument_name)
49 {
50   for (simgrid::mc::Variable& variable : frame->variables)
51     if(argument_name == variable.name)
52       return &variable;
53
54   for (simgrid::mc::Frame& scope : frame->scopes) {
55     simgrid::mc::Variable* variable = find_local_variable(
56       &scope, argument_name);
57     if(variable)
58       return variable;
59   }
60
61   return nullptr;
62 }
63
64 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable,
65                                 const void* address, unw_cursor_t* cursor)
66 {
67   simgrid::mc::Frame* subprogram = find_function_by_name(info, function);
68   assert(subprogram);
69   // TODO, Lookup frame by IP and test against name instead
70
71   const simgrid::mc::Variable* var = find_local_variable(subprogram, variable);
72   assert(var);
73
74   void* frame_base = subprogram->frame_base(*cursor);
75   simgrid::dwarf::Location location = simgrid::dwarf::resolve(var->location_list, info, cursor, frame_base, nullptr);
76
77   xbt_assert(location.in_memory(), "Expected the variable %s of function %s to be in memory", variable, function);
78   xbt_assert(location.address() == address, "Bad resolution of local variable %s of %s", variable, function);
79 }
80
81 static const simgrid::mc::Variable* test_global_variable(const simgrid::mc::RemoteProcessMemory& process,
82                                                          simgrid::mc::ObjectInformation* info, const char* name,
83                                                          void* address, long byte_size)
84 {
85   const simgrid::mc::Variable* variable = info->find_variable(name);
86   xbt_assert(variable, "Global variable %s was not found", name);
87   xbt_assert(variable->name == name, "Name mismatch for %s", name);
88   xbt_assert(variable->global, "Variable %s is not global", name);
89   xbt_assert(variable->address == address, "Address mismatch for %s : %p expected but %p found", name, address,
90              variable->address);
91
92   auto i = process.binary_info->types.find(variable->type_id);
93   xbt_assert(i != process.binary_info->types.end(), "Missing type for %s", name);
94   const simgrid::mc::Type* type = &i->second;
95   xbt_assert(type->byte_size == byte_size, "Byte size mismatch for %s", name);
96   return variable;
97 }
98
99 static const simgrid::mc::Member* find_member(const simgrid::mc::Type& type, const char* name)
100 {
101   for (const simgrid::mc::Member& member : type.members)
102     if(member.name == name)
103       return &member;
104   return nullptr;
105 }
106
107 int some_local_variable = 0;
108
109 struct s_foo {
110   int i;
111 };
112
113 static void test_type_by_name(const simgrid::mc::RemoteProcessMemory& process, s_foo /*my_foo*/)
114 {
115   assert(process.binary_info->full_types_by_name.find("struct s_foo") != process.binary_info->full_types_by_name.end());
116 }
117
118 int main(int argc, char** argv)
119 {
120   simgrid::s4u::Engine::get_instance(&argc, argv);
121
122   const simgrid::mc::Variable* var;
123   const simgrid::mc::Type* type;
124
125   simgrid::mc::RemoteProcessMemory process(getpid(), 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 }