Use O_RDWR for open write mode (for mmap)
[babeltrace.git] / tests / test-dummytrace.c
1 /*
2 * text-to-ctf.c
3 *
4 * BabelTrace - Convert Text to CTF
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * Depends on glibc 2.10 for getline().
19 */
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25 #include <dirent.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <uuid/uuid.h>
32 #include <string.h>
33 #include <endian.h>
34
35 #include <babeltrace/babeltrace.h>
36 #include <babeltrace/ctf/types.h>
37
38 static const char metadata[] =
39 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
40 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
41 "\n"
42 "trace {\n"
43 " major = %s;\n" /* major (e.g. 0) */
44 " minor = %s;\n" /* minor (e.g. 1) */
45 " uuid = %s;\n" /* UUID */
46 " byte_order = %s;\n" /* be or le */
47 " packet.header := struct {\n"
48 " uint32_t magic;\n"
49 " uint8_t trace_uuid[16];\n"
50 " };\n"
51 "};\n"
52 "\n"
53 "stream {\n"
54 " packet.context := struct {\n"
55 " uint32_t content_size;\n"
56 " uint32_t packet_size;\n"
57 " };\n"
58 "};\n"
59 "\n"
60 "event {\n"
61 " name = string;\n"
62 " fields := struct { string str; };\n"
63 "};\n";
64
65 int babeltrace_debug, babeltrace_verbose;
66
67 static uuid_t s_uuid;
68
69 static
70 void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
71 {
72 struct ctf_stream_pos dummy;
73
74 /* magic */
75 ctf_dummy_pos(pos, &dummy);
76 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
77 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
78 assert(!ctf_pos_packet(&dummy));
79
80 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
81 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
82 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
83
84 /* trace_uuid */
85 ctf_dummy_pos(pos, &dummy);
86 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
87 ctf_move_pos(&dummy, 16 * CHAR_BIT);
88 assert(!ctf_pos_packet(&dummy));
89
90 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
91 memcpy(ctf_get_pos_addr(pos), uuid, 16);
92 ctf_move_pos(pos, 16 * CHAR_BIT);
93 }
94
95 static
96 void write_packet_context(struct ctf_stream_pos *pos)
97 {
98 struct ctf_stream_pos dummy;
99
100 /* content_size */
101 ctf_dummy_pos(pos, &dummy);
102 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
103 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
104 assert(!ctf_pos_packet(&dummy));
105
106 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
107 *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */
108 pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos);
109 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
110
111 /* packet_size */
112 ctf_dummy_pos(pos, &dummy);
113 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
114 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
115 assert(!ctf_pos_packet(&dummy));
116
117 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
118 *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size;
119 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
120 }
121
122 static
123 void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
124 {
125 struct ctf_stream_pos dummy;
126 int attempt = 0;
127
128 printf_debug("read: %s\n", line);
129 retry:
130 ctf_dummy_pos(pos, &dummy);
131 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
132 ctf_move_pos(&dummy, len * CHAR_BIT);
133 if (ctf_pos_packet(&dummy)) {
134 /* TODO write content size */
135 ctf_pos_pad_packet(pos);
136 write_packet_header(pos, s_uuid);
137 write_packet_context(pos);
138 if (attempt++ == 1) {
139 fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n",
140 pos->packet_size / CHAR_BIT / 1024);
141 return;
142 }
143 goto retry;
144 }
145
146 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
147 memcpy(ctf_get_pos_addr(pos), line, len);
148 ctf_move_pos(pos, len * CHAR_BIT);
149 }
150
151 static
152 void trace_text(FILE *input, int output)
153 {
154 struct ctf_stream_pos pos;
155 ssize_t len;
156 char *line = NULL, *nl;
157 size_t linesize;
158
159 ctf_init_pos(&pos, output, O_RDWR);
160
161 write_packet_header(&pos, s_uuid);
162 write_packet_context(&pos);
163 for (;;) {
164 len = getline(&line, &linesize, input);
165 if (len < 0)
166 break;
167 nl = strrchr(line, '\n');
168 if (nl)
169 *nl = '\0';
170 trace_string(line, &pos, nl - line + 1);
171 }
172 ctf_fini_pos(&pos);
173 }
174
175 static void usage(FILE *fp)
176 {
177 fprintf(fp, "BabelTrace Log Converter %u.%u\n",
178 BABELTRACE_VERSION_MAJOR,
179 BABELTRACE_VERSION_MINOR);
180 fprintf(fp, "\n");
181 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
182 fprintf(fp, "\n");
183 fprintf(fp, "usage : babeltrace-log OUTPUT\n");
184 fprintf(fp, "\n");
185 fprintf(fp, " OUTPUT Output trace path\n");
186 fprintf(fp, "\n");
187 }
188
189 int main(int argc, char **argv)
190 {
191 int fd, ret;
192 char *outputname;
193 DIR *dir;
194 int dir_fd;
195
196 if (argc < 2) {
197 usage(stdout);
198 goto error;
199 }
200 outputname = argv[1];
201
202 ret = mkdir(outputname, S_IRWXU|S_IRWXG);
203 if (ret) {
204 perror("mkdir");
205 goto error;
206 }
207
208 dir = opendir(outputname);
209 if (!dir) {
210 perror("opendir");
211 goto error_rmdir;
212 }
213 dir_fd = dirfd(dir);
214 if (dir_fd < 0) {
215 perror("dirfd");
216 goto error_closedir;
217 }
218
219 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
220 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
221 if (fd < 0) {
222 perror("openat");
223 goto error_closedirfd;
224 }
225
226 trace_text(stdin, fd);
227
228 close(fd);
229 exit(EXIT_SUCCESS);
230
231 /* error handling */
232 error_closedirfd:
233 ret = close(dir_fd);
234 if (ret)
235 perror("close");
236 error_closedir:
237 ret = closedir(dir);
238 if (ret)
239 perror("closedir");
240 error_rmdir:
241 ret = rmdir(outputname);
242 if (ret)
243 perror("rmdir");
244 error:
245 exit(EXIT_FAILURE);
246 }
This page took 0.033768 seconds and 4 git commands to generate.