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