7 #include "bcwindowbase.inc"
18 static BC_Trace *global_trace;
19 static void reset_locks();
21 static void delete_temps();
22 static void set_temp(char *string);
23 static void unset_temp(char *string);
25 static void enable_locks();
26 static void disable_locks();
27 static int set_lock(const char *title, const char *location, trace_info *info);
28 static void set_lock2(int table_id, trace_info *info);
29 static void unset_lock2(int table_id, trace_info *info);
30 static void unset_lock(trace_info *info);
31 // Used in lock destructors so takes away all references
32 static void unset_all_locks(trace_info *info);
33 static void clear_locks_tid(pthread_t tid);
35 static void new_trace(const char *text);
36 static void new_trace(const char *file, const char *function, int line);
37 static void delete_traces();
39 static void enable_memory();
40 static void disable_memory();
41 static void set_buffer(int size, void *ptr, const char* location);
42 // This one returns 1 if the buffer wasn't found.
43 static int unset_buffer(void *ptr);
44 static void lock_locks(const char *s);
45 static void unlock_locks();
47 static void dump_traces(FILE *fp=stdout);
48 static void dump_locks(FILE *fp=stdout);
49 static void dump_buffers(FILE *fp=stdout);
50 static void dump_threads(FILE *fp=stdout);
53 class bc_trace_list : public List<trace_item> {
55 void clear() { while( last ) remove(last); }
57 ~bc_trace_list() { clear(); }
60 class bc_trace_t : public bc_trace_list {
63 bc_trace_t() : size(0) {}
67 class bc_trace_spin : public bc_trace_t {
68 pthread_spinlock_t spin;
70 void *operator new(size_t n) { return (void*) malloc(n); }
71 void operator delete(void *t, size_t n) { free(t); }
73 void lock() { pthread_spin_lock(&spin); }
74 void unlock() { pthread_spin_unlock(&spin); }
75 bc_trace_spin() { pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE); }
76 ~bc_trace_spin() { pthread_spin_destroy(&spin); }
79 class bc_trace_mutex : public bc_trace_t {
80 pthread_mutex_t mutex;
82 void *operator new(size_t n) { return (void*) malloc(n); }
83 void operator delete(void *t, size_t n) { free(t); }
85 void lock() { pthread_mutex_lock(&mutex); }
86 void unlock() { pthread_mutex_unlock(&mutex); }
87 bc_trace_mutex() { pthread_mutex_init(&mutex, 0); }
88 ~bc_trace_mutex() { pthread_mutex_destroy(&mutex); }
91 extern bc_trace_mutex execution_table;
92 extern bc_trace_mutex memory_table;
93 extern bc_trace_mutex lock_table;
94 extern bc_trace_mutex file_table;
95 extern "C" void dump();
97 class trace_item : public ListItem<trace_item> {
100 trace_item(bc_trace_t &t);
104 class execution_item : public trace_item {
106 void *operator new(size_t n) { return (void*) malloc(n); }
107 void operator delete(void *t, size_t n) { free(t); }
110 void clear() { delete [] value; value = 0; }
111 void set(const char *v) { delete [] value; value = cstrdup(v); }
113 execution_item() : trace_item(execution_table) { value = 0; }
114 ~execution_item() { clear(); }
117 class lock_item : public trace_item {
120 void *operator new(size_t n) { return (void*) malloc(n); }
121 void operator delete(void *t, size_t n) { free(t); }
129 void set(trace_info *info, const char *title, const char *loc) {
130 this->info = info; this->title = title;
131 this->loc = loc; this->is_owner = 0;
132 this->id = table_id++; this->tid = pthread_self();
135 this->info = 0; this->title = 0; this->loc = 0;
136 this->is_owner = 0; this->id = -1; this->tid = 0;
139 lock_item() : trace_item(lock_table) { clear(); }
143 class memory_item : public trace_item {
145 void *operator new(size_t n) { return (void*) malloc(n); }
146 void operator delete(void *t, size_t n) { free(t); }
152 memory_item(int size, void *ptr, const char *loc)
153 : trace_item(memory_table) {
154 this->size = size; this->ptr = ptr; this->loc = loc;
159 class file_item : public trace_item {
161 void *operator new(size_t n) { return (void*) malloc(n); }
162 void operator delete(void *t, size_t n) { free(t); }
165 void clear() { delete [] value; value = 0; }
166 void set(const char *v) { delete [] value; value = cstrdup(v); }
168 file_item() : trace_item(file_table) { value = 0; }
169 ~file_item() { clear(); }
172 // track unjoined threads at termination
177 pthread_mutex_t the_lock;
179 void lock() { pthread_mutex_lock(&the_lock); }
180 void unlock() { pthread_mutex_unlock(&the_lock); }
183 pthread_mutexattr_t attr;
184 pthread_mutexattr_init(&attr);
185 pthread_mutex_init(&the_lock, &attr);
188 pthread_mutex_destroy(&the_lock);
190 void reset() { finit(); init(); }
192 TheLock() { init(); }
193 ~TheLock() { finit(); }
198 static TheLock the_lock;
199 static void reset() { the_lock.reset(); }
201 TheLocker() { the_lock.lock(); }
202 ~TheLocker() { the_lock.unlock(); }
207 pthread_t tid, owner; const char *name;
208 TheDbg(pthread_t t, pthread_t o, const char *nm) { tid = t; owner = o; name = nm; }
213 class TheList : public ArrayList<TheDbg *> {
215 static TheList the_list;
216 static void dump_threads(FILE *fp);
217 static void dbg_add(pthread_t tid, pthread_t owner, const char *nm);
218 static void dbg_del(pthread_t tid);
219 static void reset() { the_list.remove_all_objects(); TheLocker::reset(); }
221 ~TheList() { reset(); }
226 static TheChk the_chk;
230 int i = TheList::the_list.size();
232 printf("unjoined tids / owner %d\n", i);
233 while( --i >= 0 ) printf(" %016lx / %016lx %s\n",
234 (unsigned long)TheList::the_list[i]->tid,
235 (unsigned long)TheList::the_list[i]->owner,
236 TheList::the_list[i]->name);