2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-05-01 21:36:38 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-05-01 21:36:38 +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.
|
2006-05-01 21:36:38 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-05-01 21:36:38 +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/>.
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
// Stuff related to catching SEH exceptions, monitoring threads, and trapping
|
|
|
|
// debugger messages; used by both core client and by apps.
|
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2006-05-01 21:36:38 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
2006-05-01 21:36: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
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
#ifndef __CYGWIN32__
|
2006-05-01 21:36:38 +00:00
|
|
|
#include "stackwalker_win.h"
|
|
|
|
#endif
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
#include "version.h"
|
2006-05-01 21:36:38 +00:00
|
|
|
#include "diagnostics.h"
|
2006-05-08 18:35:56 +00:00
|
|
|
#include "diagnostics_win.h"
|
2006-05-01 21:36:38 +00:00
|
|
|
#include "error_numbers.h"
|
2007-02-21 17:06:11 +00:00
|
|
|
#include "str_util.h"
|
2006-05-01 21:36:38 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// NtQuerySystemInformation
|
|
|
|
typedef NTSTATUS (WINAPI *tNTQSI)(
|
|
|
|
ULONG SystemInformationClass,
|
|
|
|
PVOID SystemInformation,
|
|
|
|
ULONG SystemInformationLength,
|
|
|
|
PULONG ReturnLength
|
|
|
|
);
|
|
|
|
|
2006-05-10 17:56:34 +00:00
|
|
|
// IsDebuggerPresent
|
|
|
|
typedef BOOL (WINAPI *tIDP)();
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// CreateToolhelp32Snapshot
|
|
|
|
typedef HANDLE (WINAPI *tCT32S)(DWORD dwFlags, DWORD dwProcessID);
|
|
|
|
// Thread32First
|
|
|
|
typedef BOOL (WINAPI *tT32F)(HANDLE hSnapshot, LPTHREADENTRY32 lpte);
|
|
|
|
// Thread32Next
|
|
|
|
typedef BOOL (WINAPI *tT32N)(HANDLE hSnapshot, LPTHREADENTRY32 lpte);
|
|
|
|
// OpenThread
|
|
|
|
typedef HANDLE (WINAPI *tOT)(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId);
|
|
|
|
|
|
|
|
|
2006-05-11 06:15:08 +00:00
|
|
|
// Look in the registry for the specified value user the BOINC diagnostics
|
|
|
|
// hive.
|
2009-04-23 03:40:49 +00:00
|
|
|
BOOL diagnostics_get_registry_value(LPCSTR lpName, LPDWORD lpdwType, LPDWORD lpdwSize, LPBYTE lpData) {
|
2006-05-11 06:15:08 +00:00
|
|
|
LONG lRetVal;
|
|
|
|
HKEY hKey;
|
|
|
|
|
|
|
|
// Detect platform information
|
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(osvi);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
|
|
|
|
if (VER_PLATFORM_WIN32_WINDOWS == osvi.dwPlatformId) {
|
2009-04-23 03:40:49 +00:00
|
|
|
lRetVal = RegOpenKeyExA(
|
2006-05-11 06:15:08 +00:00
|
|
|
HKEY_LOCAL_MACHINE,
|
2009-04-23 03:40:49 +00:00
|
|
|
"SOFTWARE\\Space Sciences Laboratory, U.C. Berkeley\\BOINC Diagnostics",
|
2008-01-31 18:34:51 +00:00
|
|
|
(DWORD)NULL,
|
2006-05-11 06:15:08 +00:00
|
|
|
KEY_READ,
|
|
|
|
&hKey
|
|
|
|
);
|
|
|
|
if (lRetVal != ERROR_SUCCESS) return FALSE;
|
|
|
|
} else {
|
2009-04-23 03:40:49 +00:00
|
|
|
lRetVal = RegOpenKeyExA(
|
2006-05-11 06:15:08 +00:00
|
|
|
HKEY_CURRENT_USER,
|
2009-04-23 03:40:49 +00:00
|
|
|
"SOFTWARE\\Space Sciences Laboratory, U.C. Berkeley\\BOINC Diagnostics",
|
2008-01-31 18:34:51 +00:00
|
|
|
(DWORD)NULL,
|
2006-05-11 06:15:08 +00:00
|
|
|
KEY_READ,
|
|
|
|
&hKey
|
|
|
|
);
|
|
|
|
if (lRetVal != ERROR_SUCCESS) return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
lRetVal = RegQueryValueExA(hKey, lpName, NULL, lpdwType, lpData, lpdwSize);
|
2006-05-11 06:15:08 +00:00
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
|
|
|
|
return (lRetVal == ERROR_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 23:01:36 +00:00
|
|
|
// Provide a structure to store process measurements at the time of a
|
|
|
|
// crash.
|
|
|
|
typedef struct _BOINC_PROCESSENTRY {
|
|
|
|
DWORD process_id;
|
|
|
|
VM_COUNTERS vm_counters;
|
|
|
|
IO_COUNTERS io_counters;
|
|
|
|
} BOINC_PROCESSENTRY, *PBOINC_PROCESSENTRY;
|
|
|
|
|
|
|
|
static BOINC_PROCESSENTRY diagnostics_process;
|
|
|
|
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// Provide a set of API's which can be used to display more friendly
|
|
|
|
// information about each thread. These should also be used to
|
|
|
|
// dump the callstacks for each executing thread when an unhandled
|
|
|
|
// SEH exception is thrown.
|
|
|
|
//
|
|
|
|
|
|
|
|
// This structure is used to keep track of stuff nessassary
|
|
|
|
// to dump backtraces for all threads during an abort or
|
|
|
|
// crash. This is platform specific in nature since it
|
|
|
|
// depends on the OS datatypes.
|
|
|
|
typedef struct _BOINC_THREADLISTENTRY {
|
2006-05-08 18:35:56 +00:00
|
|
|
DWORD thread_id;
|
|
|
|
HANDLE thread_handle;
|
|
|
|
BOOL crash_suspend_exempt;
|
|
|
|
FLOAT crash_kernel_time;
|
|
|
|
FLOAT crash_user_time;
|
|
|
|
FLOAT crash_wait_time;
|
|
|
|
INT crash_priority;
|
|
|
|
INT crash_base_priority;
|
|
|
|
INT crash_state;
|
|
|
|
INT crash_wait_reason;
|
|
|
|
PEXCEPTION_POINTERS crash_exception_record;
|
2006-05-25 11:14:27 +00:00
|
|
|
char crash_message[1024];
|
2006-05-01 21:36:38 +00:00
|
|
|
} BOINC_THREADLISTENTRY, *PBOINC_THREADLISTENTRY;
|
|
|
|
|
|
|
|
static std::vector<PBOINC_THREADLISTENTRY> diagnostics_threads;
|
|
|
|
static HANDLE hThreadListSync;
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Initialize the thread list entry.
|
|
|
|
int diagnostics_init_thread_entry(PBOINC_THREADLISTENTRY entry) {
|
|
|
|
entry->thread_id = 0;
|
|
|
|
entry->thread_handle = 0;
|
|
|
|
entry->crash_suspend_exempt = FALSE;
|
|
|
|
entry->crash_kernel_time = 0.0;
|
|
|
|
entry->crash_user_time = 0.0;
|
|
|
|
entry->crash_wait_time = 0.0;
|
|
|
|
entry->crash_priority = 0;
|
|
|
|
entry->crash_base_priority = 0;
|
|
|
|
entry->crash_state = 0;
|
|
|
|
entry->crash_wait_reason = 0;
|
|
|
|
entry->crash_exception_record = NULL;
|
2006-05-25 11:14:27 +00:00
|
|
|
strncpy(entry->crash_message, "", sizeof(entry->crash_message));
|
2006-05-08 18:35:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// Initialize the thread list, which means empty it if anything is
|
|
|
|
// in it.
|
|
|
|
int diagnostics_init_thread_list() {
|
2006-05-23 23:01:36 +00:00
|
|
|
int retval = 0;
|
2006-05-17 07:44:54 +00:00
|
|
|
size_t i;
|
|
|
|
size_t size;
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
// Create a Mutex that can be used to syncronize data access
|
|
|
|
// to the global thread list.
|
|
|
|
hThreadListSync = CreateMutex(NULL, TRUE, NULL);
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!hThreadListSync) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_thread_list(): Creating hThreadListSync failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
retval = GetLastError();
|
|
|
|
} else {
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-23 23:01:36 +00:00
|
|
|
size = diagnostics_threads.size();
|
|
|
|
for (i=0; i<size; i++) {
|
|
|
|
delete diagnostics_threads[i];
|
|
|
|
}
|
|
|
|
diagnostics_threads.clear();
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-23 23:01:36 +00:00
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2006-05-23 23:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
2006-05-08 18:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Finish the thread list, which means empty it if anything is
|
|
|
|
// in it.
|
|
|
|
int diagnostics_finish_thread_list() {
|
2006-05-17 07:44:54 +00:00
|
|
|
size_t i;
|
|
|
|
size_t size;
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
2006-05-17 07:44:54 +00:00
|
|
|
size = diagnostics_threads.size();
|
|
|
|
for (i=0; i<size; i++) {
|
2006-05-17 22:29:37 +00:00
|
|
|
delete diagnostics_threads[i];
|
2006-05-08 18:35:56 +00:00
|
|
|
}
|
2006-05-17 07:44:54 +00:00
|
|
|
diagnostics_threads.clear();
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
2006-05-08 18:35:56 +00:00
|
|
|
CloseHandle(hThreadListSync);
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Return a pointer to the thread entry.
|
|
|
|
//
|
|
|
|
PBOINC_THREADLISTENTRY diagnostics_find_thread_entry(DWORD dwThreadId) {
|
|
|
|
PBOINC_THREADLISTENTRY pThread = NULL;
|
|
|
|
UINT uiIndex = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
size = diagnostics_threads.size();
|
|
|
|
for (uiIndex = 0; uiIndex < size; uiIndex++) {
|
|
|
|
if (diagnostics_threads[uiIndex]) {
|
|
|
|
if (dwThreadId == diagnostics_threads[uiIndex]->thread_id) {
|
|
|
|
pThread = diagnostics_threads[uiIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
return pThread;
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Enumerate the running threads in the process space and add them to
|
|
|
|
// the list. This is the most compatible implementation.
|
|
|
|
int diagnostics_update_thread_list_9X() {
|
2006-05-01 21:36:38 +00:00
|
|
|
HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE hThread = NULL;
|
2006-05-17 07:44:54 +00:00
|
|
|
HMODULE hKernel32Lib = NULL;
|
2006-05-01 21:36:38 +00:00
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
tCT32S pCT32S = NULL;
|
|
|
|
tT32F pT32F = NULL;
|
|
|
|
tT32N pT32N = NULL;
|
|
|
|
tOT pOT = NULL;
|
|
|
|
THREADENTRY32 te32;
|
|
|
|
|
|
|
|
// Which version of the data structure are we using.
|
|
|
|
te32.dwSize = sizeof(te32);
|
|
|
|
|
|
|
|
// Dynamically link to the proper function pointers.
|
2009-04-23 03:40:49 +00:00
|
|
|
hKernel32Lib = GetModuleHandleA("kernel32.dll");
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-17 07:44:54 +00:00
|
|
|
pCT32S = (tCT32S) GetProcAddress( hKernel32Lib, "CreateToolhelp32Snapshot" );
|
|
|
|
pT32F = (tT32F) GetProcAddress( hKernel32Lib, "Thread32First" );
|
|
|
|
pT32N = (tT32N) GetProcAddress( hKernel32Lib, "Thread32Next" );
|
|
|
|
pOT = (tOT) GetProcAddress( hKernel32Lib, "OpenThread" );
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
if (!pCT32S || !pT32F || !pT32N) {
|
|
|
|
return ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take a snapshot of all running threads
|
|
|
|
hThreadSnap = pCT32S(TH32CS_SNAPTHREAD, 0);
|
|
|
|
if( hThreadSnap == INVALID_HANDLE_VALUE ) {
|
|
|
|
return GetLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve information about the first thread,
|
|
|
|
// and exit if unsuccessful
|
|
|
|
if( !pT32F( hThreadSnap, &te32 ) ) {
|
|
|
|
CloseHandle( hThreadSnap );
|
|
|
|
return GetLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
|
|
|
// Now walk the thread list of the system,
|
|
|
|
// and display information about each thread
|
|
|
|
// associated with the specified process
|
|
|
|
do {
|
|
|
|
if( te32.th32OwnerProcessID == GetCurrentProcessId() ) {
|
2006-05-08 18:35:56 +00:00
|
|
|
pThreadEntry = diagnostics_find_thread_entry(te32.th32ThreadID);
|
2006-05-01 21:36:38 +00:00
|
|
|
if (!pThreadEntry) {
|
|
|
|
pThreadEntry = new BOINC_THREADLISTENTRY;
|
2006-05-08 18:35:56 +00:00
|
|
|
diagnostics_init_thread_entry(pThreadEntry);
|
2006-05-01 21:36:38 +00:00
|
|
|
pThreadEntry->thread_id = te32.th32ThreadID;
|
|
|
|
if (pOT) {
|
|
|
|
hThread = pOT(
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
FALSE,
|
|
|
|
te32.th32ThreadID
|
|
|
|
);
|
|
|
|
pThreadEntry->thread_handle = hThread;
|
|
|
|
}
|
|
|
|
diagnostics_threads.push_back(pThreadEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( pT32N(hThreadSnap, &te32 ) );
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
|
|
|
|
|
|
|
CloseHandle(hThreadSnap);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Use the native NT API to get all the process and thread information
|
|
|
|
// about the current process. This isn't a fully documented API but
|
|
|
|
// enough information exists that we can rely on it for the known
|
|
|
|
// Windows OS versions. For each new Windows version check the
|
|
|
|
// _SYSTEM_PROCESS and _SYSTEM_THREAD structures in the DDK to make
|
|
|
|
// sure it is compatible with the existing stuff.
|
|
|
|
int diagnostics_get_process_information(PVOID* ppBuffer, PULONG pcbBuffer) {
|
|
|
|
int retval = 0;
|
|
|
|
NTSTATUS Status = STATUS_INFO_LENGTH_MISMATCH;
|
|
|
|
HANDLE hHeap = GetProcessHeap();
|
2006-05-17 07:44:54 +00:00
|
|
|
HMODULE hNTDllLib = NULL;
|
2006-05-08 18:35:56 +00:00
|
|
|
tNTQSI pNTQSI = NULL;
|
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
hNTDllLib = GetModuleHandleA("ntdll.dll");
|
2006-05-17 07:44:54 +00:00
|
|
|
pNTQSI = (tNTQSI)GetProcAddress(hNTDllLib, "NtQuerySystemInformation");
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
*ppBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, *pcbBuffer);
|
|
|
|
if (ppBuffer == NULL) {
|
|
|
|
retval = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = pNTQSI(
|
|
|
|
SystemProcessAndThreadInformation,
|
|
|
|
*ppBuffer,
|
|
|
|
*pcbBuffer,
|
|
|
|
pcbBuffer
|
|
|
|
);
|
|
|
|
|
|
|
|
if (Status == STATUS_INFO_LENGTH_MISMATCH) {
|
2008-01-31 18:34:51 +00:00
|
|
|
HeapFree(hHeap, (DWORD)NULL, *ppBuffer);
|
2006-05-08 18:35:56 +00:00
|
|
|
*pcbBuffer *= 2;
|
|
|
|
} else if (!NT_SUCCESS(Status)) {
|
2008-01-31 18:34:51 +00:00
|
|
|
HeapFree(hHeap, (DWORD)NULL, *ppBuffer);
|
2006-05-08 18:35:56 +00:00
|
|
|
retval = Status;
|
|
|
|
}
|
|
|
|
} while (Status == STATUS_INFO_LENGTH_MISMATCH);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Enumerate the running threads in the process space and add them to
|
|
|
|
// the list. This only works on NT 4.0 based machines. This also
|
|
|
|
// includes additional information which can be logged during a crash
|
|
|
|
// event.
|
|
|
|
int diagnostics_update_thread_list_NT() {
|
|
|
|
DWORD dwCurrentProcessId = GetCurrentProcessId();
|
|
|
|
HANDLE hThread = NULL;
|
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
ULONG cbBuffer = 32*1024; // 32k initial buffer
|
|
|
|
PVOID pBuffer = NULL;
|
|
|
|
PSYSTEM_PROCESSES_NT4 pProcesses = NULL;
|
|
|
|
PSYSTEM_THREADS pThread = NULL;
|
|
|
|
UINT uiSystemIndex = 0;
|
2006-05-17 07:44:54 +00:00
|
|
|
HMODULE hKernel32Lib;
|
2006-05-08 18:35:56 +00:00
|
|
|
tOT pOT = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
// Dynamically link to the proper function pointers.
|
2009-04-23 03:40:49 +00:00
|
|
|
hKernel32Lib = GetModuleHandleA("kernel32.dll");
|
2006-05-17 07:44:54 +00:00
|
|
|
pOT = (tOT) GetProcAddress( hKernel32Lib, "OpenThread" );
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Get a snapshot of the process and thread information.
|
|
|
|
diagnostics_get_process_information(&pBuffer, &cbBuffer);
|
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
|
|
|
// Lets start walking the structures to find the good stuff.
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES_NT4)pBuffer;
|
|
|
|
do {
|
|
|
|
// Okay, found the current procceses entry now we just need to
|
|
|
|
// update the thread data.
|
|
|
|
if (pProcesses->ProcessId == dwCurrentProcessId) {
|
|
|
|
|
2006-05-23 23:01:36 +00:00
|
|
|
// Store the process information we now know about.
|
|
|
|
diagnostics_process.process_id = pProcesses->ProcessId;
|
|
|
|
diagnostics_process.vm_counters = pProcesses->VmCounters;
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Enumerate the threads
|
|
|
|
for(uiSystemIndex = 0; uiSystemIndex < pProcesses->ThreadCount; uiSystemIndex++) {
|
|
|
|
pThread = &pProcesses->Threads[uiSystemIndex];
|
|
|
|
pThreadEntry = diagnostics_find_thread_entry(pThread->ClientId.UniqueThread);
|
|
|
|
|
|
|
|
if (pThreadEntry) {
|
|
|
|
pThreadEntry->crash_kernel_time = (FLOAT)pThread->KernelTime.QuadPart;
|
|
|
|
pThreadEntry->crash_user_time = (FLOAT)pThread->UserTime.QuadPart;
|
|
|
|
pThreadEntry->crash_wait_time = (FLOAT)pThread->WaitTime;
|
|
|
|
pThreadEntry->crash_priority = pThread->Priority;
|
|
|
|
pThreadEntry->crash_base_priority = pThread->BasePriority;
|
|
|
|
pThreadEntry->crash_state = pThread->State;
|
|
|
|
pThreadEntry->crash_wait_reason = pThread->WaitReason;
|
|
|
|
} else {
|
|
|
|
if (pOT) {
|
|
|
|
hThread = pOT(
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
FALSE,
|
|
|
|
pThread->ClientId.UniqueThread
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pThreadEntry = new BOINC_THREADLISTENTRY;
|
|
|
|
diagnostics_init_thread_entry(pThreadEntry);
|
|
|
|
pThreadEntry->thread_id = pThread->ClientId.UniqueThread;
|
|
|
|
pThreadEntry->thread_handle = hThread;
|
|
|
|
pThreadEntry->crash_kernel_time = (FLOAT)pThread->KernelTime.QuadPart;
|
|
|
|
pThreadEntry->crash_user_time = (FLOAT)pThread->UserTime.QuadPart;
|
|
|
|
pThreadEntry->crash_wait_time = (FLOAT)pThread->WaitTime;
|
|
|
|
pThreadEntry->crash_priority = pThread->Priority;
|
|
|
|
pThreadEntry->crash_base_priority = pThread->BasePriority;
|
|
|
|
pThreadEntry->crash_state = pThread->State;
|
|
|
|
pThreadEntry->crash_wait_reason = pThread->WaitReason;
|
|
|
|
diagnostics_threads.push_back(pThreadEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next structure if one exists
|
|
|
|
if (!pProcesses->NextEntryDelta) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES_NT4)(((LPBYTE)pProcesses) + pProcesses->NextEntryDelta);
|
|
|
|
} while (pProcesses);
|
|
|
|
|
|
|
|
// Release resources
|
|
|
|
if (hThreadListSync) ReleaseMutex(hThreadListSync);
|
2008-01-31 18:34:51 +00:00
|
|
|
if (pBuffer) HeapFree(GetProcessHeap(), (DWORD)NULL, pBuffer);
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Enumerate the running threads in the process space and add them to
|
|
|
|
// the list. This only works on XP or better based machines. This also
|
|
|
|
// includes additional information which can be logged during a crash
|
|
|
|
// event.
|
|
|
|
int diagnostics_update_thread_list_XP() {
|
|
|
|
DWORD dwCurrentProcessId = GetCurrentProcessId();
|
|
|
|
HANDLE hThread = NULL;
|
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
ULONG cbBuffer = 32*1024; // 32k initial buffer
|
|
|
|
PVOID pBuffer = NULL;
|
|
|
|
PSYSTEM_PROCESSES pProcesses = NULL;
|
|
|
|
PSYSTEM_THREADS pThread = NULL;
|
|
|
|
UINT uiSystemIndex = 0;
|
2006-05-17 07:44:54 +00:00
|
|
|
HMODULE hKernel32Lib;
|
2006-05-08 18:35:56 +00:00
|
|
|
tOT pOT = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
// Dynamically link to the proper function pointers.
|
2009-04-23 03:40:49 +00:00
|
|
|
hKernel32Lib = GetModuleHandleA("kernel32.dll");
|
2006-05-17 07:44:54 +00:00
|
|
|
pOT = (tOT) GetProcAddress( hKernel32Lib, "OpenThread" );
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Get a snapshot of the process and thread information.
|
|
|
|
diagnostics_get_process_information(&pBuffer, &cbBuffer);
|
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
|
|
|
// Lets start walking the structures to find the good stuff.
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES)pBuffer;
|
|
|
|
do {
|
|
|
|
// Okay, found the current procceses entry now we just need to
|
|
|
|
// update the thread data.
|
|
|
|
if (pProcesses->ProcessId == dwCurrentProcessId) {
|
2006-05-23 23:01:36 +00:00
|
|
|
|
|
|
|
// Store the process information we now know about.
|
|
|
|
diagnostics_process.process_id = pProcesses->ProcessId;
|
|
|
|
diagnostics_process.vm_counters = pProcesses->VmCounters;
|
|
|
|
diagnostics_process.io_counters = pProcesses->IoCounters;
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Enumerate the threads
|
|
|
|
for(uiSystemIndex = 0; uiSystemIndex < pProcesses->ThreadCount; uiSystemIndex++) {
|
|
|
|
pThread = &pProcesses->Threads[uiSystemIndex];
|
|
|
|
pThreadEntry = diagnostics_find_thread_entry(pThread->ClientId.UniqueThread);
|
|
|
|
|
|
|
|
if (pThreadEntry) {
|
|
|
|
pThreadEntry->crash_kernel_time = (FLOAT)pThread->KernelTime.QuadPart;
|
|
|
|
pThreadEntry->crash_user_time = (FLOAT)pThread->UserTime.QuadPart;
|
|
|
|
pThreadEntry->crash_wait_time = (FLOAT)pThread->WaitTime;
|
|
|
|
pThreadEntry->crash_priority = pThread->Priority;
|
|
|
|
pThreadEntry->crash_base_priority = pThread->BasePriority;
|
|
|
|
pThreadEntry->crash_state = pThread->State;
|
|
|
|
pThreadEntry->crash_wait_reason = pThread->WaitReason;
|
|
|
|
} else {
|
|
|
|
if (pOT) {
|
|
|
|
hThread = pOT(
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
FALSE,
|
|
|
|
pThread->ClientId.UniqueThread
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pThreadEntry = new BOINC_THREADLISTENTRY;
|
|
|
|
diagnostics_init_thread_entry(pThreadEntry);
|
|
|
|
pThreadEntry->thread_id = pThread->ClientId.UniqueThread;
|
|
|
|
pThreadEntry->thread_handle = hThread;
|
|
|
|
pThreadEntry->crash_kernel_time = (FLOAT)pThread->KernelTime.QuadPart;
|
|
|
|
pThreadEntry->crash_user_time = (FLOAT)pThread->UserTime.QuadPart;
|
|
|
|
pThreadEntry->crash_wait_time = (FLOAT)pThread->WaitTime;
|
|
|
|
pThreadEntry->crash_priority = pThread->Priority;
|
|
|
|
pThreadEntry->crash_base_priority = pThread->BasePriority;
|
|
|
|
pThreadEntry->crash_state = pThread->State;
|
|
|
|
pThreadEntry->crash_wait_reason = pThread->WaitReason;
|
|
|
|
diagnostics_threads.push_back(pThreadEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next structure if one exists
|
|
|
|
if (!pProcesses->NextEntryDelta) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES)(((LPBYTE)pProcesses) + pProcesses->NextEntryDelta);
|
|
|
|
} while (pProcesses);
|
|
|
|
|
|
|
|
// Release resources
|
|
|
|
if (hThreadListSync) ReleaseMutex(hThreadListSync);
|
2008-01-31 18:34:51 +00:00
|
|
|
if (pBuffer) HeapFree(GetProcessHeap(), (DWORD)NULL, pBuffer);
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Determine which update thread list function to call based on OS
|
|
|
|
// version.
|
|
|
|
int diagnostics_update_thread_list() {
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
// Detect platform information
|
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(osvi);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
|
|
|
|
switch(osvi.dwPlatformId) {
|
|
|
|
case VER_PLATFORM_WIN32_WINDOWS:
|
|
|
|
// Win95, Win98, WinME
|
|
|
|
retval = diagnostics_update_thread_list_9X();
|
|
|
|
break;
|
|
|
|
case VER_PLATFORM_WIN32_NT:
|
|
|
|
switch(osvi.dwMajorVersion) {
|
|
|
|
case 4:
|
|
|
|
// WinNT 4.0
|
|
|
|
retval = diagnostics_update_thread_list_NT();
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
// Win2k, WinXP, Win2k3
|
|
|
|
retval = diagnostics_update_thread_list_XP();
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (osvi.dwMinorVersion == 0) {
|
|
|
|
// WinVista
|
|
|
|
retval = diagnostics_update_thread_list_XP();
|
|
|
|
} else {
|
|
|
|
// In cases where we do not know if the interfaces have
|
|
|
|
// changed from the ones we know about, just default to
|
|
|
|
// the most compatible implementation.
|
|
|
|
retval = diagnostics_update_thread_list_9X();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// In cases where we do not know if the interfaces have
|
|
|
|
// changed from the ones we know about, just default to
|
|
|
|
// the most compatible implementation.
|
|
|
|
retval = diagnostics_update_thread_list_9X();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-19 17:22:46 +00:00
|
|
|
// Set the cached exception record for the current thread, let the exception monitor
|
|
|
|
// thread dump the human readable exception information.
|
2007-10-24 23:49:52 +00:00
|
|
|
int diagnostics_set_thread_exception_record(PEXCEPTION_POINTERS pExPtrs) {
|
2006-05-01 21:36:38 +00:00
|
|
|
HANDLE hThread;
|
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
pThreadEntry = diagnostics_find_thread_entry(GetCurrentThreadId());
|
2006-05-01 21:36:38 +00:00
|
|
|
if (pThreadEntry) {
|
2007-10-24 23:49:52 +00:00
|
|
|
pThreadEntry->crash_exception_record = pExPtrs;
|
2006-05-01 21:36:38 +00:00
|
|
|
} else {
|
|
|
|
DuplicateHandle(
|
|
|
|
GetCurrentProcess(),
|
|
|
|
GetCurrentThread(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&hThread,
|
|
|
|
0,
|
|
|
|
FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS
|
|
|
|
);
|
|
|
|
|
|
|
|
pThreadEntry = new BOINC_THREADLISTENTRY;
|
2006-05-08 18:35:56 +00:00
|
|
|
diagnostics_init_thread_entry(pThreadEntry);
|
2006-05-01 21:36:38 +00:00
|
|
|
pThreadEntry->thread_id = GetCurrentThreadId();
|
|
|
|
pThreadEntry->thread_handle = hThread;
|
2006-05-08 18:35:56 +00:00
|
|
|
pThreadEntry->crash_exception_record = pExPtrs;
|
2007-10-24 23:49:52 +00:00
|
|
|
diagnostics_threads.push_back(pThreadEntry);
|
2006-05-08 18:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-19 17:22:46 +00:00
|
|
|
// Set the current thread to suspend exempt status. Prevents deadlocks.
|
2006-05-02 07:53:28 +00:00
|
|
|
int diagnostics_set_thread_exempt_suspend() {
|
2007-10-24 23:49:52 +00:00
|
|
|
HANDLE hThread;
|
2006-05-02 07:53:28 +00:00
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
|
|
|
pThreadEntry = diagnostics_find_thread_entry(GetCurrentThreadId());
|
|
|
|
if (pThreadEntry) {
|
|
|
|
pThreadEntry->crash_suspend_exempt = TRUE;
|
2007-10-24 23:49:52 +00:00
|
|
|
} else {
|
|
|
|
DuplicateHandle(
|
|
|
|
GetCurrentProcess(),
|
|
|
|
GetCurrentThread(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&hThread,
|
|
|
|
0,
|
|
|
|
FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS
|
|
|
|
);
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2007-10-24 23:49:52 +00:00
|
|
|
pThreadEntry = new BOINC_THREADLISTENTRY;
|
|
|
|
diagnostics_init_thread_entry(pThreadEntry);
|
|
|
|
pThreadEntry->thread_id = GetCurrentThreadId();
|
|
|
|
pThreadEntry->thread_handle = hThread;
|
|
|
|
pThreadEntry->crash_suspend_exempt = TRUE;
|
|
|
|
diagnostics_threads.push_back(pThreadEntry);
|
2006-05-02 07:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-19 17:22:46 +00:00
|
|
|
// Checks to see if the specified thread id is flagged for suspend exempt status.
|
|
|
|
// returns 0 on true, 1 on false. Couldn't use a bool data type since the function
|
|
|
|
// prototype needs to be compatible with C.
|
|
|
|
int diagnostics_is_thread_exempt_suspend(long thread_id) {
|
|
|
|
int retval = 1;
|
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
|
|
|
pThreadEntry = diagnostics_find_thread_entry(thread_id);
|
|
|
|
if (pThreadEntry) {
|
|
|
|
if (pThreadEntry->crash_suspend_exempt) {
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-25 11:14:27 +00:00
|
|
|
// Set the current thread's crash message.
|
|
|
|
int diagnostics_set_thread_crash_message(char* message) {
|
2007-10-24 23:49:52 +00:00
|
|
|
HANDLE hThread;
|
2006-05-25 11:14:27 +00:00
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
|
|
|
pThreadEntry = diagnostics_find_thread_entry(GetCurrentThreadId());
|
|
|
|
if (pThreadEntry) {
|
|
|
|
int buffer_used = snprintf(
|
|
|
|
pThreadEntry->crash_message,
|
|
|
|
sizeof(pThreadEntry->crash_message),
|
|
|
|
"%s",
|
|
|
|
message
|
|
|
|
);
|
|
|
|
if ((sizeof(pThreadEntry->crash_message) == buffer_used) || (-1 == buffer_used)) {
|
|
|
|
pThreadEntry->crash_message[sizeof(pThreadEntry->crash_message)-1] = '\0';
|
|
|
|
}
|
2007-10-24 23:49:52 +00:00
|
|
|
} else {
|
|
|
|
DuplicateHandle(
|
|
|
|
GetCurrentProcess(),
|
|
|
|
GetCurrentThread(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&hThread,
|
|
|
|
0,
|
|
|
|
FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS
|
|
|
|
);
|
|
|
|
|
|
|
|
pThreadEntry = new BOINC_THREADLISTENTRY;
|
|
|
|
diagnostics_init_thread_entry(pThreadEntry);
|
|
|
|
pThreadEntry->thread_id = GetCurrentThreadId();
|
|
|
|
pThreadEntry->thread_handle = hThread;
|
|
|
|
int buffer_used = snprintf(
|
|
|
|
pThreadEntry->crash_message,
|
|
|
|
sizeof(pThreadEntry->crash_message),
|
|
|
|
"%s",
|
|
|
|
message
|
|
|
|
);
|
|
|
|
if ((sizeof(pThreadEntry->crash_message) == buffer_used) || (-1 == buffer_used)) {
|
|
|
|
pThreadEntry->crash_message[sizeof(pThreadEntry->crash_message)-1] = '\0';
|
|
|
|
}
|
|
|
|
diagnostics_threads.push_back(pThreadEntry);
|
2006-05-25 11:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Translate the thread state into a human readable form.
|
|
|
|
//
|
|
|
|
// See: http://support.microsoft.com/?kbid=837372
|
|
|
|
//
|
|
|
|
char* diagnostics_format_thread_state(int thread_state) {
|
|
|
|
switch(thread_state) {
|
2006-05-11 06:15:08 +00:00
|
|
|
case ThreadStateInitialized: return "Initialized";
|
|
|
|
case ThreadStateReady: return "Ready";
|
|
|
|
case ThreadStateRunning: return "Running";
|
|
|
|
case ThreadStateStandby: return "Standby";
|
|
|
|
case ThreadStateTerminated: return "Terminated";
|
|
|
|
case ThreadStateWaiting: return "Waiting";
|
|
|
|
case ThreadStateTransition: return "Transition";
|
|
|
|
default: return "Unknown";
|
2006-05-08 18:35:56 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Translate the thread wait reason into a human readable form.
|
|
|
|
//
|
|
|
|
// See: http://support.microsoft.com/?kbid=837372
|
|
|
|
//
|
|
|
|
char* diagnostics_format_thread_wait_reason(int thread_wait_reason) {
|
|
|
|
switch(thread_wait_reason) {
|
2006-05-11 06:15:08 +00:00
|
|
|
case ThreadWaitReasonExecutive: return "Executive";
|
|
|
|
case ThreadWaitReasonFreePage: return "FreePage";
|
|
|
|
case ThreadWaitReasonPageIn: return "PageIn";
|
|
|
|
case ThreadWaitReasonPoolAllocation: return "PoolAllocation";
|
|
|
|
case ThreadWaitReasonDelayExecution: return "ExecutionDelay";
|
|
|
|
case ThreadWaitReasonSuspended: return "Suspended";
|
|
|
|
case ThreadWaitReasonUserRequest: return "UserRequest";
|
|
|
|
case ThreadWaitReasonWrExecutive: return "Executive";
|
|
|
|
case ThreadWaitReasonWrFreePage: return "FreePage";
|
|
|
|
case ThreadWaitReasonWrPageIn: return "PageIn";
|
|
|
|
case ThreadWaitReasonWrPoolAllocation: return "PoolAllocation";
|
|
|
|
case ThreadWaitReasonWrDelayExecution: return "ExecutionDelay";
|
|
|
|
case ThreadWaitReasonWrSuspended: return "Suspended";
|
|
|
|
case ThreadWaitReasonWrUserRequest: return "UserRequest";
|
|
|
|
case ThreadWaitReasonWrEventPairHigh: return "EventPairHigh";
|
|
|
|
case ThreadWaitReasonWrEventPairLow: return "EventPairLow";
|
|
|
|
case ThreadWaitReasonWrLpcReceive: return "LPCReceive";
|
|
|
|
case ThreadWaitReasonWrLpcReply: return "LPCReply";
|
|
|
|
case ThreadWaitReasonWrVirtualMemory: return "VirtualMemory";
|
|
|
|
case ThreadWaitReasonWrPageOut: return "PageOut";
|
|
|
|
default: return "Unknown";
|
2006-05-08 18:35:56 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-22 02:51:57 +00:00
|
|
|
// Translate the process priority class into a human readable form.
|
|
|
|
//
|
|
|
|
// See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/scheduling_priorities.asp
|
|
|
|
//
|
|
|
|
char* diagnostics_format_process_priority(int process_priority) {
|
|
|
|
switch(process_priority) {
|
|
|
|
case IDLE_PRIORITY_CLASS: return "Idle";
|
|
|
|
case BELOW_NORMAL_PRIORITY_CLASS: return "Below Normal";
|
|
|
|
case NORMAL_PRIORITY_CLASS: return "Normal";
|
|
|
|
case ABOVE_NORMAL_PRIORITY_CLASS: return "Above Normal";
|
|
|
|
case HIGH_PRIORITY_CLASS: return "High";
|
|
|
|
case REALTIME_PRIORITY_CLASS: return "Realtime";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Translate the thread priority class into a human readable form.
|
|
|
|
//
|
|
|
|
// See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/scheduling_priorities.asp
|
|
|
|
//
|
|
|
|
char* diagnostics_format_thread_priority(int thread_priority) {
|
|
|
|
switch(thread_priority) {
|
|
|
|
case THREAD_PRIORITY_IDLE: return "Idle";
|
|
|
|
case THREAD_PRIORITY_LOWEST: return "Lowest";
|
|
|
|
case THREAD_PRIORITY_BELOW_NORMAL: return "Below Normal";
|
|
|
|
case THREAD_PRIORITY_NORMAL: return "Normal";
|
|
|
|
case THREAD_PRIORITY_ABOVE_NORMAL: return "Above Normal";
|
|
|
|
case THREAD_PRIORITY_HIGHEST: return "Highest";
|
|
|
|
case THREAD_PRIORITY_TIME_CRITICAL: return "Time Critical";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// Provide a mechinism to trap and report messages sent to the debugger's
|
|
|
|
// viewport. This should only been enabled if a debugger isn't running
|
|
|
|
// against the current process already.
|
|
|
|
//
|
|
|
|
// Documentation about the protocol can be found here:
|
|
|
|
// http://www.unixwiz.net/techtips/outputdebugstring.html
|
|
|
|
//
|
|
|
|
|
|
|
|
typedef struct _DEBUGGERMESSAGE {
|
|
|
|
DWORD dwProcessId;
|
|
|
|
char data[4096 - sizeof(DWORD)];
|
|
|
|
} DEBUGGERMESSAGE, *PDEBUGGERMESSAGE;
|
|
|
|
|
|
|
|
typedef struct _BOINC_MESSAGEMONITORENTRY {
|
|
|
|
double timestamp;
|
|
|
|
std::string message;
|
|
|
|
} BOINC_MESSAGEMONITORENTRY, *PBOINC_MESSAGEMONITORENTRY;
|
|
|
|
|
|
|
|
static std::vector<PBOINC_MESSAGEMONITORENTRY> diagnostics_monitor_messages;
|
|
|
|
static PDEBUGGERMESSAGE pMessageBuffer;
|
2006-08-13 20:53:47 +00:00
|
|
|
static UINT uiMessageMonitorThreadId;
|
2006-05-08 18:35:56 +00:00
|
|
|
static HANDLE hMessageMonitorThread;
|
2006-05-01 21:36:38 +00:00
|
|
|
static HANDLE hMessageMonitorSync;
|
|
|
|
static HANDLE hMessageSharedMap;
|
|
|
|
static HANDLE hMessageAckEvent;
|
|
|
|
static HANDLE hMessageReadyEvent;
|
|
|
|
static HANDLE hMessageQuitEvent;
|
2006-05-08 18:35:56 +00:00
|
|
|
static HANDLE hMessageQuitFinishedEvent;
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Initialize the needed structures and startup the message processing thread.
|
|
|
|
//
|
2006-05-01 21:36:38 +00:00
|
|
|
int diagnostics_init_message_monitor() {
|
|
|
|
int retval = 0;
|
|
|
|
unsigned int i;
|
2006-05-11 06:15:08 +00:00
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
|
|
|
DWORD dwCaptureMessages;
|
2006-05-10 17:56:34 +00:00
|
|
|
HMODULE hKernel32Lib;
|
|
|
|
tIDP pIDP = NULL;
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
SECURITY_DESCRIPTOR sd;
|
|
|
|
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
sa.lpSecurityDescriptor = &sd;
|
|
|
|
|
2006-05-23 23:01:36 +00:00
|
|
|
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
|
|
|
|
SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE);
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Create a mutex that can be used to syncronize data access
|
2006-05-01 21:36:38 +00:00
|
|
|
// to the global thread list.
|
|
|
|
hMessageMonitorSync = CreateMutex(NULL, TRUE, NULL);
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!hMessageMonitorSync) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): Creating hMessageMonitorSync failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
// Clear out any previous messages.
|
|
|
|
for (i=0; i<diagnostics_monitor_messages.size(); i++) {
|
2006-05-17 22:29:37 +00:00
|
|
|
delete diagnostics_monitor_messages[i];
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
2006-05-17 07:44:54 +00:00
|
|
|
diagnostics_monitor_messages.clear();
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-11 06:15:08 +00:00
|
|
|
// Check the registry to see if we are aloud to capture debugger messages.
|
|
|
|
// Apparently many audio and visual payback programs dump serious
|
|
|
|
// amounts of data to the debugger viewport even on a release build.
|
|
|
|
// When this feature is enabled it slows down the replay of DVDs and CDs
|
|
|
|
// such that they become jerky and unpleasent to watch or listen too.
|
|
|
|
//
|
|
|
|
// We'll turn it off by default, but keep it around just in case we need
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
dwCaptureMessages = 0;
|
|
|
|
dwType = REG_DWORD;
|
|
|
|
dwSize = sizeof(dwCaptureMessages);
|
|
|
|
diagnostics_get_registry_value(
|
|
|
|
"CaptureMessages",
|
|
|
|
&dwType,
|
|
|
|
&dwSize,
|
|
|
|
(LPBYTE)&dwCaptureMessages
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2006-05-10 17:56:34 +00:00
|
|
|
// If a debugger is present then let it capture the debugger messages
|
2009-04-23 03:40:49 +00:00
|
|
|
hKernel32Lib = GetModuleHandleA("kernel32.dll");
|
2006-05-10 17:56:34 +00:00
|
|
|
pIDP = (tIDP) GetProcAddress(hKernel32Lib, "IsDebuggerPresent");
|
|
|
|
|
|
|
|
if (pIDP) {
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!pIDP() && hMessageMonitorSync && dwCaptureMessages) {
|
2006-05-10 17:56:34 +00:00
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
hMessageAckEvent = CreateEventA(&sa, FALSE, FALSE, "DBWIN_BUFFER_READY");
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!hMessageAckEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): Creating hMessageAckEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
hMessageReadyEvent = CreateEventA(&sa, FALSE, FALSE, "DBWIN_DATA_READY");
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!hMessageReadyEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): Creating hMessageReadyEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
hMessageQuitEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!hMessageQuitEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): Creating hMessageQuitEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
hMessageQuitFinishedEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!hMessageQuitFinishedEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): Creating hMessageQuitFinishedEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-10 17:56:34 +00:00
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
hMessageSharedMap = CreateFileMappingA(
|
2006-05-10 17:56:34 +00:00
|
|
|
INVALID_HANDLE_VALUE, // use paging file
|
|
|
|
&sa, // default security
|
|
|
|
PAGE_READWRITE, // read/write access
|
|
|
|
0, // max. object size
|
|
|
|
sizeof(DEBUGGERMESSAGE), // buffer size
|
|
|
|
"DBWIN_BUFFER" // name of mapping object
|
|
|
|
);
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!hMessageSharedMap) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): CreateFileMapping hMessageSharedMap failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-10 17:56:34 +00:00
|
|
|
|
|
|
|
pMessageBuffer = (PDEBUGGERMESSAGE)MapViewOfFile(
|
|
|
|
hMessageSharedMap,
|
|
|
|
FILE_MAP_READ | FILE_MAP_WRITE,
|
|
|
|
0, // file offset high
|
|
|
|
0, // file offset low
|
|
|
|
sizeof(DEBUGGERMESSAGE) // # of bytes to map (entire file)
|
|
|
|
);
|
2006-05-23 23:01:36 +00:00
|
|
|
if (!pMessageBuffer) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): MapViewOfFile pMessageBuffer failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-10 17:56:34 +00:00
|
|
|
|
2006-05-19 02:06:14 +00:00
|
|
|
hMessageMonitorThread = (HANDLE)_beginthreadex(
|
2006-05-10 17:56:34 +00:00
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
diagnostics_message_monitor,
|
|
|
|
0,
|
|
|
|
0,
|
2006-08-13 20:53:47 +00:00
|
|
|
&uiMessageMonitorThreadId
|
2006-05-10 17:56:34 +00:00
|
|
|
);
|
2006-05-22 18:19:38 +00:00
|
|
|
if (!hMessageMonitorThread) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_message_monitor(): _beginthreadex, errno %d\n", errno
|
|
|
|
);
|
|
|
|
}
|
2006-05-10 17:56:34 +00:00
|
|
|
} else {
|
|
|
|
retval = ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hMessageMonitorSync);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Shutdown the message monitoring thread and cleanup any of the in memory
|
|
|
|
// structures.
|
|
|
|
int diagnostics_finish_message_monitor() {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
// Begin the cleanup process by means of shutting down the
|
|
|
|
// message monitoring thread.
|
|
|
|
SetEvent(hMessageQuitEvent);
|
|
|
|
|
|
|
|
// Wait until it is message monitoring thread is shutdown before
|
|
|
|
// cleaning up the structure since we'll need to aquire the
|
|
|
|
// MessageMonitorSync mutex.
|
|
|
|
WaitForSingleObject(hMessageQuitFinishedEvent, INFINITE);
|
|
|
|
WaitForSingleObject(hMessageMonitorSync, INFINITE);
|
|
|
|
|
|
|
|
|
|
|
|
// Now clean up everything we can.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Clear out any previous messages.
|
|
|
|
for (i=0; i<diagnostics_monitor_messages.size(); i++) {
|
2006-05-17 22:29:37 +00:00
|
|
|
delete diagnostics_monitor_messages[i];
|
2006-05-08 18:35:56 +00:00
|
|
|
}
|
2006-05-17 07:44:54 +00:00
|
|
|
diagnostics_monitor_messages.clear();
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Cleanup the handles
|
|
|
|
if (pMessageBuffer) UnmapViewOfFile(pMessageBuffer);
|
|
|
|
if (hMessageSharedMap) CloseHandle(hMessageSharedMap);
|
|
|
|
if (hMessageAckEvent) CloseHandle(hMessageAckEvent);
|
|
|
|
if (hMessageReadyEvent) CloseHandle(hMessageReadyEvent);
|
|
|
|
if (hMessageQuitEvent) CloseHandle(hMessageQuitEvent);
|
|
|
|
if (hMessageQuitFinishedEvent) CloseHandle(hMessageQuitFinishedEvent);
|
|
|
|
if (hMessageMonitorThread) CloseHandle(hMessageMonitorThread);
|
|
|
|
if (hMessageMonitorSync) CloseHandle(hMessageMonitorSync);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
int diagnostics_message_monitor_dump() {
|
|
|
|
unsigned int i;
|
|
|
|
PBOINC_MESSAGEMONITORENTRY pMessageEntry = NULL;
|
|
|
|
|
|
|
|
// Wait for the MessageMonitorSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hMessageMonitorSync, INFINITE);
|
|
|
|
|
|
|
|
fprintf(stderr, "\n*** Debug Message Dump ****\n");
|
|
|
|
|
|
|
|
// Clear out any previous messages.
|
|
|
|
for (i=0; i<diagnostics_monitor_messages.size(); i++) {
|
|
|
|
pMessageEntry = diagnostics_monitor_messages[i];
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
2006-05-02 07:53:28 +00:00
|
|
|
"[%s] %s",
|
2006-05-01 21:36:38 +00:00
|
|
|
time_to_string(pMessageEntry->timestamp),
|
|
|
|
pMessageEntry->message.c_str()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n\n");
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hMessageMonitorSync);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-02 07:53:28 +00:00
|
|
|
// This thread monitors the shared memory buffer used to pass debug messages
|
|
|
|
// around. due to an anomaly in the Windows debug environment it is
|
|
|
|
// suggested that a sleep(0) be introduced before any
|
|
|
|
// SetEvent/ResetEvent/PulseEvent function is called.
|
|
|
|
//
|
|
|
|
// See: http://support.microsoft.com/kb/q173260/
|
|
|
|
//
|
2006-08-24 07:02:52 +00:00
|
|
|
UINT WINAPI diagnostics_message_monitor(LPVOID /* lpParameter */) {
|
2008-01-31 18:34:51 +00:00
|
|
|
DWORD dwEvent = (DWORD)NULL;
|
|
|
|
DWORD dwCurrentProcessId = (DWORD)NULL;
|
2006-05-01 21:36:38 +00:00
|
|
|
BOOL bContinue = TRUE;
|
|
|
|
DWORD dwRepeatMessageCounter = 0;
|
|
|
|
DWORD dwRepeatMessageProcessId = 0;
|
|
|
|
std::string strRepeatMessage;
|
|
|
|
PBOINC_MESSAGEMONITORENTRY pMessageEntry = NULL;
|
2006-05-08 18:35:56 +00:00
|
|
|
HANDLE hEvents[2];
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2007-10-24 23:49:52 +00:00
|
|
|
// Make sure this thread doesn't get suspended during
|
|
|
|
// a crash dump, the DBGHELP library is pretty verbose.
|
|
|
|
// Suspending this thread will cause a deadlock.
|
2006-05-02 07:53:28 +00:00
|
|
|
diagnostics_set_thread_exempt_suspend();
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
// Which events do we want to wait for?
|
|
|
|
hEvents[0] = hMessageQuitEvent;
|
|
|
|
hEvents[1] = hMessageReadyEvent;
|
|
|
|
|
|
|
|
// Cache the current process id
|
|
|
|
dwCurrentProcessId = GetCurrentProcessId();
|
|
|
|
|
|
|
|
// Signal that the buffer is ready for action.
|
2006-05-02 07:53:28 +00:00
|
|
|
Sleep(0);
|
2006-05-01 21:36:38 +00:00
|
|
|
SetEvent(hMessageAckEvent);
|
|
|
|
|
|
|
|
while (bContinue) {
|
|
|
|
dwEvent = WaitForMultipleObjects(
|
|
|
|
2, // number of objects in array
|
|
|
|
hEvents, // array of objects
|
|
|
|
FALSE, // wait for any
|
2006-05-02 07:53:28 +00:00
|
|
|
INFINITE // wait
|
2006-05-01 21:36:38 +00:00
|
|
|
);
|
|
|
|
switch(dwEvent) {
|
|
|
|
// hMessageQuitEvent was signaled.
|
|
|
|
case WAIT_OBJECT_0 + 0:
|
|
|
|
|
|
|
|
// We are shutting down so lets cleanup and exit.
|
|
|
|
bContinue = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// hMessageReadyEvent was signaled.
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
// The debugger protocol assumes that only one debugger is going
|
|
|
|
// to exist on the system, but we are only interested in messages
|
|
|
|
// from the current process id. Since we are here we can assume
|
|
|
|
// that no debugger was present when the application was launched
|
|
|
|
// so we can safely ignore messages that didn't come from us
|
|
|
|
// because that means they are from another application.
|
|
|
|
//
|
|
|
|
// If we detect a message from a different process just ignore it
|
2006-05-08 18:35:56 +00:00
|
|
|
// and re-signal the event. We'll go to sleep for 100 milliseconds
|
2006-05-01 21:36:38 +00:00
|
|
|
// and let the other BOINC based applications have a shot at it.
|
|
|
|
//
|
|
|
|
// If we see the same message four times that means it is from an
|
|
|
|
// application that doesn't understand our modificatons, so we'll
|
|
|
|
// process the message just like we were a regular debugger and
|
|
|
|
// signal that the buffer is available again.
|
|
|
|
if (dwCurrentProcessId != pMessageBuffer->dwProcessId) {
|
|
|
|
// Message from a different process.
|
|
|
|
|
|
|
|
// Is this the same message as before?
|
|
|
|
if ((dwRepeatMessageProcessId != pMessageBuffer->dwProcessId) ||
|
|
|
|
(strRepeatMessage != pMessageBuffer->data)
|
|
|
|
) {
|
|
|
|
dwRepeatMessageCounter = 0;
|
|
|
|
|
|
|
|
// Cache the data for future checks.
|
|
|
|
dwRepeatMessageProcessId = pMessageBuffer->dwProcessId;
|
|
|
|
strRepeatMessage = pMessageBuffer->data;
|
|
|
|
} else {
|
|
|
|
dwRepeatMessageCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dwRepeatMessageCounter > 4) {
|
|
|
|
// Buffer is ready to receive a new message.
|
2006-05-02 07:53:28 +00:00
|
|
|
Sleep(0);
|
2006-05-01 21:36:38 +00:00
|
|
|
SetEvent(hMessageAckEvent);
|
|
|
|
|
|
|
|
dwRepeatMessageCounter = 0;
|
|
|
|
dwRepeatMessageProcessId = 0;
|
|
|
|
strRepeatMessage = "";
|
|
|
|
} else {
|
|
|
|
// Let another application have a go at the message.
|
2006-05-02 07:53:28 +00:00
|
|
|
Sleep(0);
|
2006-05-01 21:36:38 +00:00
|
|
|
SetEvent(hMessageReadyEvent);
|
2006-05-02 07:53:28 +00:00
|
|
|
Sleep(100);
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// A message for us to process
|
|
|
|
pMessageEntry = new BOINC_MESSAGEMONITORENTRY;
|
|
|
|
pMessageEntry->timestamp = dtime();
|
|
|
|
pMessageEntry->message = pMessageBuffer->data;
|
|
|
|
|
|
|
|
// Wait for the MessageMonitorSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hMessageMonitorSync, INFINITE);
|
|
|
|
|
|
|
|
diagnostics_monitor_messages.push_back(pMessageEntry);
|
|
|
|
|
|
|
|
// Trim back the number of messages in memory
|
|
|
|
if (diagnostics_monitor_messages.size() > 50) {
|
2006-05-17 07:44:54 +00:00
|
|
|
delete diagnostics_monitor_messages[0];
|
|
|
|
diagnostics_monitor_messages.erase(diagnostics_monitor_messages.begin());
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hMessageMonitorSync);
|
|
|
|
|
2006-05-02 07:53:28 +00:00
|
|
|
// Clear out the old message
|
|
|
|
ZeroMemory(pMessageBuffer, sizeof(DEBUGGERMESSAGE));
|
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// Buffer is ready to receive a new message.
|
2006-05-02 07:53:28 +00:00
|
|
|
Sleep(0);
|
2006-05-01 21:36:38 +00:00
|
|
|
SetEvent(hMessageAckEvent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Notify the calling thread that the message monitoring thread is
|
|
|
|
// finished.
|
|
|
|
SetEvent(hMessageQuitFinishedEvent);
|
2006-05-01 21:36:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Structured Exceptions are Windows primary mechanism for dealing with
|
|
|
|
// badly behaved applications or applications where something bad has
|
|
|
|
// happened underneath them and they need to clean up after themselves.
|
|
|
|
//
|
|
|
|
// Applications can define an unhandled exception filter to handle any
|
|
|
|
// exception event that Windows will throw. If you leave things to
|
|
|
|
// the OS defaults, you'll end up with the annoying Windows Error
|
|
|
|
// Reporting dialog and they user will be asked if they want to report
|
|
|
|
// the crash to Microsoft. Most of the time this is okay for regular
|
|
|
|
// applications, but for BOINC based applications this is really bad.
|
|
|
|
//
|
|
|
|
// BOINC based applications need to be completely autonomous. Unhandled
|
|
|
|
// exceptions are caught and we dump as much information, about what
|
|
|
|
// has happened, to stderr so that project administrators can look at
|
|
|
|
// it and fix whatever bug might have caused the event.
|
|
|
|
//
|
|
|
|
// To accomplish this BOINC starts up a thread that will handle any
|
|
|
|
// unhandled exceptions when one is detected. By using a separate
|
|
|
|
// thread the runtime debugger can avoid stack corruption issues and
|
|
|
|
// multiple unhandled exceptions. In a multi-processor system it is
|
|
|
|
// possible that both the graphics thread and the worker threads would
|
|
|
|
// be referencing the same corrupted area of memory. Previous
|
|
|
|
// implementations of the runtime debugger would have just terminated
|
|
|
|
// the process believing it was a nested unhandled exception instead
|
|
|
|
// of believing it to be two seperate exceptions thrown from different
|
|
|
|
// threads.
|
2006-05-01 21:36:38 +00:00
|
|
|
//
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// This structure is used to keep track of stuff nessassary
|
|
|
|
// to dump information about the top most window during
|
|
|
|
// a crash event.
|
|
|
|
typedef struct _BOINC_WINDOWCAPTURE {
|
|
|
|
HWND hwnd;
|
|
|
|
char window_name[256];
|
|
|
|
char window_class[256];
|
|
|
|
DWORD window_process_id;
|
|
|
|
DWORD window_thread_id;
|
|
|
|
} BOINC_WINDOWCAPTURE, *PBOINC_WINDOWCAPTURE;
|
|
|
|
|
2008-01-31 18:34:51 +00:00
|
|
|
static UINT uiExceptionMonitorThreadId = (UINT)NULL;
|
2006-05-22 18:19:38 +00:00
|
|
|
static HANDLE hExceptionMonitorThread = NULL;
|
|
|
|
static HANDLE hExceptionMonitorHalt = NULL;
|
2006-05-31 08:14:21 +00:00
|
|
|
static HANDLE hExceptionMonitorStartedEvent = NULL;
|
2006-05-22 18:19:38 +00:00
|
|
|
static HANDLE hExceptionDetectedEvent = NULL;
|
|
|
|
static HANDLE hExceptionQuitEvent = NULL;
|
|
|
|
static HANDLE hExceptionQuitFinishedEvent = NULL;
|
|
|
|
static CRITICAL_SECTION csExceptionMonitorFallback;
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Initialize the needed structures and startup the unhandled exception
|
|
|
|
// monitor thread.
|
|
|
|
int diagnostics_init_unhandled_exception_monitor() {
|
|
|
|
int retval = 0;
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-22 18:19:38 +00:00
|
|
|
// Initialize the fallback critical section in case we fail to create the
|
|
|
|
// unhandled exception monitor.
|
|
|
|
InitializeCriticalSection(&csExceptionMonitorFallback);
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Create a mutex that can be used to put any thread that has thrown
|
|
|
|
// an unhandled exception to sleep.
|
|
|
|
hExceptionMonitorHalt = CreateMutex(NULL, FALSE, NULL);
|
2006-05-22 18:19:38 +00:00
|
|
|
if (!hExceptionMonitorHalt) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_unhandled_exception_monitor(): Creating hExceptionMonitorHalt failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2006-05-31 08:14:21 +00:00
|
|
|
// The following event is thrown by the exception monitoring thread
|
|
|
|
// right before it waits for the hExceptionDetectedEvent event.
|
|
|
|
hExceptionMonitorStartedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!hExceptionMonitorStartedEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_unhandled_exception_monitor(): Creating hExceptionMonitorStartedEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// The following event is thrown by a thread that has experienced an
|
|
|
|
// unhandled exception after storing its exception record but before
|
|
|
|
// it attempts to aquire the halt mutex.
|
|
|
|
hExceptionDetectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
2006-05-22 18:19:38 +00:00
|
|
|
if (!hExceptionDetectedEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_unhandled_exception_monitor(): Creating hExceptionDetectedEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Create an event that we can use to shutdown the unhandled exception
|
|
|
|
// monitoring thread.
|
|
|
|
hExceptionQuitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
2006-05-22 18:19:38 +00:00
|
|
|
if (!hExceptionQuitEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_unhandled_exception_monitor(): Creating hExceptionQuitEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
hExceptionQuitFinishedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
2006-05-22 18:19:38 +00:00
|
|
|
if (!hExceptionQuitFinishedEvent) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_unhandled_exception_monitor(): Creating hExceptionQuitFinishedEvent failed, GLE %d\n", GetLastError()
|
|
|
|
);
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Create the thread that is going to monitor any unhandled exceptions
|
2006-05-22 18:19:38 +00:00
|
|
|
// NOTE: Only attempt to create the thread if all the thread sync objects
|
|
|
|
// have been created.
|
2006-05-22 18:26:39 +00:00
|
|
|
if (hExceptionMonitorHalt && hExceptionDetectedEvent && hExceptionQuitEvent && hExceptionQuitFinishedEvent) {
|
2006-05-22 18:19:38 +00:00
|
|
|
hExceptionMonitorThread = (HANDLE)_beginthreadex(
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
diagnostics_unhandled_exception_monitor,
|
|
|
|
0,
|
|
|
|
0,
|
2006-08-13 20:53:47 +00:00
|
|
|
&uiExceptionMonitorThreadId
|
2006-05-22 18:19:38 +00:00
|
|
|
);
|
|
|
|
if (!hExceptionMonitorThread) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "diagnostics_init_unhandled_exception_monitor(): Creating hExceptionMonitorThread failed, errno %d\n", errno
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hExceptionMonitorThread) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "WARNING: BOINC Windows Runtime Debugger has been disabled.\n"
|
|
|
|
);
|
|
|
|
retval = ERR_THREAD;
|
2006-05-31 08:14:21 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// Wait until the exception monitor is ready for business.
|
|
|
|
//
|
|
|
|
WaitForSingleObject(hExceptionMonitorStartedEvent, INFINITE);
|
|
|
|
|
2006-05-22 18:19:38 +00:00
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Shutdown the unhandled exception monitoring thread and cleanup any
|
|
|
|
// of the in memory structures.
|
|
|
|
int diagnostics_finish_unhandled_exception_monitor() {
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Begin the cleanup process by means of shutting down the
|
|
|
|
// message monitoring thread.
|
|
|
|
SetEvent(hExceptionQuitEvent);
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Wait until it is message monitoring thread is shutdown before
|
|
|
|
// cleaning up the structure since we'll need to aquire the
|
|
|
|
// MessageMonitorSync mutex.
|
|
|
|
WaitForSingleObject(hExceptionQuitFinishedEvent, INFINITE);
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Cleanup the handles
|
|
|
|
if (hExceptionDetectedEvent) CloseHandle(hExceptionDetectedEvent);
|
|
|
|
if (hExceptionQuitEvent) CloseHandle(hExceptionQuitEvent);
|
|
|
|
if (hExceptionQuitFinishedEvent) CloseHandle(hExceptionQuitFinishedEvent);
|
|
|
|
if (hExceptionMonitorHalt) CloseHandle(hExceptionMonitorHalt);
|
|
|
|
if (hExceptionMonitorThread) CloseHandle(hExceptionMonitorThread);
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-22 18:19:38 +00:00
|
|
|
// Cleanup the fallback critical section.
|
|
|
|
DeleteCriticalSection(&csExceptionMonitorFallback);
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Dump crash header information
|
|
|
|
//
|
|
|
|
int diagnostics_unhandled_exception_dump_banner() {
|
2006-06-07 08:56:55 +00:00
|
|
|
char szDate[64];
|
|
|
|
char szTime[64];
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2006-06-07 08:56:55 +00:00
|
|
|
strdate(szDate);
|
|
|
|
strtime(szTime);
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2008-05-27 21:18:17 +00:00
|
|
|
fprintf(stderr, "\n\n");
|
|
|
|
fprintf(stderr, "********************\n");
|
|
|
|
fprintf(stderr, "\n\n");
|
|
|
|
fprintf(stderr, "BOINC Windows Runtime Debugger Version %s\n", BOINC_VERSION_STRING);
|
|
|
|
fprintf(stderr, "\n\n");
|
|
|
|
fprintf(stderr, "Dump Timestamp : %s %s\n", szDate, szTime);
|
|
|
|
if (diagnostics_is_flag_set(BOINC_DIAG_BOINCAPPLICATION)) {
|
|
|
|
fprintf(stderr, "Install Directory : %s\n", diagnostics_get_boinc_install_dir());
|
|
|
|
fprintf(stderr, "Data Directory : %s\n", diagnostics_get_boinc_dir());
|
2008-06-09 19:28:35 +00:00
|
|
|
fprintf(stderr, "Project Symstore : %s\n", diagnostics_get_symstore());
|
2008-05-27 21:18:17 +00:00
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Capture the foreground window details for future use.
|
|
|
|
//
|
|
|
|
int diagnostics_capture_foreground_window(PBOINC_WINDOWCAPTURE window_info) {
|
2006-12-21 17:05:58 +00:00
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
|
|
|
DWORD dwCaptureForegroundWindow;
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2007-01-21 16:27:04 +00:00
|
|
|
|
|
|
|
// Initialize structure variables.
|
|
|
|
strcpy(window_info->window_name, "");
|
|
|
|
strcpy(window_info->window_class, "");
|
|
|
|
window_info->hwnd = 0;
|
|
|
|
window_info->window_process_id = 0;
|
|
|
|
window_info->window_thread_id = 0;
|
|
|
|
|
|
|
|
|
2006-12-21 17:05:58 +00:00
|
|
|
// Check the registry to see if we are aloud to capture the foreground
|
|
|
|
// window data. Many people were concerned about privacy issues.
|
|
|
|
//
|
|
|
|
// We'll turn it off by default, but keep it around just in case we need
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
dwCaptureForegroundWindow = 0;
|
|
|
|
dwType = REG_DWORD;
|
|
|
|
dwSize = sizeof(dwCaptureForegroundWindow);
|
|
|
|
diagnostics_get_registry_value(
|
|
|
|
"CaptureForegroundWindow",
|
|
|
|
&dwType,
|
|
|
|
&dwSize,
|
|
|
|
(LPBYTE)&dwCaptureForegroundWindow
|
2006-05-08 18:35:56 +00:00
|
|
|
);
|
|
|
|
|
2006-12-21 17:05:58 +00:00
|
|
|
|
|
|
|
if (dwCaptureForegroundWindow) {
|
|
|
|
window_info->hwnd = GetForegroundWindow();
|
|
|
|
|
|
|
|
window_info->window_thread_id = GetWindowThreadProcessId(
|
|
|
|
window_info->hwnd,
|
|
|
|
&window_info->window_process_id
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only query the window text from windows in a different process space.
|
|
|
|
// All threads that might have windows are suspended in this process
|
|
|
|
// space and attempting to get the window text will deadlock the exception
|
|
|
|
// handler.
|
|
|
|
if (window_info->window_process_id != GetCurrentProcessId()) {
|
2009-04-23 03:40:49 +00:00
|
|
|
GetWindowTextA(
|
2006-12-21 17:05:58 +00:00
|
|
|
window_info->hwnd,
|
|
|
|
window_info->window_name,
|
|
|
|
sizeof(window_info->window_name)
|
|
|
|
);
|
|
|
|
|
2009-04-23 03:40:49 +00:00
|
|
|
GetClassNameA(
|
2006-12-21 17:05:58 +00:00
|
|
|
window_info->hwnd,
|
|
|
|
window_info->window_class,
|
|
|
|
sizeof(window_info->window_class)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2006-05-18 04:36:41 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Dump the foreground window details to stderr.
|
|
|
|
//
|
|
|
|
int diagnostics_foreground_window_dump(PBOINC_WINDOWCAPTURE window_info) {
|
|
|
|
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"*** Foreground Window Data ***\n"
|
|
|
|
" Window Name : %s\n"
|
|
|
|
" Window Class : %s\n"
|
|
|
|
" Window Process ID: %x\n"
|
|
|
|
" Window Thread ID : %x\n\n",
|
|
|
|
window_info->window_name,
|
|
|
|
window_info->window_class,
|
|
|
|
window_info->window_process_id,
|
|
|
|
window_info->window_thread_id
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 23:01:36 +00:00
|
|
|
// Dump the captured information for a the current process.
|
|
|
|
//
|
|
|
|
int diagnostics_dump_process_information() {
|
|
|
|
// Header
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"*** Dump of the Process Statistics: ***\n\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
// I/O Counters
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"- I/O Operations Counters -\n"
|
|
|
|
"Read: %d, Write: %d, Other %d\n"
|
|
|
|
"\n"
|
|
|
|
"- I/O Transfers Counters -\n"
|
|
|
|
"Read: %d, Write: %d, Other %d\n"
|
|
|
|
"\n",
|
|
|
|
diagnostics_process.io_counters.ReadOperationCount,
|
|
|
|
diagnostics_process.io_counters.WriteOperationCount,
|
|
|
|
diagnostics_process.io_counters.OtherOperationCount,
|
|
|
|
diagnostics_process.io_counters.ReadTransferCount,
|
|
|
|
diagnostics_process.io_counters.WriteTransferCount,
|
|
|
|
diagnostics_process.io_counters.OtherTransferCount
|
|
|
|
);
|
|
|
|
|
|
|
|
// VM Counters
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"- Paged Pool Usage -\n"
|
|
|
|
"QuotaPagedPoolUsage: %d, QuotaPeakPagedPoolUsage: %d\n"
|
|
|
|
"QuotaNonPagedPoolUsage: %d, QuotaPeakNonPagedPoolUsage: %d\n"
|
|
|
|
"\n"
|
|
|
|
"- Virtual Memory Usage -\n"
|
|
|
|
"VirtualSize: %d, PeakVirtualSize: %d\n"
|
|
|
|
"\n"
|
|
|
|
"- Pagefile Usage -\n"
|
|
|
|
"PagefileUsage: %d, PeakPagefileUsage: %d\n"
|
|
|
|
"\n"
|
|
|
|
"- Working Set Size -\n"
|
|
|
|
"WorkingSetSize: %d, PeakWorkingSetSize: %d, PageFaultCount: %d\n"
|
|
|
|
"\n",
|
|
|
|
diagnostics_process.vm_counters.QuotaPagedPoolUsage,
|
|
|
|
diagnostics_process.vm_counters.QuotaPeakPagedPoolUsage,
|
|
|
|
diagnostics_process.vm_counters.QuotaNonPagedPoolUsage,
|
|
|
|
diagnostics_process.vm_counters.QuotaPeakNonPagedPoolUsage,
|
|
|
|
diagnostics_process.vm_counters.VirtualSize,
|
|
|
|
diagnostics_process.vm_counters.PeakVirtualSize,
|
|
|
|
diagnostics_process.vm_counters.PagefileUsage,
|
|
|
|
diagnostics_process.vm_counters.PeakPagefileUsage,
|
|
|
|
diagnostics_process.vm_counters.WorkingSetSize,
|
|
|
|
diagnostics_process.vm_counters.PeakWorkingSetSize,
|
|
|
|
diagnostics_process.vm_counters.PageFaultCount
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Dump the captured information for a given thread.
|
|
|
|
//
|
|
|
|
int diagnostics_dump_thread_information(PBOINC_THREADLISTENTRY pThreadEntry) {
|
2006-05-22 02:51:57 +00:00
|
|
|
std::string strStatusExtra;
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
if (pThreadEntry->crash_state == ThreadStateWaiting) {
|
2006-05-22 02:51:57 +00:00
|
|
|
strStatusExtra += "Wait Reason: ";
|
|
|
|
strStatusExtra += diagnostics_format_thread_wait_reason(pThreadEntry->crash_wait_reason);
|
|
|
|
strStatusExtra += ", ";
|
|
|
|
} else {
|
|
|
|
strStatusExtra += "Base Priority: ";
|
|
|
|
strStatusExtra += diagnostics_format_thread_priority(pThreadEntry->crash_base_priority);
|
|
|
|
strStatusExtra += ", ";
|
|
|
|
strStatusExtra += "Priority: ";
|
|
|
|
strStatusExtra += diagnostics_format_thread_priority(pThreadEntry->crash_priority);
|
|
|
|
strStatusExtra += ", ";
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
2007-11-27 19:28:57 +00:00
|
|
|
"*** Dump of thread ID %d (state: %s): ***\n\n"
|
2006-05-08 18:35:56 +00:00
|
|
|
"- Information -\n"
|
|
|
|
"Status: %s, "
|
|
|
|
"Kernel Time: %f, "
|
|
|
|
"User Time: %f, "
|
|
|
|
"Wait Time: %f\n"
|
|
|
|
"\n",
|
|
|
|
pThreadEntry->thread_id,
|
2006-05-22 02:51:57 +00:00
|
|
|
diagnostics_format_thread_state(pThreadEntry->crash_state),
|
|
|
|
strStatusExtra.c_str(),
|
2006-05-08 18:35:56 +00:00
|
|
|
pThreadEntry->crash_kernel_time,
|
|
|
|
pThreadEntry->crash_user_time,
|
|
|
|
pThreadEntry->crash_wait_time
|
2006-05-01 21:36:38 +00:00
|
|
|
);
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-25 11:14:27 +00:00
|
|
|
// Provide a generic way to format exceptions
|
|
|
|
//
|
|
|
|
int diagnostics_dump_generic_exception(char* exception_desc, DWORD exception_code, PVOID exception_address) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Reason: %s (0x%x) at address 0x%p\n\n",
|
|
|
|
exception_desc,
|
|
|
|
exception_code,
|
|
|
|
exception_address
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Dump the exception code record to stderr in a human readable form.
|
|
|
|
//
|
|
|
|
int diagnostics_dump_exception_record(PEXCEPTION_POINTERS pExPtrs) {
|
2006-05-25 11:14:27 +00:00
|
|
|
char status[256];
|
|
|
|
char substatus[256];
|
|
|
|
char message[1024];
|
|
|
|
PVOID exception_address = pExPtrs->ExceptionRecord->ExceptionAddress;
|
|
|
|
DWORD exception_code = pExPtrs->ExceptionRecord->ExceptionCode;
|
|
|
|
PDelayLoadInfo delay_load_info = NULL;
|
|
|
|
|
|
|
|
// Print unhandled exception banner
|
|
|
|
fprintf(stderr, "- Unhandled Exception Record -\n");
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2006-05-25 11:14:27 +00:00
|
|
|
switch (exception_code) {
|
|
|
|
case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND):
|
|
|
|
delay_load_info = (PDelayLoadInfo)pExPtrs->ExceptionRecord->ExceptionInformation[0];
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Delay Load Failure: Attempting to load '%s' failed.\n\n",
|
|
|
|
delay_load_info->szDll
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND):
|
|
|
|
delay_load_info = (PDelayLoadInfo)pExPtrs->ExceptionRecord->ExceptionInformation[0];
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Delay Load Failure: Attempting to find '%s' in '%s' failed.\n\n",
|
|
|
|
delay_load_info->dlp.szProcName,
|
|
|
|
delay_load_info->szDll
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 0xC0000135: // STATUS_DLL_NOT_FOUND
|
|
|
|
case 0xC0000139: // STATUS_ENTRYPOINT_NOT_FOUND
|
|
|
|
case 0xC0000142: // STATUS_DLL_INIT_FAILED
|
|
|
|
case 0xC0000143: // STATUS_MISSING_SYSTEMFILE
|
|
|
|
fprintf(stderr, "%s\n\n", windows_format_error_string(exception_code, message, sizeof(message)));
|
|
|
|
break;
|
2007-01-07 23:35:36 +00:00
|
|
|
case 0xE06D7363:
|
|
|
|
diagnostics_dump_generic_exception("Out Of Memory (C++ Exception)", exception_code, exception_address);
|
|
|
|
break;
|
2006-05-01 21:36:38 +00:00
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
strcpy(status, "Access Violation");
|
|
|
|
if (pExPtrs->ExceptionRecord->NumberParameters == 2) {
|
|
|
|
switch(pExPtrs->ExceptionRecord->ExceptionInformation[0]) {
|
|
|
|
case 0: // read attempt
|
|
|
|
sprintf(substatus, "read attempt to address 0x%8.8X", pExPtrs->ExceptionRecord->ExceptionInformation[1]);
|
|
|
|
break;
|
|
|
|
case 1: // write attempt
|
|
|
|
sprintf(substatus, "write attempt to address 0x%8.8X", pExPtrs->ExceptionRecord->ExceptionInformation[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-05-25 11:14:27 +00:00
|
|
|
fprintf(stderr, "Reason: %s (0x%x) at address 0x%p %s\n\n", status, exception_code, exception_address, substatus);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_DATATYPE_MISALIGNMENT:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Data Type Misalignment", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_BREAKPOINT:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Breakpoint Encountered", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_SINGLE_STEP:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Single Instruction Executed", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Array Bounds Exceeded", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_DENORMAL_OPERAND:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Float Denormal Operand", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Divide by Zero", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_INEXACT_RESULT:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Float Inexact Result", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_INVALID_OPERATION:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Float Invalid Operation", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_OVERFLOW:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Float Overflow", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_STACK_CHECK:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Float Stack Check", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_UNDERFLOW:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Float Underflow", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Integer Divide by Zero", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_INT_OVERFLOW:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Integer Overflow", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_PRIV_INSTRUCTION:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Privileged Instruction", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_IN_PAGE_ERROR:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("In Page Error", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_ILLEGAL_INSTRUCTION:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Illegal Instruction", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Noncontinuable Exception", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_STACK_OVERFLOW:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Stack Overflow", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_INVALID_DISPOSITION:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Invalid Disposition", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_GUARD_PAGE:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Guard Page Violation", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case EXCEPTION_INVALID_HANDLE:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Invalid Handle", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
case CONTROL_C_EXIT:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Ctrl+C Exit", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-05-25 11:14:27 +00:00
|
|
|
diagnostics_dump_generic_exception("Unknown exception", exception_code, exception_address);
|
2006-05-01 21:36:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Priority is given to the worker threads exception code, and then the
|
|
|
|
// graphics thread. If neither of those two threw the exception grab
|
|
|
|
// the exception code of the thread that did.
|
|
|
|
UINT diagnostics_determine_exit_code() {
|
|
|
|
UINT uiReturn = 0;
|
|
|
|
UINT uiIndex = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
|
2007-10-24 23:49:52 +00:00
|
|
|
// Any thread will do at this point
|
2006-05-08 18:35:56 +00:00
|
|
|
size = diagnostics_threads.size();
|
|
|
|
for (uiIndex = 0; uiIndex < size; uiIndex++) {
|
2007-10-24 23:49:52 +00:00
|
|
|
if (diagnostics_threads[uiIndex]->crash_exception_record) {
|
|
|
|
uiReturn =
|
|
|
|
diagnostics_threads[uiIndex]->crash_exception_record->ExceptionRecord->ExceptionCode;
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
return uiReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-24 07:02:52 +00:00
|
|
|
UINT WINAPI diagnostics_unhandled_exception_monitor(LPVOID /* lpParameter */) {
|
2008-01-31 18:34:51 +00:00
|
|
|
DWORD dwEvent = (DWORD)NULL;
|
2006-05-08 18:35:56 +00:00
|
|
|
BOOL bContinue = TRUE;
|
2006-06-08 19:40:14 +00:00
|
|
|
BOOL bDebuggerInitialized = FALSE;
|
2006-05-08 18:35:56 +00:00
|
|
|
HANDLE hEvents[2];
|
|
|
|
unsigned int i;
|
|
|
|
CONTEXT c;
|
|
|
|
BOINC_WINDOWCAPTURE window_info;
|
|
|
|
PBOINC_THREADLISTENTRY pThreadEntry = NULL;
|
|
|
|
|
2007-10-24 23:49:52 +00:00
|
|
|
// We should not suspend our crash dump thread.
|
2006-05-08 18:35:56 +00:00
|
|
|
diagnostics_set_thread_exempt_suspend();
|
|
|
|
|
|
|
|
// Aquire the mutex that will keep all the threads that throw an exception
|
|
|
|
// at bay until we are ready to deal with them.
|
|
|
|
WaitForSingleObject(hExceptionMonitorHalt, INFINITE);
|
|
|
|
|
|
|
|
// Which events do we want to wait for?
|
|
|
|
hEvents[0] = hExceptionQuitEvent;
|
|
|
|
hEvents[1] = hExceptionDetectedEvent;
|
|
|
|
|
2006-05-31 08:14:21 +00:00
|
|
|
// Notify the initialization thread that initialization is complete and now
|
|
|
|
// we are waiting for an exception event.
|
|
|
|
SetEvent(hExceptionMonitorStartedEvent);
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
while (bContinue) {
|
|
|
|
dwEvent = WaitForMultipleObjects(
|
|
|
|
2, // number of objects in array
|
|
|
|
hEvents, // array of objects
|
|
|
|
FALSE, // wait for any
|
|
|
|
INFINITE // wait
|
|
|
|
);
|
|
|
|
switch(dwEvent) {
|
|
|
|
// hExceptionQuitEvent was signaled.
|
|
|
|
case WAIT_OBJECT_0 + 0:
|
|
|
|
|
|
|
|
// We are shutting down so lets cleanup and exit.
|
|
|
|
bContinue = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// hExceptionDetectedEvent was signaled.
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
#ifdef _DEBUG
|
|
|
|
if (diagnostics_is_flag_set(BOINC_DIAG_MEMORYLEAKCHECKENABLED)) {
|
|
|
|
CLEAR_CRT_DEBUG_FIELD(_CRTDBG_LEAK_CHECK_DF);
|
|
|
|
}
|
|
|
|
if (diagnostics_is_flag_set(BOINC_DIAG_HEAPCHECKENABLED)) {
|
|
|
|
CLEAR_CRT_DEBUG_FIELD(_CRTDBG_CHECK_ALWAYS_DF);
|
|
|
|
CLEAR_CRT_DEBUG_FIELD(_CRTDBG_CHECK_EVERY_1024_DF);
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|
|
|
|
|
|
|
|
|
|
|
|
// Enumerate through all the threads so we have a complete list of what we need to dump.
|
|
|
|
diagnostics_update_thread_list();
|
|
|
|
|
|
|
|
// Get any data that will be needed later but will cause a deadlock if called after
|
|
|
|
// the other threads are suspended.
|
|
|
|
diagnostics_capture_foreground_window(&window_info);
|
|
|
|
|
|
|
|
// Wait for the ThreadListSync mutex before writing updates
|
|
|
|
WaitForSingleObject(hThreadListSync, INFINITE);
|
|
|
|
|
|
|
|
// Dump some basic stuff about runtime debugger version and date
|
|
|
|
diagnostics_unhandled_exception_dump_banner();
|
|
|
|
|
|
|
|
#ifndef __CYGWIN__
|
2008-05-21 05:09:15 +00:00
|
|
|
// Kickstart the debugger extensions, look for the debugger files
|
|
|
|
// in the install directory if it is defined, otherwise look
|
|
|
|
// in the data directory.
|
|
|
|
if (0 != strlen(diagnostics_get_boinc_install_dir())) {
|
|
|
|
bDebuggerInitialized = !DebuggerInitialize(
|
|
|
|
diagnostics_get_boinc_install_dir(),
|
|
|
|
diagnostics_get_symstore(),
|
|
|
|
diagnostics_is_proxy_enabled(),
|
|
|
|
diagnostics_get_proxy()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
bDebuggerInitialized = !DebuggerInitialize(
|
|
|
|
diagnostics_get_boinc_dir(),
|
|
|
|
diagnostics_get_symstore(),
|
|
|
|
diagnostics_is_proxy_enabled(),
|
|
|
|
diagnostics_get_proxy()
|
|
|
|
);
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Dump any useful information
|
2006-06-08 19:40:14 +00:00
|
|
|
if (bDebuggerInitialized) DebuggerDisplayDiagnostics();
|
2006-05-01 21:36:38 +00:00
|
|
|
#endif
|
2006-05-23 23:01:36 +00:00
|
|
|
// Dump the process statistics
|
|
|
|
diagnostics_dump_process_information();
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Dump the other threads stack.
|
|
|
|
for (i=0; i<diagnostics_threads.size(); i++) {
|
|
|
|
pThreadEntry = diagnostics_threads[i];
|
|
|
|
if (pThreadEntry->thread_id && !pThreadEntry->crash_suspend_exempt) {
|
|
|
|
|
|
|
|
diagnostics_dump_thread_information(pThreadEntry);
|
|
|
|
|
|
|
|
// Dump the exception record
|
|
|
|
if (pThreadEntry->crash_exception_record) {
|
|
|
|
diagnostics_dump_exception_record(
|
|
|
|
pThreadEntry->crash_exception_record
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diagnostics_is_flag_set(BOINC_DIAG_DUMPCALLSTACKENABLED)) {
|
|
|
|
#ifndef __CYGWIN__
|
2006-06-08 19:40:14 +00:00
|
|
|
if (bDebuggerInitialized) {
|
|
|
|
if (pThreadEntry->crash_exception_record ) {
|
|
|
|
StackwalkFilter(
|
|
|
|
pThreadEntry->crash_exception_record,
|
|
|
|
EXCEPTION_EXECUTE_HANDLER
|
|
|
|
);
|
|
|
|
} else {
|
2009-10-05 15:07:59 +00:00
|
|
|
// Suspend thread before extracting the contexts,
|
|
|
|
// otherwise it'll be trash.
|
|
|
|
SuspendThread(pThreadEntry->thread_handle);
|
|
|
|
|
2006-06-08 19:40:14 +00:00
|
|
|
// Get the thread context
|
|
|
|
memset(&c, 0, sizeof(CONTEXT));
|
|
|
|
c.ContextFlags = CONTEXT_FULL;
|
|
|
|
GetThreadContext(pThreadEntry->thread_handle, &c);
|
|
|
|
|
|
|
|
StackwalkThread(
|
|
|
|
pThreadEntry->thread_handle,
|
|
|
|
&c
|
|
|
|
);
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "Warning: Callstack dumps are not supported on CYGWIN\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
diagnostics_message_monitor_dump();
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
diagnostics_foreground_window_dump(&window_info);
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
fprintf(stderr, "Exiting...\n");
|
2006-05-01 21:36:38 +00:00
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Release the Mutex
|
|
|
|
ReleaseMutex(hThreadListSync);
|
|
|
|
|
|
|
|
// Force terminate the app letting BOINC know an exception has occurred.
|
|
|
|
if (diagnostics_is_aborted_via_gui()) {
|
2006-08-24 07:02:52 +00:00
|
|
|
TerminateProcess(GetCurrentProcess(), (UINT)ERR_ABORTED_VIA_GUI);
|
2006-05-08 18:35:56 +00:00
|
|
|
} else {
|
|
|
|
TerminateProcess(GetCurrentProcess(), diagnostics_determine_exit_code());
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
// Notify the calling thread that the message monitoring thread is
|
|
|
|
// finished.
|
2006-05-17 07:44:54 +00:00
|
|
|
SetEvent(hExceptionQuitFinishedEvent);
|
2006-05-08 18:35:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
|
|
|
|
static int no_reset[SIGRTMAX+1];
|
2006-05-11 23:19:04 +00:00
|
|
|
static int no_ignore[SIGRTMAX+1];
|
|
|
|
static int setup_arrays=0;
|
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
|
|
|
|
void setup_no_reset() {
|
|
|
|
no_reset[SIGILL]=1;
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
no_reset[SIGTRAP]=1;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPRIV
|
|
|
|
no_reset[SIGPRIV]=1;
|
|
|
|
#endif
|
|
|
|
no_reset[SIGINT]=1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void setup_no_ignore() {
|
|
|
|
#ifdef SIGKILL
|
|
|
|
no_ignore[SIGKILL]=1;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
|
|
|
no_ignore[SIGSTOP]=1;
|
|
|
|
#endif
|
|
|
|
no_ignore[SIGSEGV]=1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LONG pass_to_signal_handler(int signum) {
|
|
|
|
void (*handler)(int);
|
|
|
|
|
|
|
|
if (!setup_arrays) {
|
|
|
|
setup_arrays=1;
|
|
|
|
setup_no_ignore();
|
|
|
|
setup_no_reset();
|
|
|
|
}
|
2006-05-11 23:19:04 +00:00
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
// Are we using the default signal handler?
|
|
|
|
// If so return to the exception handler.
|
2006-05-11 23:19:04 +00:00
|
|
|
handler=signal(signum,SIG_DFL);
|
2006-05-11 18:28:35 +00:00
|
|
|
if (handler==SIG_DFL) {
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
2006-05-11 23:19:04 +00:00
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
// Should we ignore this signal?
|
|
|
|
if (handler==SIG_IGN) {
|
|
|
|
signal(signum,handler);
|
|
|
|
// Are we allowed to?
|
|
|
|
if (!no_ignore[signum]) {
|
|
|
|
// Yes? Attempt to ignore the exception.
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
} else {
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
}
|
2006-05-11 23:19:04 +00:00
|
|
|
|
|
|
|
// Call our signal handler, this probably won't return...
|
2006-05-11 18:28:35 +00:00
|
|
|
handler(signum);
|
2006-05-11 23:19:04 +00:00
|
|
|
|
|
|
|
// if it does, reset the signal handler if appropriate.
|
2006-05-11 18:28:35 +00:00
|
|
|
if (no_reset[signum]) signal(signum,handler);
|
2006-05-11 23:19:04 +00:00
|
|
|
|
|
|
|
// try to continue execution
|
2006-05-11 18:28:35 +00:00
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Allow apps to install signal handlers for some exceptions that bypass
|
|
|
|
// the boinc diagnostics. This translates the Windows exceptions into
|
|
|
|
// standard signals.
|
|
|
|
LONG diagnostics_check_signal_handlers(PEXCEPTION_POINTERS pExPtrs) {
|
|
|
|
switch (pExPtrs->ExceptionRecord->ExceptionCode) {
|
|
|
|
case CONTROL_C_EXIT:
|
|
|
|
return pass_to_signal_handler(SIGINT);
|
|
|
|
case EXCEPTION_BREAKPOINT:
|
|
|
|
case EXCEPTION_SINGLE_STEP:
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
return pass_to_signal_handler(SIGTRAP);
|
|
|
|
#else
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case EXCEPTION_FLT_DENORMAL_OPERAND:
|
|
|
|
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
|
|
|
case EXCEPTION_FLT_INEXACT_RESULT:
|
|
|
|
case EXCEPTION_FLT_INVALID_OPERATION:
|
|
|
|
case EXCEPTION_FLT_OVERFLOW:
|
|
|
|
case EXCEPTION_FLT_UNDERFLOW:
|
|
|
|
{
|
|
|
|
LONG rv=pass_to_signal_handler(SIGFPE);
|
|
|
|
/* MS claims ignoring an FP signal
|
|
|
|
* results in an unknown FP state.
|
|
|
|
* Does an _fpreset() help?
|
|
|
|
*/
|
|
|
|
if (rv != EXCEPTION_CONTINUE_SEARCH)
|
|
|
|
_fpreset();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
|
|
|
case EXCEPTION_INT_OVERFLOW:
|
|
|
|
return pass_to_signal_handler(SIGFPE);
|
|
|
|
case EXCEPTION_PRIV_INSTRUCTION:
|
|
|
|
#ifdef SIGPRIV
|
|
|
|
return pass_to_signal_handler(SIGPRIV);
|
|
|
|
// nobreak
|
|
|
|
#endif
|
|
|
|
case EXCEPTION_ILLEGAL_INSTRUCTION:
|
|
|
|
return pass_to_signal_handler(SIGILL);
|
|
|
|
// nobreak
|
|
|
|
case EXCEPTION_DATATYPE_MISALIGNMENT:
|
|
|
|
#ifdef SIGBUS
|
|
|
|
return pass_to_signal_handler(SIGBUS);
|
|
|
|
// nobreak
|
|
|
|
#endif
|
|
|
|
case EXCEPTION_STACK_OVERFLOW:
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
|
|
|
|
case EXCEPTION_IN_PAGE_ERROR:
|
|
|
|
return pass_to_signal_handler(SIGSEGV);
|
|
|
|
// nobreak
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
2006-05-08 18:35:56 +00:00
|
|
|
|
|
|
|
// Let the unhandled exception monitor take care of logging the exception data.
|
|
|
|
// Store the exception pointers and then singal the exception monitor to start
|
|
|
|
// partying on the data.
|
|
|
|
LONG CALLBACK boinc_catch_signal(PEXCEPTION_POINTERS pExPtrs) {
|
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
// Check whether somone has installed a standard C signal handler to
|
|
|
|
// handle this exception.
|
2006-05-11 23:19:04 +00:00
|
|
|
if (diagnostics_check_signal_handlers(pExPtrs) == EXCEPTION_CONTINUE_EXECUTION) {
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
2006-05-11 18:28:35 +00:00
|
|
|
|
2006-05-25 11:14:27 +00:00
|
|
|
fprintf( stderr, "\n\n");
|
2006-05-25 16:56:44 +00:00
|
|
|
fprintf( stderr, "Unhandled Exception Detected...\n\n");
|
|
|
|
|
|
|
|
// Dump what we know about...
|
|
|
|
diagnostics_dump_exception_record(pExPtrs);
|
2006-05-25 11:14:27 +00:00
|
|
|
|
2006-05-22 18:19:38 +00:00
|
|
|
if (hExceptionMonitorThread) {
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2006-05-22 18:19:38 +00:00
|
|
|
// Engage the BOINC Windows Runtime Debugger and dump as much diagnostic
|
|
|
|
// data as possible.
|
|
|
|
//
|
2006-05-25 16:56:44 +00:00
|
|
|
fprintf( stderr, "Engaging BOINC Windows Runtime Debugger...\n\n");
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2006-05-22 18:19:38 +00:00
|
|
|
// Store the exception record pointers.
|
|
|
|
diagnostics_set_thread_exception_record(pExPtrs);
|
|
|
|
|
|
|
|
// Wake the unhandled exception monitor up to process the exception.
|
|
|
|
SetEvent(hExceptionDetectedEvent);
|
|
|
|
|
|
|
|
// Go to sleep waiting for something this thread will never see.
|
|
|
|
WaitForSingleObject(hExceptionMonitorHalt, INFINITE);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// This is a really bad place to be. The unhandled exception monitor wasn't
|
|
|
|
// created, so we need to bail out as quickly as possible.
|
2006-05-25 11:14:27 +00:00
|
|
|
//
|
|
|
|
fprintf( stderr, "BOINC Windows Runtime Debugger not configured, terminating application...\n");
|
|
|
|
|
2006-05-22 18:19:38 +00:00
|
|
|
// Enter the critical section in case multiple threads decide to try and blow
|
|
|
|
// chunks at the same time. Let the OS decide who gets to determine what
|
|
|
|
// error code we return.
|
|
|
|
EnterCriticalSection(&csExceptionMonitorFallback);
|
|
|
|
|
|
|
|
TerminateProcess(GetCurrentProcess(), pExPtrs->ExceptionRecord->ExceptionCode);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&csExceptionMonitorFallback);
|
|
|
|
}
|
2006-05-08 18:35:56 +00:00
|
|
|
|
2006-05-01 21:36:38 +00:00
|
|
|
// We won't make it to this point, but make the compiler happy anyway.
|
2006-05-11 18:28:35 +00:00
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
2006-05-01 21:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-27 21:12:13 +00:00
|
|
|
// Starting with Visual Studio 2005 the C Runtime Library has really started to
|
|
|
|
// enforce parameter validation. Problem is that the parameter validation code
|
|
|
|
// uses its own structured exception handler and terminates without writing
|
|
|
|
// any useful output to stderr. Microsoft has created a hook an application
|
|
|
|
// developer can use to get more debugging information which is the purpose
|
|
|
|
// of this function. When an invalid parameter is passed to the C Runtime
|
|
|
|
// library this function will write whatever trace information it can and
|
|
|
|
// then throw a breakpoint exception to dump all the rest of the useful
|
|
|
|
// information.
|
|
|
|
void boinc_catch_signal_invalid_parameter(
|
2009-11-16 13:13:42 +00:00
|
|
|
const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t /* pReserved */
|
2007-06-27 21:12:13 +00:00
|
|
|
) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"ERROR: Invalid parameter detected in function %s. File: %s Line: %d\n",
|
|
|
|
function,
|
|
|
|
file,
|
|
|
|
line
|
|
|
|
);
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"ERROR: Expression: %s\n",
|
|
|
|
expression
|
|
|
|
);
|
|
|
|
|
|
|
|
// Cause a Debug Breakpoint.
|
|
|
|
DebugBreak();
|
|
|
|
}
|
|
|
|
|