(svn r28004) -Update from Eints:
[openttd.git] / src / os / macosx / crashlog_osx.cpp
blobad258bbc91985699170c4a6863d554d52e86d3d6
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file crashlog_osx.cpp OS X crash log handler */
12 #include "../../stdafx.h"
13 #include "../../crashlog.h"
14 #include "../../string_func.h"
15 #include "../../gamelog.h"
16 #include "../../saveload/saveload.h"
17 #include "macos.h"
19 #include <errno.h>
20 #include <signal.h>
21 #include <mach-o/arch.h>
22 #include <dlfcn.h>
23 #include <cxxabi.h>
25 #include "../../safeguards.h"
28 /* Macro testing a stack address for valid alignment. */
29 #if defined(__i386__)
30 #define IS_ALIGNED(addr) (((uintptr_t)(addr) & 0xf) == 8)
31 #else
32 #define IS_ALIGNED(addr) (((uintptr_t)(addr) & 0xf) == 0)
33 #endif
35 /* printf format specification for 32/64-bit addresses. */
36 #ifdef __LP64__
37 #define PRINTF_PTR "0x%016lx"
38 #else
39 #define PRINTF_PTR "0x%08lx"
40 #endif
42 #define MAX_STACK_FRAMES 64
44 /**
45 * OSX implementation for the crash logger.
47 class CrashLogOSX : public CrashLog {
48 /** Signal that has been thrown. */
49 int signum;
51 char filename_log[MAX_PATH]; ///< Path of crash.log
52 char filename_save[MAX_PATH]; ///< Path of crash.sav
53 char filename_screenshot[MAX_PATH]; ///< Path of crash.(png|bmp|pcx)
55 /* virtual */ char *LogOSVersion(char *buffer, const char *last) const
57 int ver_maj, ver_min, ver_bug;
58 GetMacOSVersion(&ver_maj, &ver_min, &ver_bug);
60 const NXArchInfo *arch = NXGetLocalArchInfo();
62 return buffer + seprintf(buffer, last,
63 "Operating system:\n"
64 " Name: Mac OS X\n"
65 " Release: %d.%d.%d\n"
66 " Machine: %s\n"
67 " Min Ver: %d\n",
68 ver_maj, ver_min, ver_bug,
69 arch != NULL ? arch->description : "unknown",
70 MAC_OS_X_VERSION_MIN_REQUIRED
74 /* virtual */ char *LogError(char *buffer, const char *last, const char *message) const
76 return buffer + seprintf(buffer, last,
77 "Crash reason:\n"
78 " Signal: %s (%d)\n"
79 " Message: %s\n\n",
80 strsignal(this->signum),
81 this->signum,
82 message == NULL ? "<none>" : message
86 /* virtual */ char *LogStacktrace(char *buffer, const char *last) const
88 /* As backtrace() is only implemented in 10.5 or later,
89 * we're rolling our own here. Mostly based on
90 * http://stackoverflow.com/questions/289820/getting-the-current-stack-trace-on-mac-os-x
91 * and some details looked up in the Darwin sources. */
92 buffer += seprintf(buffer, last, "\nStacktrace:\n");
94 void **frame;
95 #if defined(__ppc__) || defined(__ppc64__)
96 /* Apple says __builtin_frame_address can be broken on PPC. */
97 __asm__ volatile("mr %0, r1" : "=r" (frame));
98 #else
99 frame = (void **)__builtin_frame_address(0);
100 #endif
102 for (int i = 0; frame != NULL && i < MAX_STACK_FRAMES; i++) {
103 /* Get IP for current stack frame. */
104 #if defined(__ppc__) || defined(__ppc64__)
105 void *ip = frame[2];
106 #else
107 void *ip = frame[1];
108 #endif
109 if (ip == NULL) break;
111 /* Print running index. */
112 buffer += seprintf(buffer, last, " [%02d]", i);
114 Dl_info dli;
115 bool dl_valid = dladdr(ip, &dli) != 0;
117 const char *fname = "???";
118 if (dl_valid && dli.dli_fname) {
119 /* Valid image name? Extract filename from the complete path. */
120 const char *s = strrchr(dli.dli_fname, '/');
121 if (s != NULL) {
122 fname = s + 1;
123 } else {
124 fname = dli.dli_fname;
127 /* Print image name and IP. */
128 buffer += seprintf(buffer, last, " %-20s " PRINTF_PTR, fname, (uintptr_t)ip);
130 /* Print function offset if information is available. */
131 if (dl_valid && dli.dli_sname != NULL && dli.dli_saddr != NULL) {
132 /* Try to demangle a possible C++ symbol. */
133 int status = -1;
134 char *func_name = abi::__cxa_demangle(dli.dli_sname, NULL, 0, &status);
136 long int offset = (intptr_t)ip - (intptr_t)dli.dli_saddr;
137 buffer += seprintf(buffer, last, " (%s + %ld)", func_name != NULL ? func_name : dli.dli_sname, offset);
139 free(func_name);
141 buffer += seprintf(buffer, last, "\n");
143 /* Get address of next stack frame. */
144 void **next = (void **)frame[0];
145 /* Frame address not increasing or not aligned? Broken stack, exit! */
146 if (next <= frame || !IS_ALIGNED(next)) break;
147 frame = next;
150 return buffer + seprintf(buffer, last, "\n");
153 public:
155 * A crash log is always generated by signal.
156 * @param signum the signal that was caused by the crash.
158 CrashLogOSX(int signum) : signum(signum)
160 filename_log[0] = '\0';
161 filename_save[0] = '\0';
162 filename_screenshot[0] = '\0';
165 /** Generate the crash log. */
166 bool MakeCrashLog()
168 char buffer[65536];
169 bool ret = true;
171 printf("Crash encountered, generating crash log...\n");
172 this->FillCrashLog(buffer, lastof(buffer));
173 printf("%s\n", buffer);
174 printf("Crash log generated.\n\n");
176 printf("Writing crash log to disk...\n");
177 if (!this->WriteCrashLog(buffer, filename_log, lastof(filename_log))) {
178 filename_log[0] = '\0';
179 ret = false;
182 printf("Writing crash savegame...\n");
183 if (!this->WriteSavegame(filename_save, lastof(filename_save))) {
184 filename_save[0] = '\0';
185 ret = false;
188 printf("Writing crash savegame...\n");
189 if (!this->WriteScreenshot(filename_screenshot, lastof(filename_screenshot))) {
190 filename_screenshot[0] = '\0';
191 ret = false;
194 return ret;
197 /** Show a dialog with the crash information. */
198 void DisplayCrashDialog() const
200 static const char crash_title[] =
201 "A serious fault condition occurred in the game. The game will shut down.";
203 char message[1024];
204 seprintf(message, lastof(message),
205 "Please send the generated crash information and the last (auto)save to the developers. "
206 "This will greatly help debugging. The correct place to do this is http://bugs.openttd.org.\n\n"
207 "Generated file(s):\n%s\n%s\n%s",
208 this->filename_log, this->filename_save, this->filename_screenshot);
210 ShowMacDialog(crash_title, message, "Quit");
214 /** The signals we want our crash handler to handle. */
215 static const int _signals_to_handle[] = { SIGSEGV, SIGABRT, SIGFPE, SIGBUS, SIGILL, SIGSYS };
218 * Entry point for the crash handler.
219 * @note Not static so it shows up in the backtrace.
220 * @param signum the signal that caused us to crash.
222 void CDECL HandleCrash(int signum)
224 /* Disable all handling of signals by us, so we don't go into infinite loops. */
225 for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) {
226 signal(*i, SIG_DFL);
229 if (GamelogTestEmergency()) {
230 ShowMacDialog("A serious fault condition occurred in the game. The game will shut down.",
231 "As you loaded an emergency savegame no crash information will be generated.\n",
232 "Quit");
233 abort();
236 if (SaveloadCrashWithMissingNewGRFs()) {
237 ShowMacDialog("A serious fault condition occurred in the game. The game will shut down.",
238 "As you loaded an savegame for which you do not have the required NewGRFs no crash information will be generated.\n",
239 "Quit");
240 abort();
243 CrashLogOSX log(signum);
244 log.MakeCrashLog();
245 log.DisplayCrashDialog();
247 CrashLog::AfterCrashLogCleanup();
248 abort();
251 /* static */ void CrashLog::InitialiseCrashLog()
253 for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) {
254 signal(*i, HandleCrash);