# crashlog.cpp

Go to the documentation of this file.

    00001 /* $Id: crashlog.cpp 18916 2010-01-25 17:22:06Z frosch $ */
    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\n"
    00113       " Language:     %s\n"
    00114       " Music driver: %s\n"
    00115       " Music set:    %s\n"
    00116       " Network:      %s\n"
    00117       " Sound driver: %s\n"
    00118       " Sound set:    %s\n"
    00119       " Video driver: %s\n\n",
    00120       BlitterFactoryBase::GetCurrentBlitter() == NULL ? "none" : BlitterFactoryBase::GetCurrentBlitter()->GetName(),
    00121       BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
    00122       StrEmpty(_dynlang.curr_file) ? "none" : _dynlang.curr_file,
    00123       _music_driver == NULL ? "none" : _music_driver->GetName(),
    00124       BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
    00125       _networking ? (_network_server ? "server" : "client") : "no",
    00126       _sound_driver == NULL ? "none" : _sound_driver->GetName(),
    00127       BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
    00128       _video_driver == NULL ? "none" : _video_driver->GetName()
    00129   );
    00130
    00131   buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
    00132   const Company *c;
    00133   FOR_ALL_COMPANIES(c) {
    00134     if (c->ai_info == NULL) {
    00135       buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
    00136     } else {
    00137       buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
    00138     }
    00139   }
    00140   buffer += seprintf(buffer, last, "\n");
    00141
    00142   return buffer;
    00143 }
    00144
    00145 /* Include these here so it's close to where it's actually used. */
    00146 #ifdef WITH_ALLEGRO
    00147 # include <allegro.h>
    00148 #endif /* WITH_ALLEGRO */
    00149 #ifdef WITH_FONTCONFIG
    00150 # include <fontconfig/fontconfig.h>
    00151 #endif /* WITH_FONTCONFIG */
    00152 #ifdef WITH_PNG
    00153   /* pngconf.h, included by png.h doesn't like something in the
    00154    * freetype headers. As such it's not alphabetically sorted. */
    00155 # include <png.h>
    00156 #endif /* WITH_PNG */
    00157 #ifdef WITH_FREETYPE
    00158 # include <ft2build.h>
    00159 # include FT_FREETYPE_H
    00160 #endif /* WITH_FREETYPE */
    00161 #ifdef WITH_ICU
    00162 # include <unicode/uversion.h>
    00163 #endif /* WITH_ICU */
    00164 #ifdef WITH_LZO
    00165 #include <lzo/lzo1x.h>
    00166 #endif
    00167 #ifdef WITH_SDL
    00168 # include "sdl.h"
    00169 # include <SDL.h>
    00170 #endif /* WITH_SDL */
    00171 #ifdef WITH_ZLIB
    00172 # include <zlib.h>
    00173 #endif
    00174
    00175 char *CrashLog::LogLibraries(char *buffer, const char *last) const
    00176 {
    00177   buffer += seprintf(buffer, last, "Libraries:\n");
    00178
    00179 #ifdef WITH_ALLEGRO
    00180   buffer += seprintf(buffer, last, " Allegro:    %s\n", allegro_id);
    00181 #endif /* WITH_ALLEGRO */
    00182
    00183 #ifdef WITH_FONTCONFIG
    00184   int version = FcGetVersion();
    00185   buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
    00186 #endif /* WITH_FONTCONFIG */
    00187
    00188 #ifdef WITH_FREETYPE
    00189   FT_Library library;
    00190   int major, minor, patch;
    00191   FT_Init_FreeType(&library);
    00192   FT_Library_Version(library, &major, &minor, &patch);
    00193   FT_Done_FreeType(library);
    00194   buffer += seprintf(buffer, last, " FreeType:   %d.%d.%d\n", major, minor, patch);
    00195 #endif /* WITH_FREETYPE */
    00196
    00197 #ifdef WITH_ICU
    00198   /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
    00199   char buf[4 * 3 + 3 + 1];
    00200   UVersionInfo ver;
    00201   u_getVersion(ver);
    00202   u_versionToString(ver, buf);
    00203   buffer += seprintf(buffer, last, " ICU:        %s\n", buf);
    00204 #endif /* WITH_ICU */
    00205
    00206 #ifdef WITH_LZO
    00207   buffer += seprintf(buffer, last, " LZO:        %s\n", lzo_version_string());
    00208 #endif
    00209
    00210 #ifdef WITH_PNG
    00211   buffer += seprintf(buffer, last, " PNG:        %s\n", png_get_libpng_ver(NULL));
    00212 #endif /* WITH_PNG */
    00213
    00214 #ifdef WITH_SDL
    00215 #ifdef DYNAMICALLY_LOADED_SDL
    00216   if (SDL_CALL SDL_Linked_Version != NULL) {
    00217 #else
    00218   {
    00219 #endif
    00220     const SDL_version *v = SDL_CALL SDL_Linked_Version();
    00221     buffer += seprintf(buffer, last, " SDL:        %d.%d.%d\n", v->major, v->minor, v->patch);
    00222   }
    00223 #endif /* WITH_SDL */
    00224
    00225 #ifdef WITH_ZLIB
    00226   buffer += seprintf(buffer, last, " Zlib:       %s\n", zlibVersion());
    00227 #endif
    00228
    00229   buffer += seprintf(buffer, last, "\n");
    00230   return buffer;
    00231 }
    00232
    00233 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
    00234 {
    00235   CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
    00236 }
    00237
    00238 char *CrashLog::LogGamelog(char *buffer, const char *last) const
    00239 {
    00240   CrashLog::gamelog_buffer = buffer;
    00241   CrashLog::gamelog_last = last;
    00242   GamelogPrint(&CrashLog::GamelogFillCrashLog);
    00243   return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
    00244 }
    00245
    00246 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
    00247 {
    00248   time_t cur_time = time(NULL);
    00249   buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
    00250   buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
    00251
    00252   YearMonthDay ymd;
    00253   ConvertDateToYMD(_date, &ymd);
    00254   buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
    00255
    00256   buffer = this->LogError(buffer, last, CrashLog::message);
    00257   buffer = this->LogOpenTTDVersion(buffer, last);
    00258   buffer = this->LogRegisters(buffer, last);
    00259   buffer = this->LogStacktrace(buffer, last);
    00260   buffer = this->LogOSVersion(buffer, last);
    00261   buffer = this->LogCompiler(buffer, last);
    00262   buffer = this->LogConfiguration(buffer, last);
    00263   buffer = this->LogLibraries(buffer, last);
    00264   buffer = this->LogModules(buffer, last);
    00265   buffer = this->LogGamelog(buffer, last);
    00266
    00267   buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
    00268   return buffer;
    00269 }
    00270
    00271 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
    00272 {
    00273   seprintf(filename, filename_last, "%scrash.log", _personal_dir);
    00274
    00275   FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
    00276   if (file == NULL) return false;
    00277
    00278   size_t len = strlen(buffer);
    00279   size_t written = fwrite(buffer, 1, len, file);
    00280
    00281   FioFCloseFile(file);
    00282   return len == written;
    00283 }
    00284
    00285 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
    00286 {
    00287   /* Stub implementation; not all OSes support this. */
    00288   return 0;
    00289 }
    00290
    00291 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
    00292 {
    00293   /* If the map array doesn't exist, saving will fail too. If the map got
    00294    * initialised, there is a big chance the rest is initialised too. */
    00295   if (_m == NULL) return false;
    00296
    00297   try {
    00298     GamelogEmergency();
    00299
    00300     seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
    00301
    00302     /* Don't do a threaded saveload. */
    00303     return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
    00304   } catch (...) {
    00305     return false;
    00306   }
    00307 }
    00308
    00309 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
    00310 {
    00311   /* Don't draw when we have invalid screen size */
    00312   if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
    00313
    00314   bool res = MakeScreenshot(SC_RAW, "crash");
    00315   if (res) strecpy(filename, _full_screenshot_name, filename_last);
    00316   return res;
    00317 }
    00318
    00319 bool CrashLog::MakeCrashLog() const
    00320 {
    00321   /* Don't keep looping logging crashes. */
    00322   static bool crashlogged = false;
    00323   if (crashlogged) return false;
    00324   crashlogged = true;
    00325
    00326   char filename[MAX_PATH];
    00327   char buffer[65536];
    00328   bool ret = true;
    00329
    00330   printf("Crash encountered, generating crash log...\n");
    00331   this->FillCrashLog(buffer, lastof(buffer));
    00332   printf("%s\n", buffer);
    00333   printf("Crash log generated.\n\n");
    00334
    00335   printf("Writing crash log to disk...\n");
    00336   bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
    00337   if (bret) {
    00338     printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
    00339   } else {
    00340     printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
    00341     ret = false;
    00342   }
    00343
    00344   /* Don't mention writing crash dumps because not all platforms support it. */
    00345   int dret = this->WriteCrashDump(filename, lastof(filename));
    00346   if (dret < 0) {
    00347     printf("Writing crash dump failed.\n\n");
    00348     ret = false;
    00349   } else if (dret > 0) {
    00350     printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
    00351   }
    00352
    00353   printf("Writing crash savegame...\n");
    00354   bret = this->WriteSavegame(filename, lastof(filename));
    00355   if (bret) {
    00356     printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
    00357   } else {
    00358     ret = false;
    00359     printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
    00360   }
    00361
    00362   printf("Writing crash screenshot...\n");
    00363   bret = this->WriteScreenshot(filename, lastof(filename));
    00364   if (bret) {
    00365     printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
    00366   } else {
    00367     ret = false;
    00368     printf("Writing crash screenshot failed.\n\n");
    00369   }
    00370
    00371   return ret;
    00372 }
    00373
    00374 /* static */ void CrashLog::SetErrorMessage(const char *message)
    00375 {
    00376   CrashLog::message = message;
    00377 }
    00378
    00379 /* static */ void CrashLog::AfterCrashLogCleanup()
    00380 {
    00381   if (_music_driver != NULL) _music_driver->Stop();
    00382   if (_sound_driver != NULL) _sound_driver->Stop();
    00383   if (_video_driver != NULL) _video_driver->Stop();
    00384 }