Files @ r27812:2784fed9ba40
Branch filter:

Location: cpp/openttd-patchpack/source/src/crashlog.cpp

Patric Stout
Remove: [Win32] (pointer-only) stack trace in crash.log (#11211)

It only contains pointers, which nobody can decipher anyway.
So instead, just report "Not supported", like other targets do
when they can't print a sane stack trace.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * 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.cpp Implementation of generic function to be called to log a crash */

#include "stdafx.h"
#include "crashlog.h"
#include "gamelog.h"
#include "timer/timer_game_calendar.h"
#include "map_func.h"
#include "rev.h"
#include "strings_func.h"
#include "blitter/factory.hpp"
#include "base_media_base.h"
#include "music/music_driver.hpp"
#include "sound/sound_driver.hpp"
#include "video/video_driver.hpp"
#include "saveload/saveload.h"
#include "screenshot.h"
#include "gfx_func.h"
#include "network/network.h"
#include "network/network_survey.h"
#include "language.h"
#include "fontcache.h"
#include "news_gui.h"

#include "ai/ai_info.hpp"
#include "game/game.hpp"
#include "game/game_info.hpp"
#include "company_base.h"
#include "company_func.h"
#include "3rdparty/fmt/chrono.h"
#include "3rdparty/fmt/std.h"

#ifdef WITH_ALLEGRO
#	include <allegro.h>
#endif /* WITH_ALLEGRO */
#ifdef WITH_FONTCONFIG
#	include <fontconfig/fontconfig.h>
#endif /* WITH_FONTCONFIG */
#ifdef WITH_PNG
	/* pngconf.h, included by png.h doesn't like something in the
	 * freetype headers. As such it's not alphabetically sorted. */
#	include <png.h>
#endif /* WITH_PNG */
#ifdef WITH_FREETYPE
#	include <ft2build.h>
#	include FT_FREETYPE_H
#endif /* WITH_FREETYPE */
#ifdef WITH_HARFBUZZ
#	include <hb.h>
#endif /* WITH_HARFBUZZ */
#ifdef WITH_ICU_I18N
#	include <unicode/uversion.h>
#endif /* WITH_ICU_I18N */
#ifdef WITH_LIBLZMA
#	include <lzma.h>
#endif
#ifdef WITH_LZO
#include <lzo/lzo1x.h>
#endif
#if defined(WITH_SDL) || defined(WITH_SDL2)
#	include <SDL.h>
#endif /* WITH_SDL || WITH_SDL2 */
#ifdef WITH_ZLIB
# include <zlib.h>
#endif
#ifdef WITH_CURL
# include <curl/curl.h>
#endif

#include "safeguards.h"

/* static */ std::string CrashLog::message{ "<none>" };

void CrashLog::LogCompiler(std::back_insert_iterator<std::string> &output_iterator) const
{
	fmt::format_to(output_iterator, " Compiler: "
#if defined(_MSC_VER)
			"MSVC {}", _MSC_VER
#elif defined(__ICC) && defined(__GNUC__)
			"ICC {} (GCC {}.{}.{} mode)", __ICC,  __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
#elif defined(__ICC)
			"ICC {}", __ICC
#elif defined(__GNUC__)
			"GCC {}.{}.{}", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
#else
			"<unknown>"
#endif
			);
#if defined(__VERSION__)
	fmt::format_to(output_iterator, " \"" __VERSION__ "\"\n\n");
#else
	fmt::format_to(output_iterator, "\n\n");
#endif
}

/* virtual */ void CrashLog::LogModules(std::back_insert_iterator<std::string> &output_iterator) const
{
	/* Stub implementation; not all OSes support this. */
}

/**
 * Writes OpenTTD's version to the buffer.
 * @param output_iterator Iterator to write the output to.
 */
void CrashLog::LogOpenTTDVersion(std::back_insert_iterator<std::string> &output_iterator) const
{
	fmt::format_to(output_iterator,
			"OpenTTD version:\n"
			" Version:    {} ({})\n"
			" NewGRF ver: {:08x}\n"
			" Bits:       {}\n"
			" Endian:     {}\n"
			" Dedicated:  {}\n"
			" Build date: {}\n\n",
			_openttd_revision,
			_openttd_revision_modified,
			_openttd_newgrf_version,
#ifdef POINTER_IS_64BIT
			64,
#else
			32,
#endif
#if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
			"little",
#else
			"big",
#endif
#ifdef DEDICATED
			"yes",
#else
			"no",
#endif
			_openttd_build_date
	);
}

/**
 * Writes the (important) configuration settings to the buffer.
 * E.g. graphics set, sound set, blitter and AIs.
 * @param output_iterator Iterator to write the output to.
 */
void CrashLog::LogConfiguration(std::back_insert_iterator<std::string> &output_iterator) const
{
	fmt::format_to(output_iterator,
			"Configuration:\n"
			" Blitter:      {}\n"
			" Graphics set: {} ({})\n"
			" Language:     {}\n"
			" Music driver: {}\n"
			" Music set:    {} ({})\n"
			" Network:      {}\n"
			" Sound driver: {}\n"
			" Sound set:    {} ({})\n"
			" Video driver: {}\n\n",
			BlitterFactory::GetCurrentBlitter() == nullptr ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
			BaseGraphics::GetUsedSet() == nullptr ? "none" : BaseGraphics::GetUsedSet()->name,
			BaseGraphics::GetUsedSet() == nullptr ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
			_current_language == nullptr ? "none" : _current_language->file.filename(),
			MusicDriver::GetInstance() == nullptr ? "none" : MusicDriver::GetInstance()->GetName(),
			BaseMusic::GetUsedSet() == nullptr ? "none" : BaseMusic::GetUsedSet()->name,
			BaseMusic::GetUsedSet() == nullptr ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
			_networking ? (_network_server ? "server" : "client") : "no",
			SoundDriver::GetInstance() == nullptr ? "none" : SoundDriver::GetInstance()->GetName(),
			BaseSounds::GetUsedSet() == nullptr ? "none" : BaseSounds::GetUsedSet()->name,
			BaseSounds::GetUsedSet() == nullptr ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
			VideoDriver::GetInstance() == nullptr ? "none" : VideoDriver::GetInstance()->GetInfoString()
	);

	fmt::format_to(output_iterator,
			"Fonts:\n"
			" Small:  {}\n"
			" Medium: {}\n"
			" Large:  {}\n"
			" Mono:   {}\n\n",
			FontCache::GetName(FS_SMALL),
			FontCache::GetName(FS_NORMAL),
			FontCache::GetName(FS_LARGE),
			FontCache::GetName(FS_MONO)
	);

	fmt::format_to(output_iterator, "AI Configuration (local: {}) (current: {}):\n", _local_company, _current_company);
	for (const Company *c : Company::Iterate()) {
		if (c->ai_info == nullptr) {
			fmt::format_to(output_iterator, " {:2}: Human\n", c->index);
		} else {
			fmt::format_to(output_iterator, " {:2}: {} (v{})\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
		}
	}

	if (Game::GetInfo() != nullptr) {
		fmt::format_to(output_iterator, " GS: {} (v{})\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
	}
	fmt::format_to(output_iterator, "\n");
}

/**
 * Writes information (versions) of the used libraries.
 * @param output_iterator Iterator to write the output to.
 */
void CrashLog::LogLibraries(std::back_insert_iterator<std::string> &output_iterator) const
{
	fmt::format_to(output_iterator, "Libraries:\n");

#ifdef WITH_ALLEGRO
	fmt::format_to(output_iterator, " Allegro:    {}\n", allegro_id);
#endif /* WITH_ALLEGRO */

#ifdef WITH_FONTCONFIG
	int version = FcGetVersion();
	fmt::format_to(output_iterator, " FontConfig: {}.{}.{}\n", version / 10000, (version / 100) % 100, version % 100);
#endif /* WITH_FONTCONFIG */

#ifdef WITH_FREETYPE
	FT_Library library;
	int major, minor, patch;
	FT_Init_FreeType(&library);
	FT_Library_Version(library, &major, &minor, &patch);
	FT_Done_FreeType(library);
	fmt::format_to(output_iterator, " FreeType:   {}.{}.{}\n", major, minor, patch);
#endif /* WITH_FREETYPE */

#if defined(WITH_HARFBUZZ)
	fmt::format_to(output_iterator, " HarfBuzz:   {}\n", hb_version_string());
#endif /* WITH_HARFBUZZ */

#if defined(WITH_ICU_I18N)
	/* 4 times 0-255, separated by dots (.) and a trailing '\0' */
	char buf[4 * 3 + 3 + 1];
	UVersionInfo ver;
	u_getVersion(ver);
	u_versionToString(ver, buf);
	fmt::format_to(output_iterator, " ICU i18n:   {}\n", buf);
#endif /* WITH_ICU_I18N */

#ifdef WITH_LIBLZMA
	fmt::format_to(output_iterator, " LZMA:       {}\n", lzma_version_string());
#endif

#ifdef WITH_LZO
	fmt::format_to(output_iterator, " LZO:        {}\n", lzo_version_string());
#endif

#ifdef WITH_PNG
	fmt::format_to(output_iterator, " PNG:        {}\n", png_get_libpng_ver(nullptr));
#endif /* WITH_PNG */

#ifdef WITH_SDL
	const SDL_version *sdl_v = SDL_Linked_Version();
	fmt::format_to(output_iterator, " SDL1:       {}.{}.{}\n", sdl_v->major, sdl_v->minor, sdl_v->patch);
#elif defined(WITH_SDL2)
	SDL_version sdl2_v;
	SDL_GetVersion(&sdl2_v);
	fmt::format_to(output_iterator, " SDL2:       {}.{}.{}\n", sdl2_v.major, sdl2_v.minor, sdl2_v.patch);
#endif

#ifdef WITH_ZLIB
	fmt::format_to(output_iterator, " Zlib:       {}\n", zlibVersion());
#endif

#ifdef WITH_CURL
	auto *curl_v = curl_version_info(CURLVERSION_NOW);
	fmt::format_to(output_iterator, " Curl:       {}\n", curl_v->version);
	if (curl_v->ssl_version != nullptr) {
		fmt::format_to(output_iterator, " Curl SSL:   {}\n", curl_v->ssl_version);
	} else {
		fmt::format_to(output_iterator, " Curl SSL:   none\n");
	}
#endif

	fmt::format_to(output_iterator, "\n");
}

/**
 * Writes the gamelog data to the buffer.
 * @param output_iterator Iterator to write the output to.
 */
void CrashLog::LogGamelog(std::back_insert_iterator<std::string> &output_iterator) const
{
	_gamelog.Print([&output_iterator](const std::string &s) {
		fmt::format_to(output_iterator, "{}\n", s);
	});
	fmt::format_to(output_iterator, "\n");
}

/**
 * Writes up to 32 recent news messages to the buffer, with the most recent first.
 * @param output_iterator Iterator to write the output to.
 */
void CrashLog::LogRecentNews(std::back_insert_iterator<std::string> &output_iterator) const
{
	fmt::format_to(output_iterator, "Recent news messages:\n");

	int i = 0;
	for (NewsItem *news = _latest_news; i < 32 && news != nullptr; news = news->prev, i++) {
		TimerGameCalendar::YearMonthDay ymd;
		TimerGameCalendar::ConvertDateToYMD(news->date, &ymd);
		fmt::format_to(output_iterator, "({}-{:02}-{:02}) StringID: {}, Type: {}, Ref1: {}, {}, Ref2: {}, {}\n",
		                   ymd.year, ymd.month + 1, ymd.day, news->string_id, news->type,
		                   news->reftype1, news->ref1, news->reftype2, news->ref2);
	}
	fmt::format_to(output_iterator, "\n");
}

/**
 * Create a timestamped filename.
 * @param ext           The extension for the filename.
 * @param with_dir      Whether to prepend the filename with the personal directory.
 * @return The filename
 */
std::string CrashLog::CreateFileName(const char *ext, bool with_dir) const
{
	static std::string crashname;

	if (crashname.empty()) {
		crashname = fmt::format("crash{:%Y%m%d%H%M%S}", fmt::gmtime(time(nullptr)));
	}
	return fmt::format("{}{}{}", with_dir ? _personal_dir : std::string{}, crashname, ext);
}

/**
 * Fill the crash log buffer with all data of a crash log.
 * @param output_iterator Iterator to write the output to.
 */
void CrashLog::FillCrashLog(std::back_insert_iterator<std::string> &output_iterator) const
{
	fmt::format_to(output_iterator, "*** OpenTTD Crash Report ***\n\n");
	fmt::format_to(output_iterator, "Crash at: {:%Y-%m-%d %H:%M:%S} (UTC)\n", fmt::gmtime(time(nullptr)));

	TimerGameCalendar::YearMonthDay ymd;
	TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
	fmt::format_to(output_iterator, "In game date: {}-{:02}-{:02} ({})\n\n", ymd.year, ymd.month + 1, ymd.day, TimerGameCalendar::date_fract);

	this->LogError(output_iterator, CrashLog::message);
	this->LogOpenTTDVersion(output_iterator);
	this->LogStacktrace(output_iterator);
	this->LogOSVersion(output_iterator);
	this->LogCompiler(output_iterator);
	this->LogConfiguration(output_iterator);
	this->LogLibraries(output_iterator);
	this->LogModules(output_iterator);
	this->LogGamelog(output_iterator);
	this->LogRecentNews(output_iterator);

	fmt::format_to(output_iterator, "*** End of OpenTTD Crash Report ***\n");
}

/**
 * Write the crash log to a file.
 * @note The filename will be written to \c crashlog_filename.
 * @return true when the crash log was successfully written.
 */
bool CrashLog::WriteCrashLog()
{
	this->crashlog_filename = this->CreateFileName(".log");

	FILE *file = FioFOpenFile(this->crashlog_filename, "w", NO_DIRECTORY);
	if (file == nullptr) return false;

	size_t len = this->crashlog.size();
	size_t written = fwrite(this->crashlog.data(), 1, len, file);

	FioFCloseFile(file);
	return len == written;
}

/* virtual */ int CrashLog::WriteCrashDump()
{
	/* Stub implementation; not all OSes support this. */
	return 0;
}

/**
 * Write the (crash) savegame to a file.
 * @note The filename will be written to \c savegame_filename.
 * @return true when the crash save was successfully made.
 */
bool CrashLog::WriteSavegame()
{
	/* If the map doesn't exist, saving will fail too. If the map got
	 * initialised, there is a big chance the rest is initialised too. */
	if (!Map::IsInitialized()) return false;

	try {
		_gamelog.Emergency();

		this->savegame_filename = this->CreateFileName(".sav");

		/* Don't do a threaded saveload. */
		return SaveOrLoad(this->savegame_filename, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK;
	} catch (...) {
		return false;
	}
}

/**
 * Write the (crash) screenshot to a file.
 * @note The filename will be written to \c screenshot_filename.
 * @return std::nullopt when the crash screenshot could not be made, otherwise the filename.
 */
bool CrashLog::WriteScreenshot()
{
	/* Don't draw when we have invalid screen size */
	if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == nullptr) return false;

	std::string filename = this->CreateFileName("", false);
	bool res = MakeScreenshot(SC_CRASHLOG, filename);
	if (res) this->screenshot_filename = _full_screenshot_path;
	return res;
}

void CrashLog::SendSurvey() const
{
	if (_game_mode == GM_NORMAL) {
		_survey.Transmit(NetworkSurveyHandler::Reason::CRASH, true);
	}
}

/**
 * Makes the crash log, writes it to a file and then subsequently tries
 * to make a crash dump and crash savegame. It uses DEBUG to write
 * information like paths to the console.
 * @return true when everything is made successfully.
 */
bool CrashLog::MakeCrashLog()
{
	/* Don't keep looping logging crashes. */
	static bool crashlogged = false;
	if (crashlogged) return false;
	crashlogged = true;

	crashlog.reserve(65536);
	auto output_iterator = std::back_inserter(crashlog);
	bool ret = true;

	fmt::print("Crash encountered, generating crash log...\n");
	this->FillCrashLog(output_iterator);
	fmt::print("{}\n", crashlog);
	fmt::print("Crash log generated.\n\n");

	fmt::print("Writing crash log to disk...\n");
	bool bret = this->WriteCrashLog();
	if (bret) {
		fmt::print("Crash log written to {}. Please add this file to any bug reports.\n\n", this->crashlog_filename);
	} else {
		fmt::print("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
		ret = false;
	}

	/* Don't mention writing crash dumps because not all platforms support it. */
	int dret = this->WriteCrashDump();
	if (dret < 0) {
		fmt::print("Writing crash dump failed.\n\n");
		ret = false;
	} else if (dret > 0) {
		fmt::print("Crash dump written to {}. Please add this file to any bug reports.\n\n", this->crashdump_filename);
	}

	fmt::print("Writing crash savegame...\n");
	bret = this->WriteSavegame();
	if (bret) {
		fmt::print("Crash savegame written to {}. Please add this file and the last (auto)save to any bug reports.\n\n", this->savegame_filename);
	} else {
		ret = false;
		fmt::print("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
	}

	fmt::print("Writing crash screenshot...\n");
	bret = this->WriteScreenshot();
	if (bret) {
		fmt::print("Crash screenshot written to {}. Please add this file to any bug reports.\n\n", this->screenshot_filename);
	} else {
		ret = false;
		fmt::print("Writing crash screenshot failed.\n\n");
	}

	this->SendSurvey();

	return ret;
}

/**
 * Sets a message for the error message handler.
 * @param message The error message of the error.
 */
/* static */ void CrashLog::SetErrorMessage(const std::string &message)
{
	CrashLog::message = message;
}

/**
 * Try to close the sound/video stuff so it doesn't keep lingering around
 * incorrect video states or so, e.g. keeping dpmi disabled.
 */
/* static */ void CrashLog::AfterCrashLogCleanup()
{
	if (MusicDriver::GetInstance() != nullptr) MusicDriver::GetInstance()->Stop();
	if (SoundDriver::GetInstance() != nullptr) SoundDriver::GetInstance()->Stop();
	if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop();
}