From f79cf0f0781f10167a01c9d8b925fd116d184c83 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 17 Feb 2016 17:07:29 -0500 Subject: [PATCH] ir: add public bt_ctf_packet object MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The purpose of a bt_ctf_packet object is to hold the packet header and packet context fields. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/Makefile.am | 1 + formats/ctf/ir/packet.c | 200 ++++++++++++++++++ include/Makefile.am | 2 + include/babeltrace/ctf-ir/packet-internal.h | 44 ++++ include/babeltrace/ctf-ir/packet.h | 62 ++++++ .../babeltrace/ctf-ir/stream-class-internal.h | 1 + 6 files changed, 310 insertions(+) create mode 100644 formats/ctf/ir/packet.c create mode 100644 include/babeltrace/ctf-ir/packet-internal.h create mode 100644 include/babeltrace/ctf-ir/packet.h diff --git a/formats/ctf/ir/Makefile.am b/formats/ctf/ir/Makefile.am index 5b29ff77..b9048bc5 100644 --- a/formats/ctf/ir/Makefile.am +++ b/formats/ctf/ir/Makefile.am @@ -10,6 +10,7 @@ libctf_ir_la_SOURCES = \ fields.c \ field-types.c \ field-path.c \ + packet.c \ stream.c \ stream-class.c \ trace.c \ diff --git a/formats/ctf/ir/packet.c b/formats/ctf/ir/packet.c new file mode 100644 index 00000000..76a2f045 --- /dev/null +++ b/formats/ctf/ir/packet.c @@ -0,0 +1,200 @@ +/* + * packet.c + * + * Babeltrace CTF IR - Stream packet + * + * Copyright 2016 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct bt_ctf_stream *bt_ctf_packet_get_stream(struct bt_ctf_packet *packet) +{ + return packet ? bt_get(packet->stream) : NULL; +} + +struct bt_ctf_field *bt_ctf_packet_get_header( + struct bt_ctf_packet *packet) +{ + return packet ? bt_get(packet->header) : NULL; +} + +int bt_ctf_packet_set_header(struct bt_ctf_packet *packet, + struct bt_ctf_field *header) +{ + int ret = 0; + struct bt_ctf_trace *trace = NULL; + struct bt_ctf_stream_class *stream_class = NULL; + struct bt_ctf_field_type *header_field_type = NULL; + struct bt_ctf_field_type *expected_header_field_type = NULL; + + if (!packet || !header || packet->frozen) { + ret = -1; + goto end; + } + + stream_class = bt_ctf_stream_get_class(packet->stream); + assert(stream_class); + trace = bt_ctf_stream_class_get_trace(stream_class); + assert(trace); + header_field_type = bt_ctf_field_get_type(header); + assert(header_field_type); + expected_header_field_type = bt_ctf_trace_get_packet_header_type(trace); + + if (bt_ctf_field_type_compare(header_field_type, + expected_header_field_type)) { + ret = -1; + goto end; + } + + bt_put(packet->header); + packet->header = bt_get(header); + +end: + BT_PUT(trace); + BT_PUT(stream_class); + BT_PUT(header_field_type); + BT_PUT(expected_header_field_type); + + return ret; +} + +struct bt_ctf_field *bt_ctf_packet_get_context( + struct bt_ctf_packet *packet) +{ + return packet ? bt_get(packet->context) : NULL; +} + +int bt_ctf_packet_set_context(struct bt_ctf_packet *packet, + struct bt_ctf_field *context) +{ + int ret = 0; + struct bt_ctf_stream_class *stream_class = NULL; + struct bt_ctf_field_type *context_field_type = NULL; + struct bt_ctf_field_type *expected_context_field_type = NULL; + + if (!packet || !context || packet->frozen) { + ret = -1; + goto end; + } + + stream_class = bt_ctf_stream_get_class(packet->stream); + assert(stream_class); + context_field_type = bt_ctf_field_get_type(context); + assert(context_field_type); + expected_context_field_type = + bt_ctf_stream_class_get_packet_context_type(stream_class); + + if (bt_ctf_field_type_compare(context_field_type, + expected_context_field_type)) { + ret = -1; + goto end; + } + + bt_put(packet->context); + packet->context = bt_get(context); + +end: + BT_PUT(stream_class); + BT_PUT(context_field_type); + BT_PUT(expected_context_field_type); + + return ret; +} + +BT_HIDDEN +void bt_ctf_packet_freeze(struct bt_ctf_packet *packet) +{ + if (!packet) { + goto end; + } + + bt_ctf_field_freeze(packet->header); + bt_ctf_field_freeze(packet->context); + packet->frozen = 1; + +end: + return; +} + +static +void bt_ctf_packet_destroy(struct bt_object *obj) +{ + struct bt_ctf_packet *packet; + + packet = container_of(obj, struct bt_ctf_packet, base); + bt_put(packet->header); + bt_put(packet->context); + bt_put(packet->stream); + g_free(packet); +} + +extern struct bt_ctf_packet *bt_ctf_packet_create( + struct bt_ctf_stream *stream) +{ + struct bt_ctf_packet *packet = NULL; + struct bt_ctf_stream_class *stream_class = NULL; + struct bt_ctf_trace *trace = NULL; + + if (!stream || stream->pos.fd >= 0) { + goto end; + } + + stream_class = bt_ctf_stream_get_class(stream); + assert(stream_class); + trace = bt_ctf_stream_class_get_trace(stream_class); + assert(trace); + packet = g_new0(struct bt_ctf_packet, 1); + if (!packet) { + goto end; + } + + bt_object_init(packet, bt_ctf_packet_destroy); + packet->stream = bt_get(stream); + packet->header = bt_ctf_field_create(trace->packet_header_type); + if (!packet->header) { + BT_PUT(packet); + goto end; + } + + packet->context = bt_ctf_field_create( + stream->stream_class->packet_context_type); + if (!packet->context) { + BT_PUT(packet); + goto end; + } + +end: + BT_PUT(trace); + BT_PUT(stream_class); + + return packet; +} diff --git a/include/Makefile.am b/include/Makefile.am index f022571b..e7f32eab 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -31,6 +31,7 @@ babeltracectfirinclude_HEADERS = \ babeltrace/ctf-ir/event-class.h \ babeltrace/ctf-ir/field-path.h \ babeltrace/ctf-ir/stream.h \ + babeltrace/ctf-ir/packet.h \ babeltrace/ctf-ir/stream-class.h \ babeltrace/ctf-ir/trace.h \ babeltrace/ctf-ir/utils.h @@ -67,6 +68,7 @@ noinst_HEADERS = \ babeltrace/ctf-ir/resolve-internal.h \ babeltrace/ctf-ir/stream-class-internal.h \ babeltrace/ctf-ir/stream-internal.h \ + babeltrace/ctf-ir/packet-internal.h \ babeltrace/ctf-ir/trace-internal.h \ babeltrace/ctf-ir/validation-internal.h \ babeltrace/ctf-writer/functor-internal.h \ diff --git a/include/babeltrace/ctf-ir/packet-internal.h b/include/babeltrace/ctf-ir/packet-internal.h new file mode 100644 index 00000000..275524ba --- /dev/null +++ b/include/babeltrace/ctf-ir/packet-internal.h @@ -0,0 +1,44 @@ +#ifndef BABELTRACE_CTF_IR_PACKET_INTERNAL_H +#define BABELTRACE_CTF_IR_PACKET_INTERNAL_H + +/* + * Babeltrace - CTF IR: Stream packet internal + * + * Copyright 2016 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include + +struct bt_ctf_packet { + struct bt_object base; + struct bt_ctf_field *header; + struct bt_ctf_field *context; + struct bt_ctf_stream *stream; + int frozen; +}; + +BT_HIDDEN +void bt_ctf_packet_freeze(struct bt_ctf_packet *packet); + +#endif /* BABELTRACE_CTF_IR_PACKET_INTERNAL_H */ diff --git a/include/babeltrace/ctf-ir/packet.h b/include/babeltrace/ctf-ir/packet.h new file mode 100644 index 00000000..40aaab15 --- /dev/null +++ b/include/babeltrace/ctf-ir/packet.h @@ -0,0 +1,62 @@ +#ifndef BABELTRACE_CTF_IR_PACKET_H +#define BABELTRACE_CTF_IR_PACKET_H + +/* + * BabelTrace - CTF IR: Stream packet + * + * Copyright 2016 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * The Common Trace Format (CTF) Specification is available at + * http://www.efficios.com/ctf + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_ctf_stream; +struct bt_ctf_packet; + +extern struct bt_ctf_packet *bt_ctf_packet_create( + struct bt_ctf_stream *stream); + +extern struct bt_ctf_stream *bt_ctf_packet_get_stream( + struct bt_ctf_packet *packet); + +extern struct bt_ctf_field *bt_ctf_packet_get_header( + struct bt_ctf_packet *packet); + +extern int bt_ctf_packet_set_header( + struct bt_ctf_packet *packet, struct bt_ctf_field *header); + +extern struct bt_ctf_field *bt_ctf_packet_get_context( + struct bt_ctf_packet *context); + +extern int bt_ctf_packet_set_context( + struct bt_ctf_packet *packet, struct bt_ctf_field *context); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE_CTF_IR_PACKET_H */ diff --git a/include/babeltrace/ctf-ir/stream-class-internal.h b/include/babeltrace/ctf-ir/stream-class-internal.h index 453b0851..1bd1ea18 100644 --- a/include/babeltrace/ctf-ir/stream-class-internal.h +++ b/include/babeltrace/ctf-ir/stream-class-internal.h @@ -34,6 +34,7 @@ #include #include #include +#include #include struct bt_ctf_stream_class { -- 2.34.1