Adding a test which do a sequence of seek BEGIN, LAST, BEGIN, LAST
[babeltrace.git] / converter / babeltrace-log.c
CommitLineData
8c572eba 1/*
b522ac18 2 * babeltrace-log.c
8c572eba 3 *
b522ac18 4 * BabelTrace - Convert Text Log to CTF
8c572eba 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8c572eba
MD
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 * Depends on glibc 2.10 for getline().
21 */
22
c696b1b1 23#define _GNU_SOURCE
00f7fbf0 24#include <config.h>
90219914
MD
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <sys/mman.h>
989c73bc 29#include <dirent.h>
90219914 30#include <stdio.h>
989c73bc 31#include <stdlib.h>
90219914
MD
32#include <stdint.h>
33#include <unistd.h>
34#include <errno.h>
90219914 35#include <string.h>
fef3bf22 36#include <inttypes.h>
90219914 37
70bd0a12 38#include <babeltrace/babeltrace-internal.h>
8c572eba 39#include <babeltrace/ctf/types.h>
a4dfa07b 40#include <babeltrace/uuid.h>
43e34335 41#include <babeltrace/endian.h>
8c572eba 42
bfe09576 43#define USEC_PER_SEC 1000000UL
ca8b2b02 44
ca8b2b02
MD
45int babeltrace_debug, babeltrace_verbose;
46
47static char *s_outputname;
48static int s_timestamp;
39592eae 49static int s_help;
43e34335 50static unsigned char s_uuid[BABELTRACE_UUID_LEN];
ca8b2b02 51
b522ac18
MD
52/* Metadata format string */
53static const char metadata_fmt[] =
408a2c4d 54"/* CTF 1.8 */\n"
989c73bc
MD
55"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
56"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
fef3bf22 57"typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
989c73bc
MD
58"\n"
59"trace {\n"
c818559a
MD
60" major = %u;\n" /* major (e.g. 0) */
61" minor = %u;\n" /* minor (e.g. 1) */
b522ac18 62" uuid = \"%s\";\n" /* UUID */
989c73bc
MD
63" byte_order = %s;\n" /* be or le */
64" packet.header := struct {\n"
65" uint32_t magic;\n"
b4c19c1e 66" uint8_t uuid[16];\n"
989c73bc
MD
67" };\n"
68"};\n"
69"\n"
70"stream {\n"
71" packet.context := struct {\n"
fef3bf22
MD
72" uint64_t content_size;\n"
73" uint64_t packet_size;\n"
989c73bc 74" };\n"
ca8b2b02 75"%s" /* Stream event header (opt.) */
989c73bc
MD
76"};\n"
77"\n"
78"event {\n"
79" name = string;\n"
80" fields := struct { string str; };\n"
81"};\n";
82
ca8b2b02
MD
83static const char metadata_stream_event_header_timestamp[] =
84" typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
85" event.header := struct {\n"
86" uint64_t timestamp;\n"
87" };\n";
8c572eba 88
b522ac18
MD
89static
90void print_metadata(FILE *fp)
91{
a4dfa07b 92 char uuid_str[BABELTRACE_UUID_STR_LEN];
00f7fbf0
MD
93 unsigned int major = 0, minor = 0;
94 int ret;
b522ac18 95
00f7fbf0
MD
96 ret = sscanf(VERSION, "%u.%u", &major, &minor);
97 if (ret != 2)
98 fprintf(stderr, "[warning] Incorrect babeltrace version format\n.");
a4dfa07b 99 babeltrace_uuid_unparse(s_uuid, uuid_str);
b522ac18 100 fprintf(fp, metadata_fmt,
00f7fbf0
MD
101 major,
102 minor,
b522ac18 103 uuid_str,
ca8b2b02
MD
104 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
105 s_timestamp ? metadata_stream_event_header_timestamp : "");
b522ac18
MD
106}
107
8c572eba 108static
43e34335 109void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid)
8c572eba 110{
46322b33 111 struct ctf_stream_pos dummy;
8c572eba
MD
112
113 /* magic */
46322b33
MD
114 ctf_dummy_pos(pos, &dummy);
115 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
116 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
117 assert(!ctf_pos_packet(&dummy));
8c572eba 118
46322b33
MD
119 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
120 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
121 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba 122
b4c19c1e 123 /* uuid */
46322b33
MD
124 ctf_dummy_pos(pos, &dummy);
125 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
126 ctf_move_pos(&dummy, 16 * CHAR_BIT);
127 assert(!ctf_pos_packet(&dummy));
128
129 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
43e34335
MD
130 memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
131 ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT);
8c572eba
MD
132}
133
134static
46322b33 135void write_packet_context(struct ctf_stream_pos *pos)
8c572eba 136{
46322b33 137 struct ctf_stream_pos dummy;
8c572eba
MD
138
139 /* content_size */
46322b33 140 ctf_dummy_pos(pos, &dummy);
fef3bf22
MD
141 ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
142 ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
46322b33 143 assert(!ctf_pos_packet(&dummy));
8c572eba 144
fef3bf22
MD
145 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
146 *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
147 pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
148 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
8c572eba
MD
149
150 /* packet_size */
46322b33 151 ctf_dummy_pos(pos, &dummy);
fef3bf22
MD
152 ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
153 ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
46322b33 154 assert(!ctf_pos_packet(&dummy));
8c572eba 155
fef3bf22
MD
156 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
157 *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
158 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
8c572eba
MD
159}
160
ca8b2b02
MD
161static
162void write_event_header(struct ctf_stream_pos *pos, char *line,
163 char **tline, size_t len, size_t *tlen,
164 uint64_t *ts)
165{
bfe09576 166 unsigned long sec, usec;
ca8b2b02
MD
167
168 if (!s_timestamp)
169 return;
170
171 /* Only need to be executed on first pass (dummy) */
172 if (pos->dummy) {
08c82b90
MD
173 int ret;
174
ca8b2b02 175 /* Extract time from input line */
bfe09576 176 ret = sscanf(line, "[%lu.%lu] ", &sec, &usec);
ca8b2b02
MD
177 if (ret == 2) {
178 *tline = strchr(line, ']');
bfe09576
MD
179 assert(*tline);
180 (*tline)++;
181 if ((*tline)[0] == ' ') {
ca8b2b02 182 (*tline)++;
bfe09576 183 }
ca8b2b02 184 *tlen = len + line - *tline;
bfe09576 185 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
ca8b2b02
MD
186 }
187 }
188 /* timestamp */
189 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
190 if (!pos->dummy)
bc7eb6c8 191 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
ca8b2b02
MD
192 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
193}
194
8c572eba 195static
46322b33 196void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
8c572eba 197{
46322b33 198 struct ctf_stream_pos dummy;
8c572eba 199 int attempt = 0;
ca8b2b02
MD
200 char *tline = line; /* tline is start of text, after timestamp */
201 size_t tlen = len;
202 uint64_t ts = 0;
8c572eba
MD
203
204 printf_debug("read: %s\n", line);
08c82b90
MD
205
206 for (;;) {
207 ctf_dummy_pos(pos, &dummy);
208 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
209 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
210 ctf_move_pos(&dummy, tlen * CHAR_BIT);
211 if (ctf_pos_packet(&dummy)) {
212 ctf_pos_pad_packet(pos);
213 write_packet_header(pos, s_uuid);
214 write_packet_context(pos);
215 if (attempt++ == 1) {
216 fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
217 pos->packet_size / CHAR_BIT / 1024);
218 return;
219 }
220 continue;
221 } else {
222 break;
8c572eba 223 }
8c572eba
MD
224 }
225
ca8b2b02 226 write_event_header(pos, line, &tline, len, &tlen, &ts);
46322b33 227 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
ca8b2b02
MD
228 memcpy(ctf_get_pos_addr(pos), tline, tlen);
229 ctf_move_pos(pos, tlen * CHAR_BIT);
8c572eba
MD
230}
231
232static
233void trace_text(FILE *input, int output)
234{
46322b33 235 struct ctf_stream_pos pos;
8c572eba
MD
236 ssize_t len;
237 char *line = NULL, *nl;
238 size_t linesize;
f824ae04 239 int ret;
8c572eba 240
89ca8829 241 memset(&pos, 0, sizeof(pos));
f824ae04
MD
242 ret = ctf_init_pos(&pos, output, O_RDWR);
243 if (ret) {
244 fprintf(stderr, "Error in ctf_init_pos\n");
245 return;
246 }
8c572eba
MD
247 write_packet_header(&pos, s_uuid);
248 write_packet_context(&pos);
249 for (;;) {
250 len = getline(&line, &linesize, input);
251 if (len < 0)
252 break;
253 nl = strrchr(line, '\n');
c5e6c71b 254 if (nl) {
8c572eba 255 *nl = '\0';
c5e6c71b
HZ
256 trace_string(line, &pos, nl - line + 1);
257 } else {
258 trace_string(line, &pos, strlen(line) + 1);
259 }
8c572eba 260 }
f824ae04
MD
261 ret = ctf_fini_pos(&pos);
262 if (ret) {
263 fprintf(stderr, "Error in ctf_fini_pos\n");
264 }
8c572eba 265}
90219914 266
ca8b2b02
MD
267static
268void usage(FILE *fp)
989c73bc 269{
00f7fbf0 270 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
989c73bc
MD
271 fprintf(fp, "\n");
272 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
273 fprintf(fp, "\n");
78893e6e 274 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
989c73bc
MD
275 fprintf(fp, "\n");
276 fprintf(fp, " OUTPUT Output trace path\n");
277 fprintf(fp, "\n");
78893e6e
MD
278 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
279 fprintf(fp, "\n");
989c73bc
MD
280}
281
ca8b2b02
MD
282static
283int parse_args(int argc, char **argv)
284{
285 int i;
286
287 for (i = 1; i < argc; i++) {
288 if (!strcmp(argv[i], "-t"))
289 s_timestamp = 1;
39592eae
MD
290 else if (!strcmp(argv[i], "-h")) {
291 s_help = 1;
292 return 0;
293 } else if (argv[i][0] == '-')
294 return -EINVAL;
ca8b2b02
MD
295 else
296 s_outputname = argv[i];
297 }
298 if (!s_outputname)
299 return -EINVAL;
300 return 0;
301}
302
90219914
MD
303int main(int argc, char **argv)
304{
b522ac18 305 int fd, metadata_fd, ret;
989c73bc
MD
306 DIR *dir;
307 int dir_fd;
b522ac18 308 FILE *metadata_fp;
90219914 309
ca8b2b02
MD
310 ret = parse_args(argc, argv);
311 if (ret) {
3394d22e
MD
312 fprintf(stderr, "Error: invalid argument.\n");
313 usage(stderr);
989c73bc 314 goto error;
90219914 315 }
90219914 316
39592eae
MD
317 if (s_help) {
318 usage(stdout);
319 exit(EXIT_SUCCESS);
320 }
321
ca8b2b02 322 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
90219914 323 if (ret) {
989c73bc
MD
324 perror("mkdir");
325 goto error;
326 }
327
ca8b2b02 328 dir = opendir(s_outputname);
989c73bc
MD
329 if (!dir) {
330 perror("opendir");
331 goto error_rmdir;
332 }
333 dir_fd = dirfd(dir);
334 if (dir_fd < 0) {
335 perror("dirfd");
336 goto error_closedir;
337 }
338
339 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
340 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
341 if (fd < 0) {
342 perror("openat");
343 goto error_closedirfd;
90219914 344 }
90219914 345
b522ac18
MD
346 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
347 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
348 if (fd < 0) {
349 perror("openat");
350 goto error_closedatastream;
351 }
352 metadata_fp = fdopen(metadata_fd, "w");
353 if (!metadata_fp) {
354 perror("fdopen");
355 goto error_closemetadatafd;
356 }
357
43e34335 358 babeltrace_uuid_generate(s_uuid);
b522ac18 359 print_metadata(metadata_fp);
8c572eba 360 trace_text(stdin, fd);
989c73bc 361
f824ae04
MD
362 ret = close(fd);
363 if (ret)
364 perror("close");
989c73bc
MD
365 exit(EXIT_SUCCESS);
366
367 /* error handling */
b522ac18
MD
368error_closemetadatafd:
369 ret = close(metadata_fd);
370 if (ret)
371 perror("close");
372error_closedatastream:
373 ret = close(fd);
374 if (ret)
375 perror("close");
989c73bc
MD
376error_closedirfd:
377 ret = close(dir_fd);
378 if (ret)
379 perror("close");
380error_closedir:
381 ret = closedir(dir);
382 if (ret)
383 perror("closedir");
384error_rmdir:
ca8b2b02 385 ret = rmdir(s_outputname);
989c73bc
MD
386 if (ret)
387 perror("rmdir");
388error:
389 exit(EXIT_FAILURE);
90219914 390}
This page took 0.043468 seconds and 4 git commands to generate.