# crashlog.cpp

Go to the documentation of this file.

    00001 /* $Id: crashlog.cpp 20148 2010-07-14 19:29:13Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "crashlog.h"
    00014 #include "gamelog.h"
    00015 #include "date_func.h"
    00016 #include "map_func.h"
    00017 #include "rev.h"
    00018 #include "strings_func.h"
    00019 #include "blitter/factory.hpp"
    00020 #include "base_media_base.h"
    00021 #include "music/music_driver.hpp"
    00022 #include "sound/sound_driver.hpp"
    00023 #include "video/video_driver.hpp"
    00024 #include "saveload/saveload.h"
    00025 #include "screenshot.h"
    00026 #include "gfx_func.h"
    00027 #include "network/network.h"
    00028
    00029 #include "ai/ai_info.hpp"
    00030 #include "company_base.h"
    00031 #include "company_func.h"
    00032
    00033 #include <time.h>
    00034
    00035 /* static */ const char *CrashLog::message = NULL;
    00036 /* static */ char *CrashLog::gamelog_buffer = NULL;
    00037 /* static */ const char *CrashLog::gamelog_last = NULL;
    00038
    00039 char *CrashLog::LogCompiler(char *buffer, const char *last) const
    00040 {
    00041       buffer += seprintf(buffer, last, " Compiler: "
    00042 #if defined(_MSC_VER)
    00043       "MSVC %d", _MSC_VER
    00044 #elif defined(__ICC) && defined(__GNUC__)
    00045       "ICC %d (GCC %d.%d.%d mode)", __ICC,  __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
    00046 #elif defined(__ICC)
    00047       "ICC %d", __ICC
    00048 #elif defined(__GNUC__)
    00049       "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
    00050 #elif defined(__WATCOMC__)
    00051       "WatcomC %d", __WATCOMC__
    00052 #else
    00053       "<unknown>"
    00054 #endif
    00055       );
    00056 #if defined(__VERSION__)
    00057       return buffer + seprintf(buffer, last,  " \"" __VERSION__ "\"\n\n");
    00058 #else
    00059       return buffer + seprintf(buffer, last,  "\n\n");
    00060 #endif
    00061 }
    00062
    00063 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
    00064 {
    00065   /* Stub implementation; not all OSes support this. */
    00066   return buffer;
    00067 }
    00068
    00069 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
    00070 {
    00071   /* Stub implementation; not all OSes support this. */
    00072   return buffer;
    00073 }
    00074
    00075 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
    00076 {
    00077   return buffer + seprintf(buffer, last,
    00078       "OpenTTD version:\n"
    00079       " Version:    %s (%d)\n"
    00080       " NewGRF ver: %08x\n"
    00081       " Bits:       %d\n"
    00082       " Endian:     %s\n"
    00083       " Dedicated:  %s\n"
    00084       " Build date: %s\n\n",
    00085       _openttd_revision,
    00086       _openttd_revision_modified,
    00087       _openttd_newgrf_version,
    00088 #ifdef _SQ64
    00089       64,
    00090 #else
    00091       32,
    00092 #endif
    00093 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
    00094       "little",
    00095 #else
    00096       "big",
    00097 #endif
    00098 #ifdef DEDICATED
    00099       "yes",
    00100 #else
    00101       "no",
    00102 #endif
    00103       _openttd_build_date
    00104   );
    00105 }
    00106
    00107 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
    00108 {
    00109   buffer += seprintf(buffer, last,
    00110       "Configuration:\n"
    00111       " Blitter:      %s\n"
    00112       " Graphics set: %s (%d)\n"
    00113       " Language:     %s\n"
    00114       " Music driver: %s\n"
    00115       " Music set:    %s (%d)\n"
    00116       " Network:      %s\n"
    00117       " Sound driver: %s\n"
    00118       " Sound set:    %s (%d)\n"
    00119       " Video driver: %s\n\n",
    00120       BlitterFactoryBase::GetCurrentBlitter() == NULL ? "none" : BlitterFactoryBase::GetCurrentBlitter()->GetName(),
    00121       BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
    00122       BaseGraphics::GetUsedSet() == NULL ? -1 : BaseGraphics::GetUsedSet()->version,
    00123       StrEmpty(_dynlang.curr_file) ? "none" : _dynlang.curr_file,
    00124       _music_driver == NULL ? "none" : _music_driver->GetName(),
    00125       BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
    00126       BaseMusic::GetUsedSet() == NULL ? -1 : BaseMusic::GetUsedSet()->version,
    00127       _networking ? (_network_server ? "server" : "client") : "no",
    00128       _sound_driver == NULL ? "none" : _sound_driver->GetName(),
    00129       BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
    00130       BaseSounds::GetUsedSet() == NULL ? -1 : BaseSounds::GetUsedSet()->version,
    00131       _video_driver == NULL ? "none" : _video_driver->GetName()
    00132   );
    00133
    00134   buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
    00135   const Company *c;
    00136   FOR_ALL_COMPANIES(c) {
    00137     if (c->ai_info == NULL) {
    00138       buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
    00139     } else {
    00140 #ifdef ENABLE_AI
    00141       buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
    00142 #endif /* ENABLE_AI */
    00143     }
    00144   }
    00145   buffer += seprintf(buffer, last, "\n");
    00146
    00147   return buffer;
    00148 }
    00149
    00150 /* Include these here so it's close to where it's actually used. */
    00151 #ifdef WITH_ALLEGRO
    00152 # include <allegro.h>
    00153 #endif /* WITH_ALLEGRO */
    00154 #ifdef WITH_FONTCONFIG
    00155 # include <fontconfig/fontconfig.h>
    00156 #endif /* WITH_FONTCONFIG */
    00157 #ifdef WITH_PNG
    00158   /* pngconf.h, included by png.h doesn't like something in the
    00159    * freetype headers. As such it's not alphabetically sorted. */
    00160 # include <png.h>
    00161 #endif /* WITH_PNG */
    00162 #ifdef WITH_FREETYPE
    00163 # include <ft2build.h>
    00164 # include FT_FREETYPE_H
    00165 #endif /* WITH_FREETYPE */
    00166 #ifdef WITH_ICU
    00167 # include <unicode/uversion.h>
    00168 #endif /* WITH_ICU */
    00169 #ifdef WITH_LZO
    00170 #include <lzo/lzo1x.h>
    00171 #endif
    00172 #ifdef WITH_SDL
    00173 # include "sdl.h"
    00174 # include <SDL.h>
    00175 #endif /* WITH_SDL */
    00176 #ifdef WITH_ZLIB
    00177 # include <zlib.h>
    00178 #endif
    00179
    00180 char *CrashLog::LogLibraries(char *buffer, const char *last) const
    00181 {
    00182   buffer += seprintf(buffer, last, "Libraries:\n");
    00183
    00184 #ifdef WITH_ALLEGRO
    00185   buffer += seprintf(buffer, last, " Allegro:    %s\n", allegro_id);
    00186 #endif /* WITH_ALLEGRO */
    00187
    00188 #ifdef WITH_FONTCONFIG
    00189   int version = FcGetVersion();
    00190   buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
    00191 #endif /* WITH_FONTCONFIG */
    00192
    00193 #ifdef WITH_FREETYPE
    00194   FT_Library library;
    00195   int major, minor, patch;
    00196   FT_Init_FreeType(&library);
    00197   FT_Library_Version(library, &major, &minor, &patch);
    00198   FT_Done_FreeType(library);
    00199   buffer += seprintf(buffer, last, " FreeType:   %d.%d.%d\n", major, minor, patch);
    00200 #endif /* WITH_FREETYPE */
    00201
    00202 #ifdef WITH_ICU
    00203   /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
    00204   char buf[4 * 3 + 3 + 1];
    00205   UVersionInfo ver;
    00206   u_getVersion(ver);
    00207   u_versionToString(ver, buf);
    00208   buffer += seprintf(buffer, last, " ICU:        %s\n", buf);
    00209 #endif /* WITH_ICU */
    00210
    00211 #ifdef WITH_LZO
    00212   buffer += seprintf(buffer, last, " LZO:        %s\n", lzo_version_string());
    00213 #endif
    00214
    00215 #ifdef WITH_PNG
    00216   buffer += seprintf(buffer, last, " PNG:        %s\n", png_get_libpng_ver(NULL));
    00217 #endif /* WITH_PNG */
    00218
    00219 #ifdef WITH_SDL
    00220 #ifdef DYNAMICALLY_LOADED_SDL
    00221   if (SDL_CALL SDL_Linked_Version != NULL) {
    00222 #else
    00223   {
    00224 #endif
    00225     const SDL_version *v = SDL_CALL SDL_Linked_Version();
    00226     buffer += seprintf(buffer, last, " SDL:        %d.%d.%d\n", v->major, v->minor, v->patch);
    00227   }
    00228 #endif /* WITH_SDL */
    00229
    00230 #ifdef WITH_ZLIB
    00231   buffer += seprintf(buffer, last, " Zlib:       %s\n", zlibVersion());
    00232 #endif
    00233
    00234   buffer += seprintf(buffer, last, "\n");
    00235   return buffer;
    00236 }
    00237
    00238 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
    00239 {
    00240   CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
    00241 }
    00242
    00243 char *CrashLog::LogGamelog(char *buffer, const char *last) const
    00244 {
    00245   CrashLog::gamelog_buffer = buffer;
    00246   CrashLog::gamelog_last = last;
    00247   GamelogPrint(&CrashLog::GamelogFillCrashLog);
    00248   return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
    00249 }
    00250
    00251 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
    00252 {
    00253   time_t cur_time = time(NULL);
    00254   buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
    00255   buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
    00256
    00257   YearMonthDay ymd;
    00258   ConvertDateToYMD(_date, &ymd);
    00259   buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
    00260
    00261   buffer = this->LogError(buffer, last, CrashLog::message);
    00262   buffer = this->LogOpenTTDVersion(buffer, last);
    00263   buffer = this->LogRegisters(buffer, last);
    00264   buffer = this->LogStacktrace(buffer, last);
    00265   buffer = this->LogOSVersion(buffer, last);
    00266   buffer = this->LogCompiler(buffer, last);
    00267   buffer = this->LogConfiguration(buffer, last);
    00268   buffer = this->LogLibraries(buffer, last);
    00269   buffer = this->LogModules(buffer, last);
    00270   buffer = this->LogGamelog(buffer, last);
    00271
    00272   buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
    00273   return buffer;
    00274 }
    00275
    00276 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
    00277 {
    00278   seprintf(filename, filename_last, "%scrash.log", _personal_dir);
    00279
    00280   FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
    00281   if (file == NULL) return false;
    00282
    00283   size_t len = strlen(buffer);
    00284   size_t written = fwrite(buffer, 1, len, file);
    00285
    00286   FioFCloseFile(file);
    00287   return len == written;
    00288 }
    00289
    00290 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
    00291 {
    00292   /* Stub implementation; not all OSes support this. */
    00293   return 0;
    00294 }
    00295
    00296 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
    00297 {
    00298   /* If the map array doesn't exist, saving will fail too. If the map got
    00299    * initialised, there is a big chance the rest is initialised too. */
    00300   if (_m == NULL) return false;
    00301
    00302   try {
    00303     GamelogEmergency();
    00304
    00305     seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
    00306
    00307     /* Don't do a threaded saveload. */
    00308     return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
    00309   } catch (...) {
    00310     return false;
    00311   }
    00312 }
    00313
    00314 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
    00315 {
    00316   /* Don't draw when we have invalid screen size */
    00317   if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
    00318
    00319   bool res = MakeScreenshot(SC_RAW, "crash");
    00320   if (res) strecpy(filename, _full_screenshot_name, filename_last);
    00321   return res;
    00322 }
    00323
    00324 bool CrashLog::MakeCrashLog() const
    00325 {
    00326   /* Don't keep looping logging crashes. */
    00327   static bool crashlogged = false;
    00328   if (crashlogged) return false;
    00329   crashlogged = true;
    00330
    00331   char filename[MAX_PATH];
    00332   char buffer[65536];
    00333   bool ret = true;
    00334
    00335   printf("Crash encountered, generating crash log...\n");
    00336   this->FillCrashLog(buffer, lastof(buffer));
    00337   printf("%s\n", buffer);
    00338   printf("Crash log generated.\n\n");
    00339
    00340   printf("Writing crash log to disk...\n");
    00341   bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
    00342   if (bret) {
    00343     printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
    00344   } else {
    00345     printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
    00346     ret = false;
    00347   }
    00348
    00349   /* Don't mention writing crash dumps because not all platforms support it. */
    00350   int dret = this->WriteCrashDump(filename, lastof(filename));
    00351   if (dret < 0) {
    00352     printf("Writing crash dump failed.\n\n");
    00353     ret = false;
    00354   } else if (dret > 0) {
    00355     printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
    00356   }
    00357
    00358   printf("Writing crash savegame...\n");
    00359   bret = this->WriteSavegame(filename, lastof(filename));
    00360   if (bret) {
    00361     printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
    00362   } else {
    00363     ret = false;
    00364     printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
    00365   }
    00366
    00367   printf("Writing crash screenshot...\n");
    00368   bret = this->WriteScreenshot(filename, lastof(filename));
    00369   if (bret) {
    00370     printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
    00371   } else {
    00372     ret = false;
    00373     printf("Writing crash screenshot failed.\n\n");
    00374   }
    00375
    00376   return ret;
    00377 }
    00378
    00379 /* static */ void CrashLog::SetErrorMessage(const char *message)
    00380 {
    00381   CrashLog::message = message;
    00382 }
    00383
    00384 /* static */ void CrashLog::AfterCrashLogCleanup()
    00385 {
    00386   if (_music_driver != NULL) _music_driver->Stop();
    00387   if (_sound_driver != NULL) _sound_driver->Stop();
    00388   if (_video_driver != NULL) _video_driver->Stop();
    00389 }