From 31ba7e44380b3c4e26ae014eec9e423193e54797 Mon Sep 17 00:00:00 2001 From: Marion Guthmuller Date: Fri, 12 Oct 2012 13:41:03 +0200 Subject: [PATCH 1/1] model-checker : display malloc backtrace according to address --- include/xbt/mmalloc.h | 1 + src/xbt/mmalloc/mm_diff.c | 49 ++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/include/xbt/mmalloc.h b/include/xbt/mmalloc.h index 99d0b5bb47..6963c4a7e3 100644 --- a/include/xbt/mmalloc.h +++ b/include/xbt/mmalloc.h @@ -66,6 +66,7 @@ int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2); void mmalloc_backtrace_block_display(void* heapinfo, int block); void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag); +void mmalloc_backtrace_display(void *addr); int is_free_area(void *area, xbt_mheap_t heap); diff --git a/src/xbt/mmalloc/mm_diff.c b/src/xbt/mmalloc/mm_diff.c index 6115d9a1c3..efec23cec3 100644 --- a/src/xbt/mmalloc/mm_diff.c +++ b/src/xbt/mmalloc/mm_diff.c @@ -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; -- 2.20.1