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 | * | |
c462e188 MD |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | * | |
8c572eba MD |
28 | * Depends on glibc 2.10 for getline(). |
29 | */ | |
30 | ||
c696b1b1 | 31 | #define _GNU_SOURCE |
00f7fbf0 | 32 | #include <config.h> |
90219914 MD |
33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | |
35 | #include <fcntl.h> | |
36 | #include <sys/mman.h> | |
989c73bc | 37 | #include <dirent.h> |
90219914 | 38 | #include <stdio.h> |
989c73bc | 39 | #include <stdlib.h> |
90219914 MD |
40 | #include <stdint.h> |
41 | #include <unistd.h> | |
42 | #include <errno.h> | |
90219914 | 43 | #include <string.h> |
fef3bf22 | 44 | #include <inttypes.h> |
90219914 | 45 | |
70bd0a12 | 46 | #include <babeltrace/babeltrace-internal.h> |
8c572eba | 47 | #include <babeltrace/ctf/types.h> |
4cb26dfb | 48 | #include <babeltrace/compat/uuid.h> |
a386e246 | 49 | #include <babeltrace/compat/utc.h> |
43e34335 | 50 | #include <babeltrace/endian.h> |
8c572eba | 51 | |
a386e246 SKC |
52 | #define NSEC_PER_USEC 1000UL |
53 | #define NSEC_PER_MSEC 1000000UL | |
54 | #define NSEC_PER_SEC 1000000000ULL | |
bfe09576 | 55 | #define USEC_PER_SEC 1000000UL |
ca8b2b02 | 56 | |
ca8b2b02 MD |
57 | int babeltrace_debug, babeltrace_verbose; |
58 | ||
59 | static char *s_outputname; | |
60 | static int s_timestamp; | |
39592eae | 61 | static int s_help; |
43e34335 | 62 | static unsigned char s_uuid[BABELTRACE_UUID_LEN]; |
ca8b2b02 | 63 | |
b522ac18 MD |
64 | /* Metadata format string */ |
65 | static const char metadata_fmt[] = | |
408a2c4d | 66 | "/* CTF 1.8 */\n" |
989c73bc MD |
67 | "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n" |
68 | "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n" | |
fef3bf22 | 69 | "typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n" |
989c73bc MD |
70 | "\n" |
71 | "trace {\n" | |
c818559a MD |
72 | " major = %u;\n" /* major (e.g. 0) */ |
73 | " minor = %u;\n" /* minor (e.g. 1) */ | |
b522ac18 | 74 | " uuid = \"%s\";\n" /* UUID */ |
989c73bc MD |
75 | " byte_order = %s;\n" /* be or le */ |
76 | " packet.header := struct {\n" | |
77 | " uint32_t magic;\n" | |
b4c19c1e | 78 | " uint8_t uuid[16];\n" |
989c73bc MD |
79 | " };\n" |
80 | "};\n" | |
81 | "\n" | |
82 | "stream {\n" | |
83 | " packet.context := struct {\n" | |
fef3bf22 MD |
84 | " uint64_t content_size;\n" |
85 | " uint64_t packet_size;\n" | |
989c73bc | 86 | " };\n" |
ca8b2b02 | 87 | "%s" /* Stream event header (opt.) */ |
989c73bc MD |
88 | "};\n" |
89 | "\n" | |
90 | "event {\n" | |
91 | " name = string;\n" | |
92 | " fields := struct { string str; };\n" | |
93 | "};\n"; | |
94 | ||
ca8b2b02 MD |
95 | static const char metadata_stream_event_header_timestamp[] = |
96 | " typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n" | |
97 | " event.header := struct {\n" | |
98 | " uint64_t timestamp;\n" | |
99 | " };\n"; | |
8c572eba | 100 | |
b522ac18 MD |
101 | static |
102 | void print_metadata(FILE *fp) | |
103 | { | |
a4dfa07b | 104 | char uuid_str[BABELTRACE_UUID_STR_LEN]; |
00f7fbf0 MD |
105 | unsigned int major = 0, minor = 0; |
106 | int ret; | |
b522ac18 | 107 | |
00f7fbf0 MD |
108 | ret = sscanf(VERSION, "%u.%u", &major, &minor); |
109 | if (ret != 2) | |
110 | fprintf(stderr, "[warning] Incorrect babeltrace version format\n."); | |
a4dfa07b | 111 | babeltrace_uuid_unparse(s_uuid, uuid_str); |
b522ac18 | 112 | fprintf(fp, metadata_fmt, |
00f7fbf0 MD |
113 | major, |
114 | minor, | |
b522ac18 | 115 | uuid_str, |
ca8b2b02 MD |
116 | BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be", |
117 | s_timestamp ? metadata_stream_event_header_timestamp : ""); | |
b522ac18 MD |
118 | } |
119 | ||
8c572eba | 120 | static |
43e34335 | 121 | void write_packet_header(struct ctf_stream_pos *pos, unsigned char *uuid) |
8c572eba | 122 | { |
46322b33 | 123 | struct ctf_stream_pos dummy; |
8c572eba MD |
124 | |
125 | /* magic */ | |
46322b33 MD |
126 | ctf_dummy_pos(pos, &dummy); |
127 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
128 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
129 | assert(!ctf_pos_packet(&dummy)); | |
d77e781b | 130 | |
46322b33 MD |
131 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
132 | *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1; | |
133 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
8c572eba | 134 | |
b4c19c1e | 135 | /* uuid */ |
46322b33 MD |
136 | ctf_dummy_pos(pos, &dummy); |
137 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
138 | ctf_move_pos(&dummy, 16 * CHAR_BIT); | |
139 | assert(!ctf_pos_packet(&dummy)); | |
140 | ||
141 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); | |
43e34335 MD |
142 | memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN); |
143 | ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT); | |
8c572eba MD |
144 | } |
145 | ||
146 | static | |
46322b33 | 147 | void write_packet_context(struct ctf_stream_pos *pos) |
8c572eba | 148 | { |
46322b33 | 149 | struct ctf_stream_pos dummy; |
8c572eba MD |
150 | |
151 | /* content_size */ | |
46322b33 | 152 | ctf_dummy_pos(pos, &dummy); |
fef3bf22 MD |
153 | ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT); |
154 | ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT); | |
46322b33 | 155 | assert(!ctf_pos_packet(&dummy)); |
d77e781b | 156 | |
fef3bf22 MD |
157 | ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT); |
158 | *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */ | |
159 | pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos); | |
160 | ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT); | |
8c572eba MD |
161 | |
162 | /* packet_size */ | |
46322b33 | 163 | ctf_dummy_pos(pos, &dummy); |
fef3bf22 MD |
164 | ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT); |
165 | ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT); | |
46322b33 | 166 | assert(!ctf_pos_packet(&dummy)); |
d77e781b | 167 | |
fef3bf22 MD |
168 | ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT); |
169 | *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size; | |
170 | ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT); | |
8c572eba MD |
171 | } |
172 | ||
ca8b2b02 MD |
173 | static |
174 | void write_event_header(struct ctf_stream_pos *pos, char *line, | |
175 | char **tline, size_t len, size_t *tlen, | |
176 | uint64_t *ts) | |
177 | { | |
ca8b2b02 MD |
178 | if (!s_timestamp) |
179 | return; | |
180 | ||
181 | /* Only need to be executed on first pass (dummy) */ | |
a386e246 SKC |
182 | if (pos->dummy) { |
183 | int has_timestamp = 0; | |
184 | unsigned long sec, usec, msec; | |
185 | unsigned int year, mon, mday, hour, min; | |
08c82b90 | 186 | |
ca8b2b02 | 187 | /* Extract time from input line */ |
a386e246 SKC |
188 | if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) { |
189 | *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec; | |
190 | /* | |
191 | * Default CTF clock has 1GHz frequency. Convert | |
192 | * from usec to nsec. | |
193 | */ | |
194 | *ts *= NSEC_PER_USEC; | |
195 | has_timestamp = 1; | |
196 | } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ", | |
197 | &year, &mon, &mday, &hour, &min, | |
198 | &sec, &msec) == 7) { | |
199 | time_t ep_sec; | |
200 | struct tm ti; | |
201 | ||
202 | memset(&ti, 0, sizeof(ti)); | |
203 | ti.tm_year = year - 1900; /* from 1900 */ | |
204 | ti.tm_mon = mon - 1; /* 0 to 11 */ | |
205 | ti.tm_mday = mday; | |
206 | ti.tm_hour = hour; | |
207 | ti.tm_min = min; | |
208 | ti.tm_sec = sec; | |
209 | ||
210 | ep_sec = babeltrace_timegm(&ti); | |
211 | if (ep_sec != (time_t) -1) { | |
212 | *ts = (uint64_t) ep_sec * NSEC_PER_SEC | |
213 | + (uint64_t) msec * NSEC_PER_MSEC; | |
214 | } | |
215 | has_timestamp = 1; | |
216 | } | |
217 | if (has_timestamp) { | |
ca8b2b02 | 218 | *tline = strchr(line, ']'); |
bfe09576 MD |
219 | assert(*tline); |
220 | (*tline)++; | |
221 | if ((*tline)[0] == ' ') { | |
ca8b2b02 | 222 | (*tline)++; |
bfe09576 | 223 | } |
ca8b2b02 | 224 | *tlen = len + line - *tline; |
ca8b2b02 MD |
225 | } |
226 | } | |
227 | /* timestamp */ | |
228 | ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT); | |
229 | if (!pos->dummy) | |
bc7eb6c8 | 230 | *(uint64_t *) ctf_get_pos_addr(pos) = *ts; |
ca8b2b02 MD |
231 | ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT); |
232 | } | |
233 | ||
8c572eba | 234 | static |
46322b33 | 235 | void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) |
8c572eba | 236 | { |
46322b33 | 237 | struct ctf_stream_pos dummy; |
8c572eba | 238 | int attempt = 0; |
ca8b2b02 MD |
239 | char *tline = line; /* tline is start of text, after timestamp */ |
240 | size_t tlen = len; | |
241 | uint64_t ts = 0; | |
8c572eba MD |
242 | |
243 | printf_debug("read: %s\n", line); | |
08c82b90 MD |
244 | |
245 | for (;;) { | |
246 | ctf_dummy_pos(pos, &dummy); | |
247 | write_event_header(&dummy, line, &tline, len, &tlen, &ts); | |
248 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
249 | ctf_move_pos(&dummy, tlen * CHAR_BIT); | |
250 | if (ctf_pos_packet(&dummy)) { | |
251 | ctf_pos_pad_packet(pos); | |
252 | write_packet_header(pos, s_uuid); | |
253 | write_packet_context(pos); | |
254 | if (attempt++ == 1) { | |
255 | fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n", | |
256 | pos->packet_size / CHAR_BIT / 1024); | |
257 | return; | |
258 | } | |
259 | continue; | |
260 | } else { | |
261 | break; | |
8c572eba | 262 | } |
8c572eba MD |
263 | } |
264 | ||
ca8b2b02 | 265 | write_event_header(pos, line, &tline, len, &tlen, &ts); |
46322b33 | 266 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); |
ca8b2b02 MD |
267 | memcpy(ctf_get_pos_addr(pos), tline, tlen); |
268 | ctf_move_pos(pos, tlen * CHAR_BIT); | |
8c572eba MD |
269 | } |
270 | ||
271 | static | |
272 | void trace_text(FILE *input, int output) | |
273 | { | |
46322b33 | 274 | struct ctf_stream_pos pos; |
8c572eba MD |
275 | ssize_t len; |
276 | char *line = NULL, *nl; | |
277 | size_t linesize; | |
f824ae04 | 278 | int ret; |
8c572eba | 279 | |
89ca8829 | 280 | memset(&pos, 0, sizeof(pos)); |
ca334c72 | 281 | ret = ctf_init_pos(&pos, NULL, output, O_RDWR); |
f824ae04 MD |
282 | if (ret) { |
283 | fprintf(stderr, "Error in ctf_init_pos\n"); | |
284 | return; | |
285 | } | |
8c572eba MD |
286 | write_packet_header(&pos, s_uuid); |
287 | write_packet_context(&pos); | |
288 | for (;;) { | |
289 | len = getline(&line, &linesize, input); | |
290 | if (len < 0) | |
291 | break; | |
292 | nl = strrchr(line, '\n'); | |
c5e6c71b | 293 | if (nl) { |
8c572eba | 294 | *nl = '\0'; |
c5e6c71b HZ |
295 | trace_string(line, &pos, nl - line + 1); |
296 | } else { | |
297 | trace_string(line, &pos, strlen(line) + 1); | |
298 | } | |
8c572eba | 299 | } |
f824ae04 MD |
300 | ret = ctf_fini_pos(&pos); |
301 | if (ret) { | |
302 | fprintf(stderr, "Error in ctf_fini_pos\n"); | |
303 | } | |
8c572eba | 304 | } |
90219914 | 305 | |
ca8b2b02 MD |
306 | static |
307 | void usage(FILE *fp) | |
989c73bc | 308 | { |
00f7fbf0 | 309 | fprintf(fp, "BabelTrace Log Converter %s\n", VERSION); |
989c73bc MD |
310 | fprintf(fp, "\n"); |
311 | fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n"); | |
312 | fprintf(fp, "\n"); | |
78893e6e | 313 | fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n"); |
989c73bc MD |
314 | fprintf(fp, "\n"); |
315 | fprintf(fp, " OUTPUT Output trace path\n"); | |
316 | fprintf(fp, "\n"); | |
78893e6e | 317 | fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n"); |
a386e246 | 318 | fprintf(fp, " (format: [YYYY-MM-DD HH:MM:SS.MS] string\\n)\n"); |
78893e6e | 319 | fprintf(fp, "\n"); |
989c73bc MD |
320 | } |
321 | ||
ca8b2b02 MD |
322 | static |
323 | int parse_args(int argc, char **argv) | |
324 | { | |
325 | int i; | |
326 | ||
327 | for (i = 1; i < argc; i++) { | |
328 | if (!strcmp(argv[i], "-t")) | |
329 | s_timestamp = 1; | |
39592eae MD |
330 | else if (!strcmp(argv[i], "-h")) { |
331 | s_help = 1; | |
332 | return 0; | |
333 | } else if (argv[i][0] == '-') | |
334 | return -EINVAL; | |
ca8b2b02 MD |
335 | else |
336 | s_outputname = argv[i]; | |
337 | } | |
338 | if (!s_outputname) | |
339 | return -EINVAL; | |
340 | return 0; | |
341 | } | |
342 | ||
90219914 MD |
343 | int main(int argc, char **argv) |
344 | { | |
b522ac18 | 345 | int fd, metadata_fd, ret; |
989c73bc MD |
346 | DIR *dir; |
347 | int dir_fd; | |
b522ac18 | 348 | FILE *metadata_fp; |
90219914 | 349 | |
ca8b2b02 MD |
350 | ret = parse_args(argc, argv); |
351 | if (ret) { | |
3394d22e MD |
352 | fprintf(stderr, "Error: invalid argument.\n"); |
353 | usage(stderr); | |
989c73bc | 354 | goto error; |
90219914 | 355 | } |
90219914 | 356 | |
39592eae MD |
357 | if (s_help) { |
358 | usage(stdout); | |
359 | exit(EXIT_SUCCESS); | |
360 | } | |
361 | ||
ca8b2b02 | 362 | ret = mkdir(s_outputname, S_IRWXU|S_IRWXG); |
90219914 | 363 | if (ret) { |
989c73bc MD |
364 | perror("mkdir"); |
365 | goto error; | |
366 | } | |
367 | ||
ca8b2b02 | 368 | dir = opendir(s_outputname); |
989c73bc MD |
369 | if (!dir) { |
370 | perror("opendir"); | |
371 | goto error_rmdir; | |
372 | } | |
373 | dir_fd = dirfd(dir); | |
374 | if (dir_fd < 0) { | |
375 | perror("dirfd"); | |
376 | goto error_closedir; | |
377 | } | |
378 | ||
379 | fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT, | |
380 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); | |
381 | if (fd < 0) { | |
382 | perror("openat"); | |
383 | goto error_closedirfd; | |
90219914 | 384 | } |
90219914 | 385 | |
b522ac18 MD |
386 | metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT, |
387 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); | |
9396a982 | 388 | if (metadata_fd < 0) { |
b522ac18 MD |
389 | perror("openat"); |
390 | goto error_closedatastream; | |
391 | } | |
392 | metadata_fp = fdopen(metadata_fd, "w"); | |
393 | if (!metadata_fp) { | |
394 | perror("fdopen"); | |
395 | goto error_closemetadatafd; | |
396 | } | |
397 | ||
43e34335 | 398 | babeltrace_uuid_generate(s_uuid); |
b522ac18 | 399 | print_metadata(metadata_fp); |
8c572eba | 400 | trace_text(stdin, fd); |
989c73bc | 401 | |
f824ae04 MD |
402 | ret = close(fd); |
403 | if (ret) | |
404 | perror("close"); | |
989c73bc MD |
405 | exit(EXIT_SUCCESS); |
406 | ||
407 | /* error handling */ | |
b522ac18 MD |
408 | error_closemetadatafd: |
409 | ret = close(metadata_fd); | |
410 | if (ret) | |
411 | perror("close"); | |
412 | error_closedatastream: | |
413 | ret = close(fd); | |
414 | if (ret) | |
415 | perror("close"); | |
989c73bc MD |
416 | error_closedirfd: |
417 | ret = close(dir_fd); | |
418 | if (ret) | |
419 | perror("close"); | |
420 | error_closedir: | |
421 | ret = closedir(dir); | |
422 | if (ret) | |
423 | perror("closedir"); | |
424 | error_rmdir: | |
ca8b2b02 | 425 | ret = rmdir(s_outputname); |
989c73bc MD |
426 | if (ret) |
427 | perror("rmdir"); | |
428 | error: | |
429 | exit(EXIT_FAILURE); | |
90219914 | 430 | } |