2009-10-04 23:08:30 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file crashlog_osx.cpp OS X crash log handler */
|
|
|
|
|
|
|
|
#include "../../stdafx.h"
|
|
|
|
#include "../../crashlog.h"
|
2023-08-20 17:16:08 +02:00
|
|
|
#include "../../fileio_func.h"
|
2009-10-04 23:08:30 +02:00
|
|
|
#include "../../string_func.h"
|
|
|
|
#include "../../gamelog.h"
|
2010-01-16 20:08:33 +01:00
|
|
|
#include "../../saveload/saveload.h"
|
2020-01-12 19:51:11 +01:00
|
|
|
#include "../../video/video_driver.hpp"
|
2009-10-04 23:08:30 +02:00
|
|
|
#include "macos.h"
|
|
|
|
|
2023-08-27 21:54:05 +02:00
|
|
|
#include <setjmp.h>
|
2009-10-04 23:08:30 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <mach-o/arch.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <cxxabi.h>
|
|
|
|
|
2023-08-20 17:16:08 +02:00
|
|
|
#ifdef WITH_UNOFFICIAL_BREAKPAD
|
|
|
|
# include <client/mac/handler/exception_handler.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
/* Macro testing a stack address for valid alignment. */
|
|
|
|
#if defined(__i386__)
|
|
|
|
#define IS_ALIGNED(addr) (((uintptr_t)(addr) & 0xf) == 8)
|
|
|
|
#else
|
|
|
|
#define IS_ALIGNED(addr) (((uintptr_t)(addr) & 0xf) == 0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_STACK_FRAMES 64
|
|
|
|
|
2023-08-27 21:54:05 +02:00
|
|
|
/** The signals we want our crash handler to handle. */
|
|
|
|
static constexpr int _signals_to_handle[] = { SIGSEGV, SIGABRT, SIGFPE, SIGBUS, SIGILL, SIGSYS, SIGQUIT };
|
|
|
|
|
2009-10-04 23:08:30 +02:00
|
|
|
/**
|
|
|
|
* OSX implementation for the crash logger.
|
|
|
|
*/
|
|
|
|
class CrashLogOSX : public CrashLog {
|
|
|
|
/** Signal that has been thrown. */
|
|
|
|
int signum;
|
|
|
|
|
2023-05-20 19:45:38 +02:00
|
|
|
void LogOSVersion(std::back_insert_iterator<std::string> &output_iterator) const override
|
2009-10-04 23:08:30 +02:00
|
|
|
{
|
|
|
|
int ver_maj, ver_min, ver_bug;
|
|
|
|
GetMacOSVersion(&ver_maj, &ver_min, &ver_bug);
|
|
|
|
|
|
|
|
const NXArchInfo *arch = NXGetLocalArchInfo();
|
|
|
|
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator,
|
2009-10-04 23:08:30 +02:00
|
|
|
"Operating system:\n"
|
2010-01-18 11:11:27 +01:00
|
|
|
" Name: Mac OS X\n"
|
2023-05-20 19:45:38 +02:00
|
|
|
" Release: {}.{}.{}\n"
|
|
|
|
" Machine: {}\n"
|
|
|
|
" Min Ver: {}\n"
|
|
|
|
" Max Ver: {}\n",
|
2009-10-04 23:08:30 +02:00
|
|
|
ver_maj, ver_min, ver_bug,
|
2019-04-10 23:07:06 +02:00
|
|
|
arch != nullptr ? arch->description : "unknown",
|
2020-04-10 23:53:07 +02:00
|
|
|
MAC_OS_X_VERSION_MIN_REQUIRED,
|
|
|
|
MAC_OS_X_VERSION_MAX_ALLOWED
|
2009-10-04 23:08:30 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 19:45:38 +02:00
|
|
|
void LogError(std::back_insert_iterator<std::string> &output_iterator, const std::string_view message) const override
|
2009-10-04 23:08:30 +02:00
|
|
|
{
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator,
|
2009-10-04 23:08:30 +02:00
|
|
|
"Crash reason:\n"
|
2023-05-20 19:45:38 +02:00
|
|
|
" Signal: {} ({})\n"
|
|
|
|
" Message: {}\n\n",
|
2009-10-04 23:08:30 +02:00
|
|
|
strsignal(this->signum),
|
|
|
|
this->signum,
|
2023-01-27 22:40:30 +01:00
|
|
|
message
|
2009-10-04 23:08:30 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 19:45:38 +02:00
|
|
|
void LogStacktrace(std::back_insert_iterator<std::string> &output_iterator) const override
|
2009-10-04 23:08:30 +02:00
|
|
|
{
|
|
|
|
/* As backtrace() is only implemented in 10.5 or later,
|
|
|
|
* we're rolling our own here. Mostly based on
|
|
|
|
* http://stackoverflow.com/questions/289820/getting-the-current-stack-trace-on-mac-os-x
|
|
|
|
* and some details looked up in the Darwin sources. */
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator, "\nStacktrace:\n");
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
void **frame;
|
|
|
|
#if defined(__ppc__) || defined(__ppc64__)
|
|
|
|
/* Apple says __builtin_frame_address can be broken on PPC. */
|
|
|
|
__asm__ volatile("mr %0, r1" : "=r" (frame));
|
|
|
|
#else
|
|
|
|
frame = (void **)__builtin_frame_address(0);
|
|
|
|
#endif
|
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
for (int i = 0; frame != nullptr && i < MAX_STACK_FRAMES; i++) {
|
2009-10-04 23:08:30 +02:00
|
|
|
/* Get IP for current stack frame. */
|
|
|
|
#if defined(__ppc__) || defined(__ppc64__)
|
|
|
|
void *ip = frame[2];
|
|
|
|
#else
|
|
|
|
void *ip = frame[1];
|
|
|
|
#endif
|
2019-04-10 23:07:06 +02:00
|
|
|
if (ip == nullptr) break;
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
/* Print running index. */
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator, " [{:02}]", i);
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
Dl_info dli;
|
|
|
|
bool dl_valid = dladdr(ip, &dli) != 0;
|
|
|
|
|
|
|
|
const char *fname = "???";
|
|
|
|
if (dl_valid && dli.dli_fname) {
|
|
|
|
/* Valid image name? Extract filename from the complete path. */
|
|
|
|
const char *s = strrchr(dli.dli_fname, '/');
|
2019-04-10 23:07:06 +02:00
|
|
|
if (s != nullptr) {
|
2009-10-04 23:08:30 +02:00
|
|
|
fname = s + 1;
|
|
|
|
} else {
|
|
|
|
fname = dli.dli_fname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Print image name and IP. */
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator, " {:20s} {}", fname, (uintptr_t)ip);
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
/* Print function offset if information is available. */
|
2019-04-10 23:07:06 +02:00
|
|
|
if (dl_valid && dli.dli_sname != nullptr && dli.dli_saddr != nullptr) {
|
2009-10-04 23:08:30 +02:00
|
|
|
/* Try to demangle a possible C++ symbol. */
|
|
|
|
int status = -1;
|
2019-04-10 23:07:06 +02:00
|
|
|
char *func_name = abi::__cxa_demangle(dli.dli_sname, nullptr, 0, &status);
|
2009-10-04 23:08:30 +02:00
|
|
|
|
2009-10-18 00:36:35 +02:00
|
|
|
long int offset = (intptr_t)ip - (intptr_t)dli.dli_saddr;
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator, " ({} + {})", func_name != nullptr ? func_name : dli.dli_sname, offset);
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
free(func_name);
|
|
|
|
}
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator, "\n");
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
/* Get address of next stack frame. */
|
|
|
|
void **next = (void **)frame[0];
|
|
|
|
/* Frame address not increasing or not aligned? Broken stack, exit! */
|
|
|
|
if (next <= frame || !IS_ALIGNED(next)) break;
|
|
|
|
frame = next;
|
|
|
|
}
|
|
|
|
|
2023-05-20 19:45:38 +02:00
|
|
|
fmt::format_to(output_iterator, "\n");
|
2009-10-04 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 17:16:08 +02:00
|
|
|
#ifdef WITH_UNOFFICIAL_BREAKPAD
|
|
|
|
static bool MinidumpCallback(const char* dump_dir, const char* minidump_id, void* context, bool succeeded)
|
|
|
|
{
|
|
|
|
CrashLogOSX *crashlog = reinterpret_cast<CrashLogOSX *>(context);
|
|
|
|
|
|
|
|
crashlog->crashdump_filename = crashlog->CreateFileName(".dmp");
|
|
|
|
std::rename(fmt::format("{}/{}.dmp", dump_dir, minidump_id).c_str(), crashlog->crashdump_filename.c_str());
|
|
|
|
return succeeded;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:54:05 +02:00
|
|
|
bool WriteCrashDump() override
|
2023-08-20 17:16:08 +02:00
|
|
|
{
|
2023-08-27 21:54:05 +02:00
|
|
|
return google_breakpad::ExceptionHandler::WriteMinidump(_personal_dir, MinidumpCallback, this);
|
2023-08-20 17:16:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-27 21:54:05 +02:00
|
|
|
/* virtual */ bool TryExecute(std::string_view section_name, std::function<bool()> &&func) override
|
|
|
|
{
|
|
|
|
this->try_execute_active = true;
|
|
|
|
|
|
|
|
/* Setup a longjump in case a crash happens. */
|
|
|
|
if (setjmp(this->internal_fault_jmp_buf) != 0) {
|
|
|
|
fmt::print("Something went wrong when attempting to fill {} section of the crash log.\n", section_name);
|
|
|
|
|
|
|
|
/* Reset the signals and continue on. The handler is responsible for dealing with the crash. */
|
|
|
|
sigset_t sigs;
|
|
|
|
sigemptyset(&sigs);
|
|
|
|
for (int signum : _signals_to_handle) {
|
|
|
|
sigaddset(&sigs, signum);
|
|
|
|
}
|
|
|
|
sigprocmask(SIG_UNBLOCK, &sigs, nullptr);
|
|
|
|
|
|
|
|
this->try_execute_active = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool res = func();
|
|
|
|
this->try_execute_active = false;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-10-04 23:08:30 +02:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* A crash log is always generated by signal.
|
|
|
|
* @param signum the signal that was caused by the crash.
|
|
|
|
*/
|
2023-05-20 20:04:43 +02:00
|
|
|
CrashLogOSX(int signum) : signum(signum) {}
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
/** Show a dialog with the crash information. */
|
|
|
|
void DisplayCrashDialog() const
|
|
|
|
{
|
|
|
|
static const char crash_title[] =
|
2010-07-31 23:02:56 +02:00
|
|
|
"A serious fault condition occurred in the game. The game will shut down.";
|
2009-10-04 23:08:30 +02:00
|
|
|
|
2023-05-20 20:04:43 +02:00
|
|
|
std::string message = fmt::format(
|
2023-08-27 19:58:31 +02:00
|
|
|
"Please send crash.log, crash.dmp, and crash.sav to the developers. "
|
|
|
|
"This will greatly help debugging.\n\n"
|
|
|
|
"https://github.com/OpenTTD/OpenTTD/issues.\n\n"
|
|
|
|
"{}\n{}\n{}\n{}",
|
2023-08-20 17:16:08 +02:00
|
|
|
this->crashlog_filename, this->crashdump_filename, this->savegame_filename, this->screenshot_filename);
|
2009-10-04 23:08:30 +02:00
|
|
|
|
2023-05-20 20:04:43 +02:00
|
|
|
ShowMacDialog(crash_title, message.c_str(), "Quit");
|
2009-10-04 23:08:30 +02:00
|
|
|
}
|
2023-08-27 21:54:05 +02:00
|
|
|
|
|
|
|
/** Buffer to track the long jump set setup. */
|
|
|
|
jmp_buf internal_fault_jmp_buf;
|
|
|
|
|
|
|
|
/** Whether we are in a TryExecute block. */
|
|
|
|
bool try_execute_active = false;
|
|
|
|
|
|
|
|
/** Points to the current crash log. */
|
|
|
|
static CrashLogOSX *current;
|
2009-10-04 23:08:30 +02:00
|
|
|
};
|
|
|
|
|
2023-08-27 21:54:05 +02:00
|
|
|
/* static */ CrashLogOSX *CrashLogOSX::current = nullptr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a signal handler for all signals we want to capture.
|
|
|
|
*
|
|
|
|
* @param handler The handler to use.
|
|
|
|
* @return sigset_t A sigset_t containing all signals we want to capture.
|
|
|
|
*/
|
|
|
|
static sigset_t SetSignals(void(*handler)(int))
|
|
|
|
{
|
|
|
|
sigset_t sigs;
|
|
|
|
sigemptyset(&sigs);
|
|
|
|
for (int signum : _signals_to_handle) {
|
|
|
|
sigaddset(&sigs, signum);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_handler = handler;
|
|
|
|
sa.sa_mask = sigs;
|
|
|
|
|
|
|
|
for (int signum : _signals_to_handle) {
|
|
|
|
sigaction(signum, &sa, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for a crash that happened during the handling of a crash.
|
|
|
|
*
|
|
|
|
* @param signum the signal that caused us to crash.
|
|
|
|
*/
|
|
|
|
static void CDECL HandleInternalCrash(int signum)
|
|
|
|
{
|
|
|
|
if (CrashLogOSX::current == nullptr || !CrashLogOSX::current->try_execute_active) {
|
|
|
|
fmt::print("Something went seriously wrong when creating the crash log. Aborting.\n");
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
longjmp(CrashLogOSX::current->internal_fault_jmp_buf, 1);
|
|
|
|
}
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for the crash handler.
|
2023-08-27 21:54:05 +02:00
|
|
|
*
|
2009-10-04 23:08:30 +02:00
|
|
|
* @param signum the signal that caused us to crash.
|
|
|
|
*/
|
2023-08-27 21:54:05 +02:00
|
|
|
static void CDECL HandleCrash(int signum)
|
2009-10-04 23:08:30 +02:00
|
|
|
{
|
2023-08-27 21:54:05 +02:00
|
|
|
if (CrashLogOSX::current != nullptr) {
|
|
|
|
CrashLog::AfterCrashLogCleanup();
|
|
|
|
_exit(2);
|
2009-10-04 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:54:05 +02:00
|
|
|
/* Capture crashing during the handling of a crash. */
|
|
|
|
sigset_t sigs = SetSignals(HandleInternalCrash);
|
|
|
|
sigset_t old_sigset;
|
|
|
|
sigprocmask(SIG_UNBLOCK, &sigs, &old_sigset);
|
|
|
|
|
2023-05-01 19:14:31 +02:00
|
|
|
if (_gamelog.TestEmergency()) {
|
2010-07-31 23:02:56 +02:00
|
|
|
ShowMacDialog("A serious fault condition occurred in the game. The game will shut down.",
|
2009-10-04 23:08:30 +02:00
|
|
|
"As you loaded an emergency savegame no crash information will be generated.\n",
|
|
|
|
"Quit");
|
2023-08-27 21:54:05 +02:00
|
|
|
_exit(3);
|
2009-10-04 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
2010-01-16 20:08:33 +01:00
|
|
|
if (SaveloadCrashWithMissingNewGRFs()) {
|
2010-07-31 23:02:56 +02:00
|
|
|
ShowMacDialog("A serious fault condition occurred in the game. The game will shut down.",
|
2010-01-16 20:08:33 +01:00
|
|
|
"As you loaded an savegame for which you do not have the required NewGRFs no crash information will be generated.\n",
|
|
|
|
"Quit");
|
2023-08-27 21:54:05 +02:00
|
|
|
_exit(3);
|
2010-01-16 20:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:54:05 +02:00
|
|
|
CrashLogOSX *log = new CrashLogOSX(signum);
|
|
|
|
CrashLogOSX::current = log;
|
|
|
|
log->MakeCrashLog();
|
2020-01-12 19:51:11 +01:00
|
|
|
if (VideoDriver::GetInstance() == nullptr || VideoDriver::GetInstance()->HasGUI()) {
|
2023-08-27 21:54:05 +02:00
|
|
|
log->DisplayCrashDialog();
|
2020-01-12 19:51:11 +01:00
|
|
|
}
|
2009-10-04 23:08:30 +02:00
|
|
|
|
|
|
|
CrashLog::AfterCrashLogCleanup();
|
2023-08-27 21:54:05 +02:00
|
|
|
_exit(2);
|
2009-10-04 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void CrashLog::InitialiseCrashLog()
|
|
|
|
{
|
2023-08-27 21:54:05 +02:00
|
|
|
SetSignals(HandleCrash);
|
2009-10-04 23:08:30 +02:00
|
|
|
}
|
2021-03-13 21:34:51 +01:00
|
|
|
|
|
|
|
/* static */ void CrashLog::InitThread()
|
|
|
|
{
|
|
|
|
}
|