4 * Babeltrace Copy Trace Structure
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
8 * Author: Julien Desfossez <jdesfossez@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT-COPY"
32 #include <babeltrace/assert-internal.h>
33 #include <babeltrace/babeltrace.h>
35 #include <ctfcopytrace.h>
39 const struct bt_packet
*lookup_packet(struct trimmer_iterator
*trim_it
,
40 const struct bt_packet
*packet
)
42 return (const struct bt_packet
*) g_hash_table_lookup(
48 const struct bt_packet
*insert_new_packet(struct trimmer_iterator
*trim_it
,
49 const struct bt_packet
*packet
,
50 const struct bt_stream
*stream
)
52 const struct bt_packet
*writer_packet
= NULL
;
55 BT_LOGD_STR("Inserting a new packet.");
56 writer_packet
= bt_packet_create(stream
);
58 BT_LOGE_STR("Failed to create a new packet.");
62 ret
= ctf_packet_copy_header(trim_it
->err
, packet
, writer_packet
);
64 BT_LOGE_STR("Failed to copy packet header.");
68 g_hash_table_insert(trim_it
->packet_map
, (gpointer
) packet
,
73 BT_PACKET_PUT_REF_AND_RESET(writer_packet
);
79 enum bt_component_status
update_packet_context_field(FILE *err
,
80 const struct bt_packet
*writer_packet
,
81 const char *name
, int64_t value
)
83 enum bt_component_status ret
;
84 const struct bt_field
*packet_context
= NULL
, *writer_packet_context
= NULL
;
85 const struct bt_field_class
*struct_class
= NULL
, *field_class
= NULL
;
86 const struct bt_field
*field
= NULL
, *writer_field
= NULL
;
87 int nr_fields
, i
, int_ret
;
89 BT_LOGD("Updating packet context field: name=%s", name
);
90 packet_context
= bt_packet_get_context(writer_packet
);
91 BT_ASSERT(packet_context
);
93 struct_class
= bt_field_get_class(packet_context
);
94 BT_ASSERT(struct_class
);
96 writer_packet_context
= bt_packet_get_context(writer_packet
);
97 BT_ASSERT(writer_packet_context
);
99 nr_fields
= bt_field_class_structure_get_field_count(struct_class
);
100 for (i
= 0; i
< nr_fields
; i
++) {
101 const char *field_name
;
103 field
= bt_field_structure_get_field_by_index(
106 BT_LOGE("Failed to get field in packet-context: field-name=\"%s\"",
110 if (bt_field_class_structure_get_field_by_index(struct_class
,
111 &field_name
, &field_class
, i
) < 0) {
112 BT_LOGE("Failed to get field: field-name=\"%s\"",
116 if (strcmp(field_name
, name
)) {
117 BT_OBJECT_PUT_REF_AND_RESET(field_class
);
118 BT_OBJECT_PUT_REF_AND_RESET(field
);
121 if (bt_field_class_id(field_class
) !=
122 BT_FIELD_CLASS_TYPE_INTEGER
) {
123 BT_LOGE("Expecting an integer for this field: field-name=\"%s\"",
128 writer_field
= bt_field_structure_get_field_by_name(writer_packet_context
,
130 BT_ASSERT(writer_field
);
132 int_ret
= bt_field_unsigned_integer_set_value(writer_field
, value
);
133 BT_ASSERT(int_ret
== 0);
135 BT_OBJECT_PUT_REF_AND_RESET(writer_field
);
136 BT_OBJECT_PUT_REF_AND_RESET(field_class
);
137 BT_OBJECT_PUT_REF_AND_RESET(field
);
140 ret
= BT_COMPONENT_STATUS_OK
;
144 bt_object_put_ref(writer_field
);
145 bt_object_put_ref(field_class
);
146 bt_object_put_ref(field
);
147 ret
= BT_COMPONENT_STATUS_ERROR
;
149 bt_object_put_ref(struct_class
);
150 bt_object_put_ref(packet_context
);
155 const struct bt_packet
*trimmer_new_packet(
156 struct trimmer_iterator
*trim_it
,
157 const struct bt_packet
*packet
)
159 const struct bt_stream
*stream
= NULL
;
160 const struct bt_packet
*writer_packet
= NULL
;
163 stream
= bt_packet_get_stream(packet
);
167 * If a packet was already opened, close it and remove it from
170 writer_packet
= lookup_packet(trim_it
, packet
);
172 g_hash_table_remove(trim_it
->packet_map
, packet
);
173 BT_PACKET_PUT_REF_AND_RESET(writer_packet
);
176 writer_packet
= insert_new_packet(trim_it
, packet
, stream
);
177 if (!writer_packet
) {
178 BT_LOGE_STR("Failed to insert new packet.");
181 bt_packet_get_ref(writer_packet
);
183 int_ret
= ctf_packet_copy_context(trim_it
->err
, packet
,
184 stream
, writer_packet
);
186 BT_LOGE_STR("Failed to copy packet context.");
193 BT_PACKET_PUT_REF_AND_RESET(writer_packet
);
195 bt_stream_put_ref(stream
);
196 return writer_packet
;
200 const struct bt_packet
*trimmer_close_packet(
201 struct trimmer_iterator
*trim_it
,
202 const struct bt_packet
*packet
)
204 const struct bt_packet
*writer_packet
= NULL
;
206 writer_packet
= lookup_packet(trim_it
, packet
);
207 if (!writer_packet
) {
208 BT_LOGE_STR("Failed to find existing packet.");
212 g_hash_table_remove(trim_it
->packet_map
, packet
);
215 return writer_packet
;
219 const struct bt_event
*trimmer_output_event(
220 struct trimmer_iterator
*trim_it
,
221 const struct bt_event
*event
)
223 const struct bt_event_class
*event_class
= NULL
;
224 const struct bt_event
*writer_event
= NULL
;
225 const struct bt_packet
*packet
= NULL
, *writer_packet
= NULL
;
226 const char *event_name
;
229 event_class
= bt_event_get_class(event
);
230 BT_ASSERT(event_class
);
232 event_name
= bt_event_class_get_name(event_class
);
234 writer_event
= ctf_copy_event(trim_it
->err
, event
, event_class
, false);
236 BT_LOGE("Failed to copy event: event-class-name=\"%s\", event-name=\"%s\"",
237 bt_event_class_get_name(event_class
),
242 packet
= bt_event_get_packet(event
);
245 writer_packet
= lookup_packet(trim_it
, packet
);
246 if (!writer_packet
) {
247 BT_LOGE_STR("Failed to find existing packet.");
250 bt_packet_get_ref(writer_packet
);
252 int_ret
= bt_event_set_packet(writer_event
, writer_packet
);
254 BT_LOGE("Failed to append event: event-class-name=\"%s\", event-name=\"%s\"",
255 bt_event_class_get_name(event_class
),
260 /* We keep the reference on the writer_event to create a notification. */
264 BT_OBJECT_PUT_REF_AND_RESET(writer_event
);
266 bt_packet_put_ref(writer_packet
);
267 bt_packet_put_ref(packet
);
268 bt_event_class_put_ref(event_class
);