Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / plugins / utils / trimmer / copy.c
CommitLineData
19ce87a4
JD
1/*
2 * copy.c
3 *
4 * Babeltrace Copy Trace Structure
5 *
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
7 *
8 * Author: Julien Desfossez <jdesfossez@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
c59fc0d5
JD
29#define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT-COPY"
30#include "logging.h"
31
f6ccaed9 32#include <babeltrace/assert-internal.h>
9d408fca 33#include <babeltrace/babeltrace.h>
19ce87a4
JD
34
35#include <ctfcopytrace.h>
36#include "iterator.h"
37
38static
50842bdc
PP
39struct bt_packet *lookup_packet(struct trimmer_iterator *trim_it,
40 struct bt_packet *packet)
19ce87a4 41{
50842bdc 42 return (struct bt_packet *) g_hash_table_lookup(
19ce87a4
JD
43 trim_it->packet_map,
44 (gpointer) packet);
45}
46
47static
50842bdc
PP
48struct bt_packet *insert_new_packet(struct trimmer_iterator *trim_it,
49 struct bt_packet *packet,
50 struct bt_stream *stream)
19ce87a4 51{
50842bdc 52 struct bt_packet *writer_packet = NULL;
387483fc 53 int ret;
19ce87a4 54
c59fc0d5 55 BT_LOGD_STR("Inserting a new packet.");
50842bdc 56 writer_packet = bt_packet_create(stream);
19ce87a4 57 if (!writer_packet) {
c59fc0d5 58 BT_LOGE_STR("Failed to create a new packet.");
387483fc 59 goto error;
19ce87a4 60 }
19ce87a4 61
387483fc
JD
62 ret = ctf_packet_copy_header(trim_it->err, packet, writer_packet);
63 if (ret) {
c59fc0d5 64 BT_LOGE_STR("Failed to copy packet header.");
387483fc
JD
65 goto error;
66 }
67
68 g_hash_table_insert(trim_it->packet_map, (gpointer) packet,
69 writer_packet);
70 goto end;
71
72error:
73 BT_PUT(writer_packet);
19ce87a4
JD
74end:
75 return writer_packet;
76}
77
78BT_HIDDEN
79enum bt_component_status update_packet_context_field(FILE *err,
50842bdc 80 struct bt_packet *writer_packet,
19ce87a4
JD
81 const char *name, int64_t value)
82{
83 enum bt_component_status ret;
50842bdc
PP
84 struct bt_field *packet_context = NULL, *writer_packet_context = NULL;
85 struct bt_field_type *struct_type = NULL, *field_type = NULL;
86 struct bt_field *field = NULL, *writer_field = NULL;
19ce87a4
JD
87 int nr_fields, i, int_ret;
88
c59fc0d5 89 BT_LOGD("Updating packet context field: name=%s", name);
50842bdc 90 packet_context = bt_packet_get_context(writer_packet);
f6ccaed9 91 BT_ASSERT(packet_context);
19ce87a4 92
50842bdc 93 struct_type = bt_field_get_type(packet_context);
f6ccaed9 94 BT_ASSERT(struct_type);
19ce87a4 95
50842bdc 96 writer_packet_context = bt_packet_get_context(writer_packet);
f6ccaed9 97 BT_ASSERT(writer_packet_context);
19ce87a4 98
50842bdc 99 nr_fields = bt_field_type_structure_get_field_count(struct_type);
19ce87a4 100 for (i = 0; i < nr_fields; i++) {
19ce87a4
JD
101 const char *field_name;
102
50842bdc 103 field = bt_field_structure_get_field_by_index(
19ce87a4
JD
104 packet_context, i);
105 if (!field) {
c59fc0d5
JD
106 BT_LOGE("Failed to get field in packet-context: field-name=\"%s\"",
107 name);
9ae49d3d 108 goto error;
19ce87a4 109 }
50842bdc 110 if (bt_field_type_structure_get_field_by_index(struct_type,
19ce87a4 111 &field_name, &field_type, i) < 0) {
c59fc0d5
JD
112 BT_LOGE("Failed to get field: field-name=\"%s\"",
113 field_name);
9ae49d3d 114 goto error;
19ce87a4
JD
115 }
116 if (strcmp(field_name, name)) {
9ae49d3d
JD
117 BT_PUT(field_type);
118 BT_PUT(field);
19ce87a4
JD
119 continue;
120 }
50842bdc
PP
121 if (bt_field_type_get_type_id(field_type) !=
122 BT_FIELD_TYPE_ID_INTEGER) {
c59fc0d5
JD
123 BT_LOGE("Expecting an integer for this field: field-name=\"%s\"",
124 name);
9ae49d3d 125 goto error;
19ce87a4 126 }
c59fc0d5 127
50842bdc 128 writer_field = bt_field_structure_get_field_by_name(writer_packet_context,
19ce87a4 129 field_name);
f6ccaed9 130 BT_ASSERT(writer_field);
19ce87a4 131
50842bdc 132 int_ret = bt_field_unsigned_integer_set_value(writer_field, value);
f6ccaed9 133 BT_ASSERT(int_ret == 0);
c59fc0d5 134
9ae49d3d
JD
135 BT_PUT(writer_field);
136 BT_PUT(field_type);
137 BT_PUT(field);
19ce87a4
JD
138 }
139
140 ret = BT_COMPONENT_STATUS_OK;
9ae49d3d 141 goto end;
19ce87a4 142
9ae49d3d
JD
143error:
144 bt_put(writer_field);
145 bt_put(field_type);
146 bt_put(field);
147 ret = BT_COMPONENT_STATUS_ERROR;
148end:
19ce87a4 149 bt_put(struct_type);
19ce87a4 150 bt_put(packet_context);
19ce87a4
JD
151 return ret;
152}
153
154BT_HIDDEN
50842bdc 155struct bt_packet *trimmer_new_packet(
19ce87a4 156 struct trimmer_iterator *trim_it,
50842bdc 157 struct bt_packet *packet)
19ce87a4 158{
50842bdc
PP
159 struct bt_stream *stream = NULL;
160 struct bt_packet *writer_packet = NULL;
19ce87a4
JD
161 int int_ret;
162
50842bdc 163 stream = bt_packet_get_stream(packet);
f6ccaed9 164 BT_ASSERT(stream);
19ce87a4
JD
165
166 /*
167 * If a packet was already opened, close it and remove it from
168 * the HT.
169 */
170 writer_packet = lookup_packet(trim_it, packet);
171 if (writer_packet) {
172 g_hash_table_remove(trim_it->packet_map, packet);
9ae49d3d 173 BT_PUT(writer_packet);
19ce87a4
JD
174 }
175
176 writer_packet = insert_new_packet(trim_it, packet, stream);
177 if (!writer_packet) {
c59fc0d5 178 BT_LOGE_STR("Failed to insert new packet.");
9ae49d3d 179 goto error;
19ce87a4
JD
180 }
181 bt_get(writer_packet);
182
9877e1aa
JD
183 int_ret = ctf_packet_copy_context(trim_it->err, packet,
184 stream, writer_packet);
185 if (int_ret < 0) {
c59fc0d5 186 BT_LOGE_STR("Failed to copy packet context.");
9ae49d3d 187 goto error;
19ce87a4
JD
188 }
189
9ae49d3d
JD
190 goto end;
191
192error:
193 BT_PUT(writer_packet);
194end:
19ce87a4 195 bt_put(stream);
19ce87a4
JD
196 return writer_packet;
197}
198
199BT_HIDDEN
50842bdc 200struct bt_packet *trimmer_close_packet(
19ce87a4 201 struct trimmer_iterator *trim_it,
50842bdc 202 struct bt_packet *packet)
19ce87a4 203{
50842bdc 204 struct bt_packet *writer_packet = NULL;
19ce87a4
JD
205
206 writer_packet = lookup_packet(trim_it, packet);
207 if (!writer_packet) {
c59fc0d5 208 BT_LOGE_STR("Failed to find existing packet.");
19ce87a4
JD
209 goto end;
210 }
211
212 g_hash_table_remove(trim_it->packet_map, packet);
213
214end:
215 return writer_packet;
216}
217
218BT_HIDDEN
50842bdc 219struct bt_event *trimmer_output_event(
19ce87a4 220 struct trimmer_iterator *trim_it,
50842bdc 221 struct bt_event *event)
19ce87a4 222{
50842bdc
PP
223 struct bt_event_class *event_class = NULL;
224 struct bt_event *writer_event = NULL;
225 struct bt_packet *packet = NULL, *writer_packet = NULL;
19ce87a4
JD
226 const char *event_name;
227 int int_ret;
228
50842bdc 229 event_class = bt_event_get_class(event);
f6ccaed9 230 BT_ASSERT(event_class);
19ce87a4 231
50842bdc 232 event_name = bt_event_class_get_name(event_class);
19ce87a4
JD
233
234 writer_event = ctf_copy_event(trim_it->err, event, event_class, false);
235 if (!writer_event) {
c59fc0d5 236 BT_LOGE("Failed to copy event: event-class-name=\"%s\", event-name=\"%s\"",
50842bdc 237 bt_event_class_get_name(event_class),
c59fc0d5 238 event_name);
9ae49d3d 239 goto error;
19ce87a4
JD
240 }
241
50842bdc 242 packet = bt_event_get_packet(event);
f6ccaed9 243 BT_ASSERT(packet);
19ce87a4
JD
244
245 writer_packet = lookup_packet(trim_it, packet);
246 if (!writer_packet) {
c59fc0d5 247 BT_LOGE_STR("Failed to find existing packet.");
9ae49d3d 248 goto error;
19ce87a4
JD
249 }
250 bt_get(writer_packet);
251
50842bdc 252 int_ret = bt_event_set_packet(writer_event, writer_packet);
19ce87a4 253 if (int_ret < 0) {
c59fc0d5 254 BT_LOGE("Failed to append event: event-class-name=\"%s\", event-name=\"%s\"",
50842bdc 255 bt_event_class_get_name(event_class),
c59fc0d5 256 event_name);
9ae49d3d 257 goto error;
19ce87a4
JD
258 }
259
9ae49d3d
JD
260 /* We keep the reference on the writer_event to create a notification. */
261 goto end;
19ce87a4 262
9ae49d3d
JD
263error:
264 BT_PUT(writer_event);
265end:
19ce87a4 266 bt_put(writer_packet);
19ce87a4 267 bt_put(packet);
19ce87a4 268 bt_put(event_class);
19ce87a4
JD
269 return writer_event;
270}
This page took 0.045036 seconds and 4 git commands to generate.