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 void mmalloc_backtrace_display(xbt_mheap_t mdp, void *ptr){
18 size_t block = BLOCK(ptr);
22 if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
23 fprintf(stderr,"Ouch, this pointer is not mine. I cannot display its backtrace. I refuse it to death!!\n");
27 type = mdp->heapinfo[block].type;
30 //fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
33 if (mdp->heapinfo[block].busy_block.bt_size == 0) {
34 fprintf(stderr,"No backtrace available for that block, sorry.\n");
38 memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
39 e.used = mdp->heapinfo[block].busy_block.bt_size;
41 xbt_ex_setup_backtrace(&e);
43 fprintf(stderr, "(backtrace not set)\n");
44 } else if (e.bt_strings == NULL) {
45 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
49 fprintf(stderr, "Backtrace of where the block %p was malloced (%d frames):\n",ptr,e.used);
50 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
51 fprintf(stderr,"%d",i);fflush(NULL);
52 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
58 void mmalloc_backtrace_block_display(xbt_mheap_t mdp, size_t block){
63 type = mdp->heapinfo[block].type;
66 fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
69 if (mdp->heapinfo[block].busy_block.bt_size == 0) {
70 fprintf(stderr,"No backtrace available for that block, sorry.\n");
74 memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
75 e.used = mdp->heapinfo[block].busy_block.bt_size;
77 xbt_ex_setup_backtrace(&e);
79 fprintf(stderr, "(backtrace not set)\n");
80 } else if (e.bt_strings == NULL) {
81 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
85 fprintf(stderr, "Backtrace of where the block %zu was malloced (%d frames):\n", block ,e.used);
86 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
87 fprintf(stderr,"%d",i);fflush(NULL);
88 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
93 void mmalloc_backtrace_fragment_display(xbt_mheap_t mdp, size_t block, size_t frag){
97 memcpy(&e.bt,&(mdp->heapinfo[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
98 e.used = XBT_BACKTRACE_SIZE;
100 xbt_ex_setup_backtrace(&e);
102 fprintf(stderr, "(backtrace not set)\n");
103 } else if (e.bt_strings == NULL) {
104 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
108 fprintf(stderr, "Backtrace of where the fragment %zu in block %zu was malloced (%d frames):\n", frag, block ,e.used);
109 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
110 fprintf(stderr,"%d",i);fflush(NULL);
111 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
116 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2){
118 if(mdp1 == NULL && mdp2 == NULL){
119 fprintf(stderr, "Malloc descriptors null\n");
123 int errors = mmalloc_compare_mdesc(mdp1, mdp2);
129 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
133 if(mdp1->heaplimit != mdp2->heaplimit){
134 fprintf(stderr,"Different limit of valid info table indices\n");
138 void* s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
140 void *heapbase1 = (char *)mdp1 + BLOCKSIZE;
141 void *heapbase2 = (char *)mdp2 + BLOCKSIZE;
143 void * breakval1 = (char *)mdp1 + ((char *)mdp1->breakval - (char *)s_heap);
144 void * breakval2 = (char *)mdp2 + ((char *)mdp2->breakval - (char *)s_heap);
147 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
148 size_t frag_size = 0; /* FIXME: arbitrary initialization */
154 int total_distance = 0;
157 void *address_pointed1, *address_pointed2;
159 int block_pointed1, block_pointed2;
160 void *addr_block_pointed1, *addr_block_pointed2;
161 int frag_pointed1, frag_pointed2;
163 /* Check busy blocks*/
165 while(i < mdp1->heaplimit){
167 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
168 fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
172 addr_block1 = (char*)heapbase1 + ((i-1)*BLOCKSIZE);
173 xbt_assert(addr_block1 < breakval1, "Block address out of heap memory used");
175 addr_block2 = (char*)heapbase2 + ((i-1)*BLOCKSIZE);
176 xbt_assert(addr_block2 < breakval2, "Block address out of heap memory used");
178 if(mdp1->heapinfo[i].type == 0){ /* busy large block */
180 if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
181 fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
186 if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
187 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);
193 /* Hamming distance on different blocks */
196 //for(k=0;k<mdp1->heapinfo[i].busy_block.size * BLOCKSIZE;k++){
197 for(k=0;k<mdp1->heapinfo[i].busy_block.busy_size;k++){
198 if(memcmp(((char *)addr_block1) + k, ((char *)addr_block2) + k, 1) != 0){
199 //fprintf(stderr, "Different byte (offset=%d) (%p - %p) in block %zu\n", k, (char *)addr_block1 + k, (char *)addr_block2 + k, i); fflush(NULL);
200 pointer_align = (k / sizeof(void*)) * sizeof(void*);
201 address_pointed1 = *((void **)((char *)addr_block1 + pointer_align));
202 address_pointed2 = *((void **)((char *)addr_block2 + pointer_align));
203 if(((address_pointed1 > (void *)s_heap) && (address_pointed1 < mdp1->breakval)) && ((address_pointed2 > (void *)s_heap) && (address_pointed2 < mdp2->breakval))){
204 block_pointed1 = ((char*)address_pointed1 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
205 block_pointed2 = ((char*)address_pointed2 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
206 //fprintf(stderr, "Blocks pointed : %d - %d\n", block_pointed1, block_pointed2);
207 if(mdp1->heapinfo[block_pointed1].type == mdp2->heapinfo[block_pointed2].type){
208 if(mdp1->heapinfo[block_pointed1].type == 0){ // Large block
209 while(mdp1->heapinfo[block_pointed1].busy_block.size == 0)
211 while(mdp2->heapinfo[block_pointed2].busy_block.size == 0)
213 if(mdp1->heapinfo[block_pointed1].busy_block.busy_size == mdp2->heapinfo[block_pointed2].busy_block.busy_size){
214 addr_block_pointed1 = (char*)heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
215 addr_block_pointed2 = (char*)heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
217 fprintf(stderr, "Memcmp between blocks %d and %d (size = %zu)\n", block_pointed1, block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size);
218 if(memcmp(addr_block_pointed1, addr_block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size) != 0){
221 fprintf(stderr, "False difference detected\n");
226 }else{ // Fragmented block
227 address_pointed1 = (char*)mdp1 + ((char*)address_pointed1 - (char*)s_heap);
228 address_pointed2 = (char*)mdp2 + ((char*)address_pointed2 - (char*)s_heap);
229 addr_block_pointed1 = (char*)heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
230 addr_block_pointed2 = (char*)heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
232 while(address_pointed1 > (void*)((char*)addr_block_pointed1 + ((frag_pointed1 +1 ) * frag_size))){
236 while(address_pointed2 > (void*)((char*)addr_block_pointed2 + ((frag_pointed2 +1) * frag_size))){
239 if(mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1] == mdp2->heapinfo[block_pointed2].busy_frag.frag_size[frag_pointed2]){
240 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]);
241 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){
244 fprintf(stderr, "False difference detected\n");
253 fprintf(stderr, "Pointers on blocks with different types \n");
257 fprintf(stderr, "Pointed addresses (%p - %p) not in std_heap\n", address_pointed1, address_pointed2);
266 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);
268 fprintf(stderr, "Hamming distance between blocks : %d\n", distance);
269 mmalloc_backtrace_block_display(mdp1, i);
270 mmalloc_backtrace_block_display(mdp2, i);
271 fprintf(stderr, "\n");
273 total_distance += distance;
281 if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
283 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
284 fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type); fflush(NULL);
288 if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
289 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);
293 if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
294 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);
298 frag_size = pow(2, mdp1->heapinfo[i].type);
300 for(j=0; j< (BLOCKSIZE/frag_size); j++){
302 if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
303 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);
307 if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
309 addr_frag1 = (char *)addr_block1 + (j * frag_size);
310 xbt_assert(addr_frag1 < breakval1, "Fragment address out of heap memory used");
312 addr_frag2 = (char *)addr_block2 + (j * frag_size);
313 xbt_assert(addr_frag1 < breakval1, "Fragment address out of heap memory used");
315 /* Hamming distance on different blocks */
317 //for(k=0;k<frag_size;k++){
318 for(k=0;k<mdp1->heapinfo[i].busy_frag.frag_size[j];k++){
319 if(memcmp(((char *)addr_frag1) + k, ((char *)addr_frag2) + k, 1) != 0){
320 //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);
321 pointer_align = (k / sizeof(void*)) * sizeof(void*);
322 address_pointed1 = *((void **)((char *)addr_frag1 + pointer_align));
323 address_pointed2 = *((void **)((char *)addr_frag2 + pointer_align));
324 if(((address_pointed1 > (void *)s_heap) && (address_pointed1 < mdp1->breakval)) && ((address_pointed2 > (void *)s_heap) && (address_pointed2 < mdp2->breakval))){
325 block_pointed1 = ((char*)address_pointed1 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
326 block_pointed2 = ((char*)address_pointed2 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
327 //fprintf(stderr, "Blocks pointed : %d - %d\n", block_pointed1, block_pointed2);
328 if(mdp1->heapinfo[block_pointed1].type == mdp2->heapinfo[block_pointed2].type){
329 if(mdp1->heapinfo[block_pointed1].type == 0){ // Large block
330 while(mdp1->heapinfo[block_pointed1].busy_block.size == 0)
332 while(mdp2->heapinfo[block_pointed2].busy_block.size == 0)
334 if(mdp1->heapinfo[block_pointed1].busy_block.busy_size == mdp2->heapinfo[block_pointed2].busy_block.busy_size){
335 addr_block_pointed1 = (char*)heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
336 addr_block_pointed2 = (char*)heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
337 fprintf(stderr, "Memcmp between blocks %d and %d (size = %zu)\n", block_pointed1, block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size);
338 if(memcmp(addr_block_pointed1, addr_block_pointed2, mdp1->heapinfo[block_pointed1].busy_block.busy_size) != 0){
341 fprintf(stderr, "False difference detected\n");
346 }else{ // Fragmented block
347 address_pointed1 = (char*)mdp1 + ((char*)address_pointed1 - (char*)s_heap);
348 address_pointed2 = (char*)mdp2 + ((char*)address_pointed2 - (char*)s_heap);
349 addr_block_pointed1 = (char*)heapbase1 + ((block_pointed1 - 1)*BLOCKSIZE);
350 addr_block_pointed2 = (char*)heapbase2 + ((block_pointed2 - 1)*BLOCKSIZE);
352 while(address_pointed1 > (void*)((char*)addr_block_pointed1 + ((frag_pointed1 + 1) * frag_size))){
356 while(address_pointed2 > (void*)((char*)addr_block_pointed2 + ((frag_pointed2 + 1) * frag_size))){
359 if(mdp1->heapinfo[block_pointed1].busy_frag.frag_size[frag_pointed1] == mdp2->heapinfo[block_pointed2].busy_frag.frag_size[frag_pointed2]){
360 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]);
361 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){
364 fprintf(stderr, "False difference detected\n");
371 fprintf(stderr, "Pointers on blocks with different types \n");
375 fprintf(stderr, "Pointed addresses (%p - %p) not in std_heap\n", address_pointed1, address_pointed2);
383 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);
384 fprintf(stderr, "Hamming distance between fragments : %d\n", distance);
385 mmalloc_backtrace_fragment_display(mdp1, i, j);
386 mmalloc_backtrace_fragment_display(mdp2, i, j);
387 fprintf(stderr, "\n");
389 total_distance += distance;
398 }else{ /* free block */
409 fprintf(stderr, "Hamming distance between heap regions : %d\n", total_distance);
415 /* void *get_end_addr_heap(void *heap){ */
417 /* FILE *fp; /\* File pointer to process's proc maps file *\/ */
418 /* char *line = NULL; /\* Temporal storage for each line that is readed *\/ */
419 /* ssize_t read; /\* Number of bytes readed *\/ */
420 /* size_t n = 0; /\* Amount of bytes to read by getline *\/ */
422 /* fp = fopen("/proc/self/maps", "r"); */
425 /* perror("fopen failed"); */
428 /* xbt_dynar_t lfields = NULL; */
429 /* xbt_dynar_t start_end = NULL; */
430 /* void *start_addr; */
431 /* void *end_addr; */
433 /* while ((read = getline(&line, &n, fp)) != -1) { */
435 /* xbt_str_trim(line, NULL); */
436 /* xbt_str_strip_spaces(line); */
437 /* lfields = xbt_str_split(line,NULL); */
439 /* start_end = xbt_str_split(xbt_dynar_get_as(lfields, 0, char*), "-"); */
440 /* start_addr = (void *) strtoul(xbt_dynar_get_as(start_end, 0, char*), NULL, 16); */
441 /* end_addr = (void *) strtoul(xbt_dynar_get_as(start_end, 1, char*), NULL, 16); */
443 /* if(start_addr == heap){ */
446 /* xbt_dynar_reset(lfields); */
447 /* xbt_free(lfields); */
448 /* xbt_dynar_reset(start_end); */
449 /* xbt_free(start_end); */
450 /* return end_addr; */
455 /* xbt_dynar_reset(lfields); */
456 /* xbt_free(lfields); */
457 /* xbt_dynar_reset(start_end); */
458 /* xbt_free(start_end); */
467 void mmalloc_display_info_heap(xbt_mheap_t h){