Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Comment some unused parameters.
[simgrid.git] / teshsuite / mc / dwarf / dwarf.cpp
1 /* Copyright (c) 2014-2017. 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 int test_some_array[4][5][6];
25 struct some_struct {
26   int first;
27   int second[4][5];
28 };
29 some_struct test_some_struct;
30
31 static simgrid::mc::Type* find_type_by_name(simgrid::mc::ObjectInformation* info, const char* name)
32 {
33   for (auto& entry : info->types)
34     if(entry.second.name == name)
35       return &entry.second;
36   return nullptr;
37 }
38
39 static simgrid::mc::Frame* find_function_by_name(
40     simgrid::mc::ObjectInformation* info, const char* name)
41 {
42   for (auto& entry : info->subprograms)
43     if(entry.second.name == name)
44       return &entry.second;
45   return nullptr;
46 }
47
48 static simgrid::mc::Variable* find_local_variable(
49     simgrid::mc::Frame* frame, const char* argument_name)
50 {
51   for (simgrid::mc::Variable& variable : frame->variables)
52     if(argument_name == variable.name)
53       return &variable;
54
55   for (simgrid::mc::Frame& scope : frame->scopes) {
56     simgrid::mc::Variable* variable = find_local_variable(
57       &scope, argument_name);
58     if(variable)
59       return variable;
60   }
61
62   return nullptr;
63 }
64
65 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
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   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(
75     var->location_list, info, cursor, frame_base, nullptr, -1);
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 simgrid::mc::Variable* test_global_variable(simgrid::mc::RemoteClient& process,
82                                                    simgrid::mc::ObjectInformation* info, const char* name,
83                                                    void* address, long byte_size)
84 {
85   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   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 simgrid::mc::Member* find_member(simgrid::mc::Type& type, const char* name)
100 {
101   for (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(simgrid::mc::RemoteClient& 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   SIMIX_global_init(&argc, argv);
121
122   simgrid::mc::Variable* var;
123   simgrid::mc::Type* type;
124
125   simgrid::mc::RemoteClient process(getpid(), -1);
126   process.init();
127
128   test_global_variable(process, process.binary_info.get(), "some_local_variable", &some_local_variable, sizeof(int));
129
130   var = test_global_variable(process, process.binary_info.get(), "test_some_array", &test_some_array,
131                              sizeof(test_some_array));
132   auto i = process.binary_info->types.find(var->type_id);
133   xbt_assert(i != process.binary_info->types.end(), "Missing type");
134   type = &i->second;
135   xbt_assert(type->element_count == 6 * 5 * 4, "element_count mismatch in test_some_array : %i / %i",
136              type->element_count, 6 * 5 * 4);
137
138   var = test_global_variable(process, process.binary_info.get(), "test_some_struct", &test_some_struct,
139                              sizeof(test_some_struct));
140   i = process.binary_info->types.find(var->type_id);
141   xbt_assert(i != process.binary_info->types.end(), "Missing type");
142   type = &i->second;
143
144   assert(type);
145   assert(find_member(*type, "first")->offset() == 0);
146   assert(find_member(*type, "second")->offset() ==
147          ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
148
149   unw_context_t context;
150   unw_cursor_t cursor;
151   unw_getcontext(&context);
152   unw_init_local(&cursor, &context);
153
154   test_local_variable(process.binary_info.get(), "main", "argc", &argc, &cursor);
155
156   int lexical_block_variable = 50;
157   test_local_variable(process.binary_info.get(), "main", "lexical_block_variable", &lexical_block_variable, &cursor);
158
159   s_foo my_foo = {0};
160   test_type_by_name(process, my_foo);
161
162   _exit(0);
163 }