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