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