Commit | Line | Data |
---|---|---|
f79cf0f0 PP |
1 | /* |
2 | * packet.c | |
3 | * | |
4 | * Babeltrace CTF IR - Stream packet | |
5 | * | |
6 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> | |
7 | * | |
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: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
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 | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
8ea705f0 | 27 | #define BT_LOG_TAG "PACKET" |
be514b0c | 28 | #include <babeltrace/lib-logging-internal.h> |
8ea705f0 | 29 | |
f79cf0f0 PP |
30 | #include <babeltrace/ctf-ir/fields-internal.h> |
31 | #include <babeltrace/ctf-ir/packet.h> | |
32 | #include <babeltrace/ctf-ir/packet-internal.h> | |
33 | #include <babeltrace/ctf-ir/trace.h> | |
34 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
35 | #include <babeltrace/ctf-ir/stream-class.h> | |
36 | #include <babeltrace/ctf-ir/stream.h> | |
37 | #include <babeltrace/ctf-ir/stream-internal.h> | |
38 | #include <babeltrace/ctf-ir/trace-internal.h> | |
39 | #include <babeltrace/object-internal.h> | |
40 | #include <babeltrace/ref.h> | |
be514b0c | 41 | #include <inttypes.h> |
f79cf0f0 | 42 | |
50842bdc | 43 | struct bt_stream *bt_packet_get_stream(struct bt_packet *packet) |
f79cf0f0 PP |
44 | { |
45 | return packet ? bt_get(packet->stream) : NULL; | |
46 | } | |
47 | ||
50842bdc PP |
48 | struct bt_field *bt_packet_get_header( |
49 | struct bt_packet *packet) | |
f79cf0f0 PP |
50 | { |
51 | return packet ? bt_get(packet->header) : NULL; | |
52 | } | |
53 | ||
50842bdc PP |
54 | int bt_packet_set_header(struct bt_packet *packet, |
55 | struct bt_field *header) | |
f79cf0f0 PP |
56 | { |
57 | int ret = 0; | |
50842bdc PP |
58 | struct bt_trace *trace = NULL; |
59 | struct bt_stream_class *stream_class = NULL; | |
60 | struct bt_field_type *header_field_type = NULL; | |
61 | struct bt_field_type *expected_header_field_type = NULL; | |
f79cf0f0 | 62 | |
8ea705f0 PP |
63 | if (!packet) { |
64 | BT_LOGW_STR("Invalid parameter: packet is NULL."); | |
65 | ret = -1; | |
66 | goto end; | |
67 | } | |
68 | ||
69 | if (packet->frozen) { | |
70 | BT_LOGW("Invalid parameter: packet is frozen: addr=%p", | |
71 | packet); | |
f79cf0f0 PP |
72 | ret = -1; |
73 | goto end; | |
74 | } | |
75 | ||
50842bdc | 76 | stream_class = bt_stream_get_class(packet->stream); |
f79cf0f0 | 77 | assert(stream_class); |
50842bdc | 78 | trace = bt_stream_class_get_trace(stream_class); |
f79cf0f0 | 79 | assert(trace); |
50842bdc | 80 | expected_header_field_type = bt_trace_get_packet_header_type(trace); |
c5c82c6e PP |
81 | |
82 | if (!header) { | |
83 | if (expected_header_field_type) { | |
84 | BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: " | |
85 | "packet-addr=%p, packet-header-ft-addr=%p", | |
86 | packet, expected_header_field_type); | |
87 | ret = -1; | |
88 | goto end; | |
89 | } | |
90 | ||
91 | goto skip_validation; | |
92 | } | |
93 | ||
50842bdc | 94 | header_field_type = bt_field_get_type(header); |
f79cf0f0 | 95 | assert(header_field_type); |
f79cf0f0 | 96 | |
50842bdc | 97 | if (bt_field_type_compare(header_field_type, |
f79cf0f0 | 98 | expected_header_field_type)) { |
8ea705f0 PP |
99 | BT_LOGW("Invalid parameter: packet header's field type is different from the trace's packet header field type: " |
100 | "packet-addr=%p, packet-header-addr=%p", | |
101 | packet, header); | |
f79cf0f0 PP |
102 | ret = -1; |
103 | goto end; | |
104 | } | |
105 | ||
03bc92d4 | 106 | skip_validation: |
f79cf0f0 PP |
107 | bt_put(packet->header); |
108 | packet->header = bt_get(header); | |
8ea705f0 PP |
109 | BT_LOGV("Set packet's header field: packet-addr=%p, packet-header-addr=%p", |
110 | packet, header); | |
f79cf0f0 PP |
111 | |
112 | end: | |
113 | BT_PUT(trace); | |
114 | BT_PUT(stream_class); | |
115 | BT_PUT(header_field_type); | |
116 | BT_PUT(expected_header_field_type); | |
117 | ||
118 | return ret; | |
119 | } | |
120 | ||
50842bdc PP |
121 | struct bt_field *bt_packet_get_context( |
122 | struct bt_packet *packet) | |
f79cf0f0 PP |
123 | { |
124 | return packet ? bt_get(packet->context) : NULL; | |
125 | } | |
126 | ||
50842bdc PP |
127 | int bt_packet_set_context(struct bt_packet *packet, |
128 | struct bt_field *context) | |
f79cf0f0 PP |
129 | { |
130 | int ret = 0; | |
50842bdc PP |
131 | struct bt_stream_class *stream_class = NULL; |
132 | struct bt_field_type *context_field_type = NULL; | |
133 | struct bt_field_type *expected_context_field_type = NULL; | |
f79cf0f0 | 134 | |
8ea705f0 PP |
135 | if (!packet) { |
136 | BT_LOGW_STR("Invalid parameter: packet is NULL."); | |
137 | ret = -1; | |
138 | goto end; | |
139 | } | |
140 | ||
141 | if (packet->frozen) { | |
142 | BT_LOGW("Invalid parameter: packet is frozen: addr=%p", | |
143 | packet); | |
f79cf0f0 PP |
144 | ret = -1; |
145 | goto end; | |
146 | } | |
147 | ||
50842bdc | 148 | stream_class = bt_stream_get_class(packet->stream); |
c5c82c6e PP |
149 | assert(stream_class); |
150 | expected_context_field_type = | |
50842bdc | 151 | bt_stream_class_get_packet_context_type(stream_class); |
c5c82c6e | 152 | |
03bc92d4 | 153 | if (!context) { |
c5c82c6e PP |
154 | if (expected_context_field_type) { |
155 | BT_LOGW("Invalid parameter: setting no packet context but packet context field type is not NULL: " | |
156 | "packet-addr=%p, packet-context-ft-addr=%p", | |
157 | packet, expected_context_field_type); | |
158 | ret = -1; | |
159 | goto end; | |
160 | } | |
161 | ||
03bc92d4 JG |
162 | goto skip_validation; |
163 | } | |
164 | ||
50842bdc | 165 | context_field_type = bt_field_get_type(context); |
f79cf0f0 | 166 | assert(context_field_type); |
f79cf0f0 | 167 | |
50842bdc | 168 | if (bt_field_type_compare(context_field_type, |
f79cf0f0 | 169 | expected_context_field_type)) { |
8ea705f0 PP |
170 | BT_LOGW("Invalid parameter: packet context's field type is different from the stream class's packet context field type: " |
171 | "packet-addr=%p, packet-context-addr=%p", | |
172 | packet, context); | |
f79cf0f0 PP |
173 | ret = -1; |
174 | goto end; | |
175 | } | |
176 | ||
03bc92d4 | 177 | skip_validation: |
f79cf0f0 PP |
178 | bt_put(packet->context); |
179 | packet->context = bt_get(context); | |
8ea705f0 PP |
180 | BT_LOGV("Set packet's context field: packet-addr=%p, packet-context-addr=%p", |
181 | packet, context); | |
f79cf0f0 PP |
182 | |
183 | end: | |
184 | BT_PUT(stream_class); | |
185 | BT_PUT(context_field_type); | |
186 | BT_PUT(expected_context_field_type); | |
f79cf0f0 PP |
187 | return ret; |
188 | } | |
189 | ||
190 | BT_HIDDEN | |
50842bdc | 191 | void bt_packet_freeze(struct bt_packet *packet) |
f79cf0f0 | 192 | { |
8ea705f0 | 193 | if (!packet || packet->frozen) { |
5c3b707d | 194 | return; |
f79cf0f0 PP |
195 | } |
196 | ||
8ea705f0 | 197 | BT_LOGD("Freezing packet: addr=%p", packet); |
d409daba | 198 | BT_LOGD_STR("Freezing packet's header field."); |
50842bdc | 199 | bt_field_freeze(packet->header); |
d409daba | 200 | BT_LOGD_STR("Freezing packet's context field."); |
50842bdc | 201 | bt_field_freeze(packet->context); |
f79cf0f0 | 202 | packet->frozen = 1; |
f79cf0f0 PP |
203 | } |
204 | ||
205 | static | |
50842bdc | 206 | void bt_packet_destroy(struct bt_object *obj) |
f79cf0f0 | 207 | { |
50842bdc | 208 | struct bt_packet *packet; |
f79cf0f0 | 209 | |
50842bdc | 210 | packet = container_of(obj, struct bt_packet, base); |
8ea705f0 | 211 | BT_LOGD("Destroying packet: addr=%p", packet); |
d409daba | 212 | BT_LOGD_STR("Putting packet's header field."); |
f79cf0f0 | 213 | bt_put(packet->header); |
d409daba | 214 | BT_LOGD_STR("Putting packet's context field."); |
f79cf0f0 | 215 | bt_put(packet->context); |
d409daba | 216 | BT_LOGD_STR("Putting packet's stream."); |
f79cf0f0 PP |
217 | bt_put(packet->stream); |
218 | g_free(packet); | |
219 | } | |
220 | ||
50842bdc PP |
221 | struct bt_packet *bt_packet_create( |
222 | struct bt_stream *stream) | |
f79cf0f0 | 223 | { |
50842bdc PP |
224 | struct bt_packet *packet = NULL; |
225 | struct bt_stream_class *stream_class = NULL; | |
226 | struct bt_trace *trace = NULL; | |
f79cf0f0 | 227 | |
8ea705f0 PP |
228 | if (!stream) { |
229 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
230 | goto end; | |
231 | } | |
232 | ||
be514b0c PP |
233 | BT_LOGD("Creating packet object: stream-addr=%p, " |
234 | "stream-name=\"%s\", stream-class-addr=%p, " | |
235 | "stream-class-name=\"%s\", stream-class-id=%" PRId64, | |
50842bdc | 236 | stream, bt_stream_get_name(stream), |
be514b0c | 237 | stream->stream_class, |
50842bdc PP |
238 | bt_stream_class_get_name(stream->stream_class), |
239 | bt_stream_class_get_id(stream->stream_class)); | |
be514b0c | 240 | |
8ea705f0 PP |
241 | if (stream->pos.fd >= 0) { |
242 | BT_LOGW_STR("Invalid parameter: stream is a CTF writer stream."); | |
f79cf0f0 PP |
243 | goto end; |
244 | } | |
245 | ||
50842bdc | 246 | stream_class = bt_stream_get_class(stream); |
f79cf0f0 | 247 | assert(stream_class); |
50842bdc | 248 | trace = bt_stream_class_get_trace(stream_class); |
f79cf0f0 | 249 | assert(trace); |
50842bdc | 250 | packet = g_new0(struct bt_packet, 1); |
f79cf0f0 | 251 | if (!packet) { |
8ea705f0 | 252 | BT_LOGE_STR("Failed to allocate one packet object."); |
f79cf0f0 PP |
253 | goto end; |
254 | } | |
255 | ||
50842bdc | 256 | bt_object_init(packet, bt_packet_destroy); |
f79cf0f0 | 257 | packet->stream = bt_get(stream); |
be514b0c PP |
258 | |
259 | if (trace->packet_header_type) { | |
260 | BT_LOGD("Creating initial packet header field: ft-addr=%p", | |
261 | trace->packet_header_type); | |
50842bdc | 262 | packet->header = bt_field_create(trace->packet_header_type); |
be514b0c PP |
263 | if (!packet->header) { |
264 | BT_LOGE_STR("Cannot create initial packet header field object."); | |
265 | BT_PUT(packet); | |
266 | goto end; | |
267 | } | |
f79cf0f0 PP |
268 | } |
269 | ||
be514b0c PP |
270 | if (stream->stream_class->packet_context_type) { |
271 | BT_LOGD("Creating initial packet context field: ft-addr=%p", | |
272 | stream->stream_class->packet_context_type); | |
50842bdc | 273 | packet->context = bt_field_create( |
be514b0c PP |
274 | stream->stream_class->packet_context_type); |
275 | if (!packet->context) { | |
276 | BT_LOGE_STR("Cannot create initial packet header field object."); | |
277 | BT_PUT(packet); | |
278 | goto end; | |
279 | } | |
f79cf0f0 PP |
280 | } |
281 | ||
8ea705f0 PP |
282 | BT_LOGD("Created packet object: addr=%p", packet); |
283 | ||
f79cf0f0 PP |
284 | end: |
285 | BT_PUT(trace); | |
286 | BT_PUT(stream_class); | |
287 | ||
288 | return packet; | |
289 | } |