Logo AND Algorithmique Numérique Distribuée

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