forked from andreoli/fulltrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.c
88 lines (73 loc) · 1.57 KB
/
wrapper.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
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define _STR(x) #x
#define STR(x) _STR(x)
#define MAX_PATH 256
const char *find_debugfs(void)
{
static char debugfs[MAX_PATH+1];
static int debugfs_found;
char type[100];
FILE *fp;
if (debugfs_found)
return debugfs;
if ((fp = fopen("/proc/mounts","r")) == NULL) {
perror("/proc/mounts");
return NULL;
}
while (fscanf(fp, "%*s %"
STR(MAX_PATH)
"s %99s %*s %*d %*d\n",
debugfs, type) == 2) {
if (strcmp(type, "debugfs") == 0)
break;
}
fclose(fp);
if (strcmp(type, "debugfs") != 0) {
fprintf(stderr, "debugfs not mounted");
return NULL;
}
strcat(debugfs, "/tracing/");
debugfs_found = 1;
return debugfs;
}
const char *tracing_file(const char *file_name)
{
static char trace_file[MAX_PATH+1];
snprintf(trace_file, MAX_PATH, "%s/%s", find_debugfs(), file_name);
return trace_file;
}
int main (int argc, char **argv)
{
if (argc < 1)
exit(-1);
if (fork() > 0) {
int fd_pid, fd_mrk;
char line[64];
char pid[64];
int s, ret;
fd_pid = open(tracing_file("set_ftrace_pid"), O_WRONLY);
s = sprintf(pid, "%d\n", getpid());
ret = write(fd_pid, pid, s);
if (ret == -1) {
printf("error!\n");
exit(1);
}
close(fd_pid);
fd_mrk = open(tracing_file("trace_marker"), O_WRONLY);
s = sprintf(line, "----------- PROCESS START -----------\n");
ret = write(fd_mrk, line, s);
if (ret == -1) {
printf("error!\n");
exit(1);
}
close(fd_mrk);
execvp(argv[1], argv+1);
}
return 0;
}