2012-03-21 78 views
0

如何攔截從我的進程調用的其他進程發出的調用。 (說 - 我打電話make,我想攔截並修改make的gcc調用)。OS-X Linux攔截進程調用

+0

* OS-X Linux攔截進程調用* OSX或Linux?選一個。 – cnicutar 2012-03-21 16:19:26

回答

1

這裏做在Makefile是一個小例子與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"; 
    } 
} 
0

你不容易。問題所在的設施是ptrace功能,而不是心臟病。

1

從您的問題聽起來您正在尋找Makefile幫助,特別是您正在尋找所有對C編譯器的調用。

make允許在本地重新定義任何命令 - 所有您需要做的是重新定義make中的宏 - 對於gcc您只需重新定義CC宏。

你可以做到這一點從命令像,像

make CC=echo 

這將取代從gcc所有調用echo(不是很有用,但你的想法)。 或者你也可以通過添加像

CC=echo 
    testprogram: testprogram.o 

一條線,當你做make testprogram化妝會迴音東西,而不是調用gcc的