00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "CrashReporter.h"
00048 #include "stringutil.h"
00049
00050 #if defined(Q_OS_WIN32)
00051 #include <client/windows/handler/exception_handler.h>
00052 #elif defined(Q_OS_MAC)
00053 #include <client/mac/handler/exception_handler.h>
00054 #elif defined(Q_OS_LINUX)
00055 #include <client/linux/handler/exception_handler.h>
00056 #elif defined(Q_OS_SOLARIS)
00057 #include <client/solaris/handler/exception_handler.h>
00058 #endif
00059
00060 #include <QString>
00061 #include <QStringList>
00062 #include <QFileInfo>
00063 #include <QDir>
00064
00065 #include <time.h>
00066
00067
00068 namespace CrashReporter
00069 {
00070 #if defined(Q_OS_WIN32)
00071 typedef wchar_t _char_t;
00072 typedef HANDLE _file_handle_t;
00073 #define PATH_SEPARATOR TEXT("\\")
00074 # ifdef _USE_32BIT_TIME_T
00075 # define TIME_TO_STRING(buf, buflen, t) \
00076 _ltoa_s(t, buf, buflen, 10)
00077 # else
00078 # define TIME_TO_STRING(buf, buflen, t) \
00079 _i64toa_s(t, buf, buflen, 10)
00080 # endif
00081 #else
00082 typedef char _char_t;
00083 typedef int _file_handle_t;
00084 #define PATH_SEPARATOR "/"
00085 #define TEXT(x) (x)
00086 #define TIME_TO_STRING(buf, buflen, t) \
00087 snprintf(buf, buflen, "%ld", t)
00088 #endif
00089
00090
00091
00092
00093
00094 static google_breakpad::ExceptionHandler *exceptionHandler = 0;
00095
00096
00097
00098
00099
00100
00101 static bool showCrashReporter = false;
00102
00103
00104
00105
00106
00107 static _char_t crashReporterExecutable[MAX_PATH_LEN + 1] = TEXT("");
00108
00109
00110
00111
00112
00113 static char buildVersion[MAX_VERSION_LEN + 1] = "";
00114
00115
00116
00117
00118
00119
00120 static char restartExecutable[MAX_CMD_LEN + 1] = "";
00121
00122
00123
00124
00125
00126 static char restartExecutableArgs[MAX_CMD_LEN + 1] = "";
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 static time_t startupTime = 0;
00137
00138
00139
00140
00141
00142
00143
00144 static size_t
00145 append_string(_char_t *dst, const _char_t *src, size_t siz)
00146 {
00147 _char_t *d = dst;
00148 const _char_t *s = src;
00149 size_t n = siz;
00150 size_t dlen;
00151
00152
00153 while (n-- != 0 && *d != TEXT('\0'))
00154 d++;
00155 dlen = d - dst;
00156 n = siz - dlen;
00157
00158 if (n == 0)
00159 #if defined(Q_OS_WIN32)
00160 return (dlen + wcslen(s));
00161 #else
00162 return(dlen + strlen(s));
00163 #endif
00164
00165 while (*s != TEXT('\0')) {
00166 if (n != 1) {
00167 *d++ = *s;
00168 n--;
00169 }
00170 s++;
00171 }
00172 *d = TEXT('\0');
00173
00174 return(dlen + (s - src));
00175 }
00176
00177
00178
00179
00180
00181 static void
00182 write_keyval_to_file(_file_handle_t hFile, const char *key, const char *val)
00183 {
00184 #if defined(Q_OS_WIN32)
00185 DWORD dwWritten;
00186 WriteFile(hFile, key, strlen(key), &dwWritten, NULL);
00187 WriteFile(hFile, "=", 1, &dwWritten, NULL);
00188 WriteFile(hFile, val, strlen(val), &dwWritten, NULL);
00189 WriteFile(hFile, "\n", 1, &dwWritten, NULL);
00190 #else
00191 write(hFile, key, strlen(key));
00192 write(hFile, "=", 1);
00193 write(hFile, val, strlen(val));
00194 write(hFile, "\n", 1);
00195 #endif
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 static bool
00207 write_extra_dump_info(const _char_t *path, const _char_t *id, time_t crashTime)
00208 {
00209 static const char *KeyBuildVersion = "BuildVersion";
00210 static const char *KeyCrashTime = "CrashTime";
00211 static const char *KeyStartupTime = "StartupTime";
00212 static const char *KeyRestartExecutable = "RestartExecutable";
00213 static const char *KeyRestartExecutableArgs = "RestartExecutableArgs";
00214
00215 _char_t extraInfoPath[MAX_PATH_LEN] = TEXT("");
00216 append_string(extraInfoPath, path, MAX_PATH_LEN);
00217 append_string(extraInfoPath, PATH_SEPARATOR, MAX_PATH_LEN);
00218 append_string(extraInfoPath, id, MAX_PATH_LEN);
00219 size_t len = append_string(extraInfoPath, TEXT(".dmp.info"), MAX_PATH_LEN);
00220 if (len >= MAX_PATH_LEN)
00221 return false;
00222
00223 #if defined(Q_OS_WIN32)
00224 HANDLE hFile = CreateFile(extraInfoPath, GENERIC_WRITE, 0, NULL,
00225 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
00226 if (hFile == INVALID_HANDLE_VALUE)
00227 return false;
00228 #else
00229
00230 #endif
00231
00232 char crashTimeString[24], startupTimeString[24];
00233 TIME_TO_STRING(crashTimeString, 24, crashTime);
00234 TIME_TO_STRING(startupTimeString, 24, startupTime);
00235
00236 write_keyval_to_file(hFile, KeyBuildVersion, buildVersion);
00237 write_keyval_to_file(hFile, KeyCrashTime, crashTimeString);
00238 write_keyval_to_file(hFile, KeyStartupTime, startupTimeString);
00239 write_keyval_to_file(hFile, KeyRestartExecutable, restartExecutable);
00240 write_keyval_to_file(hFile, KeyRestartExecutableArgs, restartExecutableArgs);
00241
00242 #if defined(Q_OS_WIN32)
00243 CloseHandle(hFile);
00244 #else
00245
00246
00247 #endif
00248 return true;
00249 }
00250
00251
00252
00253
00254
00255
00256
00257 bool
00258 minidump_callback(const _char_t *path,
00259 const _char_t *id,
00260 void *context,
00261 #if defined(Q_OS_WIN32)
00262 EXCEPTION_POINTERS *exInfo,
00263 MDRawAssertionInfo *assertionInfo,
00264 #endif
00265 bool succeeded)
00266 {
00267 if (! succeeded || ! showCrashReporter)
00268 return false;
00269
00270
00271
00272 write_extra_dump_info(path, id, time(NULL));
00273
00274
00275 _char_t commandLine[MAX_CMD_LEN] = TEXT("");
00276 append_string(commandLine, TEXT("\""), MAX_CMD_LEN);
00277 append_string(commandLine, crashReporterExecutable, MAX_CMD_LEN);
00278 append_string(commandLine, TEXT("\" \""), MAX_CMD_LEN);
00279 append_string(commandLine, path, MAX_CMD_LEN);
00280 append_string(commandLine, PATH_SEPARATOR, MAX_CMD_LEN);
00281 append_string(commandLine, id, MAX_CMD_LEN);
00282 size_t len = append_string(commandLine, TEXT(".dmp\""), MAX_CMD_LEN);
00283 if (len >= MAX_CMD_LEN)
00284 return false;
00285
00286
00287 #if defined(Q_OS_WIN32)
00288 PROCESS_INFORMATION pi;
00289 STARTUPINFOW si;
00290
00291 ZeroMemory(&pi, sizeof(pi));
00292 ZeroMemory(&si, sizeof(si));
00293 si.cb = sizeof(si);
00294 si.dwFlags = STARTF_USESHOWWINDOW;
00295 si.wShowWindow = SW_SHOWDEFAULT;
00296
00297 BOOL rc = CreateProcess(NULL, (LPWSTR)commandLine, NULL, NULL, FALSE, 0,
00298 NULL, NULL, &si, &pi);
00299 if (rc) {
00300 CloseHandle(pi.hThread);
00301 CloseHandle(pi.hProcess);
00302 }
00303 TerminateProcess(GetCurrentProcess(), 1);
00304 return true;
00305 #else
00306
00307 return false;
00308 #endif
00309 }
00310
00311 bool
00312 install_exception_handler(const QString &dumpPath)
00313 {
00314
00315 QDir dumpDir(dumpPath);
00316 if (! dumpDir.exists() && ! dumpDir.mkdir("."))
00317 return false;
00318
00319
00320 exceptionHandler = new google_breakpad::ExceptionHandler(
00321 #if defined(Q_OS_WIN32)
00322 dumpDir.absolutePath().toStdWString(),
00323 #else
00324 dumpDir.absolutePath().toStdString(),
00325 #endif
00326 NULL,
00327 minidump_callback,
00328 NULL,
00329 #if defined(Q_OS_WIN32)
00330 google_breakpad::ExceptionHandler::HANDLER_ALL);
00331 #else
00332 true);
00333 #endif
00334 if (! exceptionHandler)
00335 return false;
00336
00337 startupTime = time(NULL);
00338 return true;
00339 }
00340
00341 void
00342 remove_exception_handler(void)
00343 {
00344 if (exceptionHandler) {
00345 delete exceptionHandler;
00346 exceptionHandler = 0;
00347 }
00348 }
00349
00350 bool
00351 set_crash_reporter(const QString &crashReporter)
00352 {
00353 #if defined(Q_OS_WIN32)
00354 if (crashReporter.length() <= CrashReporter::MAX_PATH_LEN) {
00355 crashReporter.toWCharArray(crashReporterExecutable);
00356 crashReporterExecutable[crashReporter.length()] = L'\0';
00357 #else
00358 QByteArray utf8 = crashReporter.toUtf8();
00359 if (utf8.length() <= CrashReporter::MAX_PATH_LEN) {
00360 memcpy(crashReporterExecutable, utf8.constData(), utf8.length());
00361 crashReporterExecutable[utf8.length()] = '\0';
00362 #endif
00363 showCrashReporter = true;
00364 } else {
00365
00366
00367
00368
00369 showCrashReporter = false;
00370 }
00371 return showCrashReporter;
00372 }
00373
00374 bool
00375 set_restart_options(const QString &executable, const QStringList &arguments)
00376 {
00377 QByteArray exe = executable.toUtf8();
00378 if (exe.length() > MAX_CMD_LEN)
00379 return false;
00380
00381 QByteArray args = string_format_arguments(arguments).toUtf8();
00382 if (args.length() > MAX_CMD_LEN)
00383 return false;
00384
00385 memcpy(restartExecutable, exe.constData(), exe.length());
00386 restartExecutable[exe.length()] = '\0';
00387
00388 memcpy(restartExecutableArgs, args.constData(), args.length());
00389 restartExecutableArgs[args.length()] = '\0';
00390
00391 return true;
00392 }
00393
00394 bool
00395 set_build_version(const QString &version)
00396 {
00397 if (version.length() > MAX_VERSION_LEN)
00398 return false;
00399
00400 QByteArray ascii = version.toAscii();
00401 memcpy(buildVersion, ascii.constData(), ascii.length());
00402 buildVersion[ascii.length()] = '\0';
00403
00404 return true;
00405 }
00406
00407 }
00408