2008-04-14 16:17:51 -07:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 2008 Giel van Schijndel
|
|
|
|
Copyright (C) 2008 Warzone Resurrection 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 <climits>
|
|
|
|
#include <ctime>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
#include "dumpinfo.h"
|
2008-04-20 16:56:35 -07:00
|
|
|
|
2008-04-14 16:17:51 -07:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
// FIXME: #include from src/
|
|
|
|
#include "src/version.h"
|
|
|
|
}
|
|
|
|
|
2008-04-20 16:56:35 -07:00
|
|
|
#if defined(WZ_OS_UNIX)
|
2008-04-14 16:17:51 -07:00
|
|
|
# include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
2008-04-15 11:45:35 -07:00
|
|
|
#ifndef PACKAGE_DISTRIBUTOR
|
|
|
|
# define PACKAGE_DISTRIBUTOR "UNKNOWN"
|
|
|
|
#endif
|
|
|
|
|
2008-04-14 16:17:51 -07:00
|
|
|
static char* dbgHeader = NULL;
|
|
|
|
|
|
|
|
static void dumpstr(const DumpFileHandle file, const char * const str)
|
|
|
|
{
|
|
|
|
#if defined(WZ_OS_WIN)
|
|
|
|
DWORD lNumberOfBytesWritten;
|
|
|
|
WriteFile(file, str, strlen(str), &lNumberOfBytesWritten, NULL);
|
|
|
|
#else
|
|
|
|
write(file, str, strlen(str));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void dbgDumpHeader(DumpFileHandle file)
|
|
|
|
{
|
|
|
|
if (dbgHeader)
|
|
|
|
dumpstr(file, dbgHeader);
|
|
|
|
else
|
2008-04-20 16:56:35 -07:00
|
|
|
dumpstr(file, "No debug header available (yet)!\n" );
|
2008-04-14 16:17:51 -07:00
|
|
|
}
|
|
|
|
|
2008-04-28 13:45:28 -07:00
|
|
|
static std::string getProgramPath(const char* programCommand)
|
2008-04-14 16:17:51 -07:00
|
|
|
{
|
2008-04-20 16:56:35 -07:00
|
|
|
std::vector<char> buf(PATH_MAX);
|
2008-04-14 16:17:51 -07:00
|
|
|
|
|
|
|
#if defined(WZ_OS_WIN)
|
2008-04-15 11:25:20 -07:00
|
|
|
while (GetModuleFileNameA(NULL, &buf[0], buf.size()) == buf.size())
|
2008-04-14 16:17:51 -07:00
|
|
|
{
|
|
|
|
buf.resize(buf.size() * 2);
|
|
|
|
}
|
|
|
|
#elif defined(WZ_OS_UNIX) && !defined(WZ_OS_MAC)
|
|
|
|
{
|
|
|
|
FILE * whichProgramStream;
|
|
|
|
char* whichProgramCommand;
|
|
|
|
|
|
|
|
sasprintf(&whichProgramCommand, "which %s", programCommand);
|
|
|
|
whichProgramStream = popen(whichProgramCommand, "r");
|
|
|
|
fread(&buf[0], 1, buf.size(), whichProgramStream);
|
|
|
|
pclose(whichProgramStream);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-28 13:45:28 -07:00
|
|
|
std::string programPath = &buf[0];
|
2008-04-14 16:17:51 -07:00
|
|
|
|
|
|
|
if (!programPath.empty())
|
|
|
|
{
|
2008-04-28 14:16:51 -07:00
|
|
|
// `which' adds a \n which confuses exec()
|
|
|
|
std::string::size_type eol = programPath.find('\n');
|
|
|
|
if (eol != std::string::npos)
|
|
|
|
programPath.erase(eol);
|
2008-04-14 16:17:51 -07:00
|
|
|
debug(LOG_WZ, "Found us at %s", programPath.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debug(LOG_WARNING, "Could not retrieve full path to %s, will not create extended backtrace\n", programCommand);
|
|
|
|
}
|
2008-04-28 13:45:28 -07:00
|
|
|
|
|
|
|
return programPath;
|
2008-04-14 16:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getSysinfo()
|
|
|
|
{
|
|
|
|
#if defined(WZ_OS_WIN)
|
2008-04-20 16:56:35 -07:00
|
|
|
return std::string();
|
2008-04-14 16:17:51 -07:00
|
|
|
#elif defined(WZ_OS_UNIX)
|
|
|
|
struct utsname sysInfo;
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
if (uname(&sysInfo) != 0)
|
2008-04-20 16:56:35 -07:00
|
|
|
os << "System information may be invalid!" << std::endl
|
|
|
|
<< std::endl;
|
2008-04-14 16:17:51 -07:00
|
|
|
|
2008-04-20 16:56:35 -07:00
|
|
|
os << "Operating system: " << sysInfo.sysname << std::endl
|
|
|
|
<< "Node name: " << sysInfo.nodename << std::endl
|
|
|
|
<< "Release: " << sysInfo.release << std::endl
|
|
|
|
<< "Version: " << sysInfo.version << std::endl
|
|
|
|
<< "Machine: " << sysInfo.machine << std::endl;
|
2008-04-14 16:17:51 -07:00
|
|
|
|
|
|
|
return os.str();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-04-28 13:45:28 -07:00
|
|
|
static void createHeader(const char* programCommand)
|
2008-04-14 16:17:51 -07:00
|
|
|
{
|
|
|
|
time_t currentTime = time(NULL);
|
|
|
|
std::ostringstream os;
|
|
|
|
|
2008-04-28 13:45:28 -07:00
|
|
|
os << "Program: " << getProgramPath(programCommand) << "(" PACKAGE ")" << std::endl
|
2008-04-20 16:56:35 -07:00
|
|
|
<< "Version: " << version_getFormattedVersionString() << std::endl
|
|
|
|
<< "Distributor: " PACKAGE_DISTRIBUTOR << std::endl
|
|
|
|
<< "Compiled on: " __DATE__ " " __TIME__ << std::endl
|
2008-04-14 16:17:51 -07:00
|
|
|
<< "Compiled by: "
|
|
|
|
#if defined(WZ_CC_GNU) && !defined(WZ_CC_INTEL)
|
2008-04-20 16:56:35 -07:00
|
|
|
<< "GCC " __VERSION__ << std::endl
|
2008-04-14 16:17:51 -07:00
|
|
|
#elif defined(WZ_CC_INTEL)
|
|
|
|
// Intel includes the compiler name within the version string
|
2008-04-20 16:56:35 -07:00
|
|
|
<< __VERSION__ << std::endl
|
2008-04-14 16:17:51 -07:00
|
|
|
#else
|
2008-04-20 16:56:35 -07:00
|
|
|
<< "UNKNOWN" << std::endl
|
2008-04-14 16:17:51 -07:00
|
|
|
#endif
|
2008-04-20 16:56:35 -07:00
|
|
|
<< "Executed on: " << ctime(¤tTime) << std::endl
|
|
|
|
<< getSysinfo() << std::endl
|
|
|
|
<< "Pointers: " << (sizeof(void*) * CHAR_BIT) << "bit" << std::endl
|
|
|
|
<< std::endl;
|
2008-04-14 16:17:51 -07:00
|
|
|
|
|
|
|
dbgHeader = strdup(os.str().c_str());
|
|
|
|
if (dbgHeader == NULL)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "createHeader: Out of memory!");
|
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-28 13:45:28 -07:00
|
|
|
void dbgDumpInit(const char* programCommand)
|
2008-04-14 16:17:51 -07:00
|
|
|
{
|
2008-04-28 13:45:28 -07:00
|
|
|
createHeader(programCommand);
|
2008-04-14 16:17:51 -07:00
|
|
|
}
|