6 * Copyright 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf/events.h>
30 #include <babeltrace/compat/stdlib.h>
32 #include <sys/utsname.h>
33 #include <babeltrace/compat/limits.h>
39 #include <babeltrace/compat/dirent.h>
42 #define METADATA_LINE_SIZE 512
43 #define SEQUENCE_TEST_LENGTH 10
44 #define PACKET_RESIZE_TEST_LENGTH 100000
46 static uint64_t current_time
;
48 void validate_metadata(char *parser_path
, char *metadata_path
)
51 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
52 int parser_output_fd
= -1, metadata_fd
= -1;
59 parser_output_fd
= mkstemp(parser_output_path
);
60 metadata_fd
= open(metadata_path
, O_RDONLY
);
62 unlink(parser_output_path
);
64 if (parser_output_fd
== -1 || metadata_fd
== -1) {
65 diag("Failed create temporary files for metadata parsing.");
73 waitpid(pid
, &status
, 0);
74 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
76 /* ctf-parser-test expects a metadata string on stdin. */
77 ret
= dup2(metadata_fd
, STDIN_FILENO
);
79 perror("# dup2 metadata_fd to STDIN");
83 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
85 perror("# dup2 parser_output_fd to STDOUT");
89 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
91 perror("# dup2 parser_output_fd to STDERR");
95 execl(parser_path
, "ctf-parser-test", NULL
);
96 perror("# Could not launch the ctf metadata parser process");
100 ok(ret
== 0, "Metadata string is valid");
102 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
104 size_t len
= METADATA_LINE_SIZE
;
105 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
107 metadata_fp
= fdopen(metadata_fd
, "r");
109 perror("fdopen on metadata_fd");
114 parser_output_fp
= fdopen(parser_output_fd
, "r");
115 if (!parser_output_fp
) {
116 perror("fdopen on parser_output_fd");
119 parser_output_fd
= -1;
123 diag("malloc failure");
128 /* Output the metadata and parser output as diagnostic */
129 while (getline(&line
, &len
, metadata_fp
) > 0) {
133 rewind(parser_output_fp
);
134 while (getline(&line
, &len
, parser_output_fp
) > 0) {
141 if (fclose(metadata_fp
)) {
142 diag("fclose failure");
145 if (parser_output_fp
) {
146 if (fclose(parser_output_fp
)) {
147 diag("fclose failure");
152 if (parser_output_fd
>= 0) {
153 if (close(parser_output_fd
)) {
157 if (metadata_fd
>= 0) {
158 if (close(metadata_fd
)) {
164 void validate_trace(char *parser_path
, char *trace_path
)
167 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
168 int babeltrace_output_fd
= -1;
175 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
176 unlink(babeltrace_output_path
);
178 if (babeltrace_output_fd
== -1) {
179 diag("Failed to create a temporary file for trace parsing.");
187 waitpid(pid
, &status
, 0);
188 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
190 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
192 perror("# dup2 babeltrace_output_fd to STDOUT");
196 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
198 perror("# dup2 babeltrace_output_fd to STDERR");
202 execl(parser_path
, "babeltrace", trace_path
, NULL
);
203 perror("# Could not launch the babeltrace process");
207 ok(ret
== 0, "Babeltrace could read the resulting trace");
209 if (ret
&& babeltrace_output_fd
>= 0) {
211 size_t len
= METADATA_LINE_SIZE
;
212 FILE *babeltrace_output_fp
= NULL
;
214 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
215 if (!babeltrace_output_fp
) {
216 perror("fdopen on babeltrace_output_fd");
219 babeltrace_output_fd
= -1;
223 diag("malloc error");
225 rewind(babeltrace_output_fp
);
226 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
232 if (babeltrace_output_fp
) {
233 if (fclose(babeltrace_output_fp
)) {
234 diag("fclose error");
239 if (babeltrace_output_fd
>= 0) {
240 if (close(babeltrace_output_fd
)) {
246 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
247 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
249 /* Create and add a simple event class */
250 struct bt_ctf_event_class
*simple_event_class
=
251 bt_ctf_event_class_create("Simple Event");
252 struct bt_ctf_field_type
*uint_12_type
=
253 bt_ctf_field_type_integer_create(12);
254 struct bt_ctf_field_type
*float_type
=
255 bt_ctf_field_type_floating_point_create();
256 struct bt_ctf_field_type
*enum_type
=
257 bt_ctf_field_type_enumeration_create(uint_12_type
);
258 struct bt_ctf_event
*simple_event
;
259 struct bt_ctf_field
*integer_field
;
260 struct bt_ctf_field
*float_field
;
261 struct bt_ctf_field
*enum_field
;
262 struct bt_ctf_field
*enum_container_field
;
264 bt_ctf_field_type_set_alignment(float_type
, 32);
265 bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11);
266 bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53);
268 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
269 "escaping; \"test\"", 0, 0) == 0,
270 "Accept enumeration mapping strings containing quotes");
271 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
272 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
273 "Accept enumeration mapping strings containing special characters");
274 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
275 "event clock int float", 5, 22) == 0,
276 "Accept enumeration mapping strings containing reserved keywords");
277 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
278 "enum_field") == 0, "Add enumeration field to event");
280 ok(uint_12_type
, "Create an unsigned integer type");
281 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
283 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
285 bt_ctf_stream_class_add_event_class(stream_class
,
288 simple_event
= bt_ctf_event_create(simple_event_class
);
291 "Instantiate an event containing a single integer field");
293 integer_field
= bt_ctf_field_create(uint_12_type
);
294 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
295 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
296 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
298 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
299 bt_ctf_field_floating_point_set_value(float_field
, 3.1415);
300 enum_field
= bt_ctf_field_create(enum_type
);
301 enum_container_field
= bt_ctf_field_enumeration_get_container(
303 ok(bt_ctf_field_unsigned_integer_set_value(
304 enum_container_field
, 1) == 0,
305 "Set enumeration container value");
306 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
308 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
310 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
311 "Append simple event to trace stream");
313 ok(bt_ctf_stream_flush(stream
) == 0,
314 "Flush trace stream with one event");
316 bt_ctf_event_class_put(simple_event_class
);
317 bt_ctf_event_put(simple_event
);
318 bt_ctf_field_type_put(uint_12_type
);
319 bt_ctf_field_type_put(float_type
);
320 bt_ctf_field_type_put(enum_type
);
321 bt_ctf_field_put(integer_field
);
322 bt_ctf_field_put(float_field
);
323 bt_ctf_field_put(enum_field
);
324 bt_ctf_field_put(enum_container_field
);
327 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
328 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
331 struct bt_ctf_field_type
*uint_35_type
=
332 bt_ctf_field_type_integer_create(35);
333 struct bt_ctf_field_type
*int_16_type
=
334 bt_ctf_field_type_integer_create(16);
335 struct bt_ctf_field_type
*uint_3_type
=
336 bt_ctf_field_type_integer_create(3);
337 struct bt_ctf_field_type
*enum_variant_type
=
338 bt_ctf_field_type_enumeration_create(uint_3_type
);
339 struct bt_ctf_field_type
*variant_type
=
340 bt_ctf_field_type_variant_create(enum_variant_type
,
342 struct bt_ctf_field_type
*string_type
=
343 bt_ctf_field_type_string_create();
344 struct bt_ctf_field_type
*sequence_type
;
345 struct bt_ctf_field_type
*inner_structure_type
=
346 bt_ctf_field_type_structure_create();
347 struct bt_ctf_field_type
*complex_structure_type
=
348 bt_ctf_field_type_structure_create();
349 struct bt_ctf_event_class
*event_class
;
350 struct bt_ctf_event
*event
;
351 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
352 *inner_structure_field
, *complex_structure_field
,
353 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
356 bt_ctf_field_type_set_alignment(int_16_type
, 32);
357 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
358 bt_ctf_field_type_integer_set_base(uint_35_type
,
359 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
361 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
363 bt_ctf_field_type_structure_add_field(inner_structure_type
,
364 uint_35_type
, "seq_len");
365 bt_ctf_field_type_structure_add_field(inner_structure_type
,
366 sequence_type
, "a_sequence");
368 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
370 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
372 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
373 "UINT35_TYPE", 2, 7);
374 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
375 "An unknown entry"), "Reject a variant field based on an unknown tag value");
376 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
377 "UINT3_TYPE") == 0, "Add a field to a variant");
378 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
380 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
383 bt_ctf_field_type_structure_add_field(complex_structure_type
,
384 enum_variant_type
, "variant_selector");
385 bt_ctf_field_type_structure_add_field(complex_structure_type
,
386 string_type
, "a_string");
387 bt_ctf_field_type_structure_add_field(complex_structure_type
,
388 variant_type
, "variant_value");
389 bt_ctf_field_type_structure_add_field(complex_structure_type
,
390 inner_structure_type
, "inner_structure");
392 ok(bt_ctf_event_class_create("clock") == NULL
,
393 "Reject creation of an event class with an illegal name");
394 event_class
= bt_ctf_event_class_create("Complex Test Event");
395 ok(event_class
, "Create an event class");
396 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
397 "Reject addition of a field with an empty name to an event");
398 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
399 "Reject addition of a field with a NULL type to an event");
400 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
402 "Reject addition of a type with an illegal name to an event");
403 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
405 "Add field of type unsigned integer to an event");
406 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
407 "int_16") == 0, "Add field of type signed integer to an event");
408 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
409 "complex_structure") == 0,
410 "Add composite structure to an event");
412 /* Add event class to the stream class */
413 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
414 "Reject addition of NULL event class to a stream class");
415 ok(bt_ctf_stream_class_add_event_class(stream_class
,
416 event_class
) == 0, "Add an event class to stream class");
418 event
= bt_ctf_event_create(event_class
);
419 ok(event
, "Instanciate a complex event");
421 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
422 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
423 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
424 bt_ctf_field_put(uint_35_field
);
426 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
427 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
428 bt_ctf_field_put(int_16_field
);
430 complex_structure_field
= bt_ctf_event_get_payload(event
,
431 "complex_structure");
432 inner_structure_field
= bt_ctf_field_structure_get_field(
433 complex_structure_field
, "inner_structure");
434 a_string_field
= bt_ctf_field_structure_get_field(
435 complex_structure_field
, "a_string");
436 enum_variant_field
= bt_ctf_field_structure_get_field(
437 complex_structure_field
, "variant_selector");
438 variant_field
= bt_ctf_field_structure_get_field(
439 complex_structure_field
, "variant_value");
440 uint_35_field
= bt_ctf_field_structure_get_field(
441 inner_structure_field
, "seq_len");
442 a_sequence_field
= bt_ctf_field_structure_get_field(
443 inner_structure_field
, "a_sequence");
445 enum_container_field
= bt_ctf_field_enumeration_get_container(
447 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
448 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
450 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
451 bt_ctf_field_put(int_16_field
);
452 bt_ctf_field_string_set_value(a_string_field
,
454 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
455 SEQUENCE_TEST_LENGTH
);
456 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
457 uint_35_field
) == 0, "Set a sequence field's length");
459 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
460 int_16_field
= bt_ctf_field_sequence_get_field(
461 a_sequence_field
, i
);
462 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
463 bt_ctf_field_put(int_16_field
);
466 bt_ctf_clock_set_time(clock
, ++current_time
);
467 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
468 "Append a complex event to a stream");
469 ok(bt_ctf_stream_flush(stream
) == 0,
470 "Flush a stream containing a complex event");
472 bt_ctf_field_put(uint_35_field
);
473 bt_ctf_field_put(a_string_field
);
474 bt_ctf_field_put(inner_structure_field
);
475 bt_ctf_field_put(complex_structure_field
);
476 bt_ctf_field_put(a_sequence_field
);
477 bt_ctf_field_put(enum_variant_field
);
478 bt_ctf_field_put(enum_container_field
);
479 bt_ctf_field_put(variant_field
);
480 bt_ctf_field_type_put(uint_35_type
);
481 bt_ctf_field_type_put(int_16_type
);
482 bt_ctf_field_type_put(string_type
);
483 bt_ctf_field_type_put(sequence_type
);
484 bt_ctf_field_type_put(inner_structure_type
);
485 bt_ctf_field_type_put(complex_structure_type
);
486 bt_ctf_field_type_put(uint_3_type
);
487 bt_ctf_field_type_put(enum_variant_type
);
488 bt_ctf_field_type_put(variant_type
);
489 bt_ctf_event_class_put(event_class
);
490 bt_ctf_event_put(event
);
493 void type_field_tests()
495 struct bt_ctf_field
*uint_12
;
496 struct bt_ctf_field
*int_16
;
497 struct bt_ctf_field
*string
;
498 struct bt_ctf_field
*enumeration
;
499 struct bt_ctf_field_type
*composite_structure_type
;
500 struct bt_ctf_field_type
*structure_seq_type
;
501 struct bt_ctf_field_type
*string_type
;
502 struct bt_ctf_field_type
*sequence_type
;
503 struct bt_ctf_field_type
*uint_8_type
;
504 struct bt_ctf_field_type
*int_16_type
;
505 struct bt_ctf_field_type
*uint_12_type
=
506 bt_ctf_field_type_integer_create(12);
507 struct bt_ctf_field_type
*enumeration_type
;
508 struct bt_ctf_field_type
*enumeration_sequence_type
;
509 struct bt_ctf_field_type
*enumeration_array_type
;
511 ok(uint_12_type
, "Create an unsigned integer type");
512 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
513 BT_CTF_INTEGER_BASE_BINARY
) == 0,
514 "Set integer type's base as binary");
515 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
516 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
517 "Set integer type's base as decimal");
518 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
519 BT_CTF_INTEGER_BASE_UNKNOWN
),
520 "Reject integer type's base set as unknown");
521 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
522 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
523 "Set integer type's base as octal");
524 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
525 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
526 "Set integer type's base as hexadecimal");
527 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
528 "Reject unknown integer base value");
529 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
530 "Set integer type signedness to signed");
531 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
532 "Set integer type signedness to unsigned");
534 int_16_type
= bt_ctf_field_type_integer_create(16);
535 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
536 uint_8_type
= bt_ctf_field_type_integer_create(8);
538 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
539 ok(sequence_type
, "Create a sequence of int16_t type");
541 string_type
= bt_ctf_field_type_string_create();
542 ok(string_type
, "Create a string type");
543 ok(bt_ctf_field_type_string_set_encoding(string_type
,
545 "Reject invalid \"None\" string encoding");
546 ok(bt_ctf_field_type_string_set_encoding(string_type
,
548 "Reject invalid string encoding");
549 ok(bt_ctf_field_type_string_set_encoding(string_type
,
550 CTF_STRING_ASCII
) == 0,
551 "Set string encoding to ASCII");
553 structure_seq_type
= bt_ctf_field_type_structure_create();
554 ok(structure_seq_type
, "Create a structure type");
555 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
556 uint_8_type
, "seq_len") == 0,
557 "Add a uint8_t type to a structure");
558 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
559 sequence_type
, "a_sequence") == 0,
560 "Add a sequence type to a structure");
561 composite_structure_type
= bt_ctf_field_type_structure_create();
562 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
563 string_type
, "a_string") == 0,
564 "Add a string type to a structure");
565 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
566 structure_seq_type
, "inner_structure") == 0,
567 "Add a structure type to a structure");
569 int_16
= bt_ctf_field_create(int_16_type
);
570 ok(int_16
, "Instanciate a signed 16-bit integer");
571 uint_12
= bt_ctf_field_create(uint_12_type
);
572 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
574 /* Can't modify types after instanciating them */
575 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
576 BT_CTF_INTEGER_BASE_DECIMAL
),
577 "Check an integer type' base can't be modified after instanciation");
578 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
579 "Check an integer type's signedness can't be modified after instanciation");
581 /* Check signed property is checked */
582 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
583 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
584 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
585 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
587 /* Check overflows are properly tested for */
588 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
589 "Check -32768 is allowed for a signed 16-bit integer");
590 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
591 "Check 32767 is allowed for a signed 16-bit integer");
592 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
593 "Check 32768 is not allowed for a signed 16-bit integer");
594 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
595 "Check -32769 is not allowed for a signed 16-bit integer");
596 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
597 "Check -42 is allowed for a signed 16-bit integer");
599 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
600 "Check 4095 is allowed for an unsigned 12-bit integer");
601 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
602 "Check 4096 is not allowed for a unsigned 12-bit integer");
603 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
604 "Check 0 is allowed for an unsigned 12-bit integer");
606 string
= bt_ctf_field_create(string_type
);
607 ok(string
, "Instanciate a string field");
608 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
609 "Set a string's value");
611 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
613 "Create an enumeration type with an unsigned 12-bit integer as container");
614 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
615 enumeration_type
, "count");
616 ok(!enumeration_sequence_type
,
617 "Check enumeration types are validated when creating a sequence");
618 enumeration_array_type
= bt_ctf_field_type_array_create(
619 enumeration_type
, 10);
620 ok(!enumeration_array_type
,
621 "Check enumeration types are validated when creating an array");
622 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
623 enumeration_type
, "enumeration"),
624 "Check enumeration types are validated when adding them as structure members");
625 enumeration
= bt_ctf_field_create(enumeration_type
);
627 "Check enumeration types are validated before instantiation");
629 bt_ctf_field_put(string
);
630 bt_ctf_field_put(uint_12
);
631 bt_ctf_field_put(int_16
);
632 bt_ctf_field_put(enumeration
);
633 bt_ctf_field_type_put(composite_structure_type
);
634 bt_ctf_field_type_put(structure_seq_type
);
635 bt_ctf_field_type_put(string_type
);
636 bt_ctf_field_type_put(sequence_type
);
637 bt_ctf_field_type_put(uint_8_type
);
638 bt_ctf_field_type_put(int_16_type
);
639 bt_ctf_field_type_put(uint_12_type
);
640 bt_ctf_field_type_put(enumeration_type
);
641 bt_ctf_field_type_put(enumeration_sequence_type
);
642 bt_ctf_field_type_put(enumeration_array_type
);
645 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
646 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
649 * Append enough events to force the underlying packet to be resized.
650 * Also tests that a new event can be declared after a stream has been
651 * instantiated and used/flushed.
655 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
657 struct bt_ctf_field_type
*integer_type
=
658 bt_ctf_field_type_integer_create(17);
659 struct bt_ctf_field_type
*string_type
=
660 bt_ctf_field_type_string_create();
662 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
664 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
666 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
667 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
672 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
673 struct bt_ctf_event
*event
= bt_ctf_event_create(event_class
);
674 struct bt_ctf_field
*integer
=
675 bt_ctf_field_create(integer_type
);
676 struct bt_ctf_field
*string
=
677 bt_ctf_field_create(string_type
);
679 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
680 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
681 ret
|= bt_ctf_event_set_payload(event
, "field_1",
683 bt_ctf_field_put(integer
);
684 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
685 ret
|= bt_ctf_event_set_payload(event
, "a_string",
687 bt_ctf_field_put(string
);
688 ret
|= bt_ctf_stream_append_event(stream
, event
);
689 bt_ctf_event_put(event
);
696 ok(ret
== 0, "Append 100 000 events to a stream");
697 ok(bt_ctf_stream_flush(stream
) == 0,
698 "Flush a stream that forces a packet resize");
699 bt_ctf_field_type_put(integer_type
);
700 bt_ctf_field_type_put(string_type
);
701 bt_ctf_event_class_put(event_class
);
704 int main(int argc
, char **argv
)
706 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
707 char metadata_path
[sizeof(trace_path
) + 9];
708 const char *clock_name
= "test_clock";
709 const char *clock_description
= "This is a test clock";
710 const uint64_t frequency
= 1000000000;
711 const uint64_t offset_s
= 1351530929945824323;
712 const uint64_t offset
= 1234567;
713 const uint64_t precision
= 10;
714 char *metadata_string
;
715 struct bt_ctf_writer
*writer
;
717 char hostname
[BABELTRACE_HOST_NAME_MAX
];
718 struct bt_ctf_clock
*clock
;
719 struct bt_ctf_stream_class
*stream_class
;
720 struct bt_ctf_stream
*stream1
;
724 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
730 if (!bt_mkdtemp(trace_path
)) {
734 strcpy(metadata_path
, trace_path
);
735 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
737 writer
= bt_ctf_writer_create(trace_path
);
738 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
740 /* Add environment context to the trace */
741 ret
= gethostname(hostname
, sizeof(hostname
));
745 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
746 "Add host (%s) environment field to writer instance",
748 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
750 "bt_ctf_writer_add_environment_field error with NULL writer");
751 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
753 "bt_ctf_writer_add_environment_field error with NULL field name");
754 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
756 "bt_ctf_writer_add_environment_field error with NULL field value");
758 /* On Solaris, uname() can return any positive value on success */
759 if (uname(&name
) < 0) {
764 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
765 == 0, "Add sysname (%s) environment field to writer instance",
767 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
769 "Add nodename (%s) environment field to writer instance",
771 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
772 == 0, "Add release (%s) environment field to writer instance",
774 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
775 == 0, "Add version (%s) environment field to writer instance",
777 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
778 == 0, "Add machine (%s) environment field to writer istance",
781 /* Define a clock and add it to the trace */
782 ok(bt_ctf_clock_create("signed") == NULL
, "Illegal clock name rejected");
783 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
784 clock
= bt_ctf_clock_create(clock_name
);
785 ok(clock
, "Clock created sucessfully");
786 bt_ctf_clock_set_description(clock
, clock_description
);
788 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
789 "Set clock frequency");
790 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
791 "Set clock offset (seconds)");
792 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
793 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
794 "Set clock precision");
795 ok(bt_ctf_clock_set_is_absolute(clock
, 0xFF) == 0,
796 "Set clock absolute property");
798 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
799 "Add clock to writer instance");
800 ok(bt_ctf_writer_add_clock(writer
, clock
),
801 "Verify a clock can't be added twice to a writer instance");
803 /* Define a stream class */
804 stream_class
= bt_ctf_stream_class_create("test_stream");
805 ok(stream_class
, "Create stream class");
806 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
807 "Set a stream class' clock");
809 /* Test the event fields and event types APIs */
812 /* Instantiate a stream and append events */
813 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
814 ok(stream1
, "Instanciate a stream class from writer");
816 /* Should fail after instanciating a stream (locked)*/
817 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
818 "Changes to a stream class that was already instantiated fail");
820 append_simple_event(stream_class
, stream1
, clock
);
822 packet_resize_test(stream_class
, stream1
, clock
);
824 append_complex_event(stream_class
, stream1
, clock
);
826 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
827 ok(metadata_string
, "Get metadata string");
829 bt_ctf_writer_flush_metadata(writer
);
830 validate_metadata(argv
[1], metadata_path
);
831 validate_trace(argv
[2], trace_path
);
833 bt_ctf_clock_put(clock
);
834 bt_ctf_stream_class_put(stream_class
);
835 bt_ctf_writer_put(writer
);
836 bt_ctf_stream_put(stream1
);
837 free(metadata_string
);
839 /* Remove all trace files and delete temporary trace directory */
840 DIR *trace_dir
= opendir(trace_path
);
846 struct dirent
*entry
;
847 while ((entry
= readdir(trace_dir
))) {
848 if (entry
->d_type
== DT_REG
) {
849 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);