Fix: Use list rather than ptr array for trace streams
[babeltrace.git] / converter / babeltrace-log.c
1 /*
2 * babeltrace-log.c
3 *
4 * BabelTrace - Convert Text Log to CTF
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <sys/mman.h>
33 #include <babeltrace/compat/dirent.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <inttypes.h>
41
42 #include <babeltrace/babeltrace-internal.h>
43 #include <babeltrace/ctf/types.h>
44 #include <babeltrace/compat/uuid.h>
45 #include <babeltrace/compat/utc.h>
46 #include <babeltrace/compat/stdio.h>
47 #include <babeltrace/endian.h>
48
49 #define NSEC_PER_USEC 1000UL
50 #define NSEC_PER_MSEC 1000000UL
51 #define NSEC_PER_SEC 1000000000ULL
52 #define USEC_PER_SEC 1000000UL
53
54 int babeltrace_debug, babeltrace_verbose;
55
56 static char *s_outputname;
57 static int s_timestamp;
58 static int s_help;
59 static unsigned char s_uuid[BABELTRACE_UUID_LEN];
60
61 /* Metadata format string */
62 static const char metadata_fmt[] =
63 "/* CTF 1.8 */\n"
64 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
65 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
66 "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
67 "\n"
68 "trace {\n"
69 " major = %u;\n" /* major (e.g. 0) */
70 " minor = %u;\n" /* minor (e.g. 1) */
71 " uuid = \"%s\";\n" /* UUID */
72 " byte_order = %s;\n" /* be or le */
73 " packet.header := struct {\n"
74 " uint32_t magic;\n"
75 " uint8_t uuid[16];\n"
76 " };\n"
77 "};\n"
78 "\n"
79 "stream {\n"
80 " packet.context := struct {\n"
81 " uint64_t content_size;\n"
82 " uint64_t packet_size;\n"
83 " };\n"
84 "%s" /* Stream event header (opt.) */
85 "};\n"
86 "\n"
87 "event {\n"
88 " name = string;\n"
89 " fields := struct { string str; };\n"
90 "};\n";
91
92 static const char metadata_stream_event_header_timestamp[] =
93 " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
94 " event.header := struct {\n"
95 " uint64_t timestamp;\n"
96 " };\n";
97
98 static
99 void print_metadata(FILE *fp)
100 {
101 char uuid_str[BABELTRACE_UUID_STR_LEN];
102 unsigned int major = 0, minor = 0;
103 int ret;
104
105 ret = sscanf(VERSION, "%u.%u", &major, &minor);
106 if (ret != 2)
107 fprintf(stderr, "[warning] Incorrect babeltrace version format\n.");
108 bt_uuid_unparse(s_uuid, uuid_str);
109 fprintf(fp, metadata_fmt,
110 major,
111 minor,
112 uuid_str,
113 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
114 s_timestamp ? metadata_stream_event_header_timestamp : "");
115 }
116
117 static
118 void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid)
119 {
120 struct ctf_stream_pos dummy;
121
122 /* magic */
123 ctf_dummy_pos(pos, &dummy);
124 if (!ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
125 goto error;
126 if (!ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
127 goto error;
128 assert(!ctf_pos_packet(&dummy));
129
130 if (!ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT))
131 goto error;
132 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
133 if (!ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT))
134 goto error;
135
136 /* uuid */
137 ctf_dummy_pos(pos, &dummy);
138 if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
139 goto error;
140 if (!ctf_move_pos(&dummy, 16 * CHAR_BIT))
141 goto error;
142 assert(!ctf_pos_packet(&dummy));
143
144 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
145 goto error;
146 memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
147 if (!ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT))
148 goto error;
149 return;
150
151 error:
152 fprintf(stderr, "[error] Out of packet bounds when writing packet header\n");
153 abort();
154 }
155
156 static
157 void write_packet_context(struct ctf_stream_pos *pos)
158 {
159 struct ctf_stream_pos dummy;
160
161 /* content_size */
162 ctf_dummy_pos(pos, &dummy);
163 if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
164 goto error;
165 if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
166 goto error;
167 assert(!ctf_pos_packet(&dummy));
168
169 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
170 goto error;
171 *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
172 pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
173 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
174 goto error;
175
176 /* packet_size */
177 ctf_dummy_pos(pos, &dummy);
178 if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
179 goto error;
180 if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
181 goto error;
182 assert(!ctf_pos_packet(&dummy));
183
184 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
185 goto error;
186 *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
187 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
188 goto error;
189 return;
190
191 error:
192 fprintf(stderr, "[error] Out of packet bounds when writing packet context\n");
193 abort();
194 }
195
196 static
197 void write_event_header(struct ctf_stream_pos *pos, char *line,
198 char **tline, size_t len, size_t *tlen,
199 uint64_t *ts)
200 {
201 if (!s_timestamp)
202 return;
203
204 /* Only need to be executed on first pass (dummy) */
205 if (pos->dummy) {
206 int has_timestamp = 0;
207 unsigned long sec, usec, msec;
208 unsigned int year, mon, mday, hour, min;
209
210 /* Extract time from input line */
211 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
212 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
213 /*
214 * Default CTF clock has 1GHz frequency. Convert
215 * from usec to nsec.
216 */
217 *ts *= NSEC_PER_USEC;
218 has_timestamp = 1;
219 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
220 &year, &mon, &mday, &hour, &min,
221 &sec, &msec) == 7) {
222 time_t ep_sec;
223 struct tm ti;
224
225 memset(&ti, 0, sizeof(ti));
226 ti.tm_year = year - 1900; /* from 1900 */
227 ti.tm_mon = mon - 1; /* 0 to 11 */
228 ti.tm_mday = mday;
229 ti.tm_hour = hour;
230 ti.tm_min = min;
231 ti.tm_sec = sec;
232
233 ep_sec = babeltrace_timegm(&ti);
234 if (ep_sec != (time_t) -1) {
235 *ts = (uint64_t) ep_sec * NSEC_PER_SEC
236 + (uint64_t) msec * NSEC_PER_MSEC;
237 }
238 has_timestamp = 1;
239 }
240 if (has_timestamp) {
241 *tline = strchr(line, ']');
242 assert(*tline);
243 (*tline)++;
244 if ((*tline)[0] == ' ') {
245 (*tline)++;
246 }
247 *tlen = len + line - *tline;
248 }
249 }
250 /* timestamp */
251 if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
252 goto error;
253 if (!pos->dummy)
254 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
255 if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
256 goto error;
257 return;
258
259 error:
260 fprintf(stderr, "[error] Out of packet bounds when writing event header\n");
261 abort();
262 }
263
264 static
265 void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
266 {
267 struct ctf_stream_pos dummy;
268 int attempt = 0;
269 char *tline = line; /* tline is start of text, after timestamp */
270 size_t tlen = len;
271 uint64_t ts = 0;
272
273 printf_debug("read: %s\n", line);
274
275 for (;;) {
276 int packet_filled = 0;
277
278 ctf_dummy_pos(pos, &dummy);
279 write_event_header(&dummy, line, &tline, len, &tlen, &ts);
280 if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
281 packet_filled = 1;
282 if (!ctf_move_pos(&dummy, tlen * CHAR_BIT))
283 packet_filled = 1;
284 if (packet_filled || ctf_pos_packet(&dummy)) {
285 ctf_pos_pad_packet(pos);
286 write_packet_header(pos, s_uuid);
287 write_packet_context(pos);
288 if (attempt++ == 1) {
289 fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
290 pos->packet_size / CHAR_BIT / 1024);
291 return;
292 }
293 continue;
294 } else {
295 break;
296 }
297 }
298
299 write_event_header(pos, line, &tline, len, &tlen, &ts);
300 if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
301 goto error;
302 memcpy(ctf_get_pos_addr(pos), tline, tlen);
303 if (!ctf_move_pos(pos, tlen * CHAR_BIT))
304 goto error;
305 return;
306
307 error:
308 fprintf(stderr, "[error] Out of packet bounds when writing event payload\n");
309 abort();
310 }
311
312 static
313 void trace_text(FILE *input, int output)
314 {
315 struct ctf_stream_pos pos;
316 ssize_t len;
317 char *line = NULL, *nl;
318 size_t linesize = 0;
319 int ret;
320
321 memset(&pos, 0, sizeof(pos));
322 ret = ctf_init_pos(&pos, NULL, output, O_RDWR);
323 if (ret) {
324 fprintf(stderr, "Error in ctf_init_pos\n");
325 return;
326 }
327 ctf_packet_seek(&pos.parent, 0, SEEK_CUR);
328 write_packet_header(&pos, s_uuid);
329 write_packet_context(&pos);
330 for (;;) {
331 len = bt_getline(&line, &linesize, input);
332 if (len < 0)
333 break;
334 nl = strrchr(line, '\n');
335 if (nl) {
336 *nl = '\0';
337 trace_string(line, &pos, nl - line + 1);
338 } else {
339 trace_string(line, &pos, strlen(line) + 1);
340 }
341 }
342 ret = ctf_fini_pos(&pos);
343 if (ret) {
344 fprintf(stderr, "Error in ctf_fini_pos\n");
345 }
346 }
347
348 static
349 void usage(FILE *fp)
350 {
351 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
352 fprintf(fp, "\n");
353 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
354 fprintf(fp, "\n");
355 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
356 fprintf(fp, "\n");
357 fprintf(fp, " OUTPUT Output trace path\n");
358 fprintf(fp, "\n");
359 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
360 fprintf(fp, " (format: [YYYY-MM-DD HH:MM:SS.MS] string\\n)\n");
361 fprintf(fp, "\n");
362 }
363
364 static
365 int parse_args(int argc, char **argv)
366 {
367 int i;
368
369 for (i = 1; i < argc; i++) {
370 if (!strcmp(argv[i], "-t"))
371 s_timestamp = 1;
372 else if (!strcmp(argv[i], "-h")) {
373 s_help = 1;
374 return 0;
375 } else if (argv[i][0] == '-')
376 return -EINVAL;
377 else
378 s_outputname = argv[i];
379 }
380 if (!s_outputname)
381 return -EINVAL;
382 return 0;
383 }
384
385 int main(int argc, char **argv)
386 {
387 int fd, metadata_fd, ret;
388 DIR *dir;
389 int dir_fd;
390 FILE *metadata_fp;
391
392 ret = parse_args(argc, argv);
393 if (ret) {
394 fprintf(stderr, "Error: invalid argument.\n");
395 usage(stderr);
396 goto error;
397 }
398
399 if (s_help) {
400 usage(stdout);
401 exit(EXIT_SUCCESS);
402 }
403
404 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
405 if (ret) {
406 perror("mkdir");
407 goto error;
408 }
409
410 dir = opendir(s_outputname);
411 if (!dir) {
412 perror("opendir");
413 goto error_rmdir;
414 }
415 dir_fd = bt_dirfd(dir);
416 if (dir_fd < 0) {
417 perror("dirfd");
418 goto error_closedir;
419 }
420
421 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
422 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
423 if (fd < 0) {
424 perror("openat");
425 goto error_closedirfd;
426 }
427
428 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
429 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
430 if (metadata_fd < 0) {
431 perror("openat");
432 goto error_closedatastream;
433 }
434 metadata_fp = fdopen(metadata_fd, "w");
435 if (!metadata_fp) {
436 perror("fdopen");
437 goto error_closemetadatafd;
438 }
439
440 bt_uuid_generate(s_uuid);
441 print_metadata(metadata_fp);
442 trace_text(stdin, fd);
443
444 ret = close(fd);
445 if (ret)
446 perror("close");
447 exit(EXIT_SUCCESS);
448
449 /* error handling */
450 error_closemetadatafd:
451 ret = close(metadata_fd);
452 if (ret)
453 perror("close");
454 error_closedatastream:
455 ret = close(fd);
456 if (ret)
457 perror("close");
458 error_closedirfd:
459 ret = close(dir_fd);
460 if (ret)
461 perror("close");
462 error_closedir:
463 ret = closedir(dir);
464 if (ret)
465 perror("closedir");
466 error_rmdir:
467 ret = rmdir(s_outputname);
468 if (ret)
469 perror("rmdir");
470 error:
471 exit(EXIT_FAILURE);
472 }
This page took 0.038204 seconds and 4 git commands to generate.