# crashlog.cpp

Go to the documentation of this file.

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