7f31d015226548969abfa515046ece454732dce9
[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 * Depends on glibc 2.10 for getline().
21 */
22
23 #define _GNU_SOURCE
24 #include <config.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <sys/mman.h>
29 #include <dirent.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <string.h>
36
37 #include <babeltrace/babeltrace-internal.h>
38 #include <babeltrace/ctf/types.h>
39 #include <babeltrace/uuid.h>
40 #include <babeltrace/endian.h>
41
42 #define USEC_PER_SEC 1000000UL
43
44 int babeltrace_debug, babeltrace_verbose;
45
46 static char *s_outputname;
47 static int s_timestamp;
48 static int s_help;
49 static unsigned char s_uuid[BABELTRACE_UUID_LEN];
50
51 /* Metadata format string */
52 static const char metadata_fmt[] =
53 "/* CTF 1.8 */\n"
54 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
55 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
56 "\n"
57 "trace {\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"
63 " uint32_t magic;\n"
64 " uint8_t uuid[16];\n"
65 " };\n"
66 "};\n"
67 "\n"
68 "stream {\n"
69 " packet.context := struct {\n"
70 " uint32_t content_size;\n"
71 " uint32_t packet_size;\n"
72 " };\n"
73 "%s" /* Stream event header (opt.) */
74 "};\n"
75 "\n"
76 "event {\n"
77 " name = string;\n"
78 " fields := struct { string str; };\n"
79 "};\n";
80
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"
85 " };\n";
86
87 static
88 void print_metadata(FILE *fp)
89 {
90 char uuid_str[BABELTRACE_UUID_STR_LEN];
91 unsigned int major = 0, minor = 0;
92 int ret;
93
94 ret = sscanf(VERSION, "%u.%u", &major, &minor);
95 if (ret != 2)
96 fprintf(stderr, "[warning] Incorrect babeltrace version format\n.");
97 babeltrace_uuid_unparse(s_uuid, uuid_str);
98 fprintf(fp, metadata_fmt,
99 major,
100 minor,
101 uuid_str,
102 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be",
103 s_timestamp ? metadata_stream_event_header_timestamp : "");
104 }
105
106 static
107 void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid)
108 {
109 struct ctf_stream_pos dummy;
110
111 /* magic */
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));
116
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);
120
121 /* uuid */
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));
126
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);
130 }
131
132 static
133 void write_packet_context(struct ctf_stream_pos *pos)
134 {
135 struct ctf_stream_pos dummy;
136
137 /* content_size */
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));
142
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);
147
148 /* packet_size */
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));
153
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);
157 }
158
159 static
160 void write_event_header(struct ctf_stream_pos *pos, char *line,
161 char **tline, size_t len, size_t *tlen,
162 uint64_t *ts)
163 {
164 unsigned long sec, usec;
165 int ret;
166
167 if (!s_timestamp)
168 return;
169
170 /* Only need to be executed on first pass (dummy) */
171 if (pos->dummy) {
172 /* Extract time from input line */
173 ret = sscanf(line, "[%lu.%lu] ", &sec, &usec);
174 if (ret == 2) {
175 *tline = strchr(line, ']');
176 assert(*tline);
177 (*tline)++;
178 if ((*tline)[0] == ' ') {
179 (*tline)++;
180 }
181 *tlen = len + line - *tline;
182 *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
183 }
184 }
185 /* timestamp */
186 ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
187 if (!pos->dummy)
188 *(uint64_t *) ctf_get_pos_addr(pos) = *ts;
189 ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
190 }
191
192 static
193 void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
194 {
195 struct ctf_stream_pos dummy;
196 int attempt = 0;
197 char *tline = line; /* tline is start of text, after timestamp */
198 size_t tlen = len;
199 uint64_t ts = 0;
200
201 printf_debug("read: %s\n", line);
202 retry:
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);
214 return;
215 }
216 goto retry;
217 }
218
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);
223 }
224
225 static
226 void trace_text(FILE *input, int output)
227 {
228 struct ctf_stream_pos pos;
229 ssize_t len;
230 char *line = NULL, *nl;
231 size_t linesize;
232
233 memset(&pos, 0, sizeof(pos));
234 ctf_init_pos(&pos, output, O_RDWR);
235
236 write_packet_header(&pos, s_uuid);
237 write_packet_context(&pos);
238 for (;;) {
239 len = getline(&line, &linesize, input);
240 if (len < 0)
241 break;
242 nl = strrchr(line, '\n');
243 if (nl)
244 *nl = '\0';
245 trace_string(line, &pos, nl - line + 1);
246 }
247 ctf_fini_pos(&pos);
248 }
249
250 static
251 void usage(FILE *fp)
252 {
253 fprintf(fp, "BabelTrace Log Converter %s\n", VERSION);
254 fprintf(fp, "\n");
255 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
256 fprintf(fp, "\n");
257 fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n");
258 fprintf(fp, "\n");
259 fprintf(fp, " OUTPUT Output trace path\n");
260 fprintf(fp, "\n");
261 fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n");
262 fprintf(fp, "\n");
263 }
264
265 static
266 int parse_args(int argc, char **argv)
267 {
268 int i;
269
270 for (i = 1; i < argc; i++) {
271 if (!strcmp(argv[i], "-t"))
272 s_timestamp = 1;
273 else if (!strcmp(argv[i], "-h")) {
274 s_help = 1;
275 return 0;
276 } else if (argv[i][0] == '-')
277 return -EINVAL;
278 else
279 s_outputname = argv[i];
280 }
281 if (!s_outputname)
282 return -EINVAL;
283 return 0;
284 }
285
286 int main(int argc, char **argv)
287 {
288 int fd, metadata_fd, ret;
289 DIR *dir;
290 int dir_fd;
291 FILE *metadata_fp;
292
293 ret = parse_args(argc, argv);
294 if (ret) {
295 fprintf(stderr, "Error: invalid argument.\n");
296 usage(stderr);
297 goto error;
298 }
299
300 if (s_help) {
301 usage(stdout);
302 exit(EXIT_SUCCESS);
303 }
304
305 ret = mkdir(s_outputname, S_IRWXU|S_IRWXG);
306 if (ret) {
307 perror("mkdir");
308 goto error;
309 }
310
311 dir = opendir(s_outputname);
312 if (!dir) {
313 perror("opendir");
314 goto error_rmdir;
315 }
316 dir_fd = dirfd(dir);
317 if (dir_fd < 0) {
318 perror("dirfd");
319 goto error_closedir;
320 }
321
322 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
323 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
324 if (fd < 0) {
325 perror("openat");
326 goto error_closedirfd;
327 }
328
329 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
330 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
331 if (fd < 0) {
332 perror("openat");
333 goto error_closedatastream;
334 }
335 metadata_fp = fdopen(metadata_fd, "w");
336 if (!metadata_fp) {
337 perror("fdopen");
338 goto error_closemetadatafd;
339 }
340
341 babeltrace_uuid_generate(s_uuid);
342 print_metadata(metadata_fp);
343 trace_text(stdin, fd);
344
345 close(fd);
346 exit(EXIT_SUCCESS);
347
348 /* error handling */
349 error_closemetadatafd:
350 ret = close(metadata_fd);
351 if (ret)
352 perror("close");
353 error_closedatastream:
354 ret = close(fd);
355 if (ret)
356 perror("close");
357 error_closedirfd:
358 ret = close(dir_fd);
359 if (ret)
360 perror("close");
361 error_closedir:
362 ret = closedir(dir);
363 if (ret)
364 perror("closedir");
365 error_rmdir:
366 ret = rmdir(s_outputname);
367 if (ret)
368 perror("rmdir");
369 error:
370 exit(EXIT_FAILURE);
371 }
This page took 0.036039 seconds and 3 git commands to generate.