4 * BabelTrace - Convert Text Log to CTF
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <sys/types.h>
33 #include <babeltrace/compat/dirent.h>
42 #include <babeltrace/babeltrace-internal.h>
43 #include <babeltrace/ctf/types.h>
44 #include <babeltrace/compat/uuid.h>
45 #include <babeltrace/compat/utc.h>
46 #include <babeltrace/compat/stdio.h>
47 #include <babeltrace/endian.h>
49 #define NSEC_PER_USEC 1000UL
50 #define NSEC_PER_MSEC 1000000UL
51 #define NSEC_PER_SEC 1000000000ULL
52 #define USEC_PER_SEC 1000000UL
54 int babeltrace_debug
, babeltrace_verbose
;
56 static char *s_outputname
;
57 static int s_timestamp
;
59 static unsigned char s_uuid
[BABELTRACE_UUID_LEN
];
61 /* Metadata format string */
62 static const char metadata_fmt
[] =
64 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
65 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
66 "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
69 " major = %u;\n" /* major (e.g. 0) */
70 " minor = %u;\n" /* minor (e.g. 1) */
71 " uuid = \"%s\";\n" /* UUID */
72 " byte_order = %s;\n" /* be or le */
73 " packet.header := struct {\n"
75 " uint8_t uuid[16];\n"
80 " packet.context := struct {\n"
81 " uint64_t content_size;\n"
82 " uint64_t packet_size;\n"
84 "%s" /* Stream event header (opt.) */
89 " fields := struct { string str; };\n"
92 static const char metadata_stream_event_header_timestamp
[] =
93 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
94 " event.header := struct {\n"
95 " uint64_t timestamp;\n"
99 void print_metadata(FILE *fp
)
101 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
102 unsigned int major
= 0, minor
= 0;
105 ret
= sscanf(VERSION
, "%u.%u", &major
, &minor
);
107 fprintf(stderr
, "[warning] Incorrect babeltrace version format\n.");
108 bt_uuid_unparse(s_uuid
, uuid_str
);
109 fprintf(fp
, metadata_fmt
,
113 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
114 s_timestamp
? metadata_stream_event_header_timestamp
: "");
118 void write_packet_header(struct ctf_stream_pos
*pos
, unsigned char *uuid
)
120 struct ctf_stream_pos dummy
;
123 ctf_dummy_pos(pos
, &dummy
);
124 if (!ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
))
126 if (!ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
))
128 assert(!ctf_pos_packet(&dummy
));
130 if (!ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
))
132 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
133 if (!ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
))
137 ctf_dummy_pos(pos
, &dummy
);
138 if (!ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
))
140 if (!ctf_move_pos(&dummy
, 16 * CHAR_BIT
))
142 assert(!ctf_pos_packet(&dummy
));
144 if (!ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
))
146 memcpy(ctf_get_pos_addr(pos
), uuid
, BABELTRACE_UUID_LEN
);
147 if (!ctf_move_pos(pos
, BABELTRACE_UUID_LEN
* CHAR_BIT
))
152 fprintf(stderr
, "[error] Out of packet bounds when writing packet header\n");
157 void write_packet_context(struct ctf_stream_pos
*pos
)
159 struct ctf_stream_pos dummy
;
162 ctf_dummy_pos(pos
, &dummy
);
163 if (!ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
165 if (!ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
167 assert(!ctf_pos_packet(&dummy
));
169 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
171 *(uint64_t *) ctf_get_pos_addr(pos
) = ~0ULL; /* Not known yet */
172 pos
->content_size_loc
= (uint64_t *) ctf_get_pos_addr(pos
);
173 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
177 ctf_dummy_pos(pos
, &dummy
);
178 if (!ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
180 if (!ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
182 assert(!ctf_pos_packet(&dummy
));
184 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
186 *(uint64_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
187 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
192 fprintf(stderr
, "[error] Out of packet bounds when writing packet context\n");
197 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
198 char **tline
, size_t len
, size_t *tlen
,
204 /* Only need to be executed on first pass (dummy) */
206 int has_timestamp
= 0;
207 unsigned long sec
, usec
, msec
;
208 unsigned int year
, mon
, mday
, hour
, min
;
210 /* Extract time from input line */
211 if (sscanf(line
, "[%lu.%lu] ", &sec
, &usec
) == 2) {
212 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
214 * Default CTF clock has 1GHz frequency. Convert
217 *ts
*= NSEC_PER_USEC
;
219 } else if (sscanf(line
, "[%u-%u-%u %u:%u:%lu.%lu] ",
220 &year
, &mon
, &mday
, &hour
, &min
,
225 memset(&ti
, 0, sizeof(ti
));
226 ti
.tm_year
= year
- 1900; /* from 1900 */
227 ti
.tm_mon
= mon
- 1; /* 0 to 11 */
233 ep_sec
= babeltrace_timegm(&ti
);
234 if (ep_sec
!= (time_t) -1) {
235 *ts
= (uint64_t) ep_sec
* NSEC_PER_SEC
236 + (uint64_t) msec
* NSEC_PER_MSEC
;
241 *tline
= strchr(line
, ']');
244 if ((*tline
)[0] == ' ') {
247 *tlen
= len
+ line
- *tline
;
251 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
254 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
255 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
260 fprintf(stderr
, "[error] Out of packet bounds when writing event header\n");
265 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
267 struct ctf_stream_pos dummy
;
269 char *tline
= line
; /* tline is start of text, after timestamp */
273 printf_debug("read: %s\n", line
);
276 int packet_filled
= 0;
278 ctf_dummy_pos(pos
, &dummy
);
279 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
280 if (!ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
))
282 if (!ctf_move_pos(&dummy
, tlen
* CHAR_BIT
))
284 if (packet_filled
|| ctf_pos_packet(&dummy
)) {
285 ctf_pos_pad_packet(pos
);
286 write_packet_header(pos
, s_uuid
);
287 write_packet_context(pos
);
288 if (attempt
++ == 1) {
289 fprintf(stderr
, "[Error] Line too large for packet size (%" PRIu64
"kB) (discarded)\n",
290 pos
->packet_size
/ CHAR_BIT
/ 1024);
299 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
300 if (!ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
))
302 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
303 if (!ctf_move_pos(pos
, tlen
* CHAR_BIT
))
308 fprintf(stderr
, "[error] Out of packet bounds when writing event payload\n");
313 void trace_text(FILE *input
, int output
)
315 struct ctf_stream_pos pos
;
317 char *line
= NULL
, *nl
;
321 memset(&pos
, 0, sizeof(pos
));
322 ret
= ctf_init_pos(&pos
, NULL
, output
, O_RDWR
);
324 fprintf(stderr
, "Error in ctf_init_pos\n");
327 ctf_packet_seek(&pos
.parent
, 0, SEEK_CUR
);
328 write_packet_header(&pos
, s_uuid
);
329 write_packet_context(&pos
);
331 len
= bt_getline(&line
, &linesize
, input
);
334 nl
= strrchr(line
, '\n');
337 trace_string(line
, &pos
, nl
- line
+ 1);
339 trace_string(line
, &pos
, strlen(line
) + 1);
342 ret
= ctf_fini_pos(&pos
);
344 fprintf(stderr
, "Error in ctf_fini_pos\n");
351 fprintf(fp
, "BabelTrace Log Converter %s\n", VERSION
);
353 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
355 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
357 fprintf(fp
, " OUTPUT Output trace path\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");
365 int parse_args(int argc
, char **argv
)
369 for (i
= 1; i
< argc
; i
++) {
370 if (!strcmp(argv
[i
], "-t"))
372 else if (!strcmp(argv
[i
], "-h")) {
375 } else if (argv
[i
][0] == '-')
378 s_outputname
= argv
[i
];
385 int main(int argc
, char **argv
)
387 int fd
, metadata_fd
, ret
;
392 ret
= parse_args(argc
, argv
);
394 fprintf(stderr
, "Error: invalid argument.\n");
404 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
410 dir
= opendir(s_outputname
);
415 dir_fd
= bt_dirfd(dir
);
421 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
422 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
425 goto error_closedirfd
;
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) {
432 goto error_closedatastream
;
434 metadata_fp
= fdopen(metadata_fd
, "w");
437 goto error_closemetadatafd
;
440 bt_uuid_generate(s_uuid
);
441 print_metadata(metadata_fp
);
442 trace_text(stdin
, fd
);
450 error_closemetadatafd
:
451 ret
= close(metadata_fd
);
454 error_closedatastream
:
467 ret
= rmdir(s_outputname
);