Пример #1
0
/*
 * Invoked by the mutator on thread exit
 */
int DYNINSTunregisterThread(dyntid_t tid) {
    unsigned hash_id, orig;
    unsigned tid_val = (unsigned long) tid;

    int retval = 1;
    rtdebug_printf("%s[%d]: Begin DYNINSTunregisterThread, tid %lu\n", __FILE__, __LINE__,
            dyn_pthread_self());

    if( tc_lock_lock(&DYNINST_index_lock) == DYNINST_DEAD_LOCK ) {
        rtdebug_printf("%s[%d]: DEADLOCK HERE tid %lu\n", __FILE__, __LINE__,
                dyn_pthread_self());
        return 0;
    }

    hash_id = tid_val % DYNINST_thread_hash_size;
    orig = hash_id;
    while(DYNINST_thread_hash_tids[hash_id] != tid) {
        hash_id++;
        if( hash_id == DYNINST_thread_hash_size ) hash_id = 0;
        if( orig == hash_id ) {
            retval = 0;
            break;
        }
    }

    if( retval ) {
        rtdebug_printf("%s[%d]: removed mapping for thread (index = %lu, tid = 0x%lx)\n",
                __FILE__, __LINE__, DYNINST_thread_hash_indices[hash_id], tid);
        DYNINST_thread_hash_indices[hash_id] = IDX_NONE;
        num_free++;
    }

    tc_lock_unlock(&DYNINST_index_lock);
    return retval;
}
Пример #2
0
/**
 * Translate a tid given by pthread_self into an index.  Called by 
 * basetramps everywhere.
 **/
int DYNINSTthreadIndex() {
    dyntid_t tid;
    unsigned curr_index;

    rtdebug_printf("%s[%d]:  welcome to DYNINSTthreadIndex()\n", __FILE__, __LINE__);
    if (!DYNINSThasInitialized) {
      rtdebug_printf("%s[%d]: dyninst not initialized, ret false\n", __FILE__, __LINE__);
      return 0;
    }

    tid = (dyntid_t) ((unsigned long)dyn_pthread_self());
    rtdebug_printf("%s[%d]:  DYNINSTthreadIndex(): tid = %lu\n", __FILE__, __LINE__,
                   (unsigned long) tid);
    if (tid == (dyntid_t) DYNINST_SINGLETHREADED) return 0;
    rtdebug_printf("%s[%d]: calling thread index slow (modified)\n", __FILE__, __LINE__);
    curr_index = DYNINSTthreadIndexSLOW(tid);
    rtdebug_printf("%s[%d]: back from thread index slow\n", __FILE__, __LINE__);
    /* While DYNINST_max_num_threads is an error return for
       DYNINSTthreadIndexSLOW(), there's not really anything we
       can do about it at the moment, so just return it
       and let the mutatee scribble into the so-allocated memory. */
    if ( curr_index == DYNINST_NOT_IN_HASHTABLE ) {
        rtdebug_printf("%s[%d]:  DYNINSTthreadIndex(): failed to find index for %lu\n",
                __FILE__, __LINE__, tid);
        curr_index = DYNINST_max_num_threads;
    }

    rtdebug_printf("%s[%d]:  DYNINSTthreadIndex(): returning index: %d\n",
                   __FILE__, __LINE__, curr_index);
    return curr_index;
}
Пример #3
0
/*
 * Invoked by the mutator on thread creation
 */
unsigned long DYNINSTregisterThread(dyntid_t tid, unsigned index) {
    unsigned hash_id, orig;
    unsigned long tid_val = (unsigned long) tid;

    unsigned long retval = (unsigned long)dyn_pthread_self();
    assert(retval != 0 );
    rtdebug_printf("%s[%d]: Begin DYNINSTregisterThread, tid %lu\n", __FILE__, __LINE__,
            dyn_pthread_self());

    if( tid_val != retval ) {
        tid_val = retval;
    }

    if( tc_lock_lock(&DYNINST_index_lock) == DYNINST_DEAD_LOCK ) {
       rtdebug_printf("%s[%d]:  DEADLOCK HERE tid %lu \n", __FILE__, __LINE__, 
               dyn_pthread_self());
        return 0;
    }

    hash_id = tid_val % DYNINST_thread_hash_size;
    orig = hash_id;
    while(DYNINST_thread_hash_indices[hash_id] != IDX_NONE) {
        hash_id++;
        if( hash_id == DYNINST_thread_hash_size ) hash_id = 0;
        if( orig == hash_id ) {
            retval = 0;
            break;
        }
    }

    if( retval ) {
        DYNINST_thread_hash_indices[hash_id] = index;
        DYNINST_thread_hash_tids[hash_id] = tid;
        num_free--;
        rtdebug_printf("%s[%d]: created mapping for thread (index = %lu, tid = 0x%lx)\n",
                __FILE__, __LINE__, index, tid);
    }

    tc_lock_unlock(&DYNINST_index_lock);
    return retval;
}
Пример #4
0
int tc_lock_lock(tc_lock_t *tc)
{
   dyntid_t me;

   me = dyn_pthread_self();
   if (me == tc->tid)
      return DYNINST_DEAD_LOCK;

   while (!atomic_set(&tc->mutex));

   tc->tid = me;
   return 0;
}
Пример #5
0
/**
 * A guaranteed-if-there index lookup 
 **/
unsigned DYNINSTthreadIndexSLOW(dyntid_t tid) {
    unsigned hash_id, orig;
    unsigned retval = DYNINST_NOT_IN_HASHTABLE;
    int index, result;
    unsigned long tid_val = (unsigned long) tid;
    rtdebug_printf("%s[%d]: getting dyninst index lock\n", __FILE__, __LINE__);
    result = tc_lock_lock(&DYNINST_index_lock);
    if (result == DYNINST_DEAD_LOCK) {
        rtdebug_printf("%s[%d]:  DEADLOCK HERE tid %lu \n", __FILE__, __LINE__,
                       dyn_pthread_self());
        /* We specifically return DYNINST_max_num_threads so that instrumentation
         * has someplace safe to scribble in case of an error. */

        /* DO NOT USE print statements here. That's horribly unsafe if we've instrumented
           the output functions, as we'll infinite recurse and die */
        return DYNINST_max_num_threads;
    }
    rtdebug_printf("%s[%d]: got dyninst index lock\n", __FILE__, __LINE__);

    /**
     * Search the hash table
     **/
    if (!DYNINST_thread_hash_size) {
        //Uninitialized tramp guard.
        tc_lock_unlock(&DYNINST_index_lock);
        return DYNINST_max_num_threads;
    }

    hash_id = tid_val % DYNINST_thread_hash_size;
    orig = hash_id;
    for (;;) {
        index = DYNINST_thread_hash_indices[hash_id];
        if (index != IDX_NONE && DYNINST_thread_hash_tids[hash_id] == tid) {
            retval = index;
            break;
        }

        hash_id++;
        if (hash_id == DYNINST_thread_hash_size)
            hash_id = 0;
        if (orig == hash_id)
            break;
    }

    tc_lock_unlock(&DYNINST_index_lock);
    return retval;
}
Пример #6
0
int DYNINSTthreadInfo(BPatch_newThreadEventRecord *ev)
{
   struct __pthrdsinfo pthread_desc;
   int pthread_desc_size = sizeof(struct __pthrdsinfo);
   int registers[1024];
   int regsize = 1024*sizeof(int);

   dyntid_t tidp;
   int lwpid;
   long startpc;
   void *stkbase, *rs_p;

   int result;

   pthread_t pthread_id;
   pthread_id = (pthread_t) dyn_pthread_self();

   result = dyn_pthread_getthrds_np(&pthread_id, PTHRDSINFO_QUERY_ALL,
                                    &pthread_desc, pthread_desc_size,
                                    registers, &regsize);
   if (result)
   {
      if (result != NOT_SETUP_ERR)
         perror("RTthread-aix:DYNINST_ThreadInfo");
      else
         fprintf(stderr, "[%s:%u] - TInfo not yet setup\n", __FILE__, __LINE__);
      return 0;
   }

   DYNINST_ThreadPInfo((void *)&pthread_desc, &stkbase, &tidp, &startpc, 
                       &lwpid, &rs_p);

   ev->stack_addr = stkbase;
   ev->start_pc = (void *) startpc;

   /*
   fprintf(stderr, "DYNINST_ThreadInfo, stkbase=%x, tid=%d, " 
           "startpc=%x, lwp=%d, resumestate=%x\n",
           (unsigned) *stkbase, *tidp, *startpc, *lwpid, (unsigned)*rs_p);
   */
	  
   return 1;
}