Commit | Line | Data |
---|---|---|
8c572eba | 1 | /* |
b522ac18 | 2 | * babeltrace-log.c |
8c572eba | 3 | * |
b522ac18 | 4 | * BabelTrace - Convert Text Log to CTF |
8c572eba MD |
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 | ||
90219914 MD |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <sys/mman.h> | |
989c73bc | 25 | #include <dirent.h> |
90219914 | 26 | #include <stdio.h> |
989c73bc | 27 | #include <stdlib.h> |
90219914 MD |
28 | #include <stdint.h> |
29 | #include <unistd.h> | |
30 | #include <errno.h> | |
31 | #include <uuid/uuid.h> | |
32 | #include <string.h> | |
989c73bc | 33 | #include <endian.h> |
90219914 | 34 | |
8c572eba MD |
35 | #include <babeltrace/babeltrace.h> |
36 | #include <babeltrace/ctf/types.h> | |
37 | ||
b522ac18 MD |
38 | #ifndef UUID_STR_LEN |
39 | #define UUID_STR_LEN 37 /* With \0 */ | |
40 | #endif | |
41 | ||
42 | /* Metadata format string */ | |
43 | static const char metadata_fmt[] = | |
989c73bc MD |
44 | "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n" |
45 | "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n" | |
46 | "\n" | |
47 | "trace {\n" | |
48 | " major = %s;\n" /* major (e.g. 0) */ | |
49 | " minor = %s;\n" /* minor (e.g. 1) */ | |
b522ac18 | 50 | " uuid = \"%s\";\n" /* UUID */ |
989c73bc MD |
51 | " byte_order = %s;\n" /* be or le */ |
52 | " packet.header := struct {\n" | |
53 | " uint32_t magic;\n" | |
54 | " uint8_t trace_uuid[16];\n" | |
55 | " };\n" | |
56 | "};\n" | |
57 | "\n" | |
58 | "stream {\n" | |
59 | " packet.context := struct {\n" | |
60 | " uint32_t content_size;\n" | |
61 | " uint32_t packet_size;\n" | |
62 | " };\n" | |
63 | "};\n" | |
64 | "\n" | |
65 | "event {\n" | |
66 | " name = string;\n" | |
67 | " fields := struct { string str; };\n" | |
68 | "};\n"; | |
69 | ||
c8b219a3 | 70 | int babeltrace_debug, babeltrace_verbose; |
8c572eba MD |
71 | |
72 | static uuid_t s_uuid; | |
73 | ||
b522ac18 MD |
74 | static |
75 | void print_metadata(FILE *fp) | |
76 | { | |
77 | char uuid_str[UUID_STR_LEN]; | |
78 | ||
79 | uuid_unparse(s_uuid, uuid_str); | |
80 | fprintf(fp, metadata_fmt, | |
81 | __stringify(BABELTRACE_VERSION_MAJOR), | |
82 | __stringify(BABELTRACE_VERSION_MINOR), | |
83 | uuid_str, | |
84 | BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be"); | |
85 | } | |
86 | ||
87 | ||
8c572eba | 88 | static |
46322b33 | 89 | void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid) |
8c572eba | 90 | { |
46322b33 | 91 | struct ctf_stream_pos dummy; |
8c572eba MD |
92 | |
93 | /* magic */ | |
46322b33 MD |
94 | ctf_dummy_pos(pos, &dummy); |
95 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
96 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
97 | assert(!ctf_pos_packet(&dummy)); | |
8c572eba | 98 | |
46322b33 MD |
99 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
100 | *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1; | |
101 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
8c572eba MD |
102 | |
103 | /* trace_uuid */ | |
46322b33 MD |
104 | ctf_dummy_pos(pos, &dummy); |
105 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
106 | ctf_move_pos(&dummy, 16 * CHAR_BIT); | |
107 | assert(!ctf_pos_packet(&dummy)); | |
108 | ||
109 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); | |
110 | memcpy(ctf_get_pos_addr(pos), uuid, 16); | |
111 | ctf_move_pos(pos, 16 * CHAR_BIT); | |
8c572eba MD |
112 | } |
113 | ||
114 | static | |
46322b33 | 115 | void write_packet_context(struct ctf_stream_pos *pos) |
8c572eba | 116 | { |
46322b33 | 117 | struct ctf_stream_pos dummy; |
8c572eba MD |
118 | |
119 | /* content_size */ | |
46322b33 MD |
120 | ctf_dummy_pos(pos, &dummy); |
121 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
122 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
123 | assert(!ctf_pos_packet(&dummy)); | |
8c572eba | 124 | |
46322b33 MD |
125 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
126 | *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */ | |
127 | pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos); | |
128 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
8c572eba MD |
129 | |
130 | /* packet_size */ | |
46322b33 MD |
131 | ctf_dummy_pos(pos, &dummy); |
132 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
133 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
134 | assert(!ctf_pos_packet(&dummy)); | |
8c572eba | 135 | |
46322b33 MD |
136 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
137 | *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size; | |
138 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
8c572eba MD |
139 | } |
140 | ||
141 | static | |
46322b33 | 142 | void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) |
8c572eba | 143 | { |
46322b33 | 144 | struct ctf_stream_pos dummy; |
8c572eba MD |
145 | int attempt = 0; |
146 | ||
147 | printf_debug("read: %s\n", line); | |
148 | retry: | |
46322b33 MD |
149 | ctf_dummy_pos(pos, &dummy); |
150 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
151 | ctf_move_pos(&dummy, len * CHAR_BIT); | |
152 | if (ctf_pos_packet(&dummy)) { | |
8c572eba | 153 | /* TODO write content size */ |
46322b33 | 154 | ctf_pos_pad_packet(pos); |
8c572eba MD |
155 | write_packet_header(pos, s_uuid); |
156 | write_packet_context(pos); | |
157 | if (attempt++ == 1) { | |
158 | fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n", | |
159 | pos->packet_size / CHAR_BIT / 1024); | |
160 | return; | |
161 | } | |
162 | goto retry; | |
163 | } | |
164 | ||
46322b33 MD |
165 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); |
166 | memcpy(ctf_get_pos_addr(pos), line, len); | |
167 | ctf_move_pos(pos, len * CHAR_BIT); | |
8c572eba MD |
168 | } |
169 | ||
170 | static | |
171 | void trace_text(FILE *input, int output) | |
172 | { | |
46322b33 | 173 | struct ctf_stream_pos pos; |
8c572eba MD |
174 | ssize_t len; |
175 | char *line = NULL, *nl; | |
176 | size_t linesize; | |
177 | ||
989c73bc | 178 | ctf_init_pos(&pos, output, O_RDWR); |
8c572eba MD |
179 | |
180 | write_packet_header(&pos, s_uuid); | |
181 | write_packet_context(&pos); | |
182 | for (;;) { | |
183 | len = getline(&line, &linesize, input); | |
184 | if (len < 0) | |
185 | break; | |
186 | nl = strrchr(line, '\n'); | |
187 | if (nl) | |
188 | *nl = '\0'; | |
189 | trace_string(line, &pos, nl - line + 1); | |
190 | } | |
46322b33 | 191 | ctf_fini_pos(&pos); |
8c572eba | 192 | } |
90219914 | 193 | |
989c73bc MD |
194 | static void usage(FILE *fp) |
195 | { | |
196 | fprintf(fp, "BabelTrace Log Converter %u.%u\n", | |
197 | BABELTRACE_VERSION_MAJOR, | |
198 | BABELTRACE_VERSION_MINOR); | |
199 | fprintf(fp, "\n"); | |
200 | fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n"); | |
201 | fprintf(fp, "\n"); | |
202 | fprintf(fp, "usage : babeltrace-log OUTPUT\n"); | |
203 | fprintf(fp, "\n"); | |
204 | fprintf(fp, " OUTPUT Output trace path\n"); | |
205 | fprintf(fp, "\n"); | |
206 | } | |
207 | ||
90219914 MD |
208 | int main(int argc, char **argv) |
209 | { | |
b522ac18 | 210 | int fd, metadata_fd, ret; |
989c73bc MD |
211 | char *outputname; |
212 | DIR *dir; | |
213 | int dir_fd; | |
b522ac18 | 214 | FILE *metadata_fp; |
90219914 | 215 | |
989c73bc MD |
216 | if (argc < 2) { |
217 | usage(stdout); | |
218 | goto error; | |
90219914 | 219 | } |
989c73bc | 220 | outputname = argv[1]; |
90219914 | 221 | |
989c73bc | 222 | ret = mkdir(outputname, S_IRWXU|S_IRWXG); |
90219914 | 223 | if (ret) { |
989c73bc MD |
224 | perror("mkdir"); |
225 | goto error; | |
226 | } | |
227 | ||
228 | dir = opendir(outputname); | |
229 | if (!dir) { | |
230 | perror("opendir"); | |
231 | goto error_rmdir; | |
232 | } | |
233 | dir_fd = dirfd(dir); | |
234 | if (dir_fd < 0) { | |
235 | perror("dirfd"); | |
236 | goto error_closedir; | |
237 | } | |
238 | ||
239 | fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT, | |
240 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); | |
241 | if (fd < 0) { | |
242 | perror("openat"); | |
243 | goto error_closedirfd; | |
90219914 | 244 | } |
90219914 | 245 | |
b522ac18 MD |
246 | metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT, |
247 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); | |
248 | if (fd < 0) { | |
249 | perror("openat"); | |
250 | goto error_closedatastream; | |
251 | } | |
252 | metadata_fp = fdopen(metadata_fd, "w"); | |
253 | if (!metadata_fp) { | |
254 | perror("fdopen"); | |
255 | goto error_closemetadatafd; | |
256 | } | |
257 | ||
258 | uuid_generate(s_uuid); | |
259 | print_metadata(metadata_fp); | |
8c572eba | 260 | trace_text(stdin, fd); |
989c73bc | 261 | |
90219914 | 262 | close(fd); |
989c73bc MD |
263 | exit(EXIT_SUCCESS); |
264 | ||
265 | /* error handling */ | |
b522ac18 MD |
266 | error_closemetadatafd: |
267 | ret = close(metadata_fd); | |
268 | if (ret) | |
269 | perror("close"); | |
270 | error_closedatastream: | |
271 | ret = close(fd); | |
272 | if (ret) | |
273 | perror("close"); | |
989c73bc MD |
274 | error_closedirfd: |
275 | ret = close(dir_fd); | |
276 | if (ret) | |
277 | perror("close"); | |
278 | error_closedir: | |
279 | ret = closedir(dir); | |
280 | if (ret) | |
281 | perror("closedir"); | |
282 | error_rmdir: | |
283 | ret = rmdir(outputname); | |
284 | if (ret) | |
285 | perror("rmdir"); | |
286 | error: | |
287 | exit(EXIT_FAILURE); | |
90219914 | 288 | } |