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
28 * Depends on glibc 2.10 for getline().
31 #include <sys/types.h>
35 #include <babeltrace/compat/dirent.h>
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/endian.h>
50 #define NSEC_PER_USEC 1000UL
51 #define NSEC_PER_MSEC 1000000UL
52 #define NSEC_PER_SEC 1000000000ULL
53 #define USEC_PER_SEC 1000000UL
55 int babeltrace_debug
, babeltrace_verbose
;
57 static char *s_outputname
;
58 static int s_timestamp
;
60 static unsigned char s_uuid
[BABELTRACE_UUID_LEN
];
62 /* Metadata format string */
63 static const char metadata_fmt
[] =
65 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
66 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
67 "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
70 " major = %u;\n" /* major (e.g. 0) */
71 " minor = %u;\n" /* minor (e.g. 1) */
72 " uuid = \"%s\";\n" /* UUID */
73 " byte_order = %s;\n" /* be or le */
74 " packet.header := struct {\n"
76 " uint8_t uuid[16];\n"
81 " packet.context := struct {\n"
82 " uint64_t content_size;\n"
83 " uint64_t packet_size;\n"
85 "%s" /* Stream event header (opt.) */
90 " fields := struct { string str; };\n"
93 static const char metadata_stream_event_header_timestamp
[] =
94 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
95 " event.header := struct {\n"
96 " uint64_t timestamp;\n"
100 void print_metadata(FILE *fp
)
102 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
103 unsigned int major
= 0, minor
= 0;
106 ret
= sscanf(VERSION
, "%u.%u", &major
, &minor
);
108 fprintf(stderr
, "[warning] Incorrect babeltrace version format\n.");
109 bt_uuid_unparse(s_uuid
, uuid_str
);
110 fprintf(fp
, metadata_fmt
,
114 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
115 s_timestamp
? metadata_stream_event_header_timestamp
: "");
119 void write_packet_header(struct ctf_stream_pos
*pos
, unsigned char *uuid
)
121 struct ctf_stream_pos dummy
;
124 ctf_dummy_pos(pos
, &dummy
);
125 if (!ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
))
127 if (!ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
))
129 assert(!ctf_pos_packet(&dummy
));
131 if (!ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
))
133 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
134 if (!ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
))
138 ctf_dummy_pos(pos
, &dummy
);
139 if (!ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
))
141 if (!ctf_move_pos(&dummy
, 16 * CHAR_BIT
))
143 assert(!ctf_pos_packet(&dummy
));
145 if (!ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
))
147 memcpy(ctf_get_pos_addr(pos
), uuid
, BABELTRACE_UUID_LEN
);
148 if (!ctf_move_pos(pos
, BABELTRACE_UUID_LEN
* CHAR_BIT
))
153 fprintf(stderr
, "[error] Out of packet bounds when writing packet header\n");
158 void write_packet_context(struct ctf_stream_pos
*pos
)
160 struct ctf_stream_pos dummy
;
163 ctf_dummy_pos(pos
, &dummy
);
164 if (!ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
166 if (!ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
168 assert(!ctf_pos_packet(&dummy
));
170 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
172 *(uint64_t *) ctf_get_pos_addr(pos
) = ~0ULL; /* Not known yet */
173 pos
->content_size_loc
= (uint64_t *) ctf_get_pos_addr(pos
);
174 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
178 ctf_dummy_pos(pos
, &dummy
);
179 if (!ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
181 if (!ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
183 assert(!ctf_pos_packet(&dummy
));
185 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
187 *(uint64_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
188 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
193 fprintf(stderr
, "[error] Out of packet bounds when writing packet context\n");
198 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
199 char **tline
, size_t len
, size_t *tlen
,
205 /* Only need to be executed on first pass (dummy) */
207 int has_timestamp
= 0;
208 unsigned long sec
, usec
, msec
;
209 unsigned int year
, mon
, mday
, hour
, min
;
211 /* Extract time from input line */
212 if (sscanf(line
, "[%lu.%lu] ", &sec
, &usec
) == 2) {
213 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
215 * Default CTF clock has 1GHz frequency. Convert
218 *ts
*= NSEC_PER_USEC
;
220 } else if (sscanf(line
, "[%u-%u-%u %u:%u:%lu.%lu] ",
221 &year
, &mon
, &mday
, &hour
, &min
,
226 memset(&ti
, 0, sizeof(ti
));
227 ti
.tm_year
= year
- 1900; /* from 1900 */
228 ti
.tm_mon
= mon
- 1; /* 0 to 11 */
234 ep_sec
= babeltrace_timegm(&ti
);
235 if (ep_sec
!= (time_t) -1) {
236 *ts
= (uint64_t) ep_sec
* NSEC_PER_SEC
237 + (uint64_t) msec
* NSEC_PER_MSEC
;
242 *tline
= strchr(line
, ']');
245 if ((*tline
)[0] == ' ') {
248 *tlen
= len
+ line
- *tline
;
252 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
255 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
256 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
261 fprintf(stderr
, "[error] Out of packet bounds when writing event header\n");
266 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
268 struct ctf_stream_pos dummy
;
270 char *tline
= line
; /* tline is start of text, after timestamp */
274 printf_debug("read: %s\n", line
);
277 int packet_filled
= 0;
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
))
283 if (!ctf_move_pos(&dummy
, tlen
* CHAR_BIT
))
285 if (packet_filled
|| 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);
300 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
301 if (!ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
))
303 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
304 if (!ctf_move_pos(pos
, tlen
* CHAR_BIT
))
309 fprintf(stderr
, "[error] Out of packet bounds when writing event payload\n");
314 void trace_text(FILE *input
, int output
)
316 struct ctf_stream_pos pos
;
318 char *line
= NULL
, *nl
;
322 memset(&pos
, 0, sizeof(pos
));
323 ret
= ctf_init_pos(&pos
, NULL
, output
, O_RDWR
);
325 fprintf(stderr
, "Error in ctf_init_pos\n");
328 ctf_packet_seek(&pos
.parent
, 0, SEEK_CUR
);
329 write_packet_header(&pos
, s_uuid
);
330 write_packet_context(&pos
);
332 len
= getline(&line
, &linesize
, input
);
335 nl
= strrchr(line
, '\n');
338 trace_string(line
, &pos
, nl
- line
+ 1);
340 trace_string(line
, &pos
, strlen(line
) + 1);
343 ret
= ctf_fini_pos(&pos
);
345 fprintf(stderr
, "Error in ctf_fini_pos\n");
352 fprintf(fp
, "BabelTrace Log Converter %s\n", VERSION
);
354 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
356 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
358 fprintf(fp
, " OUTPUT Output trace path\n");
360 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
361 fprintf(fp
, " (format: [YYYY-MM-DD HH:MM:SS.MS] string\\n)\n");
366 int parse_args(int argc
, char **argv
)
370 for (i
= 1; i
< argc
; i
++) {
371 if (!strcmp(argv
[i
], "-t"))
373 else if (!strcmp(argv
[i
], "-h")) {
376 } else if (argv
[i
][0] == '-')
379 s_outputname
= argv
[i
];
386 int main(int argc
, char **argv
)
388 int fd
, metadata_fd
, ret
;
393 ret
= parse_args(argc
, argv
);
395 fprintf(stderr
, "Error: invalid argument.\n");
405 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
411 dir
= opendir(s_outputname
);
416 dir_fd
= bt_dirfd(dir
);
422 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
423 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
426 goto error_closedirfd
;
429 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
430 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
431 if (metadata_fd
< 0) {
433 goto error_closedatastream
;
435 metadata_fp
= fdopen(metadata_fd
, "w");
438 goto error_closemetadatafd
;
441 bt_uuid_generate(s_uuid
);
442 print_metadata(metadata_fp
);
443 trace_text(stdin
, fd
);
451 error_closemetadatafd
:
452 ret
= close(metadata_fd
);
455 error_closedatastream
:
468 ret
= rmdir(s_outputname
);