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, int check_ignore);
32 static void match_equals(xbt_dynar_t list);
34 static int in_mmalloc_ignore(int block, int fragment);
35 static size_t heap_comparison_ignore(void *address);
37 void mmalloc_backtrace_block_display(void* heapinfo, int block){
41 if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
42 XBT_DEBUG("No backtrace available for that block, sorry.");
46 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
47 e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
49 xbt_ex_setup_backtrace(&e);
51 XBT_DEBUG("(backtrace not set)");
52 } else if (e.bt_strings == NULL) {
53 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
57 XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
58 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
59 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
65 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
69 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
70 e.used = XBT_BACKTRACE_SIZE;
72 xbt_ex_setup_backtrace(&e);
74 XBT_DEBUG("(backtrace not set)");
75 } else if (e.bt_strings == NULL) {
76 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
80 XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
81 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
82 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
88 void *s_heap, *heapbase1, *heapbase2;
89 malloc_info *heapinfo1, *heapinfo2;
90 size_t heaplimit, heapsize1, heapsize2;
94 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
96 if(heap1 == NULL && heap1 == NULL){
97 XBT_DEBUG("Malloc descriptors null");
101 if(heap1->heaplimit != heap2->heaplimit){
102 XBT_DEBUG("Different limit of valid info table indices");
106 /* Heap information */
107 heaplimit = ((struct mdesc *)heap1)->heaplimit;
109 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
111 heapbase1 = (char *)heap1 + BLOCKSIZE;
112 heapbase2 = (char *)heap2 + BLOCKSIZE;
114 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
115 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
117 heapsize1 = heap1->heapsize;
118 heapsize2 = heap2->heapsize;
120 /* Start comparison */
121 size_t i1, i2, j1, j2, k;
122 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
123 size_t frag_size1, frag_size2;
125 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
127 int equal, res_compare;
131 /* Init equal information */
134 while(i1<=heaplimit){
135 if(heapinfo1[i1].type == 0){
136 heapinfo1[i1].busy_block.equal_to = -1;
138 if(heapinfo1[i1].type > 0){
139 for(j1=0; j1 < MAX_FRAGMENT_PER_BLOCK; j1++){
140 heapinfo1[i1].busy_frag.equal_to[j1] = -1;
148 while(i2<=heaplimit){
149 if(heapinfo2[i2].type == 0){
150 heapinfo2[i2].busy_block.equal_to = -1;
152 if(heapinfo2[i2].type > 0){
153 for(j2=0; j2 < MAX_FRAGMENT_PER_BLOCK; j2++){
154 heapinfo2[i2].busy_frag.equal_to[j2] = -1;
160 /* Check busy blocks*/
164 while(i1 < heaplimit){
169 if(heapinfo1[i1].type == -1){ /* Free block */
174 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
176 if(heapinfo1[i1].type == 0){ /* Large block */
178 while(i2 <= heaplimit && !equal){
180 if(heapinfo2[i2].type != 0){
185 if(heapinfo2[i2].busy_block.equal_to == 1){
190 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
195 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
200 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
203 add_heap_area_pair(previous, i1, -1, i2, -1);
205 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
206 if(in_mmalloc_ignore((int)i1, -1))
207 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
209 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
211 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
215 for(k=0; k < heapinfo2[i2].busy_block.size; k++)
216 heapinfo2[i2+k].busy_block.equal_to = 1;
217 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
218 heapinfo1[i1+k].busy_block.equal_to = 1;
220 match_equals(previous);
222 xbt_dynar_reset(previous);
228 }else{ /* Fragmented block */
230 frag_size1 = 1 << heapinfo1[i1].type;
232 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
234 if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
237 addr_frag1 = (void*) ((char *)addr_block1 + (j1 * frag_size1));
242 while(i2 <= heaplimit && !equal){
244 if(heapinfo2[i2].type <= 0){
249 frag_size2 = 1 << heapinfo2[i2].type;
251 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
253 if(heapinfo2[i2].busy_frag.equal_to[j2] == 1){
257 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]){ /* Different size_used */
261 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
262 addr_frag2 = (void*) ((char *)addr_block2 + (j2 * frag_size2));
265 add_heap_area_pair(previous, i1, j1, i2, j2);
267 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
268 if(in_mmalloc_ignore((int)i1, (int)j1))
269 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
271 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
273 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
277 heapinfo2[i2].busy_frag.equal_to[j2] = 1;
278 heapinfo1[i1].busy_frag.equal_to[j1] = 1;
280 match_equals(previous);
283 xbt_dynar_reset(previous);
299 /* All blocks/fragments are equal to another block/fragment ? */
301 int nb_diff1 = 0, nb_diff2 = 0;
302 size_t frag_size = 0;
305 if(heapinfo1[i].type == 0){
306 if(heapinfo1[i].busy_block.busy_size > 0){
307 if(heapinfo1[i].busy_block.equal_to == -1){
308 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
309 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
310 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
311 mmalloc_backtrace_block_display((void*)heapinfo1, i);
317 if(heapinfo1[i].type > 0){
318 frag_size = 1 << heapinfo1[i].type;
319 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
320 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
321 if(heapinfo1[i].busy_frag.equal_to[j] == -1){
322 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
323 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
324 addr_frag1 = (void*) ((char *)addr_block1 + (j * frag_size));
325 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
326 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
337 XBT_DEBUG("Different blocks or fragments in heap1 : %d\n", nb_diff1);
342 if(heapinfo2[i].type == 0){
343 if(heapinfo2[i].busy_block.busy_size > 0){
344 if(heapinfo2[i].busy_block.equal_to == -1){
345 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
346 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
347 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
348 mmalloc_backtrace_block_display((void*)heapinfo2, i);
354 if(heapinfo2[i].type > 0){
355 frag_size = 1 << heapinfo2[i].type;
356 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
357 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
358 if(heapinfo2[i].busy_frag.equal_to[j] == -1){
359 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
360 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
361 addr_frag2 = (void*) ((char *)addr_block2 + (j * frag_size));
362 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
363 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
373 XBT_DEBUG("Different blocks or fragments in heap2 : %d\n", nb_diff2);
375 xbt_dynar_free(&previous);
377 return ((nb_diff1 > 0) || (nb_diff2 > 0));
381 static int in_mmalloc_ignore(int block, int fragment){
383 unsigned int cursor = 0;
385 int end = xbt_dynar_length(mmalloc_ignore) - 1;
386 mc_ignore_region_t region;
389 cursor = (start + end) / 2;
390 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
391 if(region->block == block){
392 if(region->fragment == fragment)
394 if(region->fragment < fragment)
396 if(region->fragment > fragment)
399 if(region->block < block)
401 if(region->block > block)
408 static size_t heap_comparison_ignore(void *address){
409 unsigned int cursor = 0;
411 int end = xbt_dynar_length(mmalloc_ignore) - 1;
412 mc_ignore_region_t region;
415 cursor = (start + end) / 2;
416 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
417 if(region->address == address)
419 if(region->address < address)
421 if(region->address > address)
429 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
431 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
432 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
433 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
434 size_t frag_size, frag_size1, frag_size2;
436 void *current_area1, *current_area2;
442 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
443 if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
444 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
445 if((ignore2 = heap_comparison_ignore(current_area2)) == ignore1){
454 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
456 /* Check pointer difference */
457 pointer_align = (i / sizeof(void*)) * sizeof(void*);
458 address_pointed1 = *((void **)((char *)area1 + pointer_align));
459 address_pointed2 = *((void **)((char *)area2 + pointer_align));
461 /* Get pointed blocks number */
462 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
463 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
465 /* Check if valid blocks number */
466 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)
469 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
471 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
472 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
474 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
476 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
480 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
484 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
486 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
487 if(in_mmalloc_ignore(block_pointed1, -1))
488 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
490 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
492 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
500 }else{ /* Fragmented block */
502 /* Get pointed fragments number */
503 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
504 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
506 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
509 frag_size = 1 << heapinfo1[block_pointed1].type;
511 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size));
512 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size));
514 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
516 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
517 if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
518 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
520 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
522 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
534 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
536 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
537 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
539 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
540 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
542 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
545 frag_size1 = 1 << heapinfo1[block_pointed1].type;
546 frag_size2 = 1 << heapinfo1[block_pointed2].type;
548 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size1));
549 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size2));
551 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
553 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
554 if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
555 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
557 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
559 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
573 i = pointer_align + sizeof(void *);
587 static void heap_area_pair_free(heap_area_pair_t pair){
594 static void heap_area_pair_free_voidp(void *d)
596 heap_area_pair_free((heap_area_pair_t) * (void **) d);
599 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
601 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
602 heap_area_pair_t pair = NULL;
603 pair = xbt_new0(s_heap_area_pair_t, 1);
604 pair->block1 = block1;
605 pair->fragment1 = fragment1;
606 pair->block2 = block2;
607 pair->fragment2 = fragment2;
609 xbt_dynar_push(list, &pair);
617 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
619 unsigned int cursor = 0;
620 heap_area_pair_t current_pair;
622 xbt_dynar_foreach(list, cursor, current_pair){
623 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
630 static void match_equals(xbt_dynar_t list){
632 unsigned int cursor = 0;
633 heap_area_pair_t current_pair;
635 xbt_dynar_foreach(list, cursor, current_pair){
636 if(current_pair->fragment1 != -1){
637 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = 1;
638 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = 1;
640 heapinfo1[current_pair->block1].busy_block.equal_to = 1;
641 heapinfo2[current_pair->block2].busy_block.equal_to = 1;