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