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