7 #include "bcwindowbase.inc"
12 #define BT if( top_level->display_lock_owner != pthread_self() ) booby();
24 static BC_Trace *global_trace;
25 static void reset_locks();
27 static void delete_temps();
28 static void set_temp(char *string);
29 static void unset_temp(char *string);
31 static void enable_locks();
32 static void disable_locks();
33 static int set_lock(const char *title, const char *location, trace_info *info);
34 static void set_lock2(int table_id, trace_info *info);
35 static void unset_lock2(int table_id, trace_info *info);
36 static void unset_lock(trace_info *info);
37 // Used in lock destructors so takes away all references
38 static void unset_all_locks(trace_info *info);
39 static void clear_locks_tid(pthread_t tid);
41 static void new_trace(const char *text);
42 static void new_trace(const char *file, const char *function, int line);
43 static void delete_traces();
45 static void enable_memory();
46 static void disable_memory();
47 static void set_buffer(int size, void *ptr, const char* location);
48 // This one returns 1 if the buffer wasn't found.
49 static int unset_buffer(void *ptr);
50 static void lock_locks(const char *s);
51 static void unlock_locks();
53 static void dump_traces(FILE *fp=stdout);
54 static void dump_locks(FILE *fp=stdout);
55 static void dump_buffers(FILE *fp=stdout);
56 static void dump_threads(FILE *fp=stdout);
58 static void dump_shm_stat(const char *fn, FILE *fp=stdout);
59 static void dump_shm_stats(FILE *fp=stdout);
62 class bc_trace_list : public List<trace_item> {
64 void clear() { while( last ) remove(last); }
66 ~bc_trace_list() { clear(); }
69 class bc_trace_t : public bc_trace_list {
72 bc_trace_t() : size(0) {}
76 class bc_trace_spin : public bc_trace_t {
77 pthread_spinlock_t spin;
79 void *operator new(size_t n) { return (void*) malloc(n); }
80 void operator delete(void *t, size_t n) { free(t); }
82 void lock() { pthread_spin_lock(&spin); }
83 void unlock() { pthread_spin_unlock(&spin); }
84 bc_trace_spin() { pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE); }
85 ~bc_trace_spin() { pthread_spin_destroy(&spin); }
88 class bc_trace_mutex : public bc_trace_t {
89 pthread_mutex_t mutex;
91 void *operator new(size_t n) { return (void*) malloc(n); }
92 void operator delete(void *t, size_t n) { free(t); }
94 void lock() { pthread_mutex_lock(&mutex); }
95 void unlock() { pthread_mutex_unlock(&mutex); }
96 bc_trace_mutex() { pthread_mutex_init(&mutex, 0); }
97 ~bc_trace_mutex() { pthread_mutex_destroy(&mutex); }
100 extern bc_trace_mutex execution_table;
101 extern bc_trace_mutex memory_table;
102 extern bc_trace_mutex lock_table;
103 extern bc_trace_mutex file_table;
104 extern "C" void dump();
106 class trace_item : public ListItem<trace_item> {
109 trace_item(bc_trace_t &t);
113 class execution_item : public trace_item {
115 void *operator new(size_t n) { return (void*) malloc(n); }
116 void operator delete(void *t, size_t n) { free(t); }
119 void clear() { delete [] value; value = 0; }
120 void set(const char *v) { delete [] value; value = cstrdup(v); }
122 execution_item() : trace_item(execution_table) { value = 0; }
123 ~execution_item() { clear(); }
126 class lock_item : public trace_item {
129 void *operator new(size_t n) { return (void*) malloc(n); }
130 void operator delete(void *t, size_t n) { free(t); }
138 void set(trace_info *info, const char *title, const char *loc) {
139 this->info = info; this->title = title;
140 this->loc = loc; this->is_owner = 0;
141 this->id = table_id++; this->tid = pthread_self();
144 this->info = 0; this->title = 0; this->loc = 0;
145 this->is_owner = 0; this->id = -1; this->tid = 0;
148 lock_item() : trace_item(lock_table) { clear(); }
152 class memory_item : public trace_item {
154 void *operator new(size_t n) { return (void*) malloc(n); }
155 void operator delete(void *t, size_t n) { free(t); }
161 memory_item(int size, void *ptr, const char *loc)
162 : trace_item(memory_table) {
163 this->size = size; this->ptr = ptr; this->loc = loc;
168 class file_item : public trace_item {
170 void *operator new(size_t n) { return (void*) malloc(n); }
171 void operator delete(void *t, size_t n) { free(t); }
174 void clear() { delete [] value; value = 0; }
175 void set(const char *v) { delete [] value; value = cstrdup(v); }
177 file_item() : trace_item(file_table) { value = 0; }
178 ~file_item() { clear(); }
181 // track unjoined threads at termination
186 pthread_mutex_t the_lock;
188 void lock() { pthread_mutex_lock(&the_lock); }
189 void unlock() { pthread_mutex_unlock(&the_lock); }
192 pthread_mutexattr_t attr;
193 pthread_mutexattr_init(&attr);
194 pthread_mutex_init(&the_lock, &attr);
197 pthread_mutex_destroy(&the_lock);
199 void reset() { finit(); init(); }
201 TheLock() { init(); }
202 ~TheLock() { finit(); }
207 static TheLock the_lock;
208 static void reset() { the_lock.reset(); }
210 TheLocker() { the_lock.lock(); }
211 ~TheLocker() { the_lock.unlock(); }
216 pthread_t tid, owner; const char *name;
217 TheDbg(pthread_t t, pthread_t o, const char *nm) { tid = t; owner = o; name = nm; }
222 class TheList : public ArrayList<TheDbg *> {
224 static TheList the_list;
225 static void dump_threads(FILE *fp);
226 static void dbg_add(pthread_t tid, pthread_t owner, const char *nm);
227 static void dbg_del(pthread_t tid);
228 static void reset() { the_list.remove_all_objects(); TheLocker::reset(); }
230 int i = the_list.size();
232 printf("unjoined tids / owner %d\n", i);
233 while( --i >= 0 ) printf(" %016lx / %016lx %s\n",
234 (unsigned long)the_list[i]->tid,
235 (unsigned long)the_list[i]->owner,
239 ~TheList() { check(); reset(); }