1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
27 // File: vogl_platform.cpp
28 #include "vogl_core.h"
29 #include "vogl_file_utils.h"
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
38 #if VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT
39 #warning VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT is enabled!
42 #ifdef VOGL_USE_WIN32_API
43 #include "vogl_winhdr.h"
46 // --------------------------------- Misc debugging related helpers
48 void vogl_debug_break(void)
53 #ifdef VOGL_USE_WIN32_API
55 void vogl_debug_break_if_debugging(void)
57 if (vogl_is_debugger_present())
63 bool vogl_is_debugger_present(void)
65 #if VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT
68 return IsDebuggerPresent() != 0;
72 void vogl_output_debug_string(const char *p)
74 OutputDebugStringA(p);
77 #else // !defined(VOGL_USE_WIN32_API)
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.
84 static int g_has_checked_for_debugger = -1;
86 bool vogl_is_debugger_present(bool force_check)
88 if ((g_has_checked_for_debugger < 0) || (force_check))
90 g_has_checked_for_debugger = 0;
92 dynamic_string_array status_strings;
93 if (vogl::file_utils::read_text_file_crt("/proc/self/status", status_strings))
95 for (uint i = 0; i < status_strings.size(); i++)
97 if (status_strings[i].contains("TracerPid"))
100 sscanf(status_strings[i].get_ptr(), "TracerPid: %u", &val);
103 g_has_checked_for_debugger = 1;
110 if (!g_has_checked_for_debugger)
112 pid_t parent_process_id = getppid();
113 dynamic_string exe_name(cVarArg, "/proc/%" PRIu64 "/exe", static_cast<uint64_t>(parent_process_id));
116 buf[sizeof(buf) - 1] = '\0';
117 int64_t n = readlink(exe_name.get_ptr(), buf, sizeof(buf) - 1);
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;
127 //printf("vogl_is_debugger_present: %i\n", g_has_checked_for_debugger);
130 #if VOGL_ASSUME_DEBUGGER_IS_ALWAYS_PRESENT
131 g_has_checked_for_debugger = 1;
134 return g_has_checked_for_debugger == 1;
138 int g_debugger_present = -1;
140 static void _sigtrap_handler(int signum)
142 VOGL_NOTE_UNUSED(signum);
143 g_debugger_present = 0;
144 signal(SIGTRAP, SIG_DFL);
147 void vogl_debug_break_if_debugging(void)
149 if (-1 == g_debugger_present)
151 g_debugger_present = 1;
152 signal(SIGTRAP, _sigtrap_handler);
155 if (g_debugger_present == 1)
161 void vogl_output_debug_string(const char *p)
166 #endif // #ifdef VOGL_USE_WIN32_API
168 // --------------------------------- Process signal/exception handling
170 #ifdef VOGL_USE_WIN32_API
172 #else // !defined(VOGL_USE_WIN32_API)
174 static const int MAX_SIGNALS = 16;
175 struct sigaction g_prev_sigactions[MAX_SIGNALS];
177 static vogl_exception_callback_t g_exception_callback = NULL;
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
183 static void signal_handler(int sig, siginfo_t *info, void *context)
190 static int s_recursion_count = 0;
192 buf.format("voglcore: ERROR: Caught signal %i\n", sig);
193 write(STDERR_FILENO, buf.get_ptr(), buf.size());
195 if (s_recursion_count)
197 buf.format("voglcore: Recursion detected handling signal %i\n", sig);
198 write(STDERR_FILENO, buf.get_ptr(), buf.size());
200 else if (g_exception_callback)
202 buf.format("voglcore: Calling global exception callback\n");
203 write(STDERR_FILENO, buf.get_ptr(), buf.size());
206 g_exception_callback();
210 if (sig >= MAX_SIGNALS)
212 buf.format("voglcore: ERROR: Unexpected signal %i\n", sig);
213 write(STDERR_FILENO, buf.get_ptr(), buf.size());
218 // pass on the exception to the prev handler
219 struct sigaction *pPrev_action = &g_prev_sigactions[sig];
221 if (pPrev_action->sa_flags & SA_SIGINFO)
223 buf.format("voglcore: Dispatching to prev handler for signal %i\n", sig);
224 write(STDERR_FILENO, buf.get_ptr(), buf.size());
226 pPrev_action->sa_sigaction(sig, info, context);
230 if (pPrev_action->sa_handler == SIG_DFL)
232 buf.format("voglcore: Taking default action for signal %i\n", sig);
233 write(STDERR_FILENO, buf.get_ptr(), buf.size());
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);
243 else if (pPrev_action->sa_handler == SIG_IGN)
246 buf.format("voglcore: Ignoring signal %i\n", sig);
247 write(STDERR_FILENO, buf.get_ptr(), buf.size());
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());
255 pPrev_action->sa_handler(sig);
260 vogl_exception_callback_t vogl_set_exception_callback(vogl_exception_callback_t callback)
262 if (g_exception_callback)
264 vogl_exception_callback_t pPrev = g_exception_callback;
265 g_exception_callback = callback;
269 g_exception_callback = callback;
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;
276 for (int i = 1; i < MAX_SIGNALS; i++)
278 if ((i == SIGPIPE) || (i == SIGKILL) || (i == SIGSTOP))
281 if (sigaction(i, NULL, &g_prev_sigactions[i]) >= 0)
282 sigaction(i, &new_action, NULL);
288 void vogl_reset_exception_callback()
290 g_exception_callback = NULL;
293 #endif // #ifdef VOGL_USE_WIN32_API
298 #ifdef VOGL_USE_WIN32_API
300 dynamic_string demangle(const char *pMangled)
303 console::debug("TODO: %s\n", VOGL_FUNCTION_NAME);
305 return dynamic_string(pMangled);
311 dynamic_string demangle(const char *pMangled)
315 char *p = abi::__cxa_demangle(pMangled, 0, 0, &status);
317 dynamic_string demangled(p ? p : pMangled);