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 typedef struct s_heap_area_pair{
22 }s_heap_area_pair_t, *heap_area_pair_t;
24 static void heap_area_pair_free(heap_area_pair_t pair);
25 static void heap_area_pair_free_voidp(void *d);
26 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
27 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
29 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous);
30 static void match_equals(xbt_dynar_t list);
32 void mmalloc_backtrace_block_display(void* heapinfo, int block){
36 if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
37 XBT_DEBUG("No backtrace available for that block, sorry.");
41 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
42 e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
44 xbt_ex_setup_backtrace(&e);
46 XBT_DEBUG("(backtrace not set)");
47 } else if (e.bt_strings == NULL) {
48 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
52 XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
53 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
54 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
60 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
64 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
65 e.used = XBT_BACKTRACE_SIZE;
67 xbt_ex_setup_backtrace(&e);
69 XBT_DEBUG("(backtrace not set)");
70 } else if (e.bt_strings == NULL) {
71 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
75 XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
76 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
77 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
83 void *s_heap, *heapbase1, *heapbase2;
84 malloc_info *heapinfo1, *heapinfo2;
85 size_t heaplimit, heapsize1, heapsize2;
87 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
89 if(heap1 == NULL && heap1 == NULL){
90 XBT_DEBUG("Malloc descriptors null");
94 if(heap1->heaplimit != heap2->heaplimit){
95 XBT_DEBUG("Different limit of valid info table indices");
99 /* Heap information */
100 heaplimit = ((struct mdesc *)heap1)->heaplimit;
102 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
104 heapbase1 = (char *)heap1 + BLOCKSIZE;
105 heapbase2 = (char *)heap2 + BLOCKSIZE;
107 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
108 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
110 heapsize1 = heap1->heapsize;
111 heapsize2 = heap2->heapsize;
113 /* Start comparison */
114 size_t i1, i2, j1, j2, k;
115 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
116 size_t frag_size1, frag_size2;
118 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
122 /* Check busy blocks*/
126 while(i1 < heaplimit){
131 if(heapinfo1[i1].type == -1){ /* Free block */
136 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
138 if(heapinfo1[i1].type == 0){ /* Large block */
140 while(i2 <= heaplimit && !equal){
142 if(heapinfo2[i2].type != 0){
147 if(heapinfo2[i2].busy_block.equal_to == 1){
152 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
157 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
162 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
165 add_heap_area_pair(previous, i1, -1, i2, -1);
166 if(!compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous)){
167 for(k=0; k < heapinfo2[i2].busy_block.size; k++)
168 heapinfo2[i2+k].busy_block.equal_to = 1;
169 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
170 heapinfo1[i1+k].busy_block.equal_to = 1;
172 match_equals(previous);
174 xbt_dynar_reset(previous);
180 }else{ /* Fragmented block */
182 frag_size1 = 1 << heapinfo1[i1].type;
184 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
186 if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
189 addr_frag1 = (void*) ((char *)addr_block1 + (j1 * frag_size1));
194 while(i2 <= heaplimit && !equal){
196 if(heapinfo2[i2].type <= 0){
201 frag_size2 = 1 << heapinfo2[i2].type;
203 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
205 if(heapinfo2[i2].busy_frag.equal_to[j2] == 1){
209 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]){ /* Different size_used */
213 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
214 addr_frag2 = (void*) ((char *)addr_block2 + (j2 * frag_size2));
217 add_heap_area_pair(previous, i1, j1, i2, j2);
218 if(!compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous)){
219 heapinfo2[i2].busy_frag.equal_to[j2] = 1;
220 heapinfo1[i1].busy_frag.equal_to[j1] = 1;
222 match_equals(previous);
225 xbt_dynar_reset(previous);
241 /* All blocks/fragments are equal to another block/fragment ? */
243 int nb_diff1 = 0, nb_diff2 = 0;
244 size_t frag_size = 0;
247 if(heapinfo1[i].type == 0){
248 if(heapinfo1[i].busy_block.busy_size > 0){
249 if(heapinfo1[i].busy_block.equal_to == -1){
250 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
251 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
252 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
253 mmalloc_backtrace_block_display((void*)heapinfo1, i);
259 if(heapinfo1[i].type > 0){
260 frag_size = 1 << heapinfo1[i].type;
261 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
262 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
263 if(heapinfo1[i].busy_frag.equal_to[j] == -1){
264 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
265 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
266 addr_frag1 = (void*) ((char *)addr_block1 + (j * frag_size));
267 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
268 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
279 XBT_DEBUG("Different blocks or fragments in heap1 : %d\n", nb_diff1);
284 if(heapinfo2[i].type == 0){
285 if(heapinfo2[i].busy_block.busy_size > 0){
286 if(heapinfo2[i].busy_block.equal_to == -1){
287 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
288 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
289 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
290 mmalloc_backtrace_block_display((void*)heapinfo2, i);
296 if(heapinfo2[i].type > 0){
297 frag_size = 1 << heapinfo2[i].type;
298 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
299 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
300 if(heapinfo2[i].busy_frag.equal_to[j] == -1){
301 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
302 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
303 addr_frag2 = (void*) ((char *)addr_block2 + (j * frag_size));
304 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
305 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
315 XBT_DEBUG("Different blocks or fragments in heap2 : %d\n", nb_diff2);
318 /* Reset equal information */
322 if(heapinfo1[i].type == 0){
323 heapinfo1[i].busy_block.equal_to = -1;
325 if(heapinfo1[i].type > 0){
326 for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
327 heapinfo1[i].busy_frag.equal_to[j] = -1;
336 if(heapinfo2[i].type == 0){
337 heapinfo2[i].busy_block.equal_to = -1;
339 if(heapinfo2[i].type > 0){
340 for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
341 heapinfo2[i].busy_frag.equal_to[j] = -1;
347 xbt_dynar_free(&previous);
349 return ((nb_diff1 > 0) || (nb_diff2 > 0));
354 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous){
356 size_t i = 0, pointer_align = 0;
357 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
358 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
364 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
366 /* Check pointer difference */
367 pointer_align = (i / sizeof(void*)) * sizeof(void*);
368 address_pointed1 = *((void **)((char *)area1 + pointer_align));
369 address_pointed2 = *((void **)((char *)area2 + pointer_align));
371 /* Get pointed blocks number */
372 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
373 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
375 /* Check if valid blocks number */
376 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)
379 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
381 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
382 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
384 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
386 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
390 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
394 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
396 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous);
403 }else{ /* Fragmented block */
405 /* Get pointed fragments number */
406 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
407 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
409 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
412 frag_size = 1 << heapinfo1[block_pointed1].type;
414 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size));
415 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size));
417 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
419 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous);
430 i = pointer_align + sizeof(void *);
444 static void heap_area_pair_free(heap_area_pair_t pair){
451 static void heap_area_pair_free_voidp(void *d)
453 heap_area_pair_free((heap_area_pair_t) * (void **) d);
456 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
458 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
459 heap_area_pair_t pair = NULL;
460 pair = xbt_new0(s_heap_area_pair_t, 1);
461 pair->block1 = block1;
462 pair->fragment1 = fragment1;
463 pair->block2 = block2;
464 pair->fragment2 = fragment2;
466 xbt_dynar_push(list, &pair);
474 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
476 unsigned int cursor = 0;
477 heap_area_pair_t current_pair;
479 xbt_dynar_foreach(list, cursor, current_pair){
480 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
487 static void match_equals(xbt_dynar_t list){
489 unsigned int cursor = 0;
490 heap_area_pair_t current_pair;
492 xbt_dynar_foreach(list, cursor, current_pair){
493 if(current_pair->fragment1 != -1){
494 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = 1;
495 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = 1;
497 heapinfo1[current_pair->block1].busy_block.equal_to = 1;
498 heapinfo2[current_pair->block2].busy_block.equal_to = 1;