lib: make trace IR API const-correct
[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
40f4ba76
PP
39const struct bt_packet *lookup_packet(struct trimmer_iterator *trim_it,
40 const struct bt_packet *packet)
19ce87a4 41{
40f4ba76 42 return (const struct bt_packet *) g_hash_table_lookup(
19ce87a4
JD
43 trim_it->packet_map,
44 (gpointer) packet);
45}
46
47static
40f4ba76
PP
48const struct bt_packet *insert_new_packet(struct trimmer_iterator *trim_it,
49 const struct bt_packet *packet,
50 const struct bt_stream *stream)
19ce87a4 51{
40f4ba76 52 const 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:
65300d60 73 BT_OBJECT_PUT_REF_AND_RESET(writer_packet);
19ce87a4
JD
74end:
75 return writer_packet;
76}
77
78BT_HIDDEN
79enum bt_component_status update_packet_context_field(FILE *err,
40f4ba76 80 const struct bt_packet *writer_packet,
19ce87a4
JD
81 const char *name, int64_t value)
82{
83 enum bt_component_status ret;
40f4ba76
PP
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;
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
5cd6d0e5
PP
93 struct_class = bt_field_get_class(packet_context);
94 BT_ASSERT(struct_class);
19ce87a4 95
50842bdc 96 writer_packet_context = bt_packet_get_context(writer_packet);
f6ccaed9 97 BT_ASSERT(writer_packet_context);
19ce87a4 98
5cd6d0e5 99 nr_fields = bt_field_class_structure_get_field_count(struct_class);
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 }
5cd6d0e5
PP
110 if (bt_field_class_structure_get_field_by_index(struct_class,
111 &field_name, &field_class, 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)) {
65300d60
PP
117 BT_OBJECT_PUT_REF_AND_RESET(field_class);
118 BT_OBJECT_PUT_REF_AND_RESET(field);
19ce87a4
JD
119 continue;
120 }
5cd6d0e5 121 if (bt_field_class_id(field_class) !=
864cad70 122 BT_FIELD_CLASS_TYPE_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
65300d60
PP
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);
19ce87a4
JD
138 }
139
140 ret = BT_COMPONENT_STATUS_OK;
9ae49d3d 141 goto end;
19ce87a4 142
9ae49d3d 143error:
65300d60
PP
144 bt_object_put_ref(writer_field);
145 bt_object_put_ref(field_class);
146 bt_object_put_ref(field);
9ae49d3d
JD
147 ret = BT_COMPONENT_STATUS_ERROR;
148end:
65300d60
PP
149 bt_object_put_ref(struct_class);
150 bt_object_put_ref(packet_context);
19ce87a4
JD
151 return ret;
152}
153
154BT_HIDDEN
40f4ba76 155const struct bt_packet *trimmer_new_packet(
19ce87a4 156 struct trimmer_iterator *trim_it,
40f4ba76 157 const struct bt_packet *packet)
19ce87a4 158{
40f4ba76
PP
159 const struct bt_stream *stream = NULL;
160 const 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);
65300d60 173 BT_OBJECT_PUT_REF_AND_RESET(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 180 }
65300d60 181 bt_object_get_ref(writer_packet);
19ce87a4 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:
65300d60 193 BT_OBJECT_PUT_REF_AND_RESET(writer_packet);
9ae49d3d 194end:
65300d60 195 bt_object_put_ref(stream);
19ce87a4
JD
196 return writer_packet;
197}
198
199BT_HIDDEN
40f4ba76 200const struct bt_packet *trimmer_close_packet(
19ce87a4 201 struct trimmer_iterator *trim_it,
40f4ba76 202 const struct bt_packet *packet)
19ce87a4 203{
40f4ba76 204 const 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
40f4ba76 219const struct bt_event *trimmer_output_event(
19ce87a4 220 struct trimmer_iterator *trim_it,
40f4ba76 221 const struct bt_event *event)
19ce87a4 222{
40f4ba76
PP
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;
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 249 }
65300d60 250 bt_object_get_ref(writer_packet);
19ce87a4 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 263error:
65300d60 264 BT_OBJECT_PUT_REF_AND_RESET(writer_event);
9ae49d3d 265end:
65300d60
PP
266 bt_object_put_ref(writer_packet);
267 bt_object_put_ref(packet);
268 bt_object_put_ref(event_class);
19ce87a4
JD
269 return writer_event;
270}
This page took 0.044846 seconds and 4 git commands to generate.