PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llwindebug.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 191 lines | 122 code | 31 blank | 38 comment | 15 complexity | 19c343004ff573d8636b5bbfabb8847e MD5 | raw file
  1/**
  2 * @file llwindebug.cpp
  3 * @brief Windows debugging functions
  4 *
  5 * $LicenseInfo:firstyear=2004&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26#include "llviewerprecompiledheaders.h"
 27
 28#include "llwindebug.h"
 29#include "lldir.h"
 30
 31
 32// based on dbghelp.h
 33typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
 34									CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
 35									CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
 36									CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
 37									);
 38
 39MINIDUMPWRITEDUMP f_mdwp = NULL;
 40
 41
 42class LLMemoryReserve {
 43public:
 44	LLMemoryReserve();
 45	~LLMemoryReserve();
 46	void reserve();
 47	void release();
 48protected:
 49	unsigned char *mReserve;
 50	static const size_t MEMORY_RESERVATION_SIZE;
 51};
 52
 53LLMemoryReserve::LLMemoryReserve() :
 54	mReserve(NULL)
 55{
 56};
 57
 58LLMemoryReserve::~LLMemoryReserve()
 59{
 60	release();
 61}
 62
 63// I dunno - this just seemed like a pretty good value.
 64const size_t LLMemoryReserve::MEMORY_RESERVATION_SIZE = 5 * 1024 * 1024;
 65
 66void LLMemoryReserve::reserve()
 67{
 68	if(NULL == mReserve)
 69		mReserve = new unsigned char[MEMORY_RESERVATION_SIZE];
 70};
 71
 72void LLMemoryReserve::release()
 73{
 74	delete [] mReserve;
 75	mReserve = NULL;
 76};
 77
 78static LLMemoryReserve gEmergencyMemoryReserve;
 79
 80
 81LONG NTAPI vectoredHandler(PEXCEPTION_POINTERS exception_infop)
 82{
 83	LLWinDebug::instance().generateMinidump(exception_infop);
 84	return EXCEPTION_CONTINUE_SEARCH;
 85}
 86
 87// static
 88void  LLWinDebug::init()
 89{
 90	static bool s_first_run = true;
 91	// Load the dbghelp dll now, instead of waiting for the crash.
 92	// Less potential for stack mangling
 93
 94	// Don't install vectored exception handler if being debugged.
 95	if(IsDebuggerPresent()) return;
 96
 97	if (s_first_run)
 98	{
 99		// First, try loading from the directory that the app resides in.
100		std::string local_dll_name = gDirUtilp->findFile("dbghelp.dll", gDirUtilp->getWorkingDir(), gDirUtilp->getExecutableDir());
101
102		HMODULE hDll = NULL;
103		hDll = LoadLibraryA(local_dll_name.c_str());
104		if (!hDll)
105		{
106			hDll = LoadLibrary(L"dbghelp.dll");
107		}
108
109		if (!hDll)
110		{
111			LL_WARNS("AppInit") << "Couldn't find dbghelp.dll!" << LL_ENDL;
112		}
113		else
114		{
115			f_mdwp = (MINIDUMPWRITEDUMP) GetProcAddress(hDll, "MiniDumpWriteDump");
116
117			if (!f_mdwp)
118			{
119				FreeLibrary(hDll);
120				hDll = NULL;
121			}
122		}
123
124		gEmergencyMemoryReserve.reserve();
125
126		s_first_run = false;
127
128		// Add this exeption hanlder to save windows style minidump.
129		AddVectoredExceptionHandler(0, &vectoredHandler);
130	}
131}
132
133void LLWinDebug::writeDumpToFile(MINIDUMP_TYPE type, MINIDUMP_EXCEPTION_INFORMATION *ExInfop, const std::string& filename)
134{
135	// Temporary fix to switch out the code that writes the DMP file.
136	// Fix coming that doesn't write a mini dump file for regular C++ exceptions.
137	const bool enable_write_dump_file = false;
138	if ( enable_write_dump_file )
139	{
140		if(f_mdwp == NULL || gDirUtilp == NULL)
141		{
142			return;
143		}
144		else
145		{
146			std::string dump_path = gDirUtilp->getExpandedFilename(LL_PATH_LOGS, filename);
147
148			HANDLE hFile = CreateFileA(dump_path.c_str(),
149										GENERIC_WRITE,
150										FILE_SHARE_WRITE,
151										NULL,
152										CREATE_ALWAYS,
153										FILE_ATTRIBUTE_NORMAL,
154										NULL);
155
156			if (hFile != INVALID_HANDLE_VALUE)
157			{
158				// Write the dump, ignoring the return value
159				f_mdwp(GetCurrentProcess(),
160						GetCurrentProcessId(),
161						hFile,
162						type,
163						ExInfop,
164						NULL,
165						NULL);
166
167				CloseHandle(hFile);
168			}
169
170		}
171	}
172}
173
174// static
175void LLWinDebug::generateMinidump(struct _EXCEPTION_POINTERS *exception_infop)
176{
177	std::string dump_path = gDirUtilp->getExpandedFilename(LL_PATH_LOGS,
178												"SecondLifeException");
179	if (exception_infop)
180	{
181		// Since there is exception info... Release the hounds.
182		gEmergencyMemoryReserve.release();
183
184		_MINIDUMP_EXCEPTION_INFORMATION ExInfo;
185
186		ExInfo.ThreadId = ::GetCurrentThreadId();
187		ExInfo.ExceptionPointers = exception_infop;
188		ExInfo.ClientPointers = NULL;
189		writeDumpToFile((MINIDUMP_TYPE)(MiniDumpWithDataSegs | MiniDumpWithIndirectlyReferencedMemory), &ExInfo, "SecondLife.dmp");
190	}
191}