4 * BabelTrace - Convert Text Log to CTF
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * Depends on glibc 2.10 for getline().
21 #include <sys/types.h>
31 #include <uuid/uuid.h>
35 #include <babeltrace/babeltrace.h>
36 #include <babeltrace/ctf/types.h>
38 #define USEC_PER_SEC 1000000UL
41 #define UUID_STR_LEN 37 /* With \0 */
44 int babeltrace_debug
, babeltrace_verbose
;
46 static char *s_outputname
;
47 static int s_timestamp
;
50 /* Metadata format string */
51 static const char metadata_fmt
[] =
52 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
53 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
56 " major = %u;\n" /* major (e.g. 0) */
57 " minor = %u;\n" /* minor (e.g. 1) */
58 " uuid = \"%s\";\n" /* UUID */
59 " byte_order = %s;\n" /* be or le */
60 " packet.header := struct {\n"
62 " uint8_t trace_uuid[16];\n"
67 " packet.context := struct {\n"
68 " uint32_t content_size;\n"
69 " uint32_t packet_size;\n"
71 "%s" /* Stream event header (opt.) */
76 " fields := struct { string str; };\n"
79 static const char metadata_stream_event_header_timestamp
[] =
80 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
81 " event.header := struct {\n"
82 " uint64_t timestamp;\n"
86 void print_metadata(FILE *fp
)
88 char uuid_str
[UUID_STR_LEN
];
90 uuid_unparse(s_uuid
, uuid_str
);
91 fprintf(fp
, metadata_fmt
,
92 BABELTRACE_VERSION_MAJOR
,
93 BABELTRACE_VERSION_MINOR
,
95 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
96 s_timestamp
? metadata_stream_event_header_timestamp
: "");
100 void write_packet_header(struct ctf_stream_pos
*pos
, uuid_t uuid
)
102 struct ctf_stream_pos dummy
;
105 ctf_dummy_pos(pos
, &dummy
);
106 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
107 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
108 assert(!ctf_pos_packet(&dummy
));
110 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
111 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
112 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
115 ctf_dummy_pos(pos
, &dummy
);
116 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
117 ctf_move_pos(&dummy
, 16 * CHAR_BIT
);
118 assert(!ctf_pos_packet(&dummy
));
120 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
121 memcpy(ctf_get_pos_addr(pos
), uuid
, 16);
122 ctf_move_pos(pos
, 16 * CHAR_BIT
);
126 void write_packet_context(struct ctf_stream_pos
*pos
)
128 struct ctf_stream_pos dummy
;
131 ctf_dummy_pos(pos
, &dummy
);
132 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
133 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
134 assert(!ctf_pos_packet(&dummy
));
136 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
137 *(uint32_t *) ctf_get_pos_addr(pos
) = -1U; /* Not known yet */
138 pos
->content_size_loc
= (uint32_t *) ctf_get_pos_addr(pos
);
139 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
142 ctf_dummy_pos(pos
, &dummy
);
143 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
144 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
145 assert(!ctf_pos_packet(&dummy
));
147 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
148 *(uint32_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
149 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
153 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
154 char **tline
, size_t len
, size_t *tlen
,
157 unsigned long sec
, usec
;
163 /* Only need to be executed on first pass (dummy) */
165 /* Extract time from input line */
166 ret
= sscanf(line
, "[%lu.%lu] ", &sec
, &usec
);
168 *tline
= strchr(line
, ']');
171 if ((*tline
)[0] == ' ') {
174 *tlen
= len
+ line
- *tline
;
175 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
179 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
181 *(uint32_t *) ctf_get_pos_addr(pos
) = *ts
;
182 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
186 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
188 struct ctf_stream_pos dummy
;
190 char *tline
= line
; /* tline is start of text, after timestamp */
194 printf_debug("read: %s\n", line
);
196 ctf_dummy_pos(pos
, &dummy
);
197 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
198 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
199 ctf_move_pos(&dummy
, tlen
* CHAR_BIT
);
200 if (ctf_pos_packet(&dummy
)) {
201 ctf_pos_pad_packet(pos
);
202 write_packet_header(pos
, s_uuid
);
203 write_packet_context(pos
);
204 if (attempt
++ == 1) {
205 fprintf(stdout
, "[Error] Line too large for packet size (%zukB) (discarded)\n",
206 pos
->packet_size
/ CHAR_BIT
/ 1024);
212 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
213 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
214 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
215 ctf_move_pos(pos
, tlen
* CHAR_BIT
);
219 void trace_text(FILE *input
, int output
)
221 struct ctf_stream_pos pos
;
223 char *line
= NULL
, *nl
;
226 ctf_init_pos(&pos
, output
, O_RDWR
);
228 write_packet_header(&pos
, s_uuid
);
229 write_packet_context(&pos
);
231 len
= getline(&line
, &linesize
, input
);
234 nl
= strrchr(line
, '\n');
237 trace_string(line
, &pos
, nl
- line
+ 1);
245 fprintf(fp
, "BabelTrace Log Converter %u.%u\n",
246 BABELTRACE_VERSION_MAJOR
,
247 BABELTRACE_VERSION_MINOR
);
249 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
251 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
253 fprintf(fp
, " OUTPUT Output trace path\n");
255 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
260 int parse_args(int argc
, char **argv
)
264 for (i
= 1; i
< argc
; i
++) {
265 if (!strcmp(argv
[i
], "-t"))
268 s_outputname
= argv
[i
];
275 int main(int argc
, char **argv
)
277 int fd
, metadata_fd
, ret
;
282 ret
= parse_args(argc
, argv
);
288 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
294 dir
= opendir(s_outputname
);
305 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
306 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
309 goto error_closedirfd
;
312 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
313 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
316 goto error_closedatastream
;
318 metadata_fp
= fdopen(metadata_fd
, "w");
321 goto error_closemetadatafd
;
324 uuid_generate(s_uuid
);
325 print_metadata(metadata_fp
);
326 trace_text(stdin
, fd
);
332 error_closemetadatafd
:
333 ret
= close(metadata_fd
);
336 error_closedatastream
:
349 ret
= rmdir(s_outputname
);