1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
/*
This file is part of Warzone 2100.
Copyright (C) 2008 Giel van Schijndel
Copyright (C) 2008-2020 Warzone 2100 Project
Warzone 2100 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; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dumpinfo.h"
#include <LaunchInfo.h>
#include <cerrno>
#include <climits>
#include <ctime>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <sstream>
#include <physfs.h>
#include <memory>
#include "lib/framework/stdio_ext.h"
#include "lib/framework/wzglobal.h" // required for config.h
#include "lib/framework/wzapp.h"
#include "lib/framework/i18n.h" // required to print build date in ISO 8601
#include "lib/framework/wztime.h"
#if defined(WZ_OS_UNIX)
# include <sys/utsname.h>
#endif
#ifndef PACKAGE_DISTRIBUTOR
# define PACKAGE_DISTRIBUTOR "UNKNOWN"
#endif
static const char endl[] =
#if defined(WZ_OS_WIN)
"\r\n";
#else
"\n";
#endif
using std::string;
static const std::size_t max_debug_messages = 20;
static char *dbgHeader = nullptr;
static std::recursive_mutex dbgMessagesMutex; // Protects dbgMessages.
static std::deque<std::vector<char>> dbgMessages;
// used to add custom info to the crash log
static std::vector<char> miscData;
static void dumpstr(const DumpFileHandle file, const char *const str, std::size_t const size)
{
#if defined(WZ_OS_WIN)
DWORD lNumberOfBytesWritten;
DWORD len = static_cast<DWORD>(std::min<size_t>(size, std::numeric_limits<DWORD>::max()));
WriteFile(file, str, len, &lNumberOfBytesWritten, nullptr);
#else
std::size_t written = 0;
while (written < size)
{
const ssize_t ret = write(file, str + written, size - written);
if (ret == -1)
{
switch (errno)
{
case EAGAIN:
// Sleep to prevent wasting of CPU in case of non-blocking I/O
usleep(1);
continue;
case EINTR:
continue;
default:
// TODO find a decent way to deal with the fatal errors
return;
}
}
written += ret;
}
#endif
}
static void dumpstr(const DumpFileHandle file, const char *const str)
{
dumpstr(file, str, strlen(str));
}
static void dumpEOL(const DumpFileHandle file)
{
dumpstr(file, endl);
}
static void debug_exceptionhandler_data(void **, const char *const str, code_part)
{
/* Can't use ASSERT here as that will cause us to be invoked again.
* Possibly resulting in infinite recursion.
*/
assert(str != nullptr && "Empty string sent to debug callback");
// For non-debug builds
if (str == nullptr)
{
return;
}
// Push this message on the message list
const char *last = &str[strlen(str)];
// Strip finishing newlines
while (last != str
&& *(last - 1) == '\n')
{
--last;
}
const std::unique_lock<std::recursive_mutex> G{ dbgMessagesMutex };
dbgMessages.push_back(std::vector<char>(str, last));
// Ensure the message list's maximum size is maintained
while (dbgMessages.size() > max_debug_messages)
{
dbgMessages.pop_front();
}
}
void dbgDumpHeader(DumpFileHandle file)
{
if (dbgHeader)
{
dumpstr(file, dbgHeader);
// Now get any other data that we need to include in bug report
dumpstr(file, "Misc Data:");
dumpEOL(file);
dumpstr(file, &miscData[0], miscData.size());
dumpEOL(file);
}
else
{
dumpstr(file, "No debug header available (yet)!");
dumpEOL(file);
}
}
void dbgDumpLog(DumpFileHandle file)
{
// Write all messages to the given file
std::unique_lock<std::recursive_mutex> G{ dbgMessagesMutex };
for (auto const &msg : dbgMessages)
{
dumpstr(file, "Log message: ");
dumpstr(file, msg.data(), msg.size());
dumpEOL(file);
}
G.unlock();
// Terminate with a separating newline
dumpEOL(file);
}
#if defined(WZ_OS_WIN)
#include <ntverp.h> // Windows SDK - include for access to VER_PRODUCTBUILD
#if VER_PRODUCTBUILD >= 9200
// 9200 is the Windows SDK 8.0 (which introduced family support)
#include <winapifamily.h> // Windows SDK
#else
// Earlier SDKs don't have the concept of families - provide simple implementation
// that treats everything as "desktop"
#if !defined(WINAPI_PARTITION_DESKTOP)
#define WINAPI_PARTITION_DESKTOP 0x00000001
#endif
#if !defined(WINAPI_FAMILY_PARTITION)
#define WINAPI_FAMILY_PARTITION(Partition) ((WINAPI_PARTITION_DESKTOP & Partition) == Partition)
#endif
#endif
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
static std::string getWindowsVersionString()
{
const DWORD WIN_MAX_EXTENDED_PATH = 32767;
// Get the Windows version using a method unaffected by compatibility modes
// See: https://docs.microsoft.com/en-us/windows/desktop/sysinfo/getting-the-system-version
// Get a handle to the loaded kernel32.dll
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
if (hKernel32 == NULL)
{
// Failed to get a handle to kernel32.dll??
return "Failed to access kernel32";
}
// Allocate a buffer for the path to kernel32.dll
std::vector<wchar_t> kernel32_path(WIN_MAX_EXTENDED_PATH, 0);
DWORD moduleFileNameLen = GetModuleFileNameW(hKernel32, &kernel32_path[0], WIN_MAX_EXTENDED_PATH);
DWORD lastError = GetLastError();
if (moduleFileNameLen == 0)
{
// GetModuleFileNameW failed
return std::string("GetModuleFileNameW failed with error code: ") + std::to_string(lastError);
}
if (moduleFileNameLen >= WIN_MAX_EXTENDED_PATH)
{
// GetModuleFileNameW failed - insufficient buffer length
return "GetModuleFileNameW failed - buffer";
}
// Always append a null-terminator
// (Windows XP's GetModuleFileName does not guarantee null-termination)
kernel32_path[moduleFileNameLen] = 0;
// Get the ProductVersion of kernel32.dll
DWORD dwDummy = 0;
DWORD dwFileVersionInfoSize = GetFileVersionInfoSizeW(&kernel32_path[0], &dwDummy);
if (dwFileVersionInfoSize == 0)
{
// GetFileVersionInfoSizeW failed
lastError = GetLastError();
return std::string("GetFileVersionInfoSizeW failed with error code: ") + std::to_string(lastError);
}
void *lpData = malloc(dwFileVersionInfoSize);
if (lpData == nullptr)
{
// Unable to allocate buffer
return std::string("malloc failed to allocate ") + std::to_string(dwFileVersionInfoSize) + " bytes";
}
if (GetFileVersionInfoW(&kernel32_path[0], 0, dwFileVersionInfoSize, lpData) == 0)
{
// GetFileVersionInfoW failed
lastError = GetLastError();
free(lpData);
return std::string("GetFileVersionInfoW failed with error code: ") + std::to_string(lastError);
}
VS_FIXEDFILEINFO *version = nullptr;
UINT version_len = 0;
if (!VerQueryValueW(lpData, L"\\", (LPVOID*)&version, &version_len))
{
// VerQueryValueW failed
free(lpData);
return "VerQueryValueW failed";
}
std::string productVersionString;
productVersionString = std::to_string(HIWORD(version->dwProductVersionMS)) + ".";
productVersionString += std::to_string(LOWORD(version->dwProductVersionMS)) + ".";
productVersionString += std::to_string(HIWORD(version->dwProductVersionLS)) + ".";
productVersionString += std::to_string(LOWORD(version->dwProductVersionLS));
free(lpData);
return productVersionString;
}
#else // !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
static std::string getWindowsVersionString()
{
// FIXME: Implement Windows version detection for non-desktop builds?
return std::string("n/a");
}
#endif // WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#ifndef PROCESSOR_ARCHITECTURE_AMD64
#define PROCESSOR_ARCHITECTURE_AMD64 9
#endif
#ifndef PROCESSOR_ARCHITECTURE_ARM
#define PROCESSOR_ARCHITECTURE_ARM 5
#endif
#ifndef PROCESSOR_ARCHITECTURE_ARM64
#define PROCESSOR_ARCHITECTURE_ARM64 12
#endif
static std::string getWindowsProcessorArchitecture()
{
SYSTEM_INFO siSysInfo = {};
GetNativeSystemInfo(&siSysInfo);
std::string machine;
switch (siSysInfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_INTEL:
machine = "x86";
break;
case PROCESSOR_ARCHITECTURE_IA64:
machine = "Intel Itanium-based";
break;
case PROCESSOR_ARCHITECTURE_AMD64:
machine = "x86_64";
break;
case PROCESSOR_ARCHITECTURE_ARM:
machine = "ARM";
break;
case PROCESSOR_ARCHITECTURE_ARM64:
machine = "ARM64";
break;
case PROCESSOR_ARCHITECTURE_UNKNOWN:
machine = "Unknown";
break;
default:
machine = "wProcessorArchitecture (";
machine += std::to_string(siSysInfo.wProcessorArchitecture) + ")";
break;
}
return machine;
}
#endif // defined(WZ_OS_WIN)
static std::string getSysinfo()
{
#if defined(WZ_OS_WIN)
std::ostringstream os;
os << "Operating system: " << "Windows" << endl
<< "Version: " << getWindowsVersionString() << endl
<< "Machine: " << getWindowsProcessorArchitecture() << endl;
return os.str();
#elif defined(WZ_OS_UNIX)
struct utsname sysInfo;
std::ostringstream os;
if (uname(&sysInfo) != 0)
os << "System information may be invalid!" << endl
<< endl;
os << "Operating system: " << sysInfo.sysname << endl
<< "Node name: " << sysInfo.nodename << endl
<< "Release: " << sysInfo.release << endl
<< "Version: " << sysInfo.version << endl
<< "Machine: " << sysInfo.machine << endl;
return os.str();
#else
// Not yet implemented for other platforms
return std::string();
#endif
}
static std::string getCurTime()
{
using std::string;
// Get the current time
const time_t currentTime = time(nullptr);
// Convert it to a string
string time(ctime(¤tTime));
// Mark finishing newlines as NUL characters
for (string::reverse_iterator
newline = time.rbegin();
newline != time.rend()
&& *newline == '\n';
++newline)
{
*newline = '\0';
}
// Remove everything after, and including, the first NUL character
string::size_type newline = time.find_first_of('\0');
if (newline != string::npos)
{
time.erase(newline);
}
return time;
}
template <typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, PHYSFS_Version const &ver)
{
return os << static_cast<unsigned int>(ver.major)
<< "." << static_cast<unsigned int>(ver.minor)
<< "." << static_cast<unsigned int>(ver.patch);
}
static void createHeader(int const argc, const char * const *argv, const char *packageVersion)
{
std::ostringstream os;
os << "Program: " << LaunchInfo::getCurrentProcessDetails().imageFileName.fullPath() << " (" PACKAGE ")" << endl
<< "Command line: ";
/* Dump all command line arguments surrounded by double quotes and
* separated by spaces.
*/
for (int i = 0; i < argc; ++i)
{
os << "\"" << argv[i] << "\" ";
}
os << endl;
const auto& parentProcesses = LaunchInfo::getAncestorProcesses();
os << "Ancestors: " << endl;
for (const auto& parent : parentProcesses)
{
os << "- (" << parent.pid << ") " << parent.imageFileName.fullPath() << endl;
}
os << "Version: " << packageVersion << endl
<< "Distributor: " PACKAGE_DISTRIBUTOR << endl
<< "Compiled on: " << getCompileDate() << " " << __TIME__ << endl
<< "Compiled by: "
#if defined(WZ_CC_GNU) && !defined(WZ_CC_INTEL) && !defined(WZ_CC_CLANG)
<< "GCC " __VERSION__ << endl
#elif defined(WZ_CC_CLANG)
<< "Clang " __clang_version__ << endl
#elif defined(WZ_CC_INTEL)
// Intel includes the compiler name within the version string
<< __VERSION__ << endl
#elif defined(WZ_CC_MSVC)
<< "MSVC " << _MSC_FULL_VER
# if defined(_MSVC_LANG)
<< " (c++ std: " << _MSVC_LANG << ")" << endl
# else
<< endl
# endif
#else
<< "UNKNOWN" << endl
#endif
<< "Compiled mode: "
#ifdef DEBUG
<< "Debug build" << endl
#else
<< "Release build" << endl
#endif
<< "Executed on: " << getCurTime() << endl
<< getSysinfo() << endl
<< "Pointers: " << (sizeof(void *) * CHAR_BIT) << "bit" << endl
<< endl;
PHYSFS_Version physfs_version;
// Determine PhysicsFS compile time version
PHYSFS_VERSION(&physfs_version)
os << "Compiled against PhysicsFS version: " << physfs_version << endl;
// Determine PhysicsFS runtime version
PHYSFS_getLinkedVersion(&physfs_version);
os << "Running with PhysicsFS version: " << physfs_version << endl
<< endl;
dbgHeader = strdup(os.str().c_str());
if (dbgHeader == nullptr)
{
debug(LOG_FATAL, "createHeader: Out of memory!");
abort();
return;
}
}
void addDumpInfo(const char *inbuffer)
{
char ourtime[sizeof("HH:MM:SS")];
const time_t curtime = time(nullptr);
struct tm timeinfo = getLocalTime(curtime);
strftime(ourtime, sizeof(ourtime), "%H:%M:%S", &timeinfo);
// add timestamp to all strings
std::ostringstream os;
os << "[" << ourtime << "]" << inbuffer << endl;
// Append message to miscData
string msg(os.str());
miscData.insert(miscData.end(), msg.begin(), msg.end());
}
void dbgDumpInit(int argc, const char * const *argv, const char *packageVersion)
{
debug_register_callback(&debug_exceptionhandler_data, nullptr, nullptr, nullptr);
createHeader(argc, argv, packageVersion);
}
|