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);
52 static void dump_shm_stat(const char *fn, FILE *fp=stdout);
53 static void dump_shm_stats(FILE *fp=stdout);
56 class bc_trace_list : public List<trace_item> {
58 void clear() { while( last ) remove(last); }
60 ~bc_trace_list() { clear(); }
63 class bc_trace_t : public bc_trace_list {
66 bc_trace_t() : size(0) {}
70 class bc_trace_spin : public bc_trace_t {
71 pthread_spinlock_t spin;
73 void *operator new(size_t n) { return (void*) malloc(n); }
74 void operator delete(void *t, size_t n) { free(t); }
76 void lock() { pthread_spin_lock(&spin); }
77 void unlock() { pthread_spin_unlock(&spin); }
78 bc_trace_spin() { pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE); }
79 ~bc_trace_spin() { pthread_spin_destroy(&spin); }
82 class bc_trace_mutex : public bc_trace_t {
83 pthread_mutex_t mutex;
85 void *operator new(size_t n) { return (void*) malloc(n); }
86 void operator delete(void *t, size_t n) { free(t); }
88 void lock() { pthread_mutex_lock(&mutex); }
89 void unlock() { pthread_mutex_unlock(&mutex); }
90 bc_trace_mutex() { pthread_mutex_init(&mutex, 0); }
91 ~bc_trace_mutex() { pthread_mutex_destroy(&mutex); }
94 extern bc_trace_mutex execution_table;
95 extern bc_trace_mutex memory_table;
96 extern bc_trace_mutex lock_table;
97 extern bc_trace_mutex file_table;
98 extern "C" void dump();
100 class trace_item : public ListItem<trace_item> {
103 trace_item(bc_trace_t &t);
107 class execution_item : public trace_item {
109 void *operator new(size_t n) { return (void*) malloc(n); }
110 void operator delete(void *t, size_t n) { free(t); }
113 void clear() { delete [] value; value = 0; }
114 void set(const char *v) { delete [] value; value = cstrdup(v); }
116 execution_item() : trace_item(execution_table) { value = 0; }
117 ~execution_item() { clear(); }
120 class lock_item : public trace_item {
123 void *operator new(size_t n) { return (void*) malloc(n); }
124 void operator delete(void *t, size_t n) { free(t); }
132 void set(trace_info *info, const char *title, const char *loc) {
133 this->info = info; this->title = title;
134 this->loc = loc; this->is_owner = 0;
135 this->id = table_id++; this->tid = pthread_self();
138 this->info = 0; this->title = 0; this->loc = 0;
139 this->is_owner = 0; this->id = -1; this->tid = 0;
142 lock_item() : trace_item(lock_table) { clear(); }
146 class memory_item : public trace_item {
148 void *operator new(size_t n) { return (void*) malloc(n); }
149 void operator delete(void *t, size_t n) { free(t); }
155 memory_item(int size, void *ptr, const char *loc)
156 : trace_item(memory_table) {
157 this->size = size; this->ptr = ptr; this->loc = loc;
162 class file_item : public trace_item {
164 void *operator new(size_t n) { return (void*) malloc(n); }
165 void operator delete(void *t, size_t n) { free(t); }
168 void clear() { delete [] value; value = 0; }
169 void set(const char *v) { delete [] value; value = cstrdup(v); }
171 file_item() : trace_item(file_table) { value = 0; }
172 ~file_item() { clear(); }
175 // track unjoined threads at termination
180 pthread_mutex_t the_lock;
182 void lock() { pthread_mutex_lock(&the_lock); }
183 void unlock() { pthread_mutex_unlock(&the_lock); }
186 pthread_mutexattr_t attr;
187 pthread_mutexattr_init(&attr);
188 pthread_mutex_init(&the_lock, &attr);
191 pthread_mutex_destroy(&the_lock);
193 void reset() { finit(); init(); }
195 TheLock() { init(); }
196 ~TheLock() { finit(); }
201 static TheLock the_lock;
202 static void reset() { the_lock.reset(); }
204 TheLocker() { the_lock.lock(); }
205 ~TheLocker() { the_lock.unlock(); }
210 pthread_t tid, owner; const char *name;
211 TheDbg(pthread_t t, pthread_t o, const char *nm) { tid = t; owner = o; name = nm; }
216 class TheList : public ArrayList<TheDbg *> {
218 static TheList the_list;
219 static void dump_threads(FILE *fp);
220 static void dbg_add(pthread_t tid, pthread_t owner, const char *nm);
221 static void dbg_del(pthread_t tid);
222 static void reset() { the_list.remove_all_objects(); TheLocker::reset(); }
224 ~TheList() { reset(); }
229 static TheChk the_chk;
233 int i = TheList::the_list.size();
235 printf("unjoined tids / owner %d\n", i);
236 while( --i >= 0 ) printf(" %016lx / %016lx %s\n",
237 (unsigned long)TheList::the_list[i]->tid,
238 (unsigned long)TheList::the_list[i]->owner,
239 TheList::the_list[i]->name);