2010-02-15 16:04:15 -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"
|
2011-04-10 02:19:15 -06:00
|
|
|
#include "PlatformInternal.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
using namespace std;
|
|
|
|
|
2011-02-23 03:26:33 -07:00
|
|
|
#include "LinuxProcess.h"
|
2011-02-23 04:08:30 -07:00
|
|
|
#include "ProcessFactory.h"
|
2011-02-23 13:55:07 -07:00
|
|
|
#include "MicrosoftSTL.h"
|
2010-08-20 06:10:05 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFError.h"
|
2011-04-10 02:19:15 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ptrace.h>
|
2011-04-10 02:19:15 -06:00
|
|
|
|
|
|
|
#include <md5wrapper.h>
|
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
|
2011-02-23 04:08:30 -07:00
|
|
|
namespace {
|
|
|
|
class WineProcess : public LinuxProcessBase
|
|
|
|
{
|
|
|
|
private:
|
2011-03-24 23:40:33 -06:00
|
|
|
uint8_t vector_start;
|
2011-02-23 13:55:07 -07:00
|
|
|
MicrosoftSTL stl;
|
2011-02-23 04:08:30 -07:00
|
|
|
public:
|
2011-02-24 03:43:33 -07:00
|
|
|
WineProcess(uint32_t pid, VersionInfoFactory * factory);
|
2011-03-02 14:23:24 -07:00
|
|
|
~WineProcess()
|
|
|
|
{
|
|
|
|
if(attached)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool attach();
|
|
|
|
bool detach();
|
|
|
|
|
|
|
|
bool suspend();
|
|
|
|
bool asyncSuspend();
|
|
|
|
bool resume();
|
|
|
|
bool forceresume();
|
2011-02-23 04:08:30 -07:00
|
|
|
|
2011-03-24 23:40:33 -06:00
|
|
|
void readSTLVector(const uint32_t address, t_vecTriplet & triplet);
|
2011-02-23 04:08:30 -07:00
|
|
|
const std::string readSTLString (uint32_t offset);
|
|
|
|
size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity);
|
|
|
|
void writeSTLString(const uint32_t address, const std::string writeString){};
|
|
|
|
// get class name of an object with rtti/type info
|
|
|
|
std::string readClassName(uint32_t vptr);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:43:33 -07:00
|
|
|
Process* DFHack::createWineProcess(uint32_t pid, VersionInfoFactory * factory)
|
2011-02-23 04:08:30 -07:00
|
|
|
{
|
2011-02-24 03:43:33 -07:00
|
|
|
return new WineProcess(pid, factory);
|
2011-02-23 04:08:30 -07:00
|
|
|
}
|
|
|
|
|
2011-02-24 03:43:33 -07:00
|
|
|
WineProcess::WineProcess(uint32_t pid, VersionInfoFactory * factory) : LinuxProcessBase(pid)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
|
|
|
char dir_name [256];
|
|
|
|
char exe_link_name [256];
|
|
|
|
char mem_name [256];
|
|
|
|
char cwd_name [256];
|
|
|
|
char cmdline_name [256];
|
|
|
|
char target_name[1024];
|
|
|
|
int target_result;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2011-02-23 03:49:33 -07:00
|
|
|
identified = false;
|
|
|
|
my_descriptor = 0;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
sprintf(dir_name,"/proc/%d/", pid);
|
|
|
|
sprintf(exe_link_name,"/proc/%d/exe", pid);
|
|
|
|
sprintf(mem_name,"/proc/%d/mem", pid);
|
2011-02-24 03:43:33 -07:00
|
|
|
memFile = mem_name;
|
2010-02-15 16:04:15 -07:00
|
|
|
sprintf(cwd_name,"/proc/%d/cwd", pid);
|
|
|
|
sprintf(cmdline_name,"/proc/%d/cmdline", pid);
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// resolve /proc/PID/exe link
|
|
|
|
target_result = readlink(exe_link_name, target_name, sizeof(target_name)-1);
|
|
|
|
if (target_result == -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// make sure we have a null terminated string...
|
|
|
|
target_name[target_result] = 0;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// FIXME: this fails when the wine process isn't started from the 'current working directory'. strip path data from cmdline
|
|
|
|
// is this windows version of Df running in wine?
|
|
|
|
if(strstr(target_name, "wine-preloader")!= NULL)
|
|
|
|
{
|
|
|
|
// get working directory
|
|
|
|
target_result = readlink(cwd_name, target_name, sizeof(target_name)-1);
|
|
|
|
target_name[target_result] = 0;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// got path to executable, do the same for its name
|
|
|
|
ifstream ifs ( cmdline_name , ifstream::in );
|
|
|
|
string cmdline;
|
|
|
|
getline(ifs,cmdline);
|
|
|
|
if (cmdline.find("dwarfort-w.exe") != string::npos || cmdline.find("dwarfort.exe") != string::npos || cmdline.find("Dwarf Fortress.exe") != string::npos)
|
|
|
|
{
|
|
|
|
char exe_link[1024];
|
|
|
|
// put executable name and path together
|
|
|
|
sprintf(exe_link,"%s/%s",target_name,cmdline.c_str());
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2011-02-24 03:43:33 -07:00
|
|
|
md5wrapper md5;
|
|
|
|
// get hash of the running DF process
|
|
|
|
string hash = md5.getHashFromFile(exe_link);
|
|
|
|
// create linux process, add it to the vector
|
|
|
|
VersionInfo * vinfo = factory->getVersionInfoByMD5(hash);
|
|
|
|
if(vinfo)
|
2010-04-26 16:12:22 -06:00
|
|
|
{
|
2011-02-24 03:43:33 -07:00
|
|
|
my_descriptor = new VersionInfo(*vinfo);
|
|
|
|
my_descriptor->setParentProcess(this);
|
2011-03-24 23:40:33 -06:00
|
|
|
vector_start = my_descriptor->getGroup("vector")->getOffset("start");
|
2011-02-24 03:43:33 -07:00
|
|
|
stl.init(this);
|
|
|
|
identified = true;
|
2010-04-26 16:12:22 -06:00
|
|
|
}
|
2011-02-24 03:43:33 -07:00
|
|
|
return;
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-24 23:40:33 -06:00
|
|
|
void WineProcess::readSTLVector(const uint32_t address, t_vecTriplet & triplet)
|
|
|
|
{
|
|
|
|
read(address + vector_start, sizeof(triplet), (uint8_t *) &triplet);
|
|
|
|
}
|
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
size_t WineProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
|
|
|
{
|
2011-02-23 13:55:07 -07:00
|
|
|
return stl.readSTLString(offset, buffer, bufcapacity);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const string WineProcess::readSTLString (uint32_t offset)
|
|
|
|
{
|
2011-02-23 13:55:07 -07:00
|
|
|
return stl.readSTLString(offset);
|
2010-02-18 18:48:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
string WineProcess::readClassName (uint32_t vptr)
|
|
|
|
{
|
2011-02-24 03:43:33 -07:00
|
|
|
return stl.readClassName(vptr);
|
2010-09-16 19:21:41 -06:00
|
|
|
}
|
2011-03-02 14:23:24 -07:00
|
|
|
|
|
|
|
bool WineProcess::asyncSuspend()
|
|
|
|
{
|
|
|
|
return suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WineProcess::suspend()
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
if(suspended)
|
|
|
|
return true;
|
|
|
|
// can we attach?
|
|
|
|
if (ptrace(PTRACE_ATTACH , my_pid, NULL, NULL) == -1)
|
|
|
|
{
|
|
|
|
// no, we got an error
|
|
|
|
perror("ptrace attach error");
|
|
|
|
cerr << "attach failed on pid " << my_pid << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
// we wait on the pid
|
|
|
|
pid_t w = waitpid(my_pid, &status, 0);
|
|
|
|
if (w == -1)
|
|
|
|
{
|
|
|
|
// child died
|
|
|
|
perror("wait inside attach()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// stopped -> let's continue
|
|
|
|
if (WIFSTOPPED(status))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int proc_pid_mem = open(memFile.c_str(),O_RDONLY);
|
|
|
|
if(proc_pid_mem == -1)
|
|
|
|
{
|
|
|
|
ptrace(PTRACE_DETACH, my_pid, NULL, NULL);
|
|
|
|
cerr << memFile << endl;
|
|
|
|
cerr << "couldn't open /proc/" << my_pid << "/mem" << endl;
|
|
|
|
perror("open(memFile.c_str(),O_RDONLY)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attached = suspended = true;
|
|
|
|
memFileHandle = proc_pid_mem;
|
|
|
|
return true; // we are attached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WineProcess::forceresume()
|
|
|
|
{
|
|
|
|
return resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WineProcess::resume()
|
|
|
|
{
|
|
|
|
if(!suspended)
|
|
|
|
return true;
|
|
|
|
int result = 0;
|
|
|
|
// close /proc/PID/mem
|
|
|
|
result = close(memFileHandle);
|
|
|
|
if(result == -1)
|
|
|
|
{
|
|
|
|
cerr << "couldn't close /proc/"<< my_pid <<"/mem" << endl;
|
|
|
|
perror("mem file close");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// detach
|
|
|
|
result = ptrace(PTRACE_DETACH, my_pid, NULL, NULL);
|
|
|
|
if(result == -1)
|
|
|
|
{
|
|
|
|
cerr << "couldn't detach from process pid" << my_pid << endl;
|
|
|
|
perror("ptrace detach");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attached = suspended = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool WineProcess::attach()
|
|
|
|
{
|
|
|
|
if(suspended) return true;
|
|
|
|
return suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WineProcess::detach()
|
|
|
|
{
|
|
|
|
if(!suspended) return true;
|
|
|
|
return resume();
|
|
|
|
}
|