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);
56 void mmalloc_backtrace_block_display(xbt_mheap_t mdp, size_t block){
61 type = mdp->heapinfo[block].type;
64 fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
67 if (mdp->heapinfo[block].busy_block.bt_size == 0) {
68 fprintf(stderr,"No backtrace available for that block, sorry.\n");
72 memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
73 e.used = mdp->heapinfo[block].busy_block.bt_size;
75 xbt_ex_setup_backtrace(&e);
77 fprintf(stderr, "(backtrace not set)\n");
78 } else if (e.bt_strings == NULL) {
79 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
83 fprintf(stderr, "Backtrace of where the block %zu where malloced (%d frames):\n", block ,e.used);
84 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
85 fprintf(stderr,"%d",i);fflush(NULL);
86 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
91 void mmalloc_backtrace_fragment_display(xbt_mheap_t mdp, size_t block, size_t frag){
95 memcpy(&e.bt,&(mdp->heapinfo[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
96 e.used = XBT_BACKTRACE_SIZE;
98 xbt_ex_setup_backtrace(&e);
100 fprintf(stderr, "(backtrace not set)\n");
101 } else if (e.bt_strings == NULL) {
102 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
106 fprintf(stderr, "Backtrace of where the fragment %zu in block %zu where malloced (%d frames):\n", frag, block ,e.used);
107 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
108 fprintf(stderr,"%d",i);fflush(NULL);
109 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
114 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void* s_heap, void* r_heap){
116 if(mdp1 == NULL && mdp2 == NULL){
117 fprintf(stderr, "Malloc descriptors null\n");
121 int errors = mmalloc_compare_mdesc(mdp1, mdp2, s_heap, r_heap);
127 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void* s_heap, void* r_heap){
131 /*if(mdp1->headersize != mdp2->headersize){
132 fprintf(stderr, "Different size of the file header for the mapped files\n");
136 if(mdp1->refcount != mdp2->refcount){
137 fprintf(stderr, "Different number of processes that attached the heap\n");
141 if(strcmp(mdp1->magic, mdp2->magic) != 0){
142 fprintf(stderr,"Different magic number\n");
146 if(mdp1->flags != mdp2->flags){
147 fprintf(stderr,"Different flags\n");
151 if(mdp1->heapsize != mdp2->heapsize){
152 fprintf(stderr,"Different number of info entries\n");
156 if(mdp1->heapbase != mdp2->heapbase){
157 fprintf(stderr,"Different first block of the heap\n");
161 if(mdp1->heapindex != mdp2->heapindex){
162 fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
166 if(mdp1->base != mdp2->base){
167 fprintf(stderr,"Different base address of the memory region\n");
171 if(mdp1->breakval != mdp2->breakval){
172 fprintf(stderr,"Different current location in the memory region\n");
176 if(mdp1->top != mdp2->top){
177 fprintf(stderr,"Different end of the current location in the memory region\n");
181 if(mdp1->heaplimit != mdp2->heaplimit){
182 fprintf(stderr,"Different limit of valid info table indices\n");
186 if(mdp1->fd != mdp2->fd){
187 fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
191 if(mdp1->version != mdp2->version){
192 fprintf(stderr,"Different version of the mmalloc package\n");
198 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
207 /* Check busy blocks*/
209 while(i < mdp1->heapindex){
211 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
212 fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
216 addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + (i * BLOCKSIZE);
217 addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + (i * BLOCKSIZE);
219 if(mdp1->heapinfo[i].type == 0){ /* busy large block */
221 if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
222 fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
226 if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
227 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);
231 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy_block.busy_size)) != 0){
232 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);
234 /* Hamming distance on different blocks */
236 for(k=0;k<mdp1->heapinfo[i].busy_block.busy_size;k++){
237 if(memcmp(((char *)addr_block1) + k, ((char *)addr_block2) + k, 1) != 0){
238 fprintf(stderr, "Different byte (offset=%d) (%p - %p) in block %zu\n", k, (char *)addr_block1 + k, (char *)addr_block2 + k, i);
240 pointer_align = (k / sizeof(void*)) * sizeof(void*);
241 fprintf(stderr, "Pointed address : %p (in %s) - %p (in %s)\n", *((void **)((char *)addr_block1 + pointer_align)), get_addr_memory_map(*((void **)((char *)addr_block1 + pointer_align)), s_heap, r_heap), *((void **)((char *)addr_block2 + pointer_align)),get_addr_memory_map(*((void **)((char *)addr_block2 + pointer_align)), s_heap, r_heap) );
245 fprintf(stderr, "Hamming distance between blocks : %d\n", distance);
247 mmalloc_backtrace_block_display(mdp1, i);
248 mmalloc_backtrace_block_display(mdp2, i);
253 i = i + mdp1->heapinfo[i].busy_block.size;
257 if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
259 if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
260 fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
264 if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
265 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);
269 if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
270 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);
274 frag_size = pow(2, mdp1->heapinfo[i].type);
276 for(j=0; j< (BLOCKSIZE/frag_size); j++){
278 if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
279 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]);
283 if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
285 addr_frag1 = (char *)addr_block1 + (j * frag_size);
286 addr_frag2 = (char *)addr_block2 + (j * frag_size);
289 if(memcmp(addr_frag1, addr_frag2, mdp1->heapinfo[i].busy_frag.frag_size[j]) != 0){
290 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);
292 /* Hamming distance on different blocks */
294 for(k=0;k<mdp1->heapinfo[i].busy_frag.frag_size[j];k++){
295 if(memcmp(((char *)addr_frag1) + k, ((char *)addr_frag2) + k, 1) != 0){
296 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);
298 pointer_align = (k / sizeof(void*)) * sizeof(void*);
299 fprintf(stderr, "Pointed address : %p (in %s) - %p (in %s)\n", *((void **)((char *)addr_frag1 + pointer_align)), get_addr_memory_map(*((void **)((char *)addr_frag1 + pointer_align)), s_heap, r_heap), *((void **)((char *)addr_frag2 + pointer_align)), get_addr_memory_map(*((void **)((char *)addr_frag2 + pointer_align)), s_heap, r_heap));
303 fprintf(stderr, "Hamming distance between fragments : %d\n", distance);
305 mmalloc_backtrace_fragment_display(mdp1, i, j);
306 mmalloc_backtrace_fragment_display(mdp2, i, j);
315 }else{ /* free block */
330 const char* get_addr_memory_map(void *addr, void* s_heap, void* r_heap){
332 FILE *fp; /* File pointer to process's proc maps file */
333 char *line = NULL; /* Temporal storage for each line that is readed */
334 ssize_t read; /* Number of bytes readed */
335 size_t n = 0; /* Amount of bytes to read by getline */
337 fp = fopen("/proc/self/maps", "r");
340 perror("fopen failed");
345 char *lfields[6], *start, *end, *endptr;
350 while ((read = getline(&line, &n, fp)) != -1) {
352 line[read - 1] = '\0';
354 lfields[0] = strtok(line, " ");
356 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
357 lfields[i] = strtok(NULL, " ");
360 start = strtok(lfields[0], "-");
361 start_addr = (void *) strtoul(start, &endptr, 16);
363 if(start_addr == s_heap)
364 lfields[5] = strdup("std_heap");
365 if(start_addr == r_heap)
366 lfields[5] = strdup("raw_heap");
367 end = strtok(NULL, "-");
368 end_addr = (void *) strtoul(end, &endptr, 16);
370 if((addr > start_addr) && ( addr < end_addr)){
373 if(lfields[5] != NULL){
382 return "Unknown area";
386 void mmalloc_display_info_heap(xbt_mheap_t h){