Fix: libbabeltrace lib build dependencies
[babeltrace.git] / converter / babeltrace-log.c
... / ...
CommitLineData
1/*
2 * babeltrace-log.c
3 *
4 * BabelTrace - Convert Text Log to CTF
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 * 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 *
28 * Depends on glibc 2.10 for getline().
29 */
30
31#define _GNU_SOURCE
32#include <config.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <sys/mman.h>
37#include <dirent.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <stdint.h>
41#include <unistd.h>
42#include <errno.h>
43#include <string.h>
44#include <inttypes.h>
45
46#include <babeltrace/babeltrace-internal.h>
47#include <babeltrace/ctf/types.h>
48#include <babeltrace/compat/uuid.h>
49#include <babeltrace/endian.h>
50
51#define USEC_PER_SEC 1000000UL
52
53int babeltrace_debug, babeltrace_verbose;
54
55static char *s_outputname;
56static int s_timestamp;
57static int s_help;
58static unsigned char s_uuid[BABELTRACE_UUID_LEN];
59
60/* Metadata format string */
61static const char metadata_fmt[] =
62"/* CTF 1.8 */\n"
63"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
64"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
65"typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
66"\n"
67"trace {\n"
68" major = %u;\n" /* major (e.g. 0) */
69" minor = %u;\n" /* minor (e.g. 1) */
70" uuid = \"%s\";\n" /* UUID */
71" byte_order = %s;\n" /* be or le */
72" packet.header := struct {\n"
73" uint32_t magic;\n"
74" uint8_t uuid[16];\n"
75" };\n"
76"};\n"
77"\n"
78"stream {\n"
79" packet.context := struct {\n"
80" uint64_t content_size;\n"
81" uint64_t packet_size;\n"
82" };\n"
83"%s" /* Stream event header (opt.) */
84"};\n"
85"\n"
86"event {\n"
87" name = string;\n"
88" fields := struct { string str; };\n"
89"};\n";
90
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";
96
97static
98void print_metadata(FILE *fp)
99{
100 char uuid_str[BABELTRACE_UUID_STR_LEN];
101 unsigned int major = 0, minor = 0;
102 int ret;
103
104 ret = sscanf(VERSION, "%u.%u", &major, &minor);
105 if (ret != 2)
106 fprintf(stderr, "[warning] Incorrect babeltrace version format\n.");
107 babeltrace_uuid_unparse(s_uuid, uuid_str);
108 fprintf(fp, metadata_fmt,
109 major,
110 minor,
111 uuid_str,
112 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
113 s_timestamp ? metadata_stream_event_header_timestamp : "");
114}
115
116static
117void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid)
118{
119 struct ctf_stream_pos dummy;
120
121 /* magic */
122 ctf_dummy_pos(pos, &dummy);
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;
127 assert(!ctf_pos_packet(&dummy));
128
129 if (!ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT))
130 goto error;
131 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
132 if (!ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT))
133 goto error;
134
135 /* uuid */
136 ctf_dummy_pos(pos, &dummy);
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;
141 assert(!ctf_pos_packet(&dummy));
142
143 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
144 goto error;
145 memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
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();
153}
154
155static
156void write_packet_context(struct ctf_stream_pos *pos)
157{
158 struct ctf_stream_pos dummy;
159
160 /* content_size */
161 ctf_dummy_pos(pos, &dummy);
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;
166 assert(!ctf_pos_packet(&dummy));
167
168 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
169 goto error;
170 *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
171 pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
172 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
173 goto error;
174
175 /* packet_size */
176 ctf_dummy_pos(pos, &dummy);
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;
181 assert(!ctf_pos_packet(&dummy));
182
183 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
184 goto error;
185 *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
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();
193}
194
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{
200 unsigned long sec, usec;
201
202 if (!s_timestamp)
203 return;
204
205 /* Only need to be executed on first pass (dummy) */
206 if (pos->dummy) {
207 int ret;
208
209 /* Extract time from input line */
210 ret = sscanf(line, "[%lu.%lu] ", &sec, &usec);
211 if (ret == 2) {
212 *tline = strchr(line, ']');
213 assert(*tline);
214 (*tline)++;
215 if ((*tline)[0] == ' ') {
216 (*tline)++;
217 }
218 *tlen = len + line - *tline;
219 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
220 /*
221 * Default CTF clock has 1GHz frequency. Convert
222 * from usec to nsec.
223 */
224 *ts *= 1000;
225 }
226 }
227 /* timestamp */
228 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
229 goto error;
230 if (!pos->dummy)
231 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
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();
239}
240
241static
242void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
243{
244 struct ctf_stream_pos dummy;
245 int attempt = 0;
246 char *tline = line; /* tline is start of text, after timestamp */
247 size_t tlen = len;
248 uint64_t ts = 0;
249
250 printf_debug("read: %s\n", line);
251
252 for (;;) {
253 ctf_dummy_pos(pos, &dummy);
254 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
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;
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;
271 }
272 }
273
274 write_event_header(pos, line, &tline, len, &tlen, &ts);
275 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
276 goto error;
277 memcpy(ctf_get_pos_addr(pos), tline, tlen);
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();
285}
286
287static
288void trace_text(FILE *input, int output)
289{
290 struct ctf_stream_pos pos;
291 ssize_t len;
292 char *line = NULL, *nl;
293 size_t linesize;
294 int ret;
295
296 memset(&pos, 0, sizeof(pos));
297 ret = ctf_init_pos(&pos, NULL, output, O_RDWR);
298 if (ret) {
299 fprintf(stderr, "Error in ctf_init_pos\n");
300 return;
301 }
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');
309 if (nl) {
310 *nl = '\0';
311 trace_string(line, &pos, nl - line + 1);
312 } else {
313 trace_string(line, &pos, strlen(line) + 1);
314 }
315 }
316 ret = ctf_fini_pos(&pos);
317 if (ret) {
318 fprintf(stderr, "Error in ctf_fini_pos\n");
319 }
320}
321
322static
323void usage(FILE *fp)
324{
325 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
326 fprintf(fp, "\n");
327 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
328 fprintf(fp, "\n");
329 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
330 fprintf(fp, "\n");
331 fprintf(fp, " OUTPUT Output trace path\n");
332 fprintf(fp, "\n");
333 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
334 fprintf(fp, "\n");
335}
336
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;
345 else if (!strcmp(argv[i], "-h")) {
346 s_help = 1;
347 return 0;
348 } else if (argv[i][0] == '-')
349 return -EINVAL;
350 else
351 s_outputname = argv[i];
352 }
353 if (!s_outputname)
354 return -EINVAL;
355 return 0;
356}
357
358int main(int argc, char **argv)
359{
360 int fd, metadata_fd, ret;
361 DIR *dir;
362 int dir_fd;
363 FILE *metadata_fp;
364
365 ret = parse_args(argc, argv);
366 if (ret) {
367 fprintf(stderr, "Error: invalid argument.\n");
368 usage(stderr);
369 goto error;
370 }
371
372 if (s_help) {
373 usage(stdout);
374 exit(EXIT_SUCCESS);
375 }
376
377 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
378 if (ret) {
379 perror("mkdir");
380 goto error;
381 }
382
383 dir = opendir(s_outputname);
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;
399 }
400
401 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
402 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
403 if (metadata_fd < 0) {
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
413 babeltrace_uuid_generate(s_uuid);
414 print_metadata(metadata_fp);
415 trace_text(stdin, fd);
416
417 ret = close(fd);
418 if (ret)
419 perror("close");
420 exit(EXIT_SUCCESS);
421
422 /* error handling */
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");
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:
440 ret = rmdir(s_outputname);
441 if (ret)
442 perror("rmdir");
443error:
444 exit(EXIT_FAILURE);
445}
This page took 0.023343 seconds and 4 git commands to generate.