10 #define BT_BUF_SIZE 100
11 // booby trap (backtrace)
14 void *buffer[BT_BUF_SIZE];
15 int nptrs = backtrace(buffer, BT_BUF_SIZE);
16 char **trace = backtrace_symbols(buffer, nptrs);
18 for( int i=0; i<nptrs; ) printf("%s\n", trace[i++]);
23 BC_Trace *BC_Trace::global_trace = 0;
34 bc_trace_mutex execution_table;
35 bc_trace_mutex memory_table;
36 bc_trace_mutex lock_table;
37 bc_trace_list lock_free;
38 bc_trace_mutex file_table;
40 // incase lock set after task ends
41 static pthread_t last_lock_thread = 0;
42 static const char *last_lock_title = 0;
43 static const char *last_lock_location = 0;
45 trace_item::trace_item(bc_trace_t &t) : table(t) { ++table.size; }
46 trace_item::~trace_item() { --table.size; }
48 extern "C" void dump()
50 BC_Trace::dump_traces();
51 BC_Trace::dump_locks();
52 BC_Trace::dump_buffers();
55 #define TOTAL_TRACES 16
57 void BC_Trace::new_trace(const char *text)
59 if(!global_trace) return;
60 execution_table.lock();
62 if( execution_table.size >= TOTAL_TRACES ) {
63 it = (execution_item *)execution_table.first;
64 execution_table.remove_pointer(it);
67 it = new execution_item();
69 execution_table.append(it);
70 execution_table.unlock();
73 void BC_Trace::new_trace(const char *file, const char *function, int line)
75 char string[BCTEXTLEN];
76 snprintf(string, BCTEXTLEN, "%s: %s: %d", file, function, line);
80 void BC_Trace::delete_traces()
82 if(!global_trace) return;
83 execution_table.lock();
84 execution_table.clear();
85 execution_table.unlock();
88 void BC_Trace::enable_locks()
95 void BC_Trace::disable_locks()
99 while( lock_table.last ) {
100 lock_item *p = (lock_item*)lock_table.last;
101 lock_table.remove_pointer(p);
109 // no canceling with lock held
110 void BC_Trace::lock_locks(const char *s)
113 last_lock_thread = pthread_self();
115 last_lock_location = 0;
118 void BC_Trace::unlock_locks()
123 #define TOTAL_LOCKS 256
125 int BC_Trace::set_lock(const char *title, const char *loc, trace_info *info)
127 if( !global_trace || !trace_locks ) return 0;
129 last_lock_thread = pthread_self();
130 last_lock_title = title;
131 last_lock_location = loc;
133 if( (it=(lock_item*)lock_free.first) != 0 )
134 lock_free.remove_pointer(it);
135 else if( lock_table.size >= TOTAL_LOCKS ) {
136 it = (lock_item*)lock_table.first;
137 lock_table.remove_pointer(it);
140 it = new lock_item();
141 it->set(info, title, loc);
142 lock_table.append(it);
143 info->trace = (void *)it;
148 void BC_Trace::set_lock2(int table_id, trace_info *info)
150 if( !global_trace || !trace_locks ) return;
152 lock_item *p = (lock_item *)info->trace;
153 if( !p || p->id != table_id ) {
154 p = (lock_item*)lock_table.last;
155 while( p && p->id != table_id ) p = (lock_item*)p->previous;
158 info->trace = (void *)p;
160 p->tid = pthread_self();
165 void BC_Trace::unset_lock2(int table_id, trace_info *info)
167 if( !global_trace || !trace_locks ) return;
169 lock_item *p = (lock_item *)info->trace;
170 if( !p || p->id != table_id ) {
171 p = (lock_item*)lock_table.last;
172 while( p && p->id != table_id ) p = (lock_item*)p->previous;
175 if( p->list == &lock_table ) {
176 lock_table.remove_pointer(p);
181 printf("unset_lock2: free lock_item in lock_table %p\n", p);
186 void BC_Trace::unset_lock(trace_info *info)
188 if( !global_trace || !trace_locks ) return;
190 lock_item *p = (lock_item *)info->trace;
191 if( !p || p->info!=info || !p->is_owner ) {
192 p = (lock_item*)lock_table.last;
193 while( p && ( p->info!=info || !p->is_owner ) ) p = (lock_item*)p->previous;
197 if( p->list == &lock_table ) {
198 lock_table.remove_pointer(p);
203 printf("unset_lock: free lock_item in lock_table %p\n", p);
209 void BC_Trace::unset_all_locks(trace_info *info)
211 if( !global_trace || !trace_locks ) return;
214 lock_item *p = (lock_item*)lock_table.first;
216 lock_item *lp = p; p = (lock_item*)p->next;
217 if( lp->info != info ) continue;
218 lock_table.remove_pointer(lp);
219 lock_free.append(lp);
224 void BC_Trace::clear_locks_tid(pthread_t tid)
226 if( !global_trace || !trace_locks ) return;
228 lock_item *p = (lock_item*)lock_table.first;
230 lock_item *lp = p; p = (lock_item*)p->next;
231 if( lp->tid != tid ) continue;
232 lock_table.remove_pointer(lp);
234 lock_free.append(lp);
240 void BC_Trace::enable_memory()
245 void BC_Trace::disable_memory()
251 void BC_Trace::set_buffer(int size, void *ptr, const char* loc)
253 if(!global_trace) return;
254 if(!trace_memory) return;
256 //printf("BC_Trace::set_buffer %p %s\n", ptr, loc);
257 memory_table.append(new memory_item(size, ptr, loc));
258 memory_table.unlock();
261 int BC_Trace::unset_buffer(void *ptr)
263 if(!global_trace) return 0;
264 if(!trace_memory) return 0;
266 memory_item *p = (memory_item*)memory_table.first;
267 for( ; p!=0 && p->ptr!=ptr; p=(memory_item*)p->next );
269 memory_table.unlock();
275 void* operator new(size_t size)
277 void *result = malloc(size);
278 BUFFER(size, result, "new");
282 void* operator new[](size_t size)
284 void *result = malloc(size);
285 BUFFER(size, result, "new []");
289 void operator delete(void *ptr)
295 void operator delete[](void *ptr)
303 void BC_Trace::dump_traces(FILE *fp)
306 for( trace_item *tp=execution_table.first; tp!=0; tp=tp->next ) {
307 execution_item *p=(execution_item*)tp;
308 fprintf(fp," %s\n", (char*)p->value);
312 void trace_info::set_owner() { owner = pthread_self(); }
313 void trace_info::unset_owner() { owner = 0; }
315 void BC_Trace::dump_locks(FILE *fp)
319 fprintf(fp,"signal_entry: lock table size=%d\n", lock_table.size);
320 for( trace_item *tp=lock_table.first; tp!=0; tp=tp->next ) {
321 lock_item *p=(lock_item*)tp;
322 fprintf(fp," %p %s, %s %p%s",
323 p->info, p->title, p->loc,
324 (void*)p->tid, p->is_owner ? " *" : "");
325 if( p->info->owner && p->info->owner != p->tid )
326 fprintf(fp," %p", (void*)p->info->owner);
329 int64_t lock_total = lock_table.total();
330 int64_t free_total = lock_free.total();
331 printf("lock_items: %jd\n", lock_total);
332 printf("lock_frees: %jd\n", free_total);
333 int64_t missed_locks = lock_table.size - (lock_total + free_total);
335 printf("miss locks: %jd\n", missed_locks);
339 void BC_Trace::dump_buffers(FILE *fp)
344 fprintf(fp,"BC_Trace::dump_buffers: buffer table size=%d\n", memory_table.size);
345 for( trace_item *tp=memory_table.first; tp!=0; tp=tp->next ) {
346 memory_item *p=(memory_item*)tp;
347 fprintf(fp," %d %p %s\n", p->size, p->ptr, p->loc);
349 memory_table.unlock();
353 void BC_Trace::delete_temps()
356 if( file_table.size )
357 printf("BC_Trace::delete_temps: deleting %d temp files\n", file_table.size);
358 while( file_table.first ) {
359 file_item *p = (file_item*)file_table.first;
360 printf(" %s\n", p->value);
367 void BC_Trace::reset_locks()
372 void BC_Trace::set_temp(char *string)
375 file_item *it = new file_item();
377 file_table.append(it);
381 void BC_Trace::unset_temp(char *string)
384 file_item *p = (file_item *)file_table.last;
385 for( ; p!=0 && strcmp(p->value,string); p=(file_item*)p->previous );
393 TheLock TheLocker::the_lock;
394 TheList TheList::the_list;
396 int lock_item::table_id = 0;
398 void TheList::dbg_add(pthread_t tid, pthread_t owner, const char *nm)
400 TheLocker the_locker;
401 int i = the_list.size();
402 while( --i >= 0 && !(the_list[i]->tid == tid && the_list[i]->owner == owner) );
404 printf("dbg_add, dup %016lx %s %s\n",
405 (unsigned long)tid, nm, the_list[i]->name);
408 the_list.append(new TheDbg(tid, owner, nm));
411 void TheList::dbg_del(pthread_t tid)
413 TheLocker the_locker;
414 int i = the_list.size();
415 while( --i >= 0 && the_list[i]->tid != tid );
417 printf("dbg_del, mis %016lx\n",(unsigned long)tid);
420 the_list.remove_object_number(i);
424 void TheList::dump_threads(FILE *fp)
426 int i = the_list.size();
428 fprintf(fp, "thread 0x%012lx, owner 0x%012lx, %s\n",
429 (unsigned long)the_list[i]->tid, (unsigned long)the_list[i]->owner,
435 #define dbg_add(t, o, nm) do {} while(0)
436 #define dbg_del(t) do {} while(0)
437 void TheList::dump_threads(FILE *fp)
442 void BC_Trace::dump_threads(FILE *fp)
444 TheList::dump_threads(fp);
448 void BC_Trace::dump_shm_stat(const char *fn, FILE *fp)
450 char path[BCTEXTLEN];
451 sprintf(path, "/proc/sys/kernel/%s",fn);
452 FILE *sfp = fopen(path,"r");
455 fscanf(sfp, "%ju", &v);
457 fprintf(fp, "%s = %ju\n", fn, v);
460 void BC_Trace::dump_shm_stats(FILE *fp)
462 dump_shm_stat("shmall", fp);
463 dump_shm_stat("shmmax", fp);
464 dump_shm_stat("shmmni", fp);
465 FILE *sfp = fopen("/proc/sysvipc/shm","r");
467 char line[BCTEXTLEN];
469 if( !fgets(line,sizeof(line), sfp) ) return;
470 int64_t used = 0, other = 0;
471 int n_used = 0, n_other = 0;
472 while( fgets(line,sizeof(line), sfp) ) {
473 int key, shmid, perms, cpid, lpid, uid, gid, cuid, cgid;
474 int64_t size, nattch, atime, dtime, ctime, rss, swap;
476 "%d %d %o %ju %u %u %ju %u %u %u %u %ju %ju %ju %ju %ju",
477 &key, &shmid, &perms, &size, &cpid, &lpid, &nattch,
478 &uid, &gid, &cuid, &cgid, &atime, &dtime, &ctime,
479 &rss, &swap) != 16 ) break;
490 fprintf(fp, "shmused = %jd (%d items)\n", used, n_used);
491 fprintf(fp, "shmother = %jd (%d items)\n", other, n_other);