Logo AND Algorithmique Numérique Distribuée

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