Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b8fb469c9f252945d8b3d733bd6cf00b69aa223d
[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 mc_type_t find_type_by_name(mc_object_info_t 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 mc_frame_t find_function_by_name(
33     mc_object_info_t info, const char* name)
34 {
35   xbt_dict_cursor_t cursor = 0;
36   mc_frame_t subprogram;
37   char* key;
38   xbt_dict_foreach(info->subprograms, cursor, key, subprogram)
39     if(subprogram->name == name)
40       return subprogram;
41   return nullptr;
42 }
43
44 static mc_variable_t find_local_variable(
45     mc_frame_t frame, const char* argument_name)
46 {
47   for (simgrid::mc::Variable& variable : frame->variables)
48     if(argument_name == variable.name)
49       return &variable;
50
51   for (simgrid::mc::Frame& scope : frame->scopes) {
52     simgrid::mc::Variable* variable = find_local_variable(
53       &scope, argument_name);
54     if(variable)
55       return variable;
56   }
57
58   return nullptr;
59 }
60
61 static void test_local_variable(mc_object_info_t info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
62   mc_frame_t subprogram = find_function_by_name(info, function);
63   assert(subprogram);
64   // TODO, Lookup frame by IP and test against name instead
65
66   mc_variable_t var = find_local_variable(subprogram, variable);
67   assert(var);
68
69   void* frame_base = mc_find_frame_base(subprogram, info, cursor);
70   s_mc_location_t location;
71
72   mc_dwarf_resolve_locations(&location,
73     &var->location_list, info, cursor, frame_base, NULL, -1);
74
75   xbt_assert(mc_get_location_type(&location)==MC_LOCATION_TYPE_ADDRESS,
76     "Unexpected location type for variable %s of %s", variable, function);
77
78   xbt_assert(location.memory_location == address,
79     "Bad resolution of local variable %s of %s", variable, function);
80
81 }
82
83 static mc_variable_t test_global_variable(mc_process_t process, mc_object_info_t info, const char* name, void* address, long byte_size) {
84
85   mc_variable_t variable = info->find_variable(name);
86   xbt_assert(variable, "Global variable %s was not found", name);
87   xbt_assert(variable->name == name,
88     "Name mismatch for %s", name);
89   xbt_assert(variable->global, "Variable %s is not global", name);
90   xbt_assert(variable->address == address,
91       "Address mismatch for %s : %p expected but %p found",
92       name, address, 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   mc_type_t type = &i->second;
97   xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
98   return variable;
99 }
100
101 static mc_type_t find_member(mc_object_info_t info, const char* name, mc_type_t type)
102 {
103   for (simgrid::mc::Type& member : type->members)
104     if(member.name == name)
105       return &member;
106   return nullptr;
107 }
108
109 int some_local_variable = 0;
110
111 typedef struct foo {int i;} s_foo;
112
113 static void test_type_by_name(mc_process_t process, s_foo my_foo)
114 {
115   assert(
116     process->binary_info->full_types_by_name.find("struct foo") !=
117       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   mc_variable_t var;
125   mc_type_t type;
126
127   s_mc_process_t p(getpid(), -1);
128   mc_process_t process = &p;
129
130   test_global_variable(process, process->binary_info.get(),
131     "some_local_variable", &some_local_variable, sizeof(int));
132
133   var = test_global_variable(process, process->binary_info.get(),
134     "test_some_array", &test_some_array, sizeof(test_some_array));
135   auto i = process->binary_info->types.find(var->type_id);
136   xbt_assert(i != process->binary_info->types.end(), "Missing type");
137   type = &i->second;
138   xbt_assert(type->element_count == 6*5*4,
139     "element_count mismatch in test_some_array : %i / %i",
140     type->element_count, 6*5*4);
141
142   var = test_global_variable(process, process->binary_info.get(),
143     "test_some_struct", &test_some_struct, sizeof(test_some_struct));
144   i = process->binary_info->types.find(var->type_id);
145   xbt_assert(i != process->binary_info->types.end(), "Missing type");
146   type = &i->second;
147   assert(find_member(process->binary_info.get(), "first", type)->offset() == 0);
148   assert(find_member(process->binary_info.get(), "second", type)->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   {
159     int lexical_block_variable = 50;
160     test_local_variable(process->binary_info.get(), "main",
161       "lexical_block_variable", &lexical_block_variable, &cursor);
162   }
163
164   s_foo my_foo;
165   test_type_by_name(process, my_foo);
166
167   _exit(0);
168 }