2009-12-22 14:19:39 -07:00
|
|
|
/*
|
|
|
|
www.sourceforge.net/projects/dfhack
|
|
|
|
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
#include "DFCommonInternal.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <time.h>
|
2010-01-03 22:20:28 -07:00
|
|
|
#include "../shmserver/shms.h"
|
2010-03-04 16:05:01 -07:00
|
|
|
#include "../shmserver/mod-core.h"
|
2009-12-26 20:51:54 -07:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
2010-02-28 10:08:44 -07:00
|
|
|
#include <sched.h>
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2009-12-26 20:51:54 -07:00
|
|
|
// a full memory barrier! better be safe than sorry.
|
|
|
|
#define gcc_barrier asm volatile("" ::: "memory"); __sync_synchronize();
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
class SHMProcess::Private
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
{
|
|
|
|
my_descriptor = NULL;
|
|
|
|
my_pid = 0;
|
2009-12-26 20:51:54 -07:00
|
|
|
my_shm = 0;
|
|
|
|
my_shmid = -1;
|
2009-12-22 14:19:39 -07:00
|
|
|
my_window = NULL;
|
|
|
|
attached = false;
|
|
|
|
suspended = false;
|
|
|
|
identified = false;
|
2010-03-02 21:43:38 -07:00
|
|
|
useYield = false;
|
2009-12-22 14:19:39 -07:00
|
|
|
};
|
|
|
|
~Private(){};
|
|
|
|
memory_info * my_descriptor;
|
|
|
|
DFWindow * my_window;
|
2009-12-24 19:49:35 -07:00
|
|
|
pid_t my_pid;
|
2009-12-22 14:19:39 -07:00
|
|
|
char *my_shm;
|
|
|
|
int my_shmid;
|
2010-03-04 16:05:01 -07:00
|
|
|
Process* q;
|
2009-12-22 14:19:39 -07:00
|
|
|
|
|
|
|
bool attached;
|
|
|
|
bool suspended;
|
|
|
|
bool identified;
|
2010-03-02 21:43:38 -07:00
|
|
|
bool useYield;
|
2009-12-26 20:51:54 -07:00
|
|
|
|
2010-02-22 15:34:20 -07:00
|
|
|
bool validate(char* exe_file, uint32_t pid, std::vector< memory_info* >& known_versions);
|
2010-03-07 21:15:11 -07:00
|
|
|
|
|
|
|
bool Aux_Core_Attach(bool & versionOK, pid_t & PID);
|
2010-03-04 16:05:01 -07:00
|
|
|
bool waitWhile (uint32_t state);
|
2009-12-22 14:19:39 -07:00
|
|
|
};
|
|
|
|
|
2010-03-02 21:43:38 -07:00
|
|
|
// some helpful macros to keep the code bloat in check
|
|
|
|
#define SHMCMD ((shm_cmd *)my_shm)->pingpong
|
|
|
|
#define D_SHMCMD ((shm_cmd *)d->my_shm)->pingpong
|
|
|
|
|
2010-03-03 20:40:06 -07:00
|
|
|
#define SHMHDR ((shm_core_hdr *)my_shm)
|
|
|
|
#define D_SHMHDR ((shm_core_hdr *)d->my_shm)
|
2010-03-02 21:43:38 -07:00
|
|
|
|
2010-03-07 21:15:11 -07:00
|
|
|
#define SHMDATA(type) ((type *)(my_shm + SHM_HEADER))
|
|
|
|
#define D_SHMDATA(type) ((type *)(d->my_shm + SHM_HEADER))
|
|
|
|
|
2010-03-03 20:40:06 -07:00
|
|
|
/*
|
|
|
|
Yeah. with no way to synchronize things (locks are slow, the OS doesn't give us enough control over scheduling)
|
|
|
|
we end up with this silly thing
|
|
|
|
*/
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::Private::waitWhile (uint32_t state)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
uint32_t cnt = 0;
|
|
|
|
struct shmid_ds descriptor;
|
2010-03-02 21:43:38 -07:00
|
|
|
while (SHMCMD == state)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-03 20:40:06 -07:00
|
|
|
if(cnt == 10000)// check if the other process is still there
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-02-28 10:08:44 -07:00
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
shmctl(my_shmid, IPC_STAT, &descriptor);
|
2010-03-08 19:30:22 -07:00
|
|
|
if(descriptor.shm_nattch == 1)// DF crashed or exited - no way to tell?
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-08 19:30:22 -07:00
|
|
|
//detach the shared memory
|
|
|
|
shmdt(my_shm);
|
2009-12-22 14:19:39 -07:00
|
|
|
attached = suspended = false;
|
2010-03-08 19:30:22 -07:00
|
|
|
|
|
|
|
// we aren't the current process anymore
|
|
|
|
g_pProcess = NULL;
|
|
|
|
|
|
|
|
throw Error::SHMServerDisappeared();
|
2009-12-22 14:19:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cnt = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-03-03 20:40:06 -07:00
|
|
|
if(useYield)
|
|
|
|
{
|
|
|
|
SCHED_YIELD
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
cnt++;
|
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
if(SHMCMD == CORE_ERROR)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
SHMCMD = CORE_RUNNING;
|
2009-12-22 14:19:39 -07:00
|
|
|
attached = suspended = false;
|
|
|
|
cerr << "shm server error!" << endl;
|
|
|
|
assert (false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
/*
|
|
|
|
Yeah. with no way to synchronize things (locks are slow, the OS doesn't give us enough control over scheduling)
|
|
|
|
we end up with this silly thing
|
|
|
|
*/
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::waitWhile (uint32_t state)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-03-04 18:45:53 -07:00
|
|
|
return d->waitWhile(state);
|
2010-03-04 16:05:01 -07:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:40:06 -07:00
|
|
|
uint32_t OS_getAffinity()
|
|
|
|
{
|
|
|
|
cpu_set_t mask;
|
|
|
|
sched_getaffinity(0,sizeof(cpu_set_t),&mask);
|
|
|
|
// FIXME: truncation
|
2010-03-07 21:15:11 -07:00
|
|
|
uint32_t affinity = *(uint32_t *) &mask;
|
|
|
|
return affinity;
|
2010-03-03 20:40:06 -07:00
|
|
|
}
|
|
|
|
|
2010-03-07 21:15:11 -07:00
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::Private::Aux_Core_Attach(bool & versionOK, pid_t & PID)
|
2010-03-03 20:40:06 -07:00
|
|
|
{
|
2010-03-07 21:15:11 -07:00
|
|
|
SHMDATA(coreattach)->cl_affinity = OS_getAffinity();
|
2010-03-03 20:40:06 -07:00
|
|
|
gcc_barrier
|
2010-03-07 21:15:11 -07:00
|
|
|
SHMCMD = CORE_ATTACH;
|
|
|
|
if(!waitWhile(CORE_ATTACH))
|
|
|
|
return false;
|
2010-03-03 20:40:06 -07:00
|
|
|
gcc_barrier
|
2010-03-07 21:15:11 -07:00
|
|
|
versionOK =( SHMDATA(coreattach)->sv_version == CORE_VERSION );
|
|
|
|
PID = SHMDATA(coreattach)->sv_PID;
|
|
|
|
useYield = SHMDATA(coreattach)->sv_useYield;
|
2010-03-03 20:40:06 -07:00
|
|
|
#ifdef DEBUG
|
2010-03-07 21:15:11 -07:00
|
|
|
if(useYield) cerr << "Using Yield!" << endl;
|
2010-03-03 20:40:06 -07:00
|
|
|
#endif
|
2010-03-07 21:15:11 -07:00
|
|
|
return true;
|
2010-03-03 20:40:06 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
SHMProcess::SHMProcess(uint32_t PID, vector< memory_info* >& known_versions)
|
2009-12-22 14:19:39 -07:00
|
|
|
: d(new Private())
|
|
|
|
{
|
|
|
|
char exe_link_name [256];
|
|
|
|
char target_name[1024];
|
|
|
|
int target_result;
|
|
|
|
|
2009-12-25 09:55:10 -07:00
|
|
|
/*
|
2009-12-24 19:49:35 -07:00
|
|
|
* Locate the segment.
|
|
|
|
*/
|
2010-03-07 21:15:11 -07:00
|
|
|
if ((d->my_shmid = shmget(SHM_KEY + PID, SHM_SIZE, 0666)) < 0)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2009-12-25 09:55:10 -07:00
|
|
|
return;
|
2009-12-24 19:49:35 -07:00
|
|
|
}
|
2009-12-25 09:55:10 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach the segment
|
|
|
|
*/
|
|
|
|
if ((d->my_shm = (char *) shmat(d->my_shmid, NULL, 0)) == (char *) -1)
|
2009-12-24 19:49:35 -07:00
|
|
|
{
|
2009-12-25 09:55:10 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if there are two processes connected to the segment
|
|
|
|
*/
|
2009-12-26 20:51:54 -07:00
|
|
|
shmid_ds descriptor;
|
2009-12-25 09:55:10 -07:00
|
|
|
shmctl(d->my_shmid, IPC_STAT, &descriptor);
|
|
|
|
if(descriptor.shm_nattch != 2)// badness
|
|
|
|
{
|
2010-01-05 13:51:58 -07:00
|
|
|
fprintf(stderr,"dfhack: %d : invalid no. of processes connected\n", (int) descriptor.shm_nattch);
|
2009-12-26 20:51:54 -07:00
|
|
|
fprintf(stderr,"detach: %d",shmdt(d->my_shm));
|
2009-12-25 09:55:10 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-03-07 21:15:11 -07:00
|
|
|
* Test bridge version, get PID, sync Yield
|
2009-12-25 09:55:10 -07:00
|
|
|
*/
|
|
|
|
bool bridgeOK;
|
2010-03-07 21:15:11 -07:00
|
|
|
if(!d->Aux_Core_Attach(bridgeOK,d->my_pid))
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr,"DF terminated during reading\n");
|
2010-03-07 21:15:11 -07:00
|
|
|
shmdt(d->my_shm);
|
2009-12-25 09:55:10 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!bridgeOK)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"SHM bridge version mismatch\n");
|
2010-03-07 21:15:11 -07:00
|
|
|
shmdt(d->my_shm);
|
2009-12-25 09:55:10 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
|
|
|
|
// find the binary
|
|
|
|
sprintf(exe_link_name,"/proc/%d/exe", d->my_pid);
|
|
|
|
target_result = readlink(exe_link_name, target_name, sizeof(target_name)-1);
|
|
|
|
if (target_result == -1)
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2010-03-07 21:15:11 -07:00
|
|
|
perror("readlink");
|
|
|
|
shmdt(d->my_shm);
|
|
|
|
return;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
|
|
|
|
// make sure we have a null terminated string...
|
|
|
|
// see http://www.opengroup.org/onlinepubs/000095399/functions/readlink.html
|
|
|
|
target_name[target_result] = 0;
|
|
|
|
|
|
|
|
// try to identify the DF version
|
|
|
|
d->validate(target_name, d->my_pid, known_versions);
|
|
|
|
d->my_window = new DFWindow(this);
|
|
|
|
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2009-12-25 09:55:10 -07:00
|
|
|
// at this point, DF is stopped and waiting for commands. make it run again
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_RUNNING;
|
2010-01-05 13:51:58 -07:00
|
|
|
shmdt(d->my_shm); // detach so we don't attach twice when attach() is called
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::isSuspended()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return d->suspended;
|
|
|
|
}
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::isAttached()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return d->attached;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::isIdentified()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return d->identified;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::Private::validate(char * exe_file, uint32_t pid, vector <memory_info *> & known_versions)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
md5wrapper md5;
|
|
|
|
// get hash of the running DF process
|
|
|
|
string hash = md5.getHashFromFile(exe_file);
|
2010-02-22 15:34:20 -07:00
|
|
|
vector<memory_info *>::iterator it;
|
2009-12-22 14:19:39 -07:00
|
|
|
cerr << exe_file << " " << hash << endl;
|
|
|
|
// iterate over the list of memory locations
|
|
|
|
for ( it=known_versions.begin() ; it < known_versions.end(); it++ )
|
|
|
|
{
|
2010-02-27 22:21:50 -07:00
|
|
|
try{
|
|
|
|
if(hash == (*it)->getString("md5")) // are the md5 hashes the same?
|
|
|
|
{
|
|
|
|
memory_info * m = *it;
|
|
|
|
my_descriptor = m;
|
|
|
|
my_pid = pid;
|
|
|
|
identified = true;
|
|
|
|
cerr << "identified " << m->getVersion() << endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Error::MissingMemoryDefinition&)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-02-27 22:21:50 -07:00
|
|
|
continue;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
SHMProcess::~SHMProcess()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
if(d->attached)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
}
|
|
|
|
// destroy data model. this is assigned by processmanager
|
|
|
|
if(d->my_window)
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2009-12-22 14:19:39 -07:00
|
|
|
delete d->my_window;
|
2009-12-25 09:55:10 -07:00
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
memory_info * SHMProcess::getDescriptor()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return d->my_descriptor;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
DFWindow * SHMProcess::getWindow()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return d->my_window;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
int SHMProcess::getPID()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return d->my_pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: implement
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::getThreadIDs(vector<uint32_t> & threads )
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: cross-reference with ELF segment entries?
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::getMemRanges( vector<t_memrange> & ranges )
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
char permissions[5]; // r/-, w/-, x/-, p/s, 0
|
|
|
|
|
|
|
|
sprintf(buffer, "/proc/%lu/maps", d->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';
|
|
|
|
ranges.push_back(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::suspend()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2009-12-22 14:19:39 -07:00
|
|
|
return false;
|
2009-12-25 09:55:10 -07:00
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
if(d->suspended)
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2009-12-22 14:19:39 -07:00
|
|
|
return true;
|
2009-12-25 09:55:10 -07:00
|
|
|
}
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_SUSPEND;
|
2010-03-04 16:05:01 -07:00
|
|
|
if(!waitWhile(CORE_SUSPEND))
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2009-12-22 14:19:39 -07:00
|
|
|
return false;
|
2009-12-25 09:55:10 -07:00
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
d->suspended = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::asyncSuspend()
|
2010-01-01 19:25:21 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(d->suspended)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-02 21:43:38 -07:00
|
|
|
if(D_SHMCMD == CORE_SUSPENDED)
|
2010-01-01 19:25:21 -07:00
|
|
|
{
|
|
|
|
d->suspended = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_SUSPEND;
|
2010-01-01 19:25:21 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::forceresume()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return resume();
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::resume()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
return false;
|
|
|
|
if(!d->suspended)
|
|
|
|
return true;
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_RUNNING;
|
2009-12-22 14:19:39 -07:00
|
|
|
d->suspended = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::attach()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
int status;
|
2010-01-01 15:19:09 -07:00
|
|
|
if(g_pProcess != 0)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
cerr << "there's already a different process attached" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-26 20:51:54 -07:00
|
|
|
/*
|
|
|
|
* Attach the segment
|
|
|
|
*/
|
|
|
|
if ((d->my_shm = (char *) shmat(d->my_shmid, NULL, 0)) != (char *) -1)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2009-12-26 20:51:54 -07:00
|
|
|
d->attached = true;
|
|
|
|
if(suspend())
|
|
|
|
{
|
|
|
|
d->suspended = true;
|
|
|
|
g_pProcess = this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
d->attached = false;
|
|
|
|
cerr << "unable to suspend" << endl;
|
2010-01-01 15:19:09 -07:00
|
|
|
// FIXME: detach sehment here
|
2009-12-26 20:51:54 -07:00
|
|
|
return false;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2009-12-26 20:51:54 -07:00
|
|
|
cerr << "unable to attach" << endl;
|
2009-12-22 14:19:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::detach()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2009-12-25 09:55:10 -07:00
|
|
|
if(!d->attached)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(d->suspended)
|
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
2010-01-01 15:19:09 -07:00
|
|
|
// detach segment
|
|
|
|
if(shmdt(d->my_shm) != -1)
|
|
|
|
{
|
|
|
|
d->attached = false;
|
|
|
|
d->suspended = false;
|
2010-02-28 10:08:44 -07:00
|
|
|
d->my_shm = 0;
|
2010-01-01 15:19:09 -07:00
|
|
|
g_pProcess = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fail if we can't detach
|
|
|
|
perror("failed to detach shared segment");
|
|
|
|
return false;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::read (uint32_t src_address, uint32_t size, uint8_t *target_buffer)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-01-01 19:25:21 -07:00
|
|
|
// normal read under 1MB
|
|
|
|
if(size <= SHM_BODY)
|
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = src_address;
|
|
|
|
D_SHMHDR->length = size;
|
2010-01-01 19:25:21 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_DFPP_READ;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_DFPP_READ);
|
2010-01-01 19:25:21 -07:00
|
|
|
memcpy (target_buffer, d->my_shm + SHM_HEADER,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
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = src_address;
|
|
|
|
D_SHMHDR->length = to_read;
|
2010-01-01 19:25:21 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_DFPP_READ;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_DFPP_READ);
|
2010-01-01 19:25:21 -07:00
|
|
|
memcpy (target_buffer, d->my_shm + SHM_HEADER,size);
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
uint8_t SHMProcess::readByte (const uint32_t offset)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_BYTE;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_BYTE);
|
2010-03-02 21:43:38 -07:00
|
|
|
return D_SHMHDR->value;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::readByte (const uint32_t offset, uint8_t &val )
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_BYTE;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_BYTE);
|
2010-03-02 21:43:38 -07:00
|
|
|
val = D_SHMHDR->value;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
uint16_t SHMProcess::readWord (const uint32_t offset)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_WORD;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_WORD);
|
2010-03-02 21:43:38 -07:00
|
|
|
return D_SHMHDR->value;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::readWord (const uint32_t offset, uint16_t &val)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_WORD;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_WORD);
|
2010-03-02 21:43:38 -07:00
|
|
|
val = D_SHMHDR->value;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
uint32_t SHMProcess::readDWord (const uint32_t offset)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_DWORD;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_DWORD);
|
2010-03-02 21:43:38 -07:00
|
|
|
return D_SHMHDR->value;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::readDWord (const uint32_t offset, uint32_t &val)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_DWORD;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_DWORD);
|
2010-03-02 21:43:38 -07:00
|
|
|
val = D_SHMHDR->value;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WRITING
|
|
|
|
*/
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::writeDWord (uint32_t offset, uint32_t data)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->value = data;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_WRITE_DWORD;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_WRITE_DWORD);
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// using these is expensive.
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::writeWord (uint32_t offset, uint16_t data)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->value = data;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_WRITE_WORD;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_WRITE_WORD);
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::writeByte (uint32_t offset, uint8_t data)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->value = data;
|
2009-12-26 20:51:54 -07:00
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_WRITE_BYTE;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_WRITE_BYTE);
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::write (uint32_t dst_address, uint32_t size, uint8_t *source_buffer)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-01-01 19:25:21 -07:00
|
|
|
// normal write under 1MB
|
|
|
|
if(size <= SHM_BODY)
|
|
|
|
{
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = dst_address;
|
|
|
|
D_SHMHDR->length = size;
|
2010-01-01 19:25:21 -07:00
|
|
|
memcpy(d->my_shm+SHM_HEADER,source_buffer, size);
|
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_WRITE;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_WRITE);
|
2010-01-01 19:25:21 -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-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = dst_address;
|
|
|
|
D_SHMHDR->length = to_write;
|
2010-01-01 19:25:21 -07:00
|
|
|
memcpy(d->my_shm+SHM_HEADER,source_buffer, to_write);
|
|
|
|
gcc_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_WRITE;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_WRITE);
|
2010-01-01 19:25:21 -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);
|
|
|
|
}
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: butt-fugly
|
2010-03-09 07:15:15 -07:00
|
|
|
const std::string SHMProcess::readCString (uint32_t offset)
|
2009-12-22 14:19:39 -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
|
|
|
DfVector SHMProcess::readVector (uint32_t offset, uint32_t item_size)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
GNU libstdc++ vector is three pointers long
|
|
|
|
ptr start
|
|
|
|
ptr end
|
|
|
|
ptr alloc_end
|
|
|
|
|
|
|
|
we don't care about alloc_end because we don't try to add stuff
|
|
|
|
*/
|
|
|
|
uint32_t start = g_pProcess->readDWord(offset);
|
|
|
|
uint32_t end = g_pProcess->readDWord(offset+4);
|
|
|
|
uint32_t size = (end - start) /4;
|
|
|
|
return DfVector(start,size,item_size);
|
|
|
|
}
|
|
|
|
|
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-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-02-15 16:04:15 -07:00
|
|
|
full_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_STL_STRING;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_STL_STRING);
|
2010-02-15 16:04:15 -07:00
|
|
|
//int length = ((shm_retval *)d->my_shm)->value;
|
|
|
|
return(string( (char *)d->my_shm+SHM_HEADER));
|
|
|
|
}
|
|
|
|
|
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-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = offset;
|
2010-02-15 16:04:15 -07:00
|
|
|
full_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_READ_STL_STRING;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(CORE_READ_STL_STRING);
|
2010-03-02 21:43:38 -07:00
|
|
|
size_t length = D_SHMHDR->value;
|
2010-02-15 16:04:15 -07:00
|
|
|
size_t fit = min(bufcapacity - 1, length);
|
|
|
|
strncpy(buffer,(char *)d->my_shm+SHM_HEADER,fit);
|
|
|
|
buffer[fit] = 0;
|
|
|
|
return fit;
|
|
|
|
}
|
|
|
|
|
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-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = address;
|
2010-02-15 16:04:15 -07:00
|
|
|
strncpy(d->my_shm+SHM_HEADER,writeString.c_str(),writeString.length()+1); // length + 1 for the null terminator
|
|
|
|
full_barrier
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMCMD = CORE_WRITE_STL_STRING;
|
2010-03-04 16:05:01 -07:00
|
|
|
waitWhile(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 typeinfo = readDWord(vptr - 0x4);
|
|
|
|
int typestring = readDWord(typeinfo + 0x4);
|
|
|
|
string raw = readCString(typestring);
|
|
|
|
size_t start = raw.find_first_of("abcdefghijklmnopqrstuvwxyz");// trim numbers
|
|
|
|
size_t end = raw.length();
|
|
|
|
return raw.substr(start,end-start - 2); // trim the 'st' from the end
|
2010-03-04 16:05:01 -07:00
|
|
|
}
|
|
|
|
|
2010-03-07 21:15:11 -07:00
|
|
|
// FIXME: having this around could lead to bad things in the hands of unsuspecting fools
|
|
|
|
// *!!DON'T BE AN UNSUSPECTING FOOL!!*
|
|
|
|
// the whole SHM thing works only because copying DWORDS is an atomic operation on i386 and x86_64 archs
|
2010-03-04 16:05:01 -07:00
|
|
|
// get module index by name and version. bool 1 = 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
|
|
|
{
|
|
|
|
modulelookup * payload = (modulelookup *) (d->my_shm + SHM_HEADER);
|
|
|
|
payload->version = version;
|
2010-03-07 21:15:11 -07:00
|
|
|
|
|
|
|
strncpy(payload->name,name,255);
|
|
|
|
payload->name[255] = 0;
|
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
full_barrier
|
2010-03-07 21:15:11 -07:00
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
D_SHMCMD = CORE_ACQUIRE_MODULE;
|
2010-03-07 21:15:11 -07:00
|
|
|
if(!waitWhile(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
|
|
|
{
|
|
|
|
return d->my_shm;
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|