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>
36 #include <babeltrace/babeltrace-internal.h>
37 #include <babeltrace/ctf/types.h>
38 #include <babeltrace/uuid.h>
39 #include <babeltrace/endian.h>
41 #define USEC_PER_SEC 1000000UL
43 int babeltrace_debug
, babeltrace_verbose
;
45 static char *s_outputname
;
46 static int s_timestamp
;
48 static unsigned char s_uuid
[BABELTRACE_UUID_LEN
];
50 /* Metadata format string */
51 static const char metadata_fmt
[] =
53 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
54 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
57 " major = %u;\n" /* major (e.g. 0) */
58 " minor = %u;\n" /* minor (e.g. 1) */
59 " uuid = \"%s\";\n" /* UUID */
60 " byte_order = %s;\n" /* be or le */
61 " packet.header := struct {\n"
63 " uint8_t uuid[16];\n"
68 " packet.context := struct {\n"
69 " uint32_t content_size;\n"
70 " uint32_t packet_size;\n"
72 "%s" /* Stream event header (opt.) */
77 " fields := struct { string str; };\n"
80 static const char metadata_stream_event_header_timestamp
[] =
81 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
82 " event.header := struct {\n"
83 " uint64_t timestamp;\n"
87 void print_metadata(FILE *fp
)
89 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
90 unsigned int major
= 0, minor
= 0;
93 ret
= sscanf(VERSION
, "%u.%u", &major
, &minor
);
95 fprintf(stderr
, "[warning] Incorrect babeltrace version format\n.");
96 babeltrace_uuid_unparse(s_uuid
, uuid_str
);
97 fprintf(fp
, metadata_fmt
,
101 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be",
102 s_timestamp
? metadata_stream_event_header_timestamp
: "");
106 void write_packet_header(struct ctf_stream_pos
*pos
, unsigned char *uuid
)
108 struct ctf_stream_pos dummy
;
111 ctf_dummy_pos(pos
, &dummy
);
112 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
113 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
114 assert(!ctf_pos_packet(&dummy
));
116 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
117 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
118 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
121 ctf_dummy_pos(pos
, &dummy
);
122 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
123 ctf_move_pos(&dummy
, 16 * CHAR_BIT
);
124 assert(!ctf_pos_packet(&dummy
));
126 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
127 memcpy(ctf_get_pos_addr(pos
), uuid
, BABELTRACE_UUID_LEN
);
128 ctf_move_pos(pos
, BABELTRACE_UUID_LEN
* CHAR_BIT
);
132 void write_packet_context(struct ctf_stream_pos
*pos
)
134 struct ctf_stream_pos dummy
;
137 ctf_dummy_pos(pos
, &dummy
);
138 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
139 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
140 assert(!ctf_pos_packet(&dummy
));
142 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
143 *(uint32_t *) ctf_get_pos_addr(pos
) = -1U; /* Not known yet */
144 pos
->content_size_loc
= (uint32_t *) ctf_get_pos_addr(pos
);
145 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
148 ctf_dummy_pos(pos
, &dummy
);
149 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
150 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
151 assert(!ctf_pos_packet(&dummy
));
153 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
154 *(uint32_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
155 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
159 void write_event_header(struct ctf_stream_pos
*pos
, char *line
,
160 char **tline
, size_t len
, size_t *tlen
,
163 unsigned long sec
, usec
;
169 /* Only need to be executed on first pass (dummy) */
171 /* Extract time from input line */
172 ret
= sscanf(line
, "[%lu.%lu] ", &sec
, &usec
);
174 *tline
= strchr(line
, ']');
177 if ((*tline
)[0] == ' ') {
180 *tlen
= len
+ line
- *tline
;
181 *ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
185 ctf_align_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
187 *(uint64_t *) ctf_get_pos_addr(pos
) = *ts
;
188 ctf_move_pos(pos
, sizeof(uint64_t) * CHAR_BIT
);
192 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
194 struct ctf_stream_pos dummy
;
196 char *tline
= line
; /* tline is start of text, after timestamp */
200 printf_debug("read: %s\n", line
);
202 ctf_dummy_pos(pos
, &dummy
);
203 write_event_header(&dummy
, line
, &tline
, len
, &tlen
, &ts
);
204 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
205 ctf_move_pos(&dummy
, tlen
* CHAR_BIT
);
206 if (ctf_pos_packet(&dummy
)) {
207 ctf_pos_pad_packet(pos
);
208 write_packet_header(pos
, s_uuid
);
209 write_packet_context(pos
);
210 if (attempt
++ == 1) {
211 fprintf(stderr
, "[Error] Line too large for packet size (%zukB) (discarded)\n",
212 pos
->packet_size
/ CHAR_BIT
/ 1024);
218 write_event_header(pos
, line
, &tline
, len
, &tlen
, &ts
);
219 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
220 memcpy(ctf_get_pos_addr(pos
), tline
, tlen
);
221 ctf_move_pos(pos
, tlen
* CHAR_BIT
);
225 void trace_text(FILE *input
, int output
)
227 struct ctf_stream_pos pos
;
229 char *line
= NULL
, *nl
;
232 ctf_init_pos(&pos
, output
, O_RDWR
);
234 write_packet_header(&pos
, s_uuid
);
235 write_packet_context(&pos
);
237 len
= getline(&line
, &linesize
, input
);
240 nl
= strrchr(line
, '\n');
243 trace_string(line
, &pos
, nl
- line
+ 1);
251 fprintf(fp
, "BabelTrace Log Converter %s\n", VERSION
);
253 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
255 fprintf(fp
, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
257 fprintf(fp
, " OUTPUT Output trace path\n");
259 fprintf(fp
, " -t With timestamps (format: [sec.usec] string\\n)\n");
264 int parse_args(int argc
, char **argv
)
268 for (i
= 1; i
< argc
; i
++) {
269 if (!strcmp(argv
[i
], "-t"))
271 else if (!strcmp(argv
[i
], "-h")) {
274 } else if (argv
[i
][0] == '-')
277 s_outputname
= argv
[i
];
284 int main(int argc
, char **argv
)
286 int fd
, metadata_fd
, ret
;
291 ret
= parse_args(argc
, argv
);
293 fprintf(stderr
, "Error: invalid argument.\n");
303 ret
= mkdir(s_outputname
, S_IRWXU
|S_IRWXG
);
309 dir
= opendir(s_outputname
);
320 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
321 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
324 goto error_closedirfd
;
327 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
328 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
331 goto error_closedatastream
;
333 metadata_fp
= fdopen(metadata_fd
, "w");
336 goto error_closemetadatafd
;
339 babeltrace_uuid_generate(s_uuid
);
340 print_metadata(metadata_fp
);
341 trace_text(stdin
, fd
);
347 error_closemetadatafd
:
348 ret
= close(metadata_fd
);
351 error_closedatastream
:
364 ret
= rmdir(s_outputname
);