05d44f8732701d18505f0e0ca66224b058fbec18
[goodguy/history.git] / cinelerra-5.1 / guicast / bcsignals.h
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  * 
19  */
20
21
22 // debugging functions go here
23
24 #ifndef BCSIGNALS_H
25 #define BCSIGNALS_H
26
27 #include "arraylist.h"
28 #include "bcsignals.inc"
29 #include <stdio.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <X11/Xlib.h>
33
34 #define TRON(x) BC_Signals::new_function(x);
35 #define TROFF(x) BC_Signals::delete_function(x);
36
37 // BC_Signals must be initialized at the start of every program using
38 // debugging.
39 //#define ENABLE_TRACE
40 #define TRACE_LOCKS
41 //#ifdef TRACE_LOCKS
42 //#undef TRACE_LOCKS
43 //#endif
44 //#define TRACE_MEMORY
45
46
47 // Need to use structs to avoid the memory manager.
48 // One of these tables is created every time someone locks a lock.
49 // After successfully locking, the table is flagged as being the owner of the lock.
50 // In the unlock function, the table flagged as the owner of the lock is deleted.
51 typedef struct 
52 {
53         void *ptr;
54         const char *title;
55         const char *location;
56         int is_owner;
57         int id;
58         pthread_t tid;
59 } bc_locktrace_t;
60
61 class BC_Signals
62 {
63         int (*old_err_handler)(Display *, XErrorEvent *);
64         static int x_error_handler(Display *display, XErrorEvent *event);
65 public:
66         BC_Signals();
67         ~BC_Signals();
68         void initialize();
69         void initialize2();
70         void terminate();
71
72
73         virtual void signal_handler(int signum);
74
75         static void dump_stack(FILE *fp=stdout);
76
77 #ifdef ENABLE_TRACE
78 // Add a trace
79 #define TRACE(text) BC_Signals::new_trace(text);
80 #define SET_TRACE BC_Signals::new_trace(__FILE__, __FUNCTION__, __LINE__);
81 #define PRINT_TRACE { printf("%s: %d\n", __FILE__, __LINE__); fflush(stdout); }
82 // Delete all traces
83 #define UNTRACE BC_Signals::delete_traces();
84
85 #else
86
87 #define TRACE(text) ;
88 #define UNTRACE ;
89 #define PRINT_TRACE { printf("%s: %d\n", __FILE__, __LINE__); fflush(stdout); }
90 //#define PRINT_TRACE ;
91 #define SET_TRACE ;
92
93 #endif
94
95
96 #ifdef TRACE_LOCKS
97
98 // Before user acquires
99 #define SET_LOCK(ptr, title, location) int table_id = BC_Signals::set_lock(ptr, title, location);
100 // After successful acquisition of a mutex, the table is flagged
101 #define SET_LOCK2 BC_Signals::set_lock2(table_id);
102 // After successful acquisition of a condition, the table is removed because
103 // the user never unlocks a condition after locking it.
104 // Release current lock table after failing to acquire
105 #define UNSET_LOCK2 BC_Signals::unset_lock2(table_id);
106
107 // Release current owner of lock
108 #define UNSET_LOCK(ptr) BC_Signals::unset_lock(ptr);
109
110 // Delete a lock
111 #define UNSET_ALL_LOCKS(ptr) BC_Signals::unset_all_locks(ptr);
112
113 #define LOCK_LOCKS(s) BC_Signals::lock_locks(s);
114 #define UNLOCK_LOCKS BC_Signals::unlock_locks();
115 #define CLEAR_LOCKS_TID(tid) BC_Signals::clear_locks_tid(tid);
116
117 #else
118
119 #define SET_LOCK(ptr, title, location) ;
120 #define SET_LOCK2 ;
121 #define SET_LOCK2_CONDITION ;
122 #define UNSET_LOCK(ptr) ;
123 #define UNSET_LOCK2 ;
124 #define UNSET_ALL_LOCKS(ptr) ;
125
126 #define LOCK_LOCKS(s) ;
127 #define UNLOCK_LOCKS ;
128 #define CLEAR_LOCKS_TID(tid) ;
129 #endif
130
131
132 #ifdef TRACE_MEMORY
133
134 #define ENABLE_BUFFER BC_Signals::enable_memory();
135 #define DISABLE_BUFFER BC_Signals::disable_memory();
136 // Note the size, pointer, and location of an allocation
137 #define BUFFER(size, ptr, location) BC_Signals::set_buffer(size, ptr, location);
138 // Note the pointer and location of an allocation
139 #define BUFFER2(ptr, location) BC_Signals::set_buffer(0, ptr, location);
140 // Remove a pointer from the allocation table
141 #define UNBUFFER(ptr) BC_Signals::unset_buffer(ptr);
142
143 #else
144
145 #define ENABLE_BUFFER ;
146 #define DISABLE_BUFFER ;
147 #define BUFFER(size, ptr, location);
148 #define UNBUFFER(ptr);
149
150 #endif
151
152 // Handling of temporary files in crash
153 #define SET_TEMP BC_Signals::set_temp
154 #define UNSET_TEMP BC_Signals::unset_temp
155
156 // Forks need to reset the lock status in case they forked when a lock was held.
157         static void reset_locks();
158
159 // Temporary files
160         static void delete_temps();
161         static void set_temp(char *string);
162         static void unset_temp(char *string);
163         static void signal_dump(int signum);
164
165
166         static void kill_subs();
167
168         static int set_lock(void *ptr, const char *title, const char *location);
169         static void set_lock2(int table_id);
170         static void set_lock2_condition(int table_id);
171         static void unset_lock2(int table_id);
172         static void unset_lock(void *ptr);
173 // Used in lock destructors so takes away all references
174         static void unset_all_locks(void *ptr);
175         static void clear_locks_tid(pthread_t tid);
176
177         static void new_trace(const char *text);
178         static void new_trace(const char *file, const char *function, int line);
179         static void delete_traces();
180
181         static void enable_memory();
182         static void disable_memory();
183         static void set_buffer(int size, void *ptr, const char* location);
184 // This one returns 1 if the buffer wasn't found.
185         static int unset_buffer(void *ptr);
186         static void lock_locks(const char *s);
187         static void unlock_locks();
188
189         static void dump_traces(FILE *fp=stdout);
190         static void dump_locks(FILE *fp=stdout);
191         static void dump_buffers(FILE *fp=stdout);
192         static void set_sighup_exit(int enable);
193
194         static void set_trap_path(const char *path);
195         static void set_trap_hook(void (*hook)(FILE *fp, void *data), void *data);
196         static void set_catch_segv(bool v);
197         static void set_catch_intr(bool v);
198
199 // Convert signum to text
200         static const char* sig_to_str(int number);
201
202         static BC_Signals *global_signals;
203         static const char *trap_path;
204         static void *trap_data;
205         static void (*trap_hook)(FILE *fp, void *vp);
206         static bool trap_sigsegv, trap_sigintr;
207 };
208
209
210 #endif