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 */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
12 "Logging specific to mm_diff in mmalloc");
14 extern char *xbt_binary_name;
16 void mmalloc_backtrace_display(xbt_mheap_t mdp, void *ptr){
17 size_t block = BLOCK(ptr);
21 if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
22 fprintf(stderr,"Ouch, this pointer is not mine. I cannot display its backtrace. I refuse it to death!!\n");
26 type = mdp->heapinfo[block].type;
29 //fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
32 if (mdp->heapinfo[block].busy_block.bt_size == 0) {
33 fprintf(stderr,"No backtrace available for that block, sorry.\n");
37 memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
38 e.used = mdp->heapinfo[block].busy_block.bt_size;
40 xbt_ex_setup_backtrace(&e);
42 fprintf(stderr, "(backtrace not set)\n");
43 } else if (e.bt_strings == NULL) {
44 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
48 fprintf(stderr, "Backtrace of where the block %p was malloced (%d frames):\n",ptr,e.used);
49 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
50 fprintf(stderr,"%d",i);fflush(NULL);
51 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
57 void mmalloc_backtrace_block_display(xbt_mheap_t mdp, size_t block){
62 type = mdp->heapinfo[block].type;
65 fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
68 if (mdp->heapinfo[block].busy_block.bt_size == 0) {
69 fprintf(stderr,"No backtrace available for that block, sorry.\n");
73 memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
74 e.used = mdp->heapinfo[block].busy_block.bt_size;
76 xbt_ex_setup_backtrace(&e);
78 fprintf(stderr, "(backtrace not set)\n");
79 } else if (e.bt_strings == NULL) {
80 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
84 fprintf(stderr, "Backtrace of where the block %zu was malloced (%d frames):\n", block ,e.used);
85 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
86 fprintf(stderr,"%d",i);fflush(NULL);
87 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
92 void mmalloc_backtrace_fragment_display(xbt_mheap_t mdp, size_t block, size_t frag){
96 memcpy(&e.bt,&(mdp->heapinfo[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
97 e.used = XBT_BACKTRACE_SIZE;
99 xbt_ex_setup_backtrace(&e);
101 fprintf(stderr, "(backtrace not set)\n");
102 } else if (e.bt_strings == NULL) {
103 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
107 fprintf(stderr, "Backtrace of where the fragment %zu in block %zu was malloced (%d frames):\n", frag, block ,e.used);
108 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
109 fprintf(stderr,"%d",i);fflush(NULL);
110 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
115 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void* s_heap, void* r_heap){
117 if(mdp1 == NULL && mdp2 == NULL){
118 fprintf(stderr, "Malloc descriptors null\n");
122 int errors = mmalloc_compare_mdesc(mdp1, mdp2, s_heap, r_heap);
128 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void* s_heap, void* r_heap){
132 /*if(mdp1->headersize != mdp2->headersize){
133 fprintf(stderr, "Different size of the file header for the mapped files\n");
137 if(mdp1->refcount != mdp2->refcount){
138 fprintf(stderr, "Different number of processes that attached the heap\n");
142 if(strcmp(mdp1->magic, mdp2->magic) != 0){
143 fprintf(stderr,"Different magic number\n");
147 if(mdp1->flags != mdp2->flags){
148 fprintf(stderr,"Different flags\n");
152 if(mdp1->heapsize != mdp2->heapsize){
153 fprintf(stderr,"Different number of info entries\n");
157 if(mdp1->heapbase != mdp2->heapbase){
158 fprintf(stderr,"Different first block of the heap\n");
162 if(mdp1->heapindex != mdp2->heapindex){
163 fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
167 if(mdp1->base != mdp2->base){
168 fprintf(stderr,"Different base address of the memory region\n");
172 if(mdp1->breakval != mdp2->breakval){
173 fprintf(stderr,"Different current location in the memory region\n");
177 if(mdp1->top != mdp2->top){
178 fprintf(stderr,"Different end of the current location in the memory region\n");
182 if(mdp1->heaplimit != mdp2->heaplimit){
183 fprintf(stderr,"Different limit of valid info table indices\n");
187 if(mdp1->fd != mdp2->fd){
188 fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
192 if(mdp1->version != mdp2->version){
193 fprintf(stderr,"Different version of the mmalloc package\n");
197 //void* heapbase1 = (char *)mdp1 + ((char *)mdp1->heapbase - (char *)s_heap);
198 //void* heapbase2 = (char *)mdp2 + ((char *)mdp2->heapbase - (char *)s_heap);
200 void *heapbase1 = (char *)mdp1 + BLOCKSIZE;
201 void *heapbase2 = (char *)mdp2 + BLOCKSIZE;
204 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
205 size_t frag_size = 0; /* FIXME: arbitrary initialization */
206 /* size_t frag_size_pointed; FIXME: unused */
213 void *end_heap = get_end_addr_heap(s_heap);
216 void *address_pointed1, *address_pointed2;
218 int block_pointed1, block_pointed2;
219 void *addr_block_pointed1, *addr_block_pointed2;
220 int frag_pointed1, frag_pointed2;
223 /* Check busy blocks*/
225 while(i < mdp1->heaplimit){
227 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
228 fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
232 //fprintf(stderr, "i = %zu, type = %d", i, mdp1->heapinfo[i].type);
234 //addr_block1 = (void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1);
235 //addr_block2 = (void*) (((ADDR2UINT(1)) - 1) * BLOCKSIZE + (char*)heapbase2);
237 addr_block1 = heapbase1 + ((i-1)*BLOCKSIZE);
238 addr_block2 = heapbase2 + ((i-1)*BLOCKSIZE);
240 if(mdp1->heapinfo[i].type == 0){ /* busy large block */
242 if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
243 fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
248 if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
249 fprintf(stderr,"Different busy_size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.busy_size, mdp2->heapinfo[i].busy_block.busy_size);
255 /* Hamming distance on different blocks */
258 //for(k=0;k<mdp1->heapinfo[i].busy_block.size * BLOCKSIZE;k++){
259 for(k=0;k<mdp1->heapinfo[i].busy_block.busy_size;k++){
260 if(memcmp(((char *)addr_block1) + k, ((char *)addr_block2) + k, 1) != 0){
261 //fprintf(stderr, "Different byte (offset=%d) (%p - %p) in block %zu\n", k, (char *)addr_block1 + k, (char *)addr_block2 + k, i); fflush(NULL);
262 pointer_align = (k / sizeof(void*)) * sizeof(void*);
263 address_pointed1 = *((void **)((char *)addr_block1 + pointer_align));
264 address_pointed2 = *((void **)((char *)addr_block2 + pointer_align));
265 if(((address_pointed1 > s_heap) && (address_pointed1 < end_heap)) && ((address_pointed2 > s_heap) && (address_pointed2 < end_heap))){
266 block_pointed1 = ((char*)address_pointed1 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
267 block_pointed2 = ((char*)address_pointed2 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
268 //fprintf(stderr, "Blocks pointed : %d - %d\n", block_pointed1, block_pointed2);
269 if(mdp1->heapinfo[block_pointed1].type == mdp2->heapinfo[block_pointed2].type){
270 if(mdp1->heapinfo[block_pointed1].type == 0){ // Large block
271 while(mdp1->heapinfo[block_pointed1].busy_block.size == 0)
273 while(mdp2->heapinfo[block_pointed2].busy_block.size == 0)
275 if(mdp1->heapinfo[block_pointed1].busy_block.busy_size == mdp2->heapinfo[block_pointed2].busy_block.busy_size){
276 //addr_block_pointed1 = (void*) (((ADDR2UINT((size_t)block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1);
277 //addr_block_pointed2 = (void*) (((ADDR2UINT((size_t)block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2);
278 addr_block_pointed1 = heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
279 addr_block_pointed2 = heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
281 fprintf(stderr, "Memcmp between blocks %d and %d (size = %zu)\n", block_pointed1, block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size);
282 if(memcmp(addr_block_pointed1, addr_block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size) != 0){
285 fprintf(stderr, "False difference detected\n");
291 }else{ // Fragmented block
292 frag_size_pointed = pow(2, mdp1->heapinfo[block_pointed1].type);
293 address_pointed1 = (char*)mdp1 + ((char*)address_pointed1 - (char*)s_heap);
294 address_pointed2 = (char*)mdp2 + ((char*)address_pointed2 - (char*)s_heap);
295 //addr_block_pointed1 = (void*) (((ADDR2UINT((size_t)block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1);
296 //addr_block_pointed2 = (void*) (((ADDR2UINT((size_t)block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2);
297 addr_block_pointed1 = heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
298 addr_block_pointed2 = heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
300 while(address_pointed1 > (void*)((char*)addr_block_pointed1 + ((frag_pointed1 +1 ) * frag_size))){
304 while(address_pointed2 > (void*)((char*)addr_block_pointed2 + ((frag_pointed2 +1) * frag_size))){
307 if(mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1] == mdp2->heapinfo[block_pointed2].busy_frag.frag_size[frag_pointed2]){
308 fprintf(stderr, "Memcmp between fragments %d (block %d) and %d (block %d) (size = %d)\n", frag_pointed1, block_pointed1, frag_pointed2, block_pointed2, mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1]);
309 if(memcmp((char*)addr_block_pointed1 + (frag_pointed1 * frag_size), (char*)addr_block_pointed2 + (frag_pointed2 * frag_size), mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1]) != 0){
312 fprintf(stderr, "False difference detected\n");
332 fprintf(stderr,"\nDifferent data in large block %zu (size = %zu (in blocks), busy_size = %zu (in bytes))\n", i, mdp1->heapinfo[i].busy_block.size, mdp1->heapinfo[i].busy_block.busy_size);
334 fprintf(stderr, "Hamming distance between blocks : %d\n", distance);
335 mmalloc_backtrace_block_display(mdp1, i);
336 mmalloc_backtrace_block_display(mdp2, i);
345 if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
347 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
348 fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type); fflush(NULL);
352 if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
353 fprintf(stderr,"Different free fragments in fragmented block %zu : %zu - %zu\n", i, mdp1->heapinfo[i].busy_frag.nfree, mdp2->heapinfo[i].busy_frag.nfree); fflush(NULL);
357 if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
358 fprintf(stderr,"Different busy_size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.busy_size, mdp2->heapinfo[i].busy_block.busy_size); fflush(NULL);
362 //fprintf(stderr,"\n");
364 frag_size = pow(2, mdp1->heapinfo[i].type);
366 for(j=0; j< (BLOCKSIZE/frag_size); j++){
368 if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
369 fprintf(stderr,"Different busy_size for fragment %zu in block %zu : %hu - %hu\n", j, i, mdp1->heapinfo[i].busy_frag.frag_size[j], mdp2->heapinfo[i].busy_frag.frag_size[j]); fflush(NULL);
373 if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
375 addr_frag1 = (char *)addr_block1 + (j * frag_size);
376 addr_frag2 = (char *)addr_block2 + (j * frag_size);
379 /* Hamming distance on different blocks */
381 //for(k=0;k<frag_size;k++){
382 for(k=0;k<mdp1->heapinfo[i].busy_frag.frag_size[j];k++){
383 if(memcmp(((char *)addr_frag1) + k, ((char *)addr_frag2) + k, 1) != 0){
384 //fprintf(stderr, "Different byte (offset=%d) (%p - %p) in fragment %zu in block %zu\n", k, (char *)addr_frag1 + k, (char *)addr_frag2 + k, j, i); fflush(NULL);
385 pointer_align = (k / sizeof(void*)) * sizeof(void*);
386 address_pointed1 = *((void **)((char *)addr_frag1 + pointer_align));
387 address_pointed2 = *((void **)((char *)addr_frag2 + pointer_align));
388 if(((address_pointed1 > s_heap) && (address_pointed1 < end_heap)) && ((address_pointed2 > s_heap) && (address_pointed2 < end_heap))){
389 block_pointed1 = ((char*)address_pointed1 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
390 block_pointed2 = ((char*)address_pointed2 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
391 //fprintf(stderr, "Blocks pointed : %d - %d\n", block_pointed1, block_pointed2);
392 if(mdp1->heapinfo[block_pointed1].type == mdp2->heapinfo[block_pointed2].type){
393 if(mdp1->heapinfo[block_pointed1].type == 0){ // Large block
394 while(mdp1->heapinfo[block_pointed1].busy_block.size == 0)
396 while(mdp2->heapinfo[block_pointed2].busy_block.size == 0)
398 if(mdp1->heapinfo[block_pointed1].busy_block.busy_size == mdp2->heapinfo[block_pointed2].busy_block.busy_size){
399 //addr_block_pointed1 = (void*) (((ADDR2UINT((size_t)block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1);
400 //addr_block_pointed2 = (void*) (((ADDR2UINT((size_t)block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2);
401 addr_block_pointed1 = heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
402 addr_block_pointed2 = heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
403 fprintf(stderr, "Memcmp between blocks %d and %d (size = %zu)\n", block_pointed1, block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size);
404 if(memcmp(addr_block_pointed1, addr_block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size) != 0){
407 fprintf(stderr, "False difference detected\n");
413 }else{ // Fragmented block
414 frag_size_pointed = pow(2, mdp1->heapinfo[block_pointed1].type);
415 address_pointed1 = (char*)mdp1 + ((char*)address_pointed1 - (char*)s_heap);
416 address_pointed2 = (char*)mdp2 + ((char*)address_pointed2 - (char*)s_heap);
417 //addr_block_pointed1 = (void*) (((ADDR2UINT((size_t)block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1);
418 //addr_block_pointed2 = (void*) (((ADDR2UINT((size_t)block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2);
419 addr_block_pointed1 = heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
420 addr_block_pointed2 = heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
422 while(address_pointed1 > (void*)((char*)addr_block_pointed1 + ((frag_pointed1 + 1) * frag_size))){
426 while(address_pointed2 > (void*)((char*)addr_block_pointed2 + ((frag_pointed2 + 1) * frag_size))){
429 if(mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1] == mdp2->heapinfo[block_pointed2].busy_frag.frag_size[frag_pointed2]){
430 fprintf(stderr, "Memcmp between fragments %d (block %d) and %d (block %d) (size = %d)\n", frag_pointed1, block_pointed1, frag_pointed2, block_pointed2, mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1]);
431 if(memcmp((char*)addr_block_pointed1 + (frag_pointed1 * frag_size), (char*)addr_block_pointed2 + (frag_pointed2 * frag_size), mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1]) != 0){
434 fprintf(stderr, "False difference detected\n");
451 fprintf(stderr,"\nDifferent data in fragment %zu (size = %zu, size used = %hu) in block %zu \n", j, frag_size, mdp1->heapinfo[i].busy_frag.frag_size[j], i);
452 fprintf(stderr, "Hamming distance between fragments : %d\n", distance);
453 mmalloc_backtrace_fragment_display(mdp1, i, j);
454 mmalloc_backtrace_fragment_display(mdp2, i, j);
464 }else{ /* free block */
468 //fprintf(stderr,"\n");
476 //free(pointed_address1);
477 //free(pointed_address2);
483 void *get_end_addr_heap(void *s_heap){
485 FILE *fp; /* File pointer to process's proc maps file */
486 char *line = NULL; /* Temporal storage for each line that is readed */
487 ssize_t read; /* Number of bytes readed */
488 size_t n = 0; /* Amount of bytes to read by getline */
490 fp = fopen("/proc/self/maps", "r");
493 perror("fopen failed");
496 xbt_dynar_t lfields = NULL;
497 xbt_dynar_t start_end = NULL;
501 while ((read = getline(&line, &n, fp)) != -1) {
503 xbt_str_trim(line, NULL);
504 xbt_str_strip_spaces(line);
505 lfields = xbt_str_split(line,NULL);
507 start_end = xbt_str_split(xbt_dynar_get_as(lfields, 0, char*), "-");
508 start_addr = (void *) strtoul(xbt_dynar_get_as(start_end, 0, char*), NULL, 16);
509 end_addr = (void *) strtoul(xbt_dynar_get_as(start_end, 1, char*), NULL, 16);
511 if(start_addr == s_heap){
514 xbt_dynar_reset(lfields);
516 xbt_dynar_reset(start_end);
523 xbt_dynar_reset(lfields);
525 xbt_dynar_reset(start_end);
535 void mmalloc_display_info_heap(xbt_mheap_t h){