Logo AND Algorithmique Numérique Distribuée

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