OpenRCT2/src/openrct2/platform/Crash.cpp

321 lines
11 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2019 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-01-18 13:55:34 +01:00
#include "Crash.h"
#ifdef USE_BREAKPAD
# include <iterator>
2018-10-09 23:18:15 +02:00
# include <map>
2018-07-21 16:17:06 +02:00
# include <memory>
# include <stdio.h>
# if defined(_WIN32)
# include <ShlObj.h>
# include <client/windows/handler/exception_handler.h>
2018-10-09 23:18:15 +02:00
# include <common/windows/http_upload.h>
2018-07-21 16:17:06 +02:00
# include <string>
# else
# error Breakpad support not implemented yet for this platform
# endif
2020-03-31 23:12:35 +02:00
# include "../Game.h"
# include "../OpenRCT2.h"
2018-07-21 16:17:06 +02:00
# include "../Version.h"
2018-11-30 12:42:53 +01:00
# include "../config/Config.h"
2018-07-21 16:17:06 +02:00
# include "../core/Console.hpp"
# include "../core/Guard.hpp"
2018-10-09 23:18:15 +02:00
# include "../core/String.hpp"
# include "../interface/Screenshot.h"
2018-07-21 16:17:06 +02:00
# include "../localisation/Language.h"
# include "../rct2/S6Exporter.h"
# include "../scenario/Scenario.h"
2018-10-20 23:54:59 +02:00
# include "../util/Util.h"
2018-07-21 16:17:06 +02:00
# include "platform.h"
# define WSZ(x) L"" x
# ifdef OPENRCT2_COMMIT_SHA1_SHORT
2018-06-22 23:04:38 +02:00
const wchar_t* _wszCommitSha1Short = WSZ(OPENRCT2_COMMIT_SHA1_SHORT);
2018-07-21 16:17:06 +02:00
# else
2018-06-22 23:04:38 +02:00
const wchar_t* _wszCommitSha1Short = WSZ("");
2018-07-21 16:17:06 +02:00
# endif
2016-08-28 18:17:20 +02:00
// OPENRCT2_ARCHITECTURE is required to be defined in version.h
2018-06-22 23:04:38 +02:00
const wchar_t* _wszArchitecture = WSZ(OPENRCT2_ARCHITECTURE);
2018-10-20 23:54:59 +02:00
// Note: uploading gzipped crash dumps manually requires specifying
// 'Content-Encoding: gzip' header in HTTP request, but we cannot do that,
// so just hope the file name with '.gz' suffix is enough.
// For docs on uplading to backtrace.io check
// https://documentation.backtrace.io/product_integration_minidump_breakpad/
static bool UploadMinidump(const std::map<std::wstring, std::wstring>& files, int& error, std::wstring& response)
2018-10-09 23:18:15 +02:00
{
for (auto file : files)
{
wprintf(L"files[%s] = %s\n", file.first.c_str(), file.second.c_str());
}
2018-10-20 23:54:59 +02:00
std::wstring url(L"https://openrct2.sp.backtrace.io:6098/"
L"post?format=minidump&token=bbafef6546d0845da6751ea6c28763a749883d94a371d56904a629e73f3b8910");
2018-10-09 23:18:15 +02:00
std::map<std::wstring, std::wstring> parameters;
parameters[L"product_name"] = L"openrct2";
// In case of releases this can be empty
if (wcslen(_wszCommitSha1Short) > 0)
{
2018-10-20 23:54:59 +02:00
parameters[L"commit"] = _wszCommitSha1Short;
2018-10-09 23:18:15 +02:00
}
else
{
parameters[L"commit"] = String::ToWideChar(gVersionInfoFull);
2018-10-09 23:18:15 +02:00
}
auto assertMsg = Guard::GetLastAssertMessage();
if (assertMsg)
{
parameters[L"assert_failure"] = String::ToWideChar(*assertMsg);
}
2018-10-09 23:18:15 +02:00
int timeout = 10000;
bool success = google_breakpad::HTTPUpload::SendMultipartPostRequest(url, parameters, files, &timeout, &response, &error);
2018-10-09 23:18:15 +02:00
wprintf(L"Success = %d, error = %d, response = %s\n", success, error, response.c_str());
return success;
}
2018-06-22 23:04:38 +02:00
static bool OnCrash(
const wchar_t* dumpPath, const wchar_t* miniDumpId, void* context, EXCEPTION_POINTERS* exinfo,
MDRawAssertionInfo* assertion, bool succeeded)
{
if (!succeeded)
{
constexpr const char* DumpFailedMessage = "Failed to create the dump. Please file an issue with OpenRCT2 on GitHub and "
"provide latest save, and provide "
"information about what you did before the crash occurred.";
printf("%s\n", DumpFailedMessage);
if (!gOpenRCT2SilentBreakpad)
{
2017-08-15 10:07:44 +02:00
MessageBoxA(nullptr, DumpFailedMessage, OPENRCT2_NAME, MB_OK | MB_ICONERROR);
}
return succeeded;
}
std::map<std::wstring, std::wstring> uploadFiles;
// Get filenames
wchar_t dumpFilePath[MAX_PATH];
wchar_t saveFilePath[MAX_PATH];
2018-11-30 12:42:53 +01:00
wchar_t configFilePath[MAX_PATH];
wchar_t saveFilePathGZIP[MAX_PATH];
2020-03-31 23:12:35 +02:00
wchar_t recordFilePathNew[MAX_PATH];
swprintf_s(dumpFilePath, std::size(dumpFilePath), L"%s\\%s.dmp", dumpPath, miniDumpId);
swprintf_s(saveFilePath, std::size(saveFilePath), L"%s\\%s.sv6", dumpPath, miniDumpId);
swprintf_s(configFilePath, std::size(configFilePath), L"%s\\%s.ini", dumpPath, miniDumpId);
swprintf_s(saveFilePathGZIP, std::size(saveFilePathGZIP), L"%s\\%s.sv6.gz", dumpPath, miniDumpId);
2020-03-31 23:12:35 +02:00
swprintf_s(recordFilePathNew, std::size(recordFilePathNew), L"%s\\%s.sv6r", dumpPath, miniDumpId);
wchar_t dumpFilePathNew[MAX_PATH];
2018-06-22 23:04:38 +02:00
swprintf_s(
dumpFilePathNew, std::size(dumpFilePathNew), L"%s\\%s(%s_%s).dmp", dumpPath, miniDumpId, _wszCommitSha1Short,
2018-06-22 23:04:38 +02:00
_wszArchitecture);
2018-10-20 23:54:59 +02:00
wchar_t dumpFilePathGZIP[MAX_PATH];
swprintf_s(dumpFilePathGZIP, std::size(dumpFilePathGZIP), L"%s.gz", dumpFilePathNew);
2018-10-20 23:54:59 +02:00
// Compress the dump
2018-10-20 23:54:59 +02:00
{
FILE* input = _wfopen(dumpFilePath, L"rb");
FILE* dest = _wfopen(dumpFilePathGZIP, L"wb");
if (util_gzip_compress(input, dest))
{
// TODO: enable upload of gzip-compressed dumps once supported on
// backtrace.io (uncomment the line below). For now leave compression
// on, as GitHub will accept .gz files, even though it does not
// advertise it officially.
/*
uploadFiles[L"upload_file_minidump"] = dumpFilePathGZIP;
*/
}
fclose(input);
fclose(dest);
2018-10-20 23:54:59 +02:00
}
2020-03-31 23:12:35 +02:00
bool with_record = stop_silent_record();
2018-10-20 23:54:59 +02:00
// Try to rename the files
if (_wrename(dumpFilePath, dumpFilePathNew) == 0)
{
std::wcscpy(dumpFilePath, dumpFilePathNew);
}
uploadFiles[L"upload_file_minidump"] = dumpFilePath;
2018-10-20 23:54:59 +02:00
// Compress to gzip-compatible stream
// Log information to output
wprintf(L"Dump Path: %s\n", dumpPath);
wprintf(L"Dump File Path: %s\n", dumpFilePath);
wprintf(L"Dump Id: %s\n", miniDumpId);
wprintf(L"Version: %s\n", WSZ(OPENRCT2_VERSION));
2016-08-28 18:17:20 +02:00
wprintf(L"Commit: %s\n", _wszCommitSha1Short);
bool savedGameDumped = false;
auto saveFilePathUTF8 = String::ToUtf8(saveFilePath);
2017-02-08 23:17:01 +01:00
try
{
auto exporter = std::make_unique<S6Exporter>();
exporter->Export();
exporter->SaveGame(saveFilePathUTF8.c_str());
savedGameDumped = true;
}
2018-06-22 23:04:38 +02:00
catch (const std::exception&)
2017-02-08 23:17:01 +01:00
{
}
// Compress the save
if (savedGameDumped)
{
FILE* input = _wfopen(saveFilePath, L"rb");
FILE* dest = _wfopen(saveFilePathGZIP, L"wb");
if (util_gzip_compress(input, dest))
{
uploadFiles[L"attachment_park.sv6.gz"] = saveFilePathGZIP;
}
else
{
uploadFiles[L"attachment_park.sv6"] = saveFilePath;
}
fclose(input);
fclose(dest);
}
auto configFilePathUTF8 = String::ToUtf8(configFilePath);
if (config_save(configFilePathUTF8.c_str()))
2018-11-30 12:42:53 +01:00
{
uploadFiles[L"attachment_config.ini"] = configFilePath;
}
std::string screenshotPath = screenshot_dump();
if (!screenshotPath.empty())
{
auto screenshotPathW = String::ToWideChar(screenshotPath.c_str());
uploadFiles[L"attachment_screenshot.png"] = screenshotPathW;
}
2020-03-31 23:12:35 +02:00
if (with_record)
{
auto sv6rPathW = String::ToWideChar(gSilentRecordingName);
bool record_copied = CopyFileW(sv6rPathW.c_str(), recordFilePathNew, true);
if (record_copied)
{
uploadFiles[L"attachment_replay.sv6r"] = recordFilePathNew;
}
else
{
with_record = false;
}
}
if (gOpenRCT2SilentBreakpad)
{
int error;
std::wstring response;
UploadMinidump(uploadFiles, error, response);
return succeeded;
}
2017-02-08 23:17:01 +01:00
constexpr const wchar_t* MessageFormat = L"A crash has occurred and a dump was created at\n%s.\n\nPlease file an issue "
L"with OpenRCT2 on GitHub, and provide "
2018-10-09 23:18:15 +02:00
L"the dump and saved game there.\n\nVersion: %s\nCommit: %s\n\n"
L"We would like to upload the crash dump for automated analysis, do you agree?\n"
L"The automated analysis is done by courtesy of https://backtrace.io/";
wchar_t message[MAX_PATH * 2];
2018-06-22 23:04:38 +02:00
swprintf_s(message, MessageFormat, dumpFilePath, WSZ(OPENRCT2_VERSION), _wszCommitSha1Short);
// Cannot use platform_show_messagebox here, it tries to set parent window already dead.
2018-10-25 23:18:19 +02:00
int answer = MessageBoxW(nullptr, message, WSZ(OPENRCT2_NAME), MB_YESNO | MB_ICONERROR);
2018-10-09 23:18:15 +02:00
if (answer == IDYES)
{
int error;
std::wstring response;
bool ok = UploadMinidump(uploadFiles, error, response);
if (!ok)
{
const wchar_t* MessageFormat2 = L"There was a problem while uploading the dump. Please upload it manually to "
L"GitHub. It should be highlighted for you once you close this message.\n"
L"Please provide following information as well:\n"
L"Error code = %d\n"
L"Response = %s";
swprintf_s(message, MessageFormat2, error, response.c_str());
2018-10-25 23:18:19 +02:00
MessageBoxW(nullptr, message, WSZ(OPENRCT2_NAME), MB_OK | MB_ICONERROR);
}
else
{
MessageBoxW(nullptr, L"Dump uploaded succesfully.", WSZ(OPENRCT2_NAME), MB_OK | MB_ICONINFORMATION);
}
2018-10-09 23:18:15 +02:00
}
2017-08-15 10:07:44 +02:00
HRESULT coInitializeResult = CoInitialize(nullptr);
if (SUCCEEDED(coInitializeResult))
{
2016-08-28 18:19:13 +02:00
LPITEMIDLIST pidl = ILCreateFromPathW(dumpPath);
2020-03-31 23:12:35 +02:00
LPITEMIDLIST files[4];
uint32_t numFiles = 0;
files[numFiles++] = ILCreateFromPathW(dumpFilePath);
2018-10-20 23:54:59 +02:00
// There should be no need to check if this file exists, if it doesn't
// it simply shouldn't get selected.
files[numFiles++] = ILCreateFromPathW(dumpFilePathGZIP);
if (savedGameDumped)
{
files[numFiles++] = ILCreateFromPathW(saveFilePath);
}
2020-03-31 23:12:35 +02:00
if (with_record)
{
files[numFiles++] = ILCreateFromPathW(recordFilePathNew);
}
2018-06-22 23:04:38 +02:00
if (pidl != nullptr)
{
SHOpenFolderAndSelectItems(pidl, numFiles, (LPCITEMIDLIST*)files, 0);
ILFree(pidl);
for (uint32_t i = 0; i < numFiles; i++)
{
ILFree(files[i]);
}
}
CoUninitialize();
}
// Return whether the dump was successful
return succeeded;
2016-04-09 19:21:40 +02:00
}
static std::wstring GetDumpDirectory()
{
char userDirectory[MAX_PATH];
2017-08-15 10:07:44 +02:00
platform_get_user_directory(userDirectory, nullptr, sizeof(userDirectory));
auto result = String::ToWideChar(userDirectory);
return result;
}
// Using non-null pipe name here lets breakpad try setting OOP crash handling
2018-06-22 23:04:38 +02:00
constexpr const wchar_t* PipeName = L"openrct2-bpad";
2016-10-06 23:32:10 +02:00
#endif // USE_BREAKPAD
2018-02-01 18:49:14 +01:00
CExceptionHandler crash_init()
{
#ifdef USE_BREAKPAD
// Path must exist and be RW!
auto exHandler = new google_breakpad::ExceptionHandler(
2018-06-22 23:04:38 +02:00
GetDumpDirectory(), 0, OnCrash, 0, google_breakpad::ExceptionHandler::HANDLER_ALL, MiniDumpWithDataSegs, PipeName, 0);
return reinterpret_cast<CExceptionHandler>(exHandler);
2018-06-22 23:04:38 +02:00
#else // USE_BREAKPAD
return nullptr;
#endif // USE_BREAKPAD
}