1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
13 "Logging specific to mm_diff in mmalloc");
15 extern char *xbt_binary_name;
17 xbt_dynar_t mmalloc_ignore;
19 typedef struct s_heap_area_pair{
24 }s_heap_area_pair_t, *heap_area_pair_t;
26 static void heap_area_pair_free(heap_area_pair_t pair);
27 static void heap_area_pair_free_voidp(void *d);
28 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
29 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
31 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous);
32 static void match_equals(xbt_dynar_t list);
34 static size_t heap_comparison_ignore(void *address);
36 void mmalloc_backtrace_block_display(void* heapinfo, int block){
40 if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
41 XBT_DEBUG("No backtrace available for that block, sorry.");
45 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
46 e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
48 xbt_ex_setup_backtrace(&e);
50 XBT_DEBUG("(backtrace not set)");
51 } else if (e.bt_strings == NULL) {
52 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
56 XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
57 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
58 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
64 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
68 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
69 e.used = XBT_BACKTRACE_SIZE;
71 xbt_ex_setup_backtrace(&e);
73 XBT_DEBUG("(backtrace not set)");
74 } else if (e.bt_strings == NULL) {
75 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
79 XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
80 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
81 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
87 void *s_heap, *heapbase1, *heapbase2;
88 malloc_info *heapinfo1, *heapinfo2;
89 size_t heaplimit, heapsize1, heapsize2;
91 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
93 if(heap1 == NULL && heap1 == NULL){
94 XBT_DEBUG("Malloc descriptors null");
98 if(heap1->heaplimit != heap2->heaplimit){
99 XBT_DEBUG("Different limit of valid info table indices");
103 /* Heap information */
104 heaplimit = ((struct mdesc *)heap1)->heaplimit;
106 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
108 heapbase1 = (char *)heap1 + BLOCKSIZE;
109 heapbase2 = (char *)heap2 + BLOCKSIZE;
111 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
112 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
114 heapsize1 = heap1->heapsize;
115 heapsize2 = heap2->heapsize;
117 /* Start comparison */
118 size_t i1, i2, j1, j2, k;
119 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
120 size_t frag_size1, frag_size2;
122 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
126 /* Check busy blocks*/
130 while(i1 < heaplimit){
135 if(heapinfo1[i1].type == -1){ /* Free block */
140 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
142 if(heapinfo1[i1].type == 0){ /* Large block */
144 while(i2 <= heaplimit && !equal){
146 if(heapinfo2[i2].type != 0){
151 if(heapinfo2[i2].busy_block.equal_to == 1){
156 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
161 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
166 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
169 add_heap_area_pair(previous, i1, -1, i2, -1);
170 if(!compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous)){
171 for(k=0; k < heapinfo2[i2].busy_block.size; k++)
172 heapinfo2[i2+k].busy_block.equal_to = 1;
173 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
174 heapinfo1[i1+k].busy_block.equal_to = 1;
176 match_equals(previous);
178 xbt_dynar_reset(previous);
184 }else{ /* Fragmented block */
186 frag_size1 = 1 << heapinfo1[i1].type;
188 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
190 if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
193 addr_frag1 = (void*) ((char *)addr_block1 + (j1 * frag_size1));
198 while(i2 <= heaplimit && !equal){
200 if(heapinfo2[i2].type <= 0){
205 frag_size2 = 1 << heapinfo2[i2].type;
207 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
209 if(heapinfo2[i2].busy_frag.equal_to[j2] == 1){
213 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]){ /* Different size_used */
217 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
218 addr_frag2 = (void*) ((char *)addr_block2 + (j2 * frag_size2));
221 add_heap_area_pair(previous, i1, j1, i2, j2);
222 if(!compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous)){
223 heapinfo2[i2].busy_frag.equal_to[j2] = 1;
224 heapinfo1[i1].busy_frag.equal_to[j1] = 1;
226 match_equals(previous);
229 xbt_dynar_reset(previous);
245 /* All blocks/fragments are equal to another block/fragment ? */
247 int nb_diff1 = 0, nb_diff2 = 0;
248 size_t frag_size = 0;
251 if(heapinfo1[i].type == 0){
252 if(heapinfo1[i].busy_block.busy_size > 0){
253 if(heapinfo1[i].busy_block.equal_to == -1){
254 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
255 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
256 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
257 mmalloc_backtrace_block_display((void*)heapinfo1, i);
263 if(heapinfo1[i].type > 0){
264 frag_size = 1 << heapinfo1[i].type;
265 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
266 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
267 if(heapinfo1[i].busy_frag.equal_to[j] == -1){
268 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
269 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
270 addr_frag1 = (void*) ((char *)addr_block1 + (j * frag_size));
271 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
272 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
283 XBT_DEBUG("Different blocks or fragments in heap1 : %d\n", nb_diff1);
288 if(heapinfo2[i].type == 0){
289 if(heapinfo2[i].busy_block.busy_size > 0){
290 if(heapinfo2[i].busy_block.equal_to == -1){
291 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
292 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
293 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
294 mmalloc_backtrace_block_display((void*)heapinfo2, i);
300 if(heapinfo2[i].type > 0){
301 frag_size = 1 << heapinfo2[i].type;
302 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
303 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
304 if(heapinfo2[i].busy_frag.equal_to[j] == -1){
305 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
306 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
307 addr_frag2 = (void*) ((char *)addr_block2 + (j * frag_size));
308 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
309 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
319 XBT_DEBUG("Different blocks or fragments in heap2 : %d\n", nb_diff2);
322 /* Reset equal information */
326 if(heapinfo1[i].type == 0){
327 heapinfo1[i].busy_block.equal_to = -1;
329 if(heapinfo1[i].type > 0){
330 for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
331 heapinfo1[i].busy_frag.equal_to[j] = -1;
340 if(heapinfo2[i].type == 0){
341 heapinfo2[i].busy_block.equal_to = -1;
343 if(heapinfo2[i].type > 0){
344 for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
345 heapinfo2[i].busy_frag.equal_to[j] = -1;
351 xbt_dynar_free(&previous);
353 return ((nb_diff1 > 0) || (nb_diff2 > 0));
357 static size_t heap_comparison_ignore(void *address){
358 unsigned int cursor = 0;
360 int end = xbt_dynar_length(mmalloc_ignore) - 1;
361 mc_ignore_region_t region;
364 cursor = (start + end) / 2;
365 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
366 if(region->address == address)
368 if(region->address < address)
370 if(region->address > address)
378 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous){
380 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
381 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
382 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
383 size_t frag_size, frag_size1, frag_size2;
385 void *current_area1, *current_area2;
389 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
390 if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
391 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
392 if((ignore2 = heap_comparison_ignore(current_area2)) == ignore1){
398 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
400 /* Check pointer difference */
401 pointer_align = (i / sizeof(void*)) * sizeof(void*);
402 address_pointed1 = *((void **)((char *)area1 + pointer_align));
403 address_pointed2 = *((void **)((char *)area2 + pointer_align));
405 /* Get pointed blocks number */
406 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
407 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
409 /* Check if valid blocks number */
410 if((char *)address_pointed1 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed1 > heapsize1 || block_pointed1 < 1 || (char *)address_pointed2 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed2 > heapsize2 || block_pointed2 < 1)
413 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
415 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
416 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
418 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
420 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
424 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
428 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
430 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous);
437 }else{ /* Fragmented block */
439 /* Get pointed fragments number */
440 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
441 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
443 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
446 frag_size = 1 << heapinfo1[block_pointed1].type;
448 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size));
449 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size));
451 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
453 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous);
464 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
466 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
467 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
469 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
470 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
472 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
475 frag_size1 = 1 << heapinfo1[block_pointed1].type;
476 frag_size2 = 1 << heapinfo1[block_pointed2].type;
478 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size1));
479 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size2));
481 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
483 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous);
496 i = pointer_align + sizeof(void *);
510 static void heap_area_pair_free(heap_area_pair_t pair){
517 static void heap_area_pair_free_voidp(void *d)
519 heap_area_pair_free((heap_area_pair_t) * (void **) d);
522 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
524 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
525 heap_area_pair_t pair = NULL;
526 pair = xbt_new0(s_heap_area_pair_t, 1);
527 pair->block1 = block1;
528 pair->fragment1 = fragment1;
529 pair->block2 = block2;
530 pair->fragment2 = fragment2;
532 xbt_dynar_push(list, &pair);
540 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
542 unsigned int cursor = 0;
543 heap_area_pair_t current_pair;
545 xbt_dynar_foreach(list, cursor, current_pair){
546 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
553 static void match_equals(xbt_dynar_t list){
555 unsigned int cursor = 0;
556 heap_area_pair_t current_pair;
558 xbt_dynar_foreach(list, cursor, current_pair){
559 if(current_pair->fragment1 != -1){
560 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = 1;
561 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = 1;
563 heapinfo1[current_pair->block1].busy_block.equal_to = 1;
564 heapinfo2[current_pair->block2].busy_block.equal_to = 1;