Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0e08c601f95ac175058ddc9472ad1ad2ef929a8f
[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::Type* find_type_by_name(simgrid::mc::ObjectInformation* info, const char* name)
34 {
35   for (auto& entry : info->types)
36     if(entry.second.name == name)
37       return &entry.second;
38   return nullptr;
39 }
40
41 static simgrid::mc::Frame* find_function_by_name(
42     simgrid::mc::ObjectInformation* info, const char* name)
43 {
44   for (auto& entry : info->subprograms)
45     if(entry.second.name == name)
46       return &entry.second;
47   return nullptr;
48 }
49
50 static simgrid::mc::Variable* find_local_variable(
51     simgrid::mc::Frame* frame, const char* argument_name)
52 {
53   for (simgrid::mc::Variable& variable : frame->variables)
54     if(argument_name == variable.name)
55       return &variable;
56
57   for (simgrid::mc::Frame& scope : frame->scopes) {
58     simgrid::mc::Variable* variable = find_local_variable(
59       &scope, argument_name);
60     if(variable)
61       return variable;
62   }
63
64   return nullptr;
65 }
66
67 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
68   simgrid::mc::Frame* subprogram = find_function_by_name(info, function);
69   assert(subprogram);
70   // TODO, Lookup frame by IP and test against name instead
71
72   simgrid::mc::Variable* var = find_local_variable(subprogram, variable);
73   assert(var);
74
75   void* frame_base = subprogram->frame_base(*cursor);
76   simgrid::dwarf::Location location = simgrid::dwarf::resolve(
77     var->location_list, info, cursor, frame_base, nullptr, -1);
78
79   xbt_assert(location.in_memory(), "Expected the variable %s of function %s to be in memory", variable, function);
80   xbt_assert(location.address() == address, "Bad resolution of local variable %s of %s", variable, function);
81 }
82
83 static simgrid::mc::Variable* test_global_variable(simgrid::mc::RemoteClient& process,
84                                                    simgrid::mc::ObjectInformation* info, const char* name,
85                                                    void* address, long byte_size)
86 {
87   simgrid::mc::Variable* variable = info->find_variable(name);
88   xbt_assert(variable, "Global variable %s was not found", name);
89   xbt_assert(variable->name == name, "Name mismatch for %s", name);
90   xbt_assert(variable->global, "Variable %s is not global", name);
91   xbt_assert(variable->address == address, "Address mismatch for %s : %p expected but %p found", name, address,
92              variable->address);
93
94   auto i = process.binary_info->types.find(variable->type_id);
95   xbt_assert(i != process.binary_info->types.end(), "Missing type for %s", name);
96   simgrid::mc::Type* type = &i->second;
97   xbt_assert(type->byte_size == byte_size, "Byte size mismatch for %s", name);
98   return variable;
99 }
100
101 static simgrid::mc::Member* find_member(simgrid::mc::Type& type, const char* name)
102 {
103   for (simgrid::mc::Member& member : type.members)
104     if(member.name == name)
105       return &member;
106   return nullptr;
107 }
108
109 int some_local_variable = 0;
110
111 struct s_foo {
112   int i;
113 };
114
115 static void test_type_by_name(simgrid::mc::RemoteClient& process, s_foo /*my_foo*/)
116 {
117   assert(process.binary_info->full_types_by_name.find("struct s_foo") != process.binary_info->full_types_by_name.end());
118 }
119
120 int main(int argc, char** argv)
121 {
122   SIMIX_global_init(&argc, argv);
123
124   simgrid::mc::Variable* var;
125   simgrid::mc::Type* type;
126
127   simgrid::mc::RemoteClient process(getpid(), -1);
128   process.init();
129
130   test_global_variable(process, process.binary_info.get(), "some_local_variable", &some_local_variable, sizeof(int));
131
132   var = test_global_variable(process, process.binary_info.get(), "test_some_array", &test_some_array,
133                              sizeof(test_some_array));
134   auto 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   xbt_assert(type->element_count == 6 * 5 * 4, "element_count mismatch in test_some_array : %i / %i",
138              type->element_count, 6 * 5 * 4);
139
140   var = test_global_variable(process, process.binary_info.get(), "test_some_struct", &test_some_struct,
141                              sizeof(test_some_struct));
142   i = process.binary_info->types.find(var->type_id);
143   xbt_assert(i != process.binary_info->types.end(), "Missing type");
144   type = &i->second;
145
146   assert(type);
147   assert(find_member(*type, "first")->offset() == 0);
148   assert(find_member(*type, "second")->offset() ==
149          ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
150
151   unw_context_t context;
152   unw_cursor_t cursor;
153   unw_getcontext(&context);
154   unw_init_local(&cursor, &context);
155
156   test_local_variable(process.binary_info.get(), "main", "argc", &argc, &cursor);
157
158   int lexical_block_variable = 50;
159   test_local_variable(process.binary_info.get(), "main", "lexical_block_variable", &lexical_block_variable, &cursor);
160
161   s_foo my_foo = {0};
162   test_type_by_name(process, my_foo);
163
164   _exit(0);
165 }