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