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. */
10 #include "mc_private.h"
11 #include "mc/datatypes.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_hash, mc,
15 "Logging specific to mc_hash");
18 typedef uint64_t mc_hash_t;
19 #define MC_HASH_INIT ((uint64_t)5381)
21 // #define MC_HASH(hash, value) hash = (((hash << 5) + hash) + (uint64_t) value)
22 #define MC_HASH(hash, value) \
23 { hash = (((hash << 5) + hash) + (uint64_t) value);\
24 XBT_DEBUG("%s:%i: %"PRIx64" -> %"PRIx64, __FILE__, __LINE__, (uint64_t) value, hash); }
28 typedef struct s_mc_hashing_state {
29 // Set of pointers/addresses already processed (avoid loops):
30 mc_address_set_t handled_addresses;
33 void mc_hash_state_init(mc_hashing_state* state);
34 void mc_hash_state_destroy(mc_hashing_state* state);
36 void mc_hash_state_init(mc_hashing_state* state) {
37 state->handled_addresses = mc_address_set_new();
40 void mc_hash_state_destroy(mc_hashing_state* state) {
41 mc_address_set_free(&state->handled_addresses);
44 // TODO, detect and avoid loops
46 static bool mc_ignored(const void* address, size_t size) {
47 mc_heap_ignore_region_t region;
48 unsigned int cursor = 0;
49 const void* end = (char*) address + size;
50 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, region) {
51 void* istart = region->address;
52 void* iend = (char*) region->address + region->size;
54 if(address>=istart && address<iend && end>=istart && end<iend)
61 static void mc_hash_binary(mc_hash_t *hash, const void* s, size_t len) {
62 const char* p = (const void*) s;
64 for(i=0; i!=len; ++i) {
69 /** \brief Compute a hash for a given value of a given type
71 * We try to be very conservative (do not hash too ambiguous things).
73 * \param address address of the variable
74 * \param type type of the variable
76 static void mc_hash_value(mc_hash_t* hash, mc_hashing_state* state, mc_object_info_t info, const void* address, dw_type_t type) {
80 // Simple case, hash this has binary:
81 case DW_TAG_base_type:
82 case DW_TAG_enumeration_type:
84 if(mc_ignored(address, 1))
86 mc_hash_binary(hash, address, type->byte_size);
90 case DW_TAG_array_type:
92 if(mc_ignored(address, type->byte_size))
95 long element_count = type->element_count;
96 dw_type_t subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
98 XBT_DEBUG("Hash array without subtype");
102 for(i=0; i!=element_count; ++i) {
103 XBT_DEBUG("Hash array element %i", i);
104 void* subaddress = ((char*)address)+i*subtype->byte_size;
105 mc_hash_value(hash, state, info, subaddress, subtype);
112 case DW_TAG_volatile_type:
113 case DW_TAG_const_type:
114 case DW_TAG_restrict_type:
116 if(type->dw_type_id==NULL) {
119 type = xbt_dict_get_or_null(info->types, type->dw_type_id);
126 case DW_TAG_structure_type:
127 case DW_TAG_class_type:
129 if(mc_ignored(address, type->byte_size))
132 unsigned int cursor = 0;
134 xbt_dynar_foreach(type->members, cursor, member){
135 XBT_DEBUG("Hash struct member %s", member->name);
136 dw_type_t subtype = xbt_dict_get_or_null(info->types, member->dw_type_id);
139 mc_hash_value(hash, state, info, ((char*)address) + member->offset, subtype);
144 // Pointer, we hash a single value but it might be an array.
145 case DW_TAG_pointer_type:
146 case DW_TAG_reference_type:
147 case DW_TAG_rvalue_reference_type:
149 if(mc_ignored(address, 1))
152 void* pointed = *(void**)address;
154 XBT_DEBUG("Hashed pinter is NULL");
159 if(mc_address_test(state->handled_addresses, pointed)) {
160 XBT_DEBUG("Hashed pointed data %p already hashed", pointed);
163 mc_address_add(state->handled_addresses, pointed);
165 // Anything outside the R/W segments and the heap is not hashed:
166 bool valid_pointer = (pointed >= (void*) mc_binary_info->start_rw && pointed <= (void*) mc_binary_info->end_rw)
167 || (pointed >= (void*) mc_libsimgrid_info->start_rw && pointed <= (void*) mc_libsimgrid_info->end_rw)
168 || (pointed >= std_heap && pointed < (void*) ((const char*)std_heap + STD_HEAP_SIZE));
170 XBT_DEBUG("Hashed pointed data %p is in an ignored range", pointed);
174 dw_type_t subtype = type->dw_type_id == NULL ? NULL : xbt_dict_get_or_null(info->types, type->dw_type_id);
176 XBT_DEBUG("Missing type for %p (type=%s)", pointed, type->dw_type_id);
186 case DW_TAG_union_type:
187 case DW_TAG_subroutine_type:
194 static void mc_hash_object_globals(mc_hash_t *hash, mc_hashing_state* state, mc_object_info_t info) {
195 unsigned int cursor = 0;
196 dw_variable_t variable;
197 xbt_dynar_foreach(info->global_variables, cursor, variable) {
198 XBT_DEBUG("Hash global variable %s", variable->name);
200 if(variable->type_origin == NULL) {
205 dw_type_t type = xbt_dict_get_or_null(info->types, variable->type_origin);
211 const char* address = variable->address.address;
212 bool valid_pointer = (address >= mc_binary_info->start_rw && address <= mc_binary_info->end_rw)
213 || (address >= mc_libsimgrid_info->start_rw && address <= mc_libsimgrid_info->end_rw)
214 || (address >= (const char*) std_heap && address < (const char *)std_heap + STD_HEAP_SIZE);
215 if(!valid_pointer) continue;
217 mc_hash_value(hash, state, info, variable->address.address, type);
221 static void mc_hash_stack_frame(
223 mc_object_info_t info, unw_cursor_t* unw_cursor, dw_frame_t frame, char* frame_pointer, mc_hashing_state* state
228 unsigned int cursor = 0;
229 dw_variable_t variable;
230 xbt_dynar_foreach(frame->variables, cursor, variable){
232 if(variable->type_origin==NULL) {
233 XBT_DEBUG("Hash local variable %s without type", variable->name);
236 if(variable->address.location == NULL) {
237 XBT_DEBUG("Hash local variable %s without location", variable->name);
241 XBT_DEBUG("Hash local variable %s", variable->name);
243 void* variable_address = (void*) MC_dwarf_resolve_location(unw_cursor, variable->address.location, frame_pointer);
245 dw_type_t type = xbt_dict_get_or_null(info->types, variable->type_origin);
247 XBT_DEBUG("Hash local variable %s without loctypeation", variable->name);
251 mc_hash_value(hash, state, info, variable_address, type);
255 /** \brief Find the frame base of a given frame
257 * \param ip Instruction pointer
261 void* mc_find_frame_base(unw_word_t ip, dw_frame_t frame, unw_cursor_t* unw_cursor) {
262 switch(frame->frame_base->type) {
266 for(loclist_cursor=0; loclist_cursor < xbt_dynar_length(frame->frame_base->location.loclist); loclist_cursor++){
267 dw_location_entry_t entry = xbt_dynar_get_as(frame->frame_base->location.loclist, loclist_cursor, dw_location_entry_t);
268 if((ip >= entry->lowpc) && (ip < entry->highpc)){
269 return (void*) MC_dwarf_resolve_location(unw_cursor, entry->location, NULL);
280 static void mc_hash_stack(mc_hash_t *hash, stack_region_t stack, mc_hashing_state* state) {
283 if(unw_init_local(&cursor, (unw_context_t *)stack->context)){
284 xbt_die("unw_init_local failed");
287 MC_HASH(*hash, (long)stack->address);
292 for(ret=1; ret >= 0; ret = unw_step(&cursor)) {
294 // Find the frame name:
296 char frame_name[256];
297 if(unw_get_proc_name(&cursor, frame_name, sizeof (frame_name), &off)!=0) {
301 XBT_DEBUG("Frame #%i %s", (int) count, frame_name);
303 // Stop before context switch with maestro
304 if(!strcmp(frame_name, "smx_ctx_sysv_wrapper")) {
311 if(unw_get_reg(&cursor, UNW_REG_IP, &ip))
313 if(unw_get_reg(&cursor, UNW_REG_SP, &sp))
318 // Find the object info:
319 mc_object_info_t info;
320 if((long)ip >= (long) mc_libsimgrid_info->start_exec && (long)ip < (long) mc_libsimgrid_info->end_exec)
321 info = mc_libsimgrid_info;
322 else if((long)ip >= (long) mc_binary_info->start_exec && (long)ip < (long) mc_binary_info->end_exec)
323 info = mc_binary_info;
328 dw_frame_t frame = xbt_dict_get_or_null(info->local_variables, frame_name);
332 long true_ip = (long)frame->low_pc + (long)off;
334 // Find the fame base:
335 void* frame_base = mc_find_frame_base(true_ip, frame, &cursor);
339 mc_hash_stack_frame(hash, info, &cursor, frame, frame_base, state);
342 MC_HASH(*hash, count);
345 static void mc_hash_stacks(mc_hash_t *hash, mc_hashing_state* state) {
346 unsigned int cursor = 0;
347 stack_region_t current_stack;
349 MC_HASH(*hash, xbt_dynar_length(stacks_areas));
352 xbt_dynar_foreach(stacks_areas, cursor, current_stack){
353 XBT_DEBUG("Stack %i", i);
354 mc_hash_stack(hash, current_stack, state);
359 uint64_t mc_hash_processes_state(int num_state) {
360 XBT_DEBUG("START hash %i", num_state);
362 mc_hashing_state state;
363 mc_hash_state_init(&state);
365 mc_hash_t hash = MC_HASH_INIT;
367 MC_HASH(hash, xbt_swag_size(simix_global->process_list)); // process count
368 mc_hash_object_globals(&hash, &state, mc_binary_info);
369 // mc_hash_object_globals(&hash, &state, mc_libsimgrid_info);
370 mc_hash_stacks(&hash, &state);
372 mc_hash_state_destroy(&state);
374 XBT_DEBUG("END hash %i", num_state);