如何拦截从我的进程调用的其他进程发出的调用。(比如说-我打电话给make,我想拦截和修改make对gcc的调用)。
问问题
612 次
3 回答
1
从您的问题看来,您正在寻找 Makefile 帮助,特别是您正在寻找对所有调用 c 编译器的操作。
make
允许在本地重新定义任何命令——你所要做的就是在 make 中重新定义宏——对于 gcc,你只需重新定义 CC 宏。
你可以从命令中做到这一点,比如
make CC=echo
这将替代所有来自gcc
to 的调用echo
(不是很有用,但你明白了)。或者你可以在 Makefile 中添加一行
CC=echo
testprogram: testprogram.o
当你这样做时make testprogram
,make会回显一些东西而不是调用gcc
于 2012-03-21T16:25:58.530 回答
1
这是一个 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, ®s);
/* 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";
}
}
于 2012-03-21T16:40:16.990 回答
0
你不容易。有问题的设施是 ptrace 功能,不适合胆小的人。
于 2012-03-21T16:21:39.680 回答