Add missing permission notice in each source file
[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>
a4dfa07b 48#include <babeltrace/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
MD
122 ctf_dummy_pos(pos, &dummy);
123 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
124 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
125 assert(!ctf_pos_packet(&dummy));
8c572eba 126
46322b33
MD
127 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
128 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
129 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba 130
b4c19c1e 131 /* uuid */
46322b33
MD
132 ctf_dummy_pos(pos, &dummy);
133 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
134 ctf_move_pos(&dummy, 16 * CHAR_BIT);
135 assert(!ctf_pos_packet(&dummy));
136
137 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
43e34335
MD
138 memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
139 ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT);
8c572eba
MD
140}
141
142static
46322b33 143void write_packet_context(struct ctf_stream_pos *pos)
8c572eba 144{
46322b33 145 struct ctf_stream_pos dummy;
8c572eba
MD
146
147 /* content_size */
46322b33 148 ctf_dummy_pos(pos, &dummy);
fef3bf22
MD
149 ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
150 ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
46322b33 151 assert(!ctf_pos_packet(&dummy));
8c572eba 152
fef3bf22
MD
153 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
154 *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
155 pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
156 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
8c572eba
MD
157
158 /* packet_size */
46322b33 159 ctf_dummy_pos(pos, &dummy);
fef3bf22
MD
160 ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
161 ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
46322b33 162 assert(!ctf_pos_packet(&dummy));
8c572eba 163
fef3bf22
MD
164 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
165 *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
166 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
8c572eba
MD
167}
168
ca8b2b02
MD
169static
170void write_event_header(struct ctf_stream_pos *pos, char *line,
171 char **tline, size_t len, size_t *tlen,
172 uint64_t *ts)
173{
bfe09576 174 unsigned long sec, usec;
ca8b2b02
MD
175
176 if (!s_timestamp)
177 return;
178
179 /* Only need to be executed on first pass (dummy) */
180 if (pos->dummy) {
08c82b90
MD
181 int ret;
182
ca8b2b02 183 /* Extract time from input line */
bfe09576 184 ret = sscanf(line, "[%lu.%lu] ", &sec, &usec);
ca8b2b02
MD
185 if (ret == 2) {
186 *tline = strchr(line, ']');
bfe09576
MD
187 assert(*tline);
188 (*tline)++;
189 if ((*tline)[0] == ' ') {
ca8b2b02 190 (*tline)++;
bfe09576 191 }
ca8b2b02 192 *tlen = len + line - *tline;
bfe09576 193 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
ca8b2b02
MD
194 }
195 }
196 /* timestamp */
197 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
198 if (!pos->dummy)
bc7eb6c8 199 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
ca8b2b02
MD
200 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
201}
202
8c572eba 203static
46322b33 204void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
8c572eba 205{
46322b33 206 struct ctf_stream_pos dummy;
8c572eba 207 int attempt = 0;
ca8b2b02
MD
208 char *tline = line; /* tline is start of text, after timestamp */
209 size_t tlen = len;
210 uint64_t ts = 0;
8c572eba
MD
211
212 printf_debug("read: %s\n", line);
08c82b90
MD
213
214 for (;;) {
215 ctf_dummy_pos(pos, &dummy);
216 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
217 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
218 ctf_move_pos(&dummy, tlen * CHAR_BIT);
219 if (ctf_pos_packet(&dummy)) {
220 ctf_pos_pad_packet(pos);
221 write_packet_header(pos, s_uuid);
222 write_packet_context(pos);
223 if (attempt++ == 1) {
224 fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
225 pos->packet_size / CHAR_BIT / 1024);
226 return;
227 }
228 continue;
229 } else {
230 break;
8c572eba 231 }
8c572eba
MD
232 }
233
ca8b2b02 234 write_event_header(pos, line, &tline, len, &tlen, &ts);
46322b33 235 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
ca8b2b02
MD
236 memcpy(ctf_get_pos_addr(pos), tline, tlen);
237 ctf_move_pos(pos, tlen * CHAR_BIT);
8c572eba
MD
238}
239
240static
241void trace_text(FILE *input, int output)
242{
46322b33 243 struct ctf_stream_pos pos;
8c572eba
MD
244 ssize_t len;
245 char *line = NULL, *nl;
246 size_t linesize;
f824ae04 247 int ret;
8c572eba 248
89ca8829 249 memset(&pos, 0, sizeof(pos));
f824ae04
MD
250 ret = ctf_init_pos(&pos, output, O_RDWR);
251 if (ret) {
252 fprintf(stderr, "Error in ctf_init_pos\n");
253 return;
254 }
8c572eba
MD
255 write_packet_header(&pos, s_uuid);
256 write_packet_context(&pos);
257 for (;;) {
258 len = getline(&line, &linesize, input);
259 if (len < 0)
260 break;
261 nl = strrchr(line, '\n');
c5e6c71b 262 if (nl) {
8c572eba 263 *nl = '\0';
c5e6c71b
HZ
264 trace_string(line, &pos, nl - line + 1);
265 } else {
266 trace_string(line, &pos, strlen(line) + 1);
267 }
8c572eba 268 }
f824ae04
MD
269 ret = ctf_fini_pos(&pos);
270 if (ret) {
271 fprintf(stderr, "Error in ctf_fini_pos\n");
272 }
8c572eba 273}
90219914 274
ca8b2b02
MD
275static
276void usage(FILE *fp)
989c73bc 277{
00f7fbf0 278 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
989c73bc
MD
279 fprintf(fp, "\n");
280 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
281 fprintf(fp, "\n");
78893e6e 282 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
989c73bc
MD
283 fprintf(fp, "\n");
284 fprintf(fp, " OUTPUT Output trace path\n");
285 fprintf(fp, "\n");
78893e6e
MD
286 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
287 fprintf(fp, "\n");
989c73bc
MD
288}
289
ca8b2b02
MD
290static
291int parse_args(int argc, char **argv)
292{
293 int i;
294
295 for (i = 1; i < argc; i++) {
296 if (!strcmp(argv[i], "-t"))
297 s_timestamp = 1;
39592eae
MD
298 else if (!strcmp(argv[i], "-h")) {
299 s_help = 1;
300 return 0;
301 } else if (argv[i][0] == '-')
302 return -EINVAL;
ca8b2b02
MD
303 else
304 s_outputname = argv[i];
305 }
306 if (!s_outputname)
307 return -EINVAL;
308 return 0;
309}
310
90219914
MD
311int main(int argc, char **argv)
312{
b522ac18 313 int fd, metadata_fd, ret;
989c73bc
MD
314 DIR *dir;
315 int dir_fd;
b522ac18 316 FILE *metadata_fp;
90219914 317
ca8b2b02
MD
318 ret = parse_args(argc, argv);
319 if (ret) {
3394d22e
MD
320 fprintf(stderr, "Error: invalid argument.\n");
321 usage(stderr);
989c73bc 322 goto error;
90219914 323 }
90219914 324
39592eae
MD
325 if (s_help) {
326 usage(stdout);
327 exit(EXIT_SUCCESS);
328 }
329
ca8b2b02 330 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
90219914 331 if (ret) {
989c73bc
MD
332 perror("mkdir");
333 goto error;
334 }
335
ca8b2b02 336 dir = opendir(s_outputname);
989c73bc
MD
337 if (!dir) {
338 perror("opendir");
339 goto error_rmdir;
340 }
341 dir_fd = dirfd(dir);
342 if (dir_fd < 0) {
343 perror("dirfd");
344 goto error_closedir;
345 }
346
347 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
348 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
349 if (fd < 0) {
350 perror("openat");
351 goto error_closedirfd;
90219914 352 }
90219914 353
b522ac18
MD
354 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
355 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
356 if (fd < 0) {
357 perror("openat");
358 goto error_closedatastream;
359 }
360 metadata_fp = fdopen(metadata_fd, "w");
361 if (!metadata_fp) {
362 perror("fdopen");
363 goto error_closemetadatafd;
364 }
365
43e34335 366 babeltrace_uuid_generate(s_uuid);
b522ac18 367 print_metadata(metadata_fp);
8c572eba 368 trace_text(stdin, fd);
989c73bc 369
f824ae04
MD
370 ret = close(fd);
371 if (ret)
372 perror("close");
989c73bc
MD
373 exit(EXIT_SUCCESS);
374
375 /* error handling */
b522ac18
MD
376error_closemetadatafd:
377 ret = close(metadata_fd);
378 if (ret)
379 perror("close");
380error_closedatastream:
381 ret = close(fd);
382 if (ret)
383 perror("close");
989c73bc
MD
384error_closedirfd:
385 ret = close(dir_fd);
386 if (ret)
387 perror("close");
388error_closedir:
389 ret = closedir(dir);
390 if (ret)
391 perror("closedir");
392error_rmdir:
ca8b2b02 393 ret = rmdir(s_outputname);
989c73bc
MD
394 if (ret)
395 perror("rmdir");
396error:
397 exit(EXIT_FAILURE);
90219914 398}
This page took 0.068941 seconds and 4 git commands to generate.