Logo AND Algorithmique Numérique Distribuée

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