| 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 | |
| 27 | #define BT_LOG_TAG "PACKET" |
| 28 | |
| 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> |
| 38 | #include <babeltrace/lib-logging-internal.h> |
| 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 | |
| 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); |
| 71 | ret = -1; |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | stream_class = bt_ctf_stream_get_class(packet->stream); |
| 76 | assert(stream_class); |
| 77 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 78 | assert(trace); |
| 79 | expected_header_field_type = bt_ctf_trace_get_packet_header_type(trace); |
| 80 | |
| 81 | if (!header) { |
| 82 | if (expected_header_field_type) { |
| 83 | BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: " |
| 84 | "packet-addr=%p, packet-header-ft-addr=%p", |
| 85 | packet, expected_header_field_type); |
| 86 | ret = -1; |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | goto skip_validation; |
| 91 | } |
| 92 | |
| 93 | header_field_type = bt_ctf_field_get_type(header); |
| 94 | assert(header_field_type); |
| 95 | |
| 96 | if (bt_ctf_field_type_compare(header_field_type, |
| 97 | expected_header_field_type)) { |
| 98 | BT_LOGW("Invalid parameter: packet header's field type is different from the trace's packet header field type: " |
| 99 | "packet-addr=%p, packet-header-addr=%p", |
| 100 | packet, header); |
| 101 | ret = -1; |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | skip_validation: |
| 106 | bt_put(packet->header); |
| 107 | packet->header = bt_get(header); |
| 108 | BT_LOGV("Set packet's header field: packet-addr=%p, packet-header-addr=%p", |
| 109 | packet, header); |
| 110 | |
| 111 | end: |
| 112 | BT_PUT(trace); |
| 113 | BT_PUT(stream_class); |
| 114 | BT_PUT(header_field_type); |
| 115 | BT_PUT(expected_header_field_type); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | struct bt_ctf_field *bt_ctf_packet_get_context( |
| 121 | struct bt_ctf_packet *packet) |
| 122 | { |
| 123 | return packet ? bt_get(packet->context) : NULL; |
| 124 | } |
| 125 | |
| 126 | int bt_ctf_packet_set_context(struct bt_ctf_packet *packet, |
| 127 | struct bt_ctf_field *context) |
| 128 | { |
| 129 | int ret = 0; |
| 130 | struct bt_ctf_stream_class *stream_class = NULL; |
| 131 | struct bt_ctf_field_type *context_field_type = NULL; |
| 132 | struct bt_ctf_field_type *expected_context_field_type = NULL; |
| 133 | |
| 134 | if (!packet) { |
| 135 | BT_LOGW_STR("Invalid parameter: packet is NULL."); |
| 136 | ret = -1; |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | if (packet->frozen) { |
| 141 | BT_LOGW("Invalid parameter: packet is frozen: addr=%p", |
| 142 | packet); |
| 143 | ret = -1; |
| 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | stream_class = bt_ctf_stream_get_class(packet->stream); |
| 148 | assert(stream_class); |
| 149 | expected_context_field_type = |
| 150 | bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 151 | |
| 152 | if (!context) { |
| 153 | if (expected_context_field_type) { |
| 154 | BT_LOGW("Invalid parameter: setting no packet context but packet context field type is not NULL: " |
| 155 | "packet-addr=%p, packet-context-ft-addr=%p", |
| 156 | packet, expected_context_field_type); |
| 157 | ret = -1; |
| 158 | goto end; |
| 159 | } |
| 160 | |
| 161 | goto skip_validation; |
| 162 | } |
| 163 | |
| 164 | context_field_type = bt_ctf_field_get_type(context); |
| 165 | assert(context_field_type); |
| 166 | |
| 167 | if (bt_ctf_field_type_compare(context_field_type, |
| 168 | expected_context_field_type)) { |
| 169 | BT_LOGW("Invalid parameter: packet context's field type is different from the stream class's packet context field type: " |
| 170 | "packet-addr=%p, packet-context-addr=%p", |
| 171 | packet, context); |
| 172 | ret = -1; |
| 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | skip_validation: |
| 177 | bt_put(packet->context); |
| 178 | packet->context = bt_get(context); |
| 179 | BT_LOGV("Set packet's context field: packet-addr=%p, packet-context-addr=%p", |
| 180 | packet, context); |
| 181 | |
| 182 | end: |
| 183 | BT_PUT(stream_class); |
| 184 | BT_PUT(context_field_type); |
| 185 | BT_PUT(expected_context_field_type); |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | BT_HIDDEN |
| 190 | void bt_ctf_packet_freeze(struct bt_ctf_packet *packet) |
| 191 | { |
| 192 | if (!packet || packet->frozen) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | BT_LOGD("Freezing packet: addr=%p", packet); |
| 197 | BT_LOGD_STR("Freezing packet's header field."); |
| 198 | bt_ctf_field_freeze(packet->header); |
| 199 | BT_LOGD_STR("Freezing packet's context field."); |
| 200 | bt_ctf_field_freeze(packet->context); |
| 201 | packet->frozen = 1; |
| 202 | } |
| 203 | |
| 204 | static |
| 205 | void bt_ctf_packet_destroy(struct bt_object *obj) |
| 206 | { |
| 207 | struct bt_ctf_packet *packet; |
| 208 | |
| 209 | packet = container_of(obj, struct bt_ctf_packet, base); |
| 210 | BT_LOGD("Destroying packet: addr=%p", packet); |
| 211 | BT_LOGD_STR("Putting packet's header field."); |
| 212 | bt_put(packet->header); |
| 213 | BT_LOGD_STR("Putting packet's context field."); |
| 214 | bt_put(packet->context); |
| 215 | BT_LOGD_STR("Putting packet's stream."); |
| 216 | bt_put(packet->stream); |
| 217 | g_free(packet); |
| 218 | } |
| 219 | |
| 220 | struct bt_ctf_packet *bt_ctf_packet_create( |
| 221 | struct bt_ctf_stream *stream) |
| 222 | { |
| 223 | struct bt_ctf_packet *packet = NULL; |
| 224 | struct bt_ctf_stream_class *stream_class = NULL; |
| 225 | struct bt_ctf_trace *trace = NULL; |
| 226 | |
| 227 | BT_LOGD("Creating packet object: stream-addr=%p", stream); |
| 228 | |
| 229 | if (!stream) { |
| 230 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | if (stream->pos.fd >= 0) { |
| 235 | BT_LOGW_STR("Invalid parameter: stream is a CTF writer stream."); |
| 236 | goto end; |
| 237 | } |
| 238 | |
| 239 | stream_class = bt_ctf_stream_get_class(stream); |
| 240 | assert(stream_class); |
| 241 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 242 | assert(trace); |
| 243 | packet = g_new0(struct bt_ctf_packet, 1); |
| 244 | if (!packet) { |
| 245 | BT_LOGE_STR("Failed to allocate one packet object."); |
| 246 | goto end; |
| 247 | } |
| 248 | |
| 249 | bt_object_init(packet, bt_ctf_packet_destroy); |
| 250 | packet->stream = bt_get(stream); |
| 251 | packet->header = bt_ctf_field_create(trace->packet_header_type); |
| 252 | if (!packet->header && trace->packet_header_type) { |
| 253 | BT_LOGE_STR("Cannot create initial packet header field object."); |
| 254 | BT_PUT(packet); |
| 255 | goto end; |
| 256 | } |
| 257 | |
| 258 | packet->context = bt_ctf_field_create( |
| 259 | stream->stream_class->packet_context_type); |
| 260 | if (!packet->context && stream->stream_class->packet_context_type) { |
| 261 | BT_LOGE_STR("Cannot create initial packet header field object."); |
| 262 | BT_PUT(packet); |
| 263 | goto end; |
| 264 | } |
| 265 | |
| 266 | BT_LOGD("Created packet object: addr=%p", packet); |
| 267 | |
| 268 | end: |
| 269 | BT_PUT(trace); |
| 270 | BT_PUT(stream_class); |
| 271 | |
| 272 | return packet; |
| 273 | } |