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