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.
|
|
|
|
*/
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFProcess.h"
|
|
|
|
#include "dfhack/DFMemInfo.h"
|
|
|
|
#include "dfhack/DFError.h"
|
2010-04-04 16:48:19 -06:00
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <time.h>
|
2010-04-02 09:27:35 -06:00
|
|
|
#include <shms.h>
|
|
|
|
#include <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:
|
2010-04-18 16:32:50 -06:00
|
|
|
Private(Process * self_)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
memdescriptor = NULL;
|
|
|
|
process_ID = 0;
|
|
|
|
shm_addr = 0;
|
|
|
|
//shm_addr_with_cl_idx = 0;
|
|
|
|
shm_ID = -1;
|
2009-12-22 14:19:39 -07:00
|
|
|
attached = false;
|
|
|
|
identified = false;
|
2010-03-02 21:43:38 -07:00
|
|
|
useYield = false;
|
2010-03-11 16:13:50 -07:00
|
|
|
server_lock = -1;
|
|
|
|
client_lock = -1;
|
|
|
|
suspend_lock = -1;
|
|
|
|
attachmentIdx = 0;
|
|
|
|
locked = false;
|
2010-04-18 16:32:50 -06:00
|
|
|
self = self_;
|
2009-12-22 14:19:39 -07:00
|
|
|
};
|
|
|
|
~Private(){};
|
2010-03-11 16:13:50 -07:00
|
|
|
memory_info * memdescriptor;
|
2010-04-18 16:32:50 -06:00
|
|
|
Process * self;
|
2010-03-11 16:13:50 -07:00
|
|
|
pid_t process_ID;
|
|
|
|
char *shm_addr;
|
|
|
|
int shm_ID;
|
2010-03-04 16:05:01 -07:00
|
|
|
Process* q;
|
2010-03-11 16:13:50 -07:00
|
|
|
int server_lock;
|
|
|
|
int client_lock;
|
|
|
|
int suspend_lock;
|
|
|
|
int attachmentIdx;
|
2010-03-13 09:44:36 -07:00
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool attached;
|
2010-03-13 09:44:36 -07:00
|
|
|
bool locked;
|
2009-12-22 14:19:39 -07:00
|
|
|
bool identified;
|
2010-03-02 21:43:38 -07:00
|
|
|
bool useYield;
|
2010-05-26 04:24:45 -06:00
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
bool validate(std::vector< memory_info* >& known_versions);
|
2010-05-26 04:24:45 -06:00
|
|
|
|
2010-03-07 21:15:11 -07:00
|
|
|
bool Aux_Core_Attach(bool & versionOK, pid_t & PID);
|
2010-03-11 16:13:50 -07:00
|
|
|
//bool waitWhile (uint32_t state);
|
|
|
|
bool SetAndWait (uint32_t state);
|
2010-03-09 13:25:17 -07:00
|
|
|
bool GetLocks();
|
|
|
|
bool AreLocksOk();
|
|
|
|
void FreeLocks();
|
2009-12-22 14:19:39 -07:00
|
|
|
};
|
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
#define SHMCMD ( (uint32_t *) shm_addr)[attachmentIdx]
|
|
|
|
#define D_SHMCMD ( (uint32_t *) (d->shm_addr))[d->attachmentIdx]
|
2010-03-02 21:43:38 -07:00
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
#define SHMHDR ((shm_core_hdr *)shm_addr)
|
|
|
|
#define D_SHMHDR ((shm_core_hdr *)(d->shm_addr))
|
2010-03-02 21:43:38 -07:00
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
#define SHMDATA(type) ((type *)(shm_addr + SHM_HEADER))
|
|
|
|
#define D_SHMDATA(type) ((type *)(d->shm_addr + SHM_HEADER))
|
2010-03-07 21:15:11 -07:00
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
bool SHMProcess::Private::SetAndWait (uint32_t state)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
uint32_t cnt = 0;
|
2010-03-12 10:29:11 -07:00
|
|
|
if(!attached) return false;
|
2010-03-11 16:13:50 -07:00
|
|
|
SHMCMD = state;
|
2010-03-02 21:43:38 -07:00
|
|
|
while (SHMCMD == state)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-20 10:30:13 -06:00
|
|
|
// check if the other process is still there
|
|
|
|
if(cnt == 10000)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-09 13:25:17 -07:00
|
|
|
if(!AreLocksOk())
|
|
|
|
{
|
|
|
|
//detach the shared memory
|
2010-03-11 16:13:50 -07:00
|
|
|
shmdt(shm_addr);
|
2010-03-13 09:44:36 -07:00
|
|
|
FreeLocks();
|
|
|
|
attached = locked = identified = false;
|
2010-03-09 13:25:17 -07:00
|
|
|
// we aren't the current process anymore
|
|
|
|
throw Error::SHMServerDisappeared();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cnt = 0;
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2010-03-03 20:40:06 -07:00
|
|
|
if(useYield)
|
|
|
|
{
|
|
|
|
SCHED_YIELD
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
cnt++;
|
|
|
|
}
|
2010-03-11 16:13:50 -07:00
|
|
|
// server returned a generic error
|
2010-03-07 21:15:11 -07:00
|
|
|
if(SHMCMD == CORE_ERROR)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
/*
|
2010-03-20 10:30:13 -06:00
|
|
|
Yeah. with no way to synchronize things (locks are slow, the OS doesn't give us
|
|
|
|
enough control over scheduling)
|
2010-03-04 16:05:01 -07:00
|
|
|
we end up with this silly thing
|
|
|
|
*/
|
2010-03-11 16:13:50 -07:00
|
|
|
bool SHMProcess::SetAndWait (uint32_t state)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
return d->SetAndWait(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-11 16:13:50 -07:00
|
|
|
// test if we have client and server locks and the server is present
|
2010-03-09 13:25:17 -07:00
|
|
|
bool SHMProcess::Private::AreLocksOk()
|
|
|
|
{
|
|
|
|
// both locks are inited (we hold our lock)
|
2010-03-11 16:13:50 -07:00
|
|
|
if(client_lock != -1 && server_lock != -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
if(lockf(server_lock,F_TEST,0) == -1) // and server holds its lock
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
|
|
|
return true; // OK, locks are good
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// locks are bad
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SHMProcess::Private::FreeLocks()
|
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
attachmentIdx = -1;
|
|
|
|
if(client_lock != -1)
|
|
|
|
{
|
|
|
|
lockf(client_lock,F_ULOCK,0);
|
|
|
|
close(client_lock);
|
|
|
|
client_lock = -1;
|
|
|
|
}
|
|
|
|
if(server_lock != -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
close(server_lock);
|
|
|
|
server_lock = -1;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
2010-03-11 16:13:50 -07:00
|
|
|
if(suspend_lock != -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
close(suspend_lock);
|
|
|
|
locked = false;
|
|
|
|
suspend_lock = -1;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SHMProcess::Private::GetLocks()
|
|
|
|
{
|
|
|
|
char name[256];
|
|
|
|
// try to acquire locks
|
|
|
|
// look at the server lock, if it's locked, the server is present
|
2010-03-11 16:13:50 -07:00
|
|
|
sprintf(name, "/tmp/DFHack/%d/SVlock",process_ID);
|
|
|
|
server_lock = open(name,O_WRONLY);
|
|
|
|
if(server_lock == -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
// cerr << "can't open sv lock" << endl;
|
2010-03-09 13:25:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
if(lockf( server_lock, F_TEST, 0 ) != -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
cerr << "sv lock not locked" << endl;
|
|
|
|
close(server_lock);
|
|
|
|
server_lock = -1;
|
2010-03-09 13:25:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
for(int i = 0; i < SHM_MAX_CLIENTS; i++)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
// open the client suspend locked
|
|
|
|
sprintf(name, "/tmp/DFHack/%d/CLSlock%d",process_ID,i);
|
|
|
|
suspend_lock = open(name,O_WRONLY);
|
|
|
|
if(suspend_lock == -1)
|
|
|
|
{
|
|
|
|
cerr << "can't open cl S-lock " << i << endl;
|
|
|
|
// couldn't open lock
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the client lock, try to lock it
|
|
|
|
sprintf(name, "/tmp/DFHack/%d/CLlock%d",process_ID,i);
|
|
|
|
client_lock = open(name,O_WRONLY);
|
|
|
|
if(client_lock == -1)
|
|
|
|
{
|
|
|
|
cerr << "can't open cl lock " << i << endl;
|
|
|
|
close(suspend_lock);
|
|
|
|
locked = false;
|
|
|
|
suspend_lock = -1;
|
|
|
|
// couldn't open lock
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(lockf(client_lock,F_TLOCK, 0) == -1)
|
|
|
|
{
|
|
|
|
// couldn't acquire lock
|
|
|
|
cerr << "can't acquire cl lock " << i << endl;
|
|
|
|
close(suspend_lock);
|
|
|
|
locked = false;
|
|
|
|
suspend_lock = -1;
|
|
|
|
close(client_lock);
|
|
|
|
client_lock = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// ok, we have all the locks we need!
|
|
|
|
attachmentIdx = i;
|
|
|
|
return true;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
2010-03-11 16:13:50 -07:00
|
|
|
close(server_lock);
|
|
|
|
server_lock = -1;
|
|
|
|
cerr << "can't get any client locks" << endl;
|
|
|
|
return false;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
SHMProcess::SHMProcess(uint32_t PID, vector< memory_info* >& known_versions)
|
2010-04-18 16:32:50 -06:00
|
|
|
: d(new Private(this))
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
d->process_ID = PID;
|
2010-04-18 16:32:50 -06:00
|
|
|
d->memdescriptor = 0;
|
2010-03-11 16:13:50 -07:00
|
|
|
if(!attach())
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
// couldn't attach to process
|
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-11 16:13:50 -07:00
|
|
|
if(!d->Aux_Core_Attach(bridgeOK,d->process_ID))
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
detach();
|
|
|
|
throw Error::SHMAttachFailure();
|
2009-12-25 09:55:10 -07:00
|
|
|
}
|
|
|
|
if(!bridgeOK)
|
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
detach();
|
2010-03-13 09:44:36 -07:00
|
|
|
throw Error::SHMVersionMismatch();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
// try to identify the DF version (md5 the binary, compare with known versions)
|
2010-03-13 09:44:36 -07:00
|
|
|
d->validate(known_versions);
|
2010-03-11 16:13:50 -07:00
|
|
|
// detach
|
|
|
|
detach();
|
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
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
return d->locked;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
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-13 09:44:36 -07:00
|
|
|
bool SHMProcess::Private::validate(vector <memory_info *> & known_versions)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
char exe_link_name [256];
|
|
|
|
char target_name[1024];
|
|
|
|
int target_result;
|
|
|
|
// find the binary
|
|
|
|
sprintf(exe_link_name,"/proc/%d/exe", process_ID);
|
|
|
|
target_result = readlink(exe_link_name, target_name, sizeof(target_name)-1);
|
|
|
|
if (target_result == -1)
|
|
|
|
{
|
|
|
|
perror("readlink");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// make sure we have a null terminated string...
|
|
|
|
// see http://www.opengroup.org/onlinepubs/000095399/functions/readlink.html
|
|
|
|
target_name[target_result] = 0;
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
md5wrapper md5;
|
|
|
|
// get hash of the running DF process
|
2010-03-13 09:44:36 -07:00
|
|
|
string hash = md5.getHashFromFile(target_name);
|
2010-02-22 15:34:20 -07:00
|
|
|
vector<memory_info *>::iterator it;
|
2010-03-11 16:13:50 -07:00
|
|
|
// cerr << exe_file << " " << hash << endl;
|
2009-12-22 14:19:39 -07:00
|
|
|
// 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?
|
|
|
|
{
|
2010-04-18 16:32:50 -06:00
|
|
|
memory_info *m = new memory_info(**it);
|
2010-03-11 16:13:50 -07:00
|
|
|
memdescriptor = m;
|
2010-04-18 16:32:50 -06:00
|
|
|
m->setParentProcess(dynamic_cast<Process *>( self ));
|
2010-02-27 22:21:50 -07:00
|
|
|
identified = true;
|
2010-03-11 16:13:50 -07:00
|
|
|
// cerr << "identified " << m->getVersion() << endl;
|
2010-02-27 22:21:50 -07:00
|
|
|
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();
|
|
|
|
}
|
2010-04-18 16:32:50 -06:00
|
|
|
if(d->memdescriptor)
|
|
|
|
delete d->memdescriptor;
|
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
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
return d->memdescriptor;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
int SHMProcess::getPID()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
return d->process_ID;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
// there is only one we care about.
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::getThreadIDs(vector<uint32_t> & threads )
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->attached)
|
|
|
|
{
|
|
|
|
threads.clear();
|
|
|
|
threads.push_back(d->process_ID);
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
sprintf(buffer, "/proc/%lu/maps", d->process_ID);
|
2009-12-22 14:19:39 -07:00
|
|
|
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
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->locked)
|
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-11 16:13:50 -07:00
|
|
|
|
2010-03-12 10:29:11 -07:00
|
|
|
// FIXME: this should be controlled on the server side
|
|
|
|
// FIXME: IF server got CORE_RUN in this frame, interpret CORE_SUSPEND as CORE_STEP
|
2010-03-11 16:13:50 -07:00
|
|
|
// did we just resume a moment ago?
|
|
|
|
if(D_SHMCMD == CORE_RUN)
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
//fprintf(stderr,"%d invokes step\n",d->attachmentIdx);
|
2010-03-18 12:55:01 -06:00
|
|
|
/*
|
2010-03-12 04:14:20 -07:00
|
|
|
// wait for the next window
|
2010-03-12 10:29:11 -07:00
|
|
|
if(!d->SetAndWait(CORE_STEP))
|
|
|
|
{
|
|
|
|
throw Error::SHMLockingError("if(!d->SetAndWait(CORE_STEP))");
|
|
|
|
}
|
2010-03-18 12:55:01 -06:00
|
|
|
*/
|
|
|
|
D_SHMCMD = CORE_STEP;
|
2009-12-25 09:55:10 -07:00
|
|
|
}
|
2010-03-11 16:13:50 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//fprintf(stderr,"%d invokes suspend\n",d->attachmentIdx);
|
2010-03-12 04:14:20 -07:00
|
|
|
// lock now
|
2010-03-18 12:55:01 -06:00
|
|
|
/*
|
2010-03-12 10:29:11 -07:00
|
|
|
if(!d->SetAndWait(CORE_SUSPEND))
|
|
|
|
{
|
|
|
|
throw Error::SHMLockingError("if(!d->SetAndWait(CORE_SUSPEND))");
|
|
|
|
}
|
2010-03-18 12:55:01 -06:00
|
|
|
*/
|
|
|
|
D_SHMCMD = CORE_SUSPEND;
|
2010-03-11 16:13:50 -07:00
|
|
|
}
|
|
|
|
//fprintf(stderr,"waiting for lock\n");
|
|
|
|
// we wait for the server to give up our suspend lock (held by default)
|
|
|
|
if(lockf(d->suspend_lock,F_LOCK,0) == 0)
|
|
|
|
{
|
|
|
|
d->locked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-12 10:29:11 -07:00
|
|
|
// FIXME: needs a good think-through
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::asyncSuspend()
|
2010-01-01 19:25:21 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->locked)
|
2010-01-01 19:25:21 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
uint32_t cmd = D_SHMCMD;
|
|
|
|
if(cmd == CORE_SUSPENDED)
|
2010-01-01 19:25:21 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
// we have to hold the lock to be really suspended
|
|
|
|
if(lockf(d->suspend_lock,F_LOCK,0) == 0)
|
|
|
|
{
|
|
|
|
d->locked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-01-01 19:25:21 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
// did we just resume a moment ago?
|
2010-03-13 09:44:36 -07:00
|
|
|
if(cmd == CORE_STEP)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(cmd == CORE_RUN)
|
2010-03-11 16:13:50 -07:00
|
|
|
{
|
|
|
|
D_SHMCMD = CORE_STEP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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-11 16:13:50 -07:00
|
|
|
// FIXME: wait for the server to advance a step!
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::resume()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
return false;
|
2010-03-13 09:44:36 -07:00
|
|
|
if(!d->locked)
|
2009-12-22 14:19:39 -07:00
|
|
|
return true;
|
2010-03-11 16:13:50 -07:00
|
|
|
// unlock the suspend lock
|
|
|
|
if(lockf(d->suspend_lock,F_ULOCK,0) == 0)
|
|
|
|
{
|
|
|
|
d->locked = false;
|
2010-03-12 10:29:11 -07:00
|
|
|
if(d->SetAndWait(CORE_RUN)) // we have to make sure the server responds!
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw Error::SHMLockingError("if(d->SetAndWait(CORE_RUN))");
|
2010-03-11 16:13:50 -07:00
|
|
|
}
|
2010-03-12 10:29:11 -07:00
|
|
|
throw Error::SHMLockingError("if(lockf(d->suspend_lock,F_ULOCK,0) == 0)");
|
2010-03-11 16:13:50 -07:00
|
|
|
return false;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::attach()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-04-18 13:30:02 -06:00
|
|
|
if(d->attached)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-04-18 13:30:02 -06:00
|
|
|
if(!d->locked)
|
|
|
|
return suspend();
|
|
|
|
return true;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2010-03-09 13:25:17 -07:00
|
|
|
if(!d->GetLocks())
|
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
//cerr << "server is full or not really there!" << endl;
|
2010-03-09 13:25:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
/*
|
|
|
|
* Locate the segment.
|
|
|
|
*/
|
|
|
|
if ((d->shm_ID = shmget(SHM_KEY + d->process_ID, SHM_SIZE, 0666)) < 0)
|
|
|
|
{
|
|
|
|
d->FreeLocks();
|
|
|
|
cerr << "can't find segment" << endl; // FIXME: throw
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-26 20:51:54 -07:00
|
|
|
/*
|
|
|
|
* Attach the segment
|
|
|
|
*/
|
2010-03-12 04:14:20 -07:00
|
|
|
if ((d->shm_addr = (char *) shmat(d->shm_ID, NULL, 0)) == (char *) -1)
|
|
|
|
{
|
|
|
|
d->FreeLocks();
|
2010-03-13 09:44:36 -07:00
|
|
|
cerr << "can't attach segment" << endl; // FIXME: throw
|
2010-03-12 04:14:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
d->attached = true;
|
|
|
|
if(!suspend())
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
shmdt(d->shm_addr);
|
2010-03-09 13:25:17 -07:00
|
|
|
d->FreeLocks();
|
2010-03-12 04:14:20 -07:00
|
|
|
cerr << "unable to suspend" << endl;
|
2009-12-26 20:51:54 -07:00
|
|
|
return false;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2010-03-12 04:14:20 -07:00
|
|
|
return true;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->locked)
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
2010-01-01 15:19:09 -07:00
|
|
|
// detach segment
|
2010-03-11 16:13:50 -07:00
|
|
|
if(shmdt(d->shm_addr) != -1)
|
2010-01-01 15:19:09 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
d->FreeLocks();
|
|
|
|
d->locked = false;
|
2010-01-01 15:19:09 -07:00
|
|
|
d->attached = false;
|
2010-03-11 16:13:50 -07:00
|
|
|
d->shm_addr = 0;
|
2010-01-01 15:19:09 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fail if we can't detach
|
2010-03-09 13:25:17 -07:00
|
|
|
// FIXME: throw exception here??
|
2010-01-01 15:19:09 -07:00
|
|
|
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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(CORE_READ);
|
|
|
|
memcpy (target_buffer, D_SHMDATA(void),size);
|
2010-01-01 19:25:21 -07:00
|
|
|
}
|
|
|
|
// a big read, we pull data over the shm in iterations
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// first read equals the size of the SHM window
|
2010-05-26 09:54:30 -06:00
|
|
|
uint32_t to_read = SHM_BODY - 1024;
|
2010-01-01 19:25:21 -07:00
|
|
|
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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(CORE_READ);
|
|
|
|
memcpy (target_buffer, D_SHMDATA(void) ,size);
|
2010-01-01 19:25:21 -07:00
|
|
|
// decrease size by bytes read
|
|
|
|
size -= to_read;
|
|
|
|
// move the cursors
|
|
|
|
src_address += to_read;
|
|
|
|
target_buffer += to_read;
|
|
|
|
// check how much to write in the next iteration
|
|
|
|
to_read = min(size, (uint32_t) SHM_BODY);
|
|
|
|
}
|
|
|
|
}
|
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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(CORE_READ_DWORD);
|
2010-03-02 21:43:38 -07:00
|
|
|
val = D_SHMHDR->value;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 16:12:22 -06:00
|
|
|
uint64_t SHMProcess::readQuad (const uint32_t offset)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
gcc_barrier
|
|
|
|
d->SetAndWait(CORE_READ_QUAD);
|
|
|
|
return D_SHMHDR->Qvalue;
|
|
|
|
}
|
|
|
|
void SHMProcess::readQuad (const uint32_t offset, uint64_t &val)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
gcc_barrier
|
|
|
|
d->SetAndWait(CORE_READ_QUAD);
|
|
|
|
val = D_SHMHDR->Qvalue;
|
|
|
|
}
|
|
|
|
|
2010-04-20 10:13:00 -06:00
|
|
|
float SHMProcess::readFloat (const uint32_t offset)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
gcc_barrier
|
|
|
|
d->SetAndWait(CORE_READ_DWORD);
|
|
|
|
return D_SHMHDR->value;
|
|
|
|
}
|
|
|
|
void SHMProcess::readFloat (const uint32_t offset, float &val)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
gcc_barrier
|
|
|
|
d->SetAndWait(CORE_READ_DWORD);
|
|
|
|
val = D_SHMHDR->value;
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
/*
|
|
|
|
* WRITING
|
|
|
|
*/
|
|
|
|
|
2010-04-26 16:12:22 -06:00
|
|
|
void SHMProcess::writeQuad (const uint32_t offset, const uint64_t data)
|
|
|
|
{
|
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
|
|
|
|
|
|
|
D_SHMHDR->address = offset;
|
|
|
|
D_SHMHDR->Qvalue = data;
|
|
|
|
gcc_barrier
|
|
|
|
d->SetAndWait(CORE_WRITE_QUAD);
|
|
|
|
}
|
|
|
|
|
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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-03-11 16:13:50 -07:00
|
|
|
memcpy(D_SHMDATA(void),source_buffer, size);
|
2010-01-01 19:25:21 -07:00
|
|
|
gcc_barrier
|
2010-03-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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-03-11 16:13:50 -07:00
|
|
|
memcpy(D_SHMDATA(void),source_buffer, to_write);
|
2010-01-01 19:25:21 -07:00
|
|
|
gcc_barrier
|
2010-03-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -07:00
|
|
|
|
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
|
|
|
const std::string SHMProcess::readSTLString(uint32_t offset)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(CORE_READ_STL_STRING);
|
|
|
|
return(string( D_SHMDATA(char) ));
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
size_t SHMProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -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-11 16:13:50 -07:00
|
|
|
d->SetAndWait(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);
|
2010-03-11 16:13:50 -07:00
|
|
|
strncpy(buffer,D_SHMDATA(char),fit);
|
2010-02-15 16:04:15 -07:00
|
|
|
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-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -07:00
|
|
|
|
2010-03-02 21:43:38 -07:00
|
|
|
D_SHMHDR->address = address;
|
2010-03-11 16:13:50 -07:00
|
|
|
strncpy(D_SHMDATA(char),writeString.c_str(),writeString.length()+1); // length + 1 for the null terminator
|
2010-02-15 16:04:15 -07:00
|
|
|
full_barrier
|
2010-03-11 16:13:50 -07:00
|
|
|
d->SetAndWait(CORE_WRITE_STL_STRING);
|
2010-02-18 18:48:03 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
string SHMProcess::readClassName (uint32_t vptr)
|
2010-02-18 18:48:03 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -07:00
|
|
|
|
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();
|
2010-04-09 06:44:00 -06:00
|
|
|
return raw.substr(start,end-start);
|
2010-03-04 16:05:01 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
// get module index by name and version. bool 0 = error
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied();
|
2010-03-11 16:13:50 -07:00
|
|
|
|
|
|
|
modulelookup * payload = D_SHMDATA(modulelookup);
|
2010-03-04 16:05:01 -07:00
|
|
|
payload->version = version;
|
2010-03-07 21:15:11 -07:00
|
|
|
|
|
|
|
strncpy(payload->name,name,255);
|
|
|
|
payload->name[255] = 0;
|
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
if(!SetAndWait(CORE_ACQUIRE_MODULE))
|
2010-03-07 21:15:11 -07:00
|
|
|
{
|
|
|
|
return false; // FIXME: throw a fatal exception instead
|
|
|
|
}
|
|
|
|
if(D_SHMHDR->error)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//fprintf(stderr,"%s v%d : %d\n", name, version, D_SHMHDR->value);
|
2010-03-04 16:05:01 -07:00
|
|
|
OUTPUT = D_SHMHDR->value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
char * SHMProcess::getSHMStart (void)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
if(!d->locked) return 0; //THROW HERE!
|
|
|
|
|
|
|
|
return /*d->shm_addr_with_cl_idx*/ d->shm_addr;
|
2010-03-13 09:44:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SHMProcess::Private::Aux_Core_Attach(bool & versionOK, pid_t & PID)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!locked) throw Error::MemoryAccessDenied();
|
2010-03-13 09:44:36 -07:00
|
|
|
|
|
|
|
SHMDATA(coreattach)->cl_affinity = OS_getAffinity();
|
|
|
|
if(!SetAndWait(CORE_ATTACH)) return false;
|
|
|
|
/*
|
|
|
|
cerr <<"CORE_VERSION" << CORE_VERSION << endl;
|
|
|
|
cerr <<"server CORE_VERSION" << SHMDATA(coreattach)->sv_version << endl;
|
|
|
|
*/
|
|
|
|
versionOK =( SHMDATA(coreattach)->sv_version == CORE_VERSION );
|
|
|
|
PID = SHMDATA(coreattach)->sv_PID;
|
|
|
|
useYield = SHMDATA(coreattach)->sv_useYield;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if(useYield) cerr << "Using Yield!" << endl;
|
|
|
|
#endif
|
|
|
|
return true;
|
2010-04-20 10:13:00 -06:00
|
|
|
}
|