Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix DW_OP_addr to translate address into virtual address space
[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   xbt_dict_cursor_t cursor = 0;
42   dw_frame_t subprogram;
43   char* key;
44   xbt_dict_foreach(info->subprograms, cursor, key, subprogram){
45     if(!strcmp(name, subprogram->name))
46       return subprogram;
47   }
48
49   return NULL;
50 }
51
52 static dw_variable_t find_local_variable(dw_frame_t frame, const char* argument_name) {
53   unsigned int cursor = 0;
54   dw_variable_t variable;
55   xbt_dynar_foreach(frame->variables, cursor, variable){
56     if(!strcmp(argument_name, variable->name))
57       return variable;
58   }
59
60   return NULL;
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   dw_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   dw_variable_t var = find_local_variable(subprogram, variable);
69   assert(var);
70
71   void* frame_base = mc_find_frame_base(subprogram, info, cursor);
72   xbt_assert((void*)mc_dwarf_resolve_locations(&var->locations, info, cursor, frame_base, NULL) == address,
73     "Bad resolution of local variable %s of %s", variable, function);
74
75 }
76
77 static dw_variable_t test_global_variable(mc_object_info_t info, const char* name, void* address, long byte_size) {
78   dw_variable_t variable = find_global_variable_by_name(info, name);
79   xbt_assert(variable, "Global variable %s was not found", name);
80   xbt_assert(!strcmp(variable->name, name), "Name mismatch for %s", name);
81   xbt_assert(variable->global, "Variable %s is not global", name);
82   xbt_assert(variable->address == address,
83       "Address mismatch for %s : %p expected but %p found", name, address, variable->address);
84
85   dw_type_t type = xbt_dict_get_or_null(mc_binary_info->types, variable->type_origin);
86   xbt_assert(type!=NULL, "Missing type for %s", name);
87   xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
88   return variable;
89 }
90
91 static dw_type_t find_member(mc_object_info_t info, const char* name, dw_type_t type) {
92   unsigned int cursor = 0;
93   dw_type_t member;
94   xbt_dynar_foreach(type->members, cursor, member){
95     if(!strcmp(name,member->name))
96       return member;
97   }
98   return NULL;
99 }
100
101 int some_local_variable = 0;
102
103 typedef struct foo {int i;} s_foo;
104
105 static void test_type_by_name(s_foo my_foo) {
106   assert(xbt_dict_get_or_null(mc_binary_info->full_types_by_name, "struct foo"));
107 }
108
109 int main(int argc, char** argv) {
110
111   // xbt_init(&argc, argv);
112   SIMIX_global_init(&argc, argv);
113   MC_memory_init();
114   MC_init();
115
116   dw_variable_t var;
117   dw_type_t type;
118
119   var = test_global_variable(mc_binary_info, "some_local_variable", &some_local_variable, sizeof(int));
120
121   var = test_global_variable(mc_binary_info, "test_some_array", &test_some_array, sizeof(test_some_array));
122   type = xbt_dict_get_or_null(mc_binary_info->types, var->type_origin);
123   xbt_assert(type->element_count == 6*5*4, "element_count mismatch in test_some_array : %i / %i", type->element_count, 6*5*4);
124
125   var = test_global_variable(mc_binary_info, "test_some_struct", &test_some_struct, sizeof(test_some_struct));
126   type = xbt_dict_get_or_null(mc_binary_info->types, var->type_origin);
127   assert(find_member(mc_binary_info, "first", type)->offset == 0);
128   assert(find_member(mc_binary_info, "second", type)->offset
129       == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
130
131   unw_context_t context;
132   unw_cursor_t cursor;
133   unw_getcontext(&context);
134   unw_init_local(&cursor, &context);
135
136   test_local_variable(mc_binary_info, "main", "argc", &argc, &cursor);
137
138   s_foo my_foo;
139   test_type_by_name(my_foo);
140
141   _exit(0);
142 }