3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #if defined CRAZY_DEBUG
34 #include "sm_globals.h"
35 #include "sourcemm_api.h"
37 #include "LibrarySys.h"
46 IN MINIDUMP_TYPE DumpType,
47 IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
48 IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
49 IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
52 DumpType = (MINIDUMP_TYPE)((int)DumpType|MiniDumpWithFullMemory|MiniDumpWithHandleData);
53 return MiniDumpWriteDump(hProcess, ProcessId, hFile, DumpType, ExceptionParam, UserStreamParam, CallbackParam);
56 FARPROC WINAPI GetProcAddress2(HMODULE hModule, LPCSTR lpProcName)
58 if (strcmp(lpProcName, "MiniDumpWriteDump") == 0)
60 return (FARPROC)MiniDumpWriteDump2;
63 return GetProcAddress(hModule, lpProcName);
66 class CrazyWindowsDebugger : public SMGlobalClass
69 void OnSourceModAllInitialized()
71 HANDLE hModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
74 me32.dwSize = sizeof(MODULEENTRY32);
76 if (!Module32First(hModuleList, &me32))
78 Error("Could not initialize crazy debugger!");
85 if (strcasecmp(me32.szModule, "steam.dll") == 0)
87 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)me32.modBaseAddr;
88 if (dos->e_magic != IMAGE_DOS_SIGNATURE)
90 Error("[SM] Could not detect steam.dll with valid DOS signature");
92 char *base = (char *)dos;
93 IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(base + dos->e_lfanew);
94 if (nt->Signature != IMAGE_NT_SIGNATURE)
96 Error("[SM] Could not detect steam.dll with valid NT signature");
98 IMAGE_IMPORT_DESCRIPTOR *desc =
99 (IMAGE_IMPORT_DESCRIPTOR *)
100 (base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
101 if (base == (char *)desc)
103 Error("[SM] Could not find the steam.dll IAT");
107 if (desc->FirstThunk != 0)
109 IMAGE_THUNK_DATA *data = (IMAGE_THUNK_DATA *)(base + desc->OriginalFirstThunk);
110 DWORD *iat = (DWORD *)(base + desc->FirstThunk);
111 while (data->u1.Function)
113 if ((data->u1.Ordinal & IMAGE_ORDINAL_FLAG32) != IMAGE_ORDINAL_FLAG32)
115 IMAGE_IMPORT_BY_NAME *import = (IMAGE_IMPORT_BY_NAME *)(base + data->u1.AddressOfData);
116 if (strcmp((char *)import->Name, "GetProcAddress") == 0)
118 DWORD oldprot, oldprot2;
119 VirtualProtect(iat, 4, PAGE_READWRITE, &oldprot);
120 *iat = (DWORD)GetProcAddress2;
121 VirtualProtect(iat, 4, oldprot, &oldprot2);
134 } while (Module32Next(hModuleList, &me32));
140 Error("Could not find steam.dll's GetProcAddress IAT entry");
143 CloseHandle(hModuleList);