/*************************************************************************** external.c - description ------------------- begin : Thu Nov 8 2001 copyright : (C) 2001 by Matt Sabins email : sabinsmh@clarkson.edu ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include #include #include #include "hash.h" #include "ltypes.h" #include "setlocks.h" pthread_mutex_t stats_lock; bool is_active; /* Fast call stuff void stuff (int x,int y,int z) __attribute__ ((regparm(3))); */ jmp_buf jbuff; int construct_locksmith(unsigned int approx_thread_num) { printf("Construct Locksmith!\n"); if(pthread_mutex_init(&stats_lock,NULL)) { fprintf(stderr,"Error initializing locksmith.\n"); is_active = false; return -1; } //start the process by getting the stats_lock. pthread_mutex_lock(&stats_lock); if(construct_pthread_hash(3+approx_thread_num)) { pthread_mutex_unlock(&stats_lock); fprintf(stderr,"Error initializing locksmith.\n"); return -1; } if(construct_hash(3+approx_thread_num)) { pthread_mutex_unlock(&stats_lock); fprintf(stderr,"Error initializing locksmith.\n"); return -1; } is_active = true; pthread_mutex_unlock(&stats_lock); return 0; } void locksmith_bail() { destroy_pthread_hash(); destroy_hash(); fprintf(stderr,"Locksmith is bailing.\n"); is_active = false; longjmp(jbuff,1); } void destroy_locksmith() { pthread_mutex_lock(&stats_lock); dump_memory_locks(); destroy_pthread_hash(); destroy_hash(); is_active = false; pthread_mutex_unlock(&stats_lock); } void __attribute__ ((regparm(3))) locksmith_add_lock_to_thread(pthread_mutex_t* mutex) { pthread_t thread = pthread_self(); // printf("Add Lock Thread No %d %d\n",thread,mutex); pthread_mutex_lock(&stats_lock); if(is_active == false) return; if(setjmp(jbuff)) { pthread_mutex_unlock(&stats_lock); return; } add_lock_to_thread(mutex,thread); pthread_mutex_unlock(&stats_lock); } void __attribute__ ((regparm(3))) locksmith_remove_lock_from_thread(pthread_mutex_t* mutex) { pthread_t thread = pthread_self(); // printf("Remove Lock Thread No %d %d\n",thread,mutex); pthread_mutex_lock(&stats_lock); if(is_active == false) return; if(setjmp(jbuff)) { pthread_mutex_unlock(&stats_lock); return; } remove_lock_from_thread(mutex,thread); pthread_mutex_unlock(&stats_lock); } void locksmith_add_memory_access(pthread_t thread,memaddr_t addr,line_type_e type,int lineno,char* source) { // printf("arguments %d %d %d %d %d\n",thread,addr,type,lineno,source); // puts(source); pthread_mutex_lock(&stats_lock); if(is_active == false) return; if(setjmp(jbuff)) { pthread_mutex_unlock(&stats_lock); return; } add_memory_access(addr,type,thread); pthread_mutex_unlock(&stats_lock); }