Fix babeltrace-log incorrect timestamp type
[babeltrace.git] / converter / babeltrace-log.c
1 /*
2 * babeltrace-log.c
3 *
4 * BabelTrace - Convert Text Log to CTF
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
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25 #include <dirent.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <uuid/uuid.h>
32 #include <string.h>
33 #include <endian.h>
34
35 #include <babeltrace/babeltrace.h>
36 #include <babeltrace/ctf/types.h>
37
38 #define USEC_PER_SEC 1000000UL
39
40 #ifndef UUID_STR_LEN
41 #define UUID_STR_LEN 37 /* With \0 */
42 #endif
43
44 int babeltrace_debug, babeltrace_verbose;
45
46 static char *s_outputname;
47 static int s_timestamp;
48 static int s_help;
49 static uuid_t s_uuid;
50
51 /* Metadata format string */
52 static const char metadata_fmt[] =
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"
57 " major = %u;\n" /* major (e.g. 0) */
58 " minor = %u;\n" /* minor (e.g. 1) */
59 " uuid = \"%s\";\n" /* UUID */
60 " byte_order = %s;\n" /* be or le */
61 " packet.header := struct {\n"
62 " uint32_t magic;\n"
63 " uint8_t uuid[16];\n"
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"
72 "%s" /* Stream event header (opt.) */
73 "};\n"
74 "\n"
75 "event {\n"
76 " name = string;\n"
77 " fields := struct { string str; };\n"
78 "};\n";
79
80 static 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";
85
86 static
87 void 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,
93 BABELTRACE_VERSION_MAJOR,
94 BABELTRACE_VERSION_MINOR,
95 uuid_str,
96 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
97 s_timestamp ? metadata_stream_event_header_timestamp : "");
98 }
99
100 static
101 void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
102 {
103 struct ctf_stream_pos dummy;
104
105 /* magic */
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));
110
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);
114
115 /* uuid */
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);
124 }
125
126 static
127 void write_packet_context(struct ctf_stream_pos *pos)
128 {
129 struct ctf_stream_pos dummy;
130
131 /* content_size */
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));
136
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);
141
142 /* packet_size */
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));
147
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);
151 }
152
153 static
154 void write_event_header(struct ctf_stream_pos *pos, char *line,
155 char **tline, size_t len, size_t *tlen,
156 uint64_t *ts)
157 {
158 unsigned long sec, usec;
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 */
167 ret = sscanf(line, "[%lu.%lu] ", &sec, &usec);
168 if (ret == 2) {
169 *tline = strchr(line, ']');
170 assert(*tline);
171 (*tline)++;
172 if ((*tline)[0] == ' ') {
173 (*tline)++;
174 }
175 *tlen = len + line - *tline;
176 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
177 }
178 }
179 /* timestamp */
180 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
181 if (!pos->dummy)
182 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
183 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
184 }
185
186 static
187 void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
188 {
189 struct ctf_stream_pos dummy;
190 int attempt = 0;
191 char *tline = line; /* tline is start of text, after timestamp */
192 size_t tlen = len;
193 uint64_t ts = 0;
194
195 printf_debug("read: %s\n", line);
196 retry:
197 ctf_dummy_pos(pos, &dummy);
198 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
199 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
200 ctf_move_pos(&dummy, tlen * CHAR_BIT);
201 if (ctf_pos_packet(&dummy)) {
202 ctf_pos_pad_packet(pos);
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
213 write_event_header(pos, line, &tline, len, &tlen, &ts);
214 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
215 memcpy(ctf_get_pos_addr(pos), tline, tlen);
216 ctf_move_pos(pos, tlen * CHAR_BIT);
217 }
218
219 static
220 void trace_text(FILE *input, int output)
221 {
222 struct ctf_stream_pos pos;
223 ssize_t len;
224 char *line = NULL, *nl;
225 size_t linesize;
226
227 ctf_init_pos(&pos, output, O_RDWR);
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 }
240 ctf_fini_pos(&pos);
241 }
242
243 static
244 void usage(FILE *fp)
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");
252 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
253 fprintf(fp, "\n");
254 fprintf(fp, " OUTPUT Output trace path\n");
255 fprintf(fp, "\n");
256 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
257 fprintf(fp, "\n");
258 }
259
260 static
261 int 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;
268 else if (!strcmp(argv[i], "-h")) {
269 s_help = 1;
270 return 0;
271 } else if (argv[i][0] == '-')
272 return -EINVAL;
273 else
274 s_outputname = argv[i];
275 }
276 if (!s_outputname)
277 return -EINVAL;
278 return 0;
279 }
280
281 int main(int argc, char **argv)
282 {
283 int fd, metadata_fd, ret;
284 DIR *dir;
285 int dir_fd;
286 FILE *metadata_fp;
287
288 ret = parse_args(argc, argv);
289 if (ret) {
290 fprintf(stdout, "Error: invalid argument.\n");
291 usage(stdout);
292 goto error;
293 }
294
295 if (s_help) {
296 usage(stdout);
297 exit(EXIT_SUCCESS);
298 }
299
300 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
301 if (ret) {
302 perror("mkdir");
303 goto error;
304 }
305
306 dir = opendir(s_outputname);
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;
322 }
323
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);
338 trace_text(stdin, fd);
339
340 close(fd);
341 exit(EXIT_SUCCESS);
342
343 /* error handling */
344 error_closemetadatafd:
345 ret = close(metadata_fd);
346 if (ret)
347 perror("close");
348 error_closedatastream:
349 ret = close(fd);
350 if (ret)
351 perror("close");
352 error_closedirfd:
353 ret = close(dir_fd);
354 if (ret)
355 perror("close");
356 error_closedir:
357 ret = closedir(dir);
358 if (ret)
359 perror("closedir");
360 error_rmdir:
361 ret = rmdir(s_outputname);
362 if (ret)
363 perror("rmdir");
364 error:
365 exit(EXIT_FAILURE);
366 }
This page took 0.055422 seconds and 4 git commands to generate.