Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Channel: the S4U name of msg_mailbox_t
[simgrid.git] / teshsuite / mc / dwarf / dwarf.cpp
1 /* Copyright (c) 2014. 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 <string.h>
12 #include <assert.h>
13
14 #include <xbt.h>
15 #include <mc/mc.h>
16
17 #include "../../src/include/mc/datatypes.h"
18 #include "../../src/mc/mc_object_info.h"
19 #include "../../src/mc/mc_private.h"
20
21 int test_some_array[4][5][6];
22 struct some_struct { int first; int second[4][5]; } test_some_struct;
23
24 static simgrid::mc::Type* find_type_by_name(simgrid::mc::ObjectInformation* info, const char* name)
25 {
26   for (auto& entry : info->types)
27     if(entry.second.name == name)
28       return &entry.second;
29   return nullptr;
30 }
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 = mc_find_frame_base(subprogram, info, cursor);
67   s_mc_location_t location;
68
69   mc_dwarf_resolve_locations(&location,
70     &var->location_list, info, cursor, frame_base, NULL, -1);
71
72   xbt_assert(mc_get_location_type(&location)==MC_LOCATION_TYPE_ADDRESS,
73     "Unexpected location type for variable %s of %s", variable, function);
74
75   xbt_assert(location.memory_location == address,
76     "Bad resolution of local variable %s of %s", variable, function);
77
78 }
79
80 static simgrid::mc::Variable* test_global_variable(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info, const char* name, void* address, long byte_size) {
81
82   simgrid::mc::Variable* variable = info->find_variable(name);
83   xbt_assert(variable, "Global variable %s was not found", name);
84   xbt_assert(variable->name == name,
85     "Name mismatch for %s", name);
86   xbt_assert(variable->global, "Variable %s is not global", name);
87   xbt_assert(variable->address == address,
88       "Address mismatch for %s : %p expected but %p found",
89       name, address, variable->address);
90
91   auto i = process->binary_info->types.find(variable->type_id);
92   xbt_assert(i != process->binary_info->types.end(), "Missing type for %s", name);
93   simgrid::mc::Type* type = &i->second;
94   xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
95   return variable;
96 }
97
98 static simgrid::mc::Type* find_member(simgrid::mc::ObjectInformation* info, const char* name, simgrid::mc::Type* type)
99 {
100   for (simgrid::mc::Type& member : type->members)
101     if(member.name == name)
102       return &member;
103   return nullptr;
104 }
105
106 int some_local_variable = 0;
107
108 typedef struct foo {int i;} s_foo;
109
110 static void test_type_by_name(simgrid::mc::Process* process, s_foo my_foo)
111 {
112   assert(
113     process->binary_info->full_types_by_name.find("struct foo") !=
114       process->binary_info->full_types_by_name.end());
115 }
116
117 int main(int argc, char** argv)
118 {
119   SIMIX_global_init(&argc, argv);
120
121   simgrid::mc::Variable* var;
122   simgrid::mc::Type* type;
123
124   simgrid::mc::Process p(getpid(), -1);
125   simgrid::mc::Process* process = &p;
126
127   test_global_variable(process, process->binary_info.get(),
128     "some_local_variable", &some_local_variable, sizeof(int));
129
130   var = test_global_variable(process, process->binary_info.get(),
131     "test_some_array", &test_some_array, 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,
136     "element_count mismatch in test_some_array : %i / %i",
137     type->element_count, 6*5*4);
138
139   var = test_global_variable(process, process->binary_info.get(),
140     "test_some_struct", &test_some_struct, sizeof(test_some_struct));
141   i = process->binary_info->types.find(var->type_id);
142   xbt_assert(i != process->binary_info->types.end(), "Missing type");
143   type = &i->second;
144   assert(find_member(process->binary_info.get(), "first", type)->offset() == 0);
145   assert(find_member(process->binary_info.get(), "second", type)->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   {
156     int lexical_block_variable = 50;
157     test_local_variable(process->binary_info.get(), "main",
158       "lexical_block_variable", &lexical_block_variable, &cursor);
159   }
160
161   s_foo my_foo;
162   test_type_by_name(process, my_foo);
163
164   _exit(0);
165 }