]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_platform.cpp
Initial vogl checkin
[vogl] / src / voglcore / vogl_platform.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_platform.cpp
28 #include "vogl_core.h"
29 #include "vogl_file_utils.h"
30
31 using namespace vogl;
32
33 // HACK HACK - don't leave this enabled
34 #ifndef VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT
35 #define VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT 0
36 #endif
37
38 #if VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT
39 #warning VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT is enabled!
40 #endif
41
42 #ifdef VOGL_USE_WIN32_API
43 #include "vogl_winhdr.h"
44 #endif
45
46 // --------------------------------- Misc debugging related helpers
47
48 void vogl_debug_break(void)
49 {
50     VOGL_BREAKPOINT
51 }
52
53 #ifdef VOGL_USE_WIN32_API
54
55 void vogl_debug_break_if_debugging(void)
56 {
57     if (vogl_is_debugger_present())
58     {
59         VOGL_BREAKPOINT
60     }
61 }
62
63 bool vogl_is_debugger_present(void)
64 {
65 #if VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT
66     return true;
67 #else
68     return IsDebuggerPresent() != 0;
69 #endif
70 }
71
72 void vogl_output_debug_string(const char *p)
73 {
74     OutputDebugStringA(p);
75 }
76
77 #else // !defined(VOGL_USE_WIN32_API)
78
79 // From http://stackoverflow.com/questions/3596781/detect-if-gdb-is-running
80 // For crying out loud, this is ridicuously complex compared to Windows and doesn't work if somebody attaches later.
81
82 #include <signal.h>
83
84 static int g_has_checked_for_debugger = -1;
85
86 bool vogl_is_debugger_present(bool force_check)
87 {
88     if ((g_has_checked_for_debugger < 0) || (force_check))
89     {
90         g_has_checked_for_debugger = 0;
91
92         dynamic_string_array status_strings;
93         if (vogl::file_utils::read_text_file_crt("/proc/self/status", status_strings))
94         {
95             for (uint i = 0; i < status_strings.size(); i++)
96             {
97                 if (status_strings[i].contains("TracerPid"))
98                 {
99                     int val = 0;
100                     sscanf(status_strings[i].get_ptr(), "TracerPid: %u", &val);
101                     if (val)
102                     {
103                         g_has_checked_for_debugger = 1;
104                         break;
105                     }
106                 }
107             }
108         }
109
110         if (!g_has_checked_for_debugger)
111         {
112             pid_t parent_process_id = getppid();
113             dynamic_string exe_name(cVarArg, "/proc/%" PRIu64 "/exe", static_cast<uint64_t>(parent_process_id));
114
115             char buf[4096];
116             buf[sizeof(buf) - 1] = '\0';
117             int64_t n = readlink(exe_name.get_ptr(), buf, sizeof(buf) - 1);
118
119             if (n >= 0)
120             {
121                 // total hackage to determine if the parent process is either gdb/lldb or qtcreator's process stub
122                 if ((strstr(buf, "gdb") != NULL) || (strstr(buf, "lldb") != NULL) || (strstr(buf, "qtcreator_process_stub") != NULL))
123                     g_has_checked_for_debugger = 1;
124             }
125         }
126
127         //printf("vogl_is_debugger_present: %i\n", g_has_checked_for_debugger);
128     }
129
130 #if VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT
131     g_has_checked_for_debugger = 1;
132     return true;
133 #else
134     return g_has_checked_for_debugger == 1;
135 #endif
136 }
137
138 int g_debugger_present = -1;
139
140 static void _sigtrap_handler(int signum)
141 {
142     VOGL_NOTE_UNUSED(signum);
143     g_debugger_present = 0;
144     signal(SIGTRAP, SIG_DFL);
145 }
146
147 void vogl_debug_break_if_debugging(void)
148 {
149     if (-1 == g_debugger_present)
150     {
151         g_debugger_present = 1;
152         signal(SIGTRAP, _sigtrap_handler);
153     }
154
155     if (g_debugger_present == 1)
156     {
157         __asm__("int3");
158     }
159 }
160
161 void vogl_output_debug_string(const char *p)
162 {
163     puts(p);
164 }
165
166 #endif // #ifdef VOGL_USE_WIN32_API
167
168 // --------------------------------- Process signal/exception handling
169
170 #ifdef VOGL_USE_WIN32_API
171
172 #else // !defined(VOGL_USE_WIN32_API)
173
174 static const int MAX_SIGNALS = 16;
175 struct sigaction g_prev_sigactions[MAX_SIGNALS];
176
177 static vogl_exception_callback_t g_exception_callback = NULL;
178
179 // Derived from apitrace, but this code appears to have been copied around and tweaked from multiple sources:
180 // - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
181 // - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
182
183 static void signal_handler(int sig, siginfo_t *info, void *context)
184 {
185     fixed_string256 buf;
186
187     if (sig == SIGPIPE)
188         return;
189
190     static int s_recursion_count = 0;
191
192     buf.format("voglcore: ERROR: Caught signal %i\n", sig);
193     write(STDERR_FILENO, buf.get_ptr(), buf.size());
194
195     if (s_recursion_count)
196     {
197         buf.format("voglcore: Recursion detected handling signal %i\n", sig);
198         write(STDERR_FILENO, buf.get_ptr(), buf.size());
199     }
200     else if (g_exception_callback)
201     {
202         buf.format("voglcore: Calling global exception callback\n");
203         write(STDERR_FILENO, buf.get_ptr(), buf.size());
204
205         ++s_recursion_count;
206         g_exception_callback();
207         --s_recursion_count;
208     }
209
210     if (sig >= MAX_SIGNALS)
211     {
212         buf.format("voglcore: ERROR: Unexpected signal %i\n", sig);
213         write(STDERR_FILENO, buf.get_ptr(), buf.size());
214
215         raise(SIGKILL);
216     }
217
218     // pass on the exception to the prev handler
219     struct sigaction *pPrev_action = &g_prev_sigactions[sig];
220
221     if (pPrev_action->sa_flags & SA_SIGINFO)
222     {
223         buf.format("voglcore: Dispatching to prev handler for signal %i\n", sig);
224         write(STDERR_FILENO, buf.get_ptr(), buf.size());
225
226         pPrev_action->sa_sigaction(sig, info, context);
227     }
228     else
229     {
230         if (pPrev_action->sa_handler == SIG_DFL)
231         {
232             buf.format("voglcore: Taking default action for signal %i\n", sig);
233             write(STDERR_FILENO, buf.get_ptr(), buf.size());
234
235             struct sigaction dfl_action;
236             dfl_action.sa_handler = SIG_DFL;
237             sigemptyset(&dfl_action.sa_mask);
238             dfl_action.sa_flags = 0;
239             sigaction(sig, &dfl_action, NULL);
240
241             raise(sig);
242         }
243         else if (pPrev_action->sa_handler == SIG_IGN)
244         {
245             // ignore
246             buf.format("voglcore: Ignoring signal %i\n", sig);
247             write(STDERR_FILENO, buf.get_ptr(), buf.size());
248         }
249         else
250         {
251             // dispatch to handler
252             buf.format("voglcore: Dispatching handler registered for signal %i\n", sig);
253             write(STDERR_FILENO, buf.get_ptr(), buf.size());
254
255             pPrev_action->sa_handler(sig);
256         }
257     }
258 }
259
260 vogl_exception_callback_t vogl_set_exception_callback(vogl_exception_callback_t callback)
261 {
262     if (g_exception_callback)
263     {
264         vogl_exception_callback_t pPrev = g_exception_callback;
265         g_exception_callback = callback;
266         return pPrev;
267     }
268
269     g_exception_callback = callback;
270
271     struct sigaction new_action;
272     new_action.sa_sigaction = signal_handler;
273     sigemptyset(&new_action.sa_mask);
274     new_action.sa_flags = SA_SIGINFO | SA_RESTART;
275
276     for (int i = 1; i < MAX_SIGNALS; i++)
277     {
278         if ((i == SIGPIPE) || (i == SIGKILL) || (i == SIGSTOP))
279             continue;
280
281         if (sigaction(i, NULL, &g_prev_sigactions[i]) >= 0)
282             sigaction(i, &new_action, NULL);
283     }
284
285     return NULL;
286 }
287
288 void vogl_reset_exception_callback()
289 {
290     g_exception_callback = NULL;
291 }
292
293 #endif // #ifdef VOGL_USE_WIN32_API
294
295 namespace vogl
296 {
297
298 #ifdef VOGL_USE_WIN32_API
299
300     dynamic_string demangle(const char *pMangled)
301     {
302         // TODO
303         console::debug("TODO: %s\n", VOGL_FUNCTION_NAME);
304
305         return dynamic_string(pMangled);
306     }
307
308 #else
309
310 #include <cxxabi.h>
311     dynamic_string demangle(const char *pMangled)
312     {
313         int status = 0;
314
315         char *p = abi::__cxa_demangle(pMangled, 0, 0, &status);
316
317         dynamic_string demangled(p ? p : pMangled);
318
319         std::free(p);
320
321         return demangled;
322     }
323
324 #endif
325
326 } // namespace vogl