OpenTTD
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",
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,
176  BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
177  BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
178  _networking ? (_network_server ? "server" : "client") : "no",
180  BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
181  BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
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 {
295 }
296 
303 char *CrashLog::LogGamelog(char *buffer, const char *last) const
304 {
305  CrashLog::gamelog_buffer = buffer;
306  CrashLog::gamelog_last = last;
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 {
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 
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 {
483 }
484 
490 {
494 }
Functions related to OTTD&#39;s strings.
void GamelogPrint(GamelogPrintProc *proc)
Prints active gamelog.
Definition: gamelog.cpp:164
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:74
Definition of stuff that is very close to a company, like the company struct itself.
char _full_screenshot_name[MAX_PATH]
Pathname of the screenshot file.
Definition: screenshot.cpp:40
bool _networking
are we in networking mode?
Definition: network.cpp:56
Base of all video drivers.
void GamelogEmergency()
Logs a emergency savegame.
Definition: gamelog.cpp:380
bool WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
Write the crash log to a file.
Definition: crashlog.cpp:351
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:342
Functions related to dates.
Day day
Day (1..31)
Definition: date_type.h:106
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
static const char * gamelog_last
Temporary &#39;local&#39; location of the end of the buffer.
Definition: crashlog.h:27
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:59
Index of the monospaced font in the font tables.
Definition: gfx_type.h:207
Functions to be called to log possibly unsafe game events.
virtual char * LogError(char *buffer, const char *last, const char *message) const =0
Writes actually encountered error to the buffer.
Generic functions for replacing base data (graphics, sounds).
Tile * _m
Tiles of the map.
Definition: map.cpp:32
const LanguageMetadata * _current_language
The currently loaded language.
Definition: strings.cpp:50
Base for all sound drivers.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
Functions to make screenshots.
Functions related to maps.
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:133
File is being saved.
Definition: fileio_type.h:52
virtual int WriteCrashDump(char *filename, const char *filename_last) const
Write the (crash) dump to a file.
Definition: crashlog.cpp:365
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
Save game or scenario file.
Definition: fileio_type.h:33
Functions to read fonts from files and cache them.
Functions/types related to saving and loading games.
char * LogGamelog(char *buffer, const char *last) const
Writes the gamelog data to the buffer.
Definition: crashlog.cpp:303
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:29
virtual void Stop()=0
Stop this driver.
char * LogOpenTTDVersion(char *buffer, const char *last) const
Writes OpenTTD&#39;s version to the buffer.
Definition: crashlog.cpp:119
static const char * message
Pointer to the error message.
Definition: crashlog.h:21
#define TTD_LITTLE_ENDIAN
Little endian builds use this for TTD_ENDIAN.
Definition: endian_type.hpp:24
const char * name
The name of the base set.
Functions related to the gfx engine.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:474
A path without any base directory.
Definition: fileio_type.h:127
Base for all music playback.
Definition of base types and functions in a cross-platform compatible way.
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:103
A number of safeguards to prevent using unsafe methods.
static void AfterCrashLogCleanup()
Try to close the sound/video stuff so it doesn&#39;t keep lingering around incorrect video states or so...
Definition: crashlog.cpp:489
bool MakeCrashLog() const
Makes the crash log, writes it to a file and then subsequently tries to make a crash dump and crash s...
Definition: crashlog.cpp:421
Information about languages and their files.
static char * gamelog_buffer
Temporary &#39;local&#39; location of the buffer.
Definition: crashlog.h:24
const char * _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition: fileio.cpp:1163
Year year
Year (0...)
Definition: date_type.h:104
bool WriteScreenshot(char *filename, const char *filename_last) const
Write the (crash) screenshot to a file.
Definition: crashlog.cpp:405
static MusicDriver * GetInstance()
Get the currently active instance of the music driver.
Basic functions/variables used all over the place.
SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
Main Save or Load function where the high-level saveload functions are handled.
Definition: saveload.cpp:2790
virtual char * LogCompiler(char *buffer, const char *last) const
Writes compiler (and its version, if available) to the buffer.
Definition: crashlog.cpp:77
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:147
uint32 version
The version of this base set.
virtual const char * GetName() const =0
Get the name of this driver.
static class GameInfo * GetInfo()
Get the current GameInfo.
Definition: game.hpp:82
Month month
Month (0..11)
Definition: date_type.h:105
char file[MAX_PATH]
Name of the file we read this data from.
Definition: language.h:95
bool WriteSavegame(char *filename, const char *filename_last) const
Write the (crash) savegame to a file.
Definition: crashlog.cpp:379
Functions to be called to log a crash.
bool MakeScreenshot(ScreenshotType t, const char *name)
Make an actual screenshot.
Definition: screenshot.cpp:819
static SoundDriver * GetInstance()
Get the currently active instance of the sound driver.
GameInfo keeps track of all information of an Game, like Author, Description, ... ...
virtual char * LogStacktrace(char *buffer, const char *last) const =0
Writes the stack trace to the buffer, if there is information about it available. ...
Functions related to companies.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
virtual char * LogOSVersion(char *buffer, const char *last) const =0
Writes OS&#39; version to the buffer.
SDL support.
char * LogLibraries(char *buffer, const char *last) const
Writes information (versions) of the used libraries.
Definition: crashlog.cpp:221
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
Index of the normal font in the font tables.
Definition: gfx_type.h:204
Base functions for all Games.
bool _network_server
network-server is active
Definition: network.cpp:57
Index of the small font in the font tables.
Definition: gfx_type.h:205
Index of the large font in the font tables.
Definition: gfx_type.h:206
declaration of OTTD revision dependent variables
char * LogConfiguration(char *buffer, const char *last) const
Writes the (important) configuration settings to the buffer.
Definition: crashlog.cpp:158
completed successfully
Definition: saveload.h:20
virtual char * LogRegisters(char *buffer, const char *last) const
Writes information about the data in the registers, if there is information about it available...
Definition: crashlog.cpp:101
static void GamelogFillCrashLog(const char *s)
Helper function for printing the gamelog.
Definition: crashlog.cpp:292
AIInfo keeps track of all information of an AI, like Author, Description, ...
char * FillCrashLog(char *buffer, const char *last) const
Fill the crash log buffer with all data of a crash log.
Definition: crashlog.cpp:317
virtual char * LogModules(char *buffer, const char *last) const
Writes the dynamically linked libraries/modules to the buffer, if there is information about it avail...
Definition: crashlog.cpp:107
Date _date
Current date in days (day counter)
Definition: date.cpp:28
Raw screenshot from blitter buffer.
Definition: screenshot.h:22
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:94
static void SetErrorMessage(const char *message)
Sets a message for the error message handler.
Definition: crashlog.cpp:480
Factory to &#39;query&#39; all available blitters.
static const GraphicsSet * GetUsedSet()
Return the used set.