OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

crashlog.cpp
Go to the documentation of this file.
1 /* $Id: crashlog.cpp 27653 2016-09-04 16:06:50Z alberth $ */
2
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 */
9
12 #include "stdafx.h"
13 #include "crashlog.h"
14 #include "gamelog.h"
15 #include "date_func.h"
16 #include "map_func.h"
17 #include "rev.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"
26 #include "gfx_func.h"
27 #include "network/network.h"
28 #include "language.h"
29 #include "fontcache.h"
30
31 #include "ai/ai_info.hpp"
32 #include "game/game.hpp"
33 #include "game/game_info.hpp"
34 #include "company_base.h"
35 #include "company_func.h"
36
37 #include <time.h>
38
39 #ifdef WITH_ALLEGRO
40 # include <allegro.h>
41 #endif /* WITH_ALLEGRO */
42 #ifdef WITH_FONTCONFIG
43 # include <fontconfig/fontconfig.h>
44 #endif /* WITH_FONTCONFIG */
45 #ifdef WITH_PNG
46 /* pngconf.h, included by png.h doesn't like something in the
47 * freetype headers. As such it's not alphabetically sorted. */
48 # include <png.h>
49 #endif /* WITH_PNG */
50 #ifdef WITH_FREETYPE
51 # include <ft2build.h>
52 # include FT_FREETYPE_H
53 #endif /* WITH_FREETYPE */
54 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
55 # include <unicode/uversion.h>
56 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
57 #ifdef WITH_LZMA
58 # include <lzma.h>
59 #endif
60 #ifdef WITH_LZO
61 #include <lzo/lzo1x.h>
62 #endif
63 #ifdef WITH_SDL
64 # include "sdl.h"
65 # include <SDL.h>
66 #endif /* WITH_SDL */
67 #ifdef WITH_ZLIB
68 # include <zlib.h>
69 #endif
70
71 #include "safeguards.h"
72
73 /* static */ const char *CrashLog::message = NULL;
74 /* static */ char *CrashLog::gamelog_buffer = NULL;
75 /* static */ const char *CrashLog::gamelog_last = NULL;
76
77 char *CrashLog::LogCompiler(char *buffer, const char *last) const
78 {
79 buffer += seprintf(buffer, last, " Compiler: "
80 #if defined(_MSC_VER)
81 "MSVC %d", _MSC_VER
82 #elif defined(__ICC) && defined(__GNUC__)
83 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
84 #elif defined(__ICC)
85 "ICC %d", __ICC
86 #elif defined(__GNUC__)
87 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
88 #elif defined(__WATCOMC__)
89 "WatcomC %d", __WATCOMC__
90 #else
91 "<unknown>"
92 #endif
93 );
94 #if defined(__VERSION__)
95 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
96 #else
97 return buffer + seprintf(buffer, last, "\n\n");
98 #endif
99 }
100
101 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
102 {
103 /* Stub implementation; not all OSes support this. */
104 return buffer;
105 }
106
107 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
108 {
109 /* Stub implementation; not all OSes support this. */
110 return buffer;
111 }
112
119 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
120 {
121 return buffer + seprintf(buffer, last,
122 "OpenTTD version:\n"
123 " Version: %s (%d)\n"
124 " NewGRF ver: %08x\n"
125 " Bits: %d\n"
126 " Endian: %s\n"
127 " Dedicated: %s\n"
128 " Build date: %s\n\n",
129 _openttd_revision,
130 _openttd_revision_modified,
131 _openttd_newgrf_version,
132 #ifdef _SQ64
133 64,
134 #else
135 32,
136 #endif
137 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
138 "little",
139 #else
140 "big",
141 #endif
142 #ifdef DEDICATED
143 "yes",
144 #else
145 "no",
146 #endif
147 _openttd_build_date
148 );
149 }
150
158 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
159 {
160 buffer += seprintf(buffer, last,
161 "Configuration:\n"
162 " Blitter: %s\n"
163 " Graphics set: %s (%u)\n"
164 " Language: %s\n"
165 " Music driver: %s\n"
166 " Music set: %s (%u)\n"
167 " Network: %s\n"
168 " Sound driver: %s\n"
169 " Sound set: %s (%u)\n"
170 " Video driver: %s\n\n",
171 BlitterFactory::GetCurrentBlitter() == NULL ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
172 BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
173 BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
174 _current_language == NULL ? "none" : _current_language->file,
175 MusicDriver::GetInstance() == NULL ? "none" : MusicDriver::GetInstance()->GetName(),
176 BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
177 BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
178 _networking ? (_network_server ? "server" : "client") : "no",
179 SoundDriver::GetInstance() == NULL ? "none" : SoundDriver::GetInstance()->GetName(),
180 BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
181 BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
182 VideoDriver::GetInstance() == NULL ? "none" : VideoDriver::GetInstance()->GetName()
183 );
184
185 buffer += seprintf(buffer, last,
186 "Fonts:\n"
187 " Small: %s\n"
188 " Medium: %s\n"
189 " Large: %s\n"
190 " Mono: %s\n\n",
191 FontCache::Get(FS_SMALL)->GetFontName(),
192 FontCache::Get(FS_NORMAL)->GetFontName(),
193 FontCache::Get(FS_LARGE)->GetFontName(),
194 FontCache::Get(FS_MONO)->GetFontName()
195 );
196
197 buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
198 const Company *c;
199 FOR_ALL_COMPANIES(c) {
200 if (c->ai_info == NULL) {
201 buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
202 } else {
203 buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
204 }
205 }
206
207 if (Game::GetInfo() != NULL) {
208 buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
209 }
210 buffer += seprintf(buffer, last, "\n");
211
212 return buffer;
213 }
214
221 char *CrashLog::LogLibraries(char *buffer, const char *last) const
222 {
223 buffer += seprintf(buffer, last, "Libraries:\n");
224
225 #ifdef WITH_ALLEGRO
226 buffer += seprintf(buffer, last, " Allegro: %s\n", allegro_id);
227 #endif /* WITH_ALLEGRO */
228
229 #ifdef WITH_FONTCONFIG
230 int version = FcGetVersion();
231 buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
232 #endif /* WITH_FONTCONFIG */
233
234 #ifdef WITH_FREETYPE
235 FT_Library library;
236 int major, minor, patch;
237 FT_Init_FreeType(&library);
238 FT_Library_Version(library, &major, &minor, &patch);
239 FT_Done_FreeType(library);
240 buffer += seprintf(buffer, last, " FreeType: %d.%d.%d\n", major, minor, patch);
241 #endif /* WITH_FREETYPE */
242
243 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
244 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
245 char buf[4 * 3 + 3 + 1];
246 UVersionInfo ver;
247 u_getVersion(ver);
248 u_versionToString(ver, buf);
249 #ifdef WITH_ICU_SORT
250 buffer += seprintf(buffer, last, " ICU i18n: %s\n", buf);
251 #endif
252 #ifdef WITH_ICU_LAYOUT
253 buffer += seprintf(buffer, last, " ICU lx: %s\n", buf);
254 #endif
255 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
256
257 #ifdef WITH_LZMA
258 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
259 #endif
260
261 #ifdef WITH_LZO
262 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
263 #endif
264
265 #ifdef WITH_PNG
266 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(NULL));
267 #endif /* WITH_PNG */
268
269 #ifdef WITH_SDL
270 #ifdef DYNAMICALLY_LOADED_SDL
271 if (SDL_CALL SDL_Linked_Version != NULL) {
272 #else
273 {
274 #endif
275 const SDL_version *v = SDL_CALL SDL_Linked_Version();
276 buffer += seprintf(buffer, last, " SDL: %d.%d.%d\n", v->major, v->minor, v->patch);
277 }
278 #endif /* WITH_SDL */
279
280 #ifdef WITH_ZLIB
281 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
282 #endif
283
284 buffer += seprintf(buffer, last, "\n");
285 return buffer;
286 }
287
292 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
293 {
294 CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
295 }
296
303 char *CrashLog::LogGamelog(char *buffer, const char *last) const
304 {
305 CrashLog::gamelog_buffer = buffer;
306 CrashLog::gamelog_last = last;
307 GamelogPrint(&CrashLog::GamelogFillCrashLog);
308 return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
309 }
310
317 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
318 {
319 time_t cur_time = time(NULL);
320 buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
321 buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
322
323 YearMonthDay ymd;
324 ConvertDateToYMD(_date, &ymd);
325 buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
326
327 buffer = this->LogError(buffer, last, CrashLog::message);
328 buffer = this->LogOpenTTDVersion(buffer, last);
329 buffer = this->LogRegisters(buffer, last);
330 buffer = this->LogStacktrace(buffer, last);
331 buffer = this->LogOSVersion(buffer, last);
332 buffer = this->LogCompiler(buffer, last);
333 buffer = this->LogConfiguration(buffer, last);
334 buffer = this->LogLibraries(buffer, last);
335 buffer = this->LogModules(buffer, last);
336 buffer = this->LogGamelog(buffer, last);
337
338 buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
339 return buffer;
340 }
341
351 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
352 {
353 seprintf(filename, filename_last, "%scrash.log", _personal_dir);
354
355 FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
356 if (file == NULL) return false;
357
358 size_t len = strlen(buffer);
359 size_t written = fwrite(buffer, 1, len, file);
360
361 FioFCloseFile(file);
362 return len == written;
363 }
364
365 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
366 {
367 /* Stub implementation; not all OSes support this. */
368 return 0;
369 }
370
379 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
380 {
381 /* If the map array doesn't exist, saving will fail too. If the map got
382 * initialised, there is a big chance the rest is initialised too. */
383 if (_m == NULL) return false;
384
385 try {
386 GamelogEmergency();
387
388 seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
389
390 /* Don't do a threaded saveload. */
391 return SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK;
392 } catch (...) {
393 return false;
394 }
395 }
396
405 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
406 {
407 /* Don't draw when we have invalid screen size */
408 if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
409
410 bool res = MakeScreenshot(SC_CRASHLOG, "crash");
411 if (res) strecpy(filename, _full_screenshot_name, filename_last);
412 return res;
413 }
414
421 bool CrashLog::MakeCrashLog() const
422 {
423 /* Don't keep looping logging crashes. */
424 static bool crashlogged = false;
425 if (crashlogged) return false;
426 crashlogged = true;
427
428 char filename[MAX_PATH];
429 char buffer[65536];
430 bool ret = true;
431
432 printf("Crash encountered, generating crash log...\n");
433 this->FillCrashLog(buffer, lastof(buffer));
434 printf("%s\n", buffer);
435 printf("Crash log generated.\n\n");
436
437 printf("Writing crash log to disk...\n");
438 bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
439 if (bret) {
440 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
441 } else {
442 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
443 ret = false;
444 }
445
446 /* Don't mention writing crash dumps because not all platforms support it. */
447 int dret = this->WriteCrashDump(filename, lastof(filename));
448 if (dret < 0) {
449 printf("Writing crash dump failed.\n\n");
450 ret = false;
451 } else if (dret > 0) {
452 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
453 }
454
455 printf("Writing crash savegame...\n");
456 bret = this->WriteSavegame(filename, lastof(filename));
457 if (bret) {
458 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
459 } else {
460 ret = false;
461 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
462 }
463
464 printf("Writing crash screenshot...\n");
465 bret = this->WriteScreenshot(filename, lastof(filename));
466 if (bret) {
467 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
468 } else {
469 ret = false;
470 printf("Writing crash screenshot failed.\n\n");
471 }
472
473 return ret;
474 }
475
480 /* static */ void CrashLog::SetErrorMessage(const char *message)
481 {
482 CrashLog::message = message;
483 }
484
489 /* static */ void CrashLog::AfterCrashLogCleanup()
490 {
491 if (MusicDriver::GetInstance() != NULL) MusicDriver::GetInstance()->Stop();
492 if (SoundDriver::GetInstance() != NULL) SoundDriver::GetInstance()->Stop();
493 if (VideoDriver::GetInstance() != NULL) VideoDriver::GetInstance()->Stop();
494 }