2009-09-07 11:10:49 +00: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.h Functions to be called to log a crash */
|
|
|
|
|
|
|
|
#ifndef CRASHLOG_H
|
|
|
|
#define CRASHLOG_H
|
|
|
|
|
2019-08-20 19:47:17 +00:00
|
|
|
#include "core/enum_type.hpp"
|
2019-07-04 16:04:38 +00:00
|
|
|
#include <string>
|
2021-10-04 20:37:42 +00:00
|
|
|
#include <vector>
|
2019-07-04 16:04:38 +00:00
|
|
|
|
2022-11-12 16:18:15 +00:00
|
|
|
struct DesyncDeferredSaveInfo {
|
|
|
|
std::string name_buffer;
|
|
|
|
};
|
|
|
|
|
2019-08-20 19:47:17 +00:00
|
|
|
struct DesyncExtraInfo {
|
|
|
|
enum Flags {
|
|
|
|
DEIF_NONE = 0, ///< no flags
|
|
|
|
DEIF_RAND1 = 1 << 0, ///< random 1 mismatch
|
|
|
|
DEIF_RAND2 = 1 << 1, ///< random 2 mismatch
|
|
|
|
DEIF_STATE = 1 << 2, ///< state mismatch
|
|
|
|
DEIF_DBL_RAND = 1 << 3, ///< double-seed sent
|
|
|
|
};
|
|
|
|
|
|
|
|
Flags flags = DEIF_NONE;
|
2022-01-14 18:44:29 +00:00
|
|
|
const char *client_name = nullptr;
|
|
|
|
int client_id = -1;
|
2022-11-13 01:07:53 +00:00
|
|
|
uint32 desync_frame_seed = 0;
|
|
|
|
uint32 desync_frame_state_checksum = 0;
|
2019-08-21 18:15:02 +00:00
|
|
|
FILE **log_file = nullptr; ///< save unclosed log file handle here
|
2022-11-12 16:18:15 +00:00
|
|
|
DesyncDeferredSaveInfo *defer_savegame_write = nullptr;
|
2019-08-20 19:47:17 +00:00
|
|
|
};
|
|
|
|
DECLARE_ENUM_AS_BIT_SET(DesyncExtraInfo::Flags)
|
|
|
|
|
2021-10-03 22:57:27 +00:00
|
|
|
struct InconsistencyExtraInfo {
|
|
|
|
std::vector<std::string> check_caches_result;
|
|
|
|
};
|
|
|
|
|
2009-09-07 11:10:49 +00:00
|
|
|
/**
|
|
|
|
* Helper class for creating crash logs.
|
|
|
|
*/
|
|
|
|
class CrashLog {
|
|
|
|
private:
|
|
|
|
/** Pointer to the error message. */
|
|
|
|
static const char *message;
|
|
|
|
|
|
|
|
/** Temporary 'local' location of the buffer. */
|
|
|
|
static char *gamelog_buffer;
|
|
|
|
|
|
|
|
/** Temporary 'local' location of the end of the buffer. */
|
|
|
|
static const char *gamelog_last;
|
|
|
|
|
|
|
|
static void GamelogFillCrashLog(const char *s);
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Writes OS' version to the buffer.
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogOSVersion(char *buffer, const char *last) const = 0;
|
|
|
|
|
2010-01-18 10:11:27 +00:00
|
|
|
/**
|
|
|
|
* Writes compiler (and its version, if available) to the buffer.
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogCompiler(char *buffer, const char *last) const;
|
|
|
|
|
2016-06-12 19:58:52 +00:00
|
|
|
/**
|
|
|
|
* Writes OS' version detail to the buffer, if available.
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogOSVersionDetail(char *buffer, const char *last) const;
|
|
|
|
|
2009-09-07 11:10:49 +00:00
|
|
|
/**
|
|
|
|
* Writes actually encountered error to the buffer.
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param message Message passed to use for possible errors. Can be nullptr.
|
2009-09-07 11:10:49 +00:00
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogError(char *buffer, const char *last, const char *message) const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the stack trace to the buffer, if there is information about it
|
|
|
|
* available.
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogStacktrace(char *buffer, const char *last) const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes information about the data in the registers, if there is
|
|
|
|
* information about it available.
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogRegisters(char *buffer, const char *last) const;
|
|
|
|
|
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Writes the dynamically linked libraries/modules to the buffer, if there
|
2009-09-07 11:10:49 +00:00
|
|
|
* is information about it available.
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogModules(char *buffer, const char *last) const;
|
|
|
|
|
2016-02-25 19:21:06 +00:00
|
|
|
#ifdef USE_SCOPE_INFO
|
|
|
|
/**
|
|
|
|
* Writes the scope info log to the buffer.
|
|
|
|
* This may only be called when IsMainThread() returns true
|
|
|
|
* @param buffer The begin where to write at.
|
|
|
|
* @param last The last position in the buffer to write to.
|
|
|
|
* @return the position of the \c '\0' character after the buffer.
|
|
|
|
*/
|
|
|
|
virtual char *LogScopeInfo(char *buffer, const char *last) const;
|
|
|
|
#endif
|
2009-09-07 11:10:49 +00:00
|
|
|
|
|
|
|
char *LogOpenTTDVersion(char *buffer, const char *last) const;
|
|
|
|
char *LogConfiguration(char *buffer, const char *last) const;
|
|
|
|
char *LogLibraries(char *buffer, const char *last) const;
|
|
|
|
char *LogGamelog(char *buffer, const char *last) const;
|
2018-04-12 20:31:35 +00:00
|
|
|
char *LogRecentNews(char *buffer, const char *list) const;
|
2017-02-21 21:04:28 +00:00
|
|
|
char *LogCommandLog(char *buffer, const char *last) const;
|
2009-09-07 11:10:49 +00:00
|
|
|
|
|
|
|
public:
|
2020-04-27 16:31:35 +00:00
|
|
|
/** Buffer for the filename name prefix */
|
|
|
|
char name_buffer[64];
|
2022-04-25 17:48:28 +00:00
|
|
|
FILE *crash_file = nullptr;
|
|
|
|
const char *crash_buffer_write = nullptr;
|
2020-04-27 16:31:35 +00:00
|
|
|
|
2009-09-07 11:10:49 +00:00
|
|
|
/** Stub destructor to silence some compilers. */
|
|
|
|
virtual ~CrashLog() {}
|
|
|
|
|
2022-04-25 17:48:28 +00:00
|
|
|
char *FillCrashLog(char *buffer, const char *last);
|
|
|
|
void FlushCrashLogBuffer();
|
2019-08-20 19:47:17 +00:00
|
|
|
char *FillDesyncCrashLog(char *buffer, const char *last, const DesyncExtraInfo &info) const;
|
2021-10-03 22:57:27 +00:00
|
|
|
char *FillInconsistencyLog(char *buffer, const char *last, const InconsistencyExtraInfo &info) const;
|
2019-10-07 23:05:31 +00:00
|
|
|
char *FillVersionInfoLog(char *buffer, const char *last) const;
|
2019-08-21 18:15:02 +00:00
|
|
|
bool WriteCrashLog(const char *buffer, char *filename, const char *filename_last, const char *name = "crash", FILE **crashlog_file = nullptr) const;
|
2009-09-07 11:10:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the (crash) dump to a file.
|
|
|
|
* @note On success the filename will be filled with the full path of the
|
|
|
|
* crash dump file. Make sure filename is at least \c MAX_PATH big.
|
|
|
|
* @param filename Output for the filename of the written file.
|
|
|
|
* @param filename_last The last position in the filename buffer.
|
|
|
|
* @return if less than 0, error. If 0 no dump is made, otherwise the dump
|
2010-02-27 12:29:44 +00:00
|
|
|
* was successful (not all OSes support dumping files).
|
2009-09-07 11:10:49 +00:00
|
|
|
*/
|
|
|
|
virtual int WriteCrashDump(char *filename, const char *filename_last) const;
|
2022-11-12 16:18:15 +00:00
|
|
|
|
|
|
|
static bool WriteSavegame(char *filename, const char *filename_last, const char *name = "crash");
|
|
|
|
static bool WriteDiagnosticSavegame(char *filename, const char *filename_last, const char *name);
|
|
|
|
static bool WriteScreenshot(char *filename, const char *filename_last, const char *name = "crash");
|
2009-11-17 23:12:42 +00:00
|
|
|
|
2021-12-06 18:58:19 +00:00
|
|
|
bool MakeCrashLog(char *buffer, const char *last);
|
|
|
|
bool MakeCrashLogWithStackBuffer();
|
2019-08-20 19:47:17 +00:00
|
|
|
bool MakeDesyncCrashLog(const std::string *log_in, std::string *log_out, const DesyncExtraInfo &info) const;
|
2022-11-12 16:18:15 +00:00
|
|
|
static bool WriteDesyncSavegame(const char *log_data, const char *name_buffer);
|
2021-10-03 22:57:27 +00:00
|
|
|
bool MakeInconsistencyLog(const InconsistencyExtraInfo &info) const;
|
2019-10-07 23:05:31 +00:00
|
|
|
bool MakeVersionInfoLog() const;
|
2018-08-26 21:15:21 +00:00
|
|
|
bool MakeCrashSavegameAndScreenshot() const;
|
2009-09-07 11:10:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialiser for crash logs; do the appropriate things so crashes are
|
|
|
|
* handled by our crash handler instead of returning straight to the OS.
|
|
|
|
* @note must be implemented by all implementers of CrashLog.
|
|
|
|
*/
|
|
|
|
static void InitialiseCrashLog();
|
|
|
|
|
2021-03-13 20:34:51 +00:00
|
|
|
/**
|
|
|
|
* Prepare crash log handler for a newly started thread.
|
|
|
|
* @note must be implemented by all implementers of CrashLog.
|
|
|
|
*/
|
|
|
|
static void InitThread();
|
|
|
|
|
2019-08-20 19:47:17 +00:00
|
|
|
static void DesyncCrashLog(const std::string *log_in, std::string *log_out, const DesyncExtraInfo &info);
|
2021-10-03 22:57:27 +00:00
|
|
|
static void InconsistencyLog(const InconsistencyExtraInfo &info);
|
2019-10-07 23:05:31 +00:00
|
|
|
static void VersionInfoLog();
|
2019-05-17 17:55:45 +00:00
|
|
|
|
2009-09-07 11:10:49 +00:00
|
|
|
static void SetErrorMessage(const char *message);
|
|
|
|
static void AfterCrashLogCleanup();
|
2016-06-12 18:34:06 +00:00
|
|
|
|
|
|
|
inline const char *GetMessage() const { return this->message; }
|
2017-06-22 18:30:37 +00:00
|
|
|
|
|
|
|
static const char *GetAbortCrashlogReason();
|
2009-09-07 11:10:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CRASHLOG_H */
|