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.
|
|
|
|
*/
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2010-05-26 00:42:09 -06:00
|
|
|
#include "dfhack/DFProcess.h"
|
|
|
|
#include "dfhack/DFMemInfo.h"
|
|
|
|
#include "dfhack/DFError.h"
|
2010-04-02 19:52:46 -06:00
|
|
|
#include "shms.h"
|
|
|
|
#include "mod-core.h"
|
2010-01-07 22:56:36 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
// a full memory barrier! better be safe than sorry.
|
2010-03-09 07:15:15 -07:00
|
|
|
class SHMProcess::Private
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
memdescriptor = NULL;
|
|
|
|
process_ID = 0;
|
|
|
|
shm_addr = 0;
|
2010-01-07 22:56:36 -07:00
|
|
|
attached = false;
|
2010-03-13 09:44:36 -07:00
|
|
|
locked = false;
|
2010-01-07 22:56:36 -07:00
|
|
|
identified = false;
|
2010-03-03 20:40:06 -07:00
|
|
|
useYield = 0;
|
2010-01-10 23:27:59 -07:00
|
|
|
DFSVMutex = 0;
|
|
|
|
DFCLMutex = 0;
|
2010-03-13 09:44:36 -07:00
|
|
|
DFCLSuspendMutex = 0;
|
|
|
|
attachmentIdx = -1;
|
2010-01-07 22:56:36 -07:00
|
|
|
};
|
|
|
|
~Private(){};
|
2010-03-11 16:13:50 -07:00
|
|
|
memory_info * memdescriptor;
|
2010-04-18 16:56:22 -06:00
|
|
|
SHMProcess * self;
|
2010-03-11 16:13:50 -07:00
|
|
|
uint32_t process_ID;
|
|
|
|
char *shm_addr;
|
2010-01-10 23:27:59 -07:00
|
|
|
HANDLE DFSVMutex;
|
|
|
|
HANDLE DFCLMutex;
|
2010-03-13 09:44:36 -07:00
|
|
|
HANDLE DFCLSuspendMutex;
|
|
|
|
int attachmentIdx;
|
2010-01-07 22:56:36 -07:00
|
|
|
|
|
|
|
bool attached;
|
2010-03-13 09:44:36 -07:00
|
|
|
bool locked;
|
2010-01-07 22:56:36 -07:00
|
|
|
bool identified;
|
2010-03-03 20:40:06 -07:00
|
|
|
bool useYield;
|
2010-01-07 22:56:36 -07:00
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
bool validate(std::vector< memory_info* >& known_versions);
|
|
|
|
|
2010-03-07 21:15:11 -07:00
|
|
|
bool Aux_Core_Attach(bool & versionOK, uint32_t & PID);
|
2010-03-13 09:44:36 -07:00
|
|
|
bool SetAndWait (uint32_t state);
|
|
|
|
bool GetLocks();
|
|
|
|
bool AreLocksOk();
|
|
|
|
void FreeLocks();
|
2010-01-07 22:56:36 -07:00
|
|
|
};
|
|
|
|
|
2010-03-03 20:40:06 -07:00
|
|
|
// some helpful macros to keep the code bloat in check
|
2010-03-13 09:44:36 -07:00
|
|
|
#define SHMCMD ( (uint32_t *) shm_addr)[attachmentIdx]
|
|
|
|
#define D_SHMCMD ( (uint32_t *) (d->shm_addr))[d->attachmentIdx]
|
2010-03-03 20:40:06 -07:00
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
#define SHMHDR ((shm_core_hdr *)shm_addr)
|
|
|
|
#define D_SHMHDR ((shm_core_hdr *)(d->shm_addr))
|
2010-03-07 21:15:11 -07:00
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
#define SHMDATA(type) ((type *)(shm_addr + SHM_HEADER))
|
|
|
|
#define D_SHMDATA(type) ((type *)(d->shm_addr + SHM_HEADER))
|
2010-01-10 23:27:59 -07:00
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
bool SHMProcess::SetAndWait (uint32_t state)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
return d->SetAndWait(state);
|
2010-03-04 16:05:01 -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
|
|
|
{
|
2010-03-03 20:40:06 -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++;
|
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
if(SHMCMD == CORE_ERROR)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-03 20:40:06 -07:00
|
|
|
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-03 20:40:06 -07:00
|
|
|
{
|
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;
|
2010-03-07 21:15:11 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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-03 20:40:06 -07:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
case WAIT_FAILED:
|
|
|
|
{
|
|
|
|
// TODO: now how do I respond to this?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2010-03-09 05:41:55 -07:00
|
|
|
char svmutexname [256];
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-09 05:41:55 -07:00
|
|
|
char clmutexname [256];
|
|
|
|
sprintf(clmutexname,"DFCLMutex-%d",PID);
|
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// get server and client mutex
|
2010-03-09 05:41:55 -07:00
|
|
|
d->DFSVMutex = OpenMutex(SYNCHRONIZE,false, svmutexname);
|
2010-02-15 16:04:15 -07:00
|
|
|
if(d->DFSVMutex == 0)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-02-15 16:04:15 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-03-09 05:41:55 -07:00
|
|
|
d->DFCLMutex = OpenMutex(SYNCHRONIZE,false, clmutexname);
|
2010-02-15 16:04:15 -07:00
|
|
|
if(d->DFCLMutex == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
SHMProcess::SHMProcess(uint32_t PID, vector <memory_info *> & known_versions)
|
|
|
|
: d(new Private())
|
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
d->process_ID = PID;
|
2010-04-18 16:56:22 -06:00
|
|
|
d->self = this;
|
2010-03-07 21:15:11 -07:00
|
|
|
// attach the SHM
|
2010-02-15 16:04:15 -07:00
|
|
|
if(!attach())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
// Test bridge version, get PID, sync Yield
|
2010-02-15 16:04:15 -07:00
|
|
|
bool bridgeOK;
|
2010-03-11 16:13:50 -07:00
|
|
|
if(!d->Aux_Core_Attach(bridgeOK,d->process_ID))
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
detach();
|
|
|
|
throw Error::SHMAttachFailure();
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
else if(!bridgeOK)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
detach();
|
|
|
|
throw Error::SHMVersionMismatch();
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
d->validate(known_versions);
|
2010-01-10 23:27:59 -07:00
|
|
|
// at this point, DF is attached and suspended, make it run
|
|
|
|
detach();
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::isSuspended()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
return d->locked;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::isAttached()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
return d->attached;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::isIdentified()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
return d->identified;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
bool SHMProcess::Private::validate(vector <memory_info *> & known_versions)
|
|
|
|
{
|
|
|
|
// try to identify the DF version
|
|
|
|
IMAGE_NT_HEADERS32 pe_header;
|
|
|
|
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
|
|
|
|
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, process_ID );
|
|
|
|
if (NULL == hProcess)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// read from this process
|
2010-04-18 16:56:22 -06:00
|
|
|
uint32_t pe_offset = self->readDWord(base+0x3C);
|
|
|
|
self->read(base + pe_offset , sizeof(pe_header), (uint8_t *)&pe_header);
|
|
|
|
self->read(base + pe_offset+ sizeof(pe_header), sizeof(sections) , (uint8_t *)§ions );
|
2010-03-13 09:44:36 -07:00
|
|
|
|
|
|
|
// iterate over the list of memory locations
|
|
|
|
vector<memory_info *>::iterator it;
|
|
|
|
for ( it=known_versions.begin() ; it < known_versions.end(); it++ )
|
|
|
|
{
|
|
|
|
uint32_t pe_timestamp;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pe_timestamp = (*it)->getHexValue("pe_timestamp");
|
|
|
|
}
|
2010-03-26 06:38:49 -06:00
|
|
|
catch(Error::MissingMemoryDefinition&)
|
2010-03-13 09:44:36 -07:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (pe_timestamp == pe_header.FileHeader.TimeDateStamp)
|
|
|
|
{
|
|
|
|
memory_info *m = new memory_info(**it);
|
|
|
|
m->RebaseAll(base);
|
|
|
|
memdescriptor = m;
|
2010-04-18 16:56:22 -06:00
|
|
|
m->setParentProcess(self);
|
2010-03-13 09:44:36 -07:00
|
|
|
identified = true;
|
|
|
|
cerr << "identified " << m->getVersion() << endl;
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-09 07:15:15 -07:00
|
|
|
SHMProcess::~SHMProcess()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
if(d->attached)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
}
|
|
|
|
// destroy data model. this is assigned by processmanager
|
2010-03-11 16:13:50 -07:00
|
|
|
if(d->memdescriptor)
|
2010-01-10 23:27:59 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
delete d->memdescriptor;
|
2010-01-10 23:27:59 -07:00
|
|
|
}
|
2010-01-07 22:56:36 -07:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
memory_info * SHMProcess::getDescriptor()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
return d->memdescriptor;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
int SHMProcess::getPID()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
return d->process_ID;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::getThreadIDs(vector<uint32_t> & threads )
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
HANDLE AllThreads = INVALID_HANDLE_VALUE;
|
|
|
|
THREADENTRY32 te32;
|
|
|
|
|
|
|
|
AllThreads = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
|
|
|
|
if( AllThreads == INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
te32.dwSize = sizeof(THREADENTRY32 );
|
|
|
|
|
|
|
|
if( !Thread32First( AllThreads, &te32 ) )
|
|
|
|
{
|
|
|
|
CloseHandle( AllThreads );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( te32.th32OwnerProcessID == d->process_ID )
|
|
|
|
{
|
|
|
|
threads.push_back(te32.th32ThreadID);
|
|
|
|
}
|
|
|
|
} while( Thread32Next(AllThreads, &te32 ) );
|
|
|
|
|
|
|
|
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
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::getMemRanges( vector<t_memrange> & ranges )
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
// code here is taken from hexsearch by Silas Dunmore.
|
|
|
|
// As this IMHO isn't a 'sunstantial portion' of anything, I'm not including the MIT license here
|
2010-01-07 22:56:36 -07:00
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
// I'm faking this, because there's no way I'm using VirtualQuery
|
2010-01-07 22:56:36 -07:00
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
t_memrange temp;
|
|
|
|
uint32_t base = d->memdescriptor->getBase();
|
|
|
|
temp.start = base + 0x1000; // more fakery.
|
|
|
|
temp.end = base + readDWord(base+readDWord(base+0x3C)+0x50)-1; // yay for magic.
|
|
|
|
temp.read = 1;
|
|
|
|
temp.write = 1;
|
|
|
|
temp.execute = 0; // fake
|
|
|
|
strcpy(temp.name,"pants");
|
|
|
|
ranges.push_back(temp);
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::suspend()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->locked)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
//cerr << "suspend" << endl;// FIXME: throw
|
|
|
|
// FIXME: this should be controlled on the server side
|
|
|
|
// FIXME: IF server got CORE_RUN in this frame, interpret CORE_SUSPEND as CORE_STEP
|
|
|
|
// did we just resume a moment ago?
|
|
|
|
if(D_SHMCMD == CORE_RUN)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
//fprintf(stderr,"%d invokes step\n",d->attachmentIdx);
|
|
|
|
// wait for the next window
|
2010-03-18 13:05:32 -06:00
|
|
|
/*
|
2010-03-13 09:44:36 -07:00
|
|
|
if(!d->SetAndWait(CORE_STEP))
|
|
|
|
{
|
|
|
|
throw Error::SHMLockingError("if(!d->SetAndWait(CORE_STEP))");
|
|
|
|
}
|
2010-03-18 13:05:32 -06:00
|
|
|
*/
|
|
|
|
D_SHMCMD = CORE_STEP;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//fprintf(stderr,"%d invokes suspend\n",d->attachmentIdx);
|
|
|
|
// lock now
|
2010-03-18 13:05:32 -06:00
|
|
|
/*
|
2010-03-13 09:44:36 -07:00
|
|
|
if(!d->SetAndWait(CORE_SUSPEND))
|
|
|
|
{
|
|
|
|
throw Error::SHMLockingError("if(!d->SetAndWait(CORE_SUSPEND))");
|
|
|
|
}
|
2010-03-18 13:05:32 -06:00
|
|
|
*/
|
|
|
|
D_SHMCMD = CORE_SUSPEND;
|
2010-03-13 09:44:36 -07:00
|
|
|
}
|
|
|
|
//fprintf(stderr,"waiting for lock\n");
|
|
|
|
// we wait for the server to give up our suspend lock (held by default)
|
|
|
|
if( WaitForSingleObject(d->DFCLSuspendMutex,INFINITE) == 0 )
|
|
|
|
{
|
|
|
|
d->locked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
// FIXME: needs a good think-through
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::asyncSuspend()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->locked)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
//cerr << "async suspend" << endl;// FIXME: throw
|
|
|
|
uint32_t cmd = D_SHMCMD;
|
|
|
|
if(cmd == CORE_SUSPENDED)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
// we have to hold the lock to be really suspended
|
|
|
|
if( WaitForSingleObject(d->DFCLSuspendMutex,INFINITE) == 0 )
|
|
|
|
{
|
|
|
|
d->locked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
// did we just resume a moment ago?
|
|
|
|
if(cmd == CORE_STEP)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(cmd == CORE_RUN)
|
|
|
|
{
|
|
|
|
D_SHMCMD = CORE_STEP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
D_SHMCMD = CORE_SUSPEND;
|
|
|
|
}
|
2010-01-07 22:56:36 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::forceresume()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
return resume();
|
|
|
|
}
|
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
// FIXME: wait for the server to advance a step!
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::resume()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
return false;
|
2010-03-13 09:44:36 -07:00
|
|
|
if(!d->locked)
|
2010-01-07 22:56:36 -07:00
|
|
|
return true;
|
2010-03-13 09:44:36 -07:00
|
|
|
//cerr << "resume" << endl;// FIXME: throw
|
|
|
|
// unlock the suspend lock
|
|
|
|
if( ReleaseMutex(d->DFCLSuspendMutex) != 0)
|
|
|
|
{
|
|
|
|
d->locked = false;
|
|
|
|
if(d->SetAndWait(CORE_RUN)) // we have to make sure the server responds!
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw Error::SHMLockingError("if(d->SetAndWait(CORE_RUN))");
|
2010-02-10 22:10:23 -07:00
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
throw Error::SHMLockingError("if( ReleaseMutex(d->DFCLSuspendMutex) != 0)");
|
|
|
|
return false;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 07:15:15 -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
|
|
|
/*
|
2010-01-10 23:27:59 -07:00
|
|
|
// check if DF is there
|
|
|
|
if(!d->isValidSV())
|
|
|
|
{
|
|
|
|
return false; // NOT
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
*/
|
|
|
|
/*
|
2010-01-10 23:27:59 -07:00
|
|
|
// try locking client mutex
|
|
|
|
uint32_t result = WaitForSingleObject(d->DFCLMutex,0);
|
|
|
|
if( result != WAIT_OBJECT_0 && result != WAIT_ABANDONED)
|
|
|
|
{
|
|
|
|
return false; // we couldn't lock it
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Locate the segment.
|
|
|
|
*/
|
2010-03-09 05:41:55 -07:00
|
|
|
char shmname [256];
|
2010-03-11 16:13:50 -07:00
|
|
|
sprintf(shmname,"DFShm-%d",d->process_ID);
|
2010-03-09 05:41:55 -07:00
|
|
|
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();
|
|
|
|
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-03-13 09:44:36 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::detach()
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
//cerr << "detach" << endl;// FIXME: throw
|
|
|
|
if(d->locked)
|
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
|
|
|
//cerr << "detach after resume" << endl;// FIXME: throw
|
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
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::read (uint32_t src_address, uint32_t size, uint8_t *target_buffer)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-01-07 22:56:36 -07:00
|
|
|
// normal read under 1MB
|
|
|
|
if(size <= SHM_BODY)
|
|
|
|
{
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = src_address;
|
|
|
|
D_SHMHDR->length = size;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ);
|
|
|
|
memcpy (target_buffer, D_SHMDATA(void),size);
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
// a big read, we pull data over the shm in iterations
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// first read equals the size of the SHM window
|
|
|
|
uint32_t to_read = SHM_BODY;
|
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
// read to_read bytes from src_cursor
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = src_address;
|
|
|
|
D_SHMHDR->length = to_read;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ);
|
|
|
|
memcpy (target_buffer, D_SHMDATA(void) ,size);
|
2010-01-07 22:56:36 -07:00
|
|
|
// decrease size by bytes read
|
|
|
|
size -= to_read;
|
|
|
|
// move the cursors
|
|
|
|
src_address += to_read;
|
|
|
|
target_buffer += to_read;
|
|
|
|
// check how much to write in the next iteration
|
|
|
|
to_read = min(size, (uint32_t) SHM_BODY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
uint8_t SHMProcess::readByte (const uint32_t offset)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_BYTE);
|
2010-03-04 16:05:01 -07:00
|
|
|
return D_SHMHDR->value;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::readByte (const uint32_t offset, uint8_t &val )
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_BYTE);
|
2010-03-04 16:05:01 -07:00
|
|
|
val = D_SHMHDR->value;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
uint16_t SHMProcess::readWord (const uint32_t offset)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_WORD);
|
2010-03-04 16:05:01 -07:00
|
|
|
return D_SHMHDR->value;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::readWord (const uint32_t offset, uint16_t &val)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_WORD);
|
2010-03-04 16:05:01 -07:00
|
|
|
val = D_SHMHDR->value;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
uint32_t SHMProcess::readDWord (const uint32_t offset)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_DWORD);
|
2010-03-04 16:05:01 -07:00
|
|
|
return D_SHMHDR->value;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::readDWord (const uint32_t offset, uint32_t &val)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_DWORD);
|
2010-03-04 16:05:01 -07:00
|
|
|
val = D_SHMHDR->value;
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-04-20 10:13:00 -06:00
|
|
|
float SHMProcess::readFloat (const uint32_t offset)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
full_barrier
|
|
|
|
d->SetAndWait(CORE_READ_DWORD);
|
2010-04-26 16:12:22 -06:00
|
|
|
return (float) D_SHMHDR->value;
|
2010-04-20 10:13:00 -06:00
|
|
|
}
|
|
|
|
void SHMProcess::readFloat (const uint32_t offset, float &val)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
full_barrier
|
|
|
|
d->SetAndWait(CORE_READ_DWORD);
|
2010-04-26 16:12:22 -06:00
|
|
|
val = (float) D_SHMHDR->value;
|
|
|
|
}
|
|
|
|
uint64_t SHMProcess::readQuad (const uint32_t offset)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
full_barrier
|
|
|
|
d->SetAndWait(CORE_READ_QUAD);
|
|
|
|
return D_SHMHDR->Qvalue;
|
|
|
|
}
|
|
|
|
void SHMProcess::readQuad (const uint32_t offset, uint64_t &val)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
full_barrier
|
|
|
|
d->SetAndWait(CORE_READ_QUAD);
|
|
|
|
val = D_SHMHDR->Qvalue;
|
2010-04-20 10:13:00 -06:00
|
|
|
}
|
|
|
|
|
2010-01-07 22:56:36 -07:00
|
|
|
/*
|
|
|
|
* WRITING
|
|
|
|
*/
|
|
|
|
|
2010-04-26 16:12:22 -06:00
|
|
|
void SHMProcess::writeQuad (uint32_t offset, uint64_t data)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->Qvalue = data;
|
|
|
|
full_barrier
|
|
|
|
d->SetAndWait(CORE_WRITE_QUAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::writeDWord (uint32_t offset, uint32_t data)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->value = data;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_WRITE_DWORD);
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// using these is expensive.
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::writeWord (uint32_t offset, uint16_t data)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->value = data;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_WRITE_WORD);
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::writeByte (uint32_t offset, uint8_t data)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->value = data;
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_WRITE_BYTE);
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::write (uint32_t dst_address, uint32_t size, uint8_t *source_buffer)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-01-07 22:56:36 -07:00
|
|
|
// normal write under 1MB
|
|
|
|
if(size <= SHM_BODY)
|
|
|
|
{
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = dst_address;
|
|
|
|
D_SHMHDR->length = size;
|
2010-03-13 09:44:36 -07:00
|
|
|
memcpy(D_SHMDATA(void),source_buffer, size);
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_WRITE);
|
2010-01-07 22:56:36 -07:00
|
|
|
}
|
|
|
|
// a big write, we push this over the shm in iterations
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// first write equals the size of the SHM window
|
|
|
|
uint32_t to_write = SHM_BODY;
|
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
// write to_write bytes to dst_cursor
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = dst_address;
|
|
|
|
D_SHMHDR->length = to_write;
|
2010-03-13 09:44:36 -07:00
|
|
|
memcpy(D_SHMDATA(void),source_buffer, to_write);
|
2010-01-10 23:27:59 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_WRITE);
|
2010-01-07 22:56:36 -07:00
|
|
|
// decrease size by bytes written
|
|
|
|
size -= to_write;
|
|
|
|
// move the cursors
|
|
|
|
source_buffer += to_write;
|
|
|
|
dst_address += to_write;
|
|
|
|
// check how much to write in the next iteration
|
|
|
|
to_write = min(size, (uint32_t) SHM_BODY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: butt-fugly
|
2010-03-09 07:15:15 -07:00
|
|
|
const std::string SHMProcess::readCString (uint32_t offset)
|
2010-01-07 22:56:36 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-01-07 22:56:36 -07:00
|
|
|
std::string temp;
|
|
|
|
char temp_c[256];
|
|
|
|
int counter = 0;
|
|
|
|
char r;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
r = readByte(offset+counter);
|
|
|
|
temp_c[counter] = r;
|
|
|
|
counter++;
|
|
|
|
} while (r && counter < 255);
|
|
|
|
temp_c[counter] = 0;
|
|
|
|
temp = temp_c;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
const std::string SHMProcess::readSTLString(uint32_t offset)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-02-15 16:04:15 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_STL_STRING);
|
|
|
|
return(string( D_SHMDATA(char) ));
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
size_t SHMProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-02-15 16:04:15 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_READ_STL_STRING);
|
2010-03-04 16:05:01 -07:00
|
|
|
size_t length = D_SHMHDR->value;
|
2010-03-13 09:44:36 -07:00
|
|
|
size_t fit = min(bufcapacity - 1, length);
|
|
|
|
strncpy(buffer,D_SHMDATA(char),fit);
|
|
|
|
buffer[fit] = 0;
|
|
|
|
return fit;
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::writeSTLString(const uint32_t address, const std::string writeString)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
|
|
|
D_SHMHDR->address = address;
|
|
|
|
strncpy(D_SHMDATA(char),writeString.c_str(),writeString.length()+1); // length + 1 for the null terminator
|
2010-02-15 16:04:15 -07:00
|
|
|
full_barrier
|
2010-03-13 09:44:36 -07:00
|
|
|
d->SetAndWait(CORE_WRITE_STL_STRING);
|
2010-02-18 18:48:03 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
string SHMProcess::readClassName (uint32_t vptr)
|
2010-02-18 18:48:03 -07:00
|
|
|
{
|
|
|
|
int rtti = readDWord(vptr - 0x4);
|
|
|
|
int typeinfo = readDWord(rtti + 0xC);
|
|
|
|
string raw = readCString(typeinfo + 0xC); // skips the .?AV
|
2010-04-09 06:44:00 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
// get module index by name and version. bool 0 = error
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
|
|
|
modulelookup * payload = D_SHMDATA(modulelookup);
|
2010-03-04 16:05:01 -07:00
|
|
|
payload->version = version;
|
2010-03-13 09:44:36 -07:00
|
|
|
|
|
|
|
strncpy(payload->name,name,255);
|
|
|
|
payload->name[255] = 0;
|
|
|
|
|
|
|
|
if(!SetAndWait(CORE_ACQUIRE_MODULE))
|
|
|
|
{
|
|
|
|
return false; // FIXME: throw a fatal exception instead
|
|
|
|
}
|
|
|
|
if(D_SHMHDR->error)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//fprintf(stderr,"%s v%d : %d\n", name, version, D_SHMHDR->value);
|
2010-03-04 16:05:01 -07:00
|
|
|
OUTPUT = D_SHMHDR->value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
char * SHMProcess::getSHMStart (void)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
return /*d->shm_addr_with_cl_idx*/ d->shm_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SHMProcess::Private::Aux_Core_Attach(bool & versionOK, uint32_t & PID)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
|
|
|
SHMDATA(coreattach)->cl_affinity = OS_getAffinity();
|
|
|
|
if(!SetAndWait(CORE_ATTACH)) return false;
|
|
|
|
/*
|
|
|
|
cerr <<"CORE_VERSION" << CORE_VERSION << endl;
|
|
|
|
cerr <<"server CORE_VERSION" << SHMDATA(coreattach)->sv_version << endl;
|
|
|
|
*/
|
|
|
|
versionOK =( SHMDATA(coreattach)->sv_version == CORE_VERSION );
|
|
|
|
PID = SHMDATA(coreattach)->sv_PID;
|
|
|
|
useYield = SHMDATA(coreattach)->sv_useYield;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if(useYield) cerr << "Using Yield!" << endl;
|
|
|
|
#endif
|
|
|
|
return true;
|
2010-04-20 10:13:00 -06:00
|
|
|
}
|