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 */
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
11 "Logging specific to mm_diff in mmalloc");
13 extern char *xbt_binary_name;
15 void mmalloc_backtrace_display(xbt_mheap_t mdp, void *ptr){
16 size_t block = BLOCK(ptr);
20 if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
21 fprintf(stderr,"Ouch, this pointer is not mine. I cannot display its backtrace. I refuse it to death!!\n");
25 type = mdp->heapinfo[block].type;
28 //fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
31 if (mdp->heapinfo[block].busy_block.bt_size == 0) {
32 fprintf(stderr,"No backtrace available for that block, sorry.\n");
36 memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
37 e.used = mdp->heapinfo[block].busy_block.bt_size;
39 xbt_ex_setup_backtrace(&e);
41 fprintf(stderr, "(backtrace not set)\n");
42 } else if (e.bt_strings == NULL) {
43 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
47 fprintf(stderr, "Backtrace of where the block %p where malloced (%d frames):\n",ptr,e.used);
48 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
49 fprintf(stderr,"%d",i);fflush(NULL);
50 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
55 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void *std_heap_addr){
57 if(mdp1 == NULL && mdp2 == NULL){
58 XBT_DEBUG("Malloc descriptors null\n");
62 int errors = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr);
68 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr){
70 if(mdp1->headersize != mdp2->headersize){
71 fprintf(stderr, "Different size of the file header for the mapped files\n");
75 if(mdp1->refcount != mdp2->refcount){
76 fprintf(stderr, "Different number of processes that attached the heap\n");
80 if(strcmp(mdp1->magic, mdp2->magic) != 0){
81 fprintf(stderr,"Different magic number\n");
85 if(mdp1->flags != mdp2->flags){
86 fprintf(stderr,"Different flags\n");
90 if(mdp1->heapsize != mdp2->heapsize){
91 fprintf(stderr,"Different number of info entries\n");
96 if(mdp1->heapbase != mdp2->heapbase){
97 fprintf(stderr,"Different first block of the heap\n");
102 if(mdp1->heapindex != mdp2->heapindex){
103 fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
108 if(mdp1->base != mdp2->base){
109 fprintf(stderr,"Different base address of the memory region\n");
113 if(mdp1->breakval != mdp2->breakval){
114 fprintf(stderr,"Different current location in the memory region\n");
118 if(mdp1->top != mdp2->top){
119 fprintf(stderr,"Different end of the current location in the memory region\n");
123 if(mdp1->heaplimit != mdp2->heaplimit){
124 fprintf(stderr,"Different limit of valid info table indices\n");
128 if(mdp1->fd != mdp2->fd){
129 fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
133 if(mdp1->version != mdp2->version){
134 fprintf(stderr,"Different version of the mmalloc package\n");
140 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
145 /* Check busy blocks*/
147 while(i < mdp1->heapindex){
149 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
150 fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
154 addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + (i * BLOCKSIZE);
155 addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + (i * BLOCKSIZE);
157 if(mdp1->heapinfo[i].type == 0){ /* busy large block */
159 if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
160 fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
164 if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
165 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);
169 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy_block.busy_size)) != 0){
170 fprintf(stderr,"Different 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);
174 i = i + mdp1->heapinfo[i].busy_block.size;
178 if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
180 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
181 fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
185 if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
186 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);
190 if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
191 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);
195 for(j=0; j< MAX_FRAGMENT_PER_BLOCK; j++){
197 if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
198 fprintf(stderr,"Different busy_size for fragment %zu in block %zu : %u - %u\n", j, i, mdp1->heapinfo[i].busy_frag.frag_size[j], mdp2->heapinfo[i].busy_frag.frag_size[j]);
202 if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
204 frag_size = pow(2, mdp1->heapinfo[i].type);
205 addr_frag1 = (char *)addr_block1 + (j * frag_size);
206 addr_frag2 = (char *)addr_block2 + (j * frag_size);
208 if(memcmp(addr_frag1, addr_frag2, frag_size) != 0){
209 fprintf(stderr,"Different data in fragment %zu in block %zu \n", j, i);
218 }else{ /* free block */
233 void mmalloc_display_info_heap(xbt_mheap_t h){