3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #if defined CRAZY_DEBUG
15 #include "sm_globals.h"
16 #include "sourcemm_api.h"
18 #include "LibrarySys.h"
27 IN MINIDUMP_TYPE DumpType,
28 IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
29 IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
30 IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
33 DumpType = (MINIDUMP_TYPE)((int)DumpType|MiniDumpWithFullMemory|MiniDumpWithHandleData);
34 return MiniDumpWriteDump(hProcess, ProcessId, hFile, DumpType, ExceptionParam, UserStreamParam, CallbackParam);
37 FARPROC WINAPI GetProcAddress2(HMODULE hModule, LPCSTR lpProcName)
39 if (strcmp(lpProcName, "MiniDumpWriteDump") == 0)
41 return (FARPROC)MiniDumpWriteDump2;
44 return GetProcAddress(hModule, lpProcName);
47 class CrazyWindowsDebugger : public SMGlobalClass
50 void OnSourceModAllInitialized()
52 HANDLE hModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
55 me32.dwSize = sizeof(MODULEENTRY32);
57 if (!Module32First(hModuleList, &me32))
59 Error("Could not initialize crazy debugger!");
66 if (strcasecmp(me32.szModule, "steam.dll") == 0)
68 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)me32.modBaseAddr;
69 if (dos->e_magic != IMAGE_DOS_SIGNATURE)
71 Error("[SM] Could not detect steam.dll with valid DOS signature");
73 char *base = (char *)dos;
74 IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(base + dos->e_lfanew);
75 if (nt->Signature != IMAGE_NT_SIGNATURE)
77 Error("[SM] Could not detect steam.dll with valid NT signature");
79 IMAGE_IMPORT_DESCRIPTOR *desc =
80 (IMAGE_IMPORT_DESCRIPTOR *)
81 (base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
82 if (base == (char *)desc)
84 Error("[SM] Could not find the steam.dll IAT");
88 if (desc->FirstThunk != 0)
90 IMAGE_THUNK_DATA *data = (IMAGE_THUNK_DATA *)(base + desc->OriginalFirstThunk);
91 DWORD *iat = (DWORD *)(base + desc->FirstThunk);
92 while (data->u1.Function)
94 if ((data->u1.Ordinal & IMAGE_ORDINAL_FLAG32) != IMAGE_ORDINAL_FLAG32)
96 IMAGE_IMPORT_BY_NAME *import = (IMAGE_IMPORT_BY_NAME *)(base + data->u1.AddressOfData);
97 if (strcmp((char *)import->Name, "GetProcAddress") == 0)
99 DWORD oldprot, oldprot2;
100 VirtualProtect(iat, 4, PAGE_READWRITE, &oldprot);
101 *iat = (DWORD)GetProcAddress2;
102 VirtualProtect(iat, 4, oldprot, &oldprot2);
115 } while (Module32Next(hModuleList, &me32));
121 Error("Could not find steam.dll's GetProcAddress IAT entry");
124 CloseHandle(hModuleList);