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