- 论坛徽章:
- 0
|
用ptrace()试试看,比如:- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ptrace.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <sys/reg.h> /* For user_regs_struct
- etc. */
- #include <bits/siginfo.h>
- int main(int argc, char *argv[])
- {
- pid_t traced_process;
- siginfo_t test;
- long ins;
- if(argc != 2) {
- printf("Usage: %s <pid to be traced>\n",
- argv[0], argv[1]);
- exit(1);
- }
- traced_process = atoi(argv[1]);
- if (ptrace(PTRACE_ATTACH, traced_process, NULL, NULL) == -1)
- {
- perror("ATTACH:");
- return 0;
- }
- while (1)
- {
- wait(NULL);
- if (ptrace(PTRACE_GETSIGINFO, traced_process, NULL, &test) == -1)
- {
- perror("GETSIGINFO:");
- break;
- }
- if (test.si_pid)
- {
- break;
- }
- else
- {
- if (ptrace(PTRACE_CONT, traced_process, NULL, NULL) == -1)
- {
- perror("CONT:");
- break;
- }
- }
- }
- printf("sid:%ld, uid:%ld, si_status:%x\n", test.si_pid, test.si_uid, test.si_status);
- ptrace(PTRACE_KILL, traced_process, NULL, NULL);
- ptrace(PTRACE_DETACH, traced_process, NULL, NULL);
- return 0;
- }
复制代码 |
|