Logo AND Algorithmique Numérique Distribuée

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