23 #include "C4Version.h"
31 int WINAPI WinMain (HINSTANCE hInst,
32 HINSTANCE hPrevInstance,
36 #if defined(_DEBUG) && defined(_MSC_VER)
38 _CrtSetDbgFlag( _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
47 HMODULE user32 = LoadLibrary(L
"user32");
50 typedef BOOL (WINAPI *SETPROCESSDPIAWAREPROC)();
51 SETPROCESSDPIAWAREPROC SetProcessDPIAware =
52 reinterpret_cast<SETPROCESSDPIAWAREPROC
>(GetProcAddress(user32,
"SetProcessDPIAware"));
53 if (SetProcessDPIAware)
61 std::vector<char*> argv;
63 LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
66 const char *error =
"Internal error: Unable to split command line! Exiting.";
74 LPWSTR *curwarg = wargv;
77 int arglen = WideCharToMultiByte(CP_UTF8, 0, *curwarg, -1,
nullptr, 0,
nullptr,
nullptr);
78 auto *utf8arg =
new char[arglen ? arglen : 1];
79 WideCharToMultiByte(CP_UTF8, 0, *curwarg, -1, utf8arg, arglen,
nullptr,
nullptr);
80 argv.push_back(utf8arg);
98 for(std::vector<char*>::const_iterator it = argv.begin(); it != argv.end(); ++it)
108 return WinMain(GetModuleHandle(
nullptr),
nullptr,
nullptr, 0);
116 #ifdef HAVE_SYS_TYPES_H
117 #include <sys/types.h>
121 #include <backward-cpp/backward.hpp>
123 #elif defined(HAVE_SIGNAL_H)
126 #ifdef HAVE_EXECINFO_H
127 #include <execinfo.h>
130 static void crash_handler(
int signo, siginfo_t * si,
void *)
132 static unsigned signal_count = 0;
136 case SIGINT:
case SIGTERM:
case SIGHUP:
137 if (signal_count < 2) {
142 int logfd = STDERR_FILENO;
146 write(logfd, C4VERSION
": Caught signal ",
sizeof (C4VERSION
": Caught signal ") - 1);
149 case SIGBUS: write(logfd,
"SIGBUS",
sizeof (
"SIGBUS") - 1);
break;
150 case SIGILL: write(logfd,
"SIGILL",
sizeof (
"SIGILL") - 1);
break;
151 case SIGSEGV: write(logfd,
"SIGSEGV",
sizeof (
"SIGSEGV") - 1);
break;
152 case SIGABRT: write(logfd,
"SIGABRT",
sizeof (
"SIGABRT") - 1);
break;
153 case SIGINT: write(logfd,
"SIGINT",
sizeof (
"SIGINT") - 1);
break;
154 case SIGHUP: write(logfd,
"SIGHUP",
sizeof (
"SIGHUP") - 1);
break;
155 case SIGFPE: write(logfd,
"SIGFPE",
sizeof (
"SIGFPE") - 1);
break;
156 case SIGTERM: write(logfd,
"SIGTERM",
sizeof (
"SIGTERM") - 1);
break;
158 char hex[
sizeof(
void *) * 2];
159 intptr_t x =
reinterpret_cast<intptr_t
>(si->si_addr);
162 case SIGILL:
case SIGFPE:
case SIGSEGV:
case SIGBUS:
case SIGTRAP:
163 write(logfd,
" (0x",
sizeof (
" (0x") - 1);
164 for (
int i =
sizeof(
void *) * 2 - 1; i >= 0; --i)
167 hex[i] =
'a' + (x & 0xf) - 9;
169 hex[i] =
'0' + (x & 0xf);
172 write(logfd, hex,
sizeof (hex));
173 write(logfd,
")",
sizeof (
")") - 1);
176 write(logfd,
"\n",
sizeof (
"\n") - 1);
177 if (logfd == STDERR_FILENO) logfd =
GetLogFD();
179 if (logfd < 0)
break;
181 #ifdef HAVE_EXECINFO_H
184 int count = backtrace(stack, 100);
186 backtrace_symbols_fd (stack, count, STDERR_FILENO);
189 backtrace_symbols_fd (stack, count, logfd);
197 static void restart(
char * argv[])
200 int open_max = sysconf (_SC_OPEN_MAX);
201 for (
int fd = 4; fd < open_max; fd++)
202 fcntl (fd, F_SETFD, FD_CLOEXEC);
204 execlp(argv[0], argv[0],
static_cast<char *
>(
nullptr));
207 int main (
int argc,
char * argv[])
211 printf(
"Do not run %s as root!\n", argc ? argv[0] :
"this program");
215 backward::SignalHandling sh;
216 #elif defined(HAVE_SIGNAL_H)
218 sa.sa_sigaction = crash_handler;
219 sigemptyset(&sa.sa_mask);
220 sa.sa_flags = SA_SIGINFO;
222 sigaction(SIGINT, &sa,
nullptr);
223 sigaction(SIGTERM, &sa,
nullptr);
224 sigaction(SIGHUP, &sa,
nullptr);
226 sa.sa_flags |= SA_RESETHAND;
227 sigaction(SIGBUS, &sa,
nullptr);
228 sigaction(SIGILL, &sa,
nullptr);
229 sigaction(SIGSEGV, &sa,
nullptr);
230 sigaction(SIGABRT, &sa,
nullptr);
231 sigaction(SIGFPE, &sa,
nullptr);
const int C4XRV_Completed
void InstallCrashHandler()
C4Application Application
bool Log(const char *szMessage)
int main(int argc, char *argv[])
void MessageDialog(const char *message)
bool Init(int argc, char *argv[])