IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 14, 2010, 12:03:59 PM (16 years ago)
Author:
Paul Price
Message:

Add backtrace to abort and assertion output.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psAbort.c

    r28172 r28307  
    2323#include <stdarg.h>
    2424#include <stdlib.h>
     25#include <string.h>
    2526#include <unistd.h>
    2627
     28#if defined(HAVE_BACKTRACE)
     29#include <execinfo.h>
     30#define BACKTRACE_BUFFER_SIZE 256       // Maximum size of backtrace
     31#endif
     32
    2733#include "psAbort.h"
     34#include "psString.h"
     35#include "psMemory.h"
    2836#include "psError.h"
    2937#include "psLogMsg.h"
     38
     39// Write backtrace to log
     40static inline void psBacktrace(void)
     41{
     42#ifdef HAVE_BACKTRACE
     43    void **bt = psAlloc(BACKTRACE_BUFFER_SIZE * sizeof(void *)); // Backtrace information
     44    if (!bt) {
     45        psLogMsg("psLib.sys", PS_LOG_ABORT, "Unable to allocate memory for backtrace");
     46        return;
     47    }
     48    int size = backtrace(bt, BACKTRACE_BUFFER_SIZE);             // Size of backtrace
     49    char **strings = backtrace_symbols((void *const *)bt, size);
     50    psLogMsg("psLib.sys", PS_LOG_ABORT, "Backtrace depth: %d", size);
     51    for (int i = 0; i < size; i++) {
     52        // if the caller was an anon function then strchr won't
     53        // find a '(' in the string and will return NULL
     54        char *caller = strchr(strings[i], '(');
     55        if (caller) {
     56            // skip over the '('
     57            caller++;
     58            // find the end of the symbol name
     59            size_t callerLength = abs(strchr(caller, '+') - caller);
     60            psString name = psStringNCopy(caller, callerLength);
     61            psLogMsg("psLib.sys", PS_LOG_ABORT, "Backtrace %d: %s", i, name);
     62            psFree(name);
     63        } else {
     64            psLogMsg("psLib.sys", PS_LOG_ABORT, "Backtrace %d: (unknown)", i);
     65        }
     66    }
     67#endif // ifdef HAVE_BACKTRACE
     68}
    3069
    3170void p_psAbort(const char *file,
     
    4685    // Clean up stack after variable arguement has been used
    4786    va_end(argPtr);
     87
     88    psBacktrace();
    4889
    4990    // Call system abort function to terminate program execution
     
    72113    va_end(argPtr);
    73114
     115    psBacktrace();
     116
    74117    // Call system abort function to terminate program execution
    75118    fsync(psLogGetDestination());
Note: See TracChangeset for help on using the changeset viewer.