1 /* $Id: crashlog.cpp 26209 2014-01-02 22:41:58Z rubidium $ */
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/>.
10 /** @file crashlog.cpp Implementation of generic function to be called to log a crash */
15 #include "date_func.h"
18 #include "strings_func.h"
19 #include "blitter/factory.hpp"
20 #include "base_media_base.h"
21 #include "music/music_driver.hpp"
22 #include "sound/sound_driver.hpp"
23 #include "video/video_driver.hpp"
24 #include "saveload/saveload.h"
25 #include "screenshot.h"
27 #include "network/network.h"
29 #include "fontcache.h"
31 #include "ai/ai_info.hpp"
32 #include "game/game.hpp"
33 #include "game/game_info.hpp"
34 #include "command_func.h"
35 #include "company_base.h"
36 #include "company_func.h"
42 #endif /* WITH_ALLEGRO */
43 #ifdef WITH_FONTCONFIG
44 # include <fontconfig/fontconfig.h>
45 #endif /* WITH_FONTCONFIG */
47 /* pngconf.h, included by png.h doesn't like something in the
48 * freetype headers. As such it's not alphabetically sorted. */
52 # include <ft2build.h>
53 # include FT_FREETYPE_H
54 #endif /* WITH_FREETYPE */
55 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
56 # include <unicode/uversion.h>
57 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
62 #include <lzo/lzo1x.h>
72 #include "safeguards.h"
74 /* static */ const char *CrashLog::message
= NULL
;
75 /* static */ char *CrashLog::gamelog_buffer
= NULL
;
76 /* static */ const char *CrashLog::gamelog_last
= NULL
;
78 char *CrashLog::LogCompiler(char *buffer
, const char *last
) const
80 buffer
+= seprintf(buffer
, last
, " Compiler: "
83 #elif defined(__ICC) && defined(__GNUC__)
84 "ICC %d (GCC %d.%d.%d mode)", __ICC
, __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
87 #elif defined(__GNUC__)
88 "GCC %d.%d.%d", __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
89 #elif defined(__WATCOMC__)
90 "WatcomC %d", __WATCOMC__
95 #if defined(__VERSION__)
96 return buffer
+ seprintf(buffer
, last
, " \"" __VERSION__
"\"\n\n");
98 return buffer
+ seprintf(buffer
, last
, "\n\n");
102 /* virtual */ char *CrashLog::LogRegisters(char *buffer
, const char *last
) const
104 /* Stub implementation; not all OSes support this. */
108 /* virtual */ char *CrashLog::LogModules(char *buffer
, const char *last
) const
110 /* Stub implementation; not all OSes support this. */
115 * Writes OpenTTD's version to the buffer.
116 * @param buffer The begin where to write at.
117 * @param last The last position in the buffer to write to.
118 * @return the position of the \c '\0' character after the buffer.
120 char *CrashLog::LogOpenTTDVersion(char *buffer
, const char *last
) const
122 return buffer
+ seprintf(buffer
, last
,
124 " Version: %s (%d)\n"
125 " NewGRF ver: %08x\n"
129 " Build date: %s\n\n",
131 _openttd_revision_modified
,
132 _openttd_newgrf_version
,
138 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
153 * Writes the (important) configuration settings to the buffer.
154 * E.g. graphics set, sound set, blitter and AIs.
155 * @param buffer The begin where to write at.
156 * @param last The last position in the buffer to write to.
157 * @return the position of the \c '\0' character after the buffer.
159 char *CrashLog::LogConfiguration(char *buffer
, const char *last
) const
161 buffer
+= seprintf(buffer
, last
,
164 " Graphics set: %s (%u)\n"
166 " Music driver: %s\n"
167 " Music set: %s (%u)\n"
169 " Sound driver: %s\n"
170 " Sound set: %s (%u)\n"
171 " Video driver: %s\n\n",
172 BlitterFactory::GetCurrentBlitter() == NULL
? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
173 BaseGraphics::GetUsedSet() == NULL
? "none" : BaseGraphics::GetUsedSet()->name
,
174 BaseGraphics::GetUsedSet() == NULL
? UINT32_MAX
: BaseGraphics::GetUsedSet()->version
,
175 _current_language
== NULL
? "none" : _current_language
->file
,
176 MusicDriver::GetInstance() == NULL
? "none" : MusicDriver::GetInstance()->GetName(),
177 BaseMusic::GetUsedSet() == NULL
? "none" : BaseMusic::GetUsedSet()->name
,
178 BaseMusic::GetUsedSet() == NULL
? UINT32_MAX
: BaseMusic::GetUsedSet()->version
,
179 _networking
? (_network_server
? "server" : "client") : "no",
180 SoundDriver::GetInstance() == NULL
? "none" : SoundDriver::GetInstance()->GetName(),
181 BaseSounds::GetUsedSet() == NULL
? "none" : BaseSounds::GetUsedSet()->name
,
182 BaseSounds::GetUsedSet() == NULL
? UINT32_MAX
: BaseSounds::GetUsedSet()->version
,
183 VideoDriver::GetInstance() == NULL
? "none" : VideoDriver::GetInstance()->GetName()
186 buffer
+= seprintf(buffer
, last
,
192 FontCache::Get(FS_SMALL
)->GetFontName(),
193 FontCache::Get(FS_NORMAL
)->GetFontName(),
194 FontCache::Get(FS_LARGE
)->GetFontName(),
195 FontCache::Get(FS_MONO
)->GetFontName()
198 buffer
+= seprintf(buffer
, last
, "AI Configuration (local: %i):\n", (int)_local_company
);
200 FOR_ALL_COMPANIES(c
) {
201 if (c
->ai_info
== NULL
) {
202 buffer
+= seprintf(buffer
, last
, " %2i: Human\n", (int)c
->index
);
204 buffer
+= seprintf(buffer
, last
, " %2i: %s (v%d)\n", (int)c
->index
, c
->ai_info
->GetName(), c
->ai_info
->GetVersion());
208 if (Game::GetInfo() != NULL
) {
209 buffer
+= seprintf(buffer
, last
, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
211 buffer
+= seprintf(buffer
, last
, "\n");
217 * Writes information (versions) of the used libraries.
218 * @param buffer The begin where to write at.
219 * @param last The last position in the buffer to write to.
220 * @return the position of the \c '\0' character after the buffer.
222 char *CrashLog::LogLibraries(char *buffer
, const char *last
) const
224 buffer
+= seprintf(buffer
, last
, "Libraries:\n");
227 buffer
+= seprintf(buffer
, last
, " Allegro: %s\n", allegro_id
);
228 #endif /* WITH_ALLEGRO */
230 #ifdef WITH_FONTCONFIG
231 int version
= FcGetVersion();
232 buffer
+= seprintf(buffer
, last
, " FontConfig: %d.%d.%d\n", version
/ 10000, (version
/ 100) % 100, version
% 100);
233 #endif /* WITH_FONTCONFIG */
237 int major
, minor
, patch
;
238 FT_Init_FreeType(&library
);
239 FT_Library_Version(library
, &major
, &minor
, &patch
);
240 FT_Done_FreeType(library
);
241 buffer
+= seprintf(buffer
, last
, " FreeType: %d.%d.%d\n", major
, minor
, patch
);
242 #endif /* WITH_FREETYPE */
244 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
245 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
246 char buf
[4 * 3 + 3 + 1];
249 u_versionToString(ver
, buf
);
251 buffer
+= seprintf(buffer
, last
, " ICU i18n: %s\n", buf
);
253 #ifdef WITH_ICU_LAYOUT
254 buffer
+= seprintf(buffer
, last
, " ICU lx: %s\n", buf
);
256 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
259 buffer
+= seprintf(buffer
, last
, " LZMA: %s\n", lzma_version_string());
263 buffer
+= seprintf(buffer
, last
, " LZO: %s\n", lzo_version_string());
267 buffer
+= seprintf(buffer
, last
, " PNG: %s\n", png_get_libpng_ver(NULL
));
268 #endif /* WITH_PNG */
271 #ifdef DYNAMICALLY_LOADED_SDL
272 if (SDL_CALL SDL_Linked_Version
!= NULL
) {
276 const SDL_version
*v
= SDL_CALL
SDL_Linked_Version();
277 buffer
+= seprintf(buffer
, last
, " SDL: %d.%d.%d\n", v
->major
, v
->minor
, v
->patch
);
279 #endif /* WITH_SDL */
282 buffer
+= seprintf(buffer
, last
, " Zlib: %s\n", zlibVersion());
285 buffer
+= seprintf(buffer
, last
, "\n");
290 * Helper function for printing the gamelog.
291 * @param s the string to print.
293 /* static */ void CrashLog::GamelogFillCrashLog(const char *s
)
295 CrashLog::gamelog_buffer
+= seprintf(CrashLog::gamelog_buffer
, CrashLog::gamelog_last
, "%s\n", s
);
299 * Writes the gamelog data to the buffer.
300 * @param buffer The begin where to write at.
301 * @param last The last position in the buffer to write to.
302 * @return the position of the \c '\0' character after the buffer.
304 char *CrashLog::LogGamelog(char *buffer
, const char *last
) const
306 CrashLog::gamelog_buffer
= buffer
;
307 CrashLog::gamelog_last
= last
;
308 GamelogPrint(&CrashLog::GamelogFillCrashLog
);
309 return CrashLog::gamelog_buffer
+ seprintf(CrashLog::gamelog_buffer
, last
, "\n");
313 * Writes the command log data to the buffer.
314 * @param buffer The begin where to write at.
315 * @param last The last position in the buffer to write to.
316 * @return the position of the \c '\0' character after the buffer.
318 char *CrashLog::LogCommandLog(char *buffer
, const char *last
) const
320 buffer
= DumpCommandLog(buffer
, last
);
321 buffer
+= seprintf(buffer
, last
, "\n");
326 * Fill the crash log buffer with all data of a crash log.
327 * @param buffer The begin where to write at.
328 * @param last The last position in the buffer to write to.
329 * @return the position of the \c '\0' character after the buffer.
331 char *CrashLog::FillCrashLog(char *buffer
, const char *last
) const
333 time_t cur_time
= time(NULL
);
334 buffer
+= seprintf(buffer
, last
, "*** OpenTTD Crash Report ***\n\n");
336 if (GamelogTestEmergency()) {
337 buffer
+= seprintf(buffer
, last
, "-=-=- As you loaded an emergency savegame no crash information would ordinarily be generated. -=-=-\n\n");
339 if (SaveloadCrashWithMissingNewGRFs()) {
340 buffer
+= seprintf(buffer
, last
, "-=-=- As you loaded a savegame for which you do not have the required NewGRFs no crash information would ordinarily be generated. -=-=-\n\n");
343 buffer
+= seprintf(buffer
, last
, "Crash at: %s", asctime(gmtime(&cur_time
)));
345 buffer
+= seprintf(buffer
, last
, "In game date: %i-%02i-%02i (%i)\n\n", _cur_date_ymd
.year
, _cur_date_ymd
.month
+ 1, _cur_date_ymd
.day
, _date_fract
);
347 buffer
= this->LogError(buffer
, last
, CrashLog::message
);
348 buffer
= this->LogOpenTTDVersion(buffer
, last
);
349 buffer
= this->LogRegisters(buffer
, last
);
350 buffer
= this->LogStacktrace(buffer
, last
);
351 buffer
= this->LogOSVersion(buffer
, last
);
352 buffer
= this->LogCompiler(buffer
, last
);
353 buffer
= this->LogConfiguration(buffer
, last
);
354 buffer
= this->LogLibraries(buffer
, last
);
355 buffer
= this->LogModules(buffer
, last
);
356 buffer
= this->LogGamelog(buffer
, last
);
357 buffer
= this->LogCommandLog(buffer
, last
);
359 buffer
+= seprintf(buffer
, last
, "*** End of OpenTTD Crash Report ***\n");
364 * Write the crash log to a file.
365 * @note On success the filename will be filled with the full path of the
366 * crash log file. Make sure filename is at least \c MAX_PATH big.
367 * @param buffer The begin of the buffer to write to the disk.
368 * @param filename Output for the filename of the written file.
369 * @param filename_last The last position in the filename buffer.
370 * @return true when the crash log was successfully written.
372 bool CrashLog::WriteCrashLog(const char *buffer
, char *filename
, const char *filename_last
) const
374 seprintf(filename
, filename_last
, "%scrash.log", _personal_dir
);
376 FILE *file
= FioFOpenFile(filename
, "w", NO_DIRECTORY
);
377 if (file
== NULL
) return false;
379 size_t len
= strlen(buffer
);
380 size_t written
= fwrite(buffer
, 1, len
, file
);
383 return len
== written
;
386 /* virtual */ int CrashLog::WriteCrashDump(char *filename
, const char *filename_last
) const
388 /* Stub implementation; not all OSes support this. */
393 * Write the (crash) savegame to a file.
394 * @note On success the filename will be filled with the full path of the
395 * crash save file. Make sure filename is at least \c MAX_PATH big.
396 * @param filename Output for the filename of the written file.
397 * @param filename_last The last position in the filename buffer.
398 * @return true when the crash save was successfully made.
400 bool CrashLog::WriteSavegame(char *filename
, const char *filename_last
) const
402 /* If the map array doesn't exist, saving will fail too. If the map got
403 * initialised, there is a big chance the rest is initialised too. */
404 if (_m
== NULL
) return false;
409 seprintf(filename
, filename_last
, "%scrash.sav", _personal_dir
);
411 /* Don't do a threaded saveload. */
412 return SaveOrLoad(filename
, SLO_SAVE
, DFT_GAME_FILE
, NO_DIRECTORY
, false) == SL_OK
;
419 * Write the (crash) screenshot to a file.
420 * @note On success the filename will be filled with the full path of the
421 * screenshot. Make sure filename is at least \c MAX_PATH big.
422 * @param filename Output for the filename of the written file.
423 * @param filename_last The last position in the filename buffer.
424 * @return true when the crash screenshot was successfully made.
426 bool CrashLog::WriteScreenshot(char *filename
, const char *filename_last
) const
428 /* Don't draw when we have invalid screen size */
429 if (_screen
.width
< 1 || _screen
.height
< 1 || _screen
.dst_ptr
== NULL
) return false;
431 bool res
= MakeScreenshot(SC_CRASHLOG
, "crash");
432 if (res
) strecpy(filename
, _full_screenshot_name
, filename_last
);
437 * Makes the crash log, writes it to a file and then subsequently tries
438 * to make a crash dump and crash savegame. It uses DEBUG to write
439 * information like paths to the console.
440 * @return true when everything is made successfully.
442 bool CrashLog::MakeCrashLog() const
444 /* Don't keep looping logging crashes. */
445 static bool crashlogged
= false;
446 if (crashlogged
) return false;
449 char filename
[MAX_PATH
];
450 char buffer
[65536 * 4];
453 printf("Crash encountered, generating crash log...\n");
454 this->FillCrashLog(buffer
, lastof(buffer
));
455 printf("%s\n", buffer
);
456 printf("Crash log generated.\n\n");
458 printf("Writing crash log to disk...\n");
459 bool bret
= this->WriteCrashLog(buffer
, filename
, lastof(filename
));
461 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename
);
463 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
467 /* Don't mention writing crash dumps because not all platforms support it. */
468 int dret
= this->WriteCrashDump(filename
, lastof(filename
));
470 printf("Writing crash dump failed.\n\n");
472 } else if (dret
> 0) {
473 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename
);
476 printf("Writing crash savegame...\n");
477 bret
= this->WriteSavegame(filename
, lastof(filename
));
479 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename
);
482 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
485 printf("Writing crash screenshot...\n");
486 bret
= this->WriteScreenshot(filename
, lastof(filename
));
488 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename
);
491 printf("Writing crash screenshot failed.\n\n");
498 * Sets a message for the error message handler.
499 * @param message The error message of the error.
501 /* static */ void CrashLog::SetErrorMessage(const char *message
)
503 CrashLog::message
= message
;
507 * Try to close the sound/video stuff so it doesn't keep lingering around
508 * incorrect video states or so, e.g. keeping dpmi disabled.
510 /* static */ void CrashLog::AfterCrashLogCleanup()
512 if (MusicDriver::GetInstance() != NULL
) MusicDriver::GetInstance()->Stop();
513 if (SoundDriver::GetInstance() != NULL
) SoundDriver::GetInstance()->Stop();
514 if (VideoDriver::GetInstance() != NULL
) VideoDriver::GetInstance()->Stop();
517 /* static */ const char *CrashLog::GetAbortCrashlogReason()
519 if (_settings_client
.gui
.developer
> 0) return NULL
;
521 if (GamelogTestEmergency()) {
522 return "As you loaded an emergency savegame no crash information will be generated.\n";
525 if (SaveloadCrashWithMissingNewGRFs()) {
526 return "As you loaded an savegame for which you do not have the required NewGRFs\n" \
527 "no crash information will be generated.\n";