1 /* Copyright (c) 2014. 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. */
17 #include "../../src/include/mc/datatypes.h"
18 #include "../../src/mc/mc_object_info.h"
19 #include "../../src/mc/mc_private.h"
21 int test_some_array[4][5][6];
22 struct some_struct { int first; int second[4][5]; } test_some_struct;
24 static simgrid::mc::Type* find_type_by_name(simgrid::mc::ObjectInformation* info, const char* name)
26 for (auto& entry : info->types)
27 if(entry.second.name == name)
32 static simgrid::mc::Frame* find_function_by_name(
33 simgrid::mc::ObjectInformation* info, const char* name)
35 for (auto& entry : info->subprograms)
36 if(entry.second.name == name)
41 static simgrid::mc::Variable* find_local_variable(
42 simgrid::mc::Frame* frame, const char* argument_name)
44 for (simgrid::mc::Variable& variable : frame->variables)
45 if(argument_name == variable.name)
48 for (simgrid::mc::Frame& scope : frame->scopes) {
49 simgrid::mc::Variable* variable = find_local_variable(
50 &scope, argument_name);
58 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
59 simgrid::mc::Frame* subprogram = find_function_by_name(info, function);
61 // TODO, Lookup frame by IP and test against name instead
63 simgrid::mc::Variable* var = find_local_variable(subprogram, variable);
66 void* frame_base = mc_find_frame_base(subprogram, info, cursor);
67 s_mc_location_t location;
69 mc_dwarf_resolve_locations(&location,
70 &var->location_list, info, cursor, frame_base, NULL, -1);
72 xbt_assert(mc_get_location_type(&location)==MC_LOCATION_TYPE_ADDRESS,
73 "Unexpected location type for variable %s of %s", variable, function);
75 xbt_assert(location.memory_location == address,
76 "Bad resolution of local variable %s of %s", variable, function);
80 static simgrid::mc::Variable* test_global_variable(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info, const char* name, void* address, long byte_size) {
82 simgrid::mc::Variable* variable = info->find_variable(name);
83 xbt_assert(variable, "Global variable %s was not found", name);
84 xbt_assert(variable->name == name,
85 "Name mismatch for %s", name);
86 xbt_assert(variable->global, "Variable %s is not global", name);
87 xbt_assert(variable->address == address,
88 "Address mismatch for %s : %p expected but %p found",
89 name, address, variable->address);
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);
98 static simgrid::mc::Type* find_member(simgrid::mc::ObjectInformation* info, const char* name, simgrid::mc::Type* type)
100 for (simgrid::mc::Type& member : type->members)
101 if(member.name == name)
106 int some_local_variable = 0;
108 typedef struct foo {int i;} s_foo;
110 static void test_type_by_name(simgrid::mc::Process* process, s_foo my_foo)
113 process->binary_info->full_types_by_name.find("struct foo") !=
114 process->binary_info->full_types_by_name.end());
117 int main(int argc, char** argv)
119 SIMIX_global_init(&argc, argv);
121 simgrid::mc::Variable* var;
122 simgrid::mc::Type* type;
124 simgrid::mc::Process p(getpid(), -1);
125 simgrid::mc::Process* process = &p;
127 test_global_variable(process, process->binary_info.get(),
128 "some_local_variable", &some_local_variable, sizeof(int));
130 var = test_global_variable(process, process->binary_info.get(),
131 "test_some_array", &test_some_array, sizeof(test_some_array));
132 auto i = process->binary_info->types.find(var->type_id);
133 xbt_assert(i != process->binary_info->types.end(), "Missing type");
135 xbt_assert(type->element_count == 6*5*4,
136 "element_count mismatch in test_some_array : %i / %i",
137 type->element_count, 6*5*4);
139 var = test_global_variable(process, process->binary_info.get(),
140 "test_some_struct", &test_some_struct, sizeof(test_some_struct));
141 i = process->binary_info->types.find(var->type_id);
142 xbt_assert(i != process->binary_info->types.end(), "Missing type");
144 assert(find_member(process->binary_info.get(), "first", type)->offset() == 0);
145 assert(find_member(process->binary_info.get(), "second", type)->offset()
146 == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
148 unw_context_t context;
150 unw_getcontext(&context);
151 unw_init_local(&cursor, &context);
153 test_local_variable(process->binary_info.get(), "main", "argc", &argc, &cursor);
156 int lexical_block_variable = 50;
157 test_local_variable(process->binary_info.get(), "main",
158 "lexical_block_variable", &lexical_block_variable, &cursor);
162 test_type_by_name(process, my_foo);