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 mc_type_t find_type_by_name(mc_object_info_t info, const char* name) {
25 xbt_dict_cursor_t cursor = NULL;
28 xbt_dict_foreach(info->types, cursor, key, type)
29 if(type->name == name)
34 static dw_frame_t find_function_by_name(mc_object_info_t info, const char* name) {
35 xbt_dict_cursor_t cursor = 0;
36 dw_frame_t subprogram;
38 xbt_dict_foreach(info->subprograms, cursor, key, subprogram){
39 if(!strcmp(name, subprogram->name))
46 static dw_variable_t find_local_variable(dw_frame_t frame, const char* argument_name) {
47 unsigned int cursor = 0;
48 dw_variable_t variable;
49 xbt_dynar_foreach(frame->variables, cursor, variable){
50 if(!strcmp(argument_name, variable->name))
54 dw_frame_t scope = NULL;
55 xbt_dynar_foreach(frame->scopes, cursor, scope) {
56 variable = find_local_variable(scope, argument_name);
64 static void test_local_variable(mc_object_info_t info, const char* function, const char* variable, void* address, unw_cursor_t* cursor) {
65 dw_frame_t subprogram = find_function_by_name(info, function);
67 // TODO, Lookup frame by IP and test against name instead
69 dw_variable_t var = find_local_variable(subprogram, variable);
72 void* frame_base = mc_find_frame_base(subprogram, info, cursor);
73 s_mc_location_t location;
75 mc_dwarf_resolve_locations(&location,
76 &var->locations, info, cursor, frame_base, NULL, -1);
78 xbt_assert(mc_get_location_type(&location)==MC_LOCATION_TYPE_ADDRESS,
79 "Unexpected location type for variable %s of %s", variable, function);
81 xbt_assert(location.memory_location == address,
82 "Bad resolution of local variable %s of %s", variable, function);
86 static dw_variable_t test_global_variable(mc_process_t process, mc_object_info_t info, const char* name, void* address, long byte_size) {
88 dw_variable_t variable = info->find_variable(name);
89 xbt_assert(variable, "Global variable %s was not found", name);
90 xbt_assert(!strcmp(variable->name, name), "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", name, address, variable->address);
95 mc_type_t type = (mc_type_t) xbt_dict_get_or_null(process->binary_info->types, variable->type_origin);
96 xbt_assert(type!=NULL, "Missing type for %s", name);
97 xbt_assert(type->byte_size = byte_size, "Byte size mismatch for %s", name);
101 static mc_type_t find_member(mc_object_info_t info, const char* name, mc_type_t type) {
102 unsigned int cursor = 0;
104 xbt_dynar_foreach(type->members, cursor, member)
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(mc_process_t process, s_foo my_foo) {
115 assert(xbt_dict_get_or_null(process->binary_info->full_types_by_name, "struct foo"));
118 int main(int argc, char** argv)
120 SIMIX_global_init(&argc, argv);
125 s_mc_process_t p(getpid(), -1);
126 mc_process_t process = &p;
128 test_global_variable(process, process->binary_info.get(),
129 "some_local_variable", &some_local_variable, sizeof(int));
131 var = test_global_variable(process, process->binary_info.get(),
132 "test_some_array", &test_some_array, sizeof(test_some_array));
133 type = (mc_type_t) xbt_dict_get_or_null(process->binary_info->types, var->type_origin);
134 xbt_assert(type->element_count == 6*5*4, "element_count mismatch in test_some_array : %i / %i", type->element_count, 6*5*4);
136 var = test_global_variable(process, process->binary_info.get(),
137 "test_some_struct", &test_some_struct, sizeof(test_some_struct));
138 type = (mc_type_t) xbt_dict_get_or_null(process->binary_info->types, var->type_origin);
139 assert(find_member(process->binary_info.get(), "first", type)->offset() == 0);
140 assert(find_member(process->binary_info.get(), "second", type)->offset()
141 == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct);
143 unw_context_t context;
145 unw_getcontext(&context);
146 unw_init_local(&cursor, &context);
148 test_local_variable(process->binary_info.get(), "main", "argc", &argc, &cursor);
151 int lexical_block_variable = 50;
152 test_local_variable(process->binary_info.get(), "main",
153 "lexical_block_variable", &lexical_block_variable, &cursor);
157 test_type_by_name(process, my_foo);