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. */
17 #include "mc/datatypes.h"
18 #include "mc/mc_object_info.h"
19 #include "mc/mc_private.h"
21 #include "mc/Process.hpp"
22 #include "mc/Type.hpp"
23 #include "mc/ObjectInformation.hpp"
24 #include "mc/Variable.hpp"
26 int test_some_array[4][5][6];
27 struct some_struct { int first; int second[4][5]; } test_some_struct;
29 static simgrid::mc::Type* find_type_by_name(simgrid::mc::ObjectInformation* info, const char* name)
31 for (auto& entry : info->types)
32 if(entry.second.name == name)
37 static simgrid::mc::Frame* find_function_by_name(
38 simgrid::mc::ObjectInformation* info, const char* name)
40 for (auto& entry : info->subprograms)
41 if(entry.second.name == name)
46 static simgrid::mc::Variable* find_local_variable(
47 simgrid::mc::Frame* frame, const char* argument_name)
49 for (simgrid::mc::Variable& variable : frame->variables)
50 if(argument_name == variable.name)
53 for (simgrid::mc::Frame& scope : frame->scopes) {
54 simgrid::mc::Variable* variable = find_local_variable(
55 &scope, argument_name);
63 static void test_local_variable(simgrid::mc::ObjectInformation* info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
64 simgrid::mc::Frame* subprogram = find_function_by_name(info, function);
66 // TODO, Lookup frame by IP and test against name instead
68 simgrid::mc::Variable* var = find_local_variable(subprogram, variable);
71 void* frame_base = mc_find_frame_base(subprogram, info, cursor);
72 s_mc_location_t location;
74 mc_dwarf_resolve_locations(&location,
75 &var->location_list, info, cursor, frame_base, NULL, -1);
77 xbt_assert(mc_get_location_type(&location)==MC_LOCATION_TYPE_ADDRESS,
78 "Unexpected location type for variable %s of %s", variable, function);
80 xbt_assert(location.memory_location == address,
81 "Bad resolution of local variable %s of %s", variable, function);
85 static simgrid::mc::Variable* test_global_variable(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info, const char* name, void* address, long byte_size) {
87 simgrid::mc::Variable* variable = info->find_variable(name);
88 xbt_assert(variable, "Global variable %s was not found", name);
89 xbt_assert(variable->name == name,
90 "Name mismatch for %s", name);
91 xbt_assert(variable->global, "Variable %s is not global", name);
92 xbt_assert(variable->address == address,
93 "Address mismatch for %s : %p expected but %p found",
94 name, address, variable->address);
96 auto i = process->binary_info->types.find(variable->type_id);
97 xbt_assert(i != process->binary_info->types.end(), "Missing type for %s", name);
98 simgrid::mc::Type* type = &i->second;
99 xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
103 static simgrid::mc::Member* find_member(simgrid::mc::Type& type, const char* name)
105 for (simgrid::mc::Member& member : type.members)
106 if(member.name == name)
111 int some_local_variable = 0;
113 typedef struct foo {int i;} s_foo;
115 static void test_type_by_name(simgrid::mc::Process* process, s_foo my_foo)
118 process->binary_info->full_types_by_name.find("struct foo") !=
119 process->binary_info->full_types_by_name.end());
122 int main(int argc, char** argv)
124 SIMIX_global_init(&argc, argv);
126 simgrid::mc::Variable* var;
127 simgrid::mc::Type* type;
129 simgrid::mc::Process p(getpid(), -1);
130 simgrid::mc::Process* process = &p;
132 test_global_variable(process, process->binary_info.get(),
133 "some_local_variable", &some_local_variable, sizeof(int));
135 var = test_global_variable(process, process->binary_info.get(),
136 "test_some_array", &test_some_array, sizeof(test_some_array));
137 auto i = process->binary_info->types.find(var->type_id);
138 xbt_assert(i != process->binary_info->types.end(), "Missing type");
140 xbt_assert(type->element_count == 6*5*4,
141 "element_count mismatch in test_some_array : %i / %i",
142 type->element_count, 6*5*4);
144 var = test_global_variable(process, process->binary_info.get(),
145 "test_some_struct", &test_some_struct, sizeof(test_some_struct));
146 i = process->binary_info->types.find(var->type_id);
147 xbt_assert(i != process->binary_info->types.end(), "Missing type");
151 assert(find_member(*type, "first")->offset() == 0);
152 assert(find_member(*type, "second")->offset()
153 == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
155 unw_context_t context;
157 unw_getcontext(&context);
158 unw_init_local(&cursor, &context);
160 test_local_variable(process->binary_info.get(), "main", "argc", &argc, &cursor);
163 int lexical_block_variable = 50;
164 test_local_variable(process->binary_info.get(), "main",
165 "lexical_block_variable", &lexical_block_variable, &cursor);
169 test_type_by_name(process, my_foo);