b320cbb0cd280484e91522b4a4c3357da71461e7
[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 #ifndef UUID_STR_LEN
39 #define UUID_STR_LEN 37 /* With \0 */
40 #endif
41
42 /* Metadata format string */
43 static const char metadata_fmt[] =
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"
48 " major = %s;\n" /* major (e.g. 0) */
49 " minor = %s;\n" /* minor (e.g. 1) */
50 " uuid = \"%s\";\n" /* UUID */
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
70 int babeltrace_debug, babeltrace_verbose;
71
72 static uuid_t s_uuid;
73
74 static
75 void 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,
81 __stringify(BABELTRACE_VERSION_MAJOR),
82 __stringify(BABELTRACE_VERSION_MINOR),
83 uuid_str,
84 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be");
85 }
86
87
88 static
89 void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
90 {
91 struct ctf_stream_pos dummy;
92
93 /* magic */
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));
98
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);
102
103 /* trace_uuid */
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);
112 }
113
114 static
115 void write_packet_context(struct ctf_stream_pos *pos)
116 {
117 struct ctf_stream_pos dummy;
118
119 /* content_size */
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));
124
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);
129
130 /* packet_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) = pos->packet_size;
138 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
139 }
140
141 static
142 void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
143 {
144 struct ctf_stream_pos dummy;
145 int attempt = 0;
146
147 printf_debug("read: %s\n", line);
148 retry:
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)) {
153 ctf_pos_pad_packet(pos);
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
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);
167 }
168
169 static
170 void trace_text(FILE *input, int output)
171 {
172 struct ctf_stream_pos pos;
173 ssize_t len;
174 char *line = NULL, *nl;
175 size_t linesize;
176
177 ctf_init_pos(&pos, output, O_RDWR);
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 }
190 ctf_fini_pos(&pos);
191 }
192
193 static 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
207 int main(int argc, char **argv)
208 {
209 int fd, metadata_fd, ret;
210 char *outputname;
211 DIR *dir;
212 int dir_fd;
213 FILE *metadata_fp;
214
215 if (argc < 2) {
216 usage(stdout);
217 goto error;
218 }
219 outputname = argv[1];
220
221 ret = mkdir(outputname, S_IRWXU|S_IRWXG);
222 if (ret) {
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;
243 }
244
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);
259 trace_text(stdin, fd);
260
261 close(fd);
262 exit(EXIT_SUCCESS);
263
264 /* error handling */
265 error_closemetadatafd:
266 ret = close(metadata_fd);
267 if (ret)
268 perror("close");
269 error_closedatastream:
270 ret = close(fd);
271 if (ret)
272 perror("close");
273 error_closedirfd:
274 ret = close(dir_fd);
275 if (ret)
276 perror("close");
277 error_closedir:
278 ret = closedir(dir);
279 if (ret)
280 perror("closedir");
281 error_rmdir:
282 ret = rmdir(outputname);
283 if (ret)
284 perror("rmdir");
285 error:
286 exit(EXIT_FAILURE);
287 }
This page took 0.033669 seconds and 3 git commands to generate.