2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2005-01-20 23:22:22 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2005-01-20 23:22:22 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2004-02-29 00:50:05 +00:00
|
|
|
|
2004-11-15 01:58:30 +00:00
|
|
|
// Stuff related to stderr/stdout direction and exception handling;
|
|
|
|
// used by both core client and by apps
|
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2005-07-14 16:46:38 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
2005-07-14 16:46:38 +00:00
|
|
|
#endif
|
|
|
|
|
2010-05-13 17:03:29 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
2009-08-22 17:00:19 +00:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#define strdate _strdate
|
|
|
|
#define strtime _strtime
|
|
|
|
#endif
|
|
|
|
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2004-03-24 07:39:24 +00:00
|
|
|
#ifndef _WIN32
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2008-02-27 23:26:38 +00:00
|
|
|
#include <cstring>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdlib>
|
2008-06-26 20:37:48 +00:00
|
|
|
#include <unistd.h>
|
2004-03-24 07:39:24 +00:00
|
|
|
#endif
|
2004-02-29 00:50:05 +00:00
|
|
|
|
2009-01-13 23:06:02 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "boinc_fcgi.h"
|
|
|
|
#endif
|
|
|
|
|
2005-09-20 00:45:00 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include "mac_backtrace.h"
|
|
|
|
#endif
|
|
|
|
|
2004-11-17 00:01:58 +00:00
|
|
|
#ifdef __GLIBC__
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2006-04-10 06:20:02 +00:00
|
|
|
|
|
|
|
#include "app_ipc.h"
|
2004-03-24 20:53:43 +00:00
|
|
|
#include "error_numbers.h"
|
2004-03-24 07:39:24 +00:00
|
|
|
#include "filesys.h"
|
2004-02-29 00:50:05 +00:00
|
|
|
#include "util.h"
|
2006-04-10 06:20:02 +00:00
|
|
|
#include "parse.h"
|
|
|
|
#include "diagnostics.h"
|
2004-02-29 00:50:05 +00:00
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
|
2006-07-11 16:39:34 +00:00
|
|
|
#if defined(_WIN32) && defined(_MSC_VER)
|
2006-06-14 07:14:09 +00:00
|
|
|
|
|
|
|
static _CrtMemState start_snapshot;
|
|
|
|
static _CrtMemState finish_snapshot;
|
|
|
|
static _CrtMemState difference_snapshot;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-04-19 23:28:08 +00:00
|
|
|
static int diagnostics_initialized = false;
|
2006-04-10 06:20:02 +00:00
|
|
|
static int flags;
|
|
|
|
static char stdout_log[256];
|
|
|
|
static char stdout_archive[256];
|
|
|
|
static FILE* stdout_file;
|
|
|
|
static char stderr_log[256];
|
|
|
|
static char stderr_archive[256];
|
|
|
|
static FILE* stderr_file;
|
|
|
|
static char boinc_dir[256];
|
2008-05-21 05:09:15 +00:00
|
|
|
static char boinc_install_dir[256];
|
2006-04-15 22:45:02 +00:00
|
|
|
static int boinc_proxy_enabled;
|
|
|
|
static char boinc_proxy[256];
|
2006-04-10 06:20:02 +00:00
|
|
|
static char symstore[256];
|
2006-05-01 21:36:38 +00:00
|
|
|
static int aborted_via_gui;
|
2008-05-21 05:09:15 +00:00
|
|
|
static int max_stderr_file_size = 2048*1024;
|
|
|
|
static int max_stdout_file_size = 2048*1024;
|
2005-02-01 00:54:06 +00:00
|
|
|
|
2006-03-21 12:04:14 +00:00
|
|
|
|
2006-06-14 07:44:51 +00:00
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
|
|
|
|
|
|
|
// Trap ASSERTs and TRACEs from the CRT and spew them to stderr.
|
|
|
|
//
|
|
|
|
int __cdecl boinc_message_reporting(int reportType, char *szMsg, int *retVal){
|
|
|
|
(*retVal) = 0;
|
|
|
|
|
|
|
|
switch(reportType){
|
|
|
|
|
2008-06-23 19:51:07 +00:00
|
|
|
case _CRT_WARN:
|
|
|
|
case _CRT_ERROR:
|
2006-06-14 07:44:51 +00:00
|
|
|
|
2008-06-23 19:51:07 +00:00
|
|
|
if (flags & BOINC_DIAG_TRACETOSTDERR) {
|
|
|
|
fprintf(stderr, szMsg);
|
|
|
|
}
|
2006-06-14 07:44:51 +00:00
|
|
|
|
2008-06-23 19:51:07 +00:00
|
|
|
if (flags & BOINC_DIAG_TRACETOSTDOUT) {
|
|
|
|
fprintf(stdout, szMsg);
|
|
|
|
}
|
2006-06-14 07:44:51 +00:00
|
|
|
|
2008-06-23 19:51:07 +00:00
|
|
|
break;
|
|
|
|
case _CRT_ASSERT:
|
2006-06-14 07:44:51 +00:00
|
|
|
|
2008-06-23 19:51:07 +00:00
|
|
|
fprintf(stderr, "ASSERT: %s\n", szMsg);
|
2006-06-14 07:44:51 +00:00
|
|
|
|
2008-06-23 19:51:07 +00:00
|
|
|
(*retVal) = 1;
|
|
|
|
break;
|
2006-06-14 07:44:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _WIN32 && _DEBUG
|
|
|
|
|
|
|
|
|
2008-05-08 17:48:40 +00:00
|
|
|
// initialize the app diagnostic environment.
|
2004-02-29 00:50:05 +00:00
|
|
|
//
|
2004-03-25 22:24:33 +00:00
|
|
|
int boinc_init_diagnostics(int _flags) {
|
2010-05-07 22:25:58 +00:00
|
|
|
return diagnostics_init(
|
|
|
|
BOINC_DIAG_BOINCAPPLICATION | _flags,
|
|
|
|
BOINC_DIAG_STDOUT, BOINC_DIAG_STDERR
|
|
|
|
);
|
2005-02-01 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-08 17:48:40 +00:00
|
|
|
// initialize the graphic diagnostic environment.
|
2008-04-30 05:42:06 +00:00
|
|
|
//
|
|
|
|
int boinc_init_graphics_diagnostics(int _flags) {
|
2008-05-08 17:48:40 +00:00
|
|
|
return diagnostics_init(
|
|
|
|
BOINC_DIAG_BOINCAPPLICATION | _flags,
|
|
|
|
BOINC_DIAG_GFX_STDOUT, BOINC_DIAG_GFX_STDERR
|
|
|
|
);
|
2008-04-30 05:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
// Used to cleanup the diagnostics environment.
|
|
|
|
//
|
|
|
|
int boinc_finish_diag() {
|
2006-05-17 07:44:54 +00:00
|
|
|
return diagnostics_finish();
|
2005-02-01 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_install_signal_handlers() {
|
|
|
|
#ifdef _WIN32
|
2006-04-13 22:17:23 +00:00
|
|
|
SetUnhandledExceptionFilter(boinc_catch_signal);
|
2009-11-16 13:13:42 +00:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
2007-06-27 21:12:13 +00:00
|
|
|
_set_invalid_parameter_handler(boinc_catch_signal_invalid_parameter);
|
|
|
|
#endif
|
2005-02-01 00:54:06 +00:00
|
|
|
#else //_WIN32
|
|
|
|
|
|
|
|
// register handlers for fatal internal signals
|
|
|
|
// so that they get reported in stderr.txt
|
|
|
|
// Do NOT catch SIGQUIT because core client uses that to kill app
|
|
|
|
//
|
|
|
|
boinc_set_signal_handler(SIGILL, boinc_catch_signal);
|
|
|
|
boinc_set_signal_handler(SIGABRT, boinc_catch_signal);
|
|
|
|
boinc_set_signal_handler(SIGBUS, boinc_catch_signal);
|
|
|
|
boinc_set_signal_handler(SIGSEGV, boinc_catch_signal);
|
|
|
|
boinc_set_signal_handler(SIGSYS, boinc_catch_signal);
|
|
|
|
boinc_set_signal_handler(SIGPIPE, boinc_catch_signal);
|
|
|
|
#endif //_WIN32
|
2005-04-09 02:21:11 +00:00
|
|
|
return 0;
|
2005-02-01 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// initialize the diagnostics environment.
|
|
|
|
//
|
2005-02-16 23:17:43 +00:00
|
|
|
int diagnostics_init(
|
|
|
|
int _flags, const char* stdout_prefix, const char* stderr_prefix
|
|
|
|
) {
|
2006-05-01 21:36:38 +00:00
|
|
|
// Check to see if we have already been called
|
|
|
|
if (diagnostics_initialized) {
|
|
|
|
return ERR_INVALID_PARAM;
|
|
|
|
} else {
|
|
|
|
diagnostics_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup initial values
|
2004-03-25 22:24:33 +00:00
|
|
|
flags = _flags;
|
2005-02-01 00:54:06 +00:00
|
|
|
snprintf(stdout_log, sizeof(stdout_log), "%s.txt", stdout_prefix);
|
|
|
|
snprintf(stdout_archive, sizeof(stdout_archive), "%s.old", stdout_prefix);
|
|
|
|
snprintf(stderr_log, sizeof(stderr_log), "%s.txt", stderr_prefix);
|
|
|
|
snprintf(stderr_archive, sizeof(stderr_archive), "%s.old", stderr_prefix);
|
2006-04-10 06:20:02 +00:00
|
|
|
strcpy(boinc_dir, "");
|
2008-05-21 05:09:15 +00:00
|
|
|
strcpy(boinc_install_dir, "");
|
2006-04-15 22:45:02 +00:00
|
|
|
boinc_proxy_enabled = 0;
|
|
|
|
strcpy(boinc_proxy, "");
|
2006-04-10 06:20:02 +00:00
|
|
|
strcpy(symstore, "");
|
2005-02-01 00:54:06 +00:00
|
|
|
|
|
|
|
// Check for invalid parameter combinations
|
2006-04-13 22:17:23 +00:00
|
|
|
if ((flags & BOINC_DIAG_REDIRECTSTDERR) && (flags & BOINC_DIAG_REDIRECTSTDERROVERWRITE)) {
|
2005-02-01 00:54:06 +00:00
|
|
|
return ERR_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
|
2006-04-13 22:17:23 +00:00
|
|
|
if ((flags & BOINC_DIAG_REDIRECTSTDOUT) && (flags & BOINC_DIAG_REDIRECTSTDOUTOVERWRITE)) {
|
2005-02-01 00:54:06 +00:00
|
|
|
return ERR_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
|
2004-03-24 07:39:24 +00:00
|
|
|
|
|
|
|
// Archive any old stderr.txt and stdout.txt files, if requested
|
2006-04-13 22:17:23 +00:00
|
|
|
if (flags & BOINC_DIAG_ARCHIVESTDERR) {
|
2005-02-01 00:54:06 +00:00
|
|
|
boinc_copy(stderr_log, stderr_archive);
|
2004-03-24 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
2006-04-13 22:17:23 +00:00
|
|
|
if (flags & BOINC_DIAG_ARCHIVESTDOUT) {
|
|
|
|
boinc_copy(stdout_log, stdout_archive);
|
2004-03-24 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2006-04-13 22:17:23 +00:00
|
|
|
// Redirect stderr and/or stdout, if requested
|
|
|
|
//
|
|
|
|
if (flags & BOINC_DIAG_REDIRECTSTDERR) {
|
2005-02-01 00:54:06 +00:00
|
|
|
stderr_file = freopen(stderr_log, "a", stderr);
|
2006-04-13 22:17:23 +00:00
|
|
|
if (!stderr_file) {
|
2004-12-04 01:49:50 +00:00
|
|
|
return ERR_FOPEN;
|
2004-03-25 22:24:33 +00:00
|
|
|
}
|
2006-04-13 22:17:23 +00:00
|
|
|
setbuf(stderr_file, 0);
|
2004-03-24 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
2006-04-13 22:17:23 +00:00
|
|
|
if (flags & BOINC_DIAG_REDIRECTSTDERROVERWRITE) {
|
2005-02-01 00:54:06 +00:00
|
|
|
stderr_file = freopen(stderr_log, "w", stderr);
|
2011-03-07 21:45:37 +00:00
|
|
|
if (!stderr_file) {
|
2004-12-04 01:49:50 +00:00
|
|
|
return ERR_FOPEN;
|
2004-03-25 22:24:33 +00:00
|
|
|
}
|
2011-03-07 21:45:37 +00:00
|
|
|
setbuf(stderr_file, 0);
|
2004-03-24 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
2006-04-13 22:17:23 +00:00
|
|
|
if (flags & BOINC_DIAG_REDIRECTSTDOUT) {
|
2005-04-09 02:21:11 +00:00
|
|
|
stdout_file = freopen(stdout_log, "a", stdout);
|
2011-03-07 21:45:37 +00:00
|
|
|
if (!stdout_file) {
|
2004-12-04 01:49:50 +00:00
|
|
|
return ERR_FOPEN;
|
2004-03-25 22:24:33 +00:00
|
|
|
}
|
2004-03-24 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
2006-04-13 22:17:23 +00:00
|
|
|
if (flags & BOINC_DIAG_REDIRECTSTDOUTOVERWRITE) {
|
2005-04-09 02:21:11 +00:00
|
|
|
stdout_file = freopen(stdout_log, "w", stdout);
|
2011-03-07 21:45:37 +00:00
|
|
|
if (!stdout_file) {
|
2004-12-04 01:49:50 +00:00
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2004-03-24 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-21 12:04:14 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
#if defined(_DEBUG)
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2006-04-13 22:17:23 +00:00
|
|
|
_CrtSetReportHook(boinc_message_reporting);
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
if (flags & BOINC_DIAG_MEMORYLEAKCHECKENABLED) {
|
2006-04-13 22:17:23 +00:00
|
|
|
SET_CRT_DEBUG_FIELD(_CRTDBG_LEAK_CHECK_DF);
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
if (flags & BOINC_DIAG_HEAPCHECKENABLED) {
|
|
|
|
if (flags & BOINC_DIAG_HEAPCHECKEVERYALLOC) {
|
2006-04-13 22:17:23 +00:00
|
|
|
SET_CRT_DEBUG_FIELD(_CRTDBG_CHECK_ALWAYS_DF);
|
2006-05-01 21:36:38 +00:00
|
|
|
} else {
|
2006-04-13 22:17:23 +00:00
|
|
|
SET_CRT_DEBUG_FIELD(_CRTDBG_CHECK_EVERY_1024_DF);
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2006-05-17 07:44:54 +00:00
|
|
|
if (flags & BOINC_DIAG_BOINCAPPLICATION) {
|
|
|
|
if (flags & BOINC_DIAG_MEMORYLEAKCHECKENABLED) {
|
|
|
|
_CrtMemCheckpoint(&start_snapshot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-21 12:04:14 +00:00
|
|
|
#endif // defined(_DEBUG)
|
2006-05-17 07:44:54 +00:00
|
|
|
|
|
|
|
// Initialize the thread list structure
|
|
|
|
// The data for this structure should be set by
|
|
|
|
// boinc_init or boinc_init_graphics.
|
|
|
|
diagnostics_init_thread_list();
|
|
|
|
|
|
|
|
diagnostics_init_unhandled_exception_monitor();
|
|
|
|
|
|
|
|
diagnostics_init_message_monitor();
|
|
|
|
|
2006-03-21 12:04:14 +00:00
|
|
|
#endif // defined(_WIN32)
|
2004-03-24 07:39:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Install unhandled exception filters and signal traps.
|
2006-04-13 22:17:23 +00:00
|
|
|
if (BOINC_SUCCESS != boinc_install_signal_handlers()) {
|
2004-12-04 01:49:50 +00:00
|
|
|
return ERR_SIGNAL_OP;
|
|
|
|
}
|
2004-02-29 00:50:05 +00:00
|
|
|
|
2006-04-10 06:20:02 +00:00
|
|
|
|
2008-05-21 05:09:15 +00:00
|
|
|
// Store various pieces of inforation for future use.
|
2006-04-13 22:17:23 +00:00
|
|
|
if (flags & BOINC_DIAG_BOINCAPPLICATION) {
|
2006-04-10 06:20:02 +00:00
|
|
|
char buf[256];
|
2006-04-15 22:45:02 +00:00
|
|
|
char proxy_address[256];
|
|
|
|
int proxy_port;
|
2006-04-10 06:20:02 +00:00
|
|
|
MIOFILE mf;
|
|
|
|
FILE* p;
|
2008-05-21 05:09:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LONG lReturnValue;
|
|
|
|
HKEY hkSetupHive;
|
|
|
|
DWORD dwSize = 0;
|
|
|
|
#endif
|
2006-04-10 06:20:02 +00:00
|
|
|
|
2006-04-15 22:45:02 +00:00
|
|
|
strcpy(buf, "");
|
|
|
|
strcpy(proxy_address, "");
|
|
|
|
proxy_port = 0;
|
|
|
|
|
2009-01-13 23:06:02 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2006-04-10 06:20:02 +00:00
|
|
|
p = fopen(INIT_DATA_FILE, "r");
|
2009-01-13 23:06:02 +00:00
|
|
|
#else
|
|
|
|
p = FCGI::fopen(INIT_DATA_FILE, "r");
|
|
|
|
#endif
|
|
|
|
|
2006-05-18 04:36:41 +00:00
|
|
|
if (p) {
|
|
|
|
mf.init_file(p);
|
|
|
|
while(mf.fgets(buf, sizeof(buf))) {
|
|
|
|
if (match_tag(buf, "</app_init_data>")) break;
|
|
|
|
else if (parse_str(buf, "<boinc_dir>", boinc_dir, 256)) continue;
|
2008-06-09 19:28:35 +00:00
|
|
|
else if (parse_str(buf, "<symstore>", symstore, 256)) continue;
|
2006-05-18 04:36:41 +00:00
|
|
|
else if (match_tag(buf, "<use_http_proxy/>")) {
|
|
|
|
boinc_proxy_enabled = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (parse_str(buf, "<http_server_name>", proxy_address, 256)) continue;
|
|
|
|
else if (parse_int(buf, "<http_server_port>", proxy_port)) continue;
|
|
|
|
}
|
|
|
|
fclose(p);
|
|
|
|
}
|
2006-04-15 22:45:02 +00:00
|
|
|
|
2006-04-17 08:27:03 +00:00
|
|
|
if (boinc_proxy_enabled) {
|
|
|
|
int buffer_used = snprintf(boinc_proxy, sizeof(boinc_proxy), "%s:%d", proxy_address, proxy_port);
|
|
|
|
if ((sizeof(boinc_proxy) == buffer_used) || (-1 == buffer_used)) {
|
2006-05-24 23:56:41 +00:00
|
|
|
boinc_proxy[sizeof(boinc_proxy)-1] = '\0';
|
2006-04-15 22:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-21 05:09:15 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// Lookup the location of where BOINC was installed to and store
|
|
|
|
// that for future use.
|
|
|
|
lReturnValue = RegOpenKeyEx(
|
|
|
|
HKEY_LOCAL_MACHINE,
|
|
|
|
_T("SOFTWARE\\Space Sciences Laboratory, U.C. Berkeley\\BOINC Setup"),
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
|
|
|
&hkSetupHive
|
|
|
|
);
|
|
|
|
if (lReturnValue == ERROR_SUCCESS) {
|
|
|
|
// How large does our buffer need to be?
|
|
|
|
dwSize = sizeof(boinc_install_dir);
|
|
|
|
|
|
|
|
lReturnValue = RegQueryValueEx(
|
|
|
|
hkSetupHive,
|
|
|
|
_T("INSTALLDIR"),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)&boinc_install_dir,
|
|
|
|
&dwSize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hkSetupHive) RegCloseKey(hkSetupHive);
|
|
|
|
#endif
|
2006-04-10 06:20:02 +00:00
|
|
|
}
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
return BOINC_SUCCESS;
|
|
|
|
}
|
2006-04-19 23:28:08 +00:00
|
|
|
|
|
|
|
|
2006-05-17 07:44:54 +00:00
|
|
|
// Cleanup the diagnostic framework before dumping any memory leaks.
|
|
|
|
//
|
|
|
|
int diagnostics_finish() {
|
|
|
|
|
2008-05-21 05:09:15 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-17 07:44:54 +00:00
|
|
|
|
|
|
|
// Shutdown the message monitor thread and handles
|
|
|
|
diagnostics_finish_message_monitor();
|
|
|
|
|
|
|
|
// Shutdown the unhandled exception filter thread and handles
|
|
|
|
diagnostics_finish_unhandled_exception_monitor();
|
|
|
|
|
|
|
|
// Cleanup internal thread list structures and free up any
|
|
|
|
// used memory.
|
|
|
|
diagnostics_finish_thread_list();
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
|
|
|
// Only perform the memory leak dump if it is a boinc application
|
|
|
|
// and not the BOINC Manager, BOINC Core Client, or BOINC
|
|
|
|
// Screen saver since they'll check on close.
|
|
|
|
if (flags & BOINC_DIAG_BOINCAPPLICATION) {
|
|
|
|
if (flags & BOINC_DIAG_MEMORYLEAKCHECKENABLED) {
|
|
|
|
_CrtMemCheckpoint(&finish_snapshot);
|
|
|
|
if (_CrtMemDifference(&difference_snapshot, &start_snapshot, &finish_snapshot)) {
|
|
|
|
|
|
|
|
fprintf( stderr, "\n\n");
|
|
|
|
fprintf( stderr, "**********\n");
|
|
|
|
fprintf( stderr, "**********\n");
|
|
|
|
fprintf( stderr, "\n");
|
|
|
|
fprintf( stderr, "Memory Leaks Detected!!!\n");
|
|
|
|
fprintf( stderr, "\n");
|
|
|
|
fprintf( stderr, "Memory Statistics:\n");
|
|
|
|
_CrtMemDumpStatistics(&difference_snapshot);
|
|
|
|
fprintf( stderr, "\n");
|
|
|
|
_CrtMemDumpAllObjectsSince(&difference_snapshot);
|
|
|
|
fprintf( stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(_DEBUG)
|
|
|
|
#endif // defined(_WIN32)
|
|
|
|
|
|
|
|
// Set initalization flag to false.
|
|
|
|
diagnostics_initialized = false;
|
|
|
|
|
|
|
|
return BOINC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// has the diagnostics library been initialized?
|
|
|
|
//
|
|
|
|
int diagnostics_is_initialized(){
|
|
|
|
return diagnostics_initialized;
|
|
|
|
}
|
2006-04-19 23:28:08 +00:00
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
// return true if the specified flag is set.
|
|
|
|
//
|
|
|
|
int diagnostics_is_flag_set(int _flags) {
|
|
|
|
return flags & _flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return the location of the BOINC directory.
|
|
|
|
//
|
|
|
|
char* diagnostics_get_boinc_dir() {
|
|
|
|
return boinc_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-21 05:09:15 +00:00
|
|
|
// return the location of the BOINC install directory.
|
|
|
|
//
|
|
|
|
char* diagnostics_get_boinc_install_dir() {
|
|
|
|
return boinc_install_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-24 23:56:41 +00:00
|
|
|
// return the location of the symbol store.
|
2006-05-01 21:36:38 +00:00
|
|
|
//
|
|
|
|
char* diagnostics_get_symstore() {
|
|
|
|
return symstore;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-24 23:56:41 +00:00
|
|
|
// store the location of the symbol store.
|
|
|
|
//
|
|
|
|
int diagnostics_set_symstore(char* project_symstore) {
|
|
|
|
if (!strlen(symstore)) {
|
|
|
|
int buffer_used = snprintf(symstore, sizeof(symstore), "%s", project_symstore);
|
|
|
|
if ((sizeof(symstore) == buffer_used) || (-1 == buffer_used)) {
|
|
|
|
symstore[sizeof(symstore)-1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// do we need to worry about a proxy server?
|
|
|
|
//
|
|
|
|
int diagnostics_is_proxy_enabled() {
|
|
|
|
return boinc_proxy_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// proxy server address and port
|
|
|
|
//
|
|
|
|
char* diagnostics_get_proxy() {
|
|
|
|
return boinc_proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the value of the flag
|
2006-05-08 18:35:56 +00:00
|
|
|
int diagnostics_set_aborted_via_gui() {
|
2006-05-01 21:36:38 +00:00
|
|
|
aborted_via_gui = 1;
|
|
|
|
return 0;
|
2004-02-29 00:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// Return the value of he flag
|
|
|
|
int diagnostics_is_aborted_via_gui() {
|
|
|
|
return aborted_via_gui;
|
|
|
|
}
|
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
// Cycle the log files at regular events.
|
2004-02-29 00:50:05 +00:00
|
|
|
//
|
2005-02-01 00:54:06 +00:00
|
|
|
int diagnostics_cycle_logs() {
|
|
|
|
double f_size;
|
2004-02-29 00:50:05 +00:00
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2004-05-31 18:13:01 +00:00
|
|
|
|
2005-02-01 00:54:06 +00:00
|
|
|
// If the stderr.txt or stdout.txt files are too big, cycle them
|
2004-05-31 18:13:01 +00:00
|
|
|
//
|
2005-02-01 00:54:06 +00:00
|
|
|
if (flags & BOINC_DIAG_REDIRECTSTDERR) {
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
// OS/2 can't stat() open files!
|
|
|
|
struct stat sbuf;
|
|
|
|
fstat(fileno(stderr_file), &sbuf);
|
|
|
|
f_size = (double)sbuf.st_size;
|
|
|
|
#else
|
2005-02-01 00:54:06 +00:00
|
|
|
file_size(stderr_log, f_size);
|
2005-12-02 22:29:35 +00:00
|
|
|
#endif
|
2007-10-24 22:48:47 +00:00
|
|
|
if (f_size > max_stderr_file_size) {
|
2008-05-09 05:57:11 +00:00
|
|
|
if (NULL == stderr_file) return ERR_FOPEN;
|
2006-04-13 22:17:23 +00:00
|
|
|
fclose(stderr_file);
|
2005-02-01 00:54:06 +00:00
|
|
|
boinc_copy(stderr_log, stderr_archive);
|
2006-04-13 22:17:23 +00:00
|
|
|
stderr_file = freopen(stderr_log, "w", stderr);
|
|
|
|
if (NULL == stderr_file) return ERR_FOPEN;
|
2005-02-01 00:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & BOINC_DIAG_REDIRECTSTDOUT) {
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
// OS/2 can't stat() open files!
|
|
|
|
struct stat sbuf;
|
|
|
|
fstat(fileno(stdout_file), &sbuf);
|
|
|
|
f_size = (double)sbuf.st_size;
|
|
|
|
#else
|
2005-02-01 00:54:06 +00:00
|
|
|
file_size(stdout_log, f_size);
|
2005-12-02 22:29:35 +00:00
|
|
|
#endif
|
2007-10-24 22:48:47 +00:00
|
|
|
if (f_size > max_stdout_file_size) {
|
2008-05-09 05:57:11 +00:00
|
|
|
if (NULL == stdout_file) return ERR_FOPEN;
|
2006-04-13 22:17:23 +00:00
|
|
|
fclose(stdout_file);
|
|
|
|
boinc_copy(stdout_log, stdout_archive);
|
|
|
|
stdout_file = freopen(stdout_log, "w", stdout);
|
|
|
|
if (NULL == stdout_file) return ERR_FOPEN;
|
2005-02-01 00:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-09 02:21:11 +00:00
|
|
|
return BOINC_SUCCESS;
|
2004-02-29 00:50:05 +00:00
|
|
|
}
|
|
|
|
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2004-02-29 00:50:05 +00:00
|
|
|
// Diagnostics for POSIX Compatible systems.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
|
2004-03-24 19:47:51 +00:00
|
|
|
|
|
|
|
// Set a signal handler only if it is not currently ignored
|
|
|
|
//
|
2009-02-26 00:23:23 +00:00
|
|
|
extern "C" void boinc_set_signal_handler(int sig, void(*handler)(int)) {
|
2004-03-17 04:56:46 +00:00
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
struct sigaction temp;
|
|
|
|
sigaction(sig, NULL, &temp);
|
|
|
|
if (temp.sa_handler != SIG_IGN) {
|
2004-03-24 19:47:51 +00:00
|
|
|
temp.sa_handler = handler;
|
2005-04-09 02:21:11 +00:00
|
|
|
// sigemptyset(&temp.sa_mask);
|
2004-03-24 07:39:24 +00:00
|
|
|
sigaction(sig, &temp, NULL);
|
2004-03-17 04:56:46 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void (*temp)(int);
|
|
|
|
temp = signal(sig, boinc_catch_signal);
|
|
|
|
if (temp == SIG_IGN) {
|
2004-03-24 07:39:24 +00:00
|
|
|
signal(sig, SIG_IGN);
|
2004-03-17 04:56:46 +00:00
|
|
|
}
|
2004-03-24 19:47:51 +00:00
|
|
|
#endif /* HAVE_SIGACTION */
|
2004-03-17 04:56:46 +00:00
|
|
|
}
|
2004-02-29 00:50:05 +00:00
|
|
|
|
2004-03-24 19:47:51 +00:00
|
|
|
|
|
|
|
// Set a signal handler even if it is currently ignored
|
|
|
|
//
|
2004-03-25 22:24:33 +00:00
|
|
|
void boinc_set_signal_handler_force(int sig, void(*handler)(int)) {
|
2004-03-24 19:47:51 +00:00
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
struct sigaction temp;
|
|
|
|
sigaction(sig, NULL, &temp);
|
|
|
|
temp.sa_handler = handler;
|
2005-01-08 01:17:51 +00:00
|
|
|
// sigemptyset(&temp.sa_mask);
|
2004-03-24 19:47:51 +00:00
|
|
|
sigaction(sig, &temp, NULL);
|
|
|
|
#else
|
|
|
|
void (*temp)(int);
|
2004-03-25 18:59:43 +00:00
|
|
|
temp = signal(sig, boinc_catch_signal);
|
2004-03-24 19:47:51 +00:00
|
|
|
signal(sig, SIG_IGN);
|
|
|
|
#endif /* HAVE_SIGACTION */
|
|
|
|
}
|
|
|
|
|
2008-06-23 19:51:07 +00:00
|
|
|
// exit code to use if signalled; can be changed
|
|
|
|
static int signal_exit_code = EXIT_SIGNAL;
|
|
|
|
|
|
|
|
void set_signal_exit_code(int x) {
|
|
|
|
signal_exit_code = x;
|
|
|
|
}
|
2004-03-24 19:47:51 +00:00
|
|
|
|
2004-03-25 22:24:33 +00:00
|
|
|
void boinc_catch_signal(int signal) {
|
2004-02-29 00:50:05 +00:00
|
|
|
switch(signal) {
|
2008-06-23 19:51:07 +00:00
|
|
|
case SIGHUP: fprintf(stderr, "SIGHUP: terminal line hangup\n");
|
|
|
|
return;
|
|
|
|
case SIGINT: fprintf(stderr, "SIGINT: interrupt program\n"); break;
|
|
|
|
case SIGILL: fprintf(stderr, "SIGILL: illegal instruction\n"); break;
|
|
|
|
case SIGABRT: fprintf(stderr, "SIGABRT: abort called\n"); break;
|
2009-05-27 23:36:55 +00:00
|
|
|
#if SIGBUS != SIGSEGV
|
|
|
|
// in case SIGBUS == SIGSEGV (e.g., Haiku)
|
2008-06-23 19:51:07 +00:00
|
|
|
case SIGBUS: fprintf(stderr, "SIGBUS: bus error\n"); break;
|
2009-05-27 23:36:55 +00:00
|
|
|
#endif
|
2008-06-23 19:51:07 +00:00
|
|
|
case SIGSEGV: fprintf(stderr, "SIGSEGV: segmentation violation\n"); break;
|
|
|
|
case SIGSYS: fprintf(stderr, "SIGSYS: system call given invalid argument\n"); break;
|
|
|
|
case SIGPIPE: fprintf(stderr, "SIGPIPE: write on a pipe with no reader\n");
|
|
|
|
return;
|
|
|
|
default: fprintf(stderr, "unknown signal %d\n", signal); break;
|
2004-02-29 00:50:05 +00:00
|
|
|
}
|
2004-11-15 01:58:30 +00:00
|
|
|
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
void *array[64];
|
|
|
|
size_t size;
|
|
|
|
size = backtrace (array, 64);
|
2009-08-31 23:04:01 +00:00
|
|
|
// Anything that calls malloc here (i.e *printf()) will probably fail
|
|
|
|
// so we'll do it the hard way.
|
|
|
|
write(fileno(stderr),"Stack trace (",strlen("Stack trace ("));
|
|
|
|
char mbuf[10];
|
|
|
|
char *p=mbuf+9;
|
|
|
|
int i=size;
|
|
|
|
*(p--)=0;
|
|
|
|
while (i) {
|
|
|
|
*(p--)=i%10+'0';
|
|
|
|
i/=10;
|
|
|
|
}
|
|
|
|
write(fileno(stderr),p+1,strlen(p+1));
|
|
|
|
write(fileno(stderr)," frames):",strlen(" frames):"));
|
|
|
|
mbuf[0]=10;
|
|
|
|
write(fileno(stderr),mbuf,1);
|
2004-11-15 01:58:30 +00:00
|
|
|
backtrace_symbols_fd(array, size, fileno(stderr));
|
|
|
|
#endif
|
|
|
|
|
2005-09-20 00:45:00 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
PrintBacktrace();
|
|
|
|
#endif
|
|
|
|
|
2004-02-29 00:50:05 +00:00
|
|
|
fprintf(stderr, "\nExiting...\n");
|
2008-06-23 19:51:07 +00:00
|
|
|
_exit(signal_exit_code);
|
2004-02-29 00:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2006-06-07 08:56:55 +00:00
|
|
|
//
|
|
|
|
// Diagnostics Routines common to all Platforms
|
|
|
|
//
|
|
|
|
|
2006-06-14 07:44:51 +00:00
|
|
|
// Converts the BOINCTRACE macro into a single string and report it
|
|
|
|
// to the CRT so it can be reported via the normal means.
|
|
|
|
//
|
|
|
|
void boinc_trace(const char *pszFormat, ...) {
|
|
|
|
static char szBuffer[4096];
|
|
|
|
static char szDate[64];
|
|
|
|
static char szTime[64];
|
|
|
|
|
|
|
|
// Trace messages should only be reported if running as a standalone
|
|
|
|
// application or told too.
|
|
|
|
if ((flags & BOINC_DIAG_TRACETOSTDERR) ||
|
|
|
|
(flags & BOINC_DIAG_TRACETOSTDOUT)) {
|
|
|
|
|
|
|
|
memset(szBuffer, 0, sizeof(szBuffer));
|
|
|
|
memset(szDate, 0, sizeof(szDate));
|
|
|
|
memset(szTime, 0, sizeof(szTime));
|
|
|
|
|
2006-06-14 16:25:57 +00:00
|
|
|
#ifdef _WIN32
|
2006-06-14 07:44:51 +00:00
|
|
|
strdate(szDate);
|
|
|
|
strtime(szTime);
|
2009-02-11 10:36:16 +00:00
|
|
|
#else
|
|
|
|
time_t t;
|
|
|
|
char *theCR;
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
strcpy(szTime, asctime(localtime(&t)));
|
|
|
|
theCR = strrchr(szTime, '\n');
|
|
|
|
if (theCR) *theCR = '\0';
|
|
|
|
theCR = strrchr(szTime, '\r');
|
|
|
|
if (theCR) *theCR = '\0';
|
2006-06-14 16:25:57 +00:00
|
|
|
#endif
|
2006-06-14 07:44:51 +00:00
|
|
|
|
|
|
|
va_list ptr;
|
|
|
|
va_start(ptr, pszFormat);
|
|
|
|
|
|
|
|
vsnprintf(szBuffer, sizeof(szBuffer), pszFormat, ptr);
|
|
|
|
|
|
|
|
va_end(ptr);
|
|
|
|
|
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
|
|
|
_CrtDbgReport(_CRT_WARN, NULL, NULL, NULL, "[%s %s] TRACE [%d]: %s", szDate, szTime, GetCurrentThreadId(), szBuffer);
|
|
|
|
#else
|
|
|
|
if (flags & BOINC_DIAG_TRACETOSTDERR) {
|
2006-06-14 16:20:11 +00:00
|
|
|
#ifdef _WIN32
|
2006-06-14 07:44:51 +00:00
|
|
|
fprintf(stderr, "[%s %s] TRACE [%d]: %s\n", szDate, szTime, GetCurrentThreadId(), szBuffer);
|
2006-06-14 16:20:11 +00:00
|
|
|
#else
|
2009-02-11 10:36:16 +00:00
|
|
|
fprintf(stderr, "[%s] TRACE: %s\n", szTime, szBuffer);
|
2006-06-14 16:20:11 +00:00
|
|
|
#endif
|
2006-06-14 07:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & BOINC_DIAG_TRACETOSTDOUT) {
|
2006-06-14 16:20:11 +00:00
|
|
|
#ifdef _WIN32
|
2006-06-14 07:44:51 +00:00
|
|
|
fprintf(stdout, "[%s %s] TRACE [%d]: %s\n", szDate, szTime, GetCurrentThreadId(), szBuffer);
|
2006-06-14 16:20:11 +00:00
|
|
|
#else
|
2009-02-11 10:36:16 +00:00
|
|
|
fprintf(stderr, "[%s] TRACE: %s\n", szTime, szBuffer);
|
2006-06-14 16:20:11 +00:00
|
|
|
#endif
|
2006-06-14 07:44:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-07 08:56:55 +00:00
|
|
|
// Converts the BOINCINFO macro into a single string and report it
|
|
|
|
// to stderr so it can be reported via the normal means.
|
|
|
|
//
|
2007-01-07 23:35:36 +00:00
|
|
|
#ifndef BOINC_INFOMSGS
|
|
|
|
void boinc_info(const char* /*pszFormat*/, ... ){ return; }
|
|
|
|
#else
|
|
|
|
void boinc_info(const char* pszFormat, ...){
|
2006-06-07 08:56:55 +00:00
|
|
|
static char szBuffer[4096];
|
|
|
|
static char szDate[64];
|
|
|
|
static char szTime[64];
|
|
|
|
|
|
|
|
memset(szBuffer, 0, sizeof(szBuffer));
|
2006-06-07 08:58:37 +00:00
|
|
|
memset(szDate, 0, sizeof(szDate));
|
|
|
|
memset(szTime, 0, sizeof(szTime));
|
2006-06-07 08:56:55 +00:00
|
|
|
|
|
|
|
strdate(szDate);
|
|
|
|
strtime(szTime);
|
|
|
|
|
|
|
|
va_list ptr;
|
|
|
|
va_start(ptr, pszFormat);
|
|
|
|
|
|
|
|
vsnprintf(szBuffer, sizeof(szBuffer), pszFormat, ptr);
|
|
|
|
|
|
|
|
va_end(ptr);
|
|
|
|
|
2006-06-14 07:14:09 +00:00
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
|
|
|
_CrtDbgReport(_CRT_WARN, NULL, NULL, NULL, "[%s %s] BOINCMSG: %s\n", szDate, szTime, szBuffer);
|
|
|
|
#else
|
|
|
|
if (flags & BOINC_DIAG_TRACETOSTDERR) {
|
|
|
|
fprintf(stderr, "[%s %s] BOINCMSG: %s\n", szDate, szTime, szBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & BOINC_DIAG_TRACETOSTDOUT) {
|
|
|
|
fprintf(stdout, "[%s %s] BOINCMSG: %s\n", szDate, szTime, szBuffer);
|
|
|
|
}
|
|
|
|
#endif
|
2006-06-07 08:56:55 +00:00
|
|
|
}
|
2007-01-07 23:35:36 +00:00
|
|
|
#endif
|
2006-06-07 08:56:55 +00:00
|
|
|
|
2007-10-24 22:48:47 +00:00
|
|
|
void diagnostics_set_max_file_sizes(int stdout_size, int stderr_size) {
|
|
|
|
if (stdout_size) max_stdout_file_size = stdout_size;
|
|
|
|
if (stderr_size) max_stderr_file_size = stderr_size;
|
|
|
|
}
|
2006-06-07 08:56:55 +00:00
|
|
|
|