| 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 | #include <babeltrace/lib-logging-internal.h> |
| 29 | |
| 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> |
| 43 | #include <inttypes.h> |
| 44 | |
| 45 | struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet) |
| 46 | { |
| 47 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
| 48 | return packet->stream; |
| 49 | } |
| 50 | |
| 51 | struct bt_field *bt_packet_borrow_header( |
| 52 | struct bt_packet *packet) |
| 53 | { |
| 54 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
| 55 | return packet->header; |
| 56 | } |
| 57 | |
| 58 | BT_ASSERT_PRE_FUNC |
| 59 | static inline bool validate_field_to_set(struct bt_field *field, |
| 60 | struct bt_field_type_common *expected_ft) |
| 61 | { |
| 62 | bool ret = true; |
| 63 | |
| 64 | if (!field) { |
| 65 | if (expected_ft) { |
| 66 | BT_ASSERT_PRE_MSG("Setting no field, but expected " |
| 67 | "field type is not NULL: " |
| 68 | "%![field-]+f, %![expected-ft-]+F", |
| 69 | field, expected_ft); |
| 70 | ret = false; |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | goto end; |
| 75 | } |
| 76 | |
| 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); |
| 82 | ret = false; |
| 83 | goto end; |
| 84 | } |
| 85 | |
| 86 | end: |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | int bt_packet_set_header(struct bt_packet *packet, |
| 91 | struct bt_field *header) |
| 92 | { |
| 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", |
| 103 | packet, header); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | struct bt_field *bt_packet_borrow_context(struct bt_packet *packet) |
| 108 | { |
| 109 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
| 110 | return packet->context; |
| 111 | } |
| 112 | |
| 113 | int bt_packet_set_context(struct bt_packet *packet, |
| 114 | struct bt_field *context) |
| 115 | { |
| 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", |
| 125 | packet, context); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | BT_HIDDEN |
| 130 | void _bt_packet_freeze(struct bt_packet *packet) |
| 131 | { |
| 132 | if (!packet || packet->frozen) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 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); |
| 141 | packet->frozen = 1; |
| 142 | } |
| 143 | |
| 144 | static |
| 145 | void bt_packet_destroy(struct bt_object *obj) |
| 146 | { |
| 147 | struct bt_packet *packet = (void *) obj; |
| 148 | |
| 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); |
| 156 | g_free(packet); |
| 157 | } |
| 158 | |
| 159 | struct bt_packet *bt_packet_create( |
| 160 | struct bt_stream *stream) |
| 161 | { |
| 162 | struct bt_packet *packet = NULL; |
| 163 | struct bt_stream_class *stream_class = NULL; |
| 164 | struct bt_trace *trace = NULL; |
| 165 | |
| 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); |
| 177 | BT_ASSERT(trace); |
| 178 | packet = g_new0(struct bt_packet, 1); |
| 179 | if (!packet) { |
| 180 | BT_LOGE_STR("Failed to allocate one packet object."); |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | bt_object_init(packet, bt_packet_destroy); |
| 185 | packet->stream = bt_get(stream); |
| 186 | |
| 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."); |
| 194 | BT_PUT(packet); |
| 195 | goto end; |
| 196 | } |
| 197 | } |
| 198 | |
| 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."); |
| 206 | BT_PUT(packet); |
| 207 | goto end; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | BT_LOGD("Created packet object: addr=%p", packet); |
| 212 | |
| 213 | end: |
| 214 | return packet; |
| 215 | } |