Merge branch 'master' of https://github.com/tomprince/dfhack into tomprince-master
Conflicts: library/DFProcess-linux.cppdevelop
commit
a62196dea3
@ -0,0 +1,485 @@
|
|||||||
|
/*
|
||||||
|
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 "SHMProcess.h"
|
||||||
|
#include "ProcessFactory.h"
|
||||||
|
#include "dfhack/VersionInfo.h"
|
||||||
|
#include "dfhack/DFError.h"
|
||||||
|
#include "shms.h"
|
||||||
|
#include "mod-core.h"
|
||||||
|
|
||||||
|
using namespace DFHack;
|
||||||
|
|
||||||
|
Process* DFHack::createSHMProcess(uint32_t pid, vector <VersionInfo *> & known_versions)
|
||||||
|
{
|
||||||
|
return new SHMProcess(pid, known_versions);
|
||||||
|
}
|
||||||
|
|
||||||
|
SHMProcess::SHMProcess(uint32_t PID, vector <VersionInfo *> & known_versions)
|
||||||
|
: d(new Private(this))
|
||||||
|
{
|
||||||
|
d->process_ID = PID;
|
||||||
|
// attach the SHM
|
||||||
|
if(!attach())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Test bridge version, get PID, sync Yield
|
||||||
|
bool bridgeOK;
|
||||||
|
if(!d->Aux_Core_Attach(bridgeOK,d->process_ID))
|
||||||
|
{
|
||||||
|
detach();
|
||||||
|
throw Error::SHMAttachFailure();
|
||||||
|
}
|
||||||
|
else if(!bridgeOK)
|
||||||
|
{
|
||||||
|
detach();
|
||||||
|
throw Error::SHMVersionMismatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to identify the DF version (md5 the binary, compare with known versions)
|
||||||
|
d->validate(known_versions);
|
||||||
|
// at this point, DF is attached and suspended, make it run
|
||||||
|
detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
SHMProcess::~SHMProcess()
|
||||||
|
{
|
||||||
|
if(d->attached)
|
||||||
|
{
|
||||||
|
detach();
|
||||||
|
}
|
||||||
|
// destroy data model. this is assigned by processmanager
|
||||||
|
if(d->memdescriptor)
|
||||||
|
delete d->memdescriptor;
|
||||||
|
delete d;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionInfo * SHMProcess::getDescriptor()
|
||||||
|
{
|
||||||
|
return d->memdescriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SHMProcess::getPID()
|
||||||
|
{
|
||||||
|
return d->process_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool SHMProcess::isSuspended()
|
||||||
|
{
|
||||||
|
return d->locked;
|
||||||
|
}
|
||||||
|
bool SHMProcess::isAttached()
|
||||||
|
{
|
||||||
|
return d->attached;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SHMProcess::isIdentified()
|
||||||
|
{
|
||||||
|
return d->identified;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SHMProcess::suspend()
|
||||||
|
{
|
||||||
|
if(!d->attached)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(d->locked)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//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)
|
||||||
|
{
|
||||||
|
//fprintf(stderr,"%d invokes step\n",attachmentIdx);
|
||||||
|
// wait for the next window
|
||||||
|
/*
|
||||||
|
if(!d->SetAndWait(CORE_STEP))
|
||||||
|
{
|
||||||
|
throw Error::SHMLockingError("if(!d->SetAndWait(CORE_STEP))");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
D_SHMCMD = CORE_STEP;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//fprintf(stderr,"%d invokes suspend\n",attachmentIdx);
|
||||||
|
// lock now
|
||||||
|
/*
|
||||||
|
if(!d->SetAndWait(CORE_SUSPEND))
|
||||||
|
{
|
||||||
|
throw Error::SHMLockingError("if(!d->SetAndWait(CORE_SUSPEND))");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
D_SHMCMD = CORE_SUSPEND;
|
||||||
|
}
|
||||||
|
//fprintf(stderr,"waiting for lock\n");
|
||||||
|
// we wait for the server to give up our suspend lock (held by default)
|
||||||
|
if(acquireSuspendLock())
|
||||||
|
{
|
||||||
|
d->locked = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: needs a good think-through
|
||||||
|
bool SHMProcess::asyncSuspend()
|
||||||
|
{
|
||||||
|
if(!d->attached)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(d->locked)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//cerr << "async suspend" << endl;// FIXME: throw
|
||||||
|
uint32_t cmd = D_SHMCMD;
|
||||||
|
if(cmd == CORE_SUSPENDED)
|
||||||
|
{
|
||||||
|
// we have to hold the lock to be really suspended
|
||||||
|
if(acquireSuspendLock())
|
||||||
|
{
|
||||||
|
d->locked = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SHMProcess::forceresume()
|
||||||
|
{
|
||||||
|
return resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: wait for the server to advance a step!
|
||||||
|
bool SHMProcess::resume()
|
||||||
|
{
|
||||||
|
if(!d->attached)
|
||||||
|
return false;
|
||||||
|
if(!d->locked)
|
||||||
|
return true;
|
||||||
|
//cerr << "resume" << endl;// FIXME: throw
|
||||||
|
// unlock the suspend lock
|
||||||
|
if(releaseSuspendLock())
|
||||||
|
{
|
||||||
|
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))");
|
||||||
|
}
|
||||||
|
throw Error::SHMLockingError("if(releaseSuspendLock())");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get module index by name and version. bool 0 = error
|
||||||
|
bool SHMProcess::getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
modulelookup * payload = D_SHMDATA(modulelookup);
|
||||||
|
payload->version = version;
|
||||||
|
|
||||||
|
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);
|
||||||
|
OUTPUT = D_SHMHDR->value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SHMProcess::Private::Aux_Core_Attach(bool & versionOK, pid_t & PID)
|
||||||
|
{
|
||||||
|
if(!locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::read (uint32_t src_address, uint32_t size, uint8_t *target_buffer)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
// normal read under 1MB
|
||||||
|
if(size <= SHM_BODY)
|
||||||
|
{
|
||||||
|
D_SHMHDR->address = src_address;
|
||||||
|
D_SHMHDR->length = size;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_READ);
|
||||||
|
memcpy (target_buffer, D_SHMDATA(void),size);
|
||||||
|
}
|
||||||
|
// 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
|
||||||
|
D_SHMHDR->address = src_address;
|
||||||
|
D_SHMHDR->length = to_read;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_READ);
|
||||||
|
memcpy (target_buffer, D_SHMDATA(void) ,to_read);
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::readByte (const uint32_t offset, uint8_t &val )
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_READ_BYTE);
|
||||||
|
val = D_SHMHDR->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::readWord (const uint32_t offset, uint16_t &val)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_READ_WORD);
|
||||||
|
val = D_SHMHDR->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::readDWord (const uint32_t offset, uint32_t &val)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_READ_DWORD);
|
||||||
|
val = D_SHMHDR->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
val = reinterpret_cast<float&> (D_SHMHDR->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* WRITING
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::writeDWord (uint32_t offset, uint32_t data)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
D_SHMHDR->value = data;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_WRITE_DWORD);
|
||||||
|
}
|
||||||
|
|
||||||
|
// using these is expensive.
|
||||||
|
void SHMProcess::writeWord (uint32_t offset, uint16_t data)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
D_SHMHDR->value = data;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_WRITE_WORD);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::writeByte (uint32_t offset, uint8_t data)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
D_SHMHDR->value = data;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_WRITE_BYTE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::write (uint32_t dst_address, uint32_t size, uint8_t *source_buffer)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
// normal write under 1MB
|
||||||
|
if(size <= SHM_BODY)
|
||||||
|
{
|
||||||
|
D_SHMHDR->address = dst_address;
|
||||||
|
D_SHMHDR->length = size;
|
||||||
|
memcpy(D_SHMDATA(void),source_buffer, size);
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_WRITE);
|
||||||
|
}
|
||||||
|
// 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
|
||||||
|
D_SHMHDR->address = dst_address;
|
||||||
|
D_SHMHDR->length = to_write;
|
||||||
|
memcpy(D_SHMDATA(void),source_buffer, to_write);
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_WRITE);
|
||||||
|
// 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
|
||||||
|
const std::string SHMProcess::readCString (uint32_t offset)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
std::string temp;
|
||||||
|
char temp_c[256];
|
||||||
|
int counter = 0;
|
||||||
|
char r;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
r = Process::readByte(offset+counter);
|
||||||
|
temp_c[counter] = r;
|
||||||
|
counter++;
|
||||||
|
} while (r && counter < 255);
|
||||||
|
temp_c[counter] = 0;
|
||||||
|
temp = temp_c;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string SHMProcess::readSTLString(uint32_t offset)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_READ_STL_STRING);
|
||||||
|
return(string( D_SHMDATA(char) ));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t SHMProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = offset;
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_READ_STL_STRING);
|
||||||
|
size_t length = D_SHMHDR->value;
|
||||||
|
size_t fit = min(bufcapacity - 1, length);
|
||||||
|
strncpy(buffer,D_SHMDATA(char),fit);
|
||||||
|
buffer[fit] = 0;
|
||||||
|
return fit;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHMProcess::writeSTLString(const uint32_t address, const std::string writeString)
|
||||||
|
{
|
||||||
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
||||||
|
|
||||||
|
D_SHMHDR->address = address;
|
||||||
|
strncpy(D_SHMDATA(char),writeString.c_str(),writeString.length()+1); // length + 1 for the null terminator
|
||||||
|
full_barrier
|
||||||
|
d->SetAndWait(CORE_WRITE_STL_STRING);
|
||||||
|
}
|
@ -0,0 +1,432 @@
|
|||||||
|
/*
|
||||||
|
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 "LinuxProcess.h"
|
||||||
|
#include "dfhack/VersionInfo.h"
|
||||||
|
#include "dfhack/DFError.h"
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/ptrace.h>
|
||||||
|
using namespace DFHack;
|
||||||
|
|
||||||
|
LinuxProcessBase::LinuxProcessBase(uint32_t pid)
|
||||||
|
: my_pid(pid)
|
||||||
|
{
|
||||||
|
my_descriptor = NULL;
|
||||||
|
attached = false;
|
||||||
|
suspended = false;
|
||||||
|
memFileHandle = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LinuxProcessBase::isSuspended()
|
||||||
|
{
|
||||||
|
return suspended;
|
||||||
|
}
|
||||||
|
bool LinuxProcessBase::isAttached()
|
||||||
|
{
|
||||||
|
return attached;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LinuxProcessBase::isIdentified()
|
||||||
|
{
|
||||||
|
return identified;
|
||||||
|
}
|
||||||
|
|
||||||
|
LinuxProcessBase::~LinuxProcessBase()
|
||||||
|
{
|
||||||
|
if(attached)
|
||||||
|
{
|
||||||
|
detach();
|
||||||
|
}
|
||||||
|
// destroy our copy of the memory descriptor
|
||||||
|
if(my_descriptor)
|
||||||
|
delete my_descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionInfo * LinuxProcessBase::getDescriptor()
|
||||||
|
{
|
||||||
|
return my_descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LinuxProcessBase::getPID()
|
||||||
|
{
|
||||||
|
return my_pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
//FIXME: implement
|
||||||
|
bool LinuxProcessBase::getThreadIDs(vector<uint32_t> & threads )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//FIXME: cross-reference with ELF segment entries?
|
||||||
|
void LinuxProcessBase::getMemRanges( vector<t_memrange> & ranges )
|
||||||
|
{
|
||||||
|
char buffer[1024];
|
||||||
|
char permissions[5]; // r/-, w/-, x/-, p/s, 0
|
||||||
|
|
||||||
|
sprintf(buffer, "/proc/%lu/maps", my_pid);
|
||||||
|
FILE *mapFile = ::fopen(buffer, "r");
|
||||||
|
uint64_t offset, device1, device2, node;
|
||||||
|
|
||||||
|
while (fgets(buffer, 1024, mapFile))
|
||||||
|
{
|
||||||
|
t_memrange temp;
|
||||||
|
temp.name[0] = 0;
|
||||||
|
sscanf(buffer, "%llx-%llx %s %llx %2llu:%2llu %llu %s",
|
||||||
|
&temp.start,
|
||||||
|
&temp.end,
|
||||||
|
(char*)&permissions,
|
||||||
|
&offset, &device1, &device2, &node,
|
||||||
|
(char*)&temp.name);
|
||||||
|
temp.read = permissions[0] == 'r';
|
||||||
|
temp.write = permissions[1] == 'w';
|
||||||
|
temp.execute = permissions[2] == 'x';
|
||||||
|
temp.valid = true;
|
||||||
|
ranges.push_back(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LinuxProcessBase::asyncSuspend()
|
||||||
|
{
|
||||||
|
return suspend();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LinuxProcessBase::suspend()
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
if(!attached)
|
||||||
|
return false;
|
||||||
|
if(suspended)
|
||||||
|
return true;
|
||||||
|
if (kill(my_pid, SIGSTOP) == -1)
|
||||||
|
{
|
||||||
|
// no, we got an error
|
||||||
|
perror("kill SIGSTOP error");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
// we wait on the pid
|
||||||
|
pid_t w = waitpid(my_pid, &status, 0);
|
||||||
|
if (w == -1)
|
||||||
|
{
|
||||||
|
// child died
|
||||||
|
perror("DF exited during suspend call");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// stopped -> let's continue
|
||||||
|
if (WIFSTOPPED(status))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
suspended = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LinuxProcessBase::forceresume()
|
||||||
|
{
|
||||||
|
return resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LinuxProcessBase::resume()
|
||||||
|
{
|
||||||
|
if(!attached)
|
||||||
|
return false;
|
||||||
|
if(!suspended)
|
||||||
|
return true;
|
||||||
|
if (ptrace(PTRACE_CONT, my_pid, NULL, NULL) == -1)
|
||||||
|
{
|
||||||
|
// no, we got an error
|
||||||
|
perror("ptrace resume error");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
suspended = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LinuxProcessBase::attach()
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
if(attached)
|
||||||
|
{
|
||||||
|
if(!suspended)
|
||||||
|
return suspend();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// can we attach?
|
||||||
|
if (ptrace(PTRACE_ATTACH , my_pid, NULL, NULL) == -1)
|
||||||
|
{
|
||||||
|
// no, we got an error
|
||||||
|
perror("ptrace attach error");
|
||||||
|
cerr << "attach failed on pid " << my_pid << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
// we wait on the pid
|
||||||
|
pid_t w = waitpid(my_pid, &status, 0);
|
||||||
|
if (w == -1)
|
||||||
|
{
|
||||||
|
// child died
|
||||||
|
perror("wait inside attach()");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// stopped -> let's continue
|
||||||
|
if (WIFSTOPPED(status))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
suspended = true;
|
||||||
|
|
||||||
|
int proc_pid_mem = open(memFile.c_str(),O_RDONLY);
|
||||||
|
if(proc_pid_mem == -1)
|
||||||
|
{
|
||||||
|
ptrace(PTRACE_DETACH, my_pid, NULL, NULL);
|
||||||
|
cerr << memFile << endl;
|
||||||
|
cerr << "couldn't open /proc/" << my_pid << "/mem" << endl;
|
||||||
|
perror("open(memFile.c_str(),O_RDONLY)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
attached = true;
|
||||||
|
|
||||||
|
memFileHandle = proc_pid_mem;
|
||||||
|
return true; // we are attached
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LinuxProcessBase::detach()
|
||||||
|
{
|
||||||
|
if(!attached) return true;
|
||||||
|
if(!suspended) suspend();
|
||||||
|
int result = 0;
|
||||||
|
// close /proc/PID/mem
|
||||||
|
result = close(memFileHandle);
|
||||||
|
if(result == -1)
|
||||||
|
{
|
||||||
|
cerr << "couldn't close /proc/"<< my_pid <<"/mem" << endl;
|
||||||
|
perror("mem file close");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// detach
|
||||||
|
result = ptrace(PTRACE_DETACH, my_pid, NULL, NULL);
|
||||||
|
if(result == -1)
|
||||||
|
{
|
||||||
|
cerr << "couldn't detach from process pid" << my_pid << endl;
|
||||||
|
perror("ptrace detach");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
attached = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LinuxProcessBase::read (const uint32_t offset, const uint32_t size, uint8_t *target)
|
||||||
|
{
|
||||||
|
if(size == 0) return;
|
||||||
|
|
||||||
|
ssize_t result;
|
||||||
|
ssize_t total = 0;
|
||||||
|
ssize_t remaining = size;
|
||||||
|
while (total != size)
|
||||||
|
{
|
||||||
|
result = pread(memFileHandle, target + total ,remaining,offset + total);
|
||||||
|
if(result == -1)
|
||||||
|
{
|
||||||
|
cerr << "pread failed: can't read " << size << " bytes at addres " << offset << endl;
|
||||||
|
cerr << "errno: " << errno << endl;
|
||||||
|
errno = 0;
|
||||||
|
throw Error::MemoryAccessDenied();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
total += result;
|
||||||
|
remaining -= result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinuxProcessBase::readByte (const uint32_t offset, uint8_t &val )
|
||||||
|
{
|
||||||
|
read(offset, 1, &val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinuxProcessBase::readWord (const uint32_t offset, uint16_t &val)
|
||||||
|
{
|
||||||
|
read(offset, 2, (uint8_t *) &val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinuxProcessBase::readDWord (const uint32_t offset, uint32_t &val)
|
||||||
|
{
|
||||||
|
read(offset, 4, (uint8_t *) &val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinuxProcessBase::readFloat (const uint32_t offset, float &val)
|
||||||
|
{
|
||||||
|
read(offset, 4, (uint8_t *) &val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinuxProcessBase::readQuad (const uint32_t offset, uint64_t &val)
|
||||||
|
{
|
||||||
|
read(offset, 8, (uint8_t *) &val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* WRITING
|
||||||
|
*/
|
||||||
|
|
||||||
|
void LinuxProcessBase::writeQuad (uint32_t offset, const uint64_t data)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_64_BIT
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, data);
|
||||||
|
#else
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, (uint32_t) data);
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset+4, (uint32_t) (data >> 32));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinuxProcessBase::writeDWord (uint32_t offset, uint32_t data)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_64_BIT
|
||||||
|
uint64_t orig = Process::readQuad(offset);
|
||||||
|
orig &= 0xFFFFFFFF00000000;
|
||||||
|
orig |= data;
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, orig);
|
||||||
|
#else
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, data);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// using these is expensive.
|
||||||
|
void LinuxProcessBase::writeWord (uint32_t offset, uint16_t data)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_64_BIT
|
||||||
|
uint64_t orig = Process::readQuad(offset);
|
||||||
|
orig &= 0xFFFFFFFFFFFF0000;
|
||||||
|
orig |= data;
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, orig);
|
||||||
|
#else
|
||||||
|
uint32_t orig = readDWord(offset);
|
||||||
|
orig &= 0xFFFF0000;
|
||||||
|
orig |= data;
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, orig);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinuxProcessBase::writeByte (uint32_t offset, uint8_t data)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_64_BIT
|
||||||
|
uint64_t orig = Process::readQuad(offset);
|
||||||
|
orig &= 0xFFFFFFFFFFFFFF00;
|
||||||
|
orig |= data;
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, orig);
|
||||||
|
#else
|
||||||
|
uint32_t orig = readDWord(offset);
|
||||||
|
orig &= 0xFFFFFF00;
|
||||||
|
orig |= data;
|
||||||
|
ptrace(PTRACE_POKEDATA,my_pid, offset, orig);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// blah. I hate the kernel devs for crippling /proc/PID/mem. THIS IS RIDICULOUS
|
||||||
|
void LinuxProcessBase::write (uint32_t offset, uint32_t size, uint8_t *source)
|
||||||
|
{
|
||||||
|
uint32_t indexptr = 0;
|
||||||
|
while (size > 0)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_64_BIT
|
||||||
|
// quad!
|
||||||
|
if(size >= 8)
|
||||||
|
{
|
||||||
|
writeQuad(offset, *(uint64_t *) (source + indexptr));
|
||||||
|
offset +=8;
|
||||||
|
indexptr +=8;
|
||||||
|
size -=8;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
// default: we push 4 bytes
|
||||||
|
if(size >= 4)
|
||||||
|
{
|
||||||
|
writeDWord(offset, *(uint32_t *) (source + indexptr));
|
||||||
|
offset +=4;
|
||||||
|
indexptr +=4;
|
||||||
|
size -=4;
|
||||||
|
}
|
||||||
|
// last is either three or 2 bytes
|
||||||
|
else if(size >= 2)
|
||||||
|
{
|
||||||
|
writeWord(offset, *(uint16_t *) (source + indexptr));
|
||||||
|
offset +=2;
|
||||||
|
indexptr +=2;
|
||||||
|
size -=2;
|
||||||
|
}
|
||||||
|
// finishing move
|
||||||
|
else if(size == 1)
|
||||||
|
{
|
||||||
|
writeByte(offset, *(uint8_t *) (source + indexptr));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string LinuxProcessBase::readCString (uint32_t offset)
|
||||||
|
{
|
||||||
|
std::string temp;
|
||||||
|
char temp_c[256];
|
||||||
|
int counter = 0;
|
||||||
|
char r;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
r = Process::readByte(offset+counter);
|
||||||
|
temp_c[counter] = r;
|
||||||
|
counter++;
|
||||||
|
} while (r && counter < 255);
|
||||||
|
temp_c[counter] = 0;
|
||||||
|
temp = temp_c;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
string LinuxProcessBase::getPath()
|
||||||
|
{
|
||||||
|
char cwd_name[256];
|
||||||
|
char target_name[1024];
|
||||||
|
int target_result;
|
||||||
|
|
||||||
|
sprintf(cwd_name,"/proc/%d/cwd", getPID());
|
||||||
|
// resolve /proc/PID/exe link
|
||||||
|
target_result = readlink(cwd_name, target_name, sizeof(target_name));
|
||||||
|
target_name[target_result] = '\0';
|
||||||
|
return(string(target_name));
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LINUX_PROCESS_H_INCLUDED
|
||||||
|
#define LINUX_PROCESS_H_INCLUDED
|
||||||
|
|
||||||
|
#ifdef LINUX_BUILD
|
||||||
|
|
||||||
|
#include "dfhack/DFProcess.h"
|
||||||
|
|
||||||
|
namespace DFHack
|
||||||
|
{
|
||||||
|
class LinuxProcessBase : public Process
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
VersionInfo * my_descriptor;
|
||||||
|
pid_t my_pid;
|
||||||
|
string memFile;
|
||||||
|
int memFileHandle;
|
||||||
|
bool attached;
|
||||||
|
bool suspended;
|
||||||
|
bool identified;
|
||||||
|
public:
|
||||||
|
LinuxProcessBase(uint32_t pid);
|
||||||
|
~LinuxProcessBase();
|
||||||
|
|
||||||
|
bool attach();
|
||||||
|
bool detach();
|
||||||
|
|
||||||
|
bool suspend();
|
||||||
|
bool asyncSuspend();
|
||||||
|
bool resume();
|
||||||
|
bool forceresume();
|
||||||
|
|
||||||
|
void readQuad(const uint32_t address, uint64_t & value);
|
||||||
|
void writeQuad(const uint32_t address, const uint64_t value);
|
||||||
|
|
||||||
|
void readDWord(const uint32_t address, uint32_t & value);
|
||||||
|
void writeDWord(const uint32_t address, const uint32_t value);
|
||||||
|
|
||||||
|
void readFloat(const uint32_t address, float & value);
|
||||||
|
|
||||||
|
void readWord(const uint32_t address, uint16_t & value);
|
||||||
|
void writeWord(const uint32_t address, const uint16_t value);
|
||||||
|
|
||||||
|
void readByte(const uint32_t address, uint8_t & value);
|
||||||
|
void writeByte(const uint32_t address, const uint8_t value);
|
||||||
|
|
||||||
|
void read( uint32_t address, uint32_t length, uint8_t* buffer);
|
||||||
|
void write(uint32_t address, uint32_t length, uint8_t* buffer);
|
||||||
|
|
||||||
|
const std::string readCString (uint32_t offset);
|
||||||
|
|
||||||
|
bool isSuspended();
|
||||||
|
bool isAttached();
|
||||||
|
bool isIdentified();
|
||||||
|
|
||||||
|
VersionInfo *getDescriptor();
|
||||||
|
int getPID();
|
||||||
|
std::string getPath();
|
||||||
|
|
||||||
|
bool getThreadIDs(std::vector<uint32_t> & threads );
|
||||||
|
void getMemRanges(std::vector<t_memrange> & ranges );
|
||||||
|
// get module index by name and version. bool 1 = error
|
||||||
|
bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) { OUTPUT=0; return false;};
|
||||||
|
// get the SHM start if available
|
||||||
|
char * getSHMStart (void){return 0;};
|
||||||
|
// set a SHM command and wait for a response
|
||||||
|
bool SetAndWait (uint32_t state){return false;};
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PROCESS_FACTORY_H_INCLUDED
|
||||||
|
#define PROCESS_FACTORY_H_INCLUDED
|
||||||
|
|
||||||
|
#include "dfhack/DFProcess.h"
|
||||||
|
|
||||||
|
namespace DFHack
|
||||||
|
{
|
||||||
|
Process* createNormalProcess(uint32_t pid, std::vector <VersionInfo *> & known_versions);
|
||||||
|
Process* createSHMProcess(uint32_t pid, std::vector <VersionInfo *> & known_versions);
|
||||||
|
#ifdef LINUX_BUILD
|
||||||
|
Process* createWineProcess(uint32_t pid, std::vector <VersionInfo *> & known_versions);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
@ -1,216 +0,0 @@
|
|||||||
/*
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef PROCESS_INTERNAL_H_INCLUDED
|
|
||||||
#define PROCESS_INTERNAL_H_INCLUDED
|
|
||||||
|
|
||||||
#include "dfhack/DFProcess.h"
|
|
||||||
|
|
||||||
namespace DFHack
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Compiler appeasement area. Not worth a look really... //
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
class DFHACK_EXPORT NormalProcess : public Process
|
|
||||||
{
|
|
||||||
friend class ProcessEnumerator;
|
|
||||||
class Private;
|
|
||||||
private:
|
|
||||||
Private * const d;
|
|
||||||
public:
|
|
||||||
NormalProcess(uint32_t pid, std::vector <VersionInfo *> & known_versions);
|
|
||||||
~NormalProcess();
|
|
||||||
bool attach();
|
|
||||||
bool detach();
|
|
||||||
|
|
||||||
bool suspend();
|
|
||||||
bool asyncSuspend();
|
|
||||||
bool resume();
|
|
||||||
bool forceresume();
|
|
||||||
|
|
||||||
void readQuad(const uint32_t address, uint64_t & value);
|
|
||||||
void writeQuad(const uint32_t address, const uint64_t value);
|
|
||||||
|
|
||||||
void readDWord(const uint32_t address, uint32_t & value);
|
|
||||||
void writeDWord(const uint32_t address, const uint32_t value);
|
|
||||||
|
|
||||||
void readFloat(const uint32_t address, float & value);
|
|
||||||
|
|
||||||
void readWord(const uint32_t address, uint16_t & value);
|
|
||||||
void writeWord(const uint32_t address, const uint16_t value);
|
|
||||||
|
|
||||||
void readByte(const uint32_t address, uint8_t & value);
|
|
||||||
void writeByte(const uint32_t address, const uint8_t value);
|
|
||||||
|
|
||||||
void read( uint32_t address, uint32_t length, uint8_t* buffer);
|
|
||||||
void write(uint32_t address, uint32_t length, uint8_t* buffer);
|
|
||||||
|
|
||||||
const std::string readSTLString (uint32_t offset);
|
|
||||||
size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity);
|
|
||||||
void writeSTLString(const uint32_t address, const std::string writeString){};
|
|
||||||
// get class name of an object with rtti/type info
|
|
||||||
std::string readClassName(uint32_t vptr);
|
|
||||||
|
|
||||||
const std::string readCString (uint32_t offset);
|
|
||||||
|
|
||||||
bool isSuspended();
|
|
||||||
bool isAttached();
|
|
||||||
bool isIdentified();
|
|
||||||
|
|
||||||
bool getThreadIDs(std::vector<uint32_t> & threads );
|
|
||||||
void getMemRanges(std::vector<t_memrange> & ranges );
|
|
||||||
VersionInfo *getDescriptor();
|
|
||||||
int getPID();
|
|
||||||
std::string getPath();
|
|
||||||
// get module index by name and version. bool 1 = error
|
|
||||||
bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) { OUTPUT=0; return false;};
|
|
||||||
// get the SHM start if available
|
|
||||||
char * getSHMStart (void){return 0;};
|
|
||||||
// set a SHM command and wait for a response
|
|
||||||
bool SetAndWait (uint32_t state){return false;};
|
|
||||||
};
|
|
||||||
|
|
||||||
class DFHACK_EXPORT SHMProcess : public Process
|
|
||||||
{
|
|
||||||
friend class ProcessEnumerator;
|
|
||||||
class Private;
|
|
||||||
private:
|
|
||||||
Private * const d;
|
|
||||||
|
|
||||||
public:
|
|
||||||
SHMProcess(uint32_t PID, std::vector <VersionInfo *> & known_versions);
|
|
||||||
~SHMProcess();
|
|
||||||
// Set up stuff so we can read memory
|
|
||||||
bool attach();
|
|
||||||
bool detach();
|
|
||||||
|
|
||||||
bool suspend();
|
|
||||||
bool asyncSuspend();
|
|
||||||
bool resume();
|
|
||||||
bool forceresume();
|
|
||||||
|
|
||||||
void readQuad(const uint32_t address, uint64_t & value);
|
|
||||||
void writeQuad(const uint32_t address, const uint64_t value);
|
|
||||||
|
|
||||||
void readDWord(const uint32_t address, uint32_t & value);
|
|
||||||
void writeDWord(const uint32_t address, const uint32_t value);
|
|
||||||
|
|
||||||
void readFloat(const uint32_t address, float & value);
|
|
||||||
|
|
||||||
void readWord(const uint32_t address, uint16_t & value);
|
|
||||||
void writeWord(const uint32_t address, const uint16_t value);
|
|
||||||
|
|
||||||
void readByte(const uint32_t address, uint8_t & value);
|
|
||||||
void writeByte(const uint32_t address, const uint8_t value);
|
|
||||||
|
|
||||||
void read( uint32_t address, uint32_t length, uint8_t* buffer);
|
|
||||||
void write(uint32_t address, uint32_t length, uint8_t* buffer);
|
|
||||||
|
|
||||||
const std::string readSTLString (uint32_t offset);
|
|
||||||
size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity);
|
|
||||||
void writeSTLString(const uint32_t address, const std::string writeString);
|
|
||||||
// get class name of an object with rtti/type info
|
|
||||||
std::string readClassName(uint32_t vptr);
|
|
||||||
|
|
||||||
const std::string readCString (uint32_t offset);
|
|
||||||
|
|
||||||
bool isSuspended();
|
|
||||||
bool isAttached();
|
|
||||||
bool isIdentified();
|
|
||||||
|
|
||||||
bool getThreadIDs(std::vector<uint32_t> & threads );
|
|
||||||
void getMemRanges(std::vector<t_memrange> & ranges );
|
|
||||||
VersionInfo *getDescriptor();
|
|
||||||
int getPID();
|
|
||||||
std::string getPath();
|
|
||||||
// get module index by name and version. bool 1 = error
|
|
||||||
bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT);
|
|
||||||
// get the SHM start if available
|
|
||||||
char * getSHMStart (void);
|
|
||||||
bool SetAndWait (uint32_t state);
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef LINUX_BUILD
|
|
||||||
class DFHACK_EXPORT WineProcess : public Process
|
|
||||||
{
|
|
||||||
friend class ProcessEnumerator;
|
|
||||||
class Private;
|
|
||||||
private:
|
|
||||||
Private * const d;
|
|
||||||
|
|
||||||
public:
|
|
||||||
WineProcess(uint32_t pid, std::vector <VersionInfo *> & known_versions);
|
|
||||||
~WineProcess();
|
|
||||||
bool attach();
|
|
||||||
bool detach();
|
|
||||||
|
|
||||||
bool suspend();
|
|
||||||
bool asyncSuspend();
|
|
||||||
bool resume();
|
|
||||||
bool forceresume();
|
|
||||||
|
|
||||||
void readQuad(const uint32_t address, uint64_t & value);
|
|
||||||
void writeQuad(const uint32_t address, const uint64_t value);
|
|
||||||
|
|
||||||
void readDWord(const uint32_t address, uint32_t & value);
|
|
||||||
void writeDWord(const uint32_t address, const uint32_t value);
|
|
||||||
|
|
||||||
void readFloat(const uint32_t address, float & value);
|
|
||||||
|
|
||||||
void readWord(const uint32_t address, uint16_t & value);
|
|
||||||
void writeWord(const uint32_t address, const uint16_t value);
|
|
||||||
|
|
||||||
void readByte(const uint32_t address, uint8_t & value);
|
|
||||||
void writeByte(const uint32_t address, const uint8_t value);
|
|
||||||
|
|
||||||
void read( uint32_t address, uint32_t length, uint8_t* buffer);
|
|
||||||
void write(uint32_t address, uint32_t length, uint8_t* buffer);
|
|
||||||
|
|
||||||
const std::string readSTLString (uint32_t offset);
|
|
||||||
size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity);
|
|
||||||
void writeSTLString(const uint32_t address, const std::string writeString){};
|
|
||||||
// get class name of an object with rtti/type info
|
|
||||||
std::string readClassName(uint32_t vptr);
|
|
||||||
|
|
||||||
const std::string readCString (uint32_t offset);
|
|
||||||
|
|
||||||
bool isSuspended();
|
|
||||||
bool isAttached();
|
|
||||||
bool isIdentified();
|
|
||||||
|
|
||||||
bool getThreadIDs(std::vector<uint32_t> & threads );
|
|
||||||
void getMemRanges(std::vector<t_memrange> & ranges );
|
|
||||||
VersionInfo *getDescriptor();
|
|
||||||
int getPID();
|
|
||||||
// get module index by name and version. bool 1 = error
|
|
||||||
bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) {OUTPUT=0; return false;};
|
|
||||||
// get the SHM start if available
|
|
||||||
char * getSHMStart (void){return 0;};
|
|
||||||
bool SetAndWait (uint32_t state){return false;};
|
|
||||||
std::string getPath();
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SHM_PROCESS_H_INCLUDED
|
||||||
|
#define SHM_PROCESS_H_INCLUDED
|
||||||
|
|
||||||
|
#include "dfhack/DFProcess.h"
|
||||||
|
|
||||||
|
namespace DFHack
|
||||||
|
{
|
||||||
|
class DFHACK_EXPORT SHMProcess : public Process
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
class Private;
|
||||||
|
Private * const d;
|
||||||
|
|
||||||
|
public:
|
||||||
|
SHMProcess(uint32_t PID, std::vector <VersionInfo *> & known_versions);
|
||||||
|
~SHMProcess();
|
||||||
|
// Set up stuff so we can read memory
|
||||||
|
bool attach();
|
||||||
|
bool detach();
|
||||||
|
|
||||||
|
bool suspend();
|
||||||
|
bool asyncSuspend();
|
||||||
|
bool resume();
|
||||||
|
bool forceresume();
|
||||||
|
|
||||||
|
void readQuad(const uint32_t address, uint64_t & value);
|
||||||
|
void writeQuad(const uint32_t address, const uint64_t value);
|
||||||
|
|
||||||
|
void readDWord(const uint32_t address, uint32_t & value);
|
||||||
|
void writeDWord(const uint32_t address, const uint32_t value);
|
||||||
|
|
||||||
|
void readFloat(const uint32_t address, float & value);
|
||||||
|
|
||||||
|
void readWord(const uint32_t address, uint16_t & value);
|
||||||
|
void writeWord(const uint32_t address, const uint16_t value);
|
||||||
|
|
||||||
|
void readByte(const uint32_t address, uint8_t & value);
|
||||||
|
void writeByte(const uint32_t address, const uint8_t value);
|
||||||
|
|
||||||
|
void read( uint32_t address, uint32_t length, uint8_t* buffer);
|
||||||
|
void write(uint32_t address, uint32_t length, uint8_t* buffer);
|
||||||
|
|
||||||
|
const std::string readSTLString (uint32_t offset);
|
||||||
|
size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity);
|
||||||
|
void writeSTLString(const uint32_t address, const std::string writeString);
|
||||||
|
// get class name of an object with rtti/type info
|
||||||
|
std::string readClassName(uint32_t vptr);
|
||||||
|
|
||||||
|
const std::string readCString (uint32_t offset);
|
||||||
|
|
||||||
|
bool isSuspended();
|
||||||
|
bool isAttached();
|
||||||
|
bool isIdentified();
|
||||||
|
|
||||||
|
bool getThreadIDs(std::vector<uint32_t> & threads );
|
||||||
|
void getMemRanges(std::vector<t_memrange> & ranges );
|
||||||
|
VersionInfo *getDescriptor();
|
||||||
|
int getPID();
|
||||||
|
std::string getPath();
|
||||||
|
// get module index by name and version. bool 1 = error
|
||||||
|
bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT);
|
||||||
|
// get the SHM start if available
|
||||||
|
char * getSHMStart (void);
|
||||||
|
bool SetAndWait (uint32_t state);
|
||||||
|
private:
|
||||||
|
bool acquireSuspendLock();
|
||||||
|
bool releaseSuspendLock();
|
||||||
|
};
|
||||||
|
|
||||||
|
class SHMProcess::Private
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Private(SHMProcess * self_);
|
||||||
|
~Private(){}
|
||||||
|
VersionInfo * memdescriptor;
|
||||||
|
SHMProcess * self;
|
||||||
|
char *shm_addr;
|
||||||
|
int attachmentIdx;
|
||||||
|
|
||||||
|
bool attached;
|
||||||
|
bool locked;
|
||||||
|
bool identified;
|
||||||
|
bool useYield;
|
||||||
|
|
||||||
|
#ifdef LINUX_BUILD
|
||||||
|
pid_t process_ID;
|
||||||
|
int shm_ID;
|
||||||
|
int server_lock;
|
||||||
|
int client_lock;
|
||||||
|
int suspend_lock;
|
||||||
|
#else
|
||||||
|
typedef unit32_t pid_t;
|
||||||
|
uint32_t process_ID;
|
||||||
|
HANDLE DFSVMutex;
|
||||||
|
HANDLE DFCLMutex;
|
||||||
|
HANDLE DFCLSuspendMutex;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool validate(std::vector< VersionInfo* >& known_versions);
|
||||||
|
|
||||||
|
bool Aux_Core_Attach(bool & versionOK, pid_t& PID);
|
||||||
|
bool SetAndWait (uint32_t state);
|
||||||
|
bool GetLocks();
|
||||||
|
bool AreLocksOk();
|
||||||
|
void FreeLocks();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// some helpful macros to keep the code bloat in check
|
||||||
|
#define SHMCMD ( (uint32_t *) shm_addr)[attachmentIdx]
|
||||||
|
#define D_SHMCMD ( (uint32_t *) (d->shm_addr))[d->attachmentIdx]
|
||||||
|
|
||||||
|
#define SHMHDR ((shm_core_hdr *)shm_addr)
|
||||||
|
#define D_SHMHDR ((shm_core_hdr *)(d->shm_addr))
|
||||||
|
|
||||||
|
#define SHMDATA(type) ((type *)(shm_addr + SHM_HEADER))
|
||||||
|
#define D_SHMDATA(type) ((type *)(d->shm_addr + SHM_HEADER))
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef WINDOWS_PROCESS_H_INCLUDED
|
||||||
|
#define WINDOWS_PROCESS_H_INCLUDED
|
||||||
|
#ifndef LINUX_BUILD
|
||||||
|
|
||||||
|
#include "dfhack/DFProcess.h"
|
||||||
|
|
||||||
|
namespace DFHack
|
||||||
|
{
|
||||||
|
class NormalProcess : public Process
|
||||||
|
{
|
||||||
|
class Private;
|
||||||
|
private:
|
||||||
|
Private * const d;
|
||||||
|
public:
|
||||||
|
NormalProcess(uint32_t pid, std::vector <VersionInfo *> & known_versions);
|
||||||
|
~NormalProcess();
|
||||||
|
bool attach();
|
||||||
|
bool detach();
|
||||||
|
|
||||||
|
bool suspend();
|
||||||
|
bool asyncSuspend();
|
||||||
|
bool resume();
|
||||||
|
bool forceresume();
|
||||||
|
|
||||||
|
void readQuad(const uint32_t address, uint64_t & value);
|
||||||
|
void writeQuad(const uint32_t address, const uint64_t value);
|
||||||
|
|
||||||
|
void readDWord(const uint32_t address, uint32_t & value);
|
||||||
|
void writeDWord(const uint32_t address, const uint32_t value);
|
||||||
|
|
||||||
|
void readFloat(const uint32_t address, float & value);
|
||||||
|
|
||||||
|
void readWord(const uint32_t address, uint16_t & value);
|
||||||
|
void writeWord(const uint32_t address, const uint16_t value);
|
||||||
|
|
||||||
|
void readByte(const uint32_t address, uint8_t & value);
|
||||||
|
void writeByte(const uint32_t address, const uint8_t value);
|
||||||
|
|
||||||
|
void read( uint32_t address, uint32_t length, uint8_t* buffer);
|
||||||
|
void write(uint32_t address, uint32_t length, uint8_t* buffer);
|
||||||
|
|
||||||
|
const std::string readSTLString (uint32_t offset);
|
||||||
|
size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity);
|
||||||
|
void writeSTLString(const uint32_t address, const std::string writeString){};
|
||||||
|
// get class name of an object with rtti/type info
|
||||||
|
std::string readClassName(uint32_t vptr);
|
||||||
|
|
||||||
|
const std::string readCString (uint32_t offset);
|
||||||
|
|
||||||
|
bool isSuspended();
|
||||||
|
bool isAttached();
|
||||||
|
bool isIdentified();
|
||||||
|
|
||||||
|
bool getThreadIDs(std::vector<uint32_t> & threads );
|
||||||
|
void getMemRanges(std::vector<t_memrange> & ranges );
|
||||||
|
VersionInfo *getDescriptor();
|
||||||
|
int getPID();
|
||||||
|
std::string getPath();
|
||||||
|
// get module index by name and version. bool 1 = error
|
||||||
|
bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) { OUTPUT=0; return false;};
|
||||||
|
// get the SHM start if available
|
||||||
|
char * getSHMStart (void){return 0;};
|
||||||
|
// set a SHM command and wait for a response
|
||||||
|
bool SetAndWait (uint32_t state){return false;};
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
Loading…
Reference in New Issue