Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * writer.c | |
3 | * | |
4 | * Babeltrace CTF Writer | |
5 | * | |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@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 | * 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 | */ | |
28 | ||
29 | #include <babeltrace/ctf-writer/writer.h> | |
30 | #include <babeltrace/ctf-writer/clock.h> | |
adc315b8 | 31 | #include <babeltrace/ctf-ir/clock-internal.h> |
273b65be | 32 | #include <babeltrace/ctf-writer/writer-internal.h> |
adc315b8 JG |
33 | #include <babeltrace/ctf-ir/event-types-internal.h> |
34 | #include <babeltrace/ctf-ir/event-fields-internal.h> | |
273b65be | 35 | #include <babeltrace/ctf-writer/functor-internal.h> |
adc315b8 | 36 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
3f043b05 JG |
37 | #include <babeltrace/ctf-ir/stream-internal.h> |
38 | #include <babeltrace/ctf-ir/stream.h> | |
273b65be JG |
39 | #include <babeltrace/compiler.h> |
40 | #include <stdio.h> | |
41 | #include <stdlib.h> | |
42 | #include <sys/stat.h> | |
43 | #include <errno.h> | |
44 | #include <unistd.h> | |
45 | #include <fcntl.h> | |
46 | #include <inttypes.h> | |
47 | ||
48 | #define DEFAULT_IDENTIFIER_SIZE 128 | |
49 | #define DEFAULT_METADATA_STRING_SIZE 4096 | |
50 | ||
51 | static | |
52 | void environment_variable_destroy(struct environment_variable *var); | |
53 | static | |
54 | void bt_ctf_writer_destroy(struct bt_ctf_ref *ref); | |
55 | static | |
56 | int init_trace_packet_header(struct bt_ctf_writer *writer); | |
57 | static | |
58 | int create_stream_file(struct bt_ctf_writer *writer, | |
59 | struct bt_ctf_stream *stream); | |
60 | static | |
61 | void stream_flush_cb(struct bt_ctf_stream *stream, | |
62 | struct bt_ctf_writer *writer); | |
63 | ||
64 | static | |
65 | const char * const reserved_keywords_str[] = {"align", "callsite", | |
66 | "const", "char", "clock", "double", "enum", "env", "event", | |
67 | "floating_point", "float", "integer", "int", "long", "short", "signed", | |
68 | "stream", "string", "struct", "trace", "typealias", "typedef", | |
69 | "unsigned", "variant", "void" "_Bool", "_Complex", "_Imaginary"}; | |
70 | ||
71 | static | |
72 | const unsigned int field_type_aliases_alignments[] = { | |
73 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
74 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
75 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
76 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
77 | }; | |
78 | ||
79 | static | |
80 | const unsigned int field_type_aliases_sizes[] = { | |
81 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
82 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
83 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
84 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
85 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
86 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
87 | }; | |
88 | ||
89 | static GHashTable *reserved_keywords_set; | |
90 | static int init_done; | |
91 | static int global_data_refcount; | |
92 | ||
93 | struct bt_ctf_writer *bt_ctf_writer_create(const char *path) | |
94 | { | |
95 | struct bt_ctf_writer *writer = NULL; | |
96 | ||
97 | if (!path) { | |
98 | goto error; | |
99 | } | |
100 | ||
101 | writer = g_new0(struct bt_ctf_writer, 1); | |
102 | if (!writer) { | |
103 | goto error; | |
104 | } | |
105 | ||
106 | bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE); | |
107 | bt_ctf_ref_init(&writer->ref_count); | |
108 | writer->path = g_string_new(path); | |
109 | if (!writer->path) { | |
110 | goto error_destroy; | |
111 | } | |
112 | ||
113 | /* Create trace directory if necessary and open a metadata file */ | |
114 | if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { | |
115 | perror("g_mkdir_with_parents"); | |
116 | goto error_destroy; | |
117 | } | |
118 | ||
1b8180b9 | 119 | writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG); |
273b65be JG |
120 | if (writer->trace_dir_fd < 0) { |
121 | perror("open"); | |
122 | goto error_destroy; | |
123 | } | |
124 | ||
125 | writer->metadata_fd = openat(writer->trace_dir_fd, "metadata", | |
126 | O_WRONLY | O_CREAT | O_TRUNC, | |
127 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
128 | writer->environment = g_ptr_array_new_with_free_func( | |
129 | (GDestroyNotify)environment_variable_destroy); | |
130 | writer->clocks = g_ptr_array_new_with_free_func( | |
131 | (GDestroyNotify)bt_ctf_clock_put); | |
132 | writer->streams = g_ptr_array_new_with_free_func( | |
133 | (GDestroyNotify)bt_ctf_stream_put); | |
134 | writer->stream_classes = g_ptr_array_new_with_free_func( | |
135 | (GDestroyNotify)bt_ctf_stream_class_put); | |
136 | if (!writer->environment || !writer->clocks || | |
137 | !writer->stream_classes || !writer->streams) { | |
138 | goto error_destroy; | |
139 | } | |
140 | ||
141 | /* Generate a trace UUID */ | |
142 | uuid_generate(writer->uuid); | |
143 | if (init_trace_packet_header(writer)) { | |
144 | goto error_destroy; | |
145 | } | |
146 | ||
147 | return writer; | |
148 | error_destroy: | |
149 | unlinkat(writer->trace_dir_fd, "metadata", 0); | |
150 | bt_ctf_writer_destroy(&writer->ref_count); | |
151 | writer = NULL; | |
152 | error: | |
153 | return writer; | |
154 | } | |
155 | ||
156 | void bt_ctf_writer_destroy(struct bt_ctf_ref *ref) | |
157 | { | |
158 | struct bt_ctf_writer *writer; | |
159 | ||
160 | if (!ref) { | |
161 | return; | |
162 | } | |
163 | ||
164 | writer = container_of(ref, struct bt_ctf_writer, ref_count); | |
165 | bt_ctf_writer_flush_metadata(writer); | |
166 | if (writer->path) { | |
167 | g_string_free(writer->path, TRUE); | |
168 | } | |
169 | ||
170 | if (writer->trace_dir_fd > 0) { | |
9bb7e58b MD |
171 | if (close(writer->trace_dir_fd)) { |
172 | perror("close"); | |
173 | abort(); | |
174 | } | |
273b65be JG |
175 | } |
176 | ||
177 | if (writer->metadata_fd > 0) { | |
9bb7e58b MD |
178 | if (close(writer->metadata_fd)) { |
179 | perror("close"); | |
180 | abort(); | |
181 | } | |
273b65be JG |
182 | } |
183 | ||
184 | if (writer->environment) { | |
185 | g_ptr_array_free(writer->environment, TRUE); | |
186 | } | |
187 | ||
188 | if (writer->clocks) { | |
189 | g_ptr_array_free(writer->clocks, TRUE); | |
190 | } | |
191 | ||
192 | if (writer->streams) { | |
193 | g_ptr_array_free(writer->streams, TRUE); | |
194 | } | |
195 | ||
196 | if (writer->stream_classes) { | |
197 | g_ptr_array_free(writer->stream_classes, TRUE); | |
198 | } | |
199 | ||
200 | bt_ctf_field_type_put(writer->trace_packet_header_type); | |
201 | bt_ctf_field_put(writer->trace_packet_header); | |
202 | g_free(writer); | |
203 | } | |
204 | ||
205 | struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, | |
206 | struct bt_ctf_stream_class *stream_class) | |
207 | { | |
208 | int ret; | |
209 | int stream_class_found = 0; | |
210 | size_t i; | |
211 | int stream_fd; | |
212 | struct bt_ctf_stream *stream = NULL; | |
213 | ||
214 | if (!writer || !stream_class) { | |
215 | goto error; | |
216 | } | |
217 | ||
12c8a1a3 JG |
218 | ret = bt_ctf_stream_class_set_byte_order(stream_class, |
219 | writer->byte_order == LITTLE_ENDIAN ? | |
220 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN); | |
221 | if (ret) { | |
222 | goto error; | |
223 | } | |
224 | ||
273b65be JG |
225 | stream = bt_ctf_stream_create(stream_class); |
226 | if (!stream) { | |
227 | goto error; | |
228 | } | |
229 | ||
230 | stream_fd = create_stream_file(writer, stream); | |
231 | if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) { | |
232 | goto error; | |
233 | } | |
234 | ||
235 | bt_ctf_stream_set_flush_callback(stream, (flush_func)stream_flush_cb, | |
236 | writer); | |
2f100782 | 237 | |
273b65be JG |
238 | for (i = 0; i < writer->stream_classes->len; i++) { |
239 | if (writer->stream_classes->pdata[i] == stream->stream_class) { | |
240 | stream_class_found = 1; | |
241 | } | |
242 | } | |
243 | ||
244 | if (!stream_class_found) { | |
2f100782 JG |
245 | int64_t stream_id = bt_ctf_stream_class_get_id(stream_class); |
246 | if (stream_id < 0) { | |
247 | if (bt_ctf_stream_class_set_id(stream->stream_class, | |
248 | writer->next_stream_id++)) { | |
249 | goto error; | |
250 | } | |
273b65be JG |
251 | } |
252 | ||
2f100782 JG |
253 | for (i = 0; i < writer->stream_classes->len; i++) { |
254 | if (stream_id == bt_ctf_stream_class_get_id( | |
255 | writer->stream_classes->pdata[i])) { | |
256 | /* Duplicate stream id found */ | |
257 | goto error; | |
258 | } | |
259 | } | |
273b65be JG |
260 | bt_ctf_stream_class_get(stream->stream_class); |
261 | g_ptr_array_add(writer->stream_classes, stream->stream_class); | |
262 | } | |
263 | ||
264 | bt_ctf_stream_get(stream); | |
265 | g_ptr_array_add(writer->streams, stream); | |
266 | writer->frozen = 1; | |
267 | return stream; | |
268 | error: | |
269 | bt_ctf_stream_put(stream); | |
270 | return NULL; | |
271 | } | |
272 | ||
273 | int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, | |
274 | const char *name, | |
275 | const char *value) | |
276 | { | |
277 | struct environment_variable *var = NULL; | |
278 | char *escaped_value = NULL; | |
279 | int ret = 0; | |
280 | ||
281 | if (!writer || !name || !value || validate_identifier(name)) { | |
282 | ret = -1; | |
283 | goto error; | |
284 | } | |
285 | ||
286 | if (strchr(name, ' ')) { | |
287 | ret = -1; | |
288 | goto error; | |
289 | } | |
290 | ||
291 | var = g_new0(struct environment_variable, 1); | |
292 | if (!var) { | |
293 | ret = -1; | |
294 | goto error; | |
295 | } | |
296 | ||
297 | escaped_value = g_strescape(value, NULL); | |
298 | if (!escaped_value) { | |
299 | ret = -1; | |
300 | goto error; | |
301 | } | |
302 | ||
303 | var->name = g_string_new(name); | |
304 | var->value = g_string_new(escaped_value); | |
305 | g_free(escaped_value); | |
306 | if (!var->name || !var->value) { | |
307 | ret = -1; | |
308 | goto error; | |
309 | } | |
310 | ||
311 | g_ptr_array_add(writer->environment, var); | |
312 | return ret; | |
313 | ||
314 | error: | |
315 | if (var && var->name) { | |
316 | g_string_free(var->name, TRUE); | |
317 | } | |
318 | ||
319 | if (var && var->value) { | |
320 | g_string_free(var->value, TRUE); | |
321 | } | |
322 | ||
323 | g_free(var); | |
324 | return ret; | |
325 | } | |
326 | ||
327 | int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, | |
328 | struct bt_ctf_clock *clock) | |
329 | { | |
330 | int ret = 0; | |
331 | struct search_query query = { .value = clock, .found = 0 }; | |
332 | ||
333 | if (!writer || !clock) { | |
334 | ret = -1; | |
335 | goto end; | |
336 | } | |
337 | ||
338 | /* Check for duplicate clocks */ | |
339 | g_ptr_array_foreach(writer->clocks, value_exists, &query); | |
340 | if (query.found) { | |
341 | ret = -1; | |
342 | goto end; | |
343 | } | |
344 | ||
345 | bt_ctf_clock_get(clock); | |
346 | g_ptr_array_add(writer->clocks, clock); | |
347 | end: | |
348 | return ret; | |
349 | } | |
350 | ||
351 | BT_HIDDEN | |
352 | const char *get_byte_order_string(int byte_order) | |
353 | { | |
354 | const char *string; | |
355 | ||
356 | switch (byte_order) { | |
357 | case LITTLE_ENDIAN: | |
358 | string = "le"; | |
359 | break; | |
360 | case BIG_ENDIAN: | |
361 | string = "be"; | |
362 | break; | |
363 | default: | |
364 | string = "unknown"; | |
365 | break; | |
366 | } | |
367 | ||
368 | return string; | |
369 | } | |
370 | ||
371 | static | |
12af6048 | 372 | int append_trace_metadata(struct bt_ctf_writer *writer, |
273b65be JG |
373 | struct metadata_context *context) |
374 | { | |
e32ff703 | 375 | unsigned char *uuid = writer->uuid; |
c4e511df | 376 | int ret; |
e32ff703 | 377 | |
273b65be JG |
378 | g_string_append(context->string, "trace {\n"); |
379 | ||
380 | g_string_append(context->string, "\tmajor = 1;\n"); | |
381 | g_string_append(context->string, "\tminor = 8;\n"); | |
382 | ||
273b65be JG |
383 | g_string_append_printf(context->string, |
384 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
385 | uuid[0], uuid[1], uuid[2], uuid[3], | |
386 | uuid[4], uuid[5], uuid[6], uuid[7], | |
387 | uuid[8], uuid[9], uuid[10], uuid[11], | |
388 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
389 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", | |
390 | get_byte_order_string(writer->byte_order)); | |
391 | ||
392 | g_string_append(context->string, "\tpacket.header := "); | |
393 | context->current_indentation_level++; | |
394 | g_string_assign(context->field_name, ""); | |
c4e511df MD |
395 | ret = bt_ctf_field_type_serialize(writer->trace_packet_header_type, |
396 | context); | |
12af6048 JG |
397 | if (ret) { |
398 | goto end; | |
399 | } | |
273b65be JG |
400 | context->current_indentation_level--; |
401 | ||
402 | g_string_append(context->string, ";\n};\n\n"); | |
12af6048 JG |
403 | end: |
404 | return ret; | |
273b65be JG |
405 | } |
406 | ||
407 | static | |
408 | void append_env_field_metadata(struct environment_variable *var, | |
409 | struct metadata_context *context) | |
410 | { | |
411 | g_string_append_printf(context->string, "\t%s = \"%s\";\n", | |
412 | var->name->str, var->value->str); | |
413 | } | |
414 | ||
415 | static | |
416 | void append_env_metadata(struct bt_ctf_writer *writer, | |
417 | struct metadata_context *context) | |
418 | { | |
419 | if (writer->environment->len == 0) { | |
420 | return; | |
421 | } | |
422 | ||
423 | g_string_append(context->string, "env {\n"); | |
424 | g_ptr_array_foreach(writer->environment, | |
425 | (GFunc)append_env_field_metadata, context); | |
426 | g_string_append(context->string, "};\n\n"); | |
427 | } | |
428 | ||
429 | char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer) | |
430 | { | |
431 | char *metadata = NULL; | |
432 | struct metadata_context *context = NULL; | |
433 | int err = 0; | |
434 | size_t i; | |
435 | ||
436 | if (!writer) { | |
437 | goto end; | |
438 | } | |
439 | ||
440 | context = g_new0(struct metadata_context, 1); | |
441 | if (!context) { | |
442 | goto end; | |
443 | } | |
444 | ||
445 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
446 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
447 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
12af6048 JG |
448 | if (append_trace_metadata(writer, context)) { |
449 | goto error; | |
450 | } | |
273b65be JG |
451 | append_env_metadata(writer, context); |
452 | g_ptr_array_foreach(writer->clocks, | |
453 | (GFunc)bt_ctf_clock_serialize, context); | |
454 | ||
455 | for (i = 0; i < writer->stream_classes->len; i++) { | |
456 | err = bt_ctf_stream_class_serialize( | |
457 | writer->stream_classes->pdata[i], context); | |
458 | if (err) { | |
459 | goto error; | |
460 | } | |
461 | } | |
462 | ||
463 | metadata = context->string->str; | |
464 | error: | |
465 | g_string_free(context->string, err ? TRUE : FALSE); | |
466 | g_string_free(context->field_name, TRUE); | |
467 | g_free(context); | |
468 | end: | |
469 | return metadata; | |
470 | } | |
471 | ||
472 | void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer) | |
473 | { | |
474 | int ret; | |
475 | char *metadata_string = NULL; | |
476 | ||
477 | if (!writer) { | |
478 | goto end; | |
479 | } | |
480 | ||
481 | metadata_string = bt_ctf_writer_get_metadata_string(writer); | |
482 | if (!metadata_string) { | |
483 | goto end; | |
484 | } | |
485 | ||
486 | if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) { | |
487 | perror("lseek"); | |
488 | goto end; | |
489 | } | |
490 | ||
491 | if (ftruncate(writer->metadata_fd, 0)) { | |
492 | perror("ftruncate"); | |
493 | goto end; | |
494 | } | |
495 | ||
496 | ret = write(writer->metadata_fd, metadata_string, | |
497 | strlen(metadata_string)); | |
498 | if (ret < 0) { | |
499 | perror("write"); | |
500 | goto end; | |
501 | } | |
502 | end: | |
503 | g_free(metadata_string); | |
504 | } | |
505 | ||
506 | int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, | |
507 | enum bt_ctf_byte_order byte_order) | |
508 | { | |
509 | int ret = 0; | |
510 | int internal_byte_order; | |
511 | ||
512 | if (!writer || writer->frozen) { | |
513 | ret = -1; | |
514 | goto end; | |
515 | } | |
516 | ||
517 | switch (byte_order) { | |
518 | case BT_CTF_BYTE_ORDER_NATIVE: | |
519 | internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? | |
520 | LITTLE_ENDIAN : BIG_ENDIAN; | |
521 | break; | |
522 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: | |
523 | internal_byte_order = LITTLE_ENDIAN; | |
524 | break; | |
525 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: | |
526 | case BT_CTF_BYTE_ORDER_NETWORK: | |
527 | internal_byte_order = BIG_ENDIAN; | |
528 | break; | |
529 | default: | |
530 | ret = -1; | |
531 | goto end; | |
532 | } | |
533 | ||
534 | writer->byte_order = internal_byte_order; | |
535 | if (writer->trace_packet_header_type || | |
536 | writer->trace_packet_header) { | |
537 | init_trace_packet_header(writer); | |
538 | } | |
539 | end: | |
540 | return ret; | |
541 | } | |
542 | ||
543 | void bt_ctf_writer_get(struct bt_ctf_writer *writer) | |
544 | { | |
545 | if (!writer) { | |
546 | return; | |
547 | } | |
548 | ||
549 | bt_ctf_ref_get(&writer->ref_count); | |
550 | } | |
551 | ||
552 | void bt_ctf_writer_put(struct bt_ctf_writer *writer) | |
553 | { | |
554 | if (!writer) { | |
555 | return; | |
556 | } | |
557 | ||
558 | bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy); | |
559 | } | |
560 | ||
561 | BT_HIDDEN | |
562 | int validate_identifier(const char *input_string) | |
563 | { | |
564 | int ret = 0; | |
565 | char *string = NULL; | |
566 | char *save_ptr, *token; | |
567 | ||
568 | if (!input_string || input_string[0] == '\0') { | |
569 | ret = -1; | |
570 | goto end; | |
571 | } | |
572 | ||
573 | string = strdup(input_string); | |
574 | if (!string) { | |
575 | ret = -1; | |
576 | goto end; | |
577 | } | |
578 | ||
579 | token = strtok_r(string, " ", &save_ptr); | |
580 | while (token) { | |
fe0fe95c JG |
581 | if (g_hash_table_lookup_extended(reserved_keywords_set, |
582 | GINT_TO_POINTER(g_quark_from_string(token)), | |
583 | NULL, NULL)) { | |
273b65be JG |
584 | ret = -1; |
585 | goto end; | |
586 | } | |
587 | ||
588 | token = strtok_r(NULL, " ", &save_ptr); | |
589 | } | |
590 | end: | |
591 | free(string); | |
592 | return ret; | |
593 | } | |
594 | ||
595 | BT_HIDDEN | |
596 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) | |
597 | { | |
598 | unsigned int alignment, size; | |
599 | struct bt_ctf_field_type *field_type; | |
600 | ||
601 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
602 | return NULL; | |
603 | } | |
604 | ||
605 | alignment = field_type_aliases_alignments[alias]; | |
606 | size = field_type_aliases_sizes[alias]; | |
607 | field_type = bt_ctf_field_type_integer_create(size); | |
608 | bt_ctf_field_type_set_alignment(field_type, alignment); | |
609 | return field_type; | |
610 | } | |
611 | ||
612 | static | |
613 | int init_trace_packet_header(struct bt_ctf_writer *writer) | |
614 | { | |
615 | size_t i; | |
616 | int ret = 0; | |
617 | struct bt_ctf_field *trace_packet_header = NULL, | |
618 | *magic = NULL, *uuid_array = NULL; | |
619 | struct bt_ctf_field_type *_uint32_t = | |
620 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
621 | struct bt_ctf_field_type *_uint8_t = | |
622 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
623 | struct bt_ctf_field_type *trace_packet_header_type = | |
624 | bt_ctf_field_type_structure_create(); | |
625 | struct bt_ctf_field_type *uuid_array_type = | |
626 | bt_ctf_field_type_array_create(_uint8_t, 16); | |
627 | ||
628 | if (!trace_packet_header_type || !uuid_array_type) { | |
629 | ret = -1; | |
630 | goto end; | |
631 | } | |
632 | ||
633 | ret = bt_ctf_field_type_set_byte_order(_uint32_t, | |
634 | (writer->byte_order == LITTLE_ENDIAN ? | |
635 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : | |
636 | BT_CTF_BYTE_ORDER_BIG_ENDIAN)); | |
637 | if (ret) { | |
638 | goto end; | |
639 | } | |
640 | ||
641 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
642 | _uint32_t, "magic"); | |
643 | if (ret) { | |
644 | goto end; | |
645 | } | |
646 | ||
647 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
648 | uuid_array_type, "uuid"); | |
649 | if (ret) { | |
650 | goto end; | |
651 | } | |
652 | ||
653 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
654 | _uint32_t, "stream_id"); | |
655 | if (ret) { | |
656 | goto end; | |
657 | } | |
658 | ||
659 | trace_packet_header = bt_ctf_field_create(trace_packet_header_type); | |
660 | if (!trace_packet_header) { | |
661 | ret = -1; | |
662 | goto end; | |
663 | } | |
664 | ||
665 | magic = bt_ctf_field_structure_get_field(trace_packet_header, "magic"); | |
666 | ret = bt_ctf_field_unsigned_integer_set_value(magic, 0xC1FC1FC1); | |
667 | if (ret) { | |
668 | goto end; | |
669 | } | |
670 | ||
671 | uuid_array = bt_ctf_field_structure_get_field(trace_packet_header, | |
672 | "uuid"); | |
673 | for (i = 0; i < 16; i++) { | |
674 | struct bt_ctf_field *uuid_element = | |
675 | bt_ctf_field_array_get_field(uuid_array, i); | |
676 | ret = bt_ctf_field_unsigned_integer_set_value(uuid_element, | |
677 | writer->uuid[i]); | |
678 | bt_ctf_field_put(uuid_element); | |
679 | if (ret) { | |
680 | goto end; | |
681 | } | |
682 | } | |
683 | ||
684 | bt_ctf_field_type_put(writer->trace_packet_header_type); | |
685 | bt_ctf_field_put(writer->trace_packet_header); | |
686 | writer->trace_packet_header_type = trace_packet_header_type; | |
687 | writer->trace_packet_header = trace_packet_header; | |
688 | end: | |
689 | bt_ctf_field_type_put(uuid_array_type); | |
690 | bt_ctf_field_type_put(_uint32_t); | |
691 | bt_ctf_field_type_put(_uint8_t); | |
692 | bt_ctf_field_put(magic); | |
693 | bt_ctf_field_put(uuid_array); | |
694 | if (ret) { | |
695 | bt_ctf_field_type_put(trace_packet_header_type); | |
696 | bt_ctf_field_put(trace_packet_header); | |
697 | } | |
698 | ||
699 | return ret; | |
700 | } | |
701 | ||
702 | static | |
703 | void environment_variable_destroy(struct environment_variable *var) | |
704 | { | |
705 | g_string_free(var->name, TRUE); | |
706 | g_string_free(var->value, TRUE); | |
707 | g_free(var); | |
708 | } | |
709 | ||
710 | static | |
711 | int create_stream_file(struct bt_ctf_writer *writer, | |
712 | struct bt_ctf_stream *stream) | |
713 | { | |
714 | int fd; | |
715 | GString *filename = g_string_new(stream->stream_class->name->str); | |
716 | ||
717 | g_string_append_printf(filename, "_%" PRIu32, stream->id); | |
718 | fd = openat(writer->trace_dir_fd, filename->str, | |
719 | O_RDWR | O_CREAT | O_TRUNC, | |
720 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
721 | g_string_free(filename, TRUE); | |
722 | return fd; | |
723 | } | |
724 | ||
725 | static | |
726 | void stream_flush_cb(struct bt_ctf_stream *stream, struct bt_ctf_writer *writer) | |
727 | { | |
728 | struct bt_ctf_field *stream_id; | |
729 | ||
730 | /* Start a new packet in the stream */ | |
731 | if (stream->flushed_packet_count) { | |
732 | /* ctf_init_pos has already initialized the first packet */ | |
733 | ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR); | |
734 | } | |
735 | ||
736 | stream_id = bt_ctf_field_structure_get_field( | |
737 | writer->trace_packet_header, "stream_id"); | |
c13c7ca0 | 738 | bt_ctf_field_unsigned_integer_set_value(stream_id, stream->stream_class->id); |
273b65be JG |
739 | bt_ctf_field_put(stream_id); |
740 | ||
741 | /* Write the trace_packet_header */ | |
742 | bt_ctf_field_serialize(writer->trace_packet_header, &stream->pos); | |
743 | } | |
744 | ||
745 | static __attribute__((constructor)) | |
746 | void writer_init(void) | |
747 | { | |
748 | size_t i; | |
749 | const size_t reserved_keywords_count = | |
750 | sizeof(reserved_keywords_str) / sizeof(char *); | |
751 | ||
752 | global_data_refcount++; | |
753 | if (init_done) { | |
754 | return; | |
755 | } | |
756 | ||
757 | reserved_keywords_set = g_hash_table_new(g_direct_hash, g_direct_equal); | |
758 | for (i = 0; i < reserved_keywords_count; i++) { | |
fe0fe95c JG |
759 | gpointer quark = GINT_TO_POINTER(g_quark_from_string( |
760 | reserved_keywords_str[i])); | |
761 | ||
762 | g_hash_table_insert(reserved_keywords_set, quark, quark); | |
273b65be JG |
763 | } |
764 | ||
765 | init_done = 1; | |
766 | } | |
767 | ||
768 | static __attribute__((destructor)) | |
769 | void writer_finalize(void) | |
770 | { | |
771 | if (--global_data_refcount == 0) { | |
772 | g_hash_table_destroy(reserved_keywords_set); | |
773 | } | |
774 | } |