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/>.
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"
21 #include <mach-o/arch.h>
25 #include "../../safeguards.h"
28 /* Macro testing a stack address for valid alignment. */
30 #define IS_ALIGNED(addr) (((uintptr_t)(addr) & 0xf) == 8)
32 #define IS_ALIGNED(addr) (((uintptr_t)(addr) & 0xf) == 0)
35 /* printf format specification for 32/64-bit addresses. */
37 #define PRINTF_PTR "0x%016lx"
39 #define PRINTF_PTR "0x%08lx"
42 #define MAX_STACK_FRAMES 64
45 * OSX implementation for the crash logger.
47 class CrashLogOSX
: public CrashLog
{
48 /** Signal that has been thrown. */
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
,
65 " Release: %d.%d.%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
,
80 strsignal(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");
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
));
99 frame
= (void **)__builtin_frame_address(0);
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__)
109 if (ip
== NULL
) break;
111 /* Print running index. */
112 buffer
+= seprintf(buffer
, last
, " [%02d]", i
);
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
, '/');
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. */
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
);
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;
150 return buffer
+ seprintf(buffer
, last
, "\n");
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. */
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';
182 printf("Writing crash savegame...\n");
183 if (!this->WriteSavegame(filename_save
, lastof(filename_save
))) {
184 filename_save
[0] = '\0';
188 printf("Writing crash savegame...\n");
189 if (!this->WriteScreenshot(filename_screenshot
, lastof(filename_screenshot
))) {
190 filename_screenshot
[0] = '\0';
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.";
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
++) {
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",
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",
243 CrashLogOSX
log(signum
);
245 log
.DisplayCrashDialog();
247 CrashLog::AfterCrashLogCleanup();
251 /* static */ void CrashLog::InitialiseCrashLog()
253 for (const int *i
= _signals_to_handle
; i
!= endof(_signals_to_handle
); i
++) {
254 signal(*i
, HandleCrash
);