Commit | Line | Data |
---|---|---|
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 | ||
bfe09576 | 38 | #define USEC_PER_SEC 1000000UL |
ca8b2b02 | 39 | |
b522ac18 MD |
40 | #ifndef UUID_STR_LEN |
41 | #define UUID_STR_LEN 37 /* With \0 */ | |
42 | #endif | |
43 | ||
ca8b2b02 MD |
44 | int babeltrace_debug, babeltrace_verbose; |
45 | ||
46 | static char *s_outputname; | |
47 | static int s_timestamp; | |
48 | static uuid_t s_uuid; | |
49 | ||
b522ac18 MD |
50 | /* Metadata format string */ |
51 | static const char metadata_fmt[] = | |
989c73bc MD |
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" | |
c818559a MD |
56 | " major = %u;\n" /* major (e.g. 0) */ |
57 | " minor = %u;\n" /* minor (e.g. 1) */ | |
b522ac18 | 58 | " uuid = \"%s\";\n" /* UUID */ |
989c73bc MD |
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" | |
ca8b2b02 | 71 | "%s" /* Stream event header (opt.) */ |
989c73bc MD |
72 | "};\n" |
73 | "\n" | |
74 | "event {\n" | |
75 | " name = string;\n" | |
76 | " fields := struct { string str; };\n" | |
77 | "};\n"; | |
78 | ||
ca8b2b02 MD |
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"; | |
8c572eba | 84 | |
b522ac18 MD |
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, | |
c818559a MD |
92 | BABELTRACE_VERSION_MAJOR, |
93 | BABELTRACE_VERSION_MINOR, | |
b522ac18 | 94 | uuid_str, |
ca8b2b02 MD |
95 | BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be", |
96 | s_timestamp ? metadata_stream_event_header_timestamp : ""); | |
b522ac18 MD |
97 | } |
98 | ||
8c572eba | 99 | static |
46322b33 | 100 | void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid) |
8c572eba | 101 | { |
46322b33 | 102 | struct ctf_stream_pos dummy; |
8c572eba MD |
103 | |
104 | /* magic */ | |
46322b33 MD |
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)); | |
8c572eba | 109 | |
46322b33 MD |
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); | |
8c572eba MD |
113 | |
114 | /* trace_uuid */ | |
46322b33 MD |
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); | |
8c572eba MD |
123 | } |
124 | ||
125 | static | |
46322b33 | 126 | void write_packet_context(struct ctf_stream_pos *pos) |
8c572eba | 127 | { |
46322b33 | 128 | struct ctf_stream_pos dummy; |
8c572eba MD |
129 | |
130 | /* content_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) = -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); | |
8c572eba MD |
140 | |
141 | /* packet_size */ | |
46322b33 MD |
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)); | |
8c572eba | 146 | |
46322b33 MD |
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); | |
8c572eba MD |
150 | } |
151 | ||
ca8b2b02 MD |
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 | { | |
bfe09576 | 157 | unsigned long sec, usec; |
ca8b2b02 MD |
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 */ | |
bfe09576 | 166 | ret = sscanf(line, "[%lu.%lu] ", &sec, &usec); |
ca8b2b02 MD |
167 | if (ret == 2) { |
168 | *tline = strchr(line, ']'); | |
bfe09576 MD |
169 | assert(*tline); |
170 | (*tline)++; | |
171 | if ((*tline)[0] == ' ') { | |
ca8b2b02 | 172 | (*tline)++; |
bfe09576 | 173 | } |
ca8b2b02 | 174 | *tlen = len + line - *tline; |
bfe09576 | 175 | *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec; |
ca8b2b02 MD |
176 | } |
177 | } | |
178 | /* timestamp */ | |
179 | ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT); | |
180 | if (!pos->dummy) | |
181 | *(uint32_t *) ctf_get_pos_addr(pos) = *ts; | |
182 | ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT); | |
183 | } | |
184 | ||
8c572eba | 185 | static |
46322b33 | 186 | void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) |
8c572eba | 187 | { |
46322b33 | 188 | struct ctf_stream_pos dummy; |
8c572eba | 189 | int attempt = 0; |
ca8b2b02 MD |
190 | char *tline = line; /* tline is start of text, after timestamp */ |
191 | size_t tlen = len; | |
192 | uint64_t ts = 0; | |
8c572eba MD |
193 | |
194 | printf_debug("read: %s\n", line); | |
195 | retry: | |
46322b33 | 196 | ctf_dummy_pos(pos, &dummy); |
ca8b2b02 | 197 | write_event_header(&dummy, line, &tline, len, &tlen, &ts); |
46322b33 | 198 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); |
ca8b2b02 | 199 | ctf_move_pos(&dummy, tlen * CHAR_BIT); |
46322b33 | 200 | if (ctf_pos_packet(&dummy)) { |
46322b33 | 201 | ctf_pos_pad_packet(pos); |
8c572eba MD |
202 | write_packet_header(pos, s_uuid); |
203 | write_packet_context(pos); | |
204 | if (attempt++ == 1) { | |
205 | fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n", | |
206 | pos->packet_size / CHAR_BIT / 1024); | |
207 | return; | |
208 | } | |
209 | goto retry; | |
210 | } | |
211 | ||
ca8b2b02 | 212 | write_event_header(pos, line, &tline, len, &tlen, &ts); |
46322b33 | 213 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); |
ca8b2b02 MD |
214 | memcpy(ctf_get_pos_addr(pos), tline, tlen); |
215 | ctf_move_pos(pos, tlen * CHAR_BIT); | |
8c572eba MD |
216 | } |
217 | ||
218 | static | |
219 | void trace_text(FILE *input, int output) | |
220 | { | |
46322b33 | 221 | struct ctf_stream_pos pos; |
8c572eba MD |
222 | ssize_t len; |
223 | char *line = NULL, *nl; | |
224 | size_t linesize; | |
225 | ||
989c73bc | 226 | ctf_init_pos(&pos, output, O_RDWR); |
8c572eba MD |
227 | |
228 | write_packet_header(&pos, s_uuid); | |
229 | write_packet_context(&pos); | |
230 | for (;;) { | |
231 | len = getline(&line, &linesize, input); | |
232 | if (len < 0) | |
233 | break; | |
234 | nl = strrchr(line, '\n'); | |
235 | if (nl) | |
236 | *nl = '\0'; | |
237 | trace_string(line, &pos, nl - line + 1); | |
238 | } | |
46322b33 | 239 | ctf_fini_pos(&pos); |
8c572eba | 240 | } |
90219914 | 241 | |
ca8b2b02 MD |
242 | static |
243 | void usage(FILE *fp) | |
989c73bc MD |
244 | { |
245 | fprintf(fp, "BabelTrace Log Converter %u.%u\n", | |
246 | BABELTRACE_VERSION_MAJOR, | |
247 | BABELTRACE_VERSION_MINOR); | |
248 | fprintf(fp, "\n"); | |
249 | fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n"); | |
250 | fprintf(fp, "\n"); | |
78893e6e | 251 | fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n"); |
989c73bc MD |
252 | fprintf(fp, "\n"); |
253 | fprintf(fp, " OUTPUT Output trace path\n"); | |
254 | fprintf(fp, "\n"); | |
78893e6e MD |
255 | fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n"); |
256 | fprintf(fp, "\n"); | |
989c73bc MD |
257 | } |
258 | ||
ca8b2b02 MD |
259 | static |
260 | int parse_args(int argc, char **argv) | |
261 | { | |
262 | int i; | |
263 | ||
264 | for (i = 1; i < argc; i++) { | |
265 | if (!strcmp(argv[i], "-t")) | |
266 | s_timestamp = 1; | |
267 | else | |
268 | s_outputname = argv[i]; | |
269 | } | |
270 | if (!s_outputname) | |
271 | return -EINVAL; | |
272 | return 0; | |
273 | } | |
274 | ||
90219914 MD |
275 | int main(int argc, char **argv) |
276 | { | |
b522ac18 | 277 | int fd, metadata_fd, ret; |
989c73bc MD |
278 | DIR *dir; |
279 | int dir_fd; | |
b522ac18 | 280 | FILE *metadata_fp; |
90219914 | 281 | |
ca8b2b02 MD |
282 | ret = parse_args(argc, argv); |
283 | if (ret) { | |
989c73bc MD |
284 | usage(stdout); |
285 | goto error; | |
90219914 | 286 | } |
90219914 | 287 | |
ca8b2b02 | 288 | ret = mkdir(s_outputname, S_IRWXU|S_IRWXG); |
90219914 | 289 | if (ret) { |
989c73bc MD |
290 | perror("mkdir"); |
291 | goto error; | |
292 | } | |
293 | ||
ca8b2b02 | 294 | dir = opendir(s_outputname); |
989c73bc MD |
295 | if (!dir) { |
296 | perror("opendir"); | |
297 | goto error_rmdir; | |
298 | } | |
299 | dir_fd = dirfd(dir); | |
300 | if (dir_fd < 0) { | |
301 | perror("dirfd"); | |
302 | goto error_closedir; | |
303 | } | |
304 | ||
305 | fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT, | |
306 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); | |
307 | if (fd < 0) { | |
308 | perror("openat"); | |
309 | goto error_closedirfd; | |
90219914 | 310 | } |
90219914 | 311 | |
b522ac18 MD |
312 | metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT, |
313 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); | |
314 | if (fd < 0) { | |
315 | perror("openat"); | |
316 | goto error_closedatastream; | |
317 | } | |
318 | metadata_fp = fdopen(metadata_fd, "w"); | |
319 | if (!metadata_fp) { | |
320 | perror("fdopen"); | |
321 | goto error_closemetadatafd; | |
322 | } | |
323 | ||
324 | uuid_generate(s_uuid); | |
325 | print_metadata(metadata_fp); | |
8c572eba | 326 | trace_text(stdin, fd); |
989c73bc | 327 | |
90219914 | 328 | close(fd); |
989c73bc MD |
329 | exit(EXIT_SUCCESS); |
330 | ||
331 | /* error handling */ | |
b522ac18 MD |
332 | error_closemetadatafd: |
333 | ret = close(metadata_fd); | |
334 | if (ret) | |
335 | perror("close"); | |
336 | error_closedatastream: | |
337 | ret = close(fd); | |
338 | if (ret) | |
339 | perror("close"); | |
989c73bc MD |
340 | error_closedirfd: |
341 | ret = close(dir_fd); | |
342 | if (ret) | |
343 | perror("close"); | |
344 | error_closedir: | |
345 | ret = closedir(dir); | |
346 | if (ret) | |
347 | perror("closedir"); | |
348 | error_rmdir: | |
ca8b2b02 | 349 | ret = rmdir(s_outputname); |
989c73bc MD |
350 | if (ret) |
351 | perror("rmdir"); | |
352 | error: | |
353 | exit(EXIT_FAILURE); | |
90219914 | 354 | } |