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