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