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"
20 #include "../../src/mc/mc_model_checker.h"
22 int test_some_array[4][5][6];
23 struct some_struct { int first; int second[4][5]; } test_some_struct;
25 static dw_type_t find_type_by_name(mc_object_info_t info, const char* name) {
26 xbt_dict_cursor_t cursor = NULL;
29 xbt_dict_foreach(info->types, cursor, key, type) {
30 if(!strcmp(name, type->name))
37 static dw_variable_t find_global_variable_by_name(mc_object_info_t info, const char* name) {
38 unsigned int cursor = 0;
39 dw_variable_t variable;
40 xbt_dynar_foreach(info->global_variables, cursor, variable){
41 if(!strcmp(name, variable->name))
48 static dw_frame_t find_function_by_name(mc_object_info_t info, const char* name) {
49 xbt_dict_cursor_t cursor = 0;
50 dw_frame_t subprogram;
52 xbt_dict_foreach(info->subprograms, cursor, key, subprogram){
53 if(!strcmp(name, subprogram->name))
60 static dw_variable_t find_local_variable(dw_frame_t frame, const char* argument_name) {
61 unsigned int cursor = 0;
62 dw_variable_t variable;
63 xbt_dynar_foreach(frame->variables, cursor, variable){
64 if(!strcmp(argument_name, variable->name))
68 dw_frame_t scope = NULL;
69 xbt_dynar_foreach(frame->scopes, cursor, scope) {
70 variable = find_local_variable(scope, argument_name);
78 static void test_local_variable(mc_object_info_t info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
79 dw_frame_t subprogram = find_function_by_name(info, function);
81 // TODO, Lookup frame by IP and test against name instead
83 dw_variable_t var = find_local_variable(subprogram, variable);
86 void* frame_base = mc_find_frame_base(subprogram, info, cursor);
87 s_mc_location_t location;
89 mc_dwarf_resolve_locations(&location,
90 &var->locations, info, cursor, frame_base, NULL, -1);
92 xbt_assert(mc_get_location_type(&location)==MC_LOCATION_TYPE_ADDRESS,
93 "Unexpected location type for variable %s of %s", variable, function);
95 xbt_assert(location.memory_location == address,
96 "Bad resolution of local variable %s of %s", variable, function);
100 static dw_variable_t test_global_variable(mc_object_info_t info, const char* name, void* address, long byte_size) {
101 mc_process_t process = &mc_model_checker->process;
103 dw_variable_t variable = find_global_variable_by_name(info, name);
104 xbt_assert(variable, "Global variable %s was not found", name);
105 xbt_assert(!strcmp(variable->name, name), "Name mismatch for %s", name);
106 xbt_assert(variable->global, "Variable %s is not global", name);
107 xbt_assert(variable->address == address,
108 "Address mismatch for %s : %p expected but %p found", name, address, variable->address);
110 dw_type_t type = xbt_dict_get_or_null(process->binary_info->types, variable->type_origin);
111 xbt_assert(type!=NULL, "Missing type for %s", name);
112 xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
116 static dw_type_t find_member(mc_object_info_t info, const char* name, dw_type_t type) {
117 unsigned int cursor = 0;
119 xbt_dynar_foreach(type->members, cursor, member){
120 if(!strcmp(name,member->name))
126 int some_local_variable = 0;
128 typedef struct foo {int i;} s_foo;
130 static void test_type_by_name(s_foo my_foo) {
131 mc_process_t process = &mc_model_checker->process;
132 assert(xbt_dict_get_or_null(process->binary_info->full_types_by_name, "struct foo"));
135 int main(int argc, char** argv) {
137 // xbt_init(&argc, argv);
138 SIMIX_global_init(&argc, argv);
145 mc_process_t process = &mc_model_checker->process;
147 test_global_variable(process->binary_info, "some_local_variable", &some_local_variable, sizeof(int));
149 var = test_global_variable(process->binary_info, "test_some_array", &test_some_array, sizeof(test_some_array));
150 type = xbt_dict_get_or_null(process->binary_info->types, var->type_origin);
151 xbt_assert(type->element_count == 6*5*4, "element_count mismatch in test_some_array : %i / %i", type->element_count, 6*5*4);
153 var = test_global_variable(process->binary_info, "test_some_struct", &test_some_struct, sizeof(test_some_struct));
154 type = xbt_dict_get_or_null(process->binary_info->types, var->type_origin);
155 assert(find_member(process->binary_info, "first", type)->offset == 0);
156 assert(find_member(process->binary_info, "second", type)->offset
157 == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
159 unw_context_t context;
161 unw_getcontext(&context);
162 unw_init_local(&cursor, &context);
164 test_local_variable(process->binary_info, "main", "argc", &argc, &cursor);
167 int lexical_block_variable = 50;
168 test_local_variable(process->binary_info, "main", "lexical_block_variable", &lexical_block_variable, &cursor);
172 test_type_by_name(my_foo);