ptrace (process trace system call) on Linux from D

Matej Nanut matejnanut at gmail.com
Tue May 8 16:05:12 PDT 2012


On Tuesday, 8 May 2012 at 16:41:55 UTC, mta`chrono wrote:
> But consider that fork() is a very specific UNIX syscall. There 
> is
> nothing similar like that on Windows. That's maybe why they 
> didn't wrap
> it in Phobos.

Ah yes, very true.  I didn't think of that.

> Maybe the same applies to wait() that seems to rely on the UNIX 
> signal
> stuff. But there should be some kind of derivate on windows, 
> too.

I think there must be something similiar, but I assume the usages 
are slightly different and as such wrapping these things into a 
common API might hinder performance.  Also, since I need these 
for use with ptrace, the program will only run on Linux (maybe 
POSIX?) systems anyway (I don't think Windows has ptrace?).

>> If struct method names are mangled, does that mean that that 
>> way of
>> doing it doesn't work? I'll try it anyway, to try and get rid 
>> of a few
>> extra files.
>
> It shouldn't work. But you can add another custom ptrace method 
> (_NOT_
> extern(C)) with different operators.

I do kinda want the same operators though.

> If you can give more information of your superior intention 
> (what are
> you going to create?) then I'll might provide a better 
> assistance!

I basically want to track system calls and mess with the program 
issuing them. (As per ptrace(PTRACE_SYSCALL, ...).)  The common 
pattern of doing something like this is:

---
void main(string[] args)
{
   pid_t childPid;
   switch (childPid = fork()) {
     case -1: /* error stuff */
              break;
     case 0:  ptrace(PTRACE_TRACEME, 0, null, null);
              execvp(args[1], args[1 .. $]);
              break;
     default: /* do ptrace magic in parent */
              break;
   }
}
---

And for this I would like the most D-ish way of 
importing/including/linking wait(), fork() and ptrace(). :-)

Thanks, Matej


More information about the Digitalmars-d-learn mailing list