Cleanup babeltrace-log
[babeltrace.git] / converter / babeltrace-log.c
CommitLineData
8c572eba 1/*
b522ac18 2 * babeltrace-log.c
8c572eba 3 *
b522ac18 4 * BabelTrace - Convert Text Log to CTF
8c572eba
MD
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
90219914
MD
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <sys/mman.h>
989c73bc 25#include <dirent.h>
90219914 26#include <stdio.h>
989c73bc 27#include <stdlib.h>
90219914
MD
28#include <stdint.h>
29#include <unistd.h>
30#include <errno.h>
31#include <uuid/uuid.h>
32#include <string.h>
989c73bc 33#include <endian.h>
90219914 34
8c572eba
MD
35#include <babeltrace/babeltrace.h>
36#include <babeltrace/ctf/types.h>
37
b522ac18
MD
38#ifndef UUID_STR_LEN
39#define UUID_STR_LEN 37 /* With \0 */
40#endif
41
42/* Metadata format string */
43static const char metadata_fmt[] =
989c73bc
MD
44"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
45"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
46"\n"
47"trace {\n"
c818559a
MD
48" major = %u;\n" /* major (e.g. 0) */
49" minor = %u;\n" /* minor (e.g. 1) */
b522ac18 50" uuid = \"%s\";\n" /* UUID */
989c73bc
MD
51" byte_order = %s;\n" /* be or le */
52" packet.header := struct {\n"
53" uint32_t magic;\n"
54" uint8_t trace_uuid[16];\n"
55" };\n"
56"};\n"
57"\n"
58"stream {\n"
59" packet.context := struct {\n"
60" uint32_t content_size;\n"
61" uint32_t packet_size;\n"
62" };\n"
63"};\n"
64"\n"
65"event {\n"
66" name = string;\n"
67" fields := struct { string str; };\n"
68"};\n";
69
c8b219a3 70int babeltrace_debug, babeltrace_verbose;
8c572eba
MD
71
72static uuid_t s_uuid;
73
b522ac18
MD
74static
75void print_metadata(FILE *fp)
76{
77 char uuid_str[UUID_STR_LEN];
78
79 uuid_unparse(s_uuid, uuid_str);
80 fprintf(fp, metadata_fmt,
c818559a
MD
81 BABELTRACE_VERSION_MAJOR,
82 BABELTRACE_VERSION_MINOR,
b522ac18
MD
83 uuid_str,
84 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be");
85}
86
87
8c572eba 88static
46322b33 89void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
8c572eba 90{
46322b33 91 struct ctf_stream_pos dummy;
8c572eba
MD
92
93 /* magic */
46322b33
MD
94 ctf_dummy_pos(pos, &dummy);
95 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
96 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
97 assert(!ctf_pos_packet(&dummy));
8c572eba 98
46322b33
MD
99 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
100 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
101 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
102
103 /* trace_uuid */
46322b33
MD
104 ctf_dummy_pos(pos, &dummy);
105 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
106 ctf_move_pos(&dummy, 16 * CHAR_BIT);
107 assert(!ctf_pos_packet(&dummy));
108
109 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
110 memcpy(ctf_get_pos_addr(pos), uuid, 16);
111 ctf_move_pos(pos, 16 * CHAR_BIT);
8c572eba
MD
112}
113
114static
46322b33 115void write_packet_context(struct ctf_stream_pos *pos)
8c572eba 116{
46322b33 117 struct ctf_stream_pos dummy;
8c572eba
MD
118
119 /* content_size */
46322b33
MD
120 ctf_dummy_pos(pos, &dummy);
121 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
122 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
123 assert(!ctf_pos_packet(&dummy));
8c572eba 124
46322b33
MD
125 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
126 *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */
127 pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos);
128 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
129
130 /* packet_size */
46322b33
MD
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));
8c572eba 135
46322b33
MD
136 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
137 *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size;
138 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
139}
140
141static
46322b33 142void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
8c572eba 143{
46322b33 144 struct ctf_stream_pos dummy;
8c572eba
MD
145 int attempt = 0;
146
147 printf_debug("read: %s\n", line);
148retry:
46322b33
MD
149 ctf_dummy_pos(pos, &dummy);
150 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
151 ctf_move_pos(&dummy, len * CHAR_BIT);
152 if (ctf_pos_packet(&dummy)) {
46322b33 153 ctf_pos_pad_packet(pos);
8c572eba
MD
154 write_packet_header(pos, s_uuid);
155 write_packet_context(pos);
156 if (attempt++ == 1) {
157 fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n",
158 pos->packet_size / CHAR_BIT / 1024);
159 return;
160 }
161 goto retry;
162 }
163
46322b33
MD
164 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
165 memcpy(ctf_get_pos_addr(pos), line, len);
166 ctf_move_pos(pos, len * CHAR_BIT);
8c572eba
MD
167}
168
169static
170void trace_text(FILE *input, int output)
171{
46322b33 172 struct ctf_stream_pos pos;
8c572eba
MD
173 ssize_t len;
174 char *line = NULL, *nl;
175 size_t linesize;
176
989c73bc 177 ctf_init_pos(&pos, output, O_RDWR);
8c572eba
MD
178
179 write_packet_header(&pos, s_uuid);
180 write_packet_context(&pos);
181 for (;;) {
182 len = getline(&line, &linesize, input);
183 if (len < 0)
184 break;
185 nl = strrchr(line, '\n');
186 if (nl)
187 *nl = '\0';
188 trace_string(line, &pos, nl - line + 1);
189 }
46322b33 190 ctf_fini_pos(&pos);
8c572eba 191}
90219914 192
989c73bc
MD
193static void usage(FILE *fp)
194{
195 fprintf(fp, "BabelTrace Log Converter %u.%u\n",
196 BABELTRACE_VERSION_MAJOR,
197 BABELTRACE_VERSION_MINOR);
198 fprintf(fp, "\n");
199 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
200 fprintf(fp, "\n");
201 fprintf(fp, "usage : babeltrace-log OUTPUT\n");
202 fprintf(fp, "\n");
203 fprintf(fp, " OUTPUT Output trace path\n");
204 fprintf(fp, "\n");
205}
206
90219914
MD
207int main(int argc, char **argv)
208{
b522ac18 209 int fd, metadata_fd, ret;
989c73bc
MD
210 char *outputname;
211 DIR *dir;
212 int dir_fd;
b522ac18 213 FILE *metadata_fp;
90219914 214
989c73bc
MD
215 if (argc < 2) {
216 usage(stdout);
217 goto error;
90219914 218 }
989c73bc 219 outputname = argv[1];
90219914 220
989c73bc 221 ret = mkdir(outputname, S_IRWXU|S_IRWXG);
90219914 222 if (ret) {
989c73bc
MD
223 perror("mkdir");
224 goto error;
225 }
226
227 dir = opendir(outputname);
228 if (!dir) {
229 perror("opendir");
230 goto error_rmdir;
231 }
232 dir_fd = dirfd(dir);
233 if (dir_fd < 0) {
234 perror("dirfd");
235 goto error_closedir;
236 }
237
238 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
239 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
240 if (fd < 0) {
241 perror("openat");
242 goto error_closedirfd;
90219914 243 }
90219914 244
b522ac18
MD
245 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
246 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
247 if (fd < 0) {
248 perror("openat");
249 goto error_closedatastream;
250 }
251 metadata_fp = fdopen(metadata_fd, "w");
252 if (!metadata_fp) {
253 perror("fdopen");
254 goto error_closemetadatafd;
255 }
256
257 uuid_generate(s_uuid);
258 print_metadata(metadata_fp);
8c572eba 259 trace_text(stdin, fd);
989c73bc 260
90219914 261 close(fd);
989c73bc
MD
262 exit(EXIT_SUCCESS);
263
264 /* error handling */
b522ac18
MD
265error_closemetadatafd:
266 ret = close(metadata_fd);
267 if (ret)
268 perror("close");
269error_closedatastream:
270 ret = close(fd);
271 if (ret)
272 perror("close");
989c73bc
MD
273error_closedirfd:
274 ret = close(dir_fd);
275 if (ret)
276 perror("close");
277error_closedir:
278 ret = closedir(dir);
279 if (ret)
280 perror("closedir");
281error_rmdir:
282 ret = rmdir(outputname);
283 if (ret)
284 perror("rmdir");
285error:
286 exit(EXIT_FAILURE);
90219914 287}
This page took 0.034794 seconds and 4 git commands to generate.