4 * Babeltrace CTF IR - Stream packet
6 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #define BT_LOG_TAG "PACKET"
28 #include <babeltrace/lib-logging-internal.h>
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/fields-internal.h>
32 #include <babeltrace/ctf-ir/packet.h>
33 #include <babeltrace/ctf-ir/packet-internal.h>
34 #include <babeltrace/ctf-ir/trace.h>
35 #include <babeltrace/ctf-ir/stream-class-internal.h>
36 #include <babeltrace/ctf-ir/stream-class.h>
37 #include <babeltrace/ctf-ir/stream.h>
38 #include <babeltrace/ctf-ir/stream-internal.h>
39 #include <babeltrace/ctf-ir/trace-internal.h>
40 #include <babeltrace/object-internal.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/assert-internal.h>
45 struct bt_stream
*bt_packet_borrow_stream(struct bt_packet
*packet
)
47 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
48 return packet
->stream
;
51 struct bt_field
*bt_packet_borrow_header(
52 struct bt_packet
*packet
)
54 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
55 return packet
->header
;
59 static inline bool validate_field_to_set(struct bt_field
*field
,
60 struct bt_field_type_common
*expected_ft
)
66 BT_ASSERT_PRE_MSG("Setting no field, but expected "
67 "field type is not NULL: "
68 "%![field-]+f, %![expected-ft-]+F",
77 if (bt_field_type_compare(bt_field_borrow_type(field
),
78 BT_FROM_COMMON(expected_ft
)) != 0) {
79 BT_ASSERT_PRE_MSG("Field type is different from expected "
80 " field type: %![field-ft-]+F, %![expected-ft-]+F",
81 bt_field_borrow_type(field
), expected_ft
);
90 int bt_packet_set_header(struct bt_packet
*packet
,
91 struct bt_field
*header
)
93 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
94 BT_ASSERT_PRE_HOT(packet
, "Packet", ": +%!+a", packet
);
95 BT_ASSERT_PRE(validate_field_to_set(header
,
96 bt_stream_class_borrow_trace(
97 BT_FROM_COMMON(packet
->stream
->common
.stream_class
))->common
.packet_header_field_type
),
98 "Invalid packet header field: "
99 "%![packet-]+a, %![field-]+f", packet
, header
);
100 bt_put(packet
->header
);
101 packet
->header
= bt_get(header
);
102 BT_LOGV("Set packet's header field: packet-addr=%p, packet-header-addr=%p",
107 struct bt_field
*bt_packet_borrow_context(struct bt_packet
*packet
)
109 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
110 return packet
->context
;
113 int bt_packet_set_context(struct bt_packet
*packet
,
114 struct bt_field
*context
)
116 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
117 BT_ASSERT_PRE_HOT(packet
, "Packet", ": +%!+a", packet
);
118 BT_ASSERT_PRE(validate_field_to_set(context
,
119 BT_FROM_COMMON(packet
->stream
->common
.stream_class
->packet_context_field_type
)),
120 "Invalid packet context field: "
121 "%![packet-]+a, %![field-]+f", packet
, context
);
122 bt_put(packet
->context
);
123 packet
->context
= bt_get(context
);
124 BT_LOGV("Set packet's context field: packet-addr=%p, packet-context-addr=%p",
130 void _bt_packet_freeze(struct bt_packet
*packet
)
132 if (!packet
|| packet
->frozen
) {
136 BT_LOGD("Freezing packet: addr=%p", packet
);
137 BT_LOGD_STR("Freezing packet's header field.");
138 bt_field_freeze_recursive(packet
->header
);
139 BT_LOGD_STR("Freezing packet's context field.");
140 bt_field_freeze_recursive(packet
->context
);
145 void bt_packet_destroy(struct bt_object
*obj
)
147 struct bt_packet
*packet
= (void *) obj
;
149 BT_LOGD("Destroying packet: addr=%p", packet
);
150 BT_LOGD_STR("Putting packet's header field.");
151 bt_put(packet
->header
);
152 BT_LOGD_STR("Putting packet's context field.");
153 bt_put(packet
->context
);
154 BT_LOGD_STR("Putting packet's stream.");
155 bt_put(packet
->stream
);
159 struct bt_packet
*bt_packet_create(
160 struct bt_stream
*stream
)
162 struct bt_packet
*packet
= NULL
;
163 struct bt_stream_class
*stream_class
= NULL
;
164 struct bt_trace
*trace
= NULL
;
166 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
167 BT_LOGD("Creating packet object: stream-addr=%p, "
168 "stream-name=\"%s\", stream-class-addr=%p, "
169 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
170 stream
, bt_stream_get_name(stream
),
171 stream
->common
.stream_class
,
172 bt_stream_class_common_get_name(stream
->common
.stream_class
),
173 bt_stream_class_common_get_id(stream
->common
.stream_class
));
174 stream_class
= bt_stream_borrow_class(stream
);
175 BT_ASSERT(stream_class
);
176 trace
= bt_stream_class_borrow_trace(stream_class
);
178 packet
= g_new0(struct bt_packet
, 1);
180 BT_LOGE_STR("Failed to allocate one packet object.");
184 bt_object_init(packet
, bt_packet_destroy
);
185 packet
->stream
= bt_get(stream
);
187 if (trace
->common
.packet_header_field_type
) {
188 BT_LOGD("Creating initial packet header field: ft-addr=%p",
189 trace
->common
.packet_header_field_type
);
190 packet
->header
= bt_field_create(
191 BT_FROM_COMMON(trace
->common
.packet_header_field_type
));
192 if (!packet
->header
) {
193 BT_LOGE_STR("Cannot create initial packet header field object.");
199 if (stream
->common
.stream_class
->packet_context_field_type
) {
200 BT_LOGD("Creating initial packet context field: ft-addr=%p",
201 stream
->common
.stream_class
->packet_context_field_type
);
202 packet
->context
= bt_field_create(
203 BT_FROM_COMMON(stream
->common
.stream_class
->packet_context_field_type
));
204 if (!packet
->context
) {
205 BT_LOGE_STR("Cannot create initial packet header field object.");
211 BT_LOGD("Created packet object: addr=%p", packet
);