[vox-tech] Suspending a process by PID

Peter Jay Salzman vox-tech@lists.lugod.org
Mon, 22 Dec 2003 10:18:27 -0800


hey nicole,

yeah, sure it's possible.  i'm surprised that sending SIGSTOP/SIGCONT
doesn't work.  but then again, i've never tried that.

anyway, using ptrace() to attach to a process will make the process a
child and suspend it immediately.  you can then step through
instructions, look at memory maps and all kinds of neat stuff.  for
instnace, you can use this kind of thing to cheat at games.  neat stuff.

more than you wanted, but it has the effect you desire (i think).

here's some code i wrote that attaches to a process:

bool Process::attach(void)
{
   int status;

   if(attached) {
      attached++;
      return true;
   }

   // Request to attach.
   if(ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) {
      return(false);
   }

   // Wait for the child to tell us it worked
   if(waitpid(pid, &status, WUNTRACED) != pid) {
      return false;
   }
   if(!WIFSTOPPED(status)) {
      return false;
   }

   attached++;
   return true;
}

and detach from the process:



bool Process::detach(void)
{

   // Fail if we we're unattached.
   if(! attached)
      return false;

   if(attached - 1 == 0) {

      // I don't know why it's supposed to be 1 either. ;)
      if(ptrace(PTRACE_DETACH, pid, 1, 0) == -1) {
         return(false);
      }

      memoryMaps.clear();
   }

   attached--;
   return true;
}


pete




On Mon 22 Dec 03,  9:57 AM, Nicole TWN <ana.ng@tmbg.org> said:
> Hi gang
> 
> Anyone know how to suspend a process given its PID?
> 
> It seems like it should be possible, via signals or something, but I can't 
> find how.
> 
> Thanks
> 
> --nicole twn
> 
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech

-- 
Make everything as simple as possible, but no simpler.  -- Albert Einstein
GPG Instructions: http://www.dirac.org/linux/gpg
GPG Fingerprint: B9F1 6CF3 47C4 7CD8 D33E 70A9 A3B9 1945 67EA 951D