error checking for attach

develop
Petr Mrázek 2009-10-29 14:39:40 +00:00
parent d84ed97d5a
commit 1110786d4c
1 changed files with 32 additions and 7 deletions

@ -77,26 +77,51 @@ void Process::setMemFile(const string & memf)
/*
* LINUX PART
*/
// shamelessly stolen from dwarf therapist code. credit should go to the author there :)
bool Process::attach()
{
// TODO: check for errors!
// check if another process is attached
if(g_pProcess != NULL)
{
return false;
}
// can we attach?
if (ptrace(PTRACE_ATTACH , my_handle, NULL, NULL) == -1)
{
// no, we got an error
perror("ptrace attach error");
cerr << "attach failed on pid" << my_handle << endl;
return false;
}
int status;
while(true)
{
// we wait on the pid
pid_t w = waitpid(my_handle, &status, 0);
if (w == -1)
{
// child died
perror("wait inside attach()");
//LOGC << "child died?";
//exit(-1);
return false;
}
// stopped -> let's continue
if (WIFSTOPPED(status))
{
break;
}
}
cout << "Managed to attach to pid " << my_handle << endl;
ptrace(PTRACE_ATTACH , my_handle, NULL, NULL);
wait(NULL); // wait for DF to be stopped.
attached = true;
// HACK: Set the global process variables
g_pProcess = this;
g_ProcessHandle = my_handle;
g_ProcessMemFile = open(memFile.c_str(),O_RDONLY);
return true; // we are attached
}
bool Process::detach()
{
// TODO: check for errors.