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