Merge branch 'master' into bindings/python
[babeltrace.git] / converter / babeltrace-log.c
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/compat/utc.h>
50 #include <babeltrace/endian.h>
51
52 #define NSEC_PER_USEC 1000UL
53 #define NSEC_PER_MSEC 1000000UL
54 #define NSEC_PER_SEC 1000000000ULL
55 #define USEC_PER_SEC 1000000UL
56
57 int babeltrace_debug, babeltrace_verbose;
58
59 static char *s_outputname;
60 static int s_timestamp;
61 static int s_help;
62 static unsigned char s_uuid[BABELTRACE_UUID_LEN];
63
64 /* Metadata format string */
65 static const char metadata_fmt[] =
66 "/* CTF 1.8 */\n"
67 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
68 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
69 "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
70 "\n"
71 "trace {\n"
72 " major = %u;\n" /* major (e.g. 0) */
73 " minor = %u;\n" /* minor (e.g. 1) */
74 " uuid = \"%s\";\n" /* UUID */
75 " byte_order = %s;\n" /* be or le */
76 " packet.header := struct {\n"
77 " uint32_t magic;\n"
78 " uint8_t uuid[16];\n"
79 " };\n"
80 "};\n"
81 "\n"
82 "stream {\n"
83 " packet.context := struct {\n"
84 " uint64_t content_size;\n"
85 " uint64_t packet_size;\n"
86 " };\n"
87 "%s" /* Stream event header (opt.) */
88 "};\n"
89 "\n"
90 "event {\n"
91 " name = string;\n"
92 " fields := struct { string str; };\n"
93 "};\n";
94
95 static const char metadata_stream_event_header_timestamp[] =
96 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
97 " event.header := struct {\n"
98 " uint64_t timestamp;\n"
99 " };\n";
100
101 static
102 void print_metadata(FILE *fp)
103 {
104 char uuid_str[BABELTRACE_UUID_STR_LEN];
105 unsigned int major = 0, minor = 0;
106 int ret;
107
108 ret = sscanf(VERSION, "%u.%u", &major, &minor);
109 if (ret != 2)
110 fprintf(stderr, "[warning] Incorrect babeltrace version format\n.");
111 babeltrace_uuid_unparse(s_uuid, uuid_str);
112 fprintf(fp, metadata_fmt,
113 major,
114 minor,
115 uuid_str,
116 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
117 s_timestamp ? metadata_stream_event_header_timestamp : "");
118 }
119
120 static
121 void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid)
122 {
123 struct ctf_stream_pos dummy;
124
125 /* magic */
126 ctf_dummy_pos(pos, &dummy);
127 if (!ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
128 goto error;
129 if (!ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
130 goto error;
131 assert(!ctf_pos_packet(&dummy));
132
133 if (!ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT))
134 goto error;
135 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
136 if (!ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT))
137 goto error;
138
139 /* uuid */
140 ctf_dummy_pos(pos, &dummy);
141 if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
142 goto error;
143 if (!ctf_move_pos(&dummy, 16 * CHAR_BIT))
144 goto error;
145 assert(!ctf_pos_packet(&dummy));
146
147 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
148 goto error;
149 memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
150 if (!ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT))
151 goto error;
152 return;
153
154 error:
155 fprintf(stderr, "[error] Out of packet bounds when writing packet header\n");
156 abort();
157 }
158
159 static
160 void write_packet_context(struct ctf_stream_pos *pos)
161 {
162 struct ctf_stream_pos dummy;
163
164 /* content_size */
165 ctf_dummy_pos(pos, &dummy);
166 if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
167 goto error;
168 if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
169 goto error;
170 assert(!ctf_pos_packet(&dummy));
171
172 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
173 goto error;
174 *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
175 pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
176 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
177 goto error;
178
179 /* packet_size */
180 ctf_dummy_pos(pos, &dummy);
181 if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
182 goto error;
183 if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
184 goto error;
185 assert(!ctf_pos_packet(&dummy));
186
187 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
188 goto error;
189 *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
190 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
191 goto error;
192 return;
193
194 error:
195 fprintf(stderr, "[error] Out of packet bounds when writing packet context\n");
196 abort();
197 }
198
199 static
200 void write_event_header(struct ctf_stream_pos *pos, char *line,
201 char **tline, size_t len, size_t *tlen,
202 uint64_t *ts)
203 {
204 if (!s_timestamp)
205 return;
206
207 /* Only need to be executed on first pass (dummy) */
208 if (pos->dummy) {
209 int has_timestamp = 0;
210 unsigned long sec, usec, msec;
211 unsigned int year, mon, mday, hour, min;
212
213 /* Extract time from input line */
214 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
215 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
216 /*
217 * Default CTF clock has 1GHz frequency. Convert
218 * from usec to nsec.
219 */
220 *ts *= NSEC_PER_USEC;
221 has_timestamp = 1;
222 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
223 &year, &mon, &mday, &hour, &min,
224 &sec, &msec) == 7) {
225 time_t ep_sec;
226 struct tm ti;
227
228 memset(&ti, 0, sizeof(ti));
229 ti.tm_year = year - 1900; /* from 1900 */
230 ti.tm_mon = mon - 1; /* 0 to 11 */
231 ti.tm_mday = mday;
232 ti.tm_hour = hour;
233 ti.tm_min = min;
234 ti.tm_sec = sec;
235
236 ep_sec = babeltrace_timegm(&ti);
237 if (ep_sec != (time_t) -1) {
238 *ts = (uint64_t) ep_sec * NSEC_PER_SEC
239 + (uint64_t) msec * NSEC_PER_MSEC;
240 }
241 has_timestamp = 1;
242 }
243 if (has_timestamp) {
244 *tline = strchr(line, ']');
245 assert(*tline);
246 (*tline)++;
247 if ((*tline)[0] == ' ') {
248 (*tline)++;
249 }
250 *tlen = len + line - *tline;
251 }
252 }
253 /* timestamp */
254 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
255 goto error;
256 if (!pos->dummy)
257 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
258 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
259 goto error;
260 return;
261
262 error:
263 fprintf(stderr, "[error] Out of packet bounds when writing event header\n");
264 abort();
265 }
266
267 static
268 void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
269 {
270 struct ctf_stream_pos dummy;
271 int attempt = 0;
272 char *tline = line; /* tline is start of text, after timestamp */
273 size_t tlen = len;
274 uint64_t ts = 0;
275
276 printf_debug("read: %s\n", line);
277
278 for (;;) {
279 ctf_dummy_pos(pos, &dummy);
280 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
281 if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
282 goto error;
283 if (!ctf_move_pos(&dummy, tlen * CHAR_BIT))
284 goto error;
285 if (ctf_pos_packet(&dummy)) {
286 ctf_pos_pad_packet(pos);
287 write_packet_header(pos, s_uuid);
288 write_packet_context(pos);
289 if (attempt++ == 1) {
290 fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
291 pos->packet_size / CHAR_BIT / 1024);
292 return;
293 }
294 continue;
295 } else {
296 break;
297 }
298 }
299
300 write_event_header(pos, line, &tline, len, &tlen, &ts);
301 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
302 goto error;
303 memcpy(ctf_get_pos_addr(pos), tline, tlen);
304 if (!ctf_move_pos(pos, tlen * CHAR_BIT))
305 goto error;
306 return;
307
308 error:
309 fprintf(stderr, "[error] Out of packet bounds when writing event payload\n");
310 abort();
311 }
312
313 static
314 void trace_text(FILE *input, int output)
315 {
316 struct ctf_stream_pos pos;
317 ssize_t len;
318 char *line = NULL, *nl;
319 size_t linesize;
320 int ret;
321
322 memset(&pos, 0, sizeof(pos));
323 ret = ctf_init_pos(&pos, NULL, output, O_RDWR);
324 if (ret) {
325 fprintf(stderr, "Error in ctf_init_pos\n");
326 return;
327 }
328 write_packet_header(&pos, s_uuid);
329 write_packet_context(&pos);
330 for (;;) {
331 len = getline(&line, &linesize, input);
332 if (len < 0)
333 break;
334 nl = strrchr(line, '\n');
335 if (nl) {
336 *nl = '\0';
337 trace_string(line, &pos, nl - line + 1);
338 } else {
339 trace_string(line, &pos, strlen(line) + 1);
340 }
341 }
342 ret = ctf_fini_pos(&pos);
343 if (ret) {
344 fprintf(stderr, "Error in ctf_fini_pos\n");
345 }
346 }
347
348 static
349 void usage(FILE *fp)
350 {
351 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
352 fprintf(fp, "\n");
353 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
354 fprintf(fp, "\n");
355 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
356 fprintf(fp, "\n");
357 fprintf(fp, " OUTPUT Output trace path\n");
358 fprintf(fp, "\n");
359 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
360 fprintf(fp, " (format: [YYYY-MM-DD HH:MM:SS.MS] string\\n)\n");
361 fprintf(fp, "\n");
362 }
363
364 static
365 int parse_args(int argc, char **argv)
366 {
367 int i;
368
369 for (i = 1; i < argc; i++) {
370 if (!strcmp(argv[i], "-t"))
371 s_timestamp = 1;
372 else if (!strcmp(argv[i], "-h")) {
373 s_help = 1;
374 return 0;
375 } else if (argv[i][0] == '-')
376 return -EINVAL;
377 else
378 s_outputname = argv[i];
379 }
380 if (!s_outputname)
381 return -EINVAL;
382 return 0;
383 }
384
385 int main(int argc, char **argv)
386 {
387 int fd, metadata_fd, ret;
388 DIR *dir;
389 int dir_fd;
390 FILE *metadata_fp;
391
392 ret = parse_args(argc, argv);
393 if (ret) {
394 fprintf(stderr, "Error: invalid argument.\n");
395 usage(stderr);
396 goto error;
397 }
398
399 if (s_help) {
400 usage(stdout);
401 exit(EXIT_SUCCESS);
402 }
403
404 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
405 if (ret) {
406 perror("mkdir");
407 goto error;
408 }
409
410 dir = opendir(s_outputname);
411 if (!dir) {
412 perror("opendir");
413 goto error_rmdir;
414 }
415 dir_fd = dirfd(dir);
416 if (dir_fd < 0) {
417 perror("dirfd");
418 goto error_closedir;
419 }
420
421 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
422 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
423 if (fd < 0) {
424 perror("openat");
425 goto error_closedirfd;
426 }
427
428 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
429 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
430 if (metadata_fd < 0) {
431 perror("openat");
432 goto error_closedatastream;
433 }
434 metadata_fp = fdopen(metadata_fd, "w");
435 if (!metadata_fp) {
436 perror("fdopen");
437 goto error_closemetadatafd;
438 }
439
440 babeltrace_uuid_generate(s_uuid);
441 print_metadata(metadata_fp);
442 trace_text(stdin, fd);
443
444 ret = close(fd);
445 if (ret)
446 perror("close");
447 exit(EXIT_SUCCESS);
448
449 /* error handling */
450 error_closemetadatafd:
451 ret = close(metadata_fd);
452 if (ret)
453 perror("close");
454 error_closedatastream:
455 ret = close(fd);
456 if (ret)
457 perror("close");
458 error_closedirfd:
459 ret = close(dir_fd);
460 if (ret)
461 perror("close");
462 error_closedir:
463 ret = closedir(dir);
464 if (ret)
465 perror("closedir");
466 error_rmdir:
467 ret = rmdir(s_outputname);
468 if (ret)
469 perror("rmdir");
470 error:
471 exit(EXIT_FAILURE);
472 }
This page took 0.039474 seconds and 5 git commands to generate.