0xDEAD BEEF
0xDEAD BEEF

Reputation: 2104

OS-X Linux intercept process call

how do I intercept calls made from other process which I have called from my process. (say - I call make and I would like to intercept and modify call to gcc from make).

Upvotes: 0

Views: 722

Answers (3)

perreal
perreal

Reputation: 97918

Here is a small example with ptrace:

#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <sys/user.h>
#include <sys/prctl.h>

const char *sys_call_name(long num);

int main()
{
  pid_t pid = fork();
  struct user_regs_struct regs;
  if (!pid) { 
    /* child */
    while (1) { printf("C\n"); sleep(1); }
  }
  else { /* parent */
    int status = 0;
    ptrace(PTRACE_ATTACH, pid, NULL, 0); 
    ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_SYSCALL) ;
    while (1) {
      printf("waiting\n");
      pid = wait(&status);
      /* child gone */
      //if (WIFEXITED(status)) { break; }
      ptrace(PTRACE_GETREGS, pid, 0, &regs);
      /* regs.orig_eax is the system call number */
      printf("A system call: %d : %s\n", regs.orig_eax, sys_call_name(regs.orig_eax));
      /* let child continue */
      ptrace(PTRACE_SYSCALL, pid, NULL, 0); 
    }   
  }
  return 0;
}


const char *sys_call_name(long num) {
  switch(num) {
    case   4: return "write";
    case 162: return "nanosleep";
    case 165: return "getresuid";
    case 174: return "rt_sigaction";
    case 175: return "rt_sigprocmask";
    default:  return "unknown";
  }
}

Upvotes: 1

Soren
Soren

Reputation: 14688

It sound from your question that you are looking for Makefile help, specifically you are looking for doing something for all call to the c-compiler.

make allows for any command to be redefined locally -- all you have to do is redefine the macro in make -- for gcc you would simply redefine the CC macros.

You could do that from the command like, like

make CC=echo

which would substitute all call from gcc to echo (not very useful, but you get the idea). Or you can do it in the Makefile by adding a line like

   CC=echo
   testprogram: testprogram.o

and when you do make testprogram the make will echo something rather than invoking gcc

Upvotes: 1

bmargulies
bmargulies

Reputation: 99993

You don't easily. The facility in question is the ptrace function, not for the faint of heart.

Upvotes: 0

Related Questions