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