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 26538 2014-04-28 21:06:51Z rubidium $ */
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 #include "safeguards.h"
40
41 /* static */ const char *CrashLog::message = NULL;
42 /* static */ char *CrashLog::gamelog_buffer = NULL;
43 /* static */ const char *CrashLog::gamelog_last = NULL;
44
45 char *CrashLog::LogCompiler(char *buffer, const char *last) const
46 {
47 buffer += seprintf(buffer, last, " Compiler: "
48 #if defined(_MSC_VER)
49 "MSVC %d", _MSC_VER
50 #elif defined(__ICC) && defined(__GNUC__)
51 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
52 #elif defined(__ICC)
53 "ICC %d", __ICC
54 #elif defined(__GNUC__)
55 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
56 #elif defined(__WATCOMC__)
57 "WatcomC %d", __WATCOMC__
58 #else
59 "<unknown>"
60 #endif
61 );
62 #if defined(__VERSION__)
63 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
64 #else
65 return buffer + seprintf(buffer, last, "\n\n");
66 #endif
67 }
68
69 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
70 {
71 /* Stub implementation; not all OSes support this. */
72 return buffer;
73 }
74
75 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
76 {
77 /* Stub implementation; not all OSes support this. */
78 return buffer;
79 }
80
87 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
88 {
89 return buffer + seprintf(buffer, last,
90 "OpenTTD version:\n"
91 " Version: %s (%d)\n"
92 " NewGRF ver: %08x\n"
93 " Bits: %d\n"
94 " Endian: %s\n"
95 " Dedicated: %s\n"
96 " Build date: %s\n\n",
97 _openttd_revision,
98 _openttd_revision_modified,
99 _openttd_newgrf_version,
100 #ifdef _SQ64
101 64,
102 #else
103 32,
104 #endif
105 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
106 "little",
107 #else
108 "big",
109 #endif
110 #ifdef DEDICATED
111 "yes",
112 #else
113 "no",
114 #endif
115 _openttd_build_date
116 );
117 }
118
126 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
127 {
128 buffer += seprintf(buffer, last,
129 "Configuration:\n"
130 " Blitter: %s\n"
131 " Graphics set: %s (%u)\n"
132 " Language: %s\n"
133 " Music driver: %s\n"
134 " Music set: %s (%u)\n"
135 " Network: %s\n"
136 " Sound driver: %s\n"
137 " Sound set: %s (%u)\n"
138 " Video driver: %s\n\n",
139 BlitterFactory::GetCurrentBlitter() == NULL ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
140 BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
141 BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
142 _current_language == NULL ? "none" : _current_language->file,
143 MusicDriver::GetInstance() == NULL ? "none" : MusicDriver::GetInstance()->GetName(),
144 BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
145 BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
146 _networking ? (_network_server ? "server" : "client") : "no",
147 SoundDriver::GetInstance() == NULL ? "none" : SoundDriver::GetInstance()->GetName(),
148 BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
149 BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
150 VideoDriver::GetInstance() == NULL ? "none" : VideoDriver::GetInstance()->GetName()
151 );
152
153 buffer += seprintf(buffer, last,
154 "Fonts:\n"
155 " Small: %s\n"
156 " Medium: %s\n"
157 " Large: %s\n"
158 " Mono: %s\n\n",
159 FontCache::Get(FS_SMALL)->GetFontName(),
160 FontCache::Get(FS_NORMAL)->GetFontName(),
161 FontCache::Get(FS_LARGE)->GetFontName(),
162 FontCache::Get(FS_MONO)->GetFontName()
163 );
164
165 buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
166 const Company *c;
167 FOR_ALL_COMPANIES(c) {
168 if (c->ai_info == NULL) {
169 buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
170 } else {
171 buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
172 }
173 }
174
175 if (Game::GetInfo() != NULL) {
176 buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
177 }
178 buffer += seprintf(buffer, last, "\n");
179
180 return buffer;
181 }
182
183 /* Include these here so it's close to where it's actually used. */
184 #ifdef WITH_ALLEGRO
185 # include <allegro.h>
186 #endif /* WITH_ALLEGRO */
187 #ifdef WITH_FONTCONFIG
188 # include <fontconfig/fontconfig.h>
189 #endif /* WITH_FONTCONFIG */
190 #ifdef WITH_PNG
191 /* pngconf.h, included by png.h doesn't like something in the
192 * freetype headers. As such it's not alphabetically sorted. */
193 # include <png.h>
194 #endif /* WITH_PNG */
195 #ifdef WITH_FREETYPE
196 # include <ft2build.h>
197 # include FT_FREETYPE_H
198 #endif /* WITH_FREETYPE */
199 #ifdef WITH_ICU
200 # include <unicode/uversion.h>
201 #endif /* WITH_ICU */
202 #ifdef WITH_LZMA
203 # include <lzma.h>
204 #endif
205 #ifdef WITH_LZO
206 #include <lzo/lzo1x.h>
207 #endif
208 #ifdef WITH_SDL
209 # include "sdl.h"
210 # include <SDL.h>
211 #endif /* WITH_SDL */
212 #ifdef WITH_ZLIB
213 # include <zlib.h>
214 #endif
215
222 char *CrashLog::LogLibraries(char *buffer, const char *last) const
223 {
224 buffer += seprintf(buffer, last, "Libraries:\n");
225
226 #ifdef WITH_ALLEGRO
227 buffer += seprintf(buffer, last, " Allegro: %s\n", allegro_id);
228 #endif /* WITH_ALLEGRO */
229
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 */
234
235 #ifdef WITH_FREETYPE
236 FT_Library library;
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 */
243
244 #ifdef WITH_ICU
245 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
246 char buf[4 * 3 + 3 + 1];
247 UVersionInfo ver;
248 u_getVersion(ver);
249 u_versionToString(ver, buf);
250 buffer += seprintf(buffer, last, " ICU: %s\n", buf);
251 #endif /* WITH_ICU */
252
253 #ifdef WITH_LZMA
254 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
255 #endif
256
257 #ifdef WITH_LZO
258 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
259 #endif
260
261 #ifdef WITH_PNG
262 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(NULL));
263 #endif /* WITH_PNG */
264
265 #ifdef WITH_SDL
266 #ifdef DYNAMICALLY_LOADED_SDL
267 if (SDL_CALL SDL_Linked_Version != NULL) {
268 #else
269 {
270 #endif
271 const SDL_version *v = SDL_CALL SDL_Linked_Version();
272 buffer += seprintf(buffer, last, " SDL: %d.%d.%d\n", v->major, v->minor, v->patch);
273 }
274 #endif /* WITH_SDL */
275
276 #ifdef WITH_ZLIB
277 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
278 #endif
279
280 buffer += seprintf(buffer, last, "\n");
281 return buffer;
282 }
283
288 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
289 {
290 CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
291 }
292
299 char *CrashLog::LogGamelog(char *buffer, const char *last) const
300 {
301 CrashLog::gamelog_buffer = buffer;
302 CrashLog::gamelog_last = last;
303 GamelogPrint(&CrashLog::GamelogFillCrashLog);
304 return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
305 }
306
313 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
314 {
315 time_t cur_time = time(NULL);
316 buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
317 buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
318
319 YearMonthDay ymd;
320 ConvertDateToYMD(_date, &ymd);
321 buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
322
323 buffer = this->LogError(buffer, last, CrashLog::message);
324 buffer = this->LogOpenTTDVersion(buffer, last);
325 buffer = this->LogRegisters(buffer, last);
326 buffer = this->LogStacktrace(buffer, last);
327 buffer = this->LogOSVersion(buffer, last);
328 buffer = this->LogCompiler(buffer, last);
329 buffer = this->LogConfiguration(buffer, last);
330 buffer = this->LogLibraries(buffer, last);
331 buffer = this->LogModules(buffer, last);
332 buffer = this->LogGamelog(buffer, last);
333
334 buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
335 return buffer;
336 }
337
347 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
348 {
349 seprintf(filename, filename_last, "%scrash.log", _personal_dir);
350
351 FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
352 if (file == NULL) return false;
353
354 size_t len = strlen(buffer);
355 size_t written = fwrite(buffer, 1, len, file);
356
357 FioFCloseFile(file);
358 return len == written;
359 }
360
361 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
362 {
363 /* Stub implementation; not all OSes support this. */
364 return 0;
365 }
366
375 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
376 {
377 /* If the map array doesn't exist, saving will fail too. If the map got
378 * initialised, there is a big chance the rest is initialised too. */
379 if (_m == NULL) return false;
380
381 try {
382 GamelogEmergency();
383
384 seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
385
386 /* Don't do a threaded saveload. */
387 return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
388 } catch (...) {
389 return false;
390 }
391 }
392
401 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
402 {
403 /* Don't draw when we have invalid screen size */
404 if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
405
406 bool res = MakeScreenshot(SC_CRASHLOG, "crash");
407 if (res) strecpy(filename, _full_screenshot_name, filename_last);
408 return res;
409 }
410
417 bool CrashLog::MakeCrashLog() const
418 {
419 /* Don't keep looping logging crashes. */
420 static bool crashlogged = false;
421 if (crashlogged) return false;
422 crashlogged = true;
423
424 char filename[MAX_PATH];
425 char buffer[65536];
426 bool ret = true;
427
428 printf("Crash encountered, generating crash log...\n");
429 this->FillCrashLog(buffer, lastof(buffer));
430 printf("%s\n", buffer);
431 printf("Crash log generated.\n\n");
432
433 printf("Writing crash log to disk...\n");
434 bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
435 if (bret) {
436 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
437 } else {
438 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
439 ret = false;
440 }
441
442 /* Don't mention writing crash dumps because not all platforms support it. */
443 int dret = this->WriteCrashDump(filename, lastof(filename));
444 if (dret < 0) {
445 printf("Writing crash dump failed.\n\n");
446 ret = false;
447 } else if (dret > 0) {
448 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
449 }
450
451 printf("Writing crash savegame...\n");
452 bret = this->WriteSavegame(filename, lastof(filename));
453 if (bret) {
454 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
455 } else {
456 ret = false;
457 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
458 }
459
460 printf("Writing crash screenshot...\n");
461 bret = this->WriteScreenshot(filename, lastof(filename));
462 if (bret) {
463 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
464 } else {
465 ret = false;
466 printf("Writing crash screenshot failed.\n\n");
467 }
468
469 return ret;
470 }
471
476 /* static */ void CrashLog::SetErrorMessage(const char *message)
477 {
478 CrashLog::message = message;
479 }
480
485 /* static */ void CrashLog::AfterCrashLogCleanup()
486 {
487 if (MusicDriver::GetInstance() != NULL) MusicDriver::GetInstance()->Stop();
488 if (SoundDriver::GetInstance() != NULL) SoundDriver::GetInstance()->Stop();
489 if (VideoDriver::GetInstance() != NULL) VideoDriver::GetInstance()->Stop();
490 }