Changeset 10998
- Timestamp:
- Jan 9, 2007, 12:23:05 PM (19 years ago)
- Files:
-
- 3 edited
-
branches/jch-memory/psLib/src/sys/psMemory.c (modified) (5 diffs)
-
branches/jch-memory/psLib/src/sys/psMemory.h (modified) (2 diffs)
-
trunk/psLib/configure.ac (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/jch-memory/psLib/src/sys/psMemory.c
r10940 r10998 10 10 * @author Joshua Hoblitt, University of Hawaii 11 11 * 12 * @version $Revision: 1.88.2.2 1$ $Name: not supported by cvs2svn $13 * @date $Date: 2007-01-0 5 21:26:52$12 * @version $Revision: 1.88.2.22 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2007-01-09 22:23:05 $ 14 14 * 15 15 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 16 16 */ 17 18 #if HAVE_CONFIG_H 19 # include <config.h> 20 #endif 17 21 18 22 #define PS_ALLOW_MALLOC // we're allowed to call malloc() … … 23 27 #include <string.h> 24 28 #include <assert.h> 29 30 #if defined(PS_MEM_BACKTRACE) && defined(HAVE_BACKTRACE) 31 # include <execinfo.h> 32 #endif 25 33 26 34 #include "psMemory.h" … … 424 432 memBlock->func = func; 425 433 434 #if defined(PS_MEM_BACKTRACE) && defined(HAVE_BACKTRACE) 435 #define BACKTRACE_BUFFER_SIZE 32 436 // psMemBlock.func is a 'const char *', so basically we're going to abuse 437 // that and treat it as a void ** to carry around backtrace information. 438 // psMemBlock is not ifdef'd to make sure that psMemBlock is always the 439 // same size & layout reguardless of the pslib .so that's being linked 440 // against 441 void **bt = malloc(BACKTRACE_BUFFER_SIZE * sizeof(void *)); 442 if (bt == NULL) { 443 PS_MEM_ABORT(__func__, "Failed to allocate memmory for backtrace buffer: %zd bytes at %s (%s:%d)", 32 * sizeof(void *), func, file, lineno); 444 } 445 *(size_t *)&memBlock->backtraceSize = backtrace(bt, BACKTRACE_BUFFER_SIZE); 446 *(void ***)&memBlock->backtrace = bt; 447 #endif // ifdef HAVE_BACKTRACE 448 426 449 // free function 427 450 memBlock->freeFunc = NULL; … … 548 571 MUTEX_LOCK(&memBlockListMutex); 549 572 550 for (psMemBlock *memBlock = topBlock; memBlock != NULL; memBlock = memBlock->nextBlock) { 573 // find the very first memblock 574 psMemBlock *memBlock = NULL; 575 for (memBlock = topBlock; memBlock->nextBlock != NULL; memBlock = memBlock->nextBlock) { } 576 577 // iterate through the block list starting with the oldest block 578 for (; memBlock != NULL; memBlock = memBlock->previousBlock) { 551 579 if ( (memBlock->refCounter > 0) && 552 580 ( (persistence) || (!persistence && !memBlock->persistent) ) && … … 557 585 if (fd != NULL) { 558 586 if (nleak == 1) { 559 fprintf(fd, " %20s:line ID\n", "file");587 fprintf(fd, "# func at (file:line) ID: X\n"); 560 588 } 561 589 562 fprintf(fd, " %20s:%-4d %lu\n", memBlock->file, (int)memBlock->lineno, (unsigned long)memBlock->id); 590 fprintf(fd, "%s at (%s:%d) ID: %lu", memBlock->func, memBlock->file, (int)memBlock->lineno, (unsigned long)memBlock->id); 591 #if defined(PS_MEM_BACKTRACE) && defined(HAVE_BACKTRACE) 592 593 size_t size = memBlock->backtraceSize; 594 char **strings = backtrace_symbols((void *const *)memBlock->backtrace, size); 595 596 fprintf(fd, " Alloc Call Depth: %zd\n", size); 597 598 for (int i = 0; i < size; i++) { 599 // always ident 600 int ident = 4; // initial indent 601 ident += 2 * i; // nesting depth 602 fprintf(fd, "%*s", ident, ""); 603 604 // if the caller was an anon function then strchr won't 605 // find a '(' in the string and will return NULL 606 char *caller = caller = strchr(strings[i], '('); 607 if (caller) { 608 // skip over the '(' 609 caller++; 610 // find the end of the symbol name 611 size_t callerLength = abs(strchr(caller, '+') - caller); 612 // print just the symbol name 613 for (int i = 0; i < callerLength; i++) { 614 fputc(caller[i],fd); 615 } 616 fprintf(fd, "\n"); 617 } else { 618 fprintf(fd, "(unknown)\n"); 619 } 620 } 621 622 free (strings); 623 #else // ifdef HAVE_BACKTRACE 624 // \n after "Memory Block ID" 625 fprintf(fd, "\n"); 626 #endif // ifdef HAVE_BACKTRACE 627 563 628 } 564 629 } -
branches/jch-memory/psLib/src/sys/psMemory.h
r10942 r10998 14 14 * @ingroup MemoryManagement 15 15 * 16 * @version $Revision: 1.61.2.1 0$ $Name: not supported by cvs2svn $17 * @date $Date: 2007-01-0 5 22:30:08$16 * @version $Revision: 1.61.2.11 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2007-01-09 22:23:05 $ 18 18 * 19 19 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 82 82 const unsigned int lineno; ///< set from __LINE__ in e.g. p_psAlloc 83 83 const char *func; ///< set from __func__ 84 #ifdef HAVE_BACKTRACE 85 86 const void **backtrace; ///< set from backtrace() 87 const size_t backtraceSize; ///< set from bracktrace() 88 #endif // ifdef HAVE_BACKTRACE 89 84 90 psReferenceCount refCounter; ///< how many times pointer is referenced 85 91 bool persistent; ///< marks if this non-user persistent data like error stack, etc. -
trunk/psLib/configure.ac
r10950 r10998 81 81 [tests=false]) 82 82 AM_CONDITIONAL(BUILD_TESTS, test x$tests = xtrue) 83 84 dnl turn on mem leak backtracing 85 AC_ARG_ENABLE(backtrace, 86 [AS_HELP_STRING(--enable-backtrace, enable memory allocation backtracing)], 87 [AC_MSG_RESULT(memory allocation backtracing enabled) 88 AC_DEFINE([PS_MEM_BACKTRACE], 1, [Define to 1 if you want memory backtracing]) 89 ] 90 ) 91 AC_CHECK_FUNCS_ONCE([backtrace]) 83 92 84 93 AC_LANG(C)
Note:
See TracChangeset
for help on using the changeset viewer.
