Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branches 'master' and 'master' of github.com:simgrid/simgrid
[simgrid.git] / teshsuite / mc / dwarf / dwarf.cpp
1 /* Copyright (c) 2014-2015. 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 <mc/mc.h>
15
16 #include "mc/datatypes.h"
17 #include "src/mc/mc_private.h"
18
19 #include "src/mc/Process.hpp"
20 #include "src/mc/Type.hpp"
21 #include "src/mc/ObjectInformation.hpp"
22 #include "src/mc/Variable.hpp"
23
24 int test_some_array[4][5][6];
25 struct some_struct {
26   int first;
27   int second[4][5];
28 } test_some_struct;
29
30 static simgrid::mc::Type* find_type_by_name(simgrid::mc::ObjectInformation* info, const char* name)
31 {
32   for (auto& entry : info->types)
33     if(entry.second.name == name)
34       return &entry.second;
35   return nullptr;
36 }
37
38 static simgrid::mc::Frame* find_function_by_name(
39     simgrid::mc::ObjectInformation* info, const char* name)
40 {
41   for (auto& entry : info->subprograms)
42     if(entry.second.name == name)
43       return &entry.second;
44   return nullptr;
45 }
46
47 static simgrid::mc::Variable* find_local_variable(
48     simgrid::mc::Frame* frame, const char* argument_name)
49 {
50   for (simgrid::mc::Variable& variable : frame->variables)
51     if(argument_name == variable.name)
52       return &variable;
53
54   for (simgrid::mc::Frame& scope : frame->scopes) {
55     simgrid::mc::Variable* variable = find_local_variable(
56       &scope, argument_name);
57     if(variable)
58       return variable;
59   }
60
61   return nullptr;
62 }
63
64 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
65   simgrid::mc::Frame* subprogram = find_function_by_name(info, function);
66   assert(subprogram);
67   // TODO, Lookup frame by IP and test against name instead
68
69   simgrid::mc::Variable* var = find_local_variable(subprogram, variable);
70   assert(var);
71
72   void* frame_base = subprogram->frame_base(*cursor);
73   simgrid::dwarf::Location location = simgrid::dwarf::resolve(
74     var->location_list, info, cursor, frame_base, nullptr, -1);
75
76   xbt_assert(location.in_memory(), "Expected the variable %s of function %s to be in memory", variable, function);
77   xbt_assert(location.address() == address, "Bad resolution of local variable %s of %s", variable, function);
78 }
79
80 static simgrid::mc::Variable* test_global_variable(
81   simgrid::mc::Process& process, simgrid::mc::ObjectInformation* info,
82   const char* name, void* address, long byte_size)
83 {
84   simgrid::mc::Variable* variable = info->find_variable(name);
85   xbt_assert(variable, "Global variable %s was not found", name);
86   xbt_assert(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, "Address mismatch for %s : %p expected but %p found", name, address,
89              variable->address);
90
91   auto i = process.binary_info->types.find(variable->type_id);
92   xbt_assert(i != process.binary_info->types.end(), "Missing type for %s", name);
93   simgrid::mc::Type* type = &i->second;
94   xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
95   return variable;
96 }
97
98 static simgrid::mc::Member* find_member(simgrid::mc::Type& type, const char* name)
99 {
100   for (simgrid::mc::Member& member : type.members)
101     if(member.name == name)
102       return &member;
103   return nullptr;
104 }
105
106 int some_local_variable = 0;
107
108 typedef struct foo {int i;} s_foo;
109
110 static void test_type_by_name(simgrid::mc::Process& process, s_foo my_foo)
111 {
112   assert(process.binary_info->full_types_by_name.find("struct foo") != process.binary_info->full_types_by_name.end());
113 }
114
115 int main(int argc, char** argv)
116 {
117   SIMIX_global_init(&argc, argv);
118
119   simgrid::mc::Variable* var;
120   simgrid::mc::Type* type;
121
122   simgrid::mc::Process process(getpid(), -1);
123   process.init();
124
125   test_global_variable(process, process.binary_info.get(), "some_local_variable", &some_local_variable, sizeof(int));
126
127   var = test_global_variable(process, process.binary_info.get(), "test_some_array", &test_some_array,
128                              sizeof(test_some_array));
129   auto i = process.binary_info->types.find(var->type_id);
130   xbt_assert(i != process.binary_info->types.end(), "Missing type");
131   type = &i->second;
132   xbt_assert(type->element_count == 6 * 5 * 4, "element_count mismatch in test_some_array : %i / %i",
133              type->element_count, 6 * 5 * 4);
134
135   var = test_global_variable(process, process.binary_info.get(), "test_some_struct", &test_some_struct,
136                              sizeof(test_some_struct));
137   i = process.binary_info->types.find(var->type_id);
138   xbt_assert(i != process.binary_info->types.end(), "Missing type");
139   type = &i->second;
140
141   assert(type);
142   assert(find_member(*type, "first")->offset() == 0);
143   assert(find_member(*type, "second")->offset() ==
144          ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
145
146   unw_context_t context;
147   unw_cursor_t cursor;
148   unw_getcontext(&context);
149   unw_init_local(&cursor, &context);
150
151   test_local_variable(process.binary_info.get(), "main", "argc", &argc, &cursor);
152
153   int lexical_block_variable = 50;
154   test_local_variable(process.binary_info.get(), "main", "lexical_block_variable", &lexical_block_variable, &cursor);
155
156   s_foo my_foo;
157   test_type_by_name(process, my_foo);
158
159   _exit(0);
160 }