Logo AND Algorithmique Numérique Distribuée

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