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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include "timestamp.h"
#define MAX_EVENTS 128
static int fd;
static int event_count = 1;
static cmd_t ids[MAX_EVENTS];
static unsigned long total_bytes = 0;
static int disable_all(int fd)
{
int ret, size;
ids[0] = DISABLE_CMD;
fprintf(stderr, "Disabling %d events.\n", event_count - 1);
size = event_count * sizeof(cmd_t);
ret = write(fd, ids, size);
if (ret != size)
fprintf(stderr, "write = %d, meant to write %d (%m)\n", ret, size);
return size == ret;
}
static int enable_events(int fd, char* str)
{
cmd_t *id;
cmd_t cmd[2];
id = ids + event_count;
if (!str2event(str, id))
return 0;
event_count += 1;
cmd[0] = ENABLE_CMD;
cmd[1] = id[0];
return write(fd, cmd, sizeof(cmd)) == sizeof(cmd_t) * 2;
}
static void cat2stdout(int fd)
{
static char buf[4096];
int rd;
while ((rd = read(fd, buf, 4096)) > 0) {
total_bytes += rd;
fwrite(buf, 1, rd, stdout);
}
}
static void usage(void)
{
fprintf(stderr,
"Usage: ftcat <ft device> TS1 TS2 ...."
"\n");
exit(1);
}
static void on_sigint(int sig)
{
close(fd);
fflush(stdout);
exit(0);
}
static void shutdown(int sig)
{
int ok;
ok = disable_all(fd);
if (!ok)
fprintf(stderr, "disable_all: %m\n");
}
int main(int argc, char** argv)
{
const char* trace_file;
if (argc < 3)
usage();
trace_file = argv[1];
fd = open(trace_file, O_RDWR);
if (fd < 0) {
perror("could not open feathertrace");
return 1;
}
argc -= 2;
argv += 2;
signal(SIGINT, shutdown);
signal(SIGUSR1, shutdown);
signal(SIGTERM, shutdown);
while (argc--) {
if (!enable_events(fd, *argv)) {
fprintf(stderr, "Enabling %s failed: %m\n", *argv);
return 2;
}
argv++;
}
cat2stdout(fd);
close(fd);
fflush(stdout);
fprintf(stderr, "%s: %lu bytes read.\n", trace_file, total_bytes);
return 0;
}
|