Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Store abstract subprograms in info->subprograms
[simgrid.git] / testsuite / mc / dwarf.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <string.h>
6 #include <assert.h>
7
8 #include <xbt.h>
9 #include <mc/mc.h>
10
11 #include "../../src/include/mc/datatypes.h"
12 #include "../../src/mc/mc_private.h"
13
14 int test_some_array[4][5][6];
15 struct some_struct { int first; int second[4][5]; } test_some_struct;
16
17 static dw_type_t find_type_by_name(mc_object_info_t info, const char* name) {
18   xbt_dict_cursor_t cursor = NULL;
19   char *key;
20   dw_type_t type;
21   xbt_dict_foreach(info->types, cursor, key, type) {
22     if(!strcmp(name, type->name))
23       return type;
24   }
25
26   return NULL;
27 }
28
29 static dw_variable_t find_global_variable_by_name(mc_object_info_t info, const char* name) {
30   unsigned int cursor = 0;
31   dw_variable_t variable;
32   xbt_dynar_foreach(info->global_variables, cursor, variable){
33     if(!strcmp(name, variable->name))
34       return variable;
35   }
36
37   return NULL;
38 }
39
40 static dw_frame_t find_function_by_name(mc_object_info_t info, const char* name) {
41   unsigned int cursor = 0;
42   dw_frame_t subprogram;
43   xbt_dynar_foreach(info->subprograms, cursor, subprogram){
44     if(!strcmp(name, subprogram->name))
45       return subprogram;
46   }
47
48   return NULL;
49 }
50
51 static dw_variable_t find_local_variable(dw_frame_t frame, const char* argument_name) {
52   unsigned int cursor = 0;
53   dw_variable_t variable;
54   xbt_dynar_foreach(frame->variables, cursor, variable){
55     if(!strcmp(argument_name, variable->name))
56       return variable;
57   }
58
59   return NULL;
60 }
61
62 static void test_local_variable(mc_object_info_t info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
63   dw_frame_t subprogram = find_function_by_name(info, function);
64   assert(subprogram);
65   // TODO, Lookup frame by IP and test against name instead
66
67   dw_variable_t var = find_local_variable(subprogram, variable);
68   assert(var);
69
70   void* frame_base = mc_find_frame_base(subprogram, cursor);
71   xbt_assert((void*)mc_dwarf_resolve_locations(&var->locations, cursor, frame_base, NULL) == address,
72     "Bad resolution of local variable %s of %s", variable, function);
73
74 }
75
76 static dw_variable_t test_global_variable(mc_object_info_t info, const char* name, void* address, long byte_size) {
77   dw_variable_t variable = find_global_variable_by_name(info, name);
78   xbt_assert(variable, "Global variable %s was not found", name);
79   xbt_assert(!strcmp(variable->name, name), "Name mismatch for %s", name);
80   xbt_assert(variable->global, "Variable %s is not global", name);
81   xbt_assert(variable->address == address,
82       "Address mismatch for %s : %p expected but %p found", name, address, variable->address);
83
84   dw_type_t type = xbt_dict_get_or_null(mc_binary_info->types, variable->type_origin);
85   xbt_assert(type!=NULL, "Missing type for %s", name);
86   xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
87   return variable;
88 }
89
90 static dw_type_t find_member(mc_object_info_t info, const char* name, dw_type_t type) {
91   unsigned int cursor = 0;
92   dw_type_t member;
93   xbt_dynar_foreach(type->members, cursor, member){
94     if(!strcmp(name,member->name))
95       return member;
96   }
97   return NULL;
98 }
99
100 int some_local_variable = 0;
101
102 typedef struct foo {int i;} s_foo;
103
104 static void test_type_by_name(s_foo my_foo) {
105   assert(xbt_dict_get_or_null(mc_binary_info->full_types_by_name, "struct foo"));
106 }
107
108 int main(int argc, char** argv) {
109
110   // xbt_init(&argc, argv);
111   SIMIX_global_init(&argc, argv);
112   MC_memory_init();
113   MC_init();
114
115   dw_variable_t var;
116   dw_type_t type;
117
118   var = test_global_variable(mc_binary_info, "some_local_variable", &some_local_variable, sizeof(int));
119
120   var = test_global_variable(mc_binary_info, "test_some_array", &test_some_array, sizeof(test_some_array));
121   type = xbt_dict_get_or_null(mc_binary_info->types, var->type_origin);
122   xbt_assert(type->element_count == 6*5*4, "element_count mismatch in test_some_array : %i / %i", type->element_count, 6*5*4);
123
124   var = test_global_variable(mc_binary_info, "test_some_struct", &test_some_struct, sizeof(test_some_struct));
125   type = xbt_dict_get_or_null(mc_binary_info->types, var->type_origin);
126   assert(find_member(mc_binary_info, "first", type)->offset == 0);
127   assert(find_member(mc_binary_info, "second", type)->offset
128       == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
129
130   unw_context_t context;
131   unw_cursor_t cursor;
132   unw_getcontext(&context);
133   unw_init_local(&cursor, &context);
134
135   test_local_variable(mc_binary_info, "main", "argc", &argc, &cursor);
136
137   s_foo my_foo;
138   test_type_by_name(my_foo);
139
140   _exit(0);
141 }