| 1 | /* |
| 2 | * text-to-ctf.c |
| 3 | * |
| 4 | * BabelTrace - Convert Text to CTF |
| 5 | * |
| 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * Depends on glibc 2.10 for getline(). |
| 19 | */ |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdint.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | #include <uuid/uuid.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include <babeltrace/babeltrace.h> |
| 33 | #include <babeltrace/ctf/types.h> |
| 34 | |
| 35 | int babeltrace_debug, babeltrace_verbose; |
| 36 | |
| 37 | static uuid_t s_uuid; |
| 38 | |
| 39 | static |
| 40 | void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid) |
| 41 | { |
| 42 | struct ctf_stream_pos dummy; |
| 43 | |
| 44 | /* magic */ |
| 45 | ctf_dummy_pos(pos, &dummy); |
| 46 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); |
| 47 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); |
| 48 | assert(!ctf_pos_packet(&dummy)); |
| 49 | |
| 50 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
| 51 | *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1; |
| 52 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
| 53 | |
| 54 | /* trace_uuid */ |
| 55 | ctf_dummy_pos(pos, &dummy); |
| 56 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); |
| 57 | ctf_move_pos(&dummy, 16 * CHAR_BIT); |
| 58 | assert(!ctf_pos_packet(&dummy)); |
| 59 | |
| 60 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); |
| 61 | memcpy(ctf_get_pos_addr(pos), uuid, 16); |
| 62 | ctf_move_pos(pos, 16 * CHAR_BIT); |
| 63 | } |
| 64 | |
| 65 | static |
| 66 | void write_packet_context(struct ctf_stream_pos *pos) |
| 67 | { |
| 68 | struct ctf_stream_pos dummy; |
| 69 | |
| 70 | /* content_size */ |
| 71 | ctf_dummy_pos(pos, &dummy); |
| 72 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); |
| 73 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); |
| 74 | assert(!ctf_pos_packet(&dummy)); |
| 75 | |
| 76 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
| 77 | *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */ |
| 78 | pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos); |
| 79 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
| 80 | |
| 81 | /* packet_size */ |
| 82 | ctf_dummy_pos(pos, &dummy); |
| 83 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); |
| 84 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); |
| 85 | assert(!ctf_pos_packet(&dummy)); |
| 86 | |
| 87 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
| 88 | *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size; |
| 89 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
| 90 | } |
| 91 | |
| 92 | static |
| 93 | void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) |
| 94 | { |
| 95 | struct ctf_stream_pos dummy; |
| 96 | int attempt = 0; |
| 97 | |
| 98 | printf_debug("read: %s\n", line); |
| 99 | retry: |
| 100 | ctf_dummy_pos(pos, &dummy); |
| 101 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); |
| 102 | ctf_move_pos(&dummy, len * CHAR_BIT); |
| 103 | if (ctf_pos_packet(&dummy)) { |
| 104 | /* TODO write content size */ |
| 105 | ctf_pos_pad_packet(pos); |
| 106 | write_packet_header(pos, s_uuid); |
| 107 | write_packet_context(pos); |
| 108 | if (attempt++ == 1) { |
| 109 | fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n", |
| 110 | pos->packet_size / CHAR_BIT / 1024); |
| 111 | return; |
| 112 | } |
| 113 | goto retry; |
| 114 | } |
| 115 | |
| 116 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); |
| 117 | memcpy(ctf_get_pos_addr(pos), line, len); |
| 118 | ctf_move_pos(pos, len * CHAR_BIT); |
| 119 | } |
| 120 | |
| 121 | static |
| 122 | void trace_text(FILE *input, int output) |
| 123 | { |
| 124 | struct ctf_stream_pos pos; |
| 125 | ssize_t len; |
| 126 | char *line = NULL, *nl; |
| 127 | size_t linesize; |
| 128 | |
| 129 | ctf_init_pos(&pos, output, O_WRONLY); |
| 130 | |
| 131 | write_packet_header(&pos, s_uuid); |
| 132 | write_packet_context(&pos); |
| 133 | for (;;) { |
| 134 | len = getline(&line, &linesize, input); |
| 135 | if (len < 0) |
| 136 | break; |
| 137 | nl = strrchr(line, '\n'); |
| 138 | if (nl) |
| 139 | *nl = '\0'; |
| 140 | trace_string(line, &pos, nl - line + 1); |
| 141 | } |
| 142 | ctf_fini_pos(&pos); |
| 143 | } |
| 144 | |
| 145 | int main(int argc, char **argv) |
| 146 | { |
| 147 | int fd, ret; |
| 148 | |
| 149 | ret = unlink("dummystream"); |
| 150 | if (ret < 0) { |
| 151 | perror("unlink"); |
| 152 | return -1; |
| 153 | } |
| 154 | fd = open("dummystream", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); |
| 155 | if (fd < 0) { |
| 156 | perror("open"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | ret = uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", s_uuid); |
| 161 | if (ret) { |
| 162 | printf("uuid parse error\n"); |
| 163 | close(fd); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | trace_text(stdin, fd); |
| 168 | close(fd); |
| 169 | return 0; |
| 170 | } |