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