core/CrazyDebugger.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/CrazyDebugger.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,145 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 - * =============================================================================
    1.10 - *
    1.11 - * This program is free software; you can redistribute it and/or modify it under
    1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 - * Free Software Foundation.
    1.14 - * 
    1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 - * details.
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License along with
    1.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 - *
    1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 - * or <http://www.sourcemod.net/license.php>.
    1.31 - *
    1.32 - * Version: $Id$
    1.33 - */
    1.34 -
    1.35 -#if defined CRAZY_DEBUG
    1.36 -#include "sm_globals.h"
    1.37 -#include "sourcemm_api.h"
    1.38 -#include "Tlhelp32.h"
    1.39 -#include "LibrarySys.h"
    1.40 -#include "minidump.h"
    1.41 -
    1.42 -BOOL
    1.43 -WINAPI
    1.44 -MiniDumpWriteDump2(
    1.45 -				  IN HANDLE hProcess,
    1.46 -				  IN DWORD ProcessId,
    1.47 -				  IN HANDLE hFile,
    1.48 -				  IN MINIDUMP_TYPE DumpType,
    1.49 -				  IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
    1.50 -				  IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
    1.51 -				  IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
    1.52 -				  )
    1.53 -{
    1.54 -	DumpType = (MINIDUMP_TYPE)((int)DumpType|MiniDumpWithFullMemory|MiniDumpWithHandleData);
    1.55 -	return MiniDumpWriteDump(hProcess, ProcessId, hFile, DumpType, ExceptionParam, UserStreamParam, CallbackParam);
    1.56 -}
    1.57 -
    1.58 -FARPROC WINAPI GetProcAddress2(HMODULE hModule, LPCSTR lpProcName)
    1.59 -{
    1.60 -	if (strcmp(lpProcName, "MiniDumpWriteDump") == 0)
    1.61 -	{
    1.62 -		return (FARPROC)MiniDumpWriteDump2;
    1.63 -	}
    1.64 -
    1.65 -	return GetProcAddress(hModule, lpProcName);
    1.66 -}
    1.67 -
    1.68 -class CrazyWindowsDebugger : public SMGlobalClass
    1.69 -{
    1.70 -public:
    1.71 -	void OnSourceModAllInitialized()
    1.72 -	{
    1.73 -		HANDLE hModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
    1.74 -		MODULEENTRY32 me32;
    1.75 -
    1.76 -		me32.dwSize = sizeof(MODULEENTRY32);
    1.77 -
    1.78 -		if (!Module32First(hModuleList, &me32))
    1.79 -		{
    1.80 -			Error("Could not initialize crazy debugger!");
    1.81 -		}
    1.82 -
    1.83 -		bool found = false;
    1.84 -
    1.85 -		do
    1.86 -		{
    1.87 -			if (strcasecmp(me32.szModule, "steam.dll") == 0)
    1.88 -			{
    1.89 -				IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)me32.modBaseAddr;
    1.90 -				if (dos->e_magic != IMAGE_DOS_SIGNATURE)
    1.91 -				{
    1.92 -					Error("[SM] Could not detect steam.dll with valid DOS signature");
    1.93 -				}
    1.94 -				char *base = (char *)dos;
    1.95 -				IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(base + dos->e_lfanew);
    1.96 -				if (nt->Signature != IMAGE_NT_SIGNATURE)
    1.97 -				{
    1.98 -					Error("[SM] Could not detect steam.dll with valid NT signature");
    1.99 -				}
   1.100 -				IMAGE_IMPORT_DESCRIPTOR *desc = 
   1.101 -					(IMAGE_IMPORT_DESCRIPTOR *)
   1.102 -					(base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
   1.103 -				if (base == (char *)desc)
   1.104 -				{
   1.105 -					Error("[SM] Could not find the steam.dll IAT");
   1.106 -				}
   1.107 -				while (desc->Name)
   1.108 -				{
   1.109 -					if (desc->FirstThunk != 0)
   1.110 -					{
   1.111 -						IMAGE_THUNK_DATA *data = (IMAGE_THUNK_DATA *)(base + desc->OriginalFirstThunk);
   1.112 -						DWORD *iat = (DWORD *)(base + desc->FirstThunk);
   1.113 -						while (data->u1.Function)
   1.114 -						{
   1.115 -							if ((data->u1.Ordinal & IMAGE_ORDINAL_FLAG32) != IMAGE_ORDINAL_FLAG32)
   1.116 -							{
   1.117 -								IMAGE_IMPORT_BY_NAME *import = (IMAGE_IMPORT_BY_NAME *)(base + data->u1.AddressOfData);
   1.118 -								if (strcmp((char *)import->Name, "GetProcAddress") == 0)
   1.119 -								{
   1.120 -									DWORD oldprot, oldprot2;
   1.121 -									VirtualProtect(iat, 4, PAGE_READWRITE, &oldprot);
   1.122 -									*iat = (DWORD)GetProcAddress2;
   1.123 -									VirtualProtect(iat, 4, oldprot, &oldprot2);
   1.124 -									found = true;
   1.125 -									goto _end;
   1.126 -								}
   1.127 -							}
   1.128 -							data++;
   1.129 -							iat++;
   1.130 -						}
   1.131 -					}
   1.132 -					desc++;
   1.133 -				}
   1.134 -				break;
   1.135 -			}
   1.136 -		} while  (Module32Next(hModuleList, &me32));
   1.137 -
   1.138 -_end:
   1.139 -
   1.140 -		if (!found)
   1.141 -		{
   1.142 -			Error("Could not find steam.dll's GetProcAddress IAT entry");
   1.143 -		}
   1.144 -
   1.145 -		CloseHandle(hModuleList);
   1.146 -	}
   1.147 -} s_CrazyDebugger;
   1.148 -#endif