Handle invalid strings from game scripts more leniently
[openttd/fttd.git] / src / crashlog.cpp
blob65afa821aed53eab09456d3ec33bcb989b5bedb0
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file crashlog.cpp Implementation of generic function to be called to log a crash */
12 #include "stdafx.h"
13 #include "crashlog.h"
14 #include "gamelog.h"
15 #include "date_func.h"
16 #include "rev.h"
17 #include "strings_func.h"
18 #include "blitter/factory.hpp"
19 #include "base_media_base.h"
20 #include "music/music_driver.hpp"
21 #include "sound/sound_driver.hpp"
22 #include "video/video_driver.hpp"
23 #include "saveload/saveload.h"
24 #include "screenshot.h"
25 #include "gfx_func.h"
26 #include "network/network.h"
27 #include "language.h"
29 #include "ai/ai_info.hpp"
30 #include "game/game.hpp"
31 #include "game/game_info.hpp"
32 #include "company_base.h"
33 #include "company_func.h"
35 #include <time.h>
37 /* static */ const char *CrashLog::message = NULL;
38 /* static */ char *CrashLog::gamelog_buffer = NULL;
39 /* static */ const char *CrashLog::gamelog_last = NULL;
41 char *CrashLog::LogCompiler(char *buffer, const char *last) const
43 buffer += seprintf(buffer, last, " Compiler: "
44 #if defined(_MSC_VER)
45 "MSVC %d", _MSC_VER
46 #elif defined(__ICC) && defined(__GNUC__)
47 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
48 #elif defined(__ICC)
49 "ICC %d", __ICC
50 #elif defined(__GNUC__)
51 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
52 #elif defined(__WATCOMC__)
53 "WatcomC %d", __WATCOMC__
54 #else
55 "<unknown>"
56 #endif
58 #if defined(__VERSION__)
59 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
60 #else
61 return buffer + seprintf(buffer, last, "\n\n");
62 #endif
65 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
67 /* Stub implementation; not all OSes support this. */
68 return buffer;
71 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
73 /* Stub implementation; not all OSes support this. */
74 return buffer;
77 /**
78 * Writes OpenTTD's version to the buffer.
79 * @param buffer The begin where to write at.
80 * @param last The last position in the buffer to write to.
81 * @return the position of the \c '\0' character after the buffer.
83 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
85 return buffer + seprintf(buffer, last,
86 "OpenTTD version:\n"
87 " Version: %s (%d)\n"
88 " NewGRF ver: %08x\n"
89 " Bits: %d\n"
90 " Endian: %s\n"
91 " Dedicated: %s\n"
92 " Build date: %s\n\n",
93 _openttd_revision,
94 _openttd_revision_modified,
95 _openttd_newgrf_version,
96 #ifdef _SQ64
97 64,
98 #else
99 32,
100 #endif
101 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
102 "little",
103 #else
104 "big",
105 #endif
106 #ifdef DEDICATED
107 "yes",
108 #else
109 "no",
110 #endif
111 _openttd_build_date
116 * Writes the (important) configuration settings to the buffer.
117 * E.g. graphics set, sound set, blitter and AIs.
118 * @param buffer The begin where to write at.
119 * @param last The last position in the buffer to write to.
120 * @return the position of the \c '\0' character after the buffer.
122 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
124 buffer += seprintf(buffer, last,
125 "Configuration:\n"
126 " Blitter: %s\n"
127 " Graphics set: %s (%u)\n"
128 " Language: %s\n"
129 " Music driver: %s\n"
130 " Music set: %s (%u)\n"
131 " Network: %s\n"
132 " Sound driver: %s\n"
133 " Sound set: %s (%u)\n"
134 " Video driver: %s\n\n",
135 BlitterFactoryBase::GetCurrentBlitter() == NULL ? "none" : BlitterFactoryBase::GetCurrentBlitter()->GetName(),
136 BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
137 BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
138 _current_language == NULL ? "none" : _current_language->file,
139 _music_driver == NULL ? "none" : _music_driver->GetName(),
140 BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
141 BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
142 _networking ? (_network_server ? "server" : "client") : "no",
143 _sound_driver == NULL ? "none" : _sound_driver->GetName(),
144 BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
145 BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
146 _video_driver == NULL ? "none" : _video_driver->GetName()
149 buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
150 const Company *c;
151 FOR_ALL_COMPANIES(c) {
152 if (c->ai_info == NULL) {
153 buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
154 } else {
155 buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
159 if (Game::GetInfo() != NULL) {
160 buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
162 buffer += seprintf(buffer, last, "\n");
164 return buffer;
167 /* Include these here so it's close to where it's actually used. */
168 #ifdef WITH_ALLEGRO
169 # include <allegro.h>
170 #endif /* WITH_ALLEGRO */
171 #ifdef WITH_FONTCONFIG
172 # include <fontconfig/fontconfig.h>
173 #endif /* WITH_FONTCONFIG */
174 #ifdef WITH_PNG
175 /* pngconf.h, included by png.h doesn't like something in the
176 * freetype headers. As such it's not alphabetically sorted. */
177 # include <png.h>
178 #endif /* WITH_PNG */
179 #ifdef WITH_FREETYPE
180 # include <ft2build.h>
181 # include FT_FREETYPE_H
182 #endif /* WITH_FREETYPE */
183 #ifdef WITH_ICU
184 # include <unicode/uversion.h>
185 #endif /* WITH_ICU */
186 #ifdef WITH_LZMA
187 # include <lzma.h>
188 #endif
189 #ifdef WITH_LZO
190 #include <lzo/lzo1x.h>
191 #endif
192 #ifdef WITH_SDL
193 # include "sdl.h"
194 # include <SDL.h>
195 #endif /* WITH_SDL */
196 #ifdef WITH_ZLIB
197 # include <zlib.h>
198 #endif
201 * Writes information (versions) of the used libraries.
202 * @param buffer The begin where to write at.
203 * @param last The last position in the buffer to write to.
204 * @return the position of the \c '\0' character after the buffer.
206 char *CrashLog::LogLibraries(char *buffer, const char *last) const
208 buffer += seprintf(buffer, last, "Libraries:\n");
210 #ifdef WITH_ALLEGRO
211 buffer += seprintf(buffer, last, " Allegro: %s\n", allegro_id);
212 #endif /* WITH_ALLEGRO */
214 #ifdef WITH_FONTCONFIG
215 int version = FcGetVersion();
216 buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
217 #endif /* WITH_FONTCONFIG */
219 #ifdef WITH_FREETYPE
220 FT_Library library;
221 int major, minor, patch;
222 FT_Init_FreeType(&library);
223 FT_Library_Version(library, &major, &minor, &patch);
224 FT_Done_FreeType(library);
225 buffer += seprintf(buffer, last, " FreeType: %d.%d.%d\n", major, minor, patch);
226 #endif /* WITH_FREETYPE */
228 #ifdef WITH_ICU
229 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
230 char buf[4 * 3 + 3 + 1];
231 UVersionInfo ver;
232 u_getVersion(ver);
233 u_versionToString(ver, buf);
234 buffer += seprintf(buffer, last, " ICU: %s\n", buf);
235 #endif /* WITH_ICU */
237 #ifdef WITH_LZMA
238 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
239 #endif
241 #ifdef WITH_LZO
242 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
243 #endif
245 #ifdef WITH_PNG
246 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(NULL));
247 #endif /* WITH_PNG */
249 #ifdef WITH_SDL
250 #ifdef DYNAMICALLY_LOADED_SDL
251 if (SDL_CALL SDL_Linked_Version != NULL) {
252 #else
254 #endif
255 const SDL_version *v = SDL_CALL SDL_Linked_Version();
256 buffer += seprintf(buffer, last, " SDL: %d.%d.%d\n", v->major, v->minor, v->patch);
258 #endif /* WITH_SDL */
260 #ifdef WITH_ZLIB
261 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
262 #endif
264 buffer += seprintf(buffer, last, "\n");
265 return buffer;
269 * Helper function for printing the gamelog.
270 * @param s the string to print.
272 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
274 CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
278 * Writes the gamelog data to the buffer.
279 * @param buffer The begin where to write at.
280 * @param last The last position in the buffer to write to.
281 * @return the position of the \c '\0' character after the buffer.
283 char *CrashLog::LogGamelog(char *buffer, const char *last) const
285 CrashLog::gamelog_buffer = buffer;
286 CrashLog::gamelog_last = last;
287 GamelogPrint(&CrashLog::GamelogFillCrashLog);
288 return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
292 * Fill the crash log buffer with all data of a crash log.
293 * @param buffer The begin where to write at.
294 * @param last The last position in the buffer to write to.
295 * @return the position of the \c '\0' character after the buffer.
297 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
299 time_t cur_time = time(NULL);
300 buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
301 buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
303 YearMonthDay ymd;
304 ConvertDateToYMD(_date, &ymd);
305 buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
307 buffer = this->LogError(buffer, last, CrashLog::message);
308 buffer = this->LogOpenTTDVersion(buffer, last);
309 buffer = this->LogRegisters(buffer, last);
310 buffer = this->LogStacktrace(buffer, last);
311 buffer = this->LogOSVersion(buffer, last);
312 buffer = this->LogCompiler(buffer, last);
313 buffer = this->LogConfiguration(buffer, last);
314 buffer = this->LogLibraries(buffer, last);
315 buffer = this->LogModules(buffer, last);
316 buffer = this->LogGamelog(buffer, last);
318 buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
319 return buffer;
323 * Write the crash log to a file.
324 * @note On success the filename will be filled with the full path of the
325 * crash log file. Make sure filename is at least \c MAX_PATH big.
326 * @param buffer The begin of the buffer to write to the disk.
327 * @param filename Output for the filename of the written file.
328 * @param filename_last The last position in the filename buffer.
329 * @return true when the crash log was successfully written.
331 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
333 seprintf(filename, filename_last, "%scrash.log", _personal_dir);
335 FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
336 if (file == NULL) return false;
338 size_t len = strlen(buffer);
339 size_t written = fwrite(buffer, 1, len, file);
341 FioFCloseFile(file);
342 return len == written;
345 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
347 /* Stub implementation; not all OSes support this. */
348 return 0;
352 * Write the (crash) savegame to a file.
353 * @note On success the filename will be filled with the full path of the
354 * crash save file. Make sure filename is at least \c MAX_PATH big.
355 * @param filename Output for the filename of the written file.
356 * @param filename_last The last position in the filename buffer.
357 * @return true when the crash save was successfully made.
359 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
361 /* If the map array doesn't exist, saving will fail too. If the map got
362 * initialised, there is a big chance the rest is initialised too. */
363 if (_mth == NULL) return false;
365 try {
366 GamelogEmergency();
368 seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
370 /* Don't do a threaded saveload. */
371 return SaveGame(filename, NO_DIRECTORY, false);
372 } catch (...) {
373 return false;
378 * Write the (crash) screenshot to a file.
379 * @note On success the filename will be filled with the full path of the
380 * screenshot. Make sure filename is at least \c MAX_PATH big.
381 * @param filename Output for the filename of the written file.
382 * @param filename_last The last position in the filename buffer.
383 * @return true when the crash screenshot was successfully made.
385 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
387 /* Don't draw when we have invalid screen size */
388 if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
390 bool res = MakeScreenshot(SC_CRASHLOG, "crash");
391 if (res) strecpy(filename, _full_screenshot_name, filename_last);
392 return res;
396 * Makes the crash log, writes it to a file and then subsequently tries
397 * to make a crash dump and crash savegame. It uses DEBUG to write
398 * information like paths to the console.
399 * @return true when everything is made successfully.
401 bool CrashLog::MakeCrashLog() const
403 /* Don't keep looping logging crashes. */
404 static bool crashlogged = false;
405 if (crashlogged) return false;
406 crashlogged = true;
408 char filename[MAX_PATH];
409 char buffer[65536];
410 bool ret = true;
412 printf("Crash encountered, generating crash log...\n");
413 this->FillCrashLog(buffer, lastof(buffer));
414 printf("%s\n", buffer);
415 printf("Crash log generated.\n\n");
417 printf("Writing crash log to disk...\n");
418 bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
419 if (bret) {
420 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
421 } else {
422 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
423 ret = false;
426 /* Don't mention writing crash dumps because not all platforms support it. */
427 int dret = this->WriteCrashDump(filename, lastof(filename));
428 if (dret < 0) {
429 printf("Writing crash dump failed.\n\n");
430 ret = false;
431 } else if (dret > 0) {
432 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
435 printf("Writing crash savegame...\n");
436 bret = this->WriteSavegame(filename, lastof(filename));
437 if (bret) {
438 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
439 } else {
440 ret = false;
441 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
444 printf("Writing crash screenshot...\n");
445 bret = this->WriteScreenshot(filename, lastof(filename));
446 if (bret) {
447 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
448 } else {
449 ret = false;
450 printf("Writing crash screenshot failed.\n\n");
453 return ret;
457 * Sets a message for the error message handler.
458 * @param message The error message of the error.
460 /* static */ void CrashLog::SetErrorMessage(const char *message)
462 CrashLog::message = message;
466 * Try to close the sound/video stuff so it doesn't keep lingering around
467 * incorrect video states or so, e.g. keeping dpmi disabled.
469 /* static */ void CrashLog::AfterCrashLogCleanup()
471 if (_music_driver != NULL) _music_driver->Stop();
472 if (_sound_driver != NULL) _sound_driver->Stop();
473 if (_video_driver != NULL) _video_driver->Stop();