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