aa855cc7cd95a2a9a62430e995a20399364d49fd
[babeltrace.git] / tests / test-dummytrace.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 /* TODO write content size */
154 ctf_pos_pad_packet(pos);
155 write_packet_header(pos, s_uuid);
156 write_packet_context(pos);
157 if (attempt++ == 1) {
158 fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n",
159 pos->packet_size / CHAR_BIT / 1024);
160 return;
161 }
162 goto retry;
163 }
164
165 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
166 memcpy(ctf_get_pos_addr(pos), line, len);
167 ctf_move_pos(pos, len * CHAR_BIT);
168 }
169
170 static
171 void trace_text(FILE *input, int output)
172 {
173 struct ctf_stream_pos pos;
174 ssize_t len;
175 char *line = NULL, *nl;
176 size_t linesize;
177
178 ctf_init_pos(&pos, output, O_RDWR);
179
180 write_packet_header(&pos, s_uuid);
181 write_packet_context(&pos);
182 for (;;) {
183 len = getline(&line, &linesize, input);
184 if (len < 0)
185 break;
186 nl = strrchr(line, '\n');
187 if (nl)
188 *nl = '\0';
189 trace_string(line, &pos, nl - line + 1);
190 }
191 ctf_fini_pos(&pos);
192 }
193
194 static void usage(FILE *fp)
195 {
196 fprintf(fp, "BabelTrace Log Converter %u.%u\n",
197 BABELTRACE_VERSION_MAJOR,
198 BABELTRACE_VERSION_MINOR);
199 fprintf(fp, "\n");
200 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
201 fprintf(fp, "\n");
202 fprintf(fp, "usage : babeltrace-log OUTPUT\n");
203 fprintf(fp, "\n");
204 fprintf(fp, " OUTPUT Output trace path\n");
205 fprintf(fp, "\n");
206 }
207
208 int main(int argc, char **argv)
209 {
210 int fd, metadata_fd, ret;
211 char *outputname;
212 DIR *dir;
213 int dir_fd;
214 FILE *metadata_fp;
215
216 if (argc < 2) {
217 usage(stdout);
218 goto error;
219 }
220 outputname = argv[1];
221
222 ret = mkdir(outputname, S_IRWXU|S_IRWXG);
223 if (ret) {
224 perror("mkdir");
225 goto error;
226 }
227
228 dir = opendir(outputname);
229 if (!dir) {
230 perror("opendir");
231 goto error_rmdir;
232 }
233 dir_fd = dirfd(dir);
234 if (dir_fd < 0) {
235 perror("dirfd");
236 goto error_closedir;
237 }
238
239 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
240 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
241 if (fd < 0) {
242 perror("openat");
243 goto error_closedirfd;
244 }
245
246 metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
247 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
248 if (fd < 0) {
249 perror("openat");
250 goto error_closedatastream;
251 }
252 metadata_fp = fdopen(metadata_fd, "w");
253 if (!metadata_fp) {
254 perror("fdopen");
255 goto error_closemetadatafd;
256 }
257
258 uuid_generate(s_uuid);
259 print_metadata(metadata_fp);
260 trace_text(stdin, fd);
261
262 close(fd);
263 exit(EXIT_SUCCESS);
264
265 /* error handling */
266 error_closemetadatafd:
267 ret = close(metadata_fd);
268 if (ret)
269 perror("close");
270 error_closedatastream:
271 ret = close(fd);
272 if (ret)
273 perror("close");
274 error_closedirfd:
275 ret = close(dir_fd);
276 if (ret)
277 perror("close");
278 error_closedir:
279 ret = closedir(dir);
280 if (ret)
281 perror("closedir");
282 error_rmdir:
283 ret = rmdir(outputname);
284 if (ret)
285 perror("rmdir");
286 error:
287 exit(EXIT_FAILURE);
288 }
This page took 0.034366 seconds and 3 git commands to generate.