ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #if 0
ds@1952: #include "sm_globals.h"
ds@1952: #include "sourcemm_api.h"
ds@1952: #include "Tlhelp32.h"
ds@1952: #include "LibrarySys.h"
ds@1952: #include "minidump.h"
ds@1952:
ds@1952: BOOL
ds@1952: WINAPI
ds@1952: MiniDumpWriteDump2(
ds@1952: IN HANDLE hProcess,
ds@1952: IN DWORD ProcessId,
ds@1952: IN HANDLE hFile,
ds@1952: IN MINIDUMP_TYPE DumpType,
ds@1952: IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
ds@1952: IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
ds@1952: IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
ds@1952: )
ds@1952: {
ds@1952: DumpType = (MINIDUMP_TYPE)((int)DumpType|MiniDumpWithFullMemory|MiniDumpWithHandleData);
ds@1952: return MiniDumpWriteDump(hProcess, ProcessId, hFile, DumpType, ExceptionParam, UserStreamParam, CallbackParam);
ds@1952: }
ds@1952:
ds@1952: FARPROC WINAPI GetProcAddress2(HMODULE hModule, LPCSTR lpProcName)
ds@1952: {
ds@1952: if (strcmp(lpProcName, "MiniDumpWriteDump") == 0)
ds@1952: {
ds@1952: return (FARPROC)MiniDumpWriteDump2;
ds@1952: }
ds@1952:
ds@1952: return GetProcAddress(hModule, lpProcName);
ds@1952: }
ds@1952:
ds@1952: class CrazyWindowsDebugger : public SMGlobalClass
ds@1952: {
ds@1952: public:
ds@1952: void OnSourceModAllInitialized()
ds@1952: {
ds@1952: HANDLE hModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
ds@1952: MODULEENTRY32 me32;
ds@1952:
ds@1952: me32.dwSize = sizeof(MODULEENTRY32);
ds@1952:
ds@1952: if (!Module32First(hModuleList, &me32))
ds@1952: {
ds@1952: Error("Could not initialize crazy debugger!");
ds@1952: }
ds@1952:
ds@1952: bool found = false;
ds@1952:
ds@1952: do
ds@1952: {
ds@1952: if (strcasecmp(me32.szModule, "steam.dll") == 0)
ds@1952: {
ds@1952: IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)me32.modBaseAddr;
ds@1952: if (dos->e_magic != IMAGE_DOS_SIGNATURE)
ds@1952: {
ds@1952: Error("[SM] Could not detect steam.dll with valid DOS signature");
ds@1952: }
ds@1952: char *base = (char *)dos;
ds@1952: IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(base + dos->e_lfanew);
ds@1952: if (nt->Signature != IMAGE_NT_SIGNATURE)
ds@1952: {
ds@1952: Error("[SM] Could not detect steam.dll with valid NT signature");
ds@1952: }
ds@1952: IMAGE_IMPORT_DESCRIPTOR *desc =
ds@1952: (IMAGE_IMPORT_DESCRIPTOR *)
ds@1952: (base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
ds@1952: if (base == (char *)desc)
ds@1952: {
ds@1952: Error("[SM] Could not find the steam.dll IAT");
ds@1952: }
ds@1952: while (desc->Name)
ds@1952: {
ds@1952: if (desc->FirstThunk != 0)
ds@1952: {
ds@1952: IMAGE_THUNK_DATA *data = (IMAGE_THUNK_DATA *)(base + desc->OriginalFirstThunk);
ds@1952: DWORD *iat = (DWORD *)(base + desc->FirstThunk);
ds@1952: while (data->u1.Function)
ds@1952: {
ds@1952: if ((data->u1.Ordinal & IMAGE_ORDINAL_FLAG32) != IMAGE_ORDINAL_FLAG32)
ds@1952: {
ds@1952: IMAGE_IMPORT_BY_NAME *import = (IMAGE_IMPORT_BY_NAME *)(base + data->u1.AddressOfData);
ds@1952: if (strcmp((char *)import->Name, "GetProcAddress") == 0)
ds@1952: {
ds@1952: DWORD oldprot, oldprot2;
ds@1952: VirtualProtect(iat, 4, PAGE_READWRITE, &oldprot);
ds@1952: *iat = (DWORD)GetProcAddress2;
ds@1952: VirtualProtect(iat, 4, oldprot, &oldprot2);
ds@1952: found = true;
ds@1952: goto _end;
ds@1952: }
ds@1952: }
ds@1952: data++;
ds@1952: iat++;
ds@1952: }
ds@1952: }
ds@1952: desc++;
ds@1952: }
ds@1952: break;
ds@1952: }
ds@1952: } while (Module32Next(hModuleList, &me32));
ds@1952:
ds@1952: _end:
ds@1952:
ds@1952: if (!found)
ds@1952: {
ds@1952: Error("Could not find steam.dll's GetProcAddress IAT entry");
ds@1952: }
ds@1952:
ds@1952: CloseHandle(hModuleList);
ds@1952: }
ds@1952: } s_CrazyDebugger;
ds@1952: #endif