Fix babeltrace-log incorrect timestamp type
[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
MD
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * Depends on glibc 2.10 for getline().
19 */
20
90219914
MD
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <sys/mman.h>
989c73bc 25#include <dirent.h>
90219914 26#include <stdio.h>
989c73bc 27#include <stdlib.h>
90219914
MD
28#include <stdint.h>
29#include <unistd.h>
30#include <errno.h>
31#include <uuid/uuid.h>
32#include <string.h>
989c73bc 33#include <endian.h>
90219914 34
8c572eba
MD
35#include <babeltrace/babeltrace.h>
36#include <babeltrace/ctf/types.h>
37
bfe09576 38#define USEC_PER_SEC 1000000UL
ca8b2b02 39
b522ac18
MD
40#ifndef UUID_STR_LEN
41#define UUID_STR_LEN 37 /* With \0 */
42#endif
43
ca8b2b02
MD
44int babeltrace_debug, babeltrace_verbose;
45
46static char *s_outputname;
47static int s_timestamp;
39592eae 48static int s_help;
ca8b2b02
MD
49static uuid_t s_uuid;
50
b522ac18
MD
51/* Metadata format string */
52static const char metadata_fmt[] =
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{
89 char uuid_str[UUID_STR_LEN];
90
91 uuid_unparse(s_uuid, uuid_str);
92 fprintf(fp, metadata_fmt,
c818559a
MD
93 BABELTRACE_VERSION_MAJOR,
94 BABELTRACE_VERSION_MINOR,
b522ac18 95 uuid_str,
ca8b2b02
MD
96 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
97 s_timestamp ? metadata_stream_event_header_timestamp : "");
b522ac18
MD
98}
99
8c572eba 100static
46322b33 101void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
8c572eba 102{
46322b33 103 struct ctf_stream_pos dummy;
8c572eba
MD
104
105 /* magic */
46322b33
MD
106 ctf_dummy_pos(pos, &dummy);
107 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
108 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
109 assert(!ctf_pos_packet(&dummy));
8c572eba 110
46322b33
MD
111 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
112 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
113 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba 114
b4c19c1e 115 /* uuid */
46322b33
MD
116 ctf_dummy_pos(pos, &dummy);
117 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
118 ctf_move_pos(&dummy, 16 * CHAR_BIT);
119 assert(!ctf_pos_packet(&dummy));
120
121 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
122 memcpy(ctf_get_pos_addr(pos), uuid, 16);
123 ctf_move_pos(pos, 16 * CHAR_BIT);
8c572eba
MD
124}
125
126static
46322b33 127void write_packet_context(struct ctf_stream_pos *pos)
8c572eba 128{
46322b33 129 struct ctf_stream_pos dummy;
8c572eba
MD
130
131 /* content_size */
46322b33
MD
132 ctf_dummy_pos(pos, &dummy);
133 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
134 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
135 assert(!ctf_pos_packet(&dummy));
8c572eba 136
46322b33
MD
137 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
138 *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */
139 pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos);
140 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
141
142 /* packet_size */
46322b33
MD
143 ctf_dummy_pos(pos, &dummy);
144 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
145 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
146 assert(!ctf_pos_packet(&dummy));
8c572eba 147
46322b33
MD
148 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
149 *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size;
150 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
151}
152
ca8b2b02
MD
153static
154void write_event_header(struct ctf_stream_pos *pos, char *line,
155 char **tline, size_t len, size_t *tlen,
156 uint64_t *ts)
157{
bfe09576 158 unsigned long sec, usec;
ca8b2b02
MD
159 int ret;
160
161 if (!s_timestamp)
162 return;
163
164 /* Only need to be executed on first pass (dummy) */
165 if (pos->dummy) {
166 /* Extract time from input line */
bfe09576 167 ret = sscanf(line, "[%lu.%lu] ", &sec, &usec);
ca8b2b02
MD
168 if (ret == 2) {
169 *tline = strchr(line, ']');
bfe09576
MD
170 assert(*tline);
171 (*tline)++;
172 if ((*tline)[0] == ' ') {
ca8b2b02 173 (*tline)++;
bfe09576 174 }
ca8b2b02 175 *tlen = len + line - *tline;
bfe09576 176 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
ca8b2b02
MD
177 }
178 }
179 /* timestamp */
180 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
181 if (!pos->dummy)
bc7eb6c8 182 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
ca8b2b02
MD
183 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
184}
185
8c572eba 186static
46322b33 187void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
8c572eba 188{
46322b33 189 struct ctf_stream_pos dummy;
8c572eba 190 int attempt = 0;
ca8b2b02
MD
191 char *tline = line; /* tline is start of text, after timestamp */
192 size_t tlen = len;
193 uint64_t ts = 0;
8c572eba
MD
194
195 printf_debug("read: %s\n", line);
196retry:
46322b33 197 ctf_dummy_pos(pos, &dummy);
ca8b2b02 198 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
46322b33 199 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
ca8b2b02 200 ctf_move_pos(&dummy, tlen * CHAR_BIT);
46322b33 201 if (ctf_pos_packet(&dummy)) {
46322b33 202 ctf_pos_pad_packet(pos);
8c572eba
MD
203 write_packet_header(pos, s_uuid);
204 write_packet_context(pos);
205 if (attempt++ == 1) {
206 fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n",
207 pos->packet_size / CHAR_BIT / 1024);
208 return;
209 }
210 goto retry;
211 }
212
ca8b2b02 213 write_event_header(pos, line, &tline, len, &tlen, &ts);
46322b33 214 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
ca8b2b02
MD
215 memcpy(ctf_get_pos_addr(pos), tline, tlen);
216 ctf_move_pos(pos, tlen * CHAR_BIT);
8c572eba
MD
217}
218
219static
220void trace_text(FILE *input, int output)
221{
46322b33 222 struct ctf_stream_pos pos;
8c572eba
MD
223 ssize_t len;
224 char *line = NULL, *nl;
225 size_t linesize;
226
989c73bc 227 ctf_init_pos(&pos, output, O_RDWR);
8c572eba
MD
228
229 write_packet_header(&pos, s_uuid);
230 write_packet_context(&pos);
231 for (;;) {
232 len = getline(&line, &linesize, input);
233 if (len < 0)
234 break;
235 nl = strrchr(line, '\n');
236 if (nl)
237 *nl = '\0';
238 trace_string(line, &pos, nl - line + 1);
239 }
46322b33 240 ctf_fini_pos(&pos);
8c572eba 241}
90219914 242
ca8b2b02
MD
243static
244void usage(FILE *fp)
989c73bc
MD
245{
246 fprintf(fp, "BabelTrace Log Converter %u.%u\n",
247 BABELTRACE_VERSION_MAJOR,
248 BABELTRACE_VERSION_MINOR);
249 fprintf(fp, "\n");
250 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
251 fprintf(fp, "\n");
78893e6e 252 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
989c73bc
MD
253 fprintf(fp, "\n");
254 fprintf(fp, " OUTPUT Output trace path\n");
255 fprintf(fp, "\n");
78893e6e
MD
256 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
257 fprintf(fp, "\n");
989c73bc
MD
258}
259
ca8b2b02
MD
260static
261int parse_args(int argc, char **argv)
262{
263 int i;
264
265 for (i = 1; i < argc; i++) {
266 if (!strcmp(argv[i], "-t"))
267 s_timestamp = 1;
39592eae
MD
268 else if (!strcmp(argv[i], "-h")) {
269 s_help = 1;
270 return 0;
271 } else if (argv[i][0] == '-')
272 return -EINVAL;
ca8b2b02
MD
273 else
274 s_outputname = argv[i];
275 }
276 if (!s_outputname)
277 return -EINVAL;
278 return 0;
279}
280
90219914
MD
281int main(int argc, char **argv)
282{
b522ac18 283 int fd, metadata_fd, ret;
989c73bc
MD
284 DIR *dir;
285 int dir_fd;
b522ac18 286 FILE *metadata_fp;
90219914 287
ca8b2b02
MD
288 ret = parse_args(argc, argv);
289 if (ret) {
39592eae 290 fprintf(stdout, "Error: invalid argument.\n");
989c73bc
MD
291 usage(stdout);
292 goto error;
90219914 293 }
90219914 294
39592eae
MD
295 if (s_help) {
296 usage(stdout);
297 exit(EXIT_SUCCESS);
298 }
299
ca8b2b02 300 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
90219914 301 if (ret) {
989c73bc
MD
302 perror("mkdir");
303 goto error;
304 }
305
ca8b2b02 306 dir = opendir(s_outputname);
989c73bc
MD
307 if (!dir) {
308 perror("opendir");
309 goto error_rmdir;
310 }
311 dir_fd = dirfd(dir);
312 if (dir_fd < 0) {
313 perror("dirfd");
314 goto error_closedir;
315 }
316
317 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
318 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
319 if (fd < 0) {
320 perror("openat");
321 goto error_closedirfd;
90219914 322 }
90219914 323
b522ac18
MD
324 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
325 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
326 if (fd < 0) {
327 perror("openat");
328 goto error_closedatastream;
329 }
330 metadata_fp = fdopen(metadata_fd, "w");
331 if (!metadata_fp) {
332 perror("fdopen");
333 goto error_closemetadatafd;
334 }
335
336 uuid_generate(s_uuid);
337 print_metadata(metadata_fp);
8c572eba 338 trace_text(stdin, fd);
989c73bc 339
90219914 340 close(fd);
989c73bc
MD
341 exit(EXIT_SUCCESS);
342
343 /* error handling */
b522ac18
MD
344error_closemetadatafd:
345 ret = close(metadata_fd);
346 if (ret)
347 perror("close");
348error_closedatastream:
349 ret = close(fd);
350 if (ret)
351 perror("close");
989c73bc
MD
352error_closedirfd:
353 ret = close(dir_fd);
354 if (ret)
355 perror("close");
356error_closedir:
357 ret = closedir(dir);
358 if (ret)
359 perror("closedir");
360error_rmdir:
ca8b2b02 361 ret = rmdir(s_outputname);
989c73bc
MD
362 if (ret)
363 perror("rmdir");
364error:
365 exit(EXIT_FAILURE);
90219914 366}
This page took 0.061133 seconds and 4 git commands to generate.