2009-11-16 20:19:13 -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>
|
2011-05-09 03:17:35 -06:00
|
|
|
#include <set>
|
2011-04-10 02:19:15 -06:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
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"
|
2010-08-20 06:10:05 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFError.h"
|
2009-12-22 14:19:39 -07:00
|
|
|
#include <errno.h>
|
2009-12-31 18:49:32 -07:00
|
|
|
#include <sys/ptrace.h>
|
2011-05-09 03:17:35 -06:00
|
|
|
#include <sys/syscall.h>
|
2011-04-10 02:19:15 -06:00
|
|
|
#include <md5wrapper.h>
|
2009-11-16 20:19:13 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-02-23 04:08:30 -07:00
|
|
|
namespace {
|
|
|
|
class NormalProcess : public LinuxProcessBase
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2011-03-24 23:40:33 -06:00
|
|
|
private:
|
|
|
|
uint8_t vector_start;
|
2011-05-09 03:17:35 -06:00
|
|
|
set <uint32_t> thread_ids;
|
|
|
|
|
|
|
|
void waitForSuspend(set<uint32_t> &threads);
|
2011-02-23 04:08:30 -07:00
|
|
|
public:
|
2011-02-24 03:43:33 -07:00
|
|
|
NormalProcess(uint32_t pid, VersionInfoFactory * known_versions);
|
2011-03-02 14:23:24 -07:00
|
|
|
~NormalProcess()
|
|
|
|
{
|
|
|
|
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);
|
2011-04-19 17:28:47 -06:00
|
|
|
size_t writeSTLString(const uint32_t address, const std::string writeString);
|
|
|
|
size_t copySTLString(const uint32_t address, const uint32_t target);
|
2011-02-23 04:08:30 -07:00
|
|
|
// get class name of an object with rtti/type info
|
2011-05-09 03:48:54 -06:00
|
|
|
std::string doReadClassName(uint32_t vptr);
|
2009-11-16 20:19:13 -07:00
|
|
|
};
|
2011-02-23 04:08:30 -07:00
|
|
|
}
|
|
|
|
|
2011-02-24 03:43:33 -07:00
|
|
|
Process* DFHack::createNormalProcess(uint32_t pid, VersionInfoFactory * known_versions)
|
2011-02-23 04:08:30 -07:00
|
|
|
{
|
|
|
|
return new NormalProcess(pid, known_versions);
|
|
|
|
}
|
2009-11-16 20:19:13 -07:00
|
|
|
|
2011-02-24 03:43:33 -07:00
|
|
|
NormalProcess::NormalProcess(uint32_t pid, VersionInfoFactory * known_versions) : LinuxProcessBase(pid)
|
2009-11-16 20:19:13 -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
|
|
|
|
2009-11-16 20:19:13 -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;
|
2009-11-16 20:19:13 -07:00
|
|
|
sprintf(cwd_name,"/proc/%d/cwd", pid);
|
|
|
|
sprintf(cmdline_name,"/proc/%d/cmdline", pid);
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-11-16 20:19:13 -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
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
// is this the regular linux DF?
|
2010-06-05 16:56:09 -06:00
|
|
|
if (strstr(target_name, "dwarfort.exe") != 0 || strstr(target_name,"Dwarf_Fortress") != 0)
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2011-02-24 03:43:33 -07:00
|
|
|
md5wrapper md5;
|
|
|
|
// get hash of the running DF process
|
|
|
|
string hash = md5.getHashFromFile(target_name);
|
2009-11-16 20:19:13 -07:00
|
|
|
// create linux process, add it to the vector
|
2011-02-24 03:43:33 -07:00
|
|
|
VersionInfo * vinfo = known_versions->getVersionInfoByMD5(hash);
|
|
|
|
if(vinfo)
|
2010-02-27 22:21:50 -07: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
|
|
|
identified = true;
|
2009-11-16 20:19:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
struct _Rep_base
|
|
|
|
{
|
2011-04-19 17:28:47 -06:00
|
|
|
uint32_t _M_length; // length of text stored, not including zero termination
|
|
|
|
uint32_t _M_capacity; // capacity, not including zero termination
|
2011-05-08 17:55:02 -06:00
|
|
|
int32_t _M_refcount; // reference count (two STL strings can share a common buffer, copy on write rules apply)
|
2010-02-15 16:04:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t NormalProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
|
|
|
{
|
|
|
|
_Rep_base header;
|
2011-02-22 23:50:21 -07:00
|
|
|
offset = Process::readDWord(offset);
|
2010-04-12 19:11:26 -06:00
|
|
|
read(offset - sizeof(_Rep_base),sizeof(_Rep_base),(uint8_t *)&header);
|
2010-02-15 16:04:15 -07:00
|
|
|
size_t read_real = min((size_t)header._M_length, bufcapacity-1);// keep space for null termination
|
2010-04-12 19:11:26 -06:00
|
|
|
read(offset,read_real,(uint8_t * )buffer);
|
2010-02-15 16:04:15 -07:00
|
|
|
buffer[read_real] = 0;
|
|
|
|
return read_real;
|
|
|
|
}
|
2011-04-20 13:32:49 -06:00
|
|
|
|
2011-04-19 17:28:47 -06:00
|
|
|
size_t NormalProcess::writeSTLString(const uint32_t address, const std::string writeString)
|
|
|
|
{
|
|
|
|
_Rep_base header;
|
|
|
|
// get buffer location
|
|
|
|
uint32_t start = Process::readDWord(address);
|
|
|
|
// read the header
|
|
|
|
read(start - sizeof(_Rep_base),sizeof(_Rep_base),(uint8_t *)&header);
|
|
|
|
|
|
|
|
// the buffer has actual size = 1. no space for storing anything more than a zero byte
|
|
|
|
if(header._M_capacity == 0)
|
|
|
|
return 0;
|
2011-05-08 17:55:02 -06:00
|
|
|
if(header._M_refcount > 0 ) // one ref or one non-shareable (-1) ref
|
2011-04-20 13:32:49 -06:00
|
|
|
return 0;
|
2011-04-19 17:28:47 -06:00
|
|
|
|
|
|
|
// get writeable length (lesser of our string length and capacity of the target)
|
|
|
|
uint32_t lstr = writeString.length();
|
|
|
|
uint32_t allowed_copy = min(lstr, header._M_capacity);
|
2011-04-20 13:32:49 -06:00
|
|
|
// write header with new length.
|
|
|
|
header._M_length = allowed_copy;
|
|
|
|
write(start - sizeof(_Rep_base),sizeof(_Rep_base),(uint8_t *)&header);
|
2011-04-19 17:28:47 -06:00
|
|
|
// write string, add a zero terminator, return bytes written
|
|
|
|
write(start, allowed_copy, (uint8_t *) writeString.c_str());
|
|
|
|
writeByte(start + allowed_copy, 0);
|
|
|
|
return allowed_copy;
|
|
|
|
}
|
2010-02-15 16:04:15 -07:00
|
|
|
|
2011-03-24 23:40:33 -06:00
|
|
|
void NormalProcess::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
|
|
|
const string NormalProcess::readSTLString (uint32_t offset)
|
|
|
|
{
|
|
|
|
_Rep_base header;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2011-02-22 23:50:21 -07:00
|
|
|
offset = Process::readDWord(offset);
|
2010-04-12 19:11:26 -06:00
|
|
|
read(offset - sizeof(_Rep_base),sizeof(_Rep_base),(uint8_t *)&header);
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// FIXME: use char* everywhere, avoid string
|
|
|
|
char * temp = new char[header._M_length+1];
|
2010-04-12 19:11:26 -06:00
|
|
|
read(offset,header._M_length+1,(uint8_t * )temp);
|
2010-02-15 16:04:15 -07:00
|
|
|
string ret(temp);
|
|
|
|
delete temp;
|
|
|
|
return ret;
|
2010-02-18 18:48:03 -07:00
|
|
|
}
|
|
|
|
|
2011-04-19 17:28:47 -06:00
|
|
|
size_t NormalProcess::copySTLString (uint32_t offset, uint32_t target)
|
2011-04-13 10:36:10 -06:00
|
|
|
{
|
|
|
|
_Rep_base header;
|
|
|
|
|
|
|
|
offset = Process::readDWord(offset);
|
|
|
|
uint32_t old_target = Process::readDWord(target);
|
|
|
|
|
|
|
|
if (offset == old_target)
|
2011-04-19 17:28:47 -06:00
|
|
|
return 0;
|
2011-04-13 10:36:10 -06:00
|
|
|
|
|
|
|
read(offset - sizeof(_Rep_base),sizeof(_Rep_base),(uint8_t *)&header);
|
|
|
|
|
|
|
|
// destroying the leaked state
|
|
|
|
if (header._M_refcount == -1)
|
|
|
|
header._M_refcount = 1;
|
|
|
|
else
|
|
|
|
header._M_refcount++;
|
|
|
|
|
|
|
|
write(offset - sizeof(_Rep_base),sizeof(_Rep_base),(uint8_t *)&header);
|
|
|
|
|
|
|
|
writeDWord(target, offset);
|
2011-04-19 17:28:47 -06:00
|
|
|
return header._M_length;
|
2011-04-13 10:36:10 -06:00
|
|
|
}
|
|
|
|
|
2011-05-09 03:48:54 -06:00
|
|
|
string NormalProcess::doReadClassName (uint32_t vptr)
|
2010-02-18 18:48:03 -07:00
|
|
|
{
|
2011-02-22 23:50:21 -07:00
|
|
|
int typeinfo = Process::readDWord(vptr - 0x4);
|
|
|
|
int typestring = Process::readDWord(typeinfo + 0x4);
|
2010-02-18 18:48:03 -07:00
|
|
|
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-04-20 10:13:00 -06:00
|
|
|
}
|
2011-03-02 14:23:24 -07:00
|
|
|
|
|
|
|
bool NormalProcess::asyncSuspend()
|
|
|
|
{
|
|
|
|
return suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalProcess::suspend()
|
|
|
|
{
|
|
|
|
if(!attached)
|
|
|
|
return false;
|
|
|
|
if(suspended)
|
|
|
|
return true;
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
set<uint32_t> threads;
|
|
|
|
|
|
|
|
for (set<uint32_t>::iterator it = thread_ids.begin(); it != thread_ids.end(); ++it) {
|
|
|
|
if (syscall(SYS_tgkill, my_pid, *it, SIGSTOP) == -1) {
|
|
|
|
cerr << "couldn't stop thread " << *it << endl;
|
|
|
|
perror("kill SIGSTOP error");
|
|
|
|
} else {
|
|
|
|
threads.insert(*it);
|
2011-03-02 14:23:24 -07:00
|
|
|
}
|
|
|
|
}
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
if (threads.empty()) {
|
|
|
|
cerr << "couldn't suspend any of the threads";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForSuspend(threads);
|
|
|
|
|
|
|
|
if (!threads.empty())
|
|
|
|
cerr << "couldn't suspend some of the threads";
|
|
|
|
|
2011-03-02 14:23:24 -07:00
|
|
|
suspended = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalProcess::forceresume()
|
|
|
|
{
|
|
|
|
return resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalProcess::resume()
|
|
|
|
{
|
|
|
|
if(!attached)
|
|
|
|
return false;
|
|
|
|
if(!suspended)
|
|
|
|
return true;
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
for (set<uint32_t>::iterator it = thread_ids.begin(); it != thread_ids.end(); ++it) {
|
|
|
|
int result = ptrace(PTRACE_CONT, *it, NULL, NULL);
|
|
|
|
if(result == -1)
|
|
|
|
{
|
|
|
|
cerr << "couldn't resume thread " << *it << endl;
|
|
|
|
perror("ptrace resume error");
|
|
|
|
ok = false;
|
|
|
|
}
|
2011-03-02 14:23:24 -07:00
|
|
|
}
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
if (ok)
|
|
|
|
suspended = false;
|
|
|
|
return ok;
|
2011-03-02 14:23:24 -07:00
|
|
|
}
|
|
|
|
|
2011-05-09 03:17:35 -06:00
|
|
|
void NormalProcess::waitForSuspend(set<uint32_t> &threads)
|
2011-03-02 14:23:24 -07:00
|
|
|
{
|
|
|
|
int status;
|
2011-05-09 03:17:35 -06:00
|
|
|
while (!threads.empty()) {
|
|
|
|
pid_t w = waitpid(-1, &status, __WALL);
|
|
|
|
if (w == -1) {
|
|
|
|
perror("waitpid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (threads.find(w) == threads.end()
|
|
|
|
&& thread_ids.find(w) == thread_ids.end())
|
|
|
|
continue;
|
|
|
|
if (WIFSTOPPED(status)) {
|
|
|
|
threads.erase(w);
|
|
|
|
thread_ids.insert(w);
|
|
|
|
} else if (WIFEXITED(status) || WIFSIGNALED(status)) {
|
|
|
|
threads.erase(w);
|
|
|
|
thread_ids.erase(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalProcess::attach()
|
|
|
|
{
|
2011-03-02 14:23:24 -07:00
|
|
|
if(attached)
|
|
|
|
{
|
|
|
|
if(!suspended)
|
|
|
|
return suspend();
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
set<uint32_t> threads;
|
|
|
|
vector<uint32_t> thread_vec;
|
|
|
|
|
|
|
|
if (!getThreadIDs(thread_vec))
|
2011-03-02 14:23:24 -07:00
|
|
|
return false;
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
for (vector<uint32_t>::iterator it = thread_vec.begin(); it != thread_vec.end(); ++it) {
|
|
|
|
if (ptrace(PTRACE_ATTACH, *it, NULL, NULL) == -1)
|
2011-03-02 14:23:24 -07:00
|
|
|
{
|
2011-05-09 03:17:35 -06:00
|
|
|
// no, we got an error
|
|
|
|
perror("ptrace attach error");
|
|
|
|
cerr << "attach failed on pid " << *it << endl;
|
|
|
|
continue;
|
2011-03-02 14:23:24 -07:00
|
|
|
}
|
2011-05-09 03:17:35 -06:00
|
|
|
threads.insert(*it);
|
2011-03-02 14:23:24 -07:00
|
|
|
}
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
thread_ids.clear();
|
|
|
|
waitForSuspend(threads);
|
|
|
|
|
|
|
|
if (thread_ids.empty()) {
|
|
|
|
cerr << "couldn't attach to any threads" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!threads.empty())
|
|
|
|
cerr << "couldn't attach to some threads" << endl;
|
|
|
|
|
2011-03-02 14:23:24 -07:00
|
|
|
suspended = true;
|
|
|
|
|
|
|
|
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 = true;
|
|
|
|
|
|
|
|
memFileHandle = proc_pid_mem;
|
|
|
|
return true; // we are attached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalProcess::detach()
|
|
|
|
{
|
|
|
|
if(!attached) return true;
|
|
|
|
if(!suspended) suspend();
|
|
|
|
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
|
|
|
|
{
|
2011-05-09 03:17:35 -06:00
|
|
|
for (set<uint32_t>::iterator it = thread_ids.begin(); it != thread_ids.end();) {
|
|
|
|
// detach
|
|
|
|
result = ptrace(PTRACE_DETACH, *it, NULL, NULL);
|
|
|
|
if(result == -1)
|
|
|
|
{
|
|
|
|
cerr << "couldn't detach from process pid" << *it << endl;
|
|
|
|
perror("ptrace detach");
|
|
|
|
return false;;
|
|
|
|
}
|
|
|
|
thread_ids.erase(it++);
|
2011-03-02 14:23:24 -07:00
|
|
|
}
|
2011-05-09 03:17:35 -06:00
|
|
|
|
|
|
|
attached = false;
|
|
|
|
return true;
|
2011-03-02 14:23:24 -07:00
|
|
|
}
|
|
|
|
}
|