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