IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28351


Ignore:
Timestamp:
Jun 16, 2010, 12:04:05 PM (16 years ago)
Author:
Paul Price
Message:

Add backtrace to pin down bug where task is undefined.

File:
1 edited

Legend:

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

    r28307 r28351  
    77#include <unistd.h>
    88#include <string.h>
     9
     10// Backtrace to help nail down bugs
     11#ifdef HAVE_BACKTRACE
     12#include <execinfo.h>
     13#include <stdlib.h>
     14#define BACKTRACE_BUFFER_SIZE 256       // Maximum size of backtrace
     15static void **bt_buffer = NULL;         // Backtrace buffer
     16static int bt_size = 0;                 // Backtrace buffer size
     17#endif
    918
    1019#include "psAssert.h"
     
    3342static psArray *tsd = NULL;             // Thread-specific data
    3443
     44
    3545/***** basic thread functions *****/
    3646
     
    91101    PS_ASSERT_THREAD_JOB_NON_NULL(job, false);
    92102
     103    psThreadTask *task = psHashLookup(tasks, job->type); // Task to execute job
     104    psAssert(task, "Unable to find task %s", job->type);
     105    psAssert(job->args->n == task->nArgs, "invalid number of arguments to %s", task->type);
     106
    93107    // if we failed to call psThreadPoolInit, or we called it with nThreads == 0,
    94108    // find the matching function and just run it.
     
    101115        psListAdd(done, PS_LIST_TAIL, job);
    102116
    103         // find the corresponding task and run it
    104         psThreadTask *task = psHashLookup(tasks, job->type); // Task to execute job
    105         psAssert(task, "Unable to find task %s", job->type);
    106         psAssert(job->args->n == task->nArgs, "invalid number of arguments to %s", task->type);
    107117        return task->function(job);
    108118    }
     
    209219
    210220        psThreadTask *task = psHashLookup(tasks, job->type); // Task to execute job
     221#ifdef HAVE_BACKTRACE
     222        if (!task && bt_buffer) {
     223            psLogMsg("psLib.sys", PS_LOG_ABORT, "Backtrace of waiter:\n");
     224            char **strings = backtrace_symbols((void *const *)bt_buffer, bt_size);
     225            psLogMsg("psLib.sys", PS_LOG_ABORT, "Backtrace depth: %d", bt_size);
     226            for (int i = 0; i < bt_size; i++) {
     227                char *caller = strchr(strings[i], '(');
     228                if (caller) {
     229                    caller++;
     230                    size_t callerLength = abs(strchr(caller, '+') - caller);
     231                    psString name = psStringNCopy(caller, callerLength);
     232                    psLogMsg("psLib.sys", PS_LOG_ABORT, "Backtrace %d: %s", i, name);
     233                    psFree(name);
     234                } else {
     235                    psLogMsg("psLib.sys", PS_LOG_ABORT, "Backtrace %d: (unknown)", i);
     236                }
     237            }
     238        }
     239#endif
    211240        psAssert(task, "Couldn't find thread task %s", job->type);
    212241        psAssert(job->args->n == task->nArgs,
     
    282311        return true;
    283312    }
     313
     314#ifdef HAVE_BACKTRACE
     315    if (bt_buffer) {
     316        psFree(bt_buffer);
     317    }
     318    bt_buffer = psAlloc(BACKTRACE_BUFFER_SIZE * sizeof(void *));
     319    bt_size = backtrace(bt_buffer, BACKTRACE_BUFFER_SIZE);
     320#endif
    284321
    285322    while (1) {
     
    343380    tsd = NULL;
    344381
     382#ifdef HAVE_BACKTRACE
     383    if (bt_buffer) {
     384        psFree(bt_buffer);
     385    }
     386#endif
     387
    345388    return true;
    346389}
Note: See TracChangeset for help on using the changeset viewer.