Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not store any metadata where the user (was) legitimate to write
[simgrid.git] / src / xbt / mmalloc / mm_diff.c
index f32cb5d..efec23c 100644 (file)
@@ -15,7 +15,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
 
 extern char *xbt_binary_name;
 
-xbt_dynar_t mmalloc_ignore;
+xbt_dynar_t mc_comparison_ignore;
 xbt_dynar_t stacks_areas;
 
 static void heap_area_pair_free(heap_area_pair_t pair);
@@ -27,7 +27,7 @@ static heap_area_t new_heap_area(int block, int fragment);
 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals);
 
-static int in_mmalloc_ignore(int block, int fragment);
+static int in_mc_comparison_ignore(int block, int fragment);
 static size_t heap_comparison_ignore(void *address);
 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
@@ -39,7 +39,7 @@ void mmalloc_backtrace_block_display(void* heapinfo, int block){
   xbt_ex_t e;
 
   if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
-    XBT_DEBUG("No backtrace available for that block, sorry.");
+    fprintf(stderr, "No backtrace available for that block, sorry.\n");
     return;
   }
 
@@ -48,15 +48,15 @@ void mmalloc_backtrace_block_display(void* heapinfo, int block){
 
   xbt_ex_setup_backtrace(&e);
   if (e.used == 0) {
-    XBT_DEBUG("(backtrace not set)");
+    fprintf(stderr, "(backtrace not set)\n");
   } else if (e.bt_strings == NULL) {
-    XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
+    fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
   } else {
     int i;
 
-    XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
+    fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used);
     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
-      XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
+      fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
     }
   }
 
@@ -71,20 +71,51 @@ void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
 
   xbt_ex_setup_backtrace(&e);
   if (e.used == 0) {
-    XBT_DEBUG("(backtrace not set)");
+    fprintf(stderr, "(backtrace not set)\n");
   } else if (e.bt_strings == NULL) {
-    XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
+    fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
   } else {
     int i;
 
-    XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
+    fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used);
     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
-      XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
+      fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
     }
   }
 
 }
 
+void mmalloc_backtrace_display(void *addr){
+
+  size_t block, frag_nb;
+  int type;
+  
+  xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
+
+  block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1);
+
+  type = heap->heapinfo[block].type;
+
+  switch(type){
+  case -1 : /* Free block */
+    fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n");
+    xbt_abort();
+    break; 
+  case 0: /* Large block */
+    mmalloc_backtrace_block_display(heap->heapinfo, block);
+    break;
+  default: /* Fragmented block */
+    frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type;
+    if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
+      fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n");
+      xbt_abort();
+    }
+    mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb);
+    break;
+  }
+
+}
+
 
 void *s_heap, *heapbase1, *heapbase2;
 malloc_info *heapinfo1, *heapinfo2;
@@ -232,8 +263,8 @@ int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stac
         
             add_heap_area_pair(previous, current_block, -1, current_block, -1);
         
-            if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
-              if(in_mmalloc_ignore((int)current_block, -1))
+            if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
+              if(in_mc_comparison_ignore((int)current_block, -1))
                 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
               else
                 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
@@ -300,8 +331,8 @@ int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stac
         /* Comparison */
         add_heap_area_pair(previous, i1, -1, i2, -1);
         
-        if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
-          if(in_mmalloc_ignore((int)i1, -1))
+        if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
+          if(in_mc_comparison_ignore((int)i1, -1))
             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
           else
             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
@@ -334,7 +365,7 @@ int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stac
 
         current_fragment = j1;
 
-        if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
+        if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
           continue;
 
         if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
@@ -357,8 +388,8 @@ int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stac
                
                 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
             
-                if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
-                  if(in_mmalloc_ignore((int)current_block, (int)current_fragment))
+                if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
+                  if(in_mc_comparison_ignore((int)current_block, (int)current_fragment))
                     res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
                   else
                     res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
@@ -404,8 +435,8 @@ int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stac
             /* Comparison */
             add_heap_area_pair(previous, i1, j1, i2, j2);
             
-            if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
-              if(in_mmalloc_ignore((int)i1, (int)j1))
+            if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
+              if(in_mc_comparison_ignore((int)i1, (int)j1))
                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
               else
                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
@@ -522,16 +553,16 @@ static heap_area_t new_heap_area(int block, int fragment){
   return area;
 }
 
-static int in_mmalloc_ignore(int block, int fragment){
+static int in_mc_comparison_ignore(int block, int fragment){
 
   unsigned int cursor = 0;
   int start = 0;
-  int end = xbt_dynar_length(mmalloc_ignore) - 1;
+  int end = xbt_dynar_length(mc_comparison_ignore) - 1;
   mc_ignore_region_t region;
 
   while(start <= end){
     cursor = (start + end) / 2;
-    region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
+    region = (mc_ignore_region_t)xbt_dynar_get_as(mc_comparison_ignore, cursor, mc_ignore_region_t);
     if(region->block == block){
       if(region->fragment == fragment)
         return 1;
@@ -552,12 +583,12 @@ static int in_mmalloc_ignore(int block, int fragment){
 static size_t heap_comparison_ignore(void *address){
   unsigned int cursor = 0;
   int start = 0;
-  int end = xbt_dynar_length(mmalloc_ignore) - 1;
+  int end = xbt_dynar_length(mc_comparison_ignore) - 1;
   mc_ignore_region_t region;
 
   while(start <= end){
     cursor = (start + end) / 2;
-    region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
+    region = (mc_ignore_region_t)xbt_dynar_get_as(mc_comparison_ignore, cursor, mc_ignore_region_t);
     if(region->address == address)
       return region->size;
     if(region->address < address)
@@ -626,8 +657,8 @@ static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previ
 
           if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
 
-            if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
-              if(in_mmalloc_ignore(block_pointed1, -1))
+            if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
+              if(in_mc_comparison_ignore(block_pointed1, -1))
                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
               else
                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
@@ -654,8 +685,8 @@ static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previ
 
           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
 
-            if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
-              if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
+            if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
+              if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
               else
                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
@@ -688,8 +719,8 @@ static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previ
 
           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
 
-            if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
-              if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
+            if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
+              if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
               else
                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);