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().
33 #include <sys/types.h>
46 #include <babeltrace/babeltrace-internal.h>
47 #include <babeltrace/ctf/types.h>
48 #include <babeltrace/compat/uuid.h>
49 #include <babeltrace/endian.h>
51 #define USEC_PER_SEC 1000000UL
53 int babeltrace_debug
, babeltrace_verbose
;
55 static char *s_outputname
;
56 static int s_timestamp
;
58 static unsigned char s_uuid
[BABELTRACE_UUID_LEN
];
60 /* Metadata format string */
61 static const char metadata_fmt
[] =
63 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
64 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
65 "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
68 " major = %u;\n" /* major (e.g. 0) */
69 " minor = %u;\n" /* minor (e.g. 1) */
70 " uuid = \"%s\";\n" /* UUID */
71 " byte_order = %s;\n" /* be or le */
72 " packet.header := struct {\n"
74 " uint8_t uuid[16];\n"
79 " packet.context := struct {\n"
80 " uint64_t content_size;\n"
81 " uint64_t packet_size;\n"
83 "%s" /* Stream event header (opt.) */
88 " fields := struct { string str; };\n"
91 static const char metadata_stream_event_header_timestamp
[] =
92 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
93 " event.header := struct {\n"
94 " uint64_t timestamp;\n"
98 void print_metadata(FILE *fp
)
100 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
101 unsigned int major
= 0, minor
= 0;
104 ret
= sscanf(VERSION
, "%u.%u", &major
, &minor
);
106 fprintf(stderr
, "[warning] Incorrect babeltrace version format\n.");
107 babeltrace_uuid_unparse(s_uuid
, uuid_str
);
108 fprintf(fp
, metadata_fmt
,
112 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
113 s_timestamp
? metadata_stream_event_header_timestamp
: "");
117 void write_packet_header(struct ctf_stream_pos
*pos
, unsigned char *uuid
)
119 struct ctf_stream_pos dummy
;
122 ctf_dummy_pos(pos
, &dummy
);
123 if (!ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
))
125 if (!ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
))
127 assert(!ctf_pos_packet(&dummy
));
129 if (!ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
))
131 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
132 if (!ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
))
136 ctf_dummy_pos(pos
, &dummy
);
137 if (!ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
))
139 if (!ctf_move_pos(&dummy
, 16 * CHAR_BIT
))
141 assert(!ctf_pos_packet(&dummy
));
143 if (!ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
))
145 memcpy(ctf_get_pos_addr(pos
), uuid
, BABELTRACE_UUID_LEN
);
146 if (!ctf_move_pos(pos
, BABELTRACE_UUID_LEN
* CHAR_BIT
))
151 fprintf(stderr
, "[error] Out of packet bounds when writing packet header\n");
156 void write_packet_context(struct ctf_stream_pos
*pos
)
158 struct ctf_stream_pos dummy
;
161 ctf_dummy_pos(pos
, &dummy
);
162 if (!ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
164 if (!ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
166 assert(!ctf_pos_packet(&dummy
));
168 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
170 *(uint64_t *) ctf_get_pos_addr(pos
) = ~0ULL; /* Not known yet */
171 pos
->content_size_loc
= (uint64_t *) ctf_get_pos_addr(pos
);
172 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
176 ctf_dummy_pos(pos
, &dummy
);
177 if (!ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
179 if (!ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
))
181 assert(!ctf_pos_packet(&dummy
));
183 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
185 *(uint64_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
186 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
191 fprintf(stderr
, "[error] Out of packet bounds when writing packet context\n");
196 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
197 char **tline
, size_t len
, size_t *tlen
,
200 unsigned long sec
, usec
;
205 /* Only need to be executed on first pass (dummy) */
209 /* Extract time from input line */
210 ret
= sscanf(line
, "[%lu.%lu] ", &sec
, &usec
);
212 *tline
= strchr(line
, ']');
215 if ((*tline
)[0] == ' ') {
218 *tlen
= len
+ line
- *tline
;
219 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
221 * Default CTF clock has 1GHz frequency. Convert
228 if (!ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
231 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
232 if (!ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
))
237 fprintf(stderr
, "[error] Out of packet bounds when writing event header\n");
242 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
244 struct ctf_stream_pos dummy
;
246 char *tline
= line
; /* tline is start of text, after timestamp */
250 printf_debug("read: %s\n", line
);
253 ctf_dummy_pos(pos
, &dummy
);
254 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
255 if (!ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
))
257 if (!ctf_move_pos(&dummy
, tlen
* CHAR_BIT
))
259 if (ctf_pos_packet(&dummy
)) {
260 ctf_pos_pad_packet(pos
);
261 write_packet_header(pos
, s_uuid
);
262 write_packet_context(pos
);
263 if (attempt
++ == 1) {
264 fprintf(stderr
, "[Error] Line too large for packet size (%" PRIu64
"kB) (discarded)\n",
265 pos
->packet_size
/ CHAR_BIT
/ 1024);
274 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
275 if (!ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
))
277 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
278 if (!ctf_move_pos(pos
, tlen
* CHAR_BIT
))
283 fprintf(stderr
, "[error] Out of packet bounds when writing event payload\n");
288 void trace_text(FILE *input
, int output
)
290 struct ctf_stream_pos pos
;
292 char *line
= NULL
, *nl
;
296 memset(&pos
, 0, sizeof(pos
));
297 ret
= ctf_init_pos(&pos
, NULL
, output
, O_RDWR
);
299 fprintf(stderr
, "Error in ctf_init_pos\n");
302 write_packet_header(&pos
, s_uuid
);
303 write_packet_context(&pos
);
305 len
= getline(&line
, &linesize
, input
);
308 nl
= strrchr(line
, '\n');
311 trace_string(line
, &pos
, nl
- line
+ 1);
313 trace_string(line
, &pos
, strlen(line
) + 1);
316 ret
= ctf_fini_pos(&pos
);
318 fprintf(stderr
, "Error in ctf_fini_pos\n");
325 fprintf(fp
, "BabelTrace Log Converter %s\n", VERSION
);
327 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
329 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
331 fprintf(fp
, " OUTPUT Output trace path\n");
333 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
338 int parse_args(int argc
, char **argv
)
342 for (i
= 1; i
< argc
; i
++) {
343 if (!strcmp(argv
[i
], "-t"))
345 else if (!strcmp(argv
[i
], "-h")) {
348 } else if (argv
[i
][0] == '-')
351 s_outputname
= argv
[i
];
358 int main(int argc
, char **argv
)
360 int fd
, metadata_fd
, ret
;
365 ret
= parse_args(argc
, argv
);
367 fprintf(stderr
, "Error: invalid argument.\n");
377 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
383 dir
= opendir(s_outputname
);
394 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
395 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
398 goto error_closedirfd
;
401 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
402 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
403 if (metadata_fd
< 0) {
405 goto error_closedatastream
;
407 metadata_fp
= fdopen(metadata_fd
, "w");
410 goto error_closemetadatafd
;
413 babeltrace_uuid_generate(s_uuid
);
414 print_metadata(metadata_fp
);
415 trace_text(stdin
, fd
);
423 error_closemetadatafd
:
424 ret
= close(metadata_fd
);
427 error_closedatastream
:
440 ret
= rmdir(s_outputname
);