Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright headers.
[simgrid.git] / testsuite / mc / 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   return NULL;
67 }
68
69 static void test_local_variable(mc_object_info_t info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
70   dw_frame_t subprogram = find_function_by_name(info, function);
71   assert(subprogram);
72   // TODO, Lookup frame by IP and test against name instead
73
74   dw_variable_t var = find_local_variable(subprogram, variable);
75   assert(var);
76
77   void* frame_base = mc_find_frame_base(subprogram, info, cursor);
78   xbt_assert((void*)mc_dwarf_resolve_locations(&var->locations, info, cursor, frame_base, NULL) == address,
79     "Bad resolution of local variable %s of %s", variable, function);
80
81 }
82
83 static dw_variable_t test_global_variable(mc_object_info_t info, const char* name, void* address, long byte_size) {
84   dw_variable_t variable = find_global_variable_by_name(info, name);
85   xbt_assert(variable, "Global variable %s was not found", name);
86   xbt_assert(!strcmp(variable->name, name), "Name mismatch for %s", name);
87   xbt_assert(variable->global, "Variable %s is not global", name);
88   xbt_assert(variable->address == address,
89       "Address mismatch for %s : %p expected but %p found", name, address, variable->address);
90
91   dw_type_t type = xbt_dict_get_or_null(mc_binary_info->types, variable->type_origin);
92   xbt_assert(type!=NULL, "Missing type for %s", name);
93   xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
94   return variable;
95 }
96
97 static dw_type_t find_member(mc_object_info_t info, const char* name, dw_type_t type) {
98   unsigned int cursor = 0;
99   dw_type_t member;
100   xbt_dynar_foreach(type->members, cursor, member){
101     if(!strcmp(name,member->name))
102       return member;
103   }
104   return NULL;
105 }
106
107 int some_local_variable = 0;
108
109 typedef struct foo {int i;} s_foo;
110
111 static void test_type_by_name(s_foo my_foo) {
112   assert(xbt_dict_get_or_null(mc_binary_info->full_types_by_name, "struct foo"));
113 }
114
115 int main(int argc, char** argv) {
116
117   // xbt_init(&argc, argv);
118   SIMIX_global_init(&argc, argv);
119   MC_memory_init();
120   MC_init();
121
122   dw_variable_t var;
123   dw_type_t type;
124
125   var = test_global_variable(mc_binary_info, "some_local_variable", &some_local_variable, sizeof(int));
126
127   var = test_global_variable(mc_binary_info, "test_some_array", &test_some_array, sizeof(test_some_array));
128   type = xbt_dict_get_or_null(mc_binary_info->types, var->type_origin);
129   xbt_assert(type->element_count == 6*5*4, "element_count mismatch in test_some_array : %i / %i", type->element_count, 6*5*4);
130
131   var = test_global_variable(mc_binary_info, "test_some_struct", &test_some_struct, sizeof(test_some_struct));
132   type = xbt_dict_get_or_null(mc_binary_info->types, var->type_origin);
133   assert(find_member(mc_binary_info, "first", type)->offset == 0);
134   assert(find_member(mc_binary_info, "second", type)->offset
135       == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
136
137   unw_context_t context;
138   unw_cursor_t cursor;
139   unw_getcontext(&context);
140   unw_init_local(&cursor, &context);
141
142   test_local_variable(mc_binary_info, "main", "argc", &argc, &cursor);
143
144   s_foo my_foo;
145   test_type_by_name(my_foo);
146
147   _exit(0);
148 }