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().
24 #include <sys/types.h>
34 #include <uuid/uuid.h>
38 #include <babeltrace/babeltrace-internal.h>
39 #include <babeltrace/ctf/types.h>
41 #define USEC_PER_SEC 1000000UL
44 #define UUID_STR_LEN 37 /* With \0 */
47 int babeltrace_debug
, babeltrace_verbose
;
49 static char *s_outputname
;
50 static int s_timestamp
;
54 /* Metadata format string */
55 static const char metadata_fmt
[] =
57 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
58 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
61 " major = %u;\n" /* major (e.g. 0) */
62 " minor = %u;\n" /* minor (e.g. 1) */
63 " uuid = \"%s\";\n" /* UUID */
64 " byte_order = %s;\n" /* be or le */
65 " packet.header := struct {\n"
67 " uint8_t uuid[16];\n"
72 " packet.context := struct {\n"
73 " uint32_t content_size;\n"
74 " uint32_t packet_size;\n"
76 "%s" /* Stream event header (opt.) */
81 " fields := struct { string str; };\n"
84 static const char metadata_stream_event_header_timestamp
[] =
85 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
86 " event.header := struct {\n"
87 " uint64_t timestamp;\n"
91 void print_metadata(FILE *fp
)
93 char uuid_str
[UUID_STR_LEN
];
94 unsigned int major
= 0, minor
= 0;
97 ret
= sscanf(VERSION
, "%u.%u", &major
, &minor
);
99 fprintf(stderr
, "[warning] Incorrect babeltrace version format\n.");
100 uuid_unparse(s_uuid
, uuid_str
);
101 fprintf(fp
, metadata_fmt
,
105 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
106 s_timestamp
? metadata_stream_event_header_timestamp
: "");
110 void write_packet_header(struct ctf_stream_pos
*pos
, uuid_t uuid
)
112 struct ctf_stream_pos dummy
;
115 ctf_dummy_pos(pos
, &dummy
);
116 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
117 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
118 assert(!ctf_pos_packet(&dummy
));
120 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
121 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
122 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
125 ctf_dummy_pos(pos
, &dummy
);
126 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
127 ctf_move_pos(&dummy
, 16 * CHAR_BIT
);
128 assert(!ctf_pos_packet(&dummy
));
130 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
131 memcpy(ctf_get_pos_addr(pos
), uuid
, 16);
132 ctf_move_pos(pos
, 16 * CHAR_BIT
);
136 void write_packet_context(struct ctf_stream_pos
*pos
)
138 struct ctf_stream_pos dummy
;
141 ctf_dummy_pos(pos
, &dummy
);
142 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
143 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
144 assert(!ctf_pos_packet(&dummy
));
146 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
147 *(uint32_t *) ctf_get_pos_addr(pos
) = -1U; /* Not known yet */
148 pos
->content_size_loc
= (uint32_t *) ctf_get_pos_addr(pos
);
149 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
152 ctf_dummy_pos(pos
, &dummy
);
153 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
154 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
155 assert(!ctf_pos_packet(&dummy
));
157 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
158 *(uint32_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
159 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
163 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
164 char **tline
, size_t len
, size_t *tlen
,
167 unsigned long sec
, usec
;
173 /* Only need to be executed on first pass (dummy) */
175 /* Extract time from input line */
176 ret
= sscanf(line
, "[%lu.%lu] ", &sec
, &usec
);
178 *tline
= strchr(line
, ']');
181 if ((*tline
)[0] == ' ') {
184 *tlen
= len
+ line
- *tline
;
185 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
189 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
191 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
192 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
196 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
198 struct ctf_stream_pos dummy
;
200 char *tline
= line
; /* tline is start of text, after timestamp */
204 printf_debug("read: %s\n", line
);
206 ctf_dummy_pos(pos
, &dummy
);
207 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
208 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
209 ctf_move_pos(&dummy
, tlen
* CHAR_BIT
);
210 if (ctf_pos_packet(&dummy
)) {
211 ctf_pos_pad_packet(pos
);
212 write_packet_header(pos
, s_uuid
);
213 write_packet_context(pos
);
214 if (attempt
++ == 1) {
215 fprintf(stderr
, "[Error] Line too large for packet size (%zukB) (discarded)\n",
216 pos
->packet_size
/ CHAR_BIT
/ 1024);
222 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
223 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
224 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
225 ctf_move_pos(pos
, tlen
* CHAR_BIT
);
229 void trace_text(FILE *input
, int output
)
231 struct ctf_stream_pos pos
;
233 char *line
= NULL
, *nl
;
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);
255 fprintf(fp
, "BabelTrace Log Converter %s\n", VERSION
);
257 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
259 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
261 fprintf(fp
, " OUTPUT Output trace path\n");
263 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
268 int parse_args(int argc
, char **argv
)
272 for (i
= 1; i
< argc
; i
++) {
273 if (!strcmp(argv
[i
], "-t"))
275 else if (!strcmp(argv
[i
], "-h")) {
278 } else if (argv
[i
][0] == '-')
281 s_outputname
= argv
[i
];
288 int main(int argc
, char **argv
)
290 int fd
, metadata_fd
, ret
;
295 ret
= parse_args(argc
, argv
);
297 fprintf(stderr
, "Error: invalid argument.\n");
307 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
313 dir
= opendir(s_outputname
);
324 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
325 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
328 goto error_closedirfd
;
331 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
332 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
335 goto error_closedatastream
;
337 metadata_fp
= fdopen(metadata_fd
, "w");
340 goto error_closemetadatafd
;
343 uuid_generate(s_uuid
);
344 print_metadata(metadata_fp
);
345 trace_text(stdin
, fd
);
351 error_closemetadatafd
:
352 ret
= close(metadata_fd
);
355 error_closedatastream
:
368 ret
= rmdir(s_outputname
);