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().
23 #include <sys/types.h>
33 #include <uuid/uuid.h>
37 #include <babeltrace/babeltrace.h>
38 #include <babeltrace/ctf/types.h>
40 #define USEC_PER_SEC 1000000UL
43 #define UUID_STR_LEN 37 /* With \0 */
46 int babeltrace_debug
, babeltrace_verbose
;
48 static char *s_outputname
;
49 static int s_timestamp
;
53 /* Metadata format string */
54 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"
59 " major = %u;\n" /* major (e.g. 0) */
60 " minor = %u;\n" /* minor (e.g. 1) */
61 " uuid = \"%s\";\n" /* UUID */
62 " byte_order = %s;\n" /* be or le */
63 " packet.header := struct {\n"
65 " uint8_t uuid[16];\n"
70 " packet.context := struct {\n"
71 " uint32_t content_size;\n"
72 " uint32_t packet_size;\n"
74 "%s" /* Stream event header (opt.) */
79 " fields := struct { string str; };\n"
82 static const char metadata_stream_event_header_timestamp
[] =
83 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
84 " event.header := struct {\n"
85 " uint64_t timestamp;\n"
89 void print_metadata(FILE *fp
)
91 char uuid_str
[UUID_STR_LEN
];
93 uuid_unparse(s_uuid
, uuid_str
);
94 fprintf(fp
, metadata_fmt
,
95 BABELTRACE_VERSION_MAJOR
,
96 BABELTRACE_VERSION_MINOR
,
98 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
99 s_timestamp
? metadata_stream_event_header_timestamp
: "");
103 void write_packet_header(struct ctf_stream_pos
*pos
, uuid_t uuid
)
105 struct ctf_stream_pos dummy
;
108 ctf_dummy_pos(pos
, &dummy
);
109 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
110 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
111 assert(!ctf_pos_packet(&dummy
));
113 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
114 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
115 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
118 ctf_dummy_pos(pos
, &dummy
);
119 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
120 ctf_move_pos(&dummy
, 16 * CHAR_BIT
);
121 assert(!ctf_pos_packet(&dummy
));
123 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
124 memcpy(ctf_get_pos_addr(pos
), uuid
, 16);
125 ctf_move_pos(pos
, 16 * CHAR_BIT
);
129 void write_packet_context(struct ctf_stream_pos
*pos
)
131 struct ctf_stream_pos dummy
;
134 ctf_dummy_pos(pos
, &dummy
);
135 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
136 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
137 assert(!ctf_pos_packet(&dummy
));
139 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
140 *(uint32_t *) ctf_get_pos_addr(pos
) = -1U; /* Not known yet */
141 pos
->content_size_loc
= (uint32_t *) ctf_get_pos_addr(pos
);
142 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
145 ctf_dummy_pos(pos
, &dummy
);
146 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
147 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
148 assert(!ctf_pos_packet(&dummy
));
150 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
151 *(uint32_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
152 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
156 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
157 char **tline
, size_t len
, size_t *tlen
,
160 unsigned long sec
, usec
;
166 /* Only need to be executed on first pass (dummy) */
168 /* Extract time from input line */
169 ret
= sscanf(line
, "[%lu.%lu] ", &sec
, &usec
);
171 *tline
= strchr(line
, ']');
174 if ((*tline
)[0] == ' ') {
177 *tlen
= len
+ line
- *tline
;
178 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
182 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
184 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
185 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
189 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
191 struct ctf_stream_pos dummy
;
193 char *tline
= line
; /* tline is start of text, after timestamp */
197 printf_debug("read: %s\n", line
);
199 ctf_dummy_pos(pos
, &dummy
);
200 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
201 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
202 ctf_move_pos(&dummy
, tlen
* CHAR_BIT
);
203 if (ctf_pos_packet(&dummy
)) {
204 ctf_pos_pad_packet(pos
);
205 write_packet_header(pos
, s_uuid
);
206 write_packet_context(pos
);
207 if (attempt
++ == 1) {
208 fprintf(stdout
, "[Error] Line too large for packet size (%zukB) (discarded)\n",
209 pos
->packet_size
/ CHAR_BIT
/ 1024);
215 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
216 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
217 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
218 ctf_move_pos(pos
, tlen
* CHAR_BIT
);
222 void trace_text(FILE *input
, int output
)
224 struct ctf_stream_pos pos
;
226 char *line
= NULL
, *nl
;
229 ctf_init_pos(&pos
, output
, O_RDWR
);
231 write_packet_header(&pos
, s_uuid
);
232 write_packet_context(&pos
);
234 len
= getline(&line
, &linesize
, input
);
237 nl
= strrchr(line
, '\n');
240 trace_string(line
, &pos
, nl
- line
+ 1);
248 fprintf(fp
, "BabelTrace Log Converter %u.%u\n",
249 BABELTRACE_VERSION_MAJOR
,
250 BABELTRACE_VERSION_MINOR
);
252 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
254 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
256 fprintf(fp
, " OUTPUT Output trace path\n");
258 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
263 int parse_args(int argc
, char **argv
)
267 for (i
= 1; i
< argc
; i
++) {
268 if (!strcmp(argv
[i
], "-t"))
270 else if (!strcmp(argv
[i
], "-h")) {
273 } else if (argv
[i
][0] == '-')
276 s_outputname
= argv
[i
];
283 int main(int argc
, char **argv
)
285 int fd
, metadata_fd
, ret
;
290 ret
= parse_args(argc
, argv
);
292 fprintf(stdout
, "Error: invalid argument.\n");
302 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
308 dir
= opendir(s_outputname
);
319 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
320 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
323 goto error_closedirfd
;
326 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
327 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
330 goto error_closedatastream
;
332 metadata_fp
= fdopen(metadata_fd
, "w");
335 goto error_closemetadatafd
;
338 uuid_generate(s_uuid
);
339 print_metadata(metadata_fp
);
340 trace_text(stdin
, fd
);
346 error_closemetadatafd
:
347 ret
= close(metadata_fd
);
350 error_closedatastream
:
363 ret
= rmdir(s_outputname
);