1 /* Copyright (c) 2014-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
16 #include "mc/datatypes.h"
17 #include "src/mc/mc_object_info.h"
18 #include "src/mc/mc_private.h"
20 #include "src/mc/Process.hpp"
21 #include "src/mc/Type.hpp"
22 #include "src/mc/ObjectInformation.hpp"
23 #include "src/mc/Variable.hpp"
25 int test_some_array[4][5][6];
26 struct some_struct { int first; int second[4][5]; } test_some_struct;
28 static simgrid::mc::Type* find_type_by_name(simgrid::mc::ObjectInformation* info, const char* name)
30 for (auto& entry : info->types)
31 if(entry.second.name == name)
36 static simgrid::mc::Frame* find_function_by_name(
37 simgrid::mc::ObjectInformation* info, const char* name)
39 for (auto& entry : info->subprograms)
40 if(entry.second.name == name)
45 static simgrid::mc::Variable* find_local_variable(
46 simgrid::mc::Frame* frame, const char* argument_name)
48 for (simgrid::mc::Variable& variable : frame->variables)
49 if(argument_name == variable.name)
52 for (simgrid::mc::Frame& scope : frame->scopes) {
53 simgrid::mc::Variable* variable = find_local_variable(
54 &scope, argument_name);
62 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
63 simgrid::mc::Frame* subprogram = find_function_by_name(info, function);
65 // TODO, Lookup frame by IP and test against name instead
67 simgrid::mc::Variable* var = find_local_variable(subprogram, variable);
70 void* frame_base = subprogram->frame_base(*cursor);
71 simgrid::dwarf::Location location = simgrid::dwarf::resolve(
72 var->location_list, info, cursor, frame_base, NULL, -1);
74 xbt_assert(location.in_memory(),
75 "Expected the variable %s of function %s to be in memory",
77 xbt_assert(location.address() == address,
78 "Bad resolution of local variable %s of %s", variable, function);
82 static simgrid::mc::Variable* test_global_variable(
83 simgrid::mc::Process& process, simgrid::mc::ObjectInformation* info,
84 const char* name, void* address, long byte_size)
86 simgrid::mc::Variable* variable = info->find_variable(name);
87 xbt_assert(variable, "Global variable %s was not found", name);
88 xbt_assert(variable->name == name,
89 "Name mismatch for %s", name);
90 xbt_assert(variable->global, "Variable %s is not global", name);
91 xbt_assert(variable->address == address,
92 "Address mismatch for %s : %p expected but %p found",
93 name, address, variable->address);
95 auto i = process.binary_info->types.find(variable->type_id);
96 xbt_assert(i != process.binary_info->types.end(), "Missing type for %s", name);
97 simgrid::mc::Type* type = &i->second;
98 xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
102 static simgrid::mc::Member* find_member(simgrid::mc::Type& type, const char* name)
104 for (simgrid::mc::Member& member : type.members)
105 if(member.name == name)
110 int some_local_variable = 0;
112 typedef struct foo {int i;} s_foo;
114 static void test_type_by_name(simgrid::mc::Process& process, s_foo my_foo)
117 process.binary_info->full_types_by_name.find("struct foo") !=
118 process.binary_info->full_types_by_name.end());
121 int main(int argc, char** argv)
123 SIMIX_global_init(&argc, argv);
125 simgrid::mc::Variable* var;
126 simgrid::mc::Type* type;
128 simgrid::mc::Process process(getpid(), -1);
131 test_global_variable(process, process.binary_info.get(),
132 "some_local_variable", &some_local_variable, sizeof(int));
134 var = test_global_variable(process, process.binary_info.get(),
135 "test_some_array", &test_some_array, sizeof(test_some_array));
136 auto i = process.binary_info->types.find(var->type_id);
137 xbt_assert(i != process.binary_info->types.end(), "Missing type");
139 xbt_assert(type->element_count == 6*5*4,
140 "element_count mismatch in test_some_array : %i / %i",
141 type->element_count, 6*5*4);
143 var = test_global_variable(process, process.binary_info.get(),
144 "test_some_struct", &test_some_struct, sizeof(test_some_struct));
145 i = process.binary_info->types.find(var->type_id);
146 xbt_assert(i != process.binary_info->types.end(), "Missing type");
150 assert(find_member(*type, "first")->offset() == 0);
151 assert(find_member(*type, "second")->offset()
152 == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
154 unw_context_t context;
156 unw_getcontext(&context);
157 unw_init_local(&cursor, &context);
159 test_local_variable(process.binary_info.get(), "main", "argc", &argc, &cursor);
162 int lexical_block_variable = 50;
163 test_local_variable(process.binary_info.get(), "main",
164 "lexical_block_variable", &lexical_block_variable, &cursor);
168 test_type_by_name(process, my_foo);