| 1 | /* |
| 2 | * babeltrace-log.c |
| 3 | * |
| 4 | * BabelTrace - Convert Text Log to CTF |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 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 | |
| 23 | #include <config.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <dirent.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdint.h> |
| 32 | #include <unistd.h> |
| 33 | #include <errno.h> |
| 34 | #include <uuid/uuid.h> |
| 35 | #include <string.h> |
| 36 | #include <endian.h> |
| 37 | |
| 38 | #include <babeltrace/babeltrace-internal.h> |
| 39 | #include <babeltrace/ctf/types.h> |
| 40 | #include <babeltrace/uuid.h> |
| 41 | |
| 42 | #define USEC_PER_SEC 1000000UL |
| 43 | |
| 44 | int babeltrace_debug, babeltrace_verbose; |
| 45 | |
| 46 | static char *s_outputname; |
| 47 | static int s_timestamp; |
| 48 | static int s_help; |
| 49 | static uuid_t s_uuid; |
| 50 | |
| 51 | /* Metadata format string */ |
| 52 | static const char metadata_fmt[] = |
| 53 | "/* CTF 1.8 */\n" |
| 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" |
| 58 | " major = %u;\n" /* major (e.g. 0) */ |
| 59 | " minor = %u;\n" /* minor (e.g. 1) */ |
| 60 | " uuid = \"%s\";\n" /* UUID */ |
| 61 | " byte_order = %s;\n" /* be or le */ |
| 62 | " packet.header := struct {\n" |
| 63 | " uint32_t magic;\n" |
| 64 | " uint8_t uuid[16];\n" |
| 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" |
| 73 | "%s" /* Stream event header (opt.) */ |
| 74 | "};\n" |
| 75 | "\n" |
| 76 | "event {\n" |
| 77 | " name = string;\n" |
| 78 | " fields := struct { string str; };\n" |
| 79 | "};\n"; |
| 80 | |
| 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"; |
| 86 | |
| 87 | static |
| 88 | void print_metadata(FILE *fp) |
| 89 | { |
| 90 | char uuid_str[BABELTRACE_UUID_STR_LEN]; |
| 91 | unsigned int major = 0, minor = 0; |
| 92 | int ret; |
| 93 | |
| 94 | ret = sscanf(VERSION, "%u.%u", &major, &minor); |
| 95 | if (ret != 2) |
| 96 | fprintf(stderr, "[warning] Incorrect babeltrace version format\n."); |
| 97 | babeltrace_uuid_unparse(s_uuid, uuid_str); |
| 98 | fprintf(fp, metadata_fmt, |
| 99 | major, |
| 100 | minor, |
| 101 | uuid_str, |
| 102 | BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be", |
| 103 | s_timestamp ? metadata_stream_event_header_timestamp : ""); |
| 104 | } |
| 105 | |
| 106 | static |
| 107 | void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid) |
| 108 | { |
| 109 | struct ctf_stream_pos dummy; |
| 110 | |
| 111 | /* magic */ |
| 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)); |
| 116 | |
| 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); |
| 120 | |
| 121 | /* uuid */ |
| 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); |
| 128 | memcpy(ctf_get_pos_addr(pos), uuid, 16); |
| 129 | ctf_move_pos(pos, 16 * CHAR_BIT); |
| 130 | } |
| 131 | |
| 132 | static |
| 133 | void write_packet_context(struct ctf_stream_pos *pos) |
| 134 | { |
| 135 | struct ctf_stream_pos dummy; |
| 136 | |
| 137 | /* content_size */ |
| 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)); |
| 142 | |
| 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); |
| 147 | |
| 148 | /* packet_size */ |
| 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)); |
| 153 | |
| 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); |
| 157 | } |
| 158 | |
| 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 | { |
| 164 | unsigned long sec, usec; |
| 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 */ |
| 173 | ret = sscanf(line, "[%lu.%lu] ", &sec, &usec); |
| 174 | if (ret == 2) { |
| 175 | *tline = strchr(line, ']'); |
| 176 | assert(*tline); |
| 177 | (*tline)++; |
| 178 | if ((*tline)[0] == ' ') { |
| 179 | (*tline)++; |
| 180 | } |
| 181 | *tlen = len + line - *tline; |
| 182 | *ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec; |
| 183 | } |
| 184 | } |
| 185 | /* timestamp */ |
| 186 | ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT); |
| 187 | if (!pos->dummy) |
| 188 | *(uint64_t *) ctf_get_pos_addr(pos) = *ts; |
| 189 | ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT); |
| 190 | } |
| 191 | |
| 192 | static |
| 193 | void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) |
| 194 | { |
| 195 | struct ctf_stream_pos dummy; |
| 196 | int attempt = 0; |
| 197 | char *tline = line; /* tline is start of text, after timestamp */ |
| 198 | size_t tlen = len; |
| 199 | uint64_t ts = 0; |
| 200 | |
| 201 | printf_debug("read: %s\n", line); |
| 202 | retry: |
| 203 | ctf_dummy_pos(pos, &dummy); |
| 204 | write_event_header(&dummy, line, &tline, len, &tlen, &ts); |
| 205 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); |
| 206 | ctf_move_pos(&dummy, tlen * CHAR_BIT); |
| 207 | if (ctf_pos_packet(&dummy)) { |
| 208 | ctf_pos_pad_packet(pos); |
| 209 | write_packet_header(pos, s_uuid); |
| 210 | write_packet_context(pos); |
| 211 | if (attempt++ == 1) { |
| 212 | fprintf(stderr, "[Error] Line too large for packet size (%zukB) (discarded)\n", |
| 213 | pos->packet_size / CHAR_BIT / 1024); |
| 214 | return; |
| 215 | } |
| 216 | goto retry; |
| 217 | } |
| 218 | |
| 219 | write_event_header(pos, line, &tline, len, &tlen, &ts); |
| 220 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); |
| 221 | memcpy(ctf_get_pos_addr(pos), tline, tlen); |
| 222 | ctf_move_pos(pos, tlen * CHAR_BIT); |
| 223 | } |
| 224 | |
| 225 | static |
| 226 | void trace_text(FILE *input, int output) |
| 227 | { |
| 228 | struct ctf_stream_pos pos; |
| 229 | ssize_t len; |
| 230 | char *line = NULL, *nl; |
| 231 | size_t linesize; |
| 232 | |
| 233 | ctf_init_pos(&pos, output, O_RDWR); |
| 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 | } |
| 246 | ctf_fini_pos(&pos); |
| 247 | } |
| 248 | |
| 249 | static |
| 250 | void usage(FILE *fp) |
| 251 | { |
| 252 | fprintf(fp, "BabelTrace Log Converter %s\n", VERSION); |
| 253 | fprintf(fp, "\n"); |
| 254 | fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n"); |
| 255 | fprintf(fp, "\n"); |
| 256 | fprintf(fp, "usage : babeltrace-log [OPTIONS] OUTPUT\n"); |
| 257 | fprintf(fp, "\n"); |
| 258 | fprintf(fp, " OUTPUT Output trace path\n"); |
| 259 | fprintf(fp, "\n"); |
| 260 | fprintf(fp, " -t With timestamps (format: [sec.usec] string\\n)\n"); |
| 261 | fprintf(fp, "\n"); |
| 262 | } |
| 263 | |
| 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; |
| 272 | else if (!strcmp(argv[i], "-h")) { |
| 273 | s_help = 1; |
| 274 | return 0; |
| 275 | } else if (argv[i][0] == '-') |
| 276 | return -EINVAL; |
| 277 | else |
| 278 | s_outputname = argv[i]; |
| 279 | } |
| 280 | if (!s_outputname) |
| 281 | return -EINVAL; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int main(int argc, char **argv) |
| 286 | { |
| 287 | int fd, metadata_fd, ret; |
| 288 | DIR *dir; |
| 289 | int dir_fd; |
| 290 | FILE *metadata_fp; |
| 291 | |
| 292 | ret = parse_args(argc, argv); |
| 293 | if (ret) { |
| 294 | fprintf(stderr, "Error: invalid argument.\n"); |
| 295 | usage(stderr); |
| 296 | goto error; |
| 297 | } |
| 298 | |
| 299 | if (s_help) { |
| 300 | usage(stdout); |
| 301 | exit(EXIT_SUCCESS); |
| 302 | } |
| 303 | |
| 304 | ret = mkdir(s_outputname, S_IRWXU|S_IRWXG); |
| 305 | if (ret) { |
| 306 | perror("mkdir"); |
| 307 | goto error; |
| 308 | } |
| 309 | |
| 310 | dir = opendir(s_outputname); |
| 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; |
| 326 | } |
| 327 | |
| 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 | |
| 340 | uuid_generate(s_uuid); |
| 341 | print_metadata(metadata_fp); |
| 342 | trace_text(stdin, fd); |
| 343 | |
| 344 | close(fd); |
| 345 | exit(EXIT_SUCCESS); |
| 346 | |
| 347 | /* error handling */ |
| 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"); |
| 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: |
| 365 | ret = rmdir(s_outputname); |
| 366 | if (ret) |
| 367 | perror("rmdir"); |
| 368 | error: |
| 369 | exit(EXIT_FAILURE); |
| 370 | } |