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 PP |
27 | #define BT_LOG_TAG "PACKET" |
28 | ||
f79cf0f0 PP |
29 | #include <babeltrace/ctf-ir/fields-internal.h> |
30 | #include <babeltrace/ctf-ir/packet.h> | |
31 | #include <babeltrace/ctf-ir/packet-internal.h> | |
32 | #include <babeltrace/ctf-ir/trace.h> | |
33 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
34 | #include <babeltrace/ctf-ir/stream-class.h> | |
35 | #include <babeltrace/ctf-ir/stream.h> | |
36 | #include <babeltrace/ctf-ir/stream-internal.h> | |
37 | #include <babeltrace/ctf-ir/trace-internal.h> | |
8ea705f0 | 38 | #include <babeltrace/lib-logging-internal.h> |
f79cf0f0 PP |
39 | #include <babeltrace/object-internal.h> |
40 | #include <babeltrace/ref.h> | |
41 | ||
42 | struct bt_ctf_stream *bt_ctf_packet_get_stream(struct bt_ctf_packet *packet) | |
43 | { | |
44 | return packet ? bt_get(packet->stream) : NULL; | |
45 | } | |
46 | ||
47 | struct bt_ctf_field *bt_ctf_packet_get_header( | |
48 | struct bt_ctf_packet *packet) | |
49 | { | |
50 | return packet ? bt_get(packet->header) : NULL; | |
51 | } | |
52 | ||
53 | int bt_ctf_packet_set_header(struct bt_ctf_packet *packet, | |
54 | struct bt_ctf_field *header) | |
55 | { | |
56 | int ret = 0; | |
57 | struct bt_ctf_trace *trace = NULL; | |
58 | struct bt_ctf_stream_class *stream_class = NULL; | |
59 | struct bt_ctf_field_type *header_field_type = NULL; | |
60 | struct bt_ctf_field_type *expected_header_field_type = NULL; | |
61 | ||
8ea705f0 PP |
62 | if (!packet) { |
63 | BT_LOGW_STR("Invalid parameter: packet is NULL."); | |
64 | ret = -1; | |
65 | goto end; | |
66 | } | |
67 | ||
68 | if (packet->frozen) { | |
69 | BT_LOGW("Invalid parameter: packet is frozen: addr=%p", | |
70 | packet); | |
f79cf0f0 PP |
71 | ret = -1; |
72 | goto end; | |
73 | } | |
74 | ||
03bc92d4 JG |
75 | if (!header) { |
76 | goto skip_validation; | |
77 | } | |
78 | ||
f79cf0f0 PP |
79 | stream_class = bt_ctf_stream_get_class(packet->stream); |
80 | assert(stream_class); | |
81 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
82 | assert(trace); | |
83 | header_field_type = bt_ctf_field_get_type(header); | |
84 | assert(header_field_type); | |
85 | expected_header_field_type = bt_ctf_trace_get_packet_header_type(trace); | |
86 | ||
87 | if (bt_ctf_field_type_compare(header_field_type, | |
88 | expected_header_field_type)) { | |
8ea705f0 PP |
89 | BT_LOGW("Invalid parameter: packet header's field type is different from the trace's packet header field type: " |
90 | "packet-addr=%p, packet-header-addr=%p", | |
91 | packet, header); | |
f79cf0f0 PP |
92 | ret = -1; |
93 | goto end; | |
94 | } | |
95 | ||
03bc92d4 | 96 | skip_validation: |
f79cf0f0 PP |
97 | bt_put(packet->header); |
98 | packet->header = bt_get(header); | |
8ea705f0 PP |
99 | BT_LOGV("Set packet's header field: packet-addr=%p, packet-header-addr=%p", |
100 | packet, header); | |
f79cf0f0 PP |
101 | |
102 | end: | |
103 | BT_PUT(trace); | |
104 | BT_PUT(stream_class); | |
105 | BT_PUT(header_field_type); | |
106 | BT_PUT(expected_header_field_type); | |
107 | ||
108 | return ret; | |
109 | } | |
110 | ||
111 | struct bt_ctf_field *bt_ctf_packet_get_context( | |
112 | struct bt_ctf_packet *packet) | |
113 | { | |
114 | return packet ? bt_get(packet->context) : NULL; | |
115 | } | |
116 | ||
117 | int bt_ctf_packet_set_context(struct bt_ctf_packet *packet, | |
118 | struct bt_ctf_field *context) | |
119 | { | |
120 | int ret = 0; | |
121 | struct bt_ctf_stream_class *stream_class = NULL; | |
122 | struct bt_ctf_field_type *context_field_type = NULL; | |
123 | struct bt_ctf_field_type *expected_context_field_type = NULL; | |
124 | ||
8ea705f0 PP |
125 | if (!packet) { |
126 | BT_LOGW_STR("Invalid parameter: packet is NULL."); | |
127 | ret = -1; | |
128 | goto end; | |
129 | } | |
130 | ||
131 | if (packet->frozen) { | |
132 | BT_LOGW("Invalid parameter: packet is frozen: addr=%p", | |
133 | packet); | |
f79cf0f0 PP |
134 | ret = -1; |
135 | goto end; | |
136 | } | |
137 | ||
03bc92d4 JG |
138 | if (!context) { |
139 | goto skip_validation; | |
140 | } | |
141 | ||
f79cf0f0 PP |
142 | stream_class = bt_ctf_stream_get_class(packet->stream); |
143 | assert(stream_class); | |
144 | context_field_type = bt_ctf_field_get_type(context); | |
145 | assert(context_field_type); | |
146 | expected_context_field_type = | |
147 | bt_ctf_stream_class_get_packet_context_type(stream_class); | |
148 | ||
149 | if (bt_ctf_field_type_compare(context_field_type, | |
150 | expected_context_field_type)) { | |
8ea705f0 PP |
151 | BT_LOGW("Invalid parameter: packet context's field type is different from the stream class's packet context field type: " |
152 | "packet-addr=%p, packet-context-addr=%p", | |
153 | packet, context); | |
f79cf0f0 PP |
154 | ret = -1; |
155 | goto end; | |
156 | } | |
157 | ||
03bc92d4 | 158 | skip_validation: |
f79cf0f0 PP |
159 | bt_put(packet->context); |
160 | packet->context = bt_get(context); | |
8ea705f0 PP |
161 | BT_LOGV("Set packet's context field: packet-addr=%p, packet-context-addr=%p", |
162 | packet, context); | |
f79cf0f0 PP |
163 | |
164 | end: | |
165 | BT_PUT(stream_class); | |
166 | BT_PUT(context_field_type); | |
167 | BT_PUT(expected_context_field_type); | |
f79cf0f0 PP |
168 | return ret; |
169 | } | |
170 | ||
171 | BT_HIDDEN | |
172 | void bt_ctf_packet_freeze(struct bt_ctf_packet *packet) | |
173 | { | |
8ea705f0 | 174 | if (!packet || packet->frozen) { |
5c3b707d | 175 | return; |
f79cf0f0 PP |
176 | } |
177 | ||
8ea705f0 | 178 | BT_LOGD("Freezing packet: addr=%p", packet); |
918be005 PP |
179 | bt_ctf_field_freeze(packet->header); |
180 | bt_ctf_field_freeze(packet->context); | |
f79cf0f0 | 181 | packet->frozen = 1; |
f79cf0f0 PP |
182 | } |
183 | ||
184 | static | |
185 | void bt_ctf_packet_destroy(struct bt_object *obj) | |
186 | { | |
187 | struct bt_ctf_packet *packet; | |
188 | ||
189 | packet = container_of(obj, struct bt_ctf_packet, base); | |
8ea705f0 | 190 | BT_LOGD("Destroying packet: addr=%p", packet); |
f79cf0f0 PP |
191 | bt_put(packet->header); |
192 | bt_put(packet->context); | |
193 | bt_put(packet->stream); | |
194 | g_free(packet); | |
195 | } | |
196 | ||
5c3b707d | 197 | struct bt_ctf_packet *bt_ctf_packet_create( |
f79cf0f0 PP |
198 | struct bt_ctf_stream *stream) |
199 | { | |
200 | struct bt_ctf_packet *packet = NULL; | |
201 | struct bt_ctf_stream_class *stream_class = NULL; | |
202 | struct bt_ctf_trace *trace = NULL; | |
203 | ||
8ea705f0 PP |
204 | BT_LOGD("Creating packet object: stream-addr=%p", stream); |
205 | ||
206 | if (!stream) { | |
207 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
208 | goto end; | |
209 | } | |
210 | ||
211 | if (stream->pos.fd >= 0) { | |
212 | BT_LOGW_STR("Invalid parameter: stream is a CTF writer stream."); | |
f79cf0f0 PP |
213 | goto end; |
214 | } | |
215 | ||
216 | stream_class = bt_ctf_stream_get_class(stream); | |
217 | assert(stream_class); | |
218 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
219 | assert(trace); | |
220 | packet = g_new0(struct bt_ctf_packet, 1); | |
221 | if (!packet) { | |
8ea705f0 | 222 | BT_LOGE_STR("Failed to allocate one packet object."); |
f79cf0f0 PP |
223 | goto end; |
224 | } | |
225 | ||
226 | bt_object_init(packet, bt_ctf_packet_destroy); | |
227 | packet->stream = bt_get(stream); | |
228 | packet->header = bt_ctf_field_create(trace->packet_header_type); | |
03bc92d4 | 229 | if (!packet->header && trace->packet_header_type) { |
8ea705f0 | 230 | BT_LOGE_STR("Cannot create initial packet header field object."); |
f79cf0f0 PP |
231 | BT_PUT(packet); |
232 | goto end; | |
233 | } | |
234 | ||
235 | packet->context = bt_ctf_field_create( | |
236 | stream->stream_class->packet_context_type); | |
03bc92d4 | 237 | if (!packet->context && stream->stream_class->packet_context_type) { |
8ea705f0 | 238 | BT_LOGE_STR("Cannot create initial packet header field object."); |
f79cf0f0 PP |
239 | BT_PUT(packet); |
240 | goto end; | |
241 | } | |
242 | ||
8ea705f0 PP |
243 | BT_LOGD("Created packet object: addr=%p", packet); |
244 | ||
f79cf0f0 PP |
245 | end: |
246 | BT_PUT(trace); | |
247 | BT_PUT(stream_class); | |
248 | ||
249 | return packet; | |
250 | } |