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 * Depends on glibc 2.10 for getline().
25 #include <sys/types.h>
38 #include <babeltrace/babeltrace-internal.h>
39 #include <babeltrace/ctf/types.h>
40 #include <babeltrace/uuid.h>
41 #include <babeltrace/endian.h>
43 #define USEC_PER_SEC 1000000UL
45 int babeltrace_debug
, babeltrace_verbose
;
47 static char *s_outputname
;
48 static int s_timestamp
;
50 static unsigned char s_uuid
[BABELTRACE_UUID_LEN
];
52 /* Metadata format string */
53 static const char metadata_fmt
[] =
55 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
56 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
57 "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
60 " major = %u;\n" /* major (e.g. 0) */
61 " minor = %u;\n" /* minor (e.g. 1) */
62 " uuid = \"%s\";\n" /* UUID */
63 " byte_order = %s;\n" /* be or le */
64 " packet.header := struct {\n"
66 " uint8_t uuid[16];\n"
71 " packet.context := struct {\n"
72 " uint64_t content_size;\n"
73 " uint64_t packet_size;\n"
75 "%s" /* Stream event header (opt.) */
80 " fields := struct { string str; };\n"
83 static const char metadata_stream_event_header_timestamp
[] =
84 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
85 " event.header := struct {\n"
86 " uint64_t timestamp;\n"
90 void print_metadata(FILE *fp
)
92 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
93 unsigned int major
= 0, minor
= 0;
96 ret
= sscanf(VERSION
, "%u.%u", &major
, &minor
);
98 fprintf(stderr
, "[warning] Incorrect babeltrace version format\n.");
99 babeltrace_uuid_unparse(s_uuid
, uuid_str
);
100 fprintf(fp
, metadata_fmt
,
104 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
105 s_timestamp
? metadata_stream_event_header_timestamp
: "");
109 void write_packet_header(struct ctf_stream_pos
*pos
, unsigned char *uuid
)
111 struct ctf_stream_pos dummy
;
114 ctf_dummy_pos(pos
, &dummy
);
115 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
116 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
117 assert(!ctf_pos_packet(&dummy
));
119 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
120 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
121 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
124 ctf_dummy_pos(pos
, &dummy
);
125 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
126 ctf_move_pos(&dummy
, 16 * CHAR_BIT
);
127 assert(!ctf_pos_packet(&dummy
));
129 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
130 memcpy(ctf_get_pos_addr(pos
), uuid
, BABELTRACE_UUID_LEN
);
131 ctf_move_pos(pos
, BABELTRACE_UUID_LEN
* CHAR_BIT
);
135 void write_packet_context(struct ctf_stream_pos
*pos
)
137 struct ctf_stream_pos dummy
;
140 ctf_dummy_pos(pos
, &dummy
);
141 ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
);
142 ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
);
143 assert(!ctf_pos_packet(&dummy
));
145 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
146 *(uint64_t *) ctf_get_pos_addr(pos
) = ~0ULL; /* Not known yet */
147 pos
->content_size_loc
= (uint64_t *) ctf_get_pos_addr(pos
);
148 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
151 ctf_dummy_pos(pos
, &dummy
);
152 ctf_align_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
);
153 ctf_move_pos(&dummy
, sizeof(uint64_t) * CHAR_BIT
);
154 assert(!ctf_pos_packet(&dummy
));
156 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
157 *(uint64_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
158 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
162 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
163 char **tline
, size_t len
, size_t *tlen
,
166 unsigned long sec
, usec
;
172 /* Only need to be executed on first pass (dummy) */
174 /* Extract time from input line */
175 ret
= sscanf(line
, "[%lu.%lu] ", &sec
, &usec
);
177 *tline
= strchr(line
, ']');
180 if ((*tline
)[0] == ' ') {
183 *tlen
= len
+ line
- *tline
;
184 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
188 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
190 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
191 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
195 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
197 struct ctf_stream_pos dummy
;
199 char *tline
= line
; /* tline is start of text, after timestamp */
203 printf_debug("read: %s\n", line
);
205 ctf_dummy_pos(pos
, &dummy
);
206 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
207 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
208 ctf_move_pos(&dummy
, tlen
* CHAR_BIT
);
209 if (ctf_pos_packet(&dummy
)) {
210 ctf_pos_pad_packet(pos
);
211 write_packet_header(pos
, s_uuid
);
212 write_packet_context(pos
);
213 if (attempt
++ == 1) {
214 fprintf(stderr
, "[Error] Line too large for packet size (%" PRIu64
"kB) (discarded)\n",
215 pos
->packet_size
/ CHAR_BIT
/ 1024);
221 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
222 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
223 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
224 ctf_move_pos(pos
, tlen
* CHAR_BIT
);
228 void trace_text(FILE *input
, int output
)
230 struct ctf_stream_pos pos
;
232 char *line
= NULL
, *nl
;
235 memset(&pos
, 0, sizeof(pos
));
236 ctf_init_pos(&pos
, output
, O_RDWR
);
238 write_packet_header(&pos
, s_uuid
);
239 write_packet_context(&pos
);
241 len
= getline(&line
, &linesize
, input
);
244 nl
= strrchr(line
, '\n');
247 trace_string(line
, &pos
, nl
- line
+ 1);
249 trace_string(line
, &pos
, strlen(line
) + 1);
258 fprintf(fp
, "BabelTrace Log Converter %s\n", VERSION
);
260 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
262 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
264 fprintf(fp
, " OUTPUT Output trace path\n");
266 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
271 int parse_args(int argc
, char **argv
)
275 for (i
= 1; i
< argc
; i
++) {
276 if (!strcmp(argv
[i
], "-t"))
278 else if (!strcmp(argv
[i
], "-h")) {
281 } else if (argv
[i
][0] == '-')
284 s_outputname
= argv
[i
];
291 int main(int argc
, char **argv
)
293 int fd
, metadata_fd
, ret
;
298 ret
= parse_args(argc
, argv
);
300 fprintf(stderr
, "Error: invalid argument.\n");
310 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
316 dir
= opendir(s_outputname
);
327 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
328 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
331 goto error_closedirfd
;
334 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
335 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
338 goto error_closedatastream
;
340 metadata_fp
= fdopen(metadata_fd
, "w");
343 goto error_closemetadatafd
;
346 babeltrace_uuid_generate(s_uuid
);
347 print_metadata(metadata_fp
);
348 trace_text(stdin
, fd
);
354 error_closemetadatafd
:
355 ret
= close(metadata_fd
);
358 error_closedatastream
:
371 ret
= rmdir(s_outputname
);