void countfree (const char *fname, int line, void *ptr) { mem *m ; /* fprintf( stderr, "Service mode = %d, alloca_hash = %p ptr = %p at %s:%d\n", service_mode, allocs_hash, ptr, fname, line );*/ if( service_mode > 0 || allocs_hash == NULL ) return ; if (ptr == NULL) { show_error("countfree:attempt to free NULL memory in %s:%d", fname, line); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif return; } m = count_find_and_extract (fname, line, ptr, C_MEM); if (m == NULL) { if( cleanup_mode == 0 ) { show_error( "countfree:attempt in %s:%d to free memory(%p) that was never allocated!", fname, line, ptr); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif } return; } #if 0 // this is invalid code!! if (m1->freed > 0) { fprintf (stderr, "%s:mem already freed %d time(s)!\n", __FUNCTION__, m1->freed); fprintf (stderr, "%s:freed from %s:%d\n", __FUNCTION__, (*m1).fname, (*m1).line); fprintf (stderr, "%s:called from %s:%d\n", __FUNCTION__, fname, line); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif /* exit (1); */ } else safefree (m1->ptr); m1->freed++; m1->fname = fname; m1->line = line; #else fprintf( stderr, "%s: freeing %p at %s:%d\n", __FUNCTION__, m->ptr, fname, line ); safefree (m->ptr); mem_destroy( (ASHashableValue)NULL, m ); #endif }
int as_assert (void *p, const char *fname, int line, const char *call) { if (p == NULL) { fprintf (stderr, "ASSERT FAILED in %s, line# %d (%s())\n", fname, line, call); print_simple_backtrace(); } return (p == NULL); }
static void sigsegv_handler (int signum #if defined(HAVE_SIGCONTEXT) , struct sigcontext sc #endif ) { static int level = 0; const char *MyName = get_application_name(); if (signum == SIGSEGV) { fprintf (stderr, "Segmentation Fault trapped"); if (level > 0) exit (1); /* sigsegv in sigsegv */ level++; fprintf (stderr, " in %s.\n", MyName); } else fprintf (stderr, "Non-critical Signal %d trapped in %s.\n", signum, MyName); #if defined(HAVE_SIGCONTEXT) print_diag_info (&sc); #else print_simple_backtrace (); #endif if (signum == SIGSEGV) { fprintf (stderr, "Please collect all the listed information and submit a bug report to <*****@*****.**>.\n"); fprintf (stderr, "If core dump was generated by this fault, please examine it with gdb and attach results to your report.\n"); fprintf (stderr, " You can use the following sequence to do so :\n"); fprintf (stderr, " gdb -core core /usr/bin/afterstep\n"); fprintf (stderr, " gdb>backtrace\n"); fprintf (stderr, " gdb>info frame\n"); fprintf (stderr, " gdb>info all-registers\n"); fprintf (stderr, " gdb>disassemble\n"); exit (1); } }
void * countrealloc (const char *fname, int line, void *ptr, size_t length) { if (ptr != NULL && length == 0) countfree (fname, line, ptr); if (length == 0) return NULL; if (ptr != NULL) { mem *m = NULL; ASHashResult res ; if( allocs_hash != NULL ) { ASHashData hd ; service_mode++ ; if( remove_hash_item (allocs_hash, AS_HASHABLE(ptr), &hd.vptr, False) == ASH_Success ) { m = hd.vptr ; if( (m->type & 0xff) != C_MEM ) { show_error( "while deallocating pointer 0x%lX discovered that it was allocated with different type", ptr ); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif m = NULL ; } } service_mode-- ; } if (m == NULL) { show_error ("countrealloc:attempt in %s:%d to realloc memory(%p) that was never allocated!\n", fname, line, ptr); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif return NULL; } if ((m->type & 0xff) == C_MEM) { total_alloc -= m->length; total_alloc += length; if (total_alloc > max_alloc) max_alloc = total_alloc; } else { total_x_alloc -= m->length; total_x_alloc += length; if (total_x_alloc > max_x_alloc) max_x_alloc = total_x_alloc; } m->fname = fname; m->line = line; m->length = length; m->type = C_MEM | C_REALLOC; m->ptr = saferealloc (ptr, length); m->freed = 0; ptr = m->ptr; if( (res = add_hash_item( allocs_hash, (ASHashableValue)ptr, m )) != ASH_Success ) { show_error( "failed to log allocation for pointer 0x%lX - result = %d", ptr, res); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif } reallocations++; } else ptr = countmalloc (fname, line, length); return ptr; }
void count_alloc (const char *fname, int line, void *ptr, size_t length, int type) { mem *m = NULL; ASHashResult res ; ASHashData hdata = {0}; if( service_mode > 0 ) return ; if( allocs_hash == NULL ) { service_mode++ ; allocs_hash = create_ashash( 256, pointer_hash_value, NULL, mem_destroy ); fprintf( stderr, "MEMORY AUDIT: count_alloc() called from %s:%d: allocs hash table created with pointer %p\n", fname, line, allocs_hash ); service_mode-- ; }else if( ptr == allocs_hash ) return; if( get_hash_item( allocs_hash, (ASHashableValue)ptr, &hdata.vptr ) == ASH_Success ) { m = (mem*)hdata.vptr ; if( type != (C_MEM|C_ADD_HASH_OPTIONAL_ITEM) ) { show_error( "Same pointer value 0x%lX is being counted twice!\n Called from %s:%d - previously allocated in %s:%d", (unsigned long)ptr, fname, line, m->fname, m->line ); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif }else return ; }else if( deallocated_used > 0 ) { m = deallocated_mem[--deallocated_used]; /* show_warning( "<mem> reusing deallocation cache - element %d, pointer %p auditing service memory used (%lu )\n Called from %s:%d", deallocated_used, m, total_service, fname, line ); */ }else { m = safecalloc (1, sizeof (mem)); if( total_service+sizeof(mem) > AUDIT_SERVICE_MEM_LIMIT ) { show_error( "<mem> too much auditing service memory used (%lu - was %lu)- aborting, please investigate.\n Called from %s:%d", total_service+sizeof(mem), total_service, fname, line ); print_simple_backtrace(); output_unfreed_mem (stderr); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif exit(0); } total_service += sizeof(mem); if( total_service > max_service ) max_service = total_service ; } m->fname = fname; m->line = line; m->length = length; m->type = type; m->ptr = ptr; m->freed = 0; allocations++; if ((type & 0xff) == C_MEM) { total_alloc += length; if (total_alloc > max_alloc) max_alloc = total_alloc; } else { total_x_alloc += length; if (total_x_alloc > max_x_alloc) max_x_alloc = total_x_alloc; } if (allocations - deallocations > max_allocations) max_allocations = allocations - deallocations; if( (res = add_hash_item( allocs_hash, (ASHashableValue)ptr, m )) != ASH_Success ) show_error( "failed to log allocation for pointer 0x%lX - result = %d", ptr, res); else { if( total_service+sizeof(ASHashItem) > AUDIT_SERVICE_MEM_LIMIT ) { show_error( "<add_hash_item> too much auditing service memory used (%lu - was %lu)- aborting, please investigate.\n Called from %s:%d", total_service+sizeof(ASHashItem), total_service, fname, line ); print_simple_backtrace(); #ifdef DEBUG_ALLOC_STRICT { char *segv = NULL ; *segv = 0 ; } #endif exit(0); } total_service += sizeof(ASHashItem); if( total_service > max_service ) max_service = total_service ; } }