Logo AND Algorithmique Numérique Distribuée

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