dfhack/library/DFProcess-windows-SHM.cpp

424 lines
11 KiB
C++

2010-01-07 22:56:36 -07:00
/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include "PlatformInternal.h"
#include <cstring>
2011-04-16 17:48:58 -06:00
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <map>
using namespace std;
#include "SHMProcess.h"
#include "dfhack/VersionInfo.h"
2010-05-26 00:42:09 -06:00
#include "dfhack/DFError.h"
#include "shms.h"
#include "mod-core.h"
2010-01-07 22:56:36 -07:00
using namespace DFHack;
SHMProcess::Private::Private(SHMProcess * self_)
2010-01-07 22:56:36 -07:00
{
memdescriptor = NULL;
process_ID = 0;
attached = false;
locked = false;
identified = false;
useYield = 0;
DFSVMutex = 0;
DFCLMutex = 0;
DFCLSuspendMutex = 0;
self = self_;
}
2010-01-10 23:27:59 -07:00
2010-03-13 09:44:36 -07:00
bool SHMProcess::Private::SetAndWait (uint32_t state)
2010-01-07 22:56:36 -07:00
{
uint32_t cnt = 0;
2010-03-13 09:44:36 -07:00
if(!attached) return false;
SHMCMD = state;
2010-03-04 16:05:01 -07:00
while (SHMCMD == state)
2010-01-07 22:56:36 -07:00
{
// yield the CPU, only on single-core CPUs
if(useYield)
{
SCHED_YIELD
}
2010-01-07 22:56:36 -07:00
if(cnt == 10000)
{
2010-03-13 09:44:36 -07:00
if(!AreLocksOk())// DF not there anymore?
2010-01-07 22:56:36 -07:00
{
2010-03-11 16:13:50 -07:00
UnmapViewOfFile(shm_addr);
2010-03-13 09:44:36 -07:00
FreeLocks();
attached = locked = identified = false;
// we aren't the current process anymore
2010-03-08 19:30:22 -07:00
throw Error::SHMServerDisappeared();
2010-01-07 22:56:36 -07:00
}
else
{
cnt = 0;
}
}
cnt++;
}
// server returned a generic error
if(SHMCMD == CORE_ERROR)
2010-01-07 22:56:36 -07:00
{
return false;
}
return true;
}
bool SHMProcess::SetAndWait (uint32_t state)
{
return d->SetAndWait(state);
}
uint32_t OS_getAffinity()
{
HANDLE hProcess = GetCurrentProcess();
DWORD dwProcessAffinityMask, dwSystemAffinityMask;
GetProcessAffinityMask( hProcess, &dwProcessAffinityMask, &dwSystemAffinityMask );
return dwProcessAffinityMask;
}
2010-03-13 09:44:36 -07:00
void SHMProcess::Private::FreeLocks()
{
2010-03-13 09:44:36 -07:00
attachmentIdx = -1;
if(DFCLMutex != 0)
{
ReleaseMutex(DFCLMutex);
CloseHandle(DFCLMutex);
DFCLMutex = 0;
}
if(DFSVMutex != 0)
{
CloseHandle(DFSVMutex);
DFSVMutex = 0;
}
if(DFCLSuspendMutex != 0)
{
ReleaseMutex(DFCLSuspendMutex);
CloseHandle(DFCLSuspendMutex);
// FIXME: maybe also needs ReleaseMutex!
DFCLSuspendMutex = 0;
locked = false;
}
}
bool SHMProcess::Private::GetLocks()
{
char name[256];
// try to acquire locks
// look at the server lock, if it's locked, the server is present
sprintf(name,"DFSVMutex-%d",process_ID);
DFSVMutex = OpenMutex(SYNCHRONIZE,0, name);
if(DFSVMutex == 0)
{
// cerr << "can't open sv lock" << endl;
return false;
2010-03-13 09:44:36 -07:00
}
// unlike the F_TEST of lockf, this one actually locks. we have to release
if(WaitForSingleObject(DFSVMutex,0) == 0)
{
ReleaseMutex(DFSVMutex);
// cerr << "sv lock not locked" << endl;
CloseHandle(DFSVMutex);
DFSVMutex = 0;
return false;
}
2010-03-13 09:44:36 -07:00
for(int i = 0; i < SHM_MAX_CLIENTS; i++)
{
// open the client suspend locked
sprintf(name, "DFCLSuspendMutex-%d-%d",process_ID,i);
DFCLSuspendMutex = OpenMutex(SYNCHRONIZE,0, name);
if(DFCLSuspendMutex == 0)
{
//cerr << "can't open cl S-lock " << i << endl;
// couldn't open lock
continue;
}
// open the client lock, try to lock it
2010-03-13 09:44:36 -07:00
sprintf(name,"DFCLMutex-%d-%d",process_ID,i);
DFCLMutex = OpenMutex(SYNCHRONIZE,0,name);
if(DFCLMutex == 0)
{
//cerr << "can't open cl lock " << i << endl;
CloseHandle(DFCLSuspendMutex);
locked = false;
DFCLSuspendMutex = 0;
continue;
}
uint32_t waitstate = WaitForSingleObject(DFCLMutex,0);
if(waitstate == WAIT_FAILED || waitstate == WAIT_TIMEOUT )
{
//cerr << "can't acquire cl lock " << i << endl;
CloseHandle(DFCLSuspendMutex);
locked = false;
DFCLSuspendMutex = 0;
CloseHandle(DFCLMutex);
DFCLMutex = 0;
continue;
}
// ok, we have all the locks we need!
attachmentIdx = i;
return true;
}
CloseHandle(DFSVMutex);
DFSVMutex = 0;
// cerr << "can't get any client locks" << endl;
return false;
}
2010-03-13 09:44:36 -07:00
// is the other side still there?
bool SHMProcess::Private::AreLocksOk()
2010-01-07 22:56:36 -07:00
{
2010-03-13 09:44:36 -07:00
// both locks are inited (we hold our lock)
if(DFCLMutex != 0 && DFSVMutex != 0)
2010-03-13 09:44:36 -07:00
{
// try if CL mutex is free
switch (WaitForSingleObject(DFSVMutex,0))
{
case WAIT_ABANDONED:
case WAIT_OBJECT_0:
{
ReleaseMutex(DFSVMutex);
return false;
}
case WAIT_TIMEOUT:
{
// mutex is held by DF
return true;
}
2010-03-13 09:44:36 -07:00
default:
case WAIT_FAILED:
{
// TODO: now how do I respond to this?
return false;
}
}
}
return false;
}
2011-02-24 03:43:33 -07:00
bool SHMProcess::Private::validate(VersionInfoFactory * factory)
2010-03-13 09:44:36 -07:00
{
// try to identify the DF version
2011-02-17 17:14:16 -07:00
IMAGE_NT_HEADERS pe_header;
2010-03-13 09:44:36 -07:00
IMAGE_SECTION_HEADER sections[16];
HMODULE hmod = NULL;
DWORD junk;
HANDLE hProcess;
bool found = false;
identified = false;
// open process, we only need the process open
2010-03-13 09:44:36 -07:00
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, process_ID );
if (NULL == hProcess)
return false;
2010-03-13 09:44:36 -07:00
// try getting the first module of the process
if(EnumProcessModules(hProcess, &hmod, 1 * sizeof(HMODULE), &junk) == 0)
{
CloseHandle(hProcess);
// cout << "EnumProcessModules fail'd" << endl;
return false;
}
// got base ;)
uint32_t base = (uint32_t)hmod;
2010-03-13 09:44:36 -07:00
// read from this process
uint32_t pe_offset = self->Process::readDWord(base+0x3C);
2010-04-18 16:56:22 -06:00
self->read(base + pe_offset , sizeof(pe_header), (uint8_t *)&pe_header);
self->read(base + pe_offset+ sizeof(pe_header), sizeof(sections) , (uint8_t *)&sections );
2011-02-24 03:43:33 -07:00
VersionInfo* vinfo = factory->getVersionInfoByPETimestamp(pe_header.FileHeader.TimeDateStamp);
if(vinfo)
2010-03-13 09:44:36 -07:00
{
2011-02-24 03:43:33 -07:00
VersionInfo *m = new VersionInfo(*vinfo);
m->RebaseAll(base);
memdescriptor = m;
m->setParentProcess(self);
identified = true;
vector_start = memdescriptor->getGroup("vector")->getOffset("start");
2011-02-24 03:43:33 -07:00
CloseHandle(hProcess);
return true;
2010-03-13 09:44:36 -07:00
}
return false;
}
2010-01-07 22:56:36 -07:00
bool SHMProcess::getThreadIDs(vector<uint32_t> & threads )
2010-01-07 22:56:36 -07:00
{
HANDLE AllThreads = INVALID_HANDLE_VALUE;
2010-03-13 09:44:36 -07:00
THREADENTRY32 te32;
AllThreads = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
if( AllThreads == INVALID_HANDLE_VALUE )
2010-03-13 09:44:36 -07:00
{
return false;
2010-03-13 09:44:36 -07:00
}
te32.dwSize = sizeof(THREADENTRY32 );
if( !Thread32First( AllThreads, &te32 ) )
2010-03-13 09:44:36 -07:00
{
CloseHandle( AllThreads );
return false;
}
do
{
2010-03-13 09:44:36 -07:00
if( te32.th32OwnerProcessID == d->process_ID )
{
threads.push_back(te32.th32ThreadID);
}
} while( Thread32Next(AllThreads, &te32 ) );
2010-03-13 09:44:36 -07:00
CloseHandle( AllThreads );
return true;
2010-01-07 22:56:36 -07:00
}
2010-03-13 09:44:36 -07:00
//FIXME: use VirtualQuery to probe for memory ranges, cross-reference with base-corrected PE segment entries
void SHMProcess::getMemRanges( vector<t_memrange> & ranges )
2010-01-07 22:56:36 -07:00
{
2011-02-23 17:32:15 -07:00
// BLAH
ranges.clear();
2010-01-07 22:56:36 -07:00
}
bool SHMProcess::acquireSuspendLock()
2010-01-07 22:56:36 -07:00
{
return ( WaitForSingleObject(d->DFCLSuspendMutex,INFINITE) == 0 );
2010-01-07 22:56:36 -07:00
}
bool SHMProcess::releaseSuspendLock()
2010-01-07 22:56:36 -07:00
{
return ( ReleaseMutex(d->DFCLSuspendMutex) != 0);
2010-01-07 22:56:36 -07:00
}
bool SHMProcess::attach()
2010-01-07 22:56:36 -07:00
{
2010-04-18 13:30:02 -06:00
if(d->attached)
2010-01-07 22:56:36 -07:00
{
2010-04-18 13:30:02 -06:00
if(!d->locked)
return suspend();
return true;
2010-01-07 22:56:36 -07:00
}
2010-03-13 09:44:36 -07:00
//cerr << "attach" << endl;// FIXME: throw
if(!d->GetLocks())
2010-01-07 22:56:36 -07:00
{
2010-03-13 09:44:36 -07:00
//cerr << "server is full or not really there!" << endl;
2010-01-07 22:56:36 -07:00
return false;
}
2010-03-13 09:44:36 -07:00
/*
* Locate the segment.
*/
char shmname [256];
2010-03-11 16:13:50 -07:00
sprintf(shmname,"DFShm-%d",d->process_ID);
HANDLE shmHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS,false,shmname);
2010-01-10 23:27:59 -07:00
if(!shmHandle)
{
2010-03-13 09:44:36 -07:00
d->FreeLocks();
//ReleaseMutex(d->DFCLMutex);
2010-01-10 23:27:59 -07:00
return false; // we couldn't lock it
}
2010-03-13 09:44:36 -07:00
/*
* Attach the segment
*/
d->shm_addr = (char *) MapViewOfFile(shmHandle,FILE_MAP_ALL_ACCESS, 0,0, SHM_SIZE);
2010-03-11 16:13:50 -07:00
if(!d->shm_addr)
2010-01-10 23:27:59 -07:00
{
CloseHandle(shmHandle);
2010-03-13 09:44:36 -07:00
//ReleaseMutex(d->DFCLMutex);
d->FreeLocks();
2010-03-13 09:44:36 -07:00
return false; // we couldn't attach the mapping // FIXME: throw
2010-01-10 23:27:59 -07:00
}
2010-03-13 09:44:36 -07:00
// we close the handle right here - it's not needed anymore
2010-01-10 23:27:59 -07:00
CloseHandle(shmHandle);
2010-01-10 23:27:59 -07:00
d->attached = true;
2010-03-13 09:44:36 -07:00
if(!suspend())
{
UnmapViewOfFile(d->shm_addr);
d->FreeLocks();
//cerr << "unable to suspend" << endl;// FIXME: throw
return false;
}
2010-01-10 23:27:59 -07:00
return true;
2010-01-07 22:56:36 -07:00
}
bool SHMProcess::detach()
2010-01-07 22:56:36 -07:00
{
if(!d->attached) return true;
2010-03-13 09:44:36 -07:00
if(d->locked)
{
resume();
}
2010-01-07 22:56:36 -07:00
// detach segment
2010-03-11 16:13:50 -07:00
UnmapViewOfFile(d->shm_addr);
2010-01-10 23:27:59 -07:00
// release it for some other client
2010-03-13 09:44:36 -07:00
//ReleaseMutex(d->DFCLMutex); // we keep the mutex handles
d->FreeLocks();
2010-01-10 23:27:59 -07:00
d->attached = false;
2010-03-13 09:44:36 -07:00
d->locked = false;
d->shm_addr = false;
2010-01-10 23:27:59 -07:00
return true;
2010-01-07 22:56:36 -07:00
}
void SHMProcess::readSTLVector(const uint32_t address, t_vecTriplet & triplet)
{
read(address + d->vector_start, sizeof(triplet), (uint8_t *) &triplet);
}
string SHMProcess::readClassName (uint32_t vptr)
2010-02-18 18:48:03 -07:00
{
int rtti = Process::readDWord(vptr - 0x4);
int typeinfo = Process::readDWord(rtti + 0xC);
2010-02-18 18:48:03 -07:00
string raw = readCString(typeinfo + 0xC); // skips the .?AV
raw.resize(raw.length() - 2);// trim @@ from end
2010-02-18 18:48:03 -07:00
return raw;
2010-03-04 16:05:01 -07:00
}
string SHMProcess::getPath()
{
HMODULE hmod;
DWORD junk;
char String[255];
HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, d->process_ID ); //get the handle from the process ID
EnumProcessModules(hProcess, &hmod, 1 * sizeof(HMODULE), &junk); //get the module from the handle
GetModuleFileNameEx(hProcess,hmod,String,sizeof(String)); //get the filename from the module
string out(String);
return(out.substr(0,out.find_last_of("\\")));
}
char * SHMProcess::getSHMStart (void)
2010-03-04 16:05:01 -07:00
{
2011-03-27 18:09:13 -06:00
if(!d->locked) throw Error::MemoryAccessDenied(0xdeadbeef);
return d->shm_addr;
}