2009-09-13 18:02:46 -06: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.
|
|
|
|
*/
|
2009-11-10 20:37:28 -07:00
|
|
|
#include "DFCommonInternal.h"
|
2009-11-13 20:46:56 -07:00
|
|
|
using namespace DFHack;
|
2009-09-13 18:02:46 -06:00
|
|
|
|
|
|
|
Process::Process(DataModel * dm, memory_info* mi, ProcessHandle ph, uint32_t pid)
|
|
|
|
{
|
|
|
|
my_datamodel = dm;
|
|
|
|
my_descriptor = mi;
|
2009-10-04 07:08:20 -06:00
|
|
|
my_handle = ph;
|
2009-09-13 18:02:46 -06:00
|
|
|
my_pid = pid;
|
|
|
|
attached = false;
|
2009-11-16 09:47:22 -07:00
|
|
|
suspended = false;
|
2009-09-13 18:02:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Process::~Process()
|
|
|
|
{
|
|
|
|
if(attached)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
}
|
|
|
|
// destroy data model. this is assigned by processmanager
|
|
|
|
delete my_datamodel;
|
|
|
|
freeResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DataModel *Process::getDataModel()
|
|
|
|
{
|
|
|
|
return my_datamodel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
memory_info * Process::getDescriptor()
|
|
|
|
{
|
|
|
|
return my_descriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Process::setMemFile(const string & memf)
|
|
|
|
{
|
|
|
|
assert(!attached);
|
|
|
|
memFile = memf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LINUX_BUILD
|
|
|
|
/*
|
|
|
|
* LINUX PART
|
|
|
|
*/
|
2009-10-29 08:39:40 -06:00
|
|
|
|
2009-10-31 12:09:13 -06:00
|
|
|
|
2009-11-16 09:47:22 -07:00
|
|
|
bool Process::getThreadIDs(vector<uint32_t> & threads )
|
2009-10-31 12:09:13 -06:00
|
|
|
{
|
2009-11-16 09:47:22 -07:00
|
|
|
return false;
|
2009-10-31 12:09:13 -06:00
|
|
|
}
|
2009-11-14 21:25:00 -07:00
|
|
|
|
|
|
|
void Process::getMemRanges( vector<t_memrange> & ranges )
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
char name[1024];
|
|
|
|
char permissions[5]; // r/-, w/-, x/-, p/s, 0
|
|
|
|
|
|
|
|
sprintf(buffer, "/proc/%lu/maps", my_pid);
|
|
|
|
FILE *mapFile = ::fopen(buffer, "r");
|
|
|
|
uint64_t begin, end, offset, device1, device2, node;
|
|
|
|
|
|
|
|
while (fgets(buffer, 1024, mapFile))
|
|
|
|
{
|
|
|
|
t_memrange temp;
|
2009-11-15 13:41:10 -07:00
|
|
|
temp.name[0] = 0;
|
2009-11-14 21:25:00 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-16 09:47:22 -07:00
|
|
|
bool Process::suspend()
|
2009-09-13 18:02:46 -06:00
|
|
|
{
|
2009-10-31 12:09:13 -06:00
|
|
|
int status;
|
2009-11-16 09:47:22 -07:00
|
|
|
if(!attached)
|
|
|
|
return false;
|
|
|
|
if(suspended)
|
|
|
|
return true;
|
|
|
|
if (kill(my_handle, SIGSTOP) == -1)
|
2009-09-13 18:02:46 -06:00
|
|
|
{
|
2009-11-16 09:47:22 -07:00
|
|
|
// no, we got an error
|
|
|
|
perror("kill SIGSTOP error");
|
2009-09-13 18:02:46 -06:00
|
|
|
return false;
|
|
|
|
}
|
2009-11-16 09:47:22 -07:00
|
|
|
while(true)
|
2009-10-31 12:09:13 -06:00
|
|
|
{
|
2009-11-16 09:47:22 -07:00
|
|
|
// we wait on the pid
|
|
|
|
pid_t w = waitpid(my_handle, &status, 0);
|
|
|
|
if (w == -1)
|
2009-11-01 03:56:23 -07:00
|
|
|
{
|
2009-11-16 09:47:22 -07:00
|
|
|
// child died
|
|
|
|
perror("DF exited during suspend call");
|
2009-11-01 03:56:23 -07:00
|
|
|
return false;
|
2009-11-01 03:09:19 -07:00
|
|
|
}
|
2009-11-16 09:47:22 -07:00
|
|
|
// stopped -> let's continue
|
|
|
|
if (WIFSTOPPED(status))
|
2009-11-01 02:58:49 -07:00
|
|
|
{
|
2009-11-16 09:47:22 -07:00
|
|
|
break;
|
2009-11-01 02:58:49 -07:00
|
|
|
}
|
2009-10-31 12:09:13 -06:00
|
|
|
}
|
2009-11-16 09:47:22 -07:00
|
|
|
suspended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::resume()
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
if(!attached)
|
|
|
|
return false;
|
|
|
|
if(!suspended)
|
|
|
|
return true;
|
|
|
|
if (ptrace(PTRACE_CONT, my_handle, NULL, NULL) == -1)
|
|
|
|
{
|
|
|
|
// no, we got an error
|
|
|
|
perror("ptrace resume error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
suspended = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Process::attach()
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
if(g_pProcess != NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-29 08:39:40 -06:00
|
|
|
// can we attach?
|
|
|
|
if (ptrace(PTRACE_ATTACH , my_handle, NULL, NULL) == -1)
|
2009-10-29 09:37:06 -06:00
|
|
|
{
|
2009-10-29 08:39:40 -06:00
|
|
|
// no, we got an error
|
|
|
|
perror("ptrace attach error");
|
2009-10-31 12:09:13 -06:00
|
|
|
cerr << "attach failed on pid " << my_handle << endl;
|
2009-10-29 08:39:40 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
// we wait on the pid
|
|
|
|
pid_t w = waitpid(my_handle, &status, 0);
|
|
|
|
if (w == -1)
|
|
|
|
{
|
|
|
|
// child died
|
|
|
|
perror("wait inside attach()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// stopped -> let's continue
|
|
|
|
if (WIFSTOPPED(status))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2009-11-12 10:50:50 -07:00
|
|
|
}
|
2009-11-16 09:47:22 -07:00
|
|
|
suspended = true;
|
2009-10-29 08:39:40 -06:00
|
|
|
|
2009-10-29 11:40:39 -06:00
|
|
|
int proc_pid_mem = open(memFile.c_str(),O_RDONLY);
|
|
|
|
if(proc_pid_mem == -1)
|
|
|
|
{
|
|
|
|
ptrace(PTRACE_DETACH, my_handle, NULL, NULL);
|
|
|
|
cerr << "couldn't open /proc/" << my_handle << "/mem" << endl;
|
|
|
|
perror("open(memFile.c_str(),O_RDONLY)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attached = true;
|
|
|
|
g_pProcess = this;
|
|
|
|
g_ProcessHandle = my_handle;
|
|
|
|
|
|
|
|
g_ProcessMemFile = proc_pid_mem;
|
|
|
|
return true; // we are attached
|
|
|
|
}
|
2009-09-13 18:02:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::detach()
|
|
|
|
{
|
2009-11-01 02:32:47 -07:00
|
|
|
if(!attached) return false;
|
2009-11-16 09:47:22 -07:00
|
|
|
if(!suspended) suspend();
|
2009-10-29 11:52:40 -06:00
|
|
|
int result = 0;
|
2009-11-16 09:47:22 -07:00
|
|
|
// close /proc/PID/mem
|
|
|
|
result = close(g_ProcessMemFile);
|
2009-10-29 11:52:40 -06:00
|
|
|
if(result == -1)
|
|
|
|
{
|
|
|
|
cerr << "couldn't close /proc/"<< my_handle <<"/mem" << endl;
|
2009-10-29 11:54:02 -06:00
|
|
|
perror("mem file close");
|
2009-10-29 11:52:40 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-16 09:47:22 -07:00
|
|
|
// detach
|
2009-10-29 11:52:40 -06:00
|
|
|
g_ProcessMemFile = -1;
|
|
|
|
result = ptrace(PTRACE_DETACH, my_handle, NULL, NULL);
|
|
|
|
if(result == -1)
|
|
|
|
{
|
|
|
|
cerr << "couldn't detach from process pid" << my_handle << endl;
|
2009-10-29 11:54:02 -06:00
|
|
|
perror("ptrace detach");
|
2009-10-29 11:52:40 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attached = false;
|
|
|
|
g_pProcess = NULL;
|
|
|
|
g_ProcessHandle = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-13 18:02:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-14 21:25:00 -07:00
|
|
|
|
2009-09-13 18:02:46 -06:00
|
|
|
void Process::freeResources()
|
|
|
|
{
|
|
|
|
// nil
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* WINDOWS PART
|
|
|
|
*/
|
|
|
|
|
2009-11-16 09:47:22 -07:00
|
|
|
bool Process::suspend()
|
|
|
|
{
|
|
|
|
if(!attached)
|
|
|
|
return false;
|
|
|
|
if(suspended)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
vector<uint32_t> threads;
|
|
|
|
getThreadIDs( threads );
|
|
|
|
for(int i = 0; i < /*threads.size()*/ 1; i++)
|
|
|
|
{
|
|
|
|
HANDLE thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD) threads[i]);
|
|
|
|
SuspendThread(thread_handle);
|
|
|
|
CloseHandle(thread_handle);
|
|
|
|
}
|
|
|
|
suspended = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::resume()
|
|
|
|
{
|
|
|
|
if(!attached)
|
|
|
|
return false;
|
|
|
|
if(!suspended)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
vector<uint32_t> threads;
|
|
|
|
getThreadIDs( threads );
|
|
|
|
for(int i = 0; i < /* threads.size() */ 1; i++)
|
|
|
|
{
|
|
|
|
HANDLE thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD) threads[i]);
|
|
|
|
ResumeThread(thread_handle);
|
|
|
|
CloseHandle(thread_handle);
|
|
|
|
}
|
|
|
|
suspended = false;
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-13 18:02:46 -06:00
|
|
|
|
|
|
|
bool Process::attach()
|
2009-10-04 07:08:20 -06:00
|
|
|
{
|
|
|
|
if(attached)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-16 09:47:22 -07:00
|
|
|
attached = true;
|
|
|
|
g_pProcess = this;
|
|
|
|
g_ProcessHandle = my_handle;
|
|
|
|
suspend();
|
2009-09-13 18:02:46 -06:00
|
|
|
|
2009-11-16 09:47:22 -07:00
|
|
|
return true;
|
2009-09-13 18:02:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Process::detach()
|
|
|
|
{
|
|
|
|
if(!attached)
|
|
|
|
{
|
|
|
|
return false;
|
2009-10-04 07:08:20 -06:00
|
|
|
}
|
2009-11-16 09:47:22 -07:00
|
|
|
resume();
|
|
|
|
attached = false;
|
|
|
|
g_pProcess = NULL;
|
|
|
|
g_ProcessHandle = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::getThreadIDs(vector<uint32_t> & threads )
|
|
|
|
{
|
|
|
|
HANDLE AllThreads = INVALID_HANDLE_VALUE;
|
|
|
|
THREADENTRY32 te32;
|
|
|
|
|
|
|
|
AllThreads = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
|
|
|
|
if( AllThreads == INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
te32.dwSize = sizeof(THREADENTRY32 );
|
|
|
|
|
|
|
|
if( !Thread32First( AllThreads, &te32 ) )
|
|
|
|
{
|
|
|
|
CloseHandle( AllThreads );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( te32.th32OwnerProcessID == my_pid )
|
|
|
|
{
|
|
|
|
threads.push_back(te32.th32ThreadID);
|
|
|
|
}
|
|
|
|
} while( Thread32Next(AllThreads, &te32 ) );
|
|
|
|
|
|
|
|
CloseHandle( AllThreads );
|
|
|
|
return true;
|
2009-09-13 18:02:46 -06:00
|
|
|
}
|
|
|
|
|
2009-11-16 09:47:22 -07:00
|
|
|
|
2009-11-14 21:25:00 -07:00
|
|
|
void Process::getMemRanges( vector<t_memrange> & ranges )
|
|
|
|
{
|
|
|
|
// code here is taken from hexsearch by Silas Dunmore.
|
|
|
|
// As this IMHO isn't a 'sunstantial portion' of anything, I'm not including the MIT license here
|
|
|
|
|
|
|
|
// I'm faking this, because there's no way I'm using VirtualQuery
|
|
|
|
|
|
|
|
t_memrange temp;
|
|
|
|
uint32_t base = this->my_descriptor->getBase();
|
|
|
|
temp.start = base + 0x1000; // more fakery.
|
|
|
|
temp.end = base + MreadDWord(base+MreadDWord(base+0x3C)+0x50)-1; // yay for magic.
|
|
|
|
temp.read = 1;
|
|
|
|
temp.write = 1;
|
|
|
|
temp.execute = 0; // fake
|
|
|
|
strcpy(temp.name,"pants");// that's right. I'm calling it pants. Windows can go to HELL
|
|
|
|
ranges.push_back(temp);
|
|
|
|
}
|
2009-09-13 18:02:46 -06:00
|
|
|
|
|
|
|
void Process::freeResources()
|
|
|
|
{
|
|
|
|
// opened by process manager
|
|
|
|
CloseHandle(my_handle);
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|