fix babeltrace-log error checking
[babeltrace.git] / converter / babeltrace-log.c
1 /*
2 * babeltrace-log.c
3 *
4 * BabelTrace - Convert Text Log to CTF
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 *
28 * Depends on glibc 2.10 for getline().
29 */
30
31 #define _GNU_SOURCE
32 #include <config.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <sys/mman.h>
37 #include <dirent.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdint.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <inttypes.h>
45
46 #include <babeltrace/babeltrace-internal.h>
47 #include <babeltrace/ctf/types.h>
48 #include <babeltrace/compat/uuid.h>
49 #include <babeltrace/endian.h>
50
51 #define USEC_PER_SEC 1000000UL
52
53 int babeltrace_debug, babeltrace_verbose;
54
55 static char *s_outputname;
56 static int s_timestamp;
57 static int s_help;
58 static unsigned char s_uuid[BABELTRACE_UUID_LEN];
59
60 /* Metadata format string */
61 static const char metadata_fmt[] =
62 "/* CTF 1.8 */\n"
63 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
64 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
65 "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
66 "\n"
67 "trace {\n"
68 " major = %u;\n" /* major (e.g. 0) */
69 " minor = %u;\n" /* minor (e.g. 1) */
70 " uuid = \"%s\";\n" /* UUID */
71 " byte_order = %s;\n" /* be or le */
72 " packet.header := struct {\n"
73 " uint32_t magic;\n"
74 " uint8_t uuid[16];\n"
75 " };\n"
76 "};\n"
77 "\n"
78 "stream {\n"
79 " packet.context := struct {\n"
80 " uint64_t content_size;\n"
81 " uint64_t packet_size;\n"
82 " };\n"
83 "%s" /* Stream event header (opt.) */
84 "};\n"
85 "\n"
86 "event {\n"
87 " name = string;\n"
88 " fields := struct { string str; };\n"
89 "};\n";
90
91 static const char metadata_stream_event_header_timestamp[] =
92 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
93 " event.header := struct {\n"
94 " uint64_t timestamp;\n"
95 " };\n";
96
97 static
98 void print_metadata(FILE *fp)
99 {
100 char uuid_str[BABELTRACE_UUID_STR_LEN];
101 unsigned int major = 0, minor = 0;
102 int ret;
103
104 ret = sscanf(VERSION, "%u.%u", &major, &minor);
105 if (ret != 2)
106 fprintf(stderr, "[warning] Incorrect babeltrace version format\n.");
107 babeltrace_uuid_unparse(s_uuid, uuid_str);
108 fprintf(fp, metadata_fmt,
109 major,
110 minor,
111 uuid_str,
112 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
113 s_timestamp ? metadata_stream_event_header_timestamp : "");
114 }
115
116 static
117 void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid)
118 {
119 struct ctf_stream_pos dummy;
120
121 /* magic */
122 ctf_dummy_pos(pos, &dummy);
123 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
124 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
125 assert(!ctf_pos_packet(&dummy));
126
127 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
128 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
129 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
130
131 /* uuid */
132 ctf_dummy_pos(pos, &dummy);
133 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
134 ctf_move_pos(&dummy, 16 * CHAR_BIT);
135 assert(!ctf_pos_packet(&dummy));
136
137 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
138 memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
139 ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT);
140 }
141
142 static
143 void write_packet_context(struct ctf_stream_pos *pos)
144 {
145 struct ctf_stream_pos dummy;
146
147 /* content_size */
148 ctf_dummy_pos(pos, &dummy);
149 ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
150 ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
151 assert(!ctf_pos_packet(&dummy));
152
153 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
154 *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
155 pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
156 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
157
158 /* packet_size */
159 ctf_dummy_pos(pos, &dummy);
160 ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
161 ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
162 assert(!ctf_pos_packet(&dummy));
163
164 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
165 *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
166 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
167 }
168
169 static
170 void write_event_header(struct ctf_stream_pos *pos, char *line,
171 char **tline, size_t len, size_t *tlen,
172 uint64_t *ts)
173 {
174 unsigned long sec, usec;
175
176 if (!s_timestamp)
177 return;
178
179 /* Only need to be executed on first pass (dummy) */
180 if (pos->dummy) {
181 int ret;
182
183 /* Extract time from input line */
184 ret = sscanf(line, "[%lu.%lu] ", &sec, &usec);
185 if (ret == 2) {
186 *tline = strchr(line, ']');
187 assert(*tline);
188 (*tline)++;
189 if ((*tline)[0] == ' ') {
190 (*tline)++;
191 }
192 *tlen = len + line - *tline;
193 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
194 /*
195 * Default CTF clock has 1GHz frequency. Convert
196 * from usec to nsec.
197 */
198 *ts *= 1000;
199 }
200 }
201 /* timestamp */
202 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
203 if (!pos->dummy)
204 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
205 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
206 }
207
208 static
209 void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
210 {
211 struct ctf_stream_pos dummy;
212 int attempt = 0;
213 char *tline = line; /* tline is start of text, after timestamp */
214 size_t tlen = len;
215 uint64_t ts = 0;
216
217 printf_debug("read: %s\n", line);
218
219 for (;;) {
220 ctf_dummy_pos(pos, &dummy);
221 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
222 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
223 ctf_move_pos(&dummy, tlen * CHAR_BIT);
224 if (ctf_pos_packet(&dummy)) {
225 ctf_pos_pad_packet(pos);
226 write_packet_header(pos, s_uuid);
227 write_packet_context(pos);
228 if (attempt++ == 1) {
229 fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
230 pos->packet_size / CHAR_BIT / 1024);
231 return;
232 }
233 continue;
234 } else {
235 break;
236 }
237 }
238
239 write_event_header(pos, line, &tline, len, &tlen, &ts);
240 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
241 memcpy(ctf_get_pos_addr(pos), tline, tlen);
242 ctf_move_pos(pos, tlen * CHAR_BIT);
243 }
244
245 static
246 void trace_text(FILE *input, int output)
247 {
248 struct ctf_stream_pos pos;
249 ssize_t len;
250 char *line = NULL, *nl;
251 size_t linesize;
252 int ret;
253
254 memset(&pos, 0, sizeof(pos));
255 ret = ctf_init_pos(&pos, NULL, output, O_RDWR);
256 if (ret) {
257 fprintf(stderr, "Error in ctf_init_pos\n");
258 return;
259 }
260 write_packet_header(&pos, s_uuid);
261 write_packet_context(&pos);
262 for (;;) {
263 len = getline(&line, &linesize, input);
264 if (len < 0)
265 break;
266 nl = strrchr(line, '\n');
267 if (nl) {
268 *nl = '\0';
269 trace_string(line, &pos, nl - line + 1);
270 } else {
271 trace_string(line, &pos, strlen(line) + 1);
272 }
273 }
274 ret = ctf_fini_pos(&pos);
275 if (ret) {
276 fprintf(stderr, "Error in ctf_fini_pos\n");
277 }
278 }
279
280 static
281 void usage(FILE *fp)
282 {
283 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
284 fprintf(fp, "\n");
285 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
286 fprintf(fp, "\n");
287 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
288 fprintf(fp, "\n");
289 fprintf(fp, " OUTPUT Output trace path\n");
290 fprintf(fp, "\n");
291 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
292 fprintf(fp, "\n");
293 }
294
295 static
296 int parse_args(int argc, char **argv)
297 {
298 int i;
299
300 for (i = 1; i < argc; i++) {
301 if (!strcmp(argv[i], "-t"))
302 s_timestamp = 1;
303 else if (!strcmp(argv[i], "-h")) {
304 s_help = 1;
305 return 0;
306 } else if (argv[i][0] == '-')
307 return -EINVAL;
308 else
309 s_outputname = argv[i];
310 }
311 if (!s_outputname)
312 return -EINVAL;
313 return 0;
314 }
315
316 int main(int argc, char **argv)
317 {
318 int fd, metadata_fd, ret;
319 DIR *dir;
320 int dir_fd;
321 FILE *metadata_fp;
322
323 ret = parse_args(argc, argv);
324 if (ret) {
325 fprintf(stderr, "Error: invalid argument.\n");
326 usage(stderr);
327 goto error;
328 }
329
330 if (s_help) {
331 usage(stdout);
332 exit(EXIT_SUCCESS);
333 }
334
335 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
336 if (ret) {
337 perror("mkdir");
338 goto error;
339 }
340
341 dir = opendir(s_outputname);
342 if (!dir) {
343 perror("opendir");
344 goto error_rmdir;
345 }
346 dir_fd = dirfd(dir);
347 if (dir_fd < 0) {
348 perror("dirfd");
349 goto error_closedir;
350 }
351
352 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
353 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
354 if (fd < 0) {
355 perror("openat");
356 goto error_closedirfd;
357 }
358
359 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
360 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
361 if (metadata_fd < 0) {
362 perror("openat");
363 goto error_closedatastream;
364 }
365 metadata_fp = fdopen(metadata_fd, "w");
366 if (!metadata_fp) {
367 perror("fdopen");
368 goto error_closemetadatafd;
369 }
370
371 babeltrace_uuid_generate(s_uuid);
372 print_metadata(metadata_fp);
373 trace_text(stdin, fd);
374
375 ret = close(fd);
376 if (ret)
377 perror("close");
378 exit(EXIT_SUCCESS);
379
380 /* error handling */
381 error_closemetadatafd:
382 ret = close(metadata_fd);
383 if (ret)
384 perror("close");
385 error_closedatastream:
386 ret = close(fd);
387 if (ret)
388 perror("close");
389 error_closedirfd:
390 ret = close(dir_fd);
391 if (ret)
392 perror("close");
393 error_closedir:
394 ret = closedir(dir);
395 if (ret)
396 perror("closedir");
397 error_rmdir:
398 ret = rmdir(s_outputname);
399 if (ret)
400 perror("rmdir");
401 error:
402 exit(EXIT_FAILURE);
403 }
This page took 0.037737 seconds and 5 git commands to generate.