Logo AND Algorithmique Numérique Distribuée

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