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