-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
84 lines (75 loc) · 1.36 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
** main.c for main in /home/marcha_j//test/c/strace/proj
**
** Made by hugo marchadier
** Login <marcha_j@epitech.net>
**
** Started on Thu May 3 16:11:25 2012 hugo marchadier
** Last update Tue Jun 26 17:47:47 2012 hugo marchadier
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include "xfct.h"
#include "ftrace.h"
static int is_number(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] < '0' || str[i] > '9')
return (-1);
i = i + 1;
}
return (1);
}
static int is_attach(int argc, char **argv)
{
int i;
int j;
i = 1;
while (i != argc)
{
if (strcmp(argv[i], "-p") == 0)
{
j = 1;
while (j != argc)
{
if (is_number(argv[j]) == 1)
{
ptrace_attach(atoi(argv[j]));
return (-1);
}
j = j + 1;
}
}
i = i + 1;
}
return (-1);
}
int main(int argc, char **argv)
{
pid_t pid;
if (argc < 2)
{
printf("Usage: ./strace [-p] <pid | executable>\n");
return (EXIT_SUCCESS);
}
if (is_attach(argc, argv) == -1)
{
if ((pid = fork()) == 0)
{
xptrace(PTRACE_TRACEME, 0, NULL, 0);
xexecvp(argv[1], &argv[1]);
}
else if (pid != -1)
strace(pid);
else
fprintf(stderr, "Error: fork failed\n");
}
return (0);
}