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>
37 #include <babeltrace/babeltrace-internal.h>
38 #include <babeltrace/ctf/types.h>
39 #include <babeltrace/uuid.h>
40 #include <babeltrace/endian.h>
42 #define USEC_PER_SEC 1000000UL
44 int babeltrace_debug
, babeltrace_verbose
;
46 static char *s_outputname
;
47 static int s_timestamp
;
49 static unsigned char s_uuid
[BABELTRACE_UUID_LEN
];
51 /* Metadata format string */
52 static const char metadata_fmt
[] =
54 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
55 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
58 " major = %u;\n" /* major (e.g. 0) */
59 " minor = %u;\n" /* minor (e.g. 1) */
60 " uuid = \"%s\";\n" /* UUID */
61 " byte_order = %s;\n" /* be or le */
62 " packet.header := struct {\n"
64 " uint8_t uuid[16];\n"
69 " packet.context := struct {\n"
70 " uint32_t content_size;\n"
71 " uint32_t packet_size;\n"
73 "%s" /* Stream event header (opt.) */
78 " fields := struct { string str; };\n"
81 static const char metadata_stream_event_header_timestamp
[] =
82 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
83 " event.header := struct {\n"
84 " uint64_t timestamp;\n"
88 void print_metadata(FILE *fp
)
90 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
91 unsigned int major
= 0, minor
= 0;
94 ret
= sscanf(VERSION
, "%u.%u", &major
, &minor
);
96 fprintf(stderr
, "[warning] Incorrect babeltrace version format\n.");
97 babeltrace_uuid_unparse(s_uuid
, uuid_str
);
98 fprintf(fp
, metadata_fmt
,
102 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
103 s_timestamp
? metadata_stream_event_header_timestamp
: "");
107 void write_packet_header(struct ctf_stream_pos
*pos
, unsigned char *uuid
)
109 struct ctf_stream_pos dummy
;
112 ctf_dummy_pos(pos
, &dummy
);
113 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
114 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
115 assert(!ctf_pos_packet(&dummy
));
117 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
118 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
119 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
122 ctf_dummy_pos(pos
, &dummy
);
123 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
124 ctf_move_pos(&dummy
, 16 * CHAR_BIT
);
125 assert(!ctf_pos_packet(&dummy
));
127 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
128 memcpy(ctf_get_pos_addr(pos
), uuid
, BABELTRACE_UUID_LEN
);
129 ctf_move_pos(pos
, BABELTRACE_UUID_LEN
* CHAR_BIT
);
133 void write_packet_context(struct ctf_stream_pos
*pos
)
135 struct ctf_stream_pos dummy
;
138 ctf_dummy_pos(pos
, &dummy
);
139 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
140 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
141 assert(!ctf_pos_packet(&dummy
));
143 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
144 *(uint32_t *) ctf_get_pos_addr(pos
) = -1U; /* Not known yet */
145 pos
->content_size_loc
= (uint32_t *) ctf_get_pos_addr(pos
);
146 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
149 ctf_dummy_pos(pos
, &dummy
);
150 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
151 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
152 assert(!ctf_pos_packet(&dummy
));
154 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
155 *(uint32_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
156 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
160 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
161 char **tline
, size_t len
, size_t *tlen
,
164 unsigned long sec
, usec
;
170 /* Only need to be executed on first pass (dummy) */
172 /* Extract time from input line */
173 ret
= sscanf(line
, "[%lu.%lu] ", &sec
, &usec
);
175 *tline
= strchr(line
, ']');
178 if ((*tline
)[0] == ' ') {
181 *tlen
= len
+ line
- *tline
;
182 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
186 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
188 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
189 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
193 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
195 struct ctf_stream_pos dummy
;
197 char *tline
= line
; /* tline is start of text, after timestamp */
201 printf_debug("read: %s\n", line
);
203 ctf_dummy_pos(pos
, &dummy
);
204 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
205 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
206 ctf_move_pos(&dummy
, tlen
* CHAR_BIT
);
207 if (ctf_pos_packet(&dummy
)) {
208 ctf_pos_pad_packet(pos
);
209 write_packet_header(pos
, s_uuid
);
210 write_packet_context(pos
);
211 if (attempt
++ == 1) {
212 fprintf(stderr
, "[Error] Line too large for packet size (%zukB) (discarded)\n",
213 pos
->packet_size
/ CHAR_BIT
/ 1024);
219 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
220 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
221 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
222 ctf_move_pos(pos
, tlen
* CHAR_BIT
);
226 void trace_text(FILE *input
, int output
)
228 struct ctf_stream_pos pos
;
230 char *line
= NULL
, *nl
;
233 ctf_init_pos(&pos
, output
, O_RDWR
);
235 write_packet_header(&pos
, s_uuid
);
236 write_packet_context(&pos
);
238 len
= getline(&line
, &linesize
, input
);
241 nl
= strrchr(line
, '\n');
244 trace_string(line
, &pos
, nl
- line
+ 1);
252 fprintf(fp
, "BabelTrace Log Converter %s\n", VERSION
);
254 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
256 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
258 fprintf(fp
, " OUTPUT Output trace path\n");
260 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
265 int parse_args(int argc
, char **argv
)
269 for (i
= 1; i
< argc
; i
++) {
270 if (!strcmp(argv
[i
], "-t"))
272 else if (!strcmp(argv
[i
], "-h")) {
275 } else if (argv
[i
][0] == '-')
278 s_outputname
= argv
[i
];
285 int main(int argc
, char **argv
)
287 int fd
, metadata_fd
, ret
;
292 ret
= parse_args(argc
, argv
);
294 fprintf(stderr
, "Error: invalid argument.\n");
304 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
310 dir
= opendir(s_outputname
);
321 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
322 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
325 goto error_closedirfd
;
328 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
329 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
332 goto error_closedatastream
;
334 metadata_fp
= fdopen(metadata_fd
, "w");
337 goto error_closemetadatafd
;
340 babeltrace_uuid_generate(s_uuid
);
341 print_metadata(metadata_fp
);
342 trace_text(stdin
, fd
);
348 error_closemetadatafd
:
349 ret
= close(metadata_fd
);
352 error_closedatastream
:
365 ret
= rmdir(s_outputname
);