void vlc_enable_override (void);
-static bool override = false;
-
-void vlc_enable_override (void)
-{
- override = true;
-}
-
#if defined (__GNUC__) /* typeof and statement-expression */ \
&& (defined (__ELF__) && !defined (__sun__))
/* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
+#include <pthread.h>
+#ifdef HAVE_EXECINFO_H
+# include <execinfo.h>
+#endif
+
+static bool override = false;
+
+static void vlc_reset_override (void)
+{
+ override = false;
+}
+
+void vlc_enable_override (void)
+{
+ override = true;
+ pthread_atfork (NULL, NULL, vlc_reset_override);
+}
static void vlogbug (const char *level, const char *func, const char *fmt,
va_list ap)
{
+#ifdef HAVE_BACKTRACE
+ const size_t framec = 8;
+ void *framev[framec];
+
+ backtrace (framev, framec);
+#endif
flockfile (stderr);
fprintf (stderr, "%s: call to %s(", level, func);
vfprintf (stderr, fmt, ap);
fputs (")\n", stderr);
+ fflush (stderr);
+#ifdef HAVE_BACKTRACE
+ backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
+#endif
funlockfile (stderr);
}
}
-#endif /* __ELF__ */
+/*** Pseudo random numbers ***
+ *
+ * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
+ * is much better as a reproducible non-secure PRNG). To work around this, we
+ * force evil callers to serialize. This makes the call safe, but fails to
+ * preserve reproducibility of the number sequence (which usually does not
+ * matter).
+ **/
+static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
+
+void srand (unsigned int seed)
+{
+ pthread_mutex_lock (&prng_lock);
+ LOG("Warning", "%d", seed);
+ CALL(srand, seed);
+ pthread_mutex_unlock (&prng_lock);
+}
+
+int rand (void)
+{
+ int ret;
+
+ pthread_mutex_lock (&prng_lock);
+ LOG("Warning", "");
+ ret = CALL(rand);
+ pthread_mutex_unlock (&prng_lock);
+ return ret;
+}
+
+
+/** Signals **/
+#include <signal.h>
+
+void (*signal (int signum, void (*handler) (int))) (int)
+{
+ if (override)
+ {
+ const char *msg = "Error";
+
+ if ((signum == SIGPIPE && handler == SIG_IGN)
+ || (signum != SIGPIPE && handler == SIG_DFL))
+ /* Same settings we already use */
+ msg = "Warning";
+ LOG(msg, "%d, %p", signum, handler);
+ }
+ return CALL(signal, signum, handler);
+}
+
+int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
+{
+ if (act != NULL)
+ LOG("Error", "%d, %p, %p", signum, act, old);
+ return CALL(sigaction, signum, act, old);
+}
+
+
+/*** Xlib ****/
+#ifdef HAVE_X11_XLIB_H
+# include <X11/Xlib.h>
+
+static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
+
+int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
+ (Display *, XErrorEvent *)
+{
+ if (override)
+ {
+ int (*ret) (Display *, XErrorEvent *);
+
+ pthread_mutex_lock (&xlib_lock);
+ LOG("Error", "%p", handler);
+ ret = CALL(XSetErrorHandler, handler);
+ pthread_mutex_unlock (&xlib_lock);
+ return ret;
+ }
+ return CALL(XSetErrorHandler, handler);
+}
+
+int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
+{
+ if (override)
+ {
+ int (*ret) (Display *);
+
+ pthread_mutex_lock (&xlib_lock);
+ LOG("Error", "%p", handler);
+ ret = CALL(XSetIOErrorHandler, handler);
+ pthread_mutex_unlock (&xlib_lock);
+ return ret;
+ }
+ return CALL(XSetIOErrorHandler, handler);
+}
+#endif
+#else
+static void vlc_enable_override (void)
+{
+}
+#endif