Tests: use templates for stream intersection and packet_seq_num
[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 *
c462e188
MD
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.
8c572eba
MD
27 */
28
90219914
MD
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <sys/mman.h>
8b6e9499 33#include <babeltrace/compat/dirent.h>
90219914 34#include <stdio.h>
989c73bc 35#include <stdlib.h>
90219914
MD
36#include <stdint.h>
37#include <unistd.h>
38#include <errno.h>
90219914 39#include <string.h>
fef3bf22 40#include <inttypes.h>
90219914 41
70bd0a12 42#include <babeltrace/babeltrace-internal.h>
8c572eba 43#include <babeltrace/ctf/types.h>
4cb26dfb 44#include <babeltrace/compat/uuid.h>
a386e246 45#include <babeltrace/compat/utc.h>
2ca0671b 46#include <babeltrace/compat/stdio.h>
43e34335 47#include <babeltrace/endian.h>
8c572eba 48
a386e246
SKC
49#define NSEC_PER_USEC 1000UL
50#define NSEC_PER_MSEC 1000000UL
51#define NSEC_PER_SEC 1000000000ULL
bfe09576 52#define USEC_PER_SEC 1000000UL
ca8b2b02 53
ca8b2b02
MD
54int babeltrace_debug, babeltrace_verbose;
55
56static char *s_outputname;
57static int s_timestamp;
39592eae 58static int s_help;
43e34335 59static unsigned char s_uuid[BABELTRACE_UUID_LEN];
ca8b2b02 60
b522ac18
MD
61/* Metadata format string */
62static const char metadata_fmt[] =
408a2c4d 63"/* CTF 1.8 */\n"
989c73bc
MD
64"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
65"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
fef3bf22 66"typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
989c73bc
MD
67"\n"
68"trace {\n"
c818559a
MD
69" major = %u;\n" /* major (e.g. 0) */
70" minor = %u;\n" /* minor (e.g. 1) */
b522ac18 71" uuid = \"%s\";\n" /* UUID */
989c73bc
MD
72" byte_order = %s;\n" /* be or le */
73" packet.header := struct {\n"
74" uint32_t magic;\n"
b4c19c1e 75" uint8_t uuid[16];\n"
989c73bc
MD
76" };\n"
77"};\n"
78"\n"
79"stream {\n"
80" packet.context := struct {\n"
fef3bf22
MD
81" uint64_t content_size;\n"
82" uint64_t packet_size;\n"
989c73bc 83" };\n"
ca8b2b02 84"%s" /* Stream event header (opt.) */
989c73bc
MD
85"};\n"
86"\n"
87"event {\n"
88" name = string;\n"
89" fields := struct { string str; };\n"
90"};\n";
91
ca8b2b02
MD
92static const char metadata_stream_event_header_timestamp[] =
93" typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
94" event.header := struct {\n"
95" uint64_t timestamp;\n"
96" };\n";
8c572eba 97
b522ac18
MD
98static
99void print_metadata(FILE *fp)
100{
a4dfa07b 101 char uuid_str[BABELTRACE_UUID_STR_LEN];
00f7fbf0
MD
102 unsigned int major = 0, minor = 0;
103 int ret;
b522ac18 104
00f7fbf0
MD
105 ret = sscanf(VERSION, "%u.%u", &major, &minor);
106 if (ret != 2)
107 fprintf(stderr, "[warning] Incorrect babeltrace version format\n.");
19dd40db 108 bt_uuid_unparse(s_uuid, uuid_str);
b522ac18 109 fprintf(fp, metadata_fmt,
00f7fbf0
MD
110 major,
111 minor,
b522ac18 112 uuid_str,
ca8b2b02
MD
113 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
114 s_timestamp ? metadata_stream_event_header_timestamp : "");
b522ac18
MD
115}
116
8c572eba 117static
43e34335 118void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid)
8c572eba 119{
46322b33 120 struct ctf_stream_pos dummy;
8c572eba
MD
121
122 /* magic */
46322b33 123 ctf_dummy_pos(pos, &dummy);
70fd5a51
MD
124 if (!ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
125 goto error;
126 if (!ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
127 goto error;
46322b33 128 assert(!ctf_pos_packet(&dummy));
d77e781b 129
70fd5a51
MD
130 if (!ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT))
131 goto error;
46322b33 132 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
70fd5a51
MD
133 if (!ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT))
134 goto error;
8c572eba 135
b4c19c1e 136 /* uuid */
46322b33 137 ctf_dummy_pos(pos, &dummy);
70fd5a51
MD
138 if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
139 goto error;
140 if (!ctf_move_pos(&dummy, 16 * CHAR_BIT))
141 goto error;
46322b33
MD
142 assert(!ctf_pos_packet(&dummy));
143
70fd5a51
MD
144 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
145 goto error;
43e34335 146 memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
70fd5a51
MD
147 if (!ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT))
148 goto error;
149 return;
150
151error:
152 fprintf(stderr, "[error] Out of packet bounds when writing packet header\n");
153 abort();
8c572eba
MD
154}
155
156static
46322b33 157void write_packet_context(struct ctf_stream_pos *pos)
8c572eba 158{
46322b33 159 struct ctf_stream_pos dummy;
8c572eba
MD
160
161 /* content_size */
46322b33 162 ctf_dummy_pos(pos, &dummy);
70fd5a51
MD
163 if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
164 goto error;
165 if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
166 goto error;
46322b33 167 assert(!ctf_pos_packet(&dummy));
d77e781b 168
70fd5a51
MD
169 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
170 goto error;
fef3bf22
MD
171 *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
172 pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
70fd5a51
MD
173 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
174 goto error;
8c572eba
MD
175
176 /* packet_size */
46322b33 177 ctf_dummy_pos(pos, &dummy);
70fd5a51
MD
178 if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
179 goto error;
180 if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
181 goto error;
46322b33 182 assert(!ctf_pos_packet(&dummy));
d77e781b 183
70fd5a51
MD
184 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
185 goto error;
fef3bf22 186 *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
70fd5a51
MD
187 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
188 goto error;
189 return;
190
191error:
192 fprintf(stderr, "[error] Out of packet bounds when writing packet context\n");
193 abort();
8c572eba
MD
194}
195
ca8b2b02
MD
196static
197void write_event_header(struct ctf_stream_pos *pos, char *line,
198 char **tline, size_t len, size_t *tlen,
199 uint64_t *ts)
200{
ca8b2b02
MD
201 if (!s_timestamp)
202 return;
203
204 /* Only need to be executed on first pass (dummy) */
a386e246
SKC
205 if (pos->dummy) {
206 int has_timestamp = 0;
207 unsigned long sec, usec, msec;
208 unsigned int year, mon, mday, hour, min;
08c82b90 209
ca8b2b02 210 /* Extract time from input line */
a386e246
SKC
211 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
212 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
213 /*
214 * Default CTF clock has 1GHz frequency. Convert
215 * from usec to nsec.
216 */
217 *ts *= NSEC_PER_USEC;
218 has_timestamp = 1;
219 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
220 &year, &mon, &mday, &hour, &min,
221 &sec, &msec) == 7) {
222 time_t ep_sec;
223 struct tm ti;
224
225 memset(&ti, 0, sizeof(ti));
226 ti.tm_year = year - 1900; /* from 1900 */
227 ti.tm_mon = mon - 1; /* 0 to 11 */
228 ti.tm_mday = mday;
229 ti.tm_hour = hour;
230 ti.tm_min = min;
231 ti.tm_sec = sec;
232
233 ep_sec = babeltrace_timegm(&ti);
234 if (ep_sec != (time_t) -1) {
235 *ts = (uint64_t) ep_sec * NSEC_PER_SEC
236 + (uint64_t) msec * NSEC_PER_MSEC;
237 }
238 has_timestamp = 1;
239 }
240 if (has_timestamp) {
ca8b2b02 241 *tline = strchr(line, ']');
bfe09576
MD
242 assert(*tline);
243 (*tline)++;
244 if ((*tline)[0] == ' ') {
ca8b2b02 245 (*tline)++;
bfe09576 246 }
ca8b2b02 247 *tlen = len + line - *tline;
ca8b2b02
MD
248 }
249 }
250 /* timestamp */
70fd5a51
MD
251 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
252 goto error;
ca8b2b02 253 if (!pos->dummy)
bc7eb6c8 254 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
70fd5a51
MD
255 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
256 goto error;
257 return;
258
259error:
260 fprintf(stderr, "[error] Out of packet bounds when writing event header\n");
261 abort();
ca8b2b02
MD
262}
263
8c572eba 264static
46322b33 265void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
8c572eba 266{
46322b33 267 struct ctf_stream_pos dummy;
8c572eba 268 int attempt = 0;
ca8b2b02
MD
269 char *tline = line; /* tline is start of text, after timestamp */
270 size_t tlen = len;
271 uint64_t ts = 0;
8c572eba
MD
272
273 printf_debug("read: %s\n", line);
08c82b90
MD
274
275 for (;;) {
185987b1
MD
276 int packet_filled = 0;
277
08c82b90
MD
278 ctf_dummy_pos(pos, &dummy);
279 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
70fd5a51 280 if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
185987b1 281 packet_filled = 1;
70fd5a51 282 if (!ctf_move_pos(&dummy, tlen * CHAR_BIT))
185987b1
MD
283 packet_filled = 1;
284 if (packet_filled || ctf_pos_packet(&dummy)) {
08c82b90
MD
285 ctf_pos_pad_packet(pos);
286 write_packet_header(pos, s_uuid);
287 write_packet_context(pos);
288 if (attempt++ == 1) {
289 fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
290 pos->packet_size / CHAR_BIT / 1024);
291 return;
292 }
293 continue;
294 } else {
295 break;
8c572eba 296 }
8c572eba
MD
297 }
298
ca8b2b02 299 write_event_header(pos, line, &tline, len, &tlen, &ts);
70fd5a51
MD
300 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
301 goto error;
ca8b2b02 302 memcpy(ctf_get_pos_addr(pos), tline, tlen);
70fd5a51
MD
303 if (!ctf_move_pos(pos, tlen * CHAR_BIT))
304 goto error;
305 return;
306
307error:
308 fprintf(stderr, "[error] Out of packet bounds when writing event payload\n");
309 abort();
8c572eba
MD
310}
311
312static
313void trace_text(FILE *input, int output)
314{
46322b33 315 struct ctf_stream_pos pos;
8c572eba
MD
316 ssize_t len;
317 char *line = NULL, *nl;
31058f28 318 size_t linesize = 0;
f824ae04 319 int ret;
8c572eba 320
89ca8829 321 memset(&pos, 0, sizeof(pos));
ca334c72 322 ret = ctf_init_pos(&pos, NULL, output, O_RDWR);
f824ae04
MD
323 if (ret) {
324 fprintf(stderr, "Error in ctf_init_pos\n");
325 return;
326 }
185987b1 327 ctf_packet_seek(&pos.parent, 0, SEEK_CUR);
8c572eba
MD
328 write_packet_header(&pos, s_uuid);
329 write_packet_context(&pos);
330 for (;;) {
2ca0671b 331 len = bt_getline(&line, &linesize, input);
8c572eba
MD
332 if (len < 0)
333 break;
334 nl = strrchr(line, '\n');
c5e6c71b 335 if (nl) {
8c572eba 336 *nl = '\0';
c5e6c71b
HZ
337 trace_string(line, &pos, nl - line + 1);
338 } else {
339 trace_string(line, &pos, strlen(line) + 1);
340 }
8c572eba 341 }
f824ae04
MD
342 ret = ctf_fini_pos(&pos);
343 if (ret) {
344 fprintf(stderr, "Error in ctf_fini_pos\n");
345 }
8c572eba 346}
90219914 347
ca8b2b02
MD
348static
349void usage(FILE *fp)
989c73bc 350{
00f7fbf0 351 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
989c73bc
MD
352 fprintf(fp, "\n");
353 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
354 fprintf(fp, "\n");
78893e6e 355 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
989c73bc
MD
356 fprintf(fp, "\n");
357 fprintf(fp, " OUTPUT Output trace path\n");
358 fprintf(fp, "\n");
78893e6e 359 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
a386e246 360 fprintf(fp, " (format: [YYYY-MM-DD HH:MM:SS.MS] string\\n)\n");
78893e6e 361 fprintf(fp, "\n");
989c73bc
MD
362}
363
ca8b2b02
MD
364static
365int parse_args(int argc, char **argv)
366{
367 int i;
368
369 for (i = 1; i < argc; i++) {
370 if (!strcmp(argv[i], "-t"))
371 s_timestamp = 1;
39592eae
MD
372 else if (!strcmp(argv[i], "-h")) {
373 s_help = 1;
374 return 0;
375 } else if (argv[i][0] == '-')
376 return -EINVAL;
ca8b2b02
MD
377 else
378 s_outputname = argv[i];
379 }
380 if (!s_outputname)
381 return -EINVAL;
382 return 0;
383}
384
90219914
MD
385int main(int argc, char **argv)
386{
b522ac18 387 int fd, metadata_fd, ret;
989c73bc
MD
388 DIR *dir;
389 int dir_fd;
b522ac18 390 FILE *metadata_fp;
90219914 391
ca8b2b02
MD
392 ret = parse_args(argc, argv);
393 if (ret) {
3394d22e
MD
394 fprintf(stderr, "Error: invalid argument.\n");
395 usage(stderr);
989c73bc 396 goto error;
90219914 397 }
90219914 398
39592eae
MD
399 if (s_help) {
400 usage(stdout);
401 exit(EXIT_SUCCESS);
402 }
403
ca8b2b02 404 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
90219914 405 if (ret) {
989c73bc
MD
406 perror("mkdir");
407 goto error;
408 }
409
ca8b2b02 410 dir = opendir(s_outputname);
989c73bc
MD
411 if (!dir) {
412 perror("opendir");
413 goto error_rmdir;
414 }
8b6e9499 415 dir_fd = bt_dirfd(dir);
989c73bc
MD
416 if (dir_fd < 0) {
417 perror("dirfd");
418 goto error_closedir;
419 }
420
421 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
422 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
423 if (fd < 0) {
424 perror("openat");
425 goto error_closedirfd;
90219914 426 }
90219914 427
b522ac18
MD
428 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
429 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
9396a982 430 if (metadata_fd < 0) {
b522ac18
MD
431 perror("openat");
432 goto error_closedatastream;
433 }
434 metadata_fp = fdopen(metadata_fd, "w");
435 if (!metadata_fp) {
436 perror("fdopen");
437 goto error_closemetadatafd;
438 }
439
19dd40db 440 bt_uuid_generate(s_uuid);
b522ac18 441 print_metadata(metadata_fp);
8c572eba 442 trace_text(stdin, fd);
989c73bc 443
f824ae04
MD
444 ret = close(fd);
445 if (ret)
446 perror("close");
989c73bc
MD
447 exit(EXIT_SUCCESS);
448
449 /* error handling */
b522ac18
MD
450error_closemetadatafd:
451 ret = close(metadata_fd);
452 if (ret)
453 perror("close");
454error_closedatastream:
455 ret = close(fd);
456 if (ret)
457 perror("close");
989c73bc
MD
458error_closedirfd:
459 ret = close(dir_fd);
460 if (ret)
461 perror("close");
462error_closedir:
463 ret = closedir(dir);
464 if (ret)
465 perror("closedir");
466error_rmdir:
ca8b2b02 467 ret = rmdir(s_outputname);
989c73bc
MD
468 if (ret)
469 perror("rmdir");
470error:
471 exit(EXIT_FAILURE);
90219914 472}
This page took 0.057869 seconds and 4 git commands to generate.