Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * stream.c | |
3 | * | |
d2dc44b6 | 4 | * Babeltrace CTF IR - Stream |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@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 | ||
19abc2c6 PP |
29 | #define BT_LOG_TAG "STREAM" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
ac0c6bdd PP |
32 | #include <babeltrace/ctf-ir/clock-class.h> |
33 | #include <babeltrace/ctf-writer/clock.h> | |
34 | #include <babeltrace/ctf-writer/clock-internal.h> | |
273b65be | 35 | #include <babeltrace/ctf-writer/event.h> |
adc315b8 | 36 | #include <babeltrace/ctf-ir/event-internal.h> |
2e33ac5a PP |
37 | #include <babeltrace/ctf-ir/field-types-internal.h> |
38 | #include <babeltrace/ctf-ir/fields-internal.h> | |
3f043b05 JG |
39 | #include <babeltrace/ctf-ir/stream.h> |
40 | #include <babeltrace/ctf-ir/stream-internal.h> | |
adc315b8 | 41 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
5acf2ae6 | 42 | #include <babeltrace/ctf-ir/trace.h> |
319fd969 PP |
43 | #include <babeltrace/ctf-ir/trace-internal.h> |
44 | #include <babeltrace/ctf-writer/writer-internal.h> | |
3230ee6b | 45 | #include <babeltrace/graph/component-internal.h> |
83509119 | 46 | #include <babeltrace/ref.h> |
273b65be | 47 | #include <babeltrace/ctf-writer/functor-internal.h> |
3d9990ac PP |
48 | #include <babeltrace/compiler-internal.h> |
49 | #include <babeltrace/align-internal.h> | |
dc3fffef | 50 | #include <inttypes.h> |
95076212 | 51 | #include <unistd.h> |
273b65be JG |
52 | |
53 | static | |
83509119 | 54 | void bt_ctf_stream_destroy(struct bt_object *obj); |
273b65be | 55 | static |
af9296f3 | 56 | int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
273b65be | 57 | |
ac0c6bdd PP |
58 | static |
59 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) | |
60 | { | |
61 | int ret = 0; | |
62 | struct bt_ctf_field_type *field_type = NULL; | |
63 | ||
64 | if (!field) { | |
19abc2c6 | 65 | BT_LOGW_STR("Invalid parameter: field is NULL."); |
ac0c6bdd PP |
66 | ret = -1; |
67 | goto end; | |
68 | } | |
69 | ||
70 | field_type = bt_ctf_field_get_type(field); | |
71 | assert(field_type); | |
72 | ||
73 | if (bt_ctf_field_type_get_type_id(field_type) != | |
1487a16a | 74 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
ac0c6bdd | 75 | /* Not an integer and the value is unset, error. */ |
19abc2c6 PP |
76 | BT_LOGW("Invalid parameter: field's type is not an integer field type: " |
77 | "field-addr=%p, ft-addr=%p, ft-id=%s", | |
78 | field, field_type, | |
79 | bt_ctf_field_type_id_string(field_type->id)); | |
ac0c6bdd PP |
80 | ret = -1; |
81 | goto end; | |
82 | } | |
83 | ||
84 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
85 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); | |
86 | if (ret) { | |
87 | /* Value is out of range, error. */ | |
19abc2c6 PP |
88 | BT_LOGW("Cannot set signed integer field's value: " |
89 | "addr=%p, value=%" PRId64, | |
90 | field, (int64_t) value); | |
ac0c6bdd PP |
91 | goto end; |
92 | } | |
93 | } else { | |
94 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); | |
95 | if (ret) { | |
96 | /* Value is out of range, error. */ | |
19abc2c6 PP |
97 | BT_LOGW("Cannot set unsigned integer field's value: " |
98 | "addr=%p, value=%" PRIu64, | |
99 | field, value); | |
ac0c6bdd PP |
100 | goto end; |
101 | } | |
102 | } | |
103 | end: | |
104 | bt_put(field_type); | |
105 | return ret; | |
106 | } | |
107 | ||
d246b111 JG |
108 | static |
109 | int set_packet_header_magic(struct bt_ctf_stream *stream) | |
110 | { | |
111 | int ret = 0; | |
d246b111 JG |
112 | struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field( |
113 | stream->packet_header, "magic"); | |
19abc2c6 PP |
114 | const uint32_t magic_value = 0xc1fc1fc1; |
115 | ||
116 | assert(stream); | |
d246b111 JG |
117 | |
118 | if (!magic_field) { | |
119 | /* No magic field found. Not an error, skip. */ | |
19abc2c6 PP |
120 | BT_LOGV("No field named `magic` in packet header: skipping: " |
121 | "stream-addr=%p, stream-name=\"%s\"", | |
122 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
123 | goto end; |
124 | } | |
125 | ||
b3376dd9 PP |
126 | ret = bt_ctf_field_unsigned_integer_set_value(magic_field, |
127 | (uint64_t) magic_value); | |
d246b111 | 128 | |
d246b111 | 129 | if (ret) { |
b3376dd9 PP |
130 | BT_LOGW("Cannot set packet header field's `magic` integer field's value: " |
131 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
19abc2c6 PP |
132 | stream, bt_ctf_stream_get_name(stream), |
133 | magic_field, (uint64_t) magic_value); | |
134 | } else { | |
135 | BT_LOGV("Set packet header field's `magic` field's value: " | |
b3376dd9 | 136 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
19abc2c6 PP |
137 | stream, bt_ctf_stream_get_name(stream), |
138 | magic_field, (uint64_t) magic_value); | |
d246b111 JG |
139 | } |
140 | end: | |
83509119 | 141 | bt_put(magic_field); |
d246b111 JG |
142 | return ret; |
143 | } | |
144 | ||
145 | static | |
146 | int set_packet_header_uuid(struct bt_ctf_stream *stream) | |
147 | { | |
19abc2c6 PP |
148 | int ret = 0; |
149 | int64_t i; | |
e6a8e8e4 | 150 | struct bt_ctf_trace *trace = NULL; |
d246b111 JG |
151 | struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field( |
152 | stream->packet_header, "uuid"); | |
153 | ||
19abc2c6 PP |
154 | assert(stream); |
155 | ||
d246b111 JG |
156 | if (!uuid_field) { |
157 | /* No uuid field found. Not an error, skip. */ | |
19abc2c6 PP |
158 | BT_LOGV("No field named `uuid` in packet header: skipping: " |
159 | "stream-addr=%p, stream-name=\"%s\"", | |
160 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
161 | goto end; |
162 | } | |
163 | ||
e6a8e8e4 | 164 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
d246b111 JG |
165 | for (i = 0; i < 16; i++) { |
166 | struct bt_ctf_field *uuid_element = | |
167 | bt_ctf_field_array_get_field(uuid_field, i); | |
168 | ||
b3376dd9 PP |
169 | ret = bt_ctf_field_unsigned_integer_set_value( |
170 | uuid_element, (uint64_t) trace->uuid[i]); | |
83509119 | 171 | bt_put(uuid_element); |
d246b111 | 172 | if (ret) { |
19abc2c6 PP |
173 | BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): " |
174 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, " | |
b3376dd9 | 175 | "value=%" PRIu64 ", index=%" PRId64, |
19abc2c6 PP |
176 | stream, bt_ctf_stream_get_name(stream), |
177 | uuid_element, (uint64_t) trace->uuid[i], i); | |
d246b111 JG |
178 | goto end; |
179 | } | |
180 | } | |
181 | ||
19abc2c6 PP |
182 | BT_LOGV("Set packet header field's `uuid` field's value: " |
183 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p", | |
184 | stream, bt_ctf_stream_get_name(stream), uuid_field); | |
185 | ||
d246b111 | 186 | end: |
83509119 | 187 | bt_put(uuid_field); |
e6a8e8e4 | 188 | BT_PUT(trace); |
d246b111 JG |
189 | return ret; |
190 | } | |
191 | static | |
192 | int set_packet_header_stream_id(struct bt_ctf_stream *stream) | |
193 | { | |
194 | int ret = 0; | |
195 | uint32_t stream_id; | |
d246b111 JG |
196 | struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field( |
197 | stream->packet_header, "stream_id"); | |
198 | ||
199 | if (!stream_id_field) { | |
200 | /* No stream_id field found. Not an error, skip. */ | |
19abc2c6 PP |
201 | BT_LOGV("No field named `stream_id` in packet header: skipping: " |
202 | "stream-addr=%p, stream-name=\"%s\"", | |
203 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
204 | goto end; |
205 | } | |
206 | ||
b3376dd9 PP |
207 | stream_id = stream->stream_class->id; |
208 | ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field, | |
209 | (uint64_t) stream_id); | |
210 | if (ret) { | |
211 | BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: " | |
212 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
213 | stream, bt_ctf_stream_get_name(stream), | |
214 | stream_id_field, (uint64_t) stream_id); | |
215 | } else { | |
216 | BT_LOGV("Set packet header field's `stream_id` field's value: " | |
217 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
218 | stream, bt_ctf_stream_get_name(stream), | |
219 | stream_id_field, (uint64_t) stream_id); | |
220 | } | |
221 | ||
222 | end: | |
223 | bt_put(stream_id_field); | |
224 | return ret; | |
225 | } | |
226 | ||
227 | static | |
228 | int auto_populate_packet_header(struct bt_ctf_stream *stream) | |
229 | { | |
230 | int ret = 0; | |
231 | ||
232 | if (!stream->packet_header) { | |
233 | goto end; | |
234 | } | |
235 | ||
236 | ret = set_packet_header_magic(stream); | |
237 | if (ret) { | |
238 | BT_LOGW("Cannot set packet header's magic number field: " | |
239 | "stream-addr=%p, stream-name=\"%s\"", | |
240 | stream, bt_ctf_stream_get_name(stream)); | |
241 | goto end; | |
242 | } | |
243 | ||
244 | ret = set_packet_header_uuid(stream); | |
245 | if (ret) { | |
246 | BT_LOGW("Cannot set packet header's UUID field: " | |
19abc2c6 PP |
247 | "stream-addr=%p, stream-name=\"%s\"", |
248 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
249 | goto end; |
250 | } | |
251 | ||
b3376dd9 PP |
252 | ret = set_packet_header_stream_id(stream); |
253 | if (ret) { | |
254 | BT_LOGW("Cannot set packet header's stream class ID field: " | |
255 | "stream-addr=%p, stream-name=\"%s\"", | |
256 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
257 | goto end; |
258 | } | |
259 | ||
b3376dd9 PP |
260 | BT_LOGV("Automatically populated stream's packet header's known fields: " |
261 | "stream-addr=%p, stream-name=\"%s\"", | |
262 | stream, bt_ctf_stream_get_name(stream)); | |
263 | ||
264 | end: | |
265 | return ret; | |
266 | } | |
267 | ||
268 | static | |
269 | int set_packet_context_packet_size(struct bt_ctf_stream *stream) | |
270 | { | |
271 | int ret = 0; | |
272 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field( | |
273 | stream->packet_context, "packet_size"); | |
274 | ||
275 | assert(stream); | |
276 | ||
277 | if (!field) { | |
278 | /* No packet size field found. Not an error, skip. */ | |
279 | BT_LOGV("No field named `packet_size` in packet context: skipping: " | |
280 | "stream-addr=%p, stream-name=\"%s\"", | |
281 | stream, bt_ctf_stream_get_name(stream)); | |
282 | goto end; | |
283 | } | |
284 | ||
285 | ret = bt_ctf_field_unsigned_integer_set_value(field, | |
286 | stream->pos.packet_size); | |
d246b111 | 287 | if (ret) { |
b3376dd9 PP |
288 | BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: " |
289 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
290 | stream, bt_ctf_stream_get_name(stream), | |
291 | field, stream->pos.packet_size); | |
d246b111 | 292 | } else { |
b3376dd9 PP |
293 | BT_LOGV("Set packet context field's `packet_size` field's value: " |
294 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
295 | stream, bt_ctf_stream_get_name(stream), | |
296 | field, stream->pos.packet_size); | |
d246b111 | 297 | } |
19abc2c6 | 298 | |
b3376dd9 PP |
299 | end: |
300 | bt_put(field); | |
301 | return ret; | |
302 | } | |
303 | ||
304 | static | |
305 | int set_packet_context_content_size(struct bt_ctf_stream *stream) | |
306 | { | |
307 | int ret = 0; | |
308 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field( | |
309 | stream->packet_context, "content_size"); | |
310 | ||
311 | assert(stream); | |
312 | ||
313 | if (!field) { | |
314 | /* No content size field found. Not an error, skip. */ | |
315 | BT_LOGV("No field named `content_size` in packet context: skipping: " | |
316 | "stream-addr=%p, stream-name=\"%s\"", | |
317 | stream, bt_ctf_stream_get_name(stream)); | |
318 | goto end; | |
319 | } | |
320 | ||
321 | ret = bt_ctf_field_unsigned_integer_set_value(field, | |
322 | stream->pos.offset); | |
19abc2c6 | 323 | if (ret) { |
b3376dd9 PP |
324 | BT_LOGW("Cannot set packet context field's `content_size` integer field's value: " |
325 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64, | |
19abc2c6 | 326 | stream, bt_ctf_stream_get_name(stream), |
b3376dd9 | 327 | field, stream->pos.offset); |
19abc2c6 | 328 | } else { |
b3376dd9 PP |
329 | BT_LOGV("Set packet context field's `content_size` field's value: " |
330 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64, | |
19abc2c6 | 331 | stream, bt_ctf_stream_get_name(stream), |
b3376dd9 | 332 | field, stream->pos.offset); |
19abc2c6 PP |
333 | } |
334 | ||
d246b111 | 335 | end: |
b3376dd9 | 336 | bt_put(field); |
d246b111 JG |
337 | return ret; |
338 | } | |
339 | ||
340 | static | |
b3376dd9 | 341 | int set_packet_context_events_discarded(struct bt_ctf_stream *stream) |
d246b111 | 342 | { |
b3376dd9 PP |
343 | int ret = 0; |
344 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field( | |
345 | stream->packet_context, "events_discarded"); | |
d246b111 | 346 | |
b3376dd9 PP |
347 | assert(stream); |
348 | ||
349 | if (!field) { | |
350 | /* No discarded events count field found. Not an error, skip. */ | |
351 | BT_LOGV("No field named `events_discarded` in packet context: skipping: " | |
352 | "stream-addr=%p, stream-name=\"%s\"", | |
353 | stream, bt_ctf_stream_get_name(stream)); | |
354 | goto end; | |
355 | } | |
356 | ||
59a09b0e PP |
357 | /* |
358 | * If the field is set by the user, make sure that the value is | |
359 | * greater than or equal to the stream's current count of | |
360 | * discarded events. We do not allow wrapping here. If it's | |
361 | * valid, update the stream's current count. | |
362 | */ | |
363 | if (bt_ctf_field_is_set(field)) { | |
364 | uint64_t user_val; | |
365 | ||
366 | ret = bt_ctf_field_unsigned_integer_get_value(field, | |
367 | &user_val); | |
368 | if (ret) { | |
369 | BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: " | |
370 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p", | |
371 | stream, bt_ctf_stream_get_name(stream), field); | |
372 | goto end; | |
373 | } | |
374 | ||
375 | if (user_val < stream->discarded_events) { | |
376 | BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: " | |
377 | "value is lesser than the stream's current discarded events count: " | |
378 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, " | |
379 | "value=%" PRIu64 ", " | |
380 | "stream-discarded-events-count=%" PRIu64, | |
381 | stream, bt_ctf_stream_get_name(stream), field, | |
382 | user_val, stream->discarded_events); | |
383 | goto end; | |
384 | } | |
385 | ||
386 | stream->discarded_events = user_val; | |
b3376dd9 | 387 | } else { |
59a09b0e PP |
388 | ret = bt_ctf_field_unsigned_integer_set_value(field, |
389 | stream->discarded_events); | |
390 | if (ret) { | |
391 | BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: " | |
392 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
393 | stream, bt_ctf_stream_get_name(stream), | |
394 | field, stream->discarded_events); | |
395 | } else { | |
396 | BT_LOGV("Set packet context field's `events_discarded` field's value: " | |
397 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
398 | stream, bt_ctf_stream_get_name(stream), | |
399 | field, stream->discarded_events); | |
400 | } | |
b3376dd9 PP |
401 | } |
402 | ||
403 | end: | |
404 | bt_put(field); | |
405 | return ret; | |
406 | } | |
407 | ||
408 | static | |
409 | int get_event_header_timestamp(struct bt_ctf_stream *stream, | |
410 | struct bt_ctf_field *event_header, uint64_t *timestamp) | |
411 | { | |
412 | int ret = 0; | |
413 | struct bt_ctf_field *timestamp_field = NULL; | |
414 | struct bt_ctf_clock_class *ts_field_mapped_clock_class = NULL; | |
415 | ||
416 | *timestamp = 0; | |
417 | ||
418 | if (!event_header) { | |
419 | BT_LOGV_STR("Event header does not exist."); | |
420 | goto end; | |
421 | } | |
422 | ||
423 | timestamp_field = bt_ctf_field_structure_get_field(event_header, | |
424 | "timestamp"); | |
425 | if (!timestamp_field) { | |
426 | BT_LOGV("Cannot get event header's `timestamp` field: " | |
427 | "event-header-field-addr=%p", event_header); | |
428 | goto end; | |
429 | } | |
430 | ||
431 | if (!bt_ctf_field_type_is_integer(timestamp_field->type)) { | |
432 | BT_LOGV("Event header's `timestamp` field's type is not an integer field type: " | |
433 | "event-header-field-addr=%p", event_header); | |
434 | goto end; | |
435 | } | |
436 | ||
437 | ts_field_mapped_clock_class = | |
438 | bt_ctf_field_type_integer_get_mapped_clock_class( | |
439 | timestamp_field->type); | |
440 | if (!ts_field_mapped_clock_class) { | |
441 | BT_LOGV("Event header's `timestamp` field's type is not mapped to a clock class: " | |
442 | "event-header-field-addr=%p", event_header); | |
443 | goto end; | |
444 | } | |
445 | ||
446 | if (ts_field_mapped_clock_class != | |
447 | stream->stream_class->clock->clock_class) { | |
448 | BT_LOGV("Event header's `timestamp` field's type is not mapped to the stream's clock's class: " | |
449 | "event-header-field-addr=%p", event_header); | |
450 | goto end; | |
451 | } | |
452 | ||
453 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, | |
454 | timestamp); | |
455 | if (ret) { | |
456 | BT_LOGW("Cannot get unsigned integer field's value: " | |
457 | "event-header-field-addr=%p, " | |
458 | "timestamp-field-addr=%p", | |
459 | event_header, timestamp_field); | |
460 | goto end; | |
461 | } | |
462 | ||
463 | end: | |
464 | bt_put(timestamp_field); | |
465 | bt_put(ts_field_mapped_clock_class); | |
466 | return ret; | |
467 | } | |
468 | ||
469 | static | |
470 | int set_packet_context_timestamp_field(struct bt_ctf_stream *stream, | |
471 | const char *field_name, struct bt_ctf_event *event) | |
472 | { | |
473 | int ret = 0; | |
474 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field( | |
475 | stream->packet_context, field_name); | |
476 | struct bt_ctf_clock_class *field_mapped_clock_class = NULL; | |
477 | uint64_t ts; | |
478 | ||
479 | assert(stream); | |
480 | ||
481 | if (!field) { | |
482 | /* No beginning timestamp field found. Not an error, skip. */ | |
483 | BT_LOGV("No field named `%s` in packet context: skipping: " | |
484 | "stream-addr=%p, stream-name=\"%s\"", field_name, | |
485 | stream, bt_ctf_stream_get_name(stream)); | |
486 | goto end; | |
487 | } | |
488 | ||
489 | if (!stream->stream_class->clock) { | |
490 | BT_LOGV("Stream has no clock: skipping: " | |
19abc2c6 PP |
491 | "stream-addr=%p, stream-name=\"%s\"", |
492 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
493 | goto end; |
494 | } | |
495 | ||
b3376dd9 PP |
496 | field_mapped_clock_class = |
497 | bt_ctf_field_type_integer_get_mapped_clock_class(field->type); | |
498 | if (!field_mapped_clock_class) { | |
499 | BT_LOGV("Packet context's `%s` field's type is not mapped to a clock class: skipping: " | |
500 | "stream-addr=%p, stream-name=\"%s\", " | |
501 | "field-addr=%p, ft-addr=%p", field_name, | |
502 | stream, bt_ctf_stream_get_name(stream), | |
503 | field, field->type); | |
504 | goto end; | |
505 | } | |
506 | ||
507 | if (field_mapped_clock_class != | |
508 | stream->stream_class->clock->clock_class) { | |
509 | BT_LOGV("Packet context's `%s` field's type is not mapped to the stream's clock's class: skipping: " | |
510 | "stream-addr=%p, stream-name=\"%s\", " | |
511 | "field-addr=%p, ft-addr=%p, " | |
512 | "ft-mapped-clock-class-addr=%p, " | |
513 | "ft-mapped-clock-class-name=\"%s\", " | |
514 | "stream-clock-class-addr=%p, " | |
515 | "stream-clock-class-name=\"%s\"", | |
516 | field_name, | |
517 | stream, bt_ctf_stream_get_name(stream), | |
518 | field, field->type, | |
519 | field_mapped_clock_class, | |
520 | bt_ctf_clock_class_get_name(field_mapped_clock_class), | |
521 | stream->stream_class->clock->clock_class, | |
522 | bt_ctf_clock_class_get_name( | |
523 | stream->stream_class->clock->clock_class)); | |
524 | goto end; | |
525 | } | |
526 | ||
527 | if (get_event_header_timestamp(stream, event->event_header, &ts)) { | |
528 | BT_LOGW("Cannot get event's timestamp: " | |
529 | "event-header-field-addr=%p", | |
530 | event->event_header); | |
531 | ret = -1; | |
532 | goto end; | |
533 | } | |
534 | ||
535 | ret = bt_ctf_field_unsigned_integer_set_value(field, ts); | |
d246b111 | 536 | if (ret) { |
b3376dd9 PP |
537 | BT_LOGW("Cannot set packet context field's `%s` integer field's value: " |
538 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
539 | field_name, stream, bt_ctf_stream_get_name(stream), | |
540 | field, stream->discarded_events); | |
541 | } else { | |
542 | BT_LOGV("Set packet context field's `%s` field's value: " | |
543 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
544 | field_name, stream, bt_ctf_stream_get_name(stream), | |
545 | field, stream->discarded_events); | |
546 | } | |
547 | ||
548 | end: | |
549 | bt_put(field); | |
550 | bt_put(field_mapped_clock_class); | |
551 | return ret; | |
552 | } | |
553 | ||
554 | static | |
555 | int set_packet_context_timestamp_begin(struct bt_ctf_stream *stream) | |
556 | { | |
557 | int ret = 0; | |
558 | ||
559 | if (stream->events->len == 0) { | |
560 | BT_LOGV("Current packet contains no events: skipping: " | |
19abc2c6 PP |
561 | "stream-addr=%p, stream-name=\"%s\"", |
562 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
563 | goto end; |
564 | } | |
565 | ||
b3376dd9 PP |
566 | ret = set_packet_context_timestamp_field(stream, "timestamp_begin", |
567 | g_ptr_array_index(stream->events, 0)); | |
568 | ||
569 | end: | |
570 | return ret; | |
571 | } | |
572 | ||
573 | static | |
574 | int set_packet_context_timestamp_end(struct bt_ctf_stream *stream) | |
575 | { | |
576 | int ret = 0; | |
577 | ||
578 | if (stream->events->len == 0) { | |
579 | BT_LOGV("Current packet contains no events: skipping: " | |
580 | "stream-addr=%p, stream-name=\"%s\"", | |
581 | stream, bt_ctf_stream_get_name(stream)); | |
582 | goto end; | |
583 | } | |
584 | ||
585 | ret = set_packet_context_timestamp_field(stream, "timestamp_end", | |
586 | g_ptr_array_index(stream->events, stream->events->len - 1)); | |
587 | ||
588 | end: | |
589 | return ret; | |
590 | } | |
591 | ||
592 | static | |
593 | int auto_populate_packet_context(struct bt_ctf_stream *stream) | |
594 | { | |
595 | int ret = 0; | |
596 | ||
597 | if (!stream->packet_context) { | |
598 | goto end; | |
599 | } | |
600 | ||
601 | ret = set_packet_context_packet_size(stream); | |
d246b111 | 602 | if (ret) { |
b3376dd9 PP |
603 | BT_LOGW("Cannot set packet context's packet size field: " |
604 | "stream-addr=%p, stream-name=\"%s\"", | |
605 | stream, bt_ctf_stream_get_name(stream)); | |
606 | goto end; | |
607 | } | |
608 | ||
609 | ret = set_packet_context_content_size(stream); | |
610 | if (ret) { | |
611 | BT_LOGW("Cannot set packet context's content size field: " | |
19abc2c6 PP |
612 | "stream-addr=%p, stream-name=\"%s\"", |
613 | stream, bt_ctf_stream_get_name(stream)); | |
d246b111 JG |
614 | goto end; |
615 | } | |
19abc2c6 | 616 | |
b3376dd9 PP |
617 | ret = set_packet_context_timestamp_begin(stream); |
618 | if (ret) { | |
619 | BT_LOGW("Cannot set packet context's beginning timestamp field: " | |
620 | "stream-addr=%p, stream-name=\"%s\"", | |
621 | stream, bt_ctf_stream_get_name(stream)); | |
622 | goto end; | |
623 | } | |
624 | ||
625 | ret = set_packet_context_timestamp_end(stream); | |
626 | if (ret) { | |
627 | BT_LOGW("Cannot set packet context's end timestamp field: " | |
628 | "stream-addr=%p, stream-name=\"%s\"", | |
629 | stream, bt_ctf_stream_get_name(stream)); | |
630 | goto end; | |
631 | } | |
632 | ||
633 | ret = set_packet_context_events_discarded(stream); | |
634 | if (ret) { | |
635 | BT_LOGW("Cannot set packet context's discarded events count field: " | |
636 | "stream-addr=%p, stream-name=\"%s\"", | |
637 | stream, bt_ctf_stream_get_name(stream)); | |
638 | goto end; | |
639 | } | |
640 | ||
641 | BT_LOGV("Automatically populated stream's packet context's known fields: " | |
19abc2c6 PP |
642 | "stream-addr=%p, stream-name=\"%s\"", |
643 | stream, bt_ctf_stream_get_name(stream)); | |
644 | ||
d246b111 JG |
645 | end: |
646 | return ret; | |
647 | } | |
648 | ||
123fbdec | 649 | static |
e6a8e8e4 | 650 | void release_event(struct bt_ctf_event *event) |
123fbdec | 651 | { |
e6a8e8e4 JG |
652 | if (bt_object_get_ref_count(event)) { |
653 | /* | |
654 | * The event is being orphaned, but it must guarantee the | |
655 | * existence of its event class for the duration of its | |
656 | * lifetime. | |
657 | */ | |
658 | bt_get(event->event_class); | |
659 | BT_PUT(event->base.parent); | |
660 | } else { | |
661 | bt_object_release(event); | |
662 | } | |
123fbdec JG |
663 | } |
664 | ||
319fd969 PP |
665 | static |
666 | int create_stream_file(struct bt_ctf_writer *writer, | |
667 | struct bt_ctf_stream *stream) | |
668 | { | |
669 | int fd; | |
670 | GString *filename = g_string_new(stream->stream_class->name->str); | |
ebd04048 | 671 | char *file_path; |
319fd969 | 672 | |
19abc2c6 PP |
673 | BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, " |
674 | "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"", | |
675 | writer, stream, bt_ctf_stream_get_name(stream), | |
676 | stream->stream_class, stream->stream_class->name->str); | |
677 | ||
319fd969 PP |
678 | if (stream->stream_class->name->len == 0) { |
679 | int64_t ret; | |
680 | ||
681 | ret = bt_ctf_stream_class_get_id(stream->stream_class); | |
682 | if (ret < 0) { | |
19abc2c6 PP |
683 | BT_LOGW("Cannot get stream class's ID: " |
684 | "stream-class-addr=%p, " | |
685 | "stream-class-name=\"%s\"", | |
686 | stream->stream_class, | |
687 | stream->stream_class->name->str); | |
319fd969 | 688 | fd = -1; |
19abc2c6 | 689 | goto end; |
319fd969 PP |
690 | } |
691 | ||
692 | g_string_printf(filename, "stream_%" PRId64, ret); | |
693 | } | |
694 | ||
cfe11c58 | 695 | g_string_append_printf(filename, "_%" PRId64, stream->id); |
ebd04048 MJ |
696 | file_path = g_build_filename(writer->path->str, filename->str, NULL); |
697 | if (file_path == NULL) { | |
698 | fd = -1; | |
699 | goto end; | |
700 | } | |
701 | fd = open(file_path, | |
319fd969 PP |
702 | O_RDWR | O_CREAT | O_TRUNC, |
703 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
ebd04048 | 704 | g_free(file_path); |
19abc2c6 | 705 | if (fd < 0) { |
b3376dd9 | 706 | BT_LOGW("Failed to open stream file for writing: %s: " |
ebd04048 | 707 | "filename=\"%s\", " |
b3376dd9 | 708 | "ret=%d, errno=%d", strerror(errno), |
ebd04048 | 709 | filename->str, fd, errno); |
19abc2c6 PP |
710 | goto end; |
711 | } | |
712 | ||
713 | BT_LOGD("Created stream file for writing: " | |
ebd04048 | 714 | "stream-addr=%p, stream-name=\"%s\", " |
19abc2c6 | 715 | "filename=\"%s\", fd=%d", stream, bt_ctf_stream_get_name(stream), |
ebd04048 | 716 | filename->str, fd); |
19abc2c6 PP |
717 | |
718 | end: | |
319fd969 PP |
719 | g_string_free(filename, TRUE); |
720 | return fd; | |
721 | } | |
722 | ||
723 | static | |
19abc2c6 | 724 | void set_stream_fd(struct bt_ctf_stream *stream, int fd) |
319fd969 | 725 | { |
dc3fffef | 726 | (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR); |
319fd969 | 727 | stream->pos.fd = fd; |
319fd969 PP |
728 | } |
729 | ||
3230ee6b PP |
730 | static |
731 | void component_destroy_listener(struct bt_component *component, void *data) | |
732 | { | |
733 | struct bt_ctf_stream *stream = data; | |
734 | ||
19abc2c6 PP |
735 | BT_LOGD("Component is being destroyed, stream is notified: " |
736 | "comp-addr=%p, stream-addr=%p", component, stream); | |
3230ee6b PP |
737 | g_hash_table_remove(stream->comp_cur_port, component); |
738 | } | |
739 | ||
cfe11c58 PP |
740 | static |
741 | struct bt_ctf_stream *bt_ctf_stream_create_with_id_no_check( | |
b71d7298 | 742 | struct bt_ctf_stream_class *stream_class, |
cfe11c58 | 743 | const char *name, uint64_t id) |
273b65be | 744 | { |
12c8a1a3 | 745 | int ret; |
273b65be | 746 | struct bt_ctf_stream *stream = NULL; |
319fd969 PP |
747 | struct bt_ctf_trace *trace = NULL; |
748 | struct bt_ctf_writer *writer = NULL; | |
273b65be | 749 | |
319fd969 | 750 | if (!stream_class) { |
19abc2c6 | 751 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
b71d7298 | 752 | goto error; |
319fd969 PP |
753 | } |
754 | ||
19abc2c6 | 755 | BT_LOGD("Creating stream object: stream-class-addr=%p, " |
cfe11c58 PP |
756 | "stream-class-name=\"%s\", stream-name=\"%s\", " |
757 | "stream-id=%" PRIu64, | |
19abc2c6 | 758 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
cfe11c58 PP |
759 | name, id); |
760 | trace = bt_ctf_stream_class_borrow_trace(stream_class); | |
319fd969 | 761 | if (!trace) { |
19abc2c6 | 762 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " |
b3376dd9 PP |
763 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
764 | "stream-name=\"%s\"", | |
19abc2c6 PP |
765 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
766 | name); | |
b71d7298 | 767 | goto error; |
273b65be JG |
768 | } |
769 | ||
5acf2ae6 PP |
770 | if (bt_ctf_trace_is_static(trace)) { |
771 | /* | |
772 | * A static trace has the property that all its stream | |
773 | * classes, clock classes, and streams are definitive: | |
774 | * no more can be added, and each object is also frozen. | |
775 | */ | |
19abc2c6 | 776 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: " |
b3376dd9 PP |
777 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
778 | "stream-name=\"%s\", trace-addr=%p", | |
19abc2c6 | 779 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
b3376dd9 | 780 | name, trace); |
5acf2ae6 PP |
781 | goto error; |
782 | } | |
783 | ||
cfe11c58 PP |
784 | if (id != -1ULL) { |
785 | /* | |
786 | * Validate that the given ID is unique amongst all the | |
787 | * existing trace's streams created from the same stream | |
788 | * class. | |
789 | */ | |
790 | size_t i; | |
791 | ||
792 | for (i = 0; i < trace->streams->len; i++) { | |
793 | struct bt_ctf_stream *trace_stream = | |
794 | g_ptr_array_index(trace->streams, i); | |
795 | ||
796 | if (trace_stream->stream_class != stream_class) { | |
797 | continue; | |
798 | } | |
799 | ||
800 | if (trace_stream->id == id) { | |
801 | BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID."); | |
802 | goto error; | |
803 | } | |
804 | } | |
805 | } | |
806 | ||
273b65be JG |
807 | stream = g_new0(struct bt_ctf_stream, 1); |
808 | if (!stream) { | |
19abc2c6 | 809 | BT_LOGE_STR("Failed to allocate one stream."); |
b71d7298 | 810 | goto error; |
273b65be JG |
811 | } |
812 | ||
83509119 | 813 | bt_object_init(stream, bt_ctf_stream_destroy); |
e6a8e8e4 JG |
814 | /* |
815 | * Acquire reference to parent since stream will become publicly | |
816 | * reachable; it needs its parent to remain valid. | |
817 | */ | |
818 | bt_object_set_parent(stream, trace); | |
273b65be | 819 | stream->stream_class = stream_class; |
319fd969 | 820 | stream->pos.fd = -1; |
cfe11c58 | 821 | stream->id = (int64_t) id; |
d246b111 | 822 | |
3230ee6b PP |
823 | stream->destroy_listeners = g_array_new(FALSE, TRUE, |
824 | sizeof(struct bt_ctf_stream_destroy_listener)); | |
825 | if (!stream->destroy_listeners) { | |
19abc2c6 | 826 | BT_LOGE_STR("Failed to allocate a GArray."); |
3230ee6b PP |
827 | goto error; |
828 | } | |
829 | ||
b71d7298 PP |
830 | if (name) { |
831 | stream->name = g_string_new(name); | |
832 | if (!stream->name) { | |
19abc2c6 | 833 | BT_LOGE_STR("Failed to allocate a GString."); |
b71d7298 PP |
834 | goto error; |
835 | } | |
836 | } | |
837 | ||
19abc2c6 PP |
838 | BT_LOGD("Set stream's trace parent: trace-addr=%p", trace); |
839 | ||
319fd969 PP |
840 | if (trace->is_created_by_writer) { |
841 | int fd; | |
cfe11c58 PP |
842 | writer = (struct bt_ctf_writer *) bt_object_get_parent(trace); |
843 | stream->id = (int64_t) stream_class->next_stream_id++; | |
319fd969 | 844 | |
19abc2c6 PP |
845 | BT_LOGD("Stream object belongs to a writer's trace: " |
846 | "writer-addr=%p", writer); | |
319fd969 | 847 | assert(writer); |
b3376dd9 | 848 | |
98edd02c | 849 | if (stream_class->packet_context_type) { |
19abc2c6 PP |
850 | BT_LOGD("Creating stream's packet context field: " |
851 | "ft-addr=%p", stream_class->packet_context_type); | |
98edd02c JG |
852 | stream->packet_context = bt_ctf_field_create( |
853 | stream_class->packet_context_type); | |
854 | if (!stream->packet_context) { | |
19abc2c6 | 855 | BT_LOGW_STR("Cannot create stream's packet context field."); |
98edd02c JG |
856 | goto error; |
857 | } | |
319fd969 | 858 | |
98edd02c | 859 | /* Initialize events_discarded */ |
af9296f3 | 860 | ret = try_set_structure_field_integer( |
19abc2c6 | 861 | stream->packet_context, "events_discarded", 0); |
7ee636df | 862 | if (ret < 0) { |
19abc2c6 PP |
863 | BT_LOGW("Cannot set `events_discarded` field in packet context: " |
864 | "ret=%d, packet-context-field-addr=%p", | |
865 | ret, stream->packet_context); | |
98edd02c JG |
866 | goto error; |
867 | } | |
319fd969 PP |
868 | } |
869 | ||
870 | stream->events = g_ptr_array_new_with_free_func( | |
871 | (GDestroyNotify) release_event); | |
872 | if (!stream->events) { | |
19abc2c6 | 873 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
319fd969 PP |
874 | goto error; |
875 | } | |
876 | ||
b3376dd9 PP |
877 | if (trace->packet_header_type) { |
878 | BT_LOGD("Creating stream's packet header field: " | |
879 | "ft-addr=%p", trace->packet_header_type); | |
880 | stream->packet_header = | |
881 | bt_ctf_field_create(trace->packet_header_type); | |
882 | if (!stream->packet_header) { | |
883 | BT_LOGW_STR("Cannot create stream's packet header field."); | |
884 | goto error; | |
885 | } | |
319fd969 PP |
886 | } |
887 | ||
888 | /* | |
889 | * Attempt to populate the default trace packet header fields | |
890 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
891 | * fields not be found or be of an incompatible type; they will | |
892 | * simply not be populated automatically. The user will have to | |
893 | * make sure to set the trace packet header fields himself | |
894 | * before flushing. | |
895 | */ | |
b3376dd9 | 896 | ret = auto_populate_packet_header(stream); |
319fd969 | 897 | if (ret) { |
b3376dd9 | 898 | BT_LOGW_STR("Cannot automatically populate the stream's packet header."); |
319fd969 PP |
899 | goto error; |
900 | } | |
901 | ||
902 | /* Create file associated with this stream */ | |
903 | fd = create_stream_file(writer, stream); | |
904 | if (fd < 0) { | |
19abc2c6 | 905 | BT_LOGW_STR("Cannot create stream file."); |
319fd969 PP |
906 | goto error; |
907 | } | |
908 | ||
19abc2c6 | 909 | set_stream_fd(stream, fd); |
319fd969 PP |
910 | |
911 | /* Freeze the writer */ | |
19abc2c6 | 912 | BT_LOGD_STR("Freezing stream's CTF writer."); |
319fd969 PP |
913 | bt_ctf_writer_freeze(writer); |
914 | } else { | |
915 | /* Non-writer stream indicated by a negative FD */ | |
19abc2c6 | 916 | set_stream_fd(stream, -1); |
3230ee6b PP |
917 | stream->comp_cur_port = g_hash_table_new(g_direct_hash, |
918 | g_direct_equal); | |
919 | if (!stream->comp_cur_port) { | |
19abc2c6 | 920 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
3230ee6b PP |
921 | goto error; |
922 | } | |
d246b111 | 923 | } |
319fd969 PP |
924 | |
925 | /* Add this stream to the trace's streams */ | |
926 | g_ptr_array_add(trace->streams, stream); | |
19abc2c6 | 927 | BT_LOGD("Created stream object: addr=%p", stream); |
cfe11c58 PP |
928 | goto end; |
929 | ||
83509119 JG |
930 | error: |
931 | BT_PUT(stream); | |
cfe11c58 PP |
932 | |
933 | end: | |
934 | bt_put(writer); | |
935 | return stream; | |
936 | } | |
937 | ||
938 | struct bt_ctf_stream *bt_ctf_stream_create_with_id( | |
939 | struct bt_ctf_stream_class *stream_class, | |
940 | const char *name, uint64_t id_param) | |
941 | { | |
942 | struct bt_ctf_trace *trace; | |
943 | struct bt_ctf_stream *stream = NULL; | |
944 | int64_t id = (int64_t) id_param; | |
945 | ||
946 | if (!stream_class) { | |
947 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
948 | goto end; | |
949 | } | |
950 | ||
951 | if (id < 0) { | |
952 | BT_LOGW("Invalid parameter: invalid stream's ID: " | |
953 | "name=\"%s\", id=%" PRIu64, | |
954 | name, id_param); | |
955 | goto end; | |
956 | } | |
957 | ||
958 | trace = bt_ctf_stream_class_borrow_trace(stream_class); | |
959 | if (!trace) { | |
960 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " | |
961 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
962 | "stream-name=\"%s\", stream-id=%" PRIu64, | |
963 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
964 | name, id_param); | |
965 | goto end; | |
966 | } | |
967 | ||
968 | if (trace->is_created_by_writer) { | |
969 | BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_ctf_stream_create(): " | |
970 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
971 | "stream-name=\"%s\", stream-id=%" PRIu64, | |
972 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
973 | name, id_param); | |
974 | goto end; | |
975 | } | |
976 | ||
977 | stream = bt_ctf_stream_create_with_id_no_check(stream_class, | |
978 | name, id_param); | |
979 | ||
980 | end: | |
83509119 | 981 | return stream; |
273b65be JG |
982 | } |
983 | ||
cfe11c58 PP |
984 | struct bt_ctf_stream *bt_ctf_stream_create( |
985 | struct bt_ctf_stream_class *stream_class, | |
986 | const char *name) | |
987 | { | |
988 | return bt_ctf_stream_create_with_id_no_check(stream_class, | |
989 | name, -1ULL); | |
990 | } | |
991 | ||
3baf0856 JG |
992 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( |
993 | struct bt_ctf_stream *stream) | |
994 | { | |
995 | struct bt_ctf_stream_class *stream_class = NULL; | |
996 | ||
997 | if (!stream) { | |
19abc2c6 | 998 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
3baf0856 JG |
999 | goto end; |
1000 | } | |
1001 | ||
1002 | stream_class = stream->stream_class; | |
83509119 | 1003 | bt_get(stream_class); |
3baf0856 JG |
1004 | end: |
1005 | return stream_class; | |
1006 | } | |
1007 | ||
37949e9e | 1008 | int bt_ctf_stream_get_discarded_events_count( |
a78a2e25 JG |
1009 | struct bt_ctf_stream *stream, uint64_t *count) |
1010 | { | |
37949e9e | 1011 | int ret = 0; |
a78a2e25 | 1012 | |
19abc2c6 PP |
1013 | if (!stream) { |
1014 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
37949e9e | 1015 | ret = -1; |
19abc2c6 PP |
1016 | goto end; |
1017 | } | |
1018 | ||
1019 | if (!count) { | |
1020 | BT_LOGW_STR("Invalid parameter: count is NULL."); | |
37949e9e | 1021 | ret = -1; |
19abc2c6 PP |
1022 | goto end; |
1023 | } | |
1024 | ||
b3376dd9 PP |
1025 | if (stream->pos.fd < 0) { |
1026 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
19abc2c6 PP |
1027 | "stream-addr=%p, stream-name=\"%s\"", |
1028 | stream, bt_ctf_stream_get_name(stream)); | |
37949e9e | 1029 | ret = -1; |
19abc2c6 PP |
1030 | goto end; |
1031 | } | |
1032 | ||
b3376dd9 PP |
1033 | *count = (uint64_t) stream->discarded_events; |
1034 | ||
1035 | end: | |
1036 | return ret; | |
1037 | } | |
1038 | ||
1039 | static | |
1040 | int set_packet_context_events_discarded_field(struct bt_ctf_stream *stream, | |
1041 | uint64_t count) | |
1042 | { | |
1043 | int ret = 0; | |
1044 | struct bt_ctf_field *events_discarded_field = NULL; | |
1045 | ||
1046 | if (!stream->packet_context) { | |
a78a2e25 JG |
1047 | goto end; |
1048 | } | |
1049 | ||
12c8a1a3 JG |
1050 | events_discarded_field = bt_ctf_field_structure_get_field( |
1051 | stream->packet_context, "events_discarded"); | |
1052 | if (!events_discarded_field) { | |
12c8a1a3 JG |
1053 | goto end; |
1054 | } | |
1055 | ||
b3376dd9 PP |
1056 | ret = bt_ctf_field_unsigned_integer_set_value( |
1057 | events_discarded_field, count); | |
1058 | if (ret) { | |
1059 | BT_LOGW("Cannot set packet context's `events_discarded` field: " | |
1060 | "field-addr=%p, value=%" PRIu64, | |
12c8a1a3 | 1061 | events_discarded_field, count); |
b3376dd9 | 1062 | goto end; |
12c8a1a3 | 1063 | } |
b3376dd9 | 1064 | |
a78a2e25 | 1065 | end: |
83509119 | 1066 | bt_put(events_discarded_field); |
a78a2e25 JG |
1067 | return ret; |
1068 | } | |
1069 | ||
273b65be JG |
1070 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
1071 | uint64_t event_count) | |
1072 | { | |
12c8a1a3 | 1073 | int ret; |
12c8a1a3 JG |
1074 | uint64_t new_count; |
1075 | struct bt_ctf_field *events_discarded_field = NULL; | |
12c8a1a3 | 1076 | |
19abc2c6 PP |
1077 | if (!stream) { |
1078 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1079 | goto end; | |
1080 | } | |
1081 | ||
1082 | BT_LOGV("Appending discarded events to stream: " | |
1083 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, | |
1084 | stream, bt_ctf_stream_get_name(stream), event_count); | |
1085 | ||
1086 | if (!stream->packet_context) { | |
1087 | BT_LOGW_STR("Invalid parameter: stream has no packet context field."); | |
1088 | goto end; | |
1089 | } | |
1090 | ||
1091 | if (stream->pos.fd < 0) { | |
1092 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
12c8a1a3 JG |
1093 | goto end; |
1094 | } | |
1095 | ||
12c8a1a3 JG |
1096 | events_discarded_field = bt_ctf_field_structure_get_field( |
1097 | stream->packet_context, "events_discarded"); | |
1098 | if (!events_discarded_field) { | |
b3376dd9 | 1099 | BT_LOGW_STR("No field named `events_discarded` in stream's packet context."); |
12c8a1a3 JG |
1100 | goto end; |
1101 | } | |
1102 | ||
b3376dd9 PP |
1103 | new_count = stream->discarded_events + event_count; |
1104 | if (new_count < stream->discarded_events) { | |
1105 | BT_LOGW("New discarded events count is less than the stream's current discarded events count: " | |
1106 | "cur-count=%" PRIu64 ", new-count=%" PRIu64, | |
1107 | stream->discarded_events, new_count); | |
1108 | goto end; | |
1109 | } | |
1110 | ||
1111 | ret = set_packet_context_events_discarded_field(stream, new_count); | |
1112 | if (ret) { | |
1113 | /* set_packet_context_events_discarded_field() logs errors */ | |
1114 | goto end; | |
12c8a1a3 JG |
1115 | } |
1116 | ||
b3376dd9 | 1117 | stream->discarded_events = new_count; |
19abc2c6 PP |
1118 | BT_LOGV("Appended discarded events to stream: " |
1119 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, | |
1120 | stream, bt_ctf_stream_get_name(stream), event_count); | |
1121 | ||
12c8a1a3 | 1122 | end: |
83509119 | 1123 | bt_put(events_discarded_field); |
273b65be JG |
1124 | } |
1125 | ||
ac0c6bdd PP |
1126 | static int auto_populate_event_header(struct bt_ctf_stream *stream, |
1127 | struct bt_ctf_event *event) | |
1128 | { | |
1129 | int ret = 0; | |
1130 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; | |
1131 | struct bt_ctf_clock_class *mapped_clock_class = NULL; | |
19abc2c6 | 1132 | uint64_t event_class_id; |
ac0c6bdd | 1133 | |
19abc2c6 PP |
1134 | assert(event); |
1135 | ||
06b019a7 JD |
1136 | if (!event->event_header) { |
1137 | goto end; | |
1138 | } | |
1139 | ||
19abc2c6 PP |
1140 | if (event->frozen) { |
1141 | BT_LOGW_STR("Cannot populate event header field: event is frozen."); | |
ac0c6bdd PP |
1142 | ret = -1; |
1143 | goto end; | |
1144 | } | |
1145 | ||
b3376dd9 | 1146 | BT_LOGV("Automatically populating event's header field: " |
19abc2c6 PP |
1147 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
1148 | stream, bt_ctf_stream_get_name(stream), event); | |
1149 | ||
ac0c6bdd | 1150 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); |
19abc2c6 PP |
1151 | event_class_id = (uint64_t) bt_ctf_event_class_get_id(event->event_class); |
1152 | assert(event_class_id >= 0); | |
b3376dd9 | 1153 | if (id_field && bt_ctf_field_type_is_integer(id_field->type)) { |
19abc2c6 | 1154 | ret = set_integer_field_value(id_field, event_class_id); |
ac0c6bdd | 1155 | if (ret) { |
19abc2c6 PP |
1156 | BT_LOGW("Cannot set event header's `id` field's value: " |
1157 | "addr=%p, value=%" PRIu64, id_field, | |
1158 | event_class_id); | |
ac0c6bdd PP |
1159 | goto end; |
1160 | } | |
1161 | } | |
1162 | ||
1163 | /* | |
1164 | * The conditions to automatically set the timestamp are: | |
1165 | * | |
1166 | * 1. The event header field "timestamp" exists and is an | |
1167 | * integer field. | |
1168 | * 2. This stream's class has a registered clock (set with | |
1169 | * bt_ctf_stream_class_set_clock()). | |
1170 | * 3. The event header field "timestamp" has its type mapped to | |
1171 | * a clock class which is also the clock class of this | |
1172 | * stream's class's registered clock. | |
ac0c6bdd PP |
1173 | */ |
1174 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, | |
1175 | "timestamp"); | |
b3376dd9 PP |
1176 | if (timestamp_field && stream->stream_class->clock && |
1177 | bt_ctf_field_type_is_integer(timestamp_field->type)) { | |
ac0c6bdd PP |
1178 | struct bt_ctf_clock_class *stream_class_clock_class = |
1179 | stream->stream_class->clock->clock_class; | |
ac0c6bdd | 1180 | |
ac0c6bdd PP |
1181 | mapped_clock_class = |
1182 | bt_ctf_field_type_integer_get_mapped_clock_class( | |
b3376dd9 | 1183 | timestamp_field->type); |
ac0c6bdd PP |
1184 | if (mapped_clock_class == stream_class_clock_class) { |
1185 | uint64_t timestamp; | |
1186 | ||
1187 | ret = bt_ctf_clock_get_value( | |
1188 | stream->stream_class->clock, | |
1189 | ×tamp); | |
19abc2c6 | 1190 | assert(ret == 0); |
ac0c6bdd PP |
1191 | ret = set_integer_field_value(timestamp_field, |
1192 | timestamp); | |
1193 | if (ret) { | |
19abc2c6 PP |
1194 | BT_LOGW("Cannot set event header's `timestamp` field's value: " |
1195 | "addr=%p, value=%" PRIu64, | |
1196 | timestamp_field, timestamp); | |
ac0c6bdd PP |
1197 | goto end; |
1198 | } | |
1199 | } | |
1200 | } | |
1201 | ||
b3376dd9 | 1202 | BT_LOGV("Automatically populated event's header field: " |
19abc2c6 PP |
1203 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
1204 | stream, bt_ctf_stream_get_name(stream), event); | |
1205 | ||
ac0c6bdd PP |
1206 | end: |
1207 | bt_put(id_field); | |
1208 | bt_put(timestamp_field); | |
1209 | bt_put(mapped_clock_class); | |
1210 | return ret; | |
1211 | } | |
1212 | ||
273b65be JG |
1213 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, |
1214 | struct bt_ctf_event *event) | |
1215 | { | |
1216 | int ret = 0; | |
273b65be | 1217 | |
19abc2c6 PP |
1218 | if (!stream) { |
1219 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1220 | ret = -1; | |
1221 | goto end; | |
1222 | } | |
1223 | ||
1224 | if (!event) { | |
1225 | BT_LOGW_STR("Invalid parameter: event is NULL."); | |
1226 | ret = -1; | |
1227 | goto end; | |
1228 | } | |
1229 | ||
1230 | if (stream->pos.fd < 0) { | |
1231 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
273b65be JG |
1232 | ret = -1; |
1233 | goto end; | |
1234 | } | |
1235 | ||
19abc2c6 PP |
1236 | BT_LOGV("Appending event to stream: " |
1237 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " | |
1238 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
1239 | stream, bt_ctf_stream_get_name(stream), event, | |
1240 | bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event)), | |
1241 | bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event))); | |
1242 | ||
fa29ba83 PP |
1243 | /* |
1244 | * The event is not supposed to have a parent stream at this | |
1245 | * point. The only other way an event can have a parent stream | |
1246 | * is if it was assigned when setting a packet to the event, | |
1247 | * in which case the packet's stream is not a writer stream, | |
1248 | * and thus the user is trying to append an event which belongs | |
1249 | * to another stream. | |
1250 | */ | |
1251 | if (event->base.parent) { | |
1252 | ret = -1; | |
1253 | goto end; | |
1254 | } | |
1255 | ||
e6a8e8e4 | 1256 | bt_object_set_parent(event, stream); |
19abc2c6 | 1257 | BT_LOGV_STR("Automatically populating the header of the event to append."); |
ac0c6bdd | 1258 | ret = auto_populate_event_header(stream, event); |
662e778c | 1259 | if (ret) { |
19abc2c6 | 1260 | /* auto_populate_event_header() reports errors */ |
37f30168 | 1261 | goto error; |
662e778c JG |
1262 | } |
1263 | ||
5fd2e9fd | 1264 | /* Make sure the various scopes of the event are set */ |
19abc2c6 | 1265 | BT_LOGV_STR("Validating event to append."); |
273b65be JG |
1266 | ret = bt_ctf_event_validate(event); |
1267 | if (ret) { | |
37f30168 | 1268 | goto error; |
273b65be JG |
1269 | } |
1270 | ||
0d688c15 | 1271 | /* Save the new event and freeze it */ |
19abc2c6 | 1272 | BT_LOGV_STR("Freezing the event to append."); |
0d688c15 | 1273 | bt_ctf_event_freeze(event); |
273b65be | 1274 | g_ptr_array_add(stream->events, event); |
5fd2e9fd | 1275 | |
e6a8e8e4 JG |
1276 | /* |
1277 | * Event had to hold a reference to its event class as long as it wasn't | |
1278 | * part of the same trace hierarchy. From now on, the event and its | |
1279 | * class share the same lifetime guarantees and the reference is no | |
1280 | * longer needed. | |
1281 | */ | |
19abc2c6 | 1282 | BT_LOGV_STR("Putting the event's class."); |
e6a8e8e4 | 1283 | bt_put(event->event_class); |
19abc2c6 PP |
1284 | BT_LOGV("Appended event to stream: " |
1285 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " | |
1286 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
1287 | stream, bt_ctf_stream_get_name(stream), event, | |
1288 | bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event)), | |
1289 | bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event))); | |
37f30168 | 1290 | |
273b65be | 1291 | end: |
37f30168 PP |
1292 | return ret; |
1293 | ||
1294 | error: | |
1295 | /* | |
1296 | * Orphan the event; we were not successful in associating it to | |
1297 | * a stream. | |
1298 | */ | |
1299 | bt_object_set_parent(event, NULL); | |
1300 | ||
273b65be JG |
1301 | return ret; |
1302 | } | |
1303 | ||
12c8a1a3 JG |
1304 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( |
1305 | struct bt_ctf_stream *stream) | |
1306 | { | |
1307 | struct bt_ctf_field *packet_context = NULL; | |
1308 | ||
19abc2c6 PP |
1309 | if (!stream) { |
1310 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1311 | goto end; | |
1312 | } | |
1313 | ||
1314 | if (stream->pos.fd < 0) { | |
1315 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
1316 | "stream-addr=%p, stream-name=\"%s\"", stream, | |
1317 | bt_ctf_stream_get_name(stream)); | |
12c8a1a3 JG |
1318 | goto end; |
1319 | } | |
1320 | ||
1321 | packet_context = stream->packet_context; | |
12c8a1a3 | 1322 | if (packet_context) { |
83509119 | 1323 | bt_get(packet_context); |
12c8a1a3 | 1324 | } |
34629a55 | 1325 | end: |
12c8a1a3 JG |
1326 | return packet_context; |
1327 | } | |
1328 | ||
1329 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, | |
1330 | struct bt_ctf_field *field) | |
1331 | { | |
1332 | int ret = 0; | |
1333 | struct bt_ctf_field_type *field_type; | |
1334 | ||
19abc2c6 PP |
1335 | if (!stream) { |
1336 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1337 | ret = -1; | |
1338 | goto end; | |
1339 | } | |
1340 | ||
1341 | if (stream->pos.fd < 0) { | |
1342 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
12c8a1a3 JG |
1343 | ret = -1; |
1344 | goto end; | |
1345 | } | |
1346 | ||
1347 | field_type = bt_ctf_field_get_type(field); | |
09840de5 PP |
1348 | if (bt_ctf_field_type_compare(field_type, |
1349 | stream->stream_class->packet_context_type)) { | |
19abc2c6 PP |
1350 | BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: " |
1351 | "stream-addr=%p, stream-name=\"%s\", " | |
1352 | "packet-context-field-addr=%p, " | |
1353 | "packet-context-ft-addr=%p", | |
1354 | stream, bt_ctf_stream_get_name(stream), | |
1355 | field, field_type); | |
12c8a1a3 JG |
1356 | ret = -1; |
1357 | goto end; | |
1358 | } | |
1359 | ||
83509119 | 1360 | bt_put(field_type); |
83509119 | 1361 | bt_put(stream->packet_context); |
835b2d10 | 1362 | stream->packet_context = bt_get(field); |
19abc2c6 PP |
1363 | BT_LOGV("Set stream's packet context field: " |
1364 | "stream-addr=%p, stream-name=\"%s\", " | |
1365 | "packet-context-field-addr=%p", | |
1366 | stream, bt_ctf_stream_get_name(stream), field); | |
12c8a1a3 JG |
1367 | end: |
1368 | return ret; | |
1369 | } | |
1370 | ||
263a7df5 JG |
1371 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
1372 | struct bt_ctf_stream *stream) | |
1373 | { | |
1374 | struct bt_ctf_field *packet_header = NULL; | |
1375 | ||
19abc2c6 PP |
1376 | if (!stream) { |
1377 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1378 | goto end; | |
1379 | } | |
1380 | ||
1381 | if (stream->pos.fd < 0) { | |
1382 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
1383 | "stream-addr=%p, stream-name=\"%s\"", stream, | |
1384 | bt_ctf_stream_get_name(stream)); | |
263a7df5 JG |
1385 | goto end; |
1386 | } | |
1387 | ||
1388 | packet_header = stream->packet_header; | |
1389 | if (packet_header) { | |
83509119 | 1390 | bt_get(packet_header); |
263a7df5 JG |
1391 | } |
1392 | end: | |
1393 | return packet_header; | |
1394 | } | |
1395 | ||
1396 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, | |
1397 | struct bt_ctf_field *field) | |
1398 | { | |
1399 | int ret = 0; | |
e6a8e8e4 | 1400 | struct bt_ctf_trace *trace = NULL; |
263a7df5 JG |
1401 | struct bt_ctf_field_type *field_type = NULL; |
1402 | ||
19abc2c6 PP |
1403 | if (!stream) { |
1404 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1405 | ret = -1; | |
1406 | goto end; | |
1407 | } | |
1408 | ||
1409 | if (stream->pos.fd < 0) { | |
1410 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
263a7df5 JG |
1411 | ret = -1; |
1412 | goto end; | |
1413 | } | |
1414 | ||
e6a8e8e4 | 1415 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
b3376dd9 PP |
1416 | |
1417 | if (!field) { | |
1418 | if (trace->packet_header_type) { | |
1419 | BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: " | |
1420 | "stream-addr=%p, stream-name=\"%s\", " | |
1421 | "packet-header-field-addr=%p, " | |
1422 | "expected-ft-addr=%p", | |
1423 | stream, bt_ctf_stream_get_name(stream), | |
1424 | field, trace->packet_header_type); | |
1425 | ret = -1; | |
1426 | goto end; | |
1427 | } | |
1428 | ||
1429 | goto skip_validation; | |
1430 | } | |
1431 | ||
263a7df5 | 1432 | field_type = bt_ctf_field_get_type(field); |
b3376dd9 PP |
1433 | assert(field_type); |
1434 | ||
09840de5 | 1435 | if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) { |
19abc2c6 PP |
1436 | BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: " |
1437 | "stream-addr=%p, stream-name=\"%s\", " | |
1438 | "packet-header-field-addr=%p, " | |
1439 | "packet-header-ft-addr=%p", | |
1440 | stream, bt_ctf_stream_get_name(stream), | |
1441 | field, field_type); | |
263a7df5 JG |
1442 | ret = -1; |
1443 | goto end; | |
1444 | } | |
1445 | ||
b3376dd9 | 1446 | skip_validation: |
83509119 | 1447 | bt_put(stream->packet_header); |
835b2d10 | 1448 | stream->packet_header = bt_get(field); |
19abc2c6 PP |
1449 | BT_LOGV("Set stream's packet header field: " |
1450 | "stream-addr=%p, stream-name=\"%s\", " | |
1451 | "packet-header-field-addr=%p", | |
1452 | stream, bt_ctf_stream_get_name(stream), field); | |
263a7df5 | 1453 | end: |
e6a8e8e4 | 1454 | BT_PUT(trace); |
83509119 | 1455 | bt_put(field_type); |
263a7df5 JG |
1456 | return ret; |
1457 | } | |
1458 | ||
c9af50d1 JG |
1459 | static |
1460 | void reset_structure_field(struct bt_ctf_field *structure, const char *name) | |
1461 | { | |
1462 | struct bt_ctf_field *member; | |
1463 | ||
1464 | member = bt_ctf_field_structure_get_field(structure, name); | |
db1af8cc JD |
1465 | if (member) { |
1466 | (void) bt_ctf_field_reset(member); | |
1467 | bt_put(member); | |
1468 | } | |
c9af50d1 JG |
1469 | } |
1470 | ||
273b65be JG |
1471 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
1472 | { | |
1473 | int ret = 0; | |
1474 | size_t i; | |
dc3fffef PP |
1475 | struct bt_ctf_stream_pos packet_context_pos; |
1476 | struct bt_ctf_trace *trace; | |
1477 | enum bt_ctf_byte_order native_byte_order; | |
1c1d572f | 1478 | bool has_packet_size = false; |
273b65be | 1479 | |
19abc2c6 PP |
1480 | if (!stream) { |
1481 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1482 | ret = -1; | |
1483 | goto end; | |
1484 | } | |
1485 | ||
1486 | if (stream->pos.fd < 0) { | |
1487 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
273b65be JG |
1488 | ret = -1; |
1489 | goto end; | |
1490 | } | |
1491 | ||
1c1d572f | 1492 | if (stream->packet_context) { |
b3376dd9 PP |
1493 | struct bt_ctf_field *packet_size_field; |
1494 | ||
1c1d572f JD |
1495 | packet_size_field = bt_ctf_field_structure_get_field( |
1496 | stream->packet_context, "packet_size"); | |
1497 | has_packet_size = (packet_size_field != NULL); | |
1498 | bt_put(packet_size_field); | |
1499 | } | |
1500 | ||
1501 | if (stream->flushed_packet_count == 1) { | |
b3376dd9 PP |
1502 | if (!stream->packet_context) { |
1503 | BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once."); | |
1504 | ret = -1; | |
1505 | goto end; | |
1506 | } | |
1507 | ||
1c1d572f | 1508 | if (!has_packet_size) { |
b3376dd9 PP |
1509 | BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once."); |
1510 | ret = -1; | |
1511 | goto end; | |
1512 | } | |
664f50c8 JG |
1513 | } |
1514 | ||
19abc2c6 PP |
1515 | BT_LOGV("Flushing stream's current packet: stream-addr=%p, " |
1516 | "stream-name=\"%s\", packet-index=%u", stream, | |
1517 | bt_ctf_stream_get_name(stream), stream->flushed_packet_count); | |
dc3fffef PP |
1518 | trace = bt_ctf_stream_class_borrow_trace(stream->stream_class); |
1519 | assert(trace); | |
8a716c8d | 1520 | native_byte_order = bt_ctf_trace_get_native_byte_order(trace); |
d246b111 | 1521 | |
b3376dd9 | 1522 | ret = auto_populate_packet_header(stream); |
d246b111 | 1523 | if (ret) { |
b3376dd9 PP |
1524 | BT_LOGW_STR("Cannot automatically populate the stream's packet header field."); |
1525 | ret = -1; | |
d246b111 | 1526 | goto end; |
273b65be JG |
1527 | } |
1528 | ||
b3376dd9 PP |
1529 | ret = auto_populate_packet_context(stream); |
1530 | if (ret) { | |
1531 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); | |
1532 | ret = -1; | |
1533 | goto end; | |
1534 | } | |
98edd02c | 1535 | |
b3376dd9 PP |
1536 | /* mmap the next packet */ |
1537 | BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64, | |
1538 | stream->pos.offset); | |
1539 | bt_ctf_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR); | |
1540 | assert(stream->pos.packet_size % 8 == 0); | |
273b65be | 1541 | |
b3376dd9 PP |
1542 | if (stream->packet_header) { |
1543 | BT_LOGV_STR("Serializing packet header field."); | |
1544 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos, | |
1545 | native_byte_order); | |
1546 | if (ret) { | |
1547 | BT_LOGW("Cannot serialize stream's packet header field: " | |
1548 | "field-addr=%p", stream->packet_header); | |
98edd02c JG |
1549 | goto end; |
1550 | } | |
b3376dd9 | 1551 | } |
12c8a1a3 | 1552 | |
b3376dd9 | 1553 | if (stream->packet_context) { |
10cb7b41 JG |
1554 | /* Write packet context */ |
1555 | memcpy(&packet_context_pos, &stream->pos, | |
dc3fffef | 1556 | sizeof(packet_context_pos)); |
19abc2c6 | 1557 | BT_LOGV_STR("Serializing packet context field."); |
10cb7b41 | 1558 | ret = bt_ctf_field_serialize(stream->packet_context, |
dc3fffef | 1559 | &stream->pos, native_byte_order); |
98edd02c | 1560 | if (ret) { |
b3376dd9 | 1561 | BT_LOGW("Cannot serialize stream's packet context field: " |
19abc2c6 | 1562 | "field-addr=%p", stream->packet_context); |
98edd02c JG |
1563 | goto end; |
1564 | } | |
12c8a1a3 JG |
1565 | } |
1566 | ||
19abc2c6 PP |
1567 | BT_LOGV("Serializing events: count=%u", stream->events->len); |
1568 | ||
273b65be JG |
1569 | for (i = 0; i < stream->events->len; i++) { |
1570 | struct bt_ctf_event *event = g_ptr_array_index( | |
1571 | stream->events, i); | |
19abc2c6 PP |
1572 | struct bt_ctf_event_class *event_class = |
1573 | bt_ctf_event_borrow_event_class(event); | |
1574 | ||
b3376dd9 | 1575 | BT_LOGV("Serializing event: index=%zu, event-addr=%p, " |
19abc2c6 PP |
1576 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
1577 | "pos-offset=%" PRId64 ", packet-size=%" PRIu64, | |
1578 | i, event, bt_ctf_event_class_get_name(event_class), | |
1579 | bt_ctf_event_class_get_id(event_class), | |
1580 | stream->pos.offset, stream->pos.packet_size); | |
273b65be | 1581 | |
273b65be | 1582 | /* Write event header */ |
06b019a7 JD |
1583 | if (event->event_header) { |
1584 | BT_LOGV_STR("Serializing event's header field."); | |
1585 | ret = bt_ctf_field_serialize(event->event_header, | |
1586 | &stream->pos, native_byte_order); | |
1587 | if (ret) { | |
1588 | BT_LOGW("Cannot serialize event's header field: " | |
1589 | "field-addr=%p", event->event_header); | |
1590 | goto end; | |
1591 | } | |
273b65be JG |
1592 | } |
1593 | ||
8bfa3f9c | 1594 | /* Write stream event context */ |
5fd2e9fd | 1595 | if (event->stream_event_context) { |
19abc2c6 | 1596 | BT_LOGV_STR("Serializing event's stream event context field."); |
8bfa3f9c | 1597 | ret = bt_ctf_field_serialize( |
dc3fffef PP |
1598 | event->stream_event_context, &stream->pos, |
1599 | native_byte_order); | |
8bfa3f9c | 1600 | if (ret) { |
b3376dd9 | 1601 | BT_LOGW("Cannot serialize event's stream event context field: " |
19abc2c6 | 1602 | "field-addr=%p", event->stream_event_context); |
8bfa3f9c JG |
1603 | goto end; |
1604 | } | |
1605 | } | |
1606 | ||
273b65be | 1607 | /* Write event content */ |
dc3fffef PP |
1608 | ret = bt_ctf_event_serialize(event, &stream->pos, |
1609 | native_byte_order); | |
273b65be | 1610 | if (ret) { |
19abc2c6 | 1611 | /* bt_ctf_event_serialize() logs errors */ |
273b65be JG |
1612 | goto end; |
1613 | } | |
1614 | } | |
1615 | ||
1c1d572f JD |
1616 | if (!has_packet_size && stream->pos.offset % 8 != 0) { |
1617 | BT_LOGW("Stream's packet context field type has no `packet_size` field, " | |
1618 | "but current content size is not a multiple of 8 bits: " | |
1619 | "content-size=%" PRId64 ", " | |
1620 | "packet-size=%" PRIu64, | |
1621 | stream->pos.offset, | |
1622 | stream->pos.packet_size); | |
1623 | ret = -1; | |
1624 | goto end; | |
1625 | } | |
1626 | ||
b3376dd9 | 1627 | assert(stream->pos.packet_size % 8 == 0); |
0686ef94 | 1628 | |
1c1d572f JD |
1629 | /* |
1630 | * Remove extra padding bytes. | |
1631 | */ | |
1632 | stream->pos.packet_size = (stream->pos.offset + 7) & ~7; | |
1633 | ||
98edd02c JG |
1634 | if (stream->packet_context) { |
1635 | /* | |
b3376dd9 PP |
1636 | * The whole packet is serialized at this point. Make sure that, |
1637 | * if `packet_size` is missing, the current content size is | |
1638 | * equal to the current packet size. | |
98edd02c | 1639 | */ |
b3376dd9 PP |
1640 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field( |
1641 | stream->packet_context, "content_size"); | |
1642 | ||
1643 | bt_put(field); | |
1644 | if (!field) { | |
1645 | if (stream->pos.offset != stream->pos.packet_size) { | |
1646 | BT_LOGW("Stream's packet context's `content_size` field is missing, " | |
1647 | "but current packet's content size is not equal to its packet size: " | |
1648 | "content-size=%" PRId64 ", " | |
1649 | "packet-size=%" PRIu64, | |
1650 | stream->pos.offset, | |
1651 | stream->pos.packet_size); | |
1652 | ret = -1; | |
c9af50d1 JG |
1653 | goto end; |
1654 | } | |
98edd02c | 1655 | } |
273b65be | 1656 | |
b3376dd9 PP |
1657 | /* |
1658 | * Overwrite the packet context now that the stream | |
1659 | * position's packet and content sizes have the correct | |
1660 | * values. | |
1661 | * | |
1662 | * Copy base_mma as the packet may have been remapped | |
1663 | * (e.g. when a packet is resized). | |
1664 | */ | |
1665 | packet_context_pos.base_mma = stream->pos.base_mma; | |
1666 | ret = auto_populate_packet_context(stream); | |
1667 | if (ret) { | |
1668 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); | |
1669 | ret = -1; | |
1670 | goto end; | |
98edd02c | 1671 | } |
273b65be | 1672 | |
19abc2c6 | 1673 | BT_LOGV("Rewriting (serializing) packet context field."); |
98edd02c | 1674 | ret = bt_ctf_field_serialize(stream->packet_context, |
dc3fffef | 1675 | &packet_context_pos, native_byte_order); |
98edd02c | 1676 | if (ret) { |
b3376dd9 | 1677 | BT_LOGW("Cannot serialize stream's packet context field: " |
19abc2c6 | 1678 | "field-addr=%p", stream->packet_context); |
98edd02c JG |
1679 | goto end; |
1680 | } | |
273b65be JG |
1681 | } |
1682 | ||
1683 | g_ptr_array_set_size(stream->events, 0); | |
1684 | stream->flushed_packet_count++; | |
b3376dd9 | 1685 | stream->size += stream->pos.packet_size / CHAR_BIT; |
1c1d572f JD |
1686 | |
1687 | do { | |
1688 | ret = ftruncate(stream->pos.fd, stream->size); | |
1689 | } while (ret == -1 && errno == EINTR); | |
1690 | if (ret == -1) { | |
0290b3c5 PP |
1691 | BT_LOGE_ERRNO("Cannot truncate stream file to new size", |
1692 | ": size=%" PRIu64 ", stream-addr=%p, " | |
1693 | "stream-name=\"%s\"", | |
1694 | stream->size, stream, | |
1695 | bt_ctf_stream_get_name(stream)); | |
1c1d572f JD |
1696 | } |
1697 | ||
273b65be | 1698 | end: |
c9af50d1 | 1699 | /* Reset automatically-set fields. */ |
1c1d572f JD |
1700 | if (stream->packet_context) { |
1701 | reset_structure_field(stream->packet_context, "timestamp_begin"); | |
1702 | reset_structure_field(stream->packet_context, "timestamp_end"); | |
1703 | reset_structure_field(stream->packet_context, "packet_size"); | |
1704 | reset_structure_field(stream->packet_context, "content_size"); | |
1705 | reset_structure_field(stream->packet_context, "events_discarded"); | |
1706 | } | |
cd7d8fb7 JG |
1707 | |
1708 | if (ret < 0) { | |
1709 | /* | |
1710 | * We failed to write the packet. Its size is therefore set to 0 | |
1711 | * to ensure the next mapping is done in the same place rather | |
1712 | * than advancing by "stream->pos.packet_size", which would | |
1713 | * leave a corrupted packet in the trace. | |
1714 | */ | |
1715 | stream->pos.packet_size = 0; | |
19abc2c6 | 1716 | } else { |
b3376dd9 PP |
1717 | BT_LOGV("Flushed stream's current packet: content-size=%" PRId64 ", " |
1718 | "packet-size=%" PRIu64, | |
1719 | stream->pos.offset, stream->pos.packet_size); | |
cd7d8fb7 | 1720 | } |
273b65be JG |
1721 | return ret; |
1722 | } | |
1723 | ||
19abc2c6 | 1724 | /* Pre-2.0 CTF writer backward compatibility */ |
273b65be JG |
1725 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) |
1726 | { | |
83509119 | 1727 | bt_get(stream); |
273b65be JG |
1728 | } |
1729 | ||
19abc2c6 | 1730 | /* Pre-2.0 CTF writer backward compatibility */ |
273b65be JG |
1731 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) |
1732 | { | |
83509119 | 1733 | bt_put(stream); |
273b65be JG |
1734 | } |
1735 | ||
1736 | static | |
83509119 | 1737 | void bt_ctf_stream_destroy(struct bt_object *obj) |
273b65be JG |
1738 | { |
1739 | struct bt_ctf_stream *stream; | |
3230ee6b | 1740 | int i; |
273b65be | 1741 | |
83509119 | 1742 | stream = container_of(obj, struct bt_ctf_stream, base); |
19abc2c6 PP |
1743 | BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"", |
1744 | stream, bt_ctf_stream_get_name(stream)); | |
3230ee6b PP |
1745 | |
1746 | /* Call destroy listeners in reverse registration order */ | |
1747 | for (i = stream->destroy_listeners->len - 1; i >= 0; i--) { | |
1748 | struct bt_ctf_stream_destroy_listener *listener = | |
1749 | &g_array_index(stream->destroy_listeners, | |
1750 | struct bt_ctf_stream_destroy_listener, i); | |
1751 | ||
19abc2c6 PP |
1752 | BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d", |
1753 | listener->func, listener->data, i); | |
3230ee6b PP |
1754 | listener->func(stream, listener->data); |
1755 | } | |
1756 | ||
dc3fffef | 1757 | (void) bt_ctf_stream_pos_fini(&stream->pos); |
0686ef94 JG |
1758 | if (stream->pos.fd >= 0) { |
1759 | int ret; | |
1760 | ||
1761 | /* | |
1762 | * Truncate the file's size to the minimum required to fit the | |
1763 | * last packet as we might have grown it too much on the last | |
1764 | * mmap. | |
1765 | */ | |
1766 | do { | |
1767 | ret = ftruncate(stream->pos.fd, stream->size); | |
1768 | } while (ret == -1 && errno == EINTR); | |
1769 | if (ret) { | |
19abc2c6 PP |
1770 | BT_LOGE("Failed to truncate stream file: %s: " |
1771 | "ret=%d, errno=%d, size=%" PRIu64, | |
1772 | strerror(errno), ret, errno, | |
1773 | (uint64_t) stream->size); | |
0686ef94 JG |
1774 | } |
1775 | ||
1776 | if (close(stream->pos.fd)) { | |
19abc2c6 PP |
1777 | BT_LOGE("Failed to close stream file: %s: " |
1778 | "ret=%d, errno=%d", strerror(errno), | |
1779 | ret, errno); | |
0686ef94 | 1780 | } |
9f56e450 | 1781 | } |
12c8a1a3 | 1782 | |
12c8a1a3 | 1783 | if (stream->events) { |
19abc2c6 | 1784 | BT_LOGD_STR("Putting events."); |
12c8a1a3 JG |
1785 | g_ptr_array_free(stream->events, TRUE); |
1786 | } | |
b71d7298 PP |
1787 | |
1788 | if (stream->name) { | |
1789 | g_string_free(stream->name, TRUE); | |
1790 | } | |
41ac640a | 1791 | |
3230ee6b PP |
1792 | if (stream->comp_cur_port) { |
1793 | GHashTableIter ht_iter; | |
1794 | gpointer comp_gptr, port_gptr; | |
1795 | ||
1796 | /* | |
1797 | * Since we're destroying the stream, remove the destroy | |
1798 | * listeners that it registered for each component in | |
1799 | * its component-port mapping hash table. Otherwise they | |
1800 | * would be called and the stream would be accessed once | |
1801 | * it's freed or another stream would be accessed. | |
1802 | */ | |
1803 | g_hash_table_iter_init(&ht_iter, stream->comp_cur_port); | |
1804 | ||
1805 | while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) { | |
1806 | assert(comp_gptr); | |
1807 | bt_component_remove_destroy_listener((void *) comp_gptr, | |
1808 | component_destroy_listener, stream); | |
1809 | } | |
1810 | ||
1811 | g_hash_table_destroy(stream->comp_cur_port); | |
1812 | } | |
1813 | ||
1814 | if (stream->destroy_listeners) { | |
1815 | g_array_free(stream->destroy_listeners, TRUE); | |
1816 | } | |
1817 | ||
19abc2c6 | 1818 | BT_LOGD_STR("Putting packet header field."); |
83509119 | 1819 | bt_put(stream->packet_header); |
19abc2c6 | 1820 | BT_LOGD_STR("Putting packet context field."); |
83509119 | 1821 | bt_put(stream->packet_context); |
273b65be JG |
1822 | g_free(stream); |
1823 | } | |
1824 | ||
273b65be | 1825 | static |
af9296f3 | 1826 | int _set_structure_field_integer(struct bt_ctf_field *structure, char *name, |
c55a9f58 | 1827 | uint64_t value, bt_bool force) |
273b65be JG |
1828 | { |
1829 | int ret = 0; | |
d7b1ea66 | 1830 | struct bt_ctf_field_type *field_type = NULL; |
19abc2c6 | 1831 | struct bt_ctf_field *integer; |
12c8a1a3 | 1832 | |
19abc2c6 PP |
1833 | assert(structure); |
1834 | assert(name); | |
273b65be | 1835 | |
19abc2c6 | 1836 | integer = bt_ctf_field_structure_get_field(structure, name); |
12c8a1a3 JG |
1837 | if (!integer) { |
1838 | /* Field not found, not an error. */ | |
19abc2c6 PP |
1839 | BT_LOGV("Field not found: struct-field-addr=%p, " |
1840 | "name=\"%s\", force=%d", structure, name, force); | |
12c8a1a3 JG |
1841 | goto end; |
1842 | } | |
1843 | ||
1844 | /* Make sure the payload has not already been set. */ | |
af9296f3 | 1845 | if (!force && bt_ctf_field_is_set(integer)) { |
12c8a1a3 | 1846 | /* Payload already set, not an error */ |
19abc2c6 PP |
1847 | BT_LOGV("Field's payload is already set: struct-field-addr=%p, " |
1848 | "name=\"%s\", force=%d", structure, name, force); | |
12c8a1a3 JG |
1849 | goto end; |
1850 | } | |
1851 | ||
d7b1ea66 | 1852 | field_type = bt_ctf_field_get_type(integer); |
d7b1ea66 | 1853 | assert(field_type); |
1487a16a | 1854 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d7b1ea66 JG |
1855 | /* |
1856 | * The user most likely meant for us to populate this field | |
1857 | * automatically. However, we can only do this if the field | |
1858 | * is an integer. Return an error. | |
1859 | */ | |
19abc2c6 PP |
1860 | BT_LOGW("Invalid parameter: field's type is not an integer field type: " |
1861 | "field-addr=%p, ft-addr=%p, ft-id=%s", | |
1862 | integer, field_type, | |
1863 | bt_ctf_field_type_id_string(field_type->id)); | |
d7b1ea66 JG |
1864 | ret = -1; |
1865 | goto end; | |
1866 | } | |
1867 | ||
1868 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
1869 | ret = bt_ctf_field_signed_integer_set_value(integer, | |
1870 | (int64_t) value); | |
1871 | } else { | |
1872 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); | |
1873 | } | |
af9296f3 | 1874 | ret = !ret ? 1 : ret; |
273b65be | 1875 | end: |
83509119 JG |
1876 | bt_put(integer); |
1877 | bt_put(field_type); | |
273b65be JG |
1878 | return ret; |
1879 | } | |
b71d7298 | 1880 | |
af9296f3 JG |
1881 | /* |
1882 | * Returns the following codes: | |
1883 | * 1 if the field was found and set, | |
1884 | * 0 if nothing was done (field not found, or was already set), | |
1885 | * <0 if an error was encoutered | |
1886 | */ | |
1887 | static | |
1888 | int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
1889 | uint64_t value) | |
1890 | { | |
c55a9f58 | 1891 | return _set_structure_field_integer(structure, name, value, BT_FALSE); |
af9296f3 JG |
1892 | } |
1893 | ||
b71d7298 PP |
1894 | const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream) |
1895 | { | |
1896 | const char *name = NULL; | |
1897 | ||
1898 | if (!stream) { | |
19abc2c6 | 1899 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
b71d7298 PP |
1900 | goto end; |
1901 | } | |
1902 | ||
1903 | name = stream->name ? stream->name->str : NULL; | |
1904 | ||
1905 | end: | |
1906 | return name; | |
1907 | } | |
98a4cbef PP |
1908 | |
1909 | int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream) | |
1910 | { | |
1911 | int ret = -1; | |
1912 | ||
1913 | if (!stream) { | |
19abc2c6 | 1914 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
98a4cbef PP |
1915 | goto end; |
1916 | } | |
1917 | ||
1918 | ret = (stream->pos.fd >= 0); | |
1919 | ||
1920 | end: | |
1921 | return ret; | |
1922 | } | |
3230ee6b PP |
1923 | |
1924 | BT_HIDDEN | |
1925 | void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream *stream, | |
1926 | struct bt_component *comp, | |
1927 | struct bt_port *port) | |
1928 | { | |
1929 | assert(stream); | |
1930 | assert(comp); | |
1931 | assert(port); | |
1932 | assert(stream->comp_cur_port); | |
1933 | ||
1934 | /* | |
1935 | * Do not take a reference to the component here because we | |
1936 | * don't want the component to exist as long as this stream | |
1937 | * exists. Instead, keep a weak reference, but add a destroy | |
1938 | * listener so that we remove this hash table entry when we know | |
1939 | * the component is destroyed. | |
1940 | */ | |
a2136eca PP |
1941 | BT_LOGV("Adding component's destroy listener for stream: " |
1942 | "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, " | |
1943 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
19abc2c6 PP |
1944 | stream, bt_ctf_stream_get_name(stream), |
1945 | comp, bt_component_get_name(comp), port, | |
1946 | bt_port_get_name(port)); | |
a2136eca PP |
1947 | bt_component_add_destroy_listener(comp, component_destroy_listener, |
1948 | stream); | |
1949 | g_hash_table_insert(stream->comp_cur_port, comp, port); | |
1950 | BT_LOGV_STR("Mapped component to port for stream."); | |
3230ee6b PP |
1951 | } |
1952 | ||
1953 | BT_HIDDEN | |
1954 | struct bt_port *bt_ctf_stream_port_for_component(struct bt_ctf_stream *stream, | |
1955 | struct bt_component *comp) | |
1956 | { | |
1957 | assert(stream); | |
1958 | assert(comp); | |
1959 | assert(stream->comp_cur_port); | |
1960 | return g_hash_table_lookup(stream->comp_cur_port, comp); | |
1961 | } | |
1962 | ||
1963 | BT_HIDDEN | |
1964 | void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream *stream, | |
1965 | bt_ctf_stream_destroy_listener_func func, void *data) | |
1966 | { | |
1967 | struct bt_ctf_stream_destroy_listener listener; | |
1968 | ||
1969 | assert(stream); | |
1970 | assert(func); | |
1971 | listener.func = func; | |
1972 | listener.data = data; | |
1973 | g_array_append_val(stream->destroy_listeners, listener); | |
19abc2c6 PP |
1974 | BT_LOGV("Added stream destroy listener: stream-addr=%p, " |
1975 | "stream-name=\"%s\", func=%p, data=%p", | |
1976 | stream, bt_ctf_stream_get_name(stream), func, data); | |
3230ee6b PP |
1977 | } |
1978 | ||
1979 | BT_HIDDEN | |
1980 | void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream *stream, | |
1981 | bt_ctf_stream_destroy_listener_func func, void *data) | |
1982 | { | |
1983 | size_t i; | |
1984 | ||
1985 | assert(stream); | |
1986 | assert(func); | |
1987 | ||
1988 | for (i = 0; i < stream->destroy_listeners->len; i++) { | |
1989 | struct bt_ctf_stream_destroy_listener *listener = | |
1990 | &g_array_index(stream->destroy_listeners, | |
1991 | struct bt_ctf_stream_destroy_listener, i); | |
1992 | ||
1993 | if (listener->func == func && listener->data == data) { | |
1994 | g_array_remove_index(stream->destroy_listeners, i); | |
1995 | i--; | |
19abc2c6 PP |
1996 | BT_LOGV("Removed stream destroy listener: stream-addr=%p, " |
1997 | "stream-name=\"%s\", func=%p, data=%p", | |
1998 | stream, bt_ctf_stream_get_name(stream), | |
1999 | func, data); | |
3230ee6b PP |
2000 | } |
2001 | } | |
2002 | } | |
cfe11c58 PP |
2003 | |
2004 | int64_t bt_ctf_stream_get_id(struct bt_ctf_stream *stream) | |
2005 | { | |
2006 | int64_t ret; | |
2007 | ||
2008 | if (!stream) { | |
2009 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
2010 | ret = (int64_t) -1; | |
2011 | goto end; | |
2012 | } | |
2013 | ||
2014 | ret = stream->id; | |
2015 | if (ret < 0) { | |
2016 | BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"", | |
2017 | stream, bt_ctf_stream_get_name(stream)); | |
2018 | } | |
2019 | ||
2020 | end: | |
2021 | return ret; | |
2022 | } |