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