Merge with TortoiseSVN:r9360
[i18n-zh.git] / cauchy / ptrace / ptrace_open_i686.c
blob3115f0512602d1f39d523c56dda5adc4911a530e
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/ptrace.h>
4 #include <sys/wait.h>
6 #define die(msg) { perror(msg); exit(EXIT_FAILURE); }
8 int
9 main(int argc, char *argv[])
11 int pid, c, rc, status;
12 struct timeval tv, tv2;
14 if (argc <= 1) {
15 printf("no enough args\n");
16 exit(EXIT_FAILURE);
19 pid = fork();
20 if (pid == -1) {
21 die("fork");
22 } else if (pid == 0) {
23 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) == -1) die("trace me");
24 if (kill(getpid(), SIGSTOP) == -1) die("stop");
25 if(argc == 2) {
26 execvp(argv[1], NULL);
27 } else {
28 execvp(argv[1], &argv[2]);
30 die("execvp");
33 c = 0;
34 while(1) {
35 c++;
36 gettimeofday(&tv, NULL);
37 rc = ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0);
38 if (rc == -1) die("trace syscall");
40 /* 0x57F */
41 rc = waitpid(pid, &status, 0);
42 if (rc == -1) die("waitpid");
44 gettimeofday(&tv2, NULL);
46 if (WIFEXITED(status)) break;
48 fprintf(stderr, "%03d: %07.5lf\n", c, (tv2.tv_sec - tv.tv_sec) + (tv2.tv_usec - tv.tv_usec) / 1E6);
51 return EXIT_SUCCESS;