]> git.sesse.net Git - vlc/blob - bin/override.c
Add small backtrace
[vlc] / bin / override.c
1 /*****************************************************************************
2  * override.c: overriden function calls for VLC media player
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdbool.h>
26
27 void vlc_enable_override (void);
28
29 static bool override = false;
30
31 void vlc_enable_override (void)
32 {
33     override = true;
34 }
35
36 #if defined (__GNUC__) /* typeof and statement-expression */ \
37  && (defined (__ELF__) && !defined (__sun__))
38 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
39
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <dlfcn.h>
44 #include <pthread.h>
45 #ifdef HAVE_EXECINFO_H
46 # include <execinfo.h>
47 #endif
48
49 static void vlogbug (const char *level, const char *func, const char *fmt,
50                      va_list ap)
51 {
52 #ifdef HAVE_BACKTRACE
53     const size_t framec = 8;
54     void *framev[framec];
55
56     backtrace (framev, framec);
57 #endif
58     flockfile (stderr);
59     fprintf (stderr, "%s: call to %s(", level, func);
60     vfprintf (stderr, fmt, ap);
61     fputs (")\n", stderr);
62     fflush (stderr);
63 #ifdef HAVE_BACKTRACE
64     backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
65 #endif
66     funlockfile (stderr);
67 }
68
69 static void logbug (const char *level, const char *func, const char *fmt, ...)
70 {
71     va_list ap;
72
73     va_start (ap, fmt);
74     vlogbug (level, func, fmt, ap);
75     va_end (ap);
76 }
77
78 static void *getsym (const char *name)
79 {
80     void *sym = dlsym (RTLD_NEXT, name);
81     if (sym == NULL)
82     {
83         fprintf (stderr, "Cannot resolve symbol %s!\n", name);
84         abort ();
85     }
86     return sym;
87 }
88
89 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
90 #define CALL(func, ...) \
91     ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
92
93
94 /*** Environment ***
95  *
96  * "Conforming multi-threaded applications shall not use the environ variable
97  *  to access or modify any environment variable while any other thread is
98  *  concurrently modifying any environment variable." -- POSIX.
99  *
100  * Some evil libraries modify the environment. We currently ignore the calls as
101  * they could crash the process. This may cause funny behaviour though. */
102 int putenv (char *str)
103 {
104     if (override)
105     {
106         LOG("Blocked", "\"%s\"", str);
107         return 0;
108     }
109     return CALL(putenv, str);
110 }
111
112 int setenv (const char *name, const char *value, int overwrite)
113 {
114     if (override)
115     {
116         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
117         return 0;
118     }
119     return CALL(setenv, name, value, overwrite);
120 }
121
122 int unsetenv (const char *name)
123 {
124     if (override)
125     {
126         LOG("Blocked", "\"%s\"", name);
127         return 0;
128     }
129     return CALL(unsetenv, name);
130 }
131
132
133 /*** Pseudo random numbers ***
134  *
135  * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
136  * is much better as a reproducible non-secure PRNG). To work around this, we
137  * force evil callers to serialize. This makes the call safe, but fails to
138  * preserve reproducibility of the number sequence (which usually does not
139  * matter).
140  **/
141 static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
142
143 void srand (unsigned int seed)
144 {
145     pthread_mutex_lock (&prng_lock);
146     LOG("Warning", "%d", seed);
147     CALL(srand, seed);
148     pthread_mutex_unlock (&prng_lock);
149 }
150
151 int rand (void)
152 {
153     int ret;
154
155     pthread_mutex_lock (&prng_lock);
156     LOG("Warning", "");
157     ret = CALL(rand);
158     pthread_mutex_unlock (&prng_lock);
159     return ret;
160 }
161
162
163 /** Signals **/
164 #include <signal.h>
165
166 void (*signal (int signum, void (*handler) (int))) (int)
167 {
168     if (override)
169     {
170         const char *msg = "Error";
171
172         if ((signum == SIGPIPE && handler == SIG_IGN)
173          || (signum != SIGPIPE && handler == SIG_DFL))
174             /* Same settings we already use */
175             msg = "Warning";
176         LOG(msg, "%d, %p", signum, handler);
177     }
178     return CALL(signal, signum, handler);
179 }
180
181 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
182 {
183     if (act != NULL)
184         LOG("Error", "%d, %p, %p", signum, act, old);
185     return CALL(sigaction, signum, act, old);
186 }
187
188
189 #endif /* __ELF__ */