Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[simgrid.git] / src / xbt / mmalloc / mm_diff.c
1 /* mm_diff - Memory snapshooting and comparison                             */
2
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
9 #include "xbt/str.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
12                                 "Logging specific to mm_diff in mmalloc");
13
14 extern char *xbt_binary_name;
15
16 void mmalloc_backtrace_display(xbt_mheap_t mdp, void *ptr){
17   size_t block = BLOCK(ptr);
18   int type;
19   xbt_ex_t e;
20
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");
23     abort();
24   }
25
26   type = mdp->heapinfo[block].type;
27
28   if (type != 0) {
29     //fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
30     return;
31   }
32   if (mdp->heapinfo[block].busy_block.bt_size == 0) {
33     fprintf(stderr,"No backtrace available for that block, sorry.\n");
34     return;
35   }
36
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;
39
40   xbt_ex_setup_backtrace(&e);
41   if (e.used == 0) {
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");
45   } else {
46     int i;
47
48     fprintf(stderr, "Backtrace of where the block %p where 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);
52     }
53   }
54 }
55
56
57 void mmalloc_backtrace_block_display(xbt_mheap_t mdp, size_t block){
58
59   int type;
60   xbt_ex_t e;
61
62   type = mdp->heapinfo[block].type;
63
64   if (type != 0) {
65     fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
66     return;
67   }
68   if (mdp->heapinfo[block].busy_block.bt_size == 0) {
69     fprintf(stderr,"No backtrace available for that block, sorry.\n");
70     return;
71   }
72
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;
75
76   xbt_ex_setup_backtrace(&e);
77   if (e.used == 0) {
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");
81   } else {
82     int i;
83
84     fprintf(stderr, "Backtrace of where the block %zu where 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);
88     }
89   }
90 }
91
92 void mmalloc_backtrace_fragment_display(xbt_mheap_t mdp, size_t block, size_t frag){
93   
94   xbt_ex_t e;
95
96   memcpy(&e.bt,&(mdp->heapinfo[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
97   e.used = XBT_BACKTRACE_SIZE;
98
99   xbt_ex_setup_backtrace(&e);
100   if (e.used == 0) {
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");
104   } else {
105     int i;
106
107     fprintf(stderr, "Backtrace of where the fragment %zu in block %zu where 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);
111     }
112   }
113 }
114
115 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void* s_heap, void* r_heap){
116
117   if(mdp1 == NULL && mdp2 == NULL){
118     fprintf(stderr, "Malloc descriptors null\n");
119     return 0;
120   }
121
122   int errors = mmalloc_compare_mdesc(mdp1, mdp2, s_heap, r_heap);
123
124   return (errors > 0);
125
126 }
127
128 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void* s_heap, void* r_heap){
129
130   int errors = 0;
131
132   /*if(mdp1->headersize != mdp2->headersize){
133     fprintf(stderr, "Different size of the file header for the mapped files\n");
134     return 1;
135   }
136
137   if(mdp1->refcount != mdp2->refcount){
138     fprintf(stderr, "Different number of processes that attached the heap\n");
139     return 1;
140   }
141
142   if(strcmp(mdp1->magic, mdp2->magic) != 0){
143     fprintf(stderr,"Different magic number\n");
144     return 1;
145   }
146
147   if(mdp1->flags != mdp2->flags){
148     fprintf(stderr,"Different flags\n");  
149     return 1;
150   }
151
152   if(mdp1->heapsize != mdp2->heapsize){
153     fprintf(stderr,"Different number of info entries\n");
154     return 1;
155   }  
156
157   if(mdp1->heapbase != mdp2->heapbase){
158     fprintf(stderr,"Different first block of the heap\n");
159     return 1;
160   }
161
162   if(mdp1->heapindex != mdp2->heapindex){
163     fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
164     return 1;
165   }
166
167   if(mdp1->base != mdp2->base){
168     fprintf(stderr,"Different base address of the memory region\n");
169     return 1;
170   }
171
172   if(mdp1->breakval != mdp2->breakval){
173     fprintf(stderr,"Different current location in the memory region\n");
174     return 1;
175   }
176
177   if(mdp1->top != mdp2->top){
178     fprintf(stderr,"Different end of the current location in the memory region\n");
179     return 1;
180   }
181
182   if(mdp1->heaplimit != mdp2->heaplimit){
183     fprintf(stderr,"Different limit of valid info table indices\n");
184     return 1;
185   }
186
187   if(mdp1->fd != mdp2->fd){
188     fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
189     return 1;
190   }
191
192   if(mdp1->version != mdp2->version){
193     fprintf(stderr,"Different version of the mmalloc package\n");
194     return 1;
195     }*/
196
197
198   size_t i, j;
199   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
200   size_t frag_size;
201
202   i = 0;
203
204   int k;
205   int distance = 0;
206   int pointer_align;
207
208   /* Check busy blocks*/
209
210   while(i < mdp1->heapindex){
211
212     if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
213       fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
214       errors++;
215     }
216
217     addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + (i * BLOCKSIZE);
218     addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + (i * BLOCKSIZE);
219
220     if(mdp1->heapinfo[i].type == 0){ /* busy large block */
221
222       if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
223         fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
224         errors++;
225       } 
226
227       if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
228         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);
229         errors++;
230       } 
231
232       if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy_block.busy_size)) != 0){
233         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
235         /* Hamming distance on different blocks */
236         distance = 0;
237         for(k=0;k<mdp1->heapinfo[i].busy_block.busy_size;k++){
238           if(memcmp(((char *)addr_block1) + k, ((char *)addr_block2) + k, 1) != 0){
239             fprintf(stderr, "Different byte (offset=%d) (%p - %p) in block %zu\n", k, (char *)addr_block1 + k, (char *)addr_block2 + k, i); 
240             distance++;
241             pointer_align = (k / sizeof(void*)) * sizeof(void*); 
242             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) );
243           }
244         }
245
246         fprintf(stderr, "Hamming distance between blocks : %d\n", distance);
247
248         mmalloc_backtrace_block_display(mdp1, i);
249         mmalloc_backtrace_block_display(mdp2, i);
250         errors++;
251       }
252
253    
254       i = i + mdp1->heapinfo[i].busy_block.size;
255
256     }else{
257       
258       if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
259
260         if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
261           fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
262           errors++;
263         }
264
265         if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
266           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);
267           errors++;
268         } 
269         
270         if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
271           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);
272           errors++;
273         } 
274
275         frag_size = pow(2, mdp1->heapinfo[i].type);
276
277         for(j=0; j< (BLOCKSIZE/frag_size); j++){
278
279           if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
280             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]);
281             errors++;
282           }
283
284           if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
285             
286             addr_frag1 = (char *)addr_block1 + (j * frag_size);
287             addr_frag2 = (char *)addr_block2 + (j * frag_size);
288
289
290             if(memcmp(addr_frag1, addr_frag2, mdp1->heapinfo[i].busy_frag.frag_size[j]) != 0){
291               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
293               /* Hamming distance on different blocks */
294               distance = 0;
295               for(k=0;k<mdp1->heapinfo[i].busy_frag.frag_size[j];k++){
296                 if(memcmp(((char *)addr_frag1) + k, ((char *)addr_frag2) + k, 1) != 0){
297                   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                   distance++;
299                   pointer_align = (k / sizeof(void*)) * sizeof(void*);
300                   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));
301                 }
302               }
303
304               fprintf(stderr, "Hamming distance between fragments : %d\n", distance);
305
306               mmalloc_backtrace_fragment_display(mdp1, i, j);
307               mmalloc_backtrace_fragment_display(mdp2, i, j);
308               errors++;
309             }
310
311           }
312         }
313
314         i++;
315
316       }else{ /* free block */
317
318         i++;
319
320       }
321       
322     }
323
324   }
325
326
327   return (errors);
328 }
329
330
331 const char* get_addr_memory_map(void *addr, void* s_heap, void* r_heap){
332
333   FILE *fp;                     /* File pointer to process's proc maps file */
334   char *line = NULL;            /* Temporal storage for each line that is readed */
335   ssize_t read;                 /* Number of bytes readed */
336   size_t n = 0;                 /* Amount of bytes to read by getline */
337
338   fp = fopen("/proc/self/maps", "r");
339   
340   if(fp == NULL)
341     perror("fopen failed");
342
343   if(addr == NULL)
344     return "nil";
345
346   char *lfields[6], *start, *end, *endptr, *map;
347   void *start_addr;
348   void *end_addr;
349   int i;
350
351   while ((read = getline(&line, &n, fp)) != -1) {
352
353     line[read - 1] = '\0';
354
355     lfields[0] = strtok(line, " ");
356
357     for (i = 1; i < 5 && lfields[i - 1] != NULL ; i++) {
358       lfields[i] = strdup(strtok(NULL, " "));
359     }
360
361     map = strtok(NULL, " ");
362     if(map != NULL)
363       lfields[5] = strdup(map);
364     else
365       lfields[5] = strdup("Anonymous");
366
367     start = strtok(lfields[0], "-");
368     start_addr = (void *) strtoul(start, &endptr, 16); 
369    
370     if(start_addr == s_heap)
371       lfields[5] = strdup("std_heap");
372     if(start_addr == r_heap)
373       lfields[5] = strdup("raw_heap");
374
375     end = strtok(NULL, "-");
376     end_addr = (void *) strtoul(end, &endptr, 16); 
377    
378     if((addr > start_addr) && ( addr < end_addr)){
379       free(line);
380       fclose(fp);
381       return lfields[5];
382     }
383     
384   }
385
386   free(line);
387   fclose(fp);
388   return "Unknown area";
389
390 }
391
392 void mmalloc_display_info_heap(xbt_mheap_t h){
393
394 }
395
396
397