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