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