# New Ticket Created by Andy Dougherty # Please include the string: [perl #38576] # in the subject line of all future correspondence about this issue. # <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=38576 >
While trying to debug an "Out of mem" PANIC, I decided to try using DETAIL_MEMORY_DEBUG in src/memory.c. Alas, it didn't help for two reasons: 1. It prints to standard output, so that when you try to rebuild parrot, you get things like $ make parrot Invoking Parrot to generate runtime/parrot/include/config.fpmc --cross your fingers ./miniparrot config_lib.pasm > runtime/parrot/include/config.fpmc perl5.6 tools/build/parrot_config_c.pl > src/parrot_config.c [ . . . ] cc -o parrot [. . . ] src/parrot_config.o [ . . . ] which ends up putting all that debug information into src/parrot_config.o, and, eventually, into parrot. The resulting parrot dumps core immediately. Not very helpful. 2. Some (but not all) of the debugging statements were *after* the call to PANIC. That means that you never get to see the call to the function that triggered the panic. The following patch to src/memory.c re-orders statements, and prints to stderr instead of stdout. I know stderr might not be available in all situations, but this at least can work sometimes. Finally, I put in a guard against calling free(0). It might not be necessary, but we do end up trying to call it, and old habits die hard.:-). --- parrot-current/src/memory.c Sat Feb 11 12:23:31 2006 +++ parrot-andy/src/memory.c Wed Feb 15 12:36:51 2006 @@ -41,11 +41,11 @@ mem_sys_allocate(size_t size) { void *ptr = malloc((size_t)size); - if (!ptr) - PANIC("Out of mem"); #ifdef DETAIL_MEMORY_DEBUG - printf("Allocated %i at %p\n", size, ptr); + fprintf(stderr, "Allocated %i at %p\n", size, ptr); #endif + if (!ptr) + PANIC("Out of mem"); return ptr; } @@ -54,7 +54,7 @@ { void *ptr = malloc((size_t)size); #ifdef DETAIL_MEMORY_DEBUG - printf("Internal malloc %i at %p (%s/%d)\n", size, ptr, file, line); + fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", size, ptr, file, line); #endif if (!ptr) PANIC("Out of mem"); @@ -76,11 +76,11 @@ mem_sys_allocate_zeroed(size_t size) { void *ptr = calloc(1, (size_t)size); - if (!ptr) - PANIC("Out of mem"); #ifdef DETAIL_MEMORY_DEBUG - printf("Allocated %i at %p\n", size, ptr); + fprintf(stderr, "Allocated %i at %p\n", size, ptr); #endif + if (!ptr) + PANIC("Out of mem"); return ptr; } @@ -88,11 +88,11 @@ mem__internal_allocate_zeroed(size_t size, const char *file, int line) { void *ptr = calloc(1, (size_t)size); - if (!ptr) - PANIC("Out of mem"); #ifdef DETAIL_MEMORY_DEBUG - printf("Internal malloc %i at %p (%s/%d)\n", size, ptr, file, line); + fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", size, ptr, file, line); #endif + if (!ptr) + PANIC("Out of mem"); return ptr; } @@ -112,14 +112,14 @@ { void *ptr; #ifdef DETAIL_MEMORY_DEBUG - printf("Freed %p (realloc -- %i bytes)\n", from, size); + fprintf(stderr, "Freed %p (realloc -- %i bytes)\n", from, size); #endif ptr = realloc(from, size); - if (!ptr) - PANIC("Out of mem"); #ifdef DETAIL_MEMORY_DEBUG - printf("Allocated %i at %p\n", size, ptr); + fprintf(stderr, "Allocated %i at %p\n", size, ptr); #endif + if (!ptr) + PANIC("Out of mem"); return ptr; } @@ -127,12 +127,12 @@ mem__internal_realloc(void *from, size_t size, const char *file, int line) { void *ptr = realloc(from, size); - if (!ptr) - PANIC("Out of mem"); #ifdef DETAIL_MEMORY_DEBUG - printf("internal free of %p (realloc -- %i bytes) (%s/%d)\n", from, size, file, line); - printf("Internal malloc %i at %p (%s/%d)\n", size, ptr, file, line); + fprintf(stderr, "internal free of %p (realloc -- %i bytes) (%s/%d)\n", from, size, file, line); + fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", size, ptr, file, line); #endif + if (!ptr) + PANIC("Out of mem"); return ptr; } #undef interpreter @@ -152,16 +152,17 @@ mem_sys_free(void *from) { #ifdef DETAIL_MEMORY_DEBUG - printf("Freed %p\n", from); + fprintf(stderr, "Freed %p\n", from); #endif - free(from); + if (from) + free(from); } void mem__internal_free(void *from, const char *file, int line) { #ifdef DETAIL_MEMORY_DEBUG - printf("Internal free of %p (%s/%d)\n", from, file, line); + fprintf(stderr, "Internal free of %p (%s/%d)\n", from, file, line); #endif free(from); } -- Andy Dougherty [EMAIL PROTECTED]