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-02-23 03:26:33 -07:00
|
|
|
#include "LinuxProcess.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"
|
2010-02-15 16:04:15 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ptrace.h>
|
2010-04-26 10:12:00 -06:00
|
|
|
#include <stdio.h>
|
2010-02-15 16:04:15 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-02-23 03:26:33 -07:00
|
|
|
WineProcess::WineProcess(uint32_t pid, vector <VersionInfo *> & known_versions) : 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);
|
|
|
|
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
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// create wine process, add it to the vector
|
2011-02-23 03:49:33 -07:00
|
|
|
identified = validate(exe_link,pid,mem_name,known_versions);
|
2010-02-15 16:04:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-23 03:26:33 -07:00
|
|
|
bool WineProcess::validate(char * exe_file,uint32_t pid, char * memFile, vector <VersionInfo *> & known_versions)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
|
|
|
md5wrapper md5;
|
|
|
|
// get hash of the running DF process
|
|
|
|
string hash = md5.getHashFromFile(exe_file);
|
2010-08-20 06:10:05 -06:00
|
|
|
vector<VersionInfo *>::iterator it;
|
|
|
|
|
2010-02-15 16:04:15 -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
|
|
|
|
{
|
2011-02-23 03:26:33 -07:00
|
|
|
if (hash == (*it)->getMD5()) // are the md5 hashes the same?
|
2010-04-26 16:12:22 -06:00
|
|
|
{
|
2011-02-23 03:26:33 -07:00
|
|
|
if (OS_WINDOWS == (*it)->getOS())
|
|
|
|
{
|
|
|
|
// keep track of created memory_info object so we can destroy it later
|
2011-02-23 03:49:33 -07:00
|
|
|
my_descriptor = new VersionInfo(**it);
|
|
|
|
my_descriptor->setParentProcess(this);
|
2011-02-23 03:26:33 -07:00
|
|
|
// tell Process about the /proc/PID/mem file
|
2011-02-23 03:49:33 -07:00
|
|
|
memFile = memFile;
|
|
|
|
identified = true;
|
2011-02-23 03:26:33 -07:00
|
|
|
|
2011-02-23 03:49:33 -07:00
|
|
|
OffsetGroup * strGrp = my_descriptor->getGroup("string")->getGroup("MSVC");
|
2011-02-23 03:26:33 -07:00
|
|
|
STLSTR_buf_off = strGrp->getOffset("buffer");
|
|
|
|
STLSTR_size_off = strGrp->getOffset("size");
|
|
|
|
STLSTR_cap_off = strGrp->getOffset("capacity");
|
|
|
|
return true;
|
|
|
|
}
|
2010-04-26 16:12:22 -06:00
|
|
|
}
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
2011-02-23 03:26:33 -07:00
|
|
|
catch (Error::AllMemdef&)
|
2010-02-15 16:04:15 -07:00
|
|
|
{
|
2011-02-23 03:26:33 -07:00
|
|
|
continue;
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
}
|
2011-02-23 03:26:33 -07:00
|
|
|
return false;
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t WineProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
|
|
|
{
|
2011-02-23 03:26:33 -07:00
|
|
|
uint32_t start_offset = offset + STLSTR_buf_off;
|
|
|
|
size_t length = Process::readDWord(offset + STLSTR_size_off);
|
|
|
|
size_t capacity = Process::readDWord(offset + STLSTR_cap_off);
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
size_t read_real = min(length, bufcapacity-1);// keep space for null termination
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// read data from inside the string structure
|
|
|
|
if(capacity < 16)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
read(start_offset, read_real , (uint8_t *)buffer);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
else // read data from what the offset + 4 dword points to
|
|
|
|
{
|
2011-02-22 23:50:21 -07:00
|
|
|
start_offset = Process::readDWord(start_offset);// dereference the start offset
|
2010-04-12 19:11:26 -06:00
|
|
|
read(start_offset, read_real, (uint8_t *)buffer);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
buffer[read_real] = 0;
|
|
|
|
return read_real;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string WineProcess::readSTLString (uint32_t offset)
|
|
|
|
{
|
2011-02-23 03:26:33 -07:00
|
|
|
uint32_t start_offset = offset + STLSTR_buf_off;
|
|
|
|
size_t length = Process::readDWord(offset + STLSTR_size_off);
|
|
|
|
size_t capacity = Process::readDWord(offset + STLSTR_cap_off);
|
2010-09-17 20:36:14 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
char * temp = new char[capacity+1];
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// read data from inside the string structure
|
|
|
|
if(capacity < 16)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
read(start_offset, capacity, (uint8_t *)temp);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
else // read data from what the offset + 4 dword points to
|
|
|
|
{
|
2011-02-22 23:50:21 -07:00
|
|
|
start_offset = Process::readDWord(start_offset);// dereference the start offset
|
2010-04-12 19:11:26 -06:00
|
|
|
read(start_offset, capacity, (uint8_t *)temp);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
temp[length] = 0;
|
|
|
|
string ret = temp;
|
|
|
|
delete temp;
|
|
|
|
return ret;
|
2010-02-18 18:48:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
string WineProcess::readClassName (uint32_t vptr)
|
|
|
|
{
|
2011-02-22 23:50:21 -07:00
|
|
|
int rtti = Process::readDWord(vptr - 0x4);
|
|
|
|
int typeinfo = Process::readDWord(rtti + 0xC);
|
2010-02-18 18:48:03 -07:00
|
|
|
string raw = readCString(typeinfo + 0xC); // skips the .?AV
|
2010-04-09 06:44:00 -06:00
|
|
|
raw.resize(raw.length() - 2);// trim @@ from end
|
2010-02-18 18:48:03 -07:00
|
|
|
return raw;
|
2010-09-16 19:21:41 -06:00
|
|
|
}
|