|
|
|
@ -28,6 +28,7 @@ distribution.
|
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include "../shmserver/shms.h"
|
|
|
|
|
#include "../shmserver/mod-core.h"
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <sched.h>
|
|
|
|
@ -50,6 +51,9 @@ class SHMProcess::Private
|
|
|
|
|
attached = false;
|
|
|
|
|
suspended = false;
|
|
|
|
|
identified = false;
|
|
|
|
|
useYield = false;
|
|
|
|
|
my_SVfileLock = -1;
|
|
|
|
|
my_CLfileLock = -1;
|
|
|
|
|
};
|
|
|
|
|
~Private(){};
|
|
|
|
|
memory_info * my_descriptor;
|
|
|
|
@ -57,45 +61,93 @@ class SHMProcess::Private
|
|
|
|
|
pid_t my_pid;
|
|
|
|
|
char *my_shm;
|
|
|
|
|
int my_shmid;
|
|
|
|
|
Process* q;
|
|
|
|
|
int my_SVfileLock;
|
|
|
|
|
int my_CLfileLock;
|
|
|
|
|
|
|
|
|
|
bool attached;
|
|
|
|
|
bool suspended;
|
|
|
|
|
bool identified;
|
|
|
|
|
bool useYield;
|
|
|
|
|
|
|
|
|
|
bool validate(char* exe_file, uint32_t pid, std::vector< memory_info* >& known_versions);
|
|
|
|
|
bool waitWhile (DF_PINGPONG state);
|
|
|
|
|
bool DF_TestBridgeVersion(bool & ret);
|
|
|
|
|
bool DF_GetPID(pid_t & ret);
|
|
|
|
|
|
|
|
|
|
bool Aux_Core_Attach(bool & versionOK, pid_t & PID);
|
|
|
|
|
bool waitWhile (uint32_t state);
|
|
|
|
|
bool GetLocks();
|
|
|
|
|
bool AreLocksOk();
|
|
|
|
|
void FreeLocks();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool SHMProcess::Private::waitWhile (DF_PINGPONG state)
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
#define SHMHDR ((shm_core_hdr *)my_shm)
|
|
|
|
|
#define D_SHMHDR ((shm_core_hdr *)d->my_shm)
|
|
|
|
|
|
|
|
|
|
#define SHMDATA(type) ((type *)(my_shm + SHM_HEADER))
|
|
|
|
|
#define D_SHMDATA(type) ((type *)(d->my_shm + SHM_HEADER))
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
bool SHMProcess::Private::waitWhile (uint32_t state)
|
|
|
|
|
{
|
|
|
|
|
uint32_t cnt = 0;
|
|
|
|
|
struct shmid_ds descriptor;
|
|
|
|
|
while (((shm_cmd *)my_shm)->pingpong == state)
|
|
|
|
|
while (SHMCMD == state)
|
|
|
|
|
{
|
|
|
|
|
if(cnt == 10000)
|
|
|
|
|
if(cnt == 10000)// check if the other process is still there
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
shmctl(my_shmid, IPC_STAT, &descriptor);
|
|
|
|
|
if(descriptor.shm_nattch == 1)// DF crashed?
|
|
|
|
|
if(descriptor.shm_nattch == 1)// DF crashed or exited - no way to tell?
|
|
|
|
|
{
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_cmd *)my_shm)->pingpong = DFPP_RUNNING;
|
|
|
|
|
//detach the shared memory
|
|
|
|
|
shmdt(my_shm);
|
|
|
|
|
attached = suspended = false;
|
|
|
|
|
|
|
|
|
|
// we aren't the current process anymore
|
|
|
|
|
g_pProcess = NULL;
|
|
|
|
|
|
|
|
|
|
throw Error::SHMServerDisappeared();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cnt = 0;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
if(!AreLocksOk())
|
|
|
|
|
{
|
|
|
|
|
//detach the shared memory
|
|
|
|
|
shmdt(my_shm);
|
|
|
|
|
attached = suspended = false;
|
|
|
|
|
|
|
|
|
|
// we aren't the current process anymore
|
|
|
|
|
g_pProcess = NULL;
|
|
|
|
|
FreeLocks();
|
|
|
|
|
|
|
|
|
|
throw Error::SHMServerDisappeared();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cnt = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(useYield)
|
|
|
|
|
{
|
|
|
|
|
SCHED_YIELD
|
|
|
|
|
}
|
|
|
|
|
cnt++;
|
|
|
|
|
}
|
|
|
|
|
if(((shm_cmd *)my_shm)->pingpong == DFPP_SV_ERROR)
|
|
|
|
|
if(SHMCMD == CORE_ERROR)
|
|
|
|
|
{
|
|
|
|
|
((shm_cmd *)my_shm)->pingpong = DFPP_RUNNING;
|
|
|
|
|
SHMCMD = CORE_RUNNING;
|
|
|
|
|
attached = suspended = false;
|
|
|
|
|
cerr << "shm server error!" << endl;
|
|
|
|
|
assert (false);
|
|
|
|
@ -104,31 +156,109 @@ bool SHMProcess::Private::waitWhile (DF_PINGPONG state)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SHMProcess::Private::DF_TestBridgeVersion(bool & ret)
|
|
|
|
|
/*
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
bool SHMProcess::waitWhile (uint32_t state)
|
|
|
|
|
{
|
|
|
|
|
return d->waitWhile(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t OS_getAffinity()
|
|
|
|
|
{
|
|
|
|
|
cpu_set_t mask;
|
|
|
|
|
sched_getaffinity(0,sizeof(cpu_set_t),&mask);
|
|
|
|
|
// FIXME: truncation
|
|
|
|
|
uint32_t affinity = *(uint32_t *) &mask;
|
|
|
|
|
return affinity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SHMProcess::Private::Aux_Core_Attach(bool & versionOK, pid_t & PID)
|
|
|
|
|
{
|
|
|
|
|
((shm_cmd *)my_shm)->pingpong = DFPP_VERSION;
|
|
|
|
|
SHMDATA(coreattach)->cl_affinity = OS_getAffinity();
|
|
|
|
|
gcc_barrier
|
|
|
|
|
if(!waitWhile(DFPP_VERSION))
|
|
|
|
|
SHMCMD = CORE_ATTACH;
|
|
|
|
|
if(!waitWhile(CORE_ATTACH))
|
|
|
|
|
return false;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_cmd *)my_shm)->pingpong = DFPP_SUSPENDED;
|
|
|
|
|
ret =( ((shm_retval *)my_shm)->value == PINGPONG_VERSION );
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SHMProcess::Private::DF_GetPID(pid_t & ret)
|
|
|
|
|
bool SHMProcess::Private::AreLocksOk()
|
|
|
|
|
{
|
|
|
|
|
((shm_cmd *)my_shm)->pingpong = DFPP_PID;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
if(!waitWhile(DFPP_PID))
|
|
|
|
|
// both locks are inited (we hold our lock)
|
|
|
|
|
if(my_CLfileLock != -1 && my_SVfileLock != -1)
|
|
|
|
|
{
|
|
|
|
|
if(lockf(my_SVfileLock,F_TEST,0) == -1) // and server holds its lock
|
|
|
|
|
{
|
|
|
|
|
return true; // OK, locks are good
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// locks are bad
|
|
|
|
|
return false;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_cmd *)my_shm)->pingpong = DFPP_SUSPENDED;
|
|
|
|
|
ret = ((shm_retval *)my_shm)->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SHMProcess::Private::FreeLocks()
|
|
|
|
|
{
|
|
|
|
|
if(my_CLfileLock != -1)
|
|
|
|
|
{
|
|
|
|
|
lockf(my_CLfileLock,F_ULOCK,0);
|
|
|
|
|
close(my_CLfileLock);
|
|
|
|
|
my_CLfileLock = -1;
|
|
|
|
|
}
|
|
|
|
|
if(my_SVfileLock != -1)
|
|
|
|
|
{
|
|
|
|
|
close(my_SVfileLock);
|
|
|
|
|
my_SVfileLock = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, "/tmp/DFHack/%d/SVlock",my_pid,name);
|
|
|
|
|
my_SVfileLock = open(name,O_WRONLY);
|
|
|
|
|
if(my_SVfileLock == -1)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(lockf( my_SVfileLock, F_TEST, 0 ) != -1)
|
|
|
|
|
{
|
|
|
|
|
close(my_SVfileLock);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// open the client lock, try to lock it
|
|
|
|
|
sprintf(name, "/tmp/DFHack/%d/CLlock",my_pid,name);
|
|
|
|
|
my_CLfileLock = open(name,O_WRONLY);
|
|
|
|
|
if(my_CLfileLock == -1)
|
|
|
|
|
{
|
|
|
|
|
close(my_SVfileLock);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(lockf(my_CLfileLock,F_TLOCK, 0) == -1)
|
|
|
|
|
{
|
|
|
|
|
// couldn't acquire lock
|
|
|
|
|
close(my_SVfileLock);
|
|
|
|
|
close(my_CLfileLock);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// ok, we have all the locks!
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SHMProcess::SHMProcess(vector <memory_info *> & known_versions)
|
|
|
|
|
SHMProcess::SHMProcess(uint32_t PID, vector< memory_info* >& known_versions)
|
|
|
|
|
: d(new Private())
|
|
|
|
|
{
|
|
|
|
|
char exe_link_name [256];
|
|
|
|
@ -138,7 +268,7 @@ SHMProcess::SHMProcess(vector <memory_info *> & known_versions)
|
|
|
|
|
/*
|
|
|
|
|
* Locate the segment.
|
|
|
|
|
*/
|
|
|
|
|
if ((d->my_shmid = shmget(SHM_KEY, SHM_SIZE, 0666)) < 0)
|
|
|
|
|
if ((d->my_shmid = shmget(SHM_KEY + PID, SHM_SIZE, 0666)) < 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -151,45 +281,48 @@ SHMProcess::SHMProcess(vector <memory_info *> & known_versions)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check if there are two processes connected to the segment
|
|
|
|
|
*/
|
|
|
|
|
shmid_ds descriptor;
|
|
|
|
|
shmctl(d->my_shmid, IPC_STAT, &descriptor);
|
|
|
|
|
if(descriptor.shm_nattch != 2)// badness
|
|
|
|
|
// set pid and gets lock for it
|
|
|
|
|
d->my_pid = PID;
|
|
|
|
|
if(!d->GetLocks())
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr,"dfhack: %d : invalid no. of processes connected\n", (int) descriptor.shm_nattch);
|
|
|
|
|
fprintf(stderr,"detach: %d",shmdt(d->my_shm));
|
|
|
|
|
fprintf(stderr,"Couldn't get locks for PID %d'\n", PID);
|
|
|
|
|
shmdt(d->my_shm);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Test bridge version, will also detect when we connect to something that doesn't respond
|
|
|
|
|
* Test bridge version, get PID, sync Yield
|
|
|
|
|
*/
|
|
|
|
|
bool bridgeOK;
|
|
|
|
|
if(!d->DF_TestBridgeVersion(bridgeOK))
|
|
|
|
|
if(!d->Aux_Core_Attach(bridgeOK,d->my_pid))
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr,"DF terminated during reading\n");
|
|
|
|
|
shmdt(d->my_shm);
|
|
|
|
|
// free locks
|
|
|
|
|
d->FreeLocks();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!bridgeOK)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr,"SHM bridge version mismatch\n");
|
|
|
|
|
shmdt(d->my_shm);
|
|
|
|
|
// free locks
|
|
|
|
|
d->FreeLocks();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* get the PID from DF
|
|
|
|
|
*/
|
|
|
|
|
if(d->DF_GetPID(d->my_pid))
|
|
|
|
|
{
|
|
|
|
|
// find its binary
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
perror("readlink");
|
|
|
|
|
shmdt(d->my_shm);
|
|
|
|
|
// free locks
|
|
|
|
|
d->FreeLocks();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make sure we have a null terminated string...
|
|
|
|
|
// see http://www.opengroup.org/onlinepubs/000095399/functions/readlink.html
|
|
|
|
|
target_name[target_result] = 0;
|
|
|
|
@ -197,11 +330,14 @@ SHMProcess::SHMProcess(vector <memory_info *> & known_versions)
|
|
|
|
|
// try to identify the DF version
|
|
|
|
|
d->validate(target_name, d->my_pid, known_versions);
|
|
|
|
|
d->my_window = new DFWindow(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gcc_barrier
|
|
|
|
|
// at this point, DF is stopped and waiting for commands. make it run again
|
|
|
|
|
((shm_cmd *)d->my_shm)->pingpong = DFPP_RUNNING;
|
|
|
|
|
D_SHMCMD = CORE_RUNNING;
|
|
|
|
|
shmdt(d->my_shm); // detach so we don't attach twice when attach() is called
|
|
|
|
|
|
|
|
|
|
// free locks
|
|
|
|
|
d->FreeLocks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SHMProcess::isSuspended()
|
|
|
|
@ -319,8 +455,8 @@ bool SHMProcess::suspend()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
((shm_cmd *)d->my_shm)->pingpong = DFPP_SUSPEND;
|
|
|
|
|
if(!d->waitWhile(DFPP_SUSPEND))
|
|
|
|
|
D_SHMCMD = CORE_SUSPEND;
|
|
|
|
|
if(!waitWhile(CORE_SUSPEND))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -338,14 +474,14 @@ bool SHMProcess::asyncSuspend()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if(((shm_cmd *)d->my_shm)->pingpong == DFPP_SUSPENDED)
|
|
|
|
|
if(D_SHMCMD == CORE_SUSPENDED)
|
|
|
|
|
{
|
|
|
|
|
d->suspended = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
((shm_cmd *)d->my_shm)->pingpong = DFPP_SUSPEND;
|
|
|
|
|
D_SHMCMD = CORE_SUSPEND;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -361,7 +497,7 @@ bool SHMProcess::resume()
|
|
|
|
|
return false;
|
|
|
|
|
if(!d->suspended)
|
|
|
|
|
return true;
|
|
|
|
|
((shm_cmd *)d->my_shm)->pingpong = DFPP_RUNNING;
|
|
|
|
|
D_SHMCMD = CORE_RUNNING;
|
|
|
|
|
d->suspended = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
@ -372,9 +508,16 @@ bool SHMProcess::attach()
|
|
|
|
|
int status;
|
|
|
|
|
if(g_pProcess != 0)
|
|
|
|
|
{
|
|
|
|
|
cerr << "there's already a different process attached" << endl;
|
|
|
|
|
// FIXME: throw exception here - programmer error
|
|
|
|
|
cerr << "client is already attached to a process!" << endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(!d->GetLocks())
|
|
|
|
|
{
|
|
|
|
|
cerr << "server is full or not really there!" << endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Attach the segment
|
|
|
|
|
*/
|
|
|
|
@ -389,10 +532,12 @@ bool SHMProcess::attach()
|
|
|
|
|
}
|
|
|
|
|
d->attached = false;
|
|
|
|
|
cerr << "unable to suspend" << endl;
|
|
|
|
|
// FIXME: detach sehment here
|
|
|
|
|
shmdt(d->my_shm);
|
|
|
|
|
d->FreeLocks();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
cerr << "unable to attach" << endl;
|
|
|
|
|
d->FreeLocks();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -413,9 +558,11 @@ bool SHMProcess::detach()
|
|
|
|
|
d->suspended = false;
|
|
|
|
|
d->my_shm = 0;
|
|
|
|
|
g_pProcess = 0;
|
|
|
|
|
d->FreeLocks();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// fail if we can't detach
|
|
|
|
|
// FIXME: throw exception here??
|
|
|
|
|
perror("failed to detach shared segment");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -425,11 +572,11 @@ void SHMProcess::read (uint32_t src_address, uint32_t size, uint8_t *target_buff
|
|
|
|
|
// normal read under 1MB
|
|
|
|
|
if(size <= SHM_BODY)
|
|
|
|
|
{
|
|
|
|
|
((shm_read *)d->my_shm)->address = src_address;
|
|
|
|
|
((shm_read *)d->my_shm)->length = size;
|
|
|
|
|
D_SHMHDR->address = src_address;
|
|
|
|
|
D_SHMHDR->length = size;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read *)d->my_shm)->pingpong = DFPP_READ;
|
|
|
|
|
d->waitWhile(DFPP_READ);
|
|
|
|
|
D_SHMCMD = CORE_DFPP_READ;
|
|
|
|
|
waitWhile(CORE_DFPP_READ);
|
|
|
|
|
memcpy (target_buffer, d->my_shm + SHM_HEADER,size);
|
|
|
|
|
}
|
|
|
|
|
// a big read, we pull data over the shm in iterations
|
|
|
|
@ -440,11 +587,11 @@ void SHMProcess::read (uint32_t src_address, uint32_t size, uint8_t *target_buff
|
|
|
|
|
while (size)
|
|
|
|
|
{
|
|
|
|
|
// read to_read bytes from src_cursor
|
|
|
|
|
((shm_read *)d->my_shm)->address = src_address;
|
|
|
|
|
((shm_read *)d->my_shm)->length = to_read;
|
|
|
|
|
D_SHMHDR->address = src_address;
|
|
|
|
|
D_SHMHDR->length = to_read;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read *)d->my_shm)->pingpong = DFPP_READ;
|
|
|
|
|
d->waitWhile(DFPP_READ);
|
|
|
|
|
D_SHMCMD = CORE_DFPP_READ;
|
|
|
|
|
waitWhile(CORE_DFPP_READ);
|
|
|
|
|
memcpy (target_buffer, d->my_shm + SHM_HEADER,size);
|
|
|
|
|
// decrease size by bytes read
|
|
|
|
|
size -= to_read;
|
|
|
|
@ -459,55 +606,55 @@ void SHMProcess::read (uint32_t src_address, uint32_t size, uint8_t *target_buff
|
|
|
|
|
|
|
|
|
|
uint8_t SHMProcess::readByte (const uint32_t offset)
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_BYTE;
|
|
|
|
|
d->waitWhile(DFPP_READ_BYTE);
|
|
|
|
|
return ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
D_SHMCMD = CORE_READ_BYTE;
|
|
|
|
|
waitWhile(CORE_READ_BYTE);
|
|
|
|
|
return D_SHMHDR->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SHMProcess::readByte (const uint32_t offset, uint8_t &val )
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_BYTE;
|
|
|
|
|
d->waitWhile(DFPP_READ_BYTE);
|
|
|
|
|
val = ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
D_SHMCMD = CORE_READ_BYTE;
|
|
|
|
|
waitWhile(CORE_READ_BYTE);
|
|
|
|
|
val = D_SHMHDR->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t SHMProcess::readWord (const uint32_t offset)
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_WORD;
|
|
|
|
|
d->waitWhile(DFPP_READ_WORD);
|
|
|
|
|
return ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
D_SHMCMD = CORE_READ_WORD;
|
|
|
|
|
waitWhile(CORE_READ_WORD);
|
|
|
|
|
return D_SHMHDR->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SHMProcess::readWord (const uint32_t offset, uint16_t &val)
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_WORD;
|
|
|
|
|
d->waitWhile(DFPP_READ_WORD);
|
|
|
|
|
val = ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
D_SHMCMD = CORE_READ_WORD;
|
|
|
|
|
waitWhile(CORE_READ_WORD);
|
|
|
|
|
val = D_SHMHDR->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t SHMProcess::readDWord (const uint32_t offset)
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_DWORD;
|
|
|
|
|
d->waitWhile(DFPP_READ_DWORD);
|
|
|
|
|
return ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
D_SHMCMD = CORE_READ_DWORD;
|
|
|
|
|
waitWhile(CORE_READ_DWORD);
|
|
|
|
|
return D_SHMHDR->value;
|
|
|
|
|
}
|
|
|
|
|
void SHMProcess::readDWord (const uint32_t offset, uint32_t &val)
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_DWORD;
|
|
|
|
|
d->waitWhile(DFPP_READ_DWORD);
|
|
|
|
|
val = ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
D_SHMCMD = CORE_READ_DWORD;
|
|
|
|
|
waitWhile(CORE_READ_DWORD);
|
|
|
|
|
val = D_SHMHDR->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -516,30 +663,30 @@ void SHMProcess::readDWord (const uint32_t offset, uint32_t &val)
|
|
|
|
|
|
|
|
|
|
void SHMProcess::writeDWord (uint32_t offset, uint32_t data)
|
|
|
|
|
{
|
|
|
|
|
((shm_write_small *)d->my_shm)->address = offset;
|
|
|
|
|
((shm_write_small *)d->my_shm)->value = data;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
D_SHMHDR->value = data;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_write_small *)d->my_shm)->pingpong = DFPP_WRITE_DWORD;
|
|
|
|
|
d->waitWhile(DFPP_WRITE_DWORD);
|
|
|
|
|
D_SHMCMD = CORE_WRITE_DWORD;
|
|
|
|
|
waitWhile(CORE_WRITE_DWORD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// using these is expensive.
|
|
|
|
|
void SHMProcess::writeWord (uint32_t offset, uint16_t data)
|
|
|
|
|
{
|
|
|
|
|
((shm_write_small *)d->my_shm)->address = offset;
|
|
|
|
|
((shm_write_small *)d->my_shm)->value = data;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
D_SHMHDR->value = data;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_write_small *)d->my_shm)->pingpong = DFPP_WRITE_WORD;
|
|
|
|
|
d->waitWhile(DFPP_WRITE_WORD);
|
|
|
|
|
D_SHMCMD = CORE_WRITE_WORD;
|
|
|
|
|
waitWhile(CORE_WRITE_WORD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SHMProcess::writeByte (uint32_t offset, uint8_t data)
|
|
|
|
|
{
|
|
|
|
|
((shm_write_small *)d->my_shm)->address = offset;
|
|
|
|
|
((shm_write_small *)d->my_shm)->value = data;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
D_SHMHDR->value = data;
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_write_small *)d->my_shm)->pingpong = DFPP_WRITE_BYTE;
|
|
|
|
|
d->waitWhile(DFPP_WRITE_BYTE);
|
|
|
|
|
D_SHMCMD = CORE_WRITE_BYTE;
|
|
|
|
|
waitWhile(CORE_WRITE_BYTE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SHMProcess::write (uint32_t dst_address, uint32_t size, uint8_t *source_buffer)
|
|
|
|
@ -547,12 +694,12 @@ void SHMProcess::write (uint32_t dst_address, uint32_t size, uint8_t *source_buf
|
|
|
|
|
// normal write under 1MB
|
|
|
|
|
if(size <= SHM_BODY)
|
|
|
|
|
{
|
|
|
|
|
((shm_write *)d->my_shm)->address = dst_address;
|
|
|
|
|
((shm_write *)d->my_shm)->length = size;
|
|
|
|
|
D_SHMHDR->address = dst_address;
|
|
|
|
|
D_SHMHDR->length = size;
|
|
|
|
|
memcpy(d->my_shm+SHM_HEADER,source_buffer, size);
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_write *)d->my_shm)->pingpong = DFPP_WRITE;
|
|
|
|
|
d->waitWhile(DFPP_WRITE);
|
|
|
|
|
D_SHMCMD = CORE_WRITE;
|
|
|
|
|
waitWhile(CORE_WRITE);
|
|
|
|
|
}
|
|
|
|
|
// a big write, we push this over the shm in iterations
|
|
|
|
|
else
|
|
|
|
@ -562,12 +709,12 @@ void SHMProcess::write (uint32_t dst_address, uint32_t size, uint8_t *source_buf
|
|
|
|
|
while (size)
|
|
|
|
|
{
|
|
|
|
|
// write to_write bytes to dst_cursor
|
|
|
|
|
((shm_write *)d->my_shm)->address = dst_address;
|
|
|
|
|
((shm_write *)d->my_shm)->length = to_write;
|
|
|
|
|
D_SHMHDR->address = dst_address;
|
|
|
|
|
D_SHMHDR->length = to_write;
|
|
|
|
|
memcpy(d->my_shm+SHM_HEADER,source_buffer, to_write);
|
|
|
|
|
gcc_barrier
|
|
|
|
|
((shm_write *)d->my_shm)->pingpong = DFPP_WRITE;
|
|
|
|
|
d->waitWhile(DFPP_WRITE);
|
|
|
|
|
D_SHMCMD = CORE_WRITE;
|
|
|
|
|
waitWhile(CORE_WRITE);
|
|
|
|
|
// decrease size by bytes written
|
|
|
|
|
size -= to_write;
|
|
|
|
|
// move the cursors
|
|
|
|
@ -615,21 +762,21 @@ DfVector SHMProcess::readVector (uint32_t offset, uint32_t item_size)
|
|
|
|
|
|
|
|
|
|
const std::string SHMProcess::readSTLString(uint32_t offset)
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
full_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_STL_STRING;
|
|
|
|
|
d->waitWhile(DFPP_READ_STL_STRING);
|
|
|
|
|
D_SHMCMD = CORE_READ_STL_STRING;
|
|
|
|
|
waitWhile(CORE_READ_STL_STRING);
|
|
|
|
|
//int length = ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
return(string( (char *)d->my_shm+SHM_HEADER));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t SHMProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
|
|
|
|
{
|
|
|
|
|
((shm_read_small *)d->my_shm)->address = offset;
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
|
full_barrier
|
|
|
|
|
((shm_read_small *)d->my_shm)->pingpong = DFPP_READ_STL_STRING;
|
|
|
|
|
d->waitWhile(DFPP_READ_STL_STRING);
|
|
|
|
|
size_t length = ((shm_retval *)d->my_shm)->value;
|
|
|
|
|
D_SHMCMD = CORE_READ_STL_STRING;
|
|
|
|
|
waitWhile(CORE_READ_STL_STRING);
|
|
|
|
|
size_t length = D_SHMHDR->value;
|
|
|
|
|
size_t fit = min(bufcapacity - 1, length);
|
|
|
|
|
strncpy(buffer,(char *)d->my_shm+SHM_HEADER,fit);
|
|
|
|
|
buffer[fit] = 0;
|
|
|
|
@ -638,11 +785,11 @@ size_t SHMProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapa
|
|
|
|
|
|
|
|
|
|
void SHMProcess::writeSTLString(const uint32_t address, const std::string writeString)
|
|
|
|
|
{
|
|
|
|
|
((shm_write_small *)d->my_shm)->address = address;
|
|
|
|
|
D_SHMHDR->address = address;
|
|
|
|
|
strncpy(d->my_shm+SHM_HEADER,writeString.c_str(),writeString.length()+1); // length + 1 for the null terminator
|
|
|
|
|
full_barrier
|
|
|
|
|
((shm_write_small *)d->my_shm)->pingpong = DFPP_WRITE_STL_STRING;
|
|
|
|
|
d->waitWhile(DFPP_WRITE_STL_STRING);
|
|
|
|
|
D_SHMCMD = CORE_WRITE_STL_STRING;
|
|
|
|
|
waitWhile(CORE_WRITE_STL_STRING);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string SHMProcess::readClassName (uint32_t vptr)
|
|
|
|
@ -654,3 +801,36 @@ string SHMProcess::readClassName (uint32_t vptr)
|
|
|
|
|
size_t end = raw.length();
|
|
|
|
|
return raw.substr(start,end-start - 2); // trim the 'st' from the end
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
// get module index by name and version. bool 1 = error
|
|
|
|
|
bool SHMProcess::getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT)
|
|
|
|
|
{
|
|
|
|
|
modulelookup * payload = (modulelookup *) (d->my_shm + SHM_HEADER);
|
|
|
|
|
payload->version = version;
|
|
|
|
|
|
|
|
|
|
strncpy(payload->name,name,255);
|
|
|
|
|
payload->name[255] = 0;
|
|
|
|
|
|
|
|
|
|
full_barrier
|
|
|
|
|
|
|
|
|
|
D_SHMCMD = CORE_ACQUIRE_MODULE;
|
|
|
|
|
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);
|
|
|
|
|
OUTPUT = D_SHMHDR->value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char * SHMProcess::getSHMStart (void)
|
|
|
|
|
{
|
|
|
|
|
return d->my_shm;
|
|
|
|
|
}
|