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 | ||
cfe11c58 | 962 | static |
50842bdc PP |
963 | struct bt_stream *bt_stream_create_with_id_no_check( |
964 | struct bt_stream_class *stream_class, | |
cfe11c58 | 965 | const char *name, uint64_t id) |
273b65be | 966 | { |
12c8a1a3 | 967 | int ret; |
50842bdc PP |
968 | struct bt_stream *stream = NULL; |
969 | struct bt_trace *trace = NULL; | |
319fd969 | 970 | struct bt_ctf_writer *writer = NULL; |
273b65be | 971 | |
319fd969 | 972 | if (!stream_class) { |
19abc2c6 | 973 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
b71d7298 | 974 | goto error; |
319fd969 PP |
975 | } |
976 | ||
19abc2c6 | 977 | BT_LOGD("Creating stream object: stream-class-addr=%p, " |
cfe11c58 PP |
978 | "stream-class-name=\"%s\", stream-name=\"%s\", " |
979 | "stream-id=%" PRIu64, | |
50842bdc | 980 | stream_class, bt_stream_class_get_name(stream_class), |
cfe11c58 | 981 | name, id); |
50842bdc | 982 | trace = bt_stream_class_borrow_trace(stream_class); |
319fd969 | 983 | if (!trace) { |
19abc2c6 | 984 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " |
b3376dd9 PP |
985 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
986 | "stream-name=\"%s\"", | |
50842bdc | 987 | stream_class, bt_stream_class_get_name(stream_class), |
19abc2c6 | 988 | name); |
b71d7298 | 989 | goto error; |
273b65be JG |
990 | } |
991 | ||
50842bdc | 992 | if (bt_trace_is_static(trace)) { |
5acf2ae6 PP |
993 | /* |
994 | * A static trace has the property that all its stream | |
995 | * classes, clock classes, and streams are definitive: | |
996 | * no more can be added, and each object is also frozen. | |
997 | */ | |
19abc2c6 | 998 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: " |
b3376dd9 PP |
999 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
1000 | "stream-name=\"%s\", trace-addr=%p", | |
50842bdc | 1001 | stream_class, bt_stream_class_get_name(stream_class), |
b3376dd9 | 1002 | name, trace); |
5acf2ae6 PP |
1003 | goto error; |
1004 | } | |
1005 | ||
cfe11c58 PP |
1006 | if (id != -1ULL) { |
1007 | /* | |
1008 | * Validate that the given ID is unique amongst all the | |
1009 | * existing trace's streams created from the same stream | |
1010 | * class. | |
1011 | */ | |
1012 | size_t i; | |
1013 | ||
1014 | for (i = 0; i < trace->streams->len; i++) { | |
50842bdc | 1015 | struct bt_stream *trace_stream = |
cfe11c58 PP |
1016 | g_ptr_array_index(trace->streams, i); |
1017 | ||
1018 | if (trace_stream->stream_class != stream_class) { | |
1019 | continue; | |
1020 | } | |
1021 | ||
1022 | if (trace_stream->id == id) { | |
1023 | BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID."); | |
1024 | goto error; | |
1025 | } | |
1026 | } | |
1027 | } | |
1028 | ||
50842bdc | 1029 | stream = g_new0(struct bt_stream, 1); |
273b65be | 1030 | if (!stream) { |
19abc2c6 | 1031 | BT_LOGE_STR("Failed to allocate one stream."); |
b71d7298 | 1032 | goto error; |
273b65be JG |
1033 | } |
1034 | ||
50842bdc | 1035 | bt_object_init(stream, bt_stream_destroy); |
e6a8e8e4 JG |
1036 | /* |
1037 | * Acquire reference to parent since stream will become publicly | |
1038 | * reachable; it needs its parent to remain valid. | |
1039 | */ | |
1040 | bt_object_set_parent(stream, trace); | |
273b65be | 1041 | stream->stream_class = stream_class; |
319fd969 | 1042 | stream->pos.fd = -1; |
cfe11c58 | 1043 | stream->id = (int64_t) id; |
d246b111 | 1044 | |
3230ee6b | 1045 | stream->destroy_listeners = g_array_new(FALSE, TRUE, |
50842bdc | 1046 | sizeof(struct bt_stream_destroy_listener)); |
3230ee6b | 1047 | if (!stream->destroy_listeners) { |
19abc2c6 | 1048 | BT_LOGE_STR("Failed to allocate a GArray."); |
3230ee6b PP |
1049 | goto error; |
1050 | } | |
1051 | ||
b71d7298 PP |
1052 | if (name) { |
1053 | stream->name = g_string_new(name); | |
1054 | if (!stream->name) { | |
19abc2c6 | 1055 | BT_LOGE_STR("Failed to allocate a GString."); |
b71d7298 PP |
1056 | goto error; |
1057 | } | |
1058 | } | |
1059 | ||
19abc2c6 PP |
1060 | BT_LOGD("Set stream's trace parent: trace-addr=%p", trace); |
1061 | ||
319fd969 PP |
1062 | if (trace->is_created_by_writer) { |
1063 | int fd; | |
2a03740b | 1064 | |
cfe11c58 PP |
1065 | writer = (struct bt_ctf_writer *) bt_object_get_parent(trace); |
1066 | stream->id = (int64_t) stream_class->next_stream_id++; | |
2a03740b | 1067 | stream->last_ts_end = -1ULL; |
319fd969 | 1068 | |
19abc2c6 PP |
1069 | BT_LOGD("Stream object belongs to a writer's trace: " |
1070 | "writer-addr=%p", writer); | |
f6ccaed9 | 1071 | BT_ASSERT(writer); |
b3376dd9 | 1072 | |
98edd02c | 1073 | if (stream_class->packet_context_type) { |
19abc2c6 PP |
1074 | BT_LOGD("Creating stream's packet context field: " |
1075 | "ft-addr=%p", stream_class->packet_context_type); | |
50842bdc | 1076 | stream->packet_context = bt_field_create( |
98edd02c JG |
1077 | stream_class->packet_context_type); |
1078 | if (!stream->packet_context) { | |
19abc2c6 | 1079 | BT_LOGW_STR("Cannot create stream's packet context field."); |
98edd02c JG |
1080 | goto error; |
1081 | } | |
319fd969 | 1082 | |
98edd02c | 1083 | /* Initialize events_discarded */ |
af9296f3 | 1084 | ret = try_set_structure_field_integer( |
19abc2c6 | 1085 | stream->packet_context, "events_discarded", 0); |
7ee636df | 1086 | if (ret < 0) { |
19abc2c6 PP |
1087 | BT_LOGW("Cannot set `events_discarded` field in packet context: " |
1088 | "ret=%d, packet-context-field-addr=%p", | |
1089 | ret, stream->packet_context); | |
98edd02c JG |
1090 | goto error; |
1091 | } | |
319fd969 PP |
1092 | } |
1093 | ||
1094 | stream->events = g_ptr_array_new_with_free_func( | |
1095 | (GDestroyNotify) release_event); | |
1096 | if (!stream->events) { | |
19abc2c6 | 1097 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
319fd969 PP |
1098 | goto error; |
1099 | } | |
1100 | ||
b3376dd9 PP |
1101 | if (trace->packet_header_type) { |
1102 | BT_LOGD("Creating stream's packet header field: " | |
1103 | "ft-addr=%p", trace->packet_header_type); | |
1104 | stream->packet_header = | |
50842bdc | 1105 | bt_field_create(trace->packet_header_type); |
b3376dd9 PP |
1106 | if (!stream->packet_header) { |
1107 | BT_LOGW_STR("Cannot create stream's packet header field."); | |
1108 | goto error; | |
1109 | } | |
319fd969 PP |
1110 | } |
1111 | ||
1112 | /* | |
1113 | * Attempt to populate the default trace packet header fields | |
1114 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
1115 | * fields not be found or be of an incompatible type; they will | |
1116 | * simply not be populated automatically. The user will have to | |
1117 | * make sure to set the trace packet header fields himself | |
1118 | * before flushing. | |
1119 | */ | |
b3376dd9 | 1120 | ret = auto_populate_packet_header(stream); |
319fd969 | 1121 | if (ret) { |
b3376dd9 | 1122 | BT_LOGW_STR("Cannot automatically populate the stream's packet header."); |
319fd969 PP |
1123 | goto error; |
1124 | } | |
1125 | ||
1126 | /* Create file associated with this stream */ | |
1127 | fd = create_stream_file(writer, stream); | |
1128 | if (fd < 0) { | |
19abc2c6 | 1129 | BT_LOGW_STR("Cannot create stream file."); |
319fd969 PP |
1130 | goto error; |
1131 | } | |
1132 | ||
19abc2c6 | 1133 | set_stream_fd(stream, fd); |
319fd969 PP |
1134 | |
1135 | /* Freeze the writer */ | |
19abc2c6 | 1136 | BT_LOGD_STR("Freezing stream's CTF writer."); |
319fd969 PP |
1137 | bt_ctf_writer_freeze(writer); |
1138 | } else { | |
1139 | /* Non-writer stream indicated by a negative FD */ | |
19abc2c6 | 1140 | set_stream_fd(stream, -1); |
d246b111 | 1141 | } |
319fd969 PP |
1142 | |
1143 | /* Add this stream to the trace's streams */ | |
1144 | g_ptr_array_add(trace->streams, stream); | |
19abc2c6 | 1145 | BT_LOGD("Created stream object: addr=%p", stream); |
cfe11c58 PP |
1146 | goto end; |
1147 | ||
83509119 JG |
1148 | error: |
1149 | BT_PUT(stream); | |
cfe11c58 PP |
1150 | |
1151 | end: | |
1152 | bt_put(writer); | |
1153 | return stream; | |
1154 | } | |
1155 | ||
50842bdc PP |
1156 | struct bt_stream *bt_stream_create_with_id( |
1157 | struct bt_stream_class *stream_class, | |
cfe11c58 PP |
1158 | const char *name, uint64_t id_param) |
1159 | { | |
50842bdc PP |
1160 | struct bt_trace *trace; |
1161 | struct bt_stream *stream = NULL; | |
cfe11c58 PP |
1162 | int64_t id = (int64_t) id_param; |
1163 | ||
1164 | if (!stream_class) { | |
1165 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
1166 | goto end; | |
1167 | } | |
1168 | ||
1169 | if (id < 0) { | |
1170 | BT_LOGW("Invalid parameter: invalid stream's ID: " | |
1171 | "name=\"%s\", id=%" PRIu64, | |
1172 | name, id_param); | |
1173 | goto end; | |
1174 | } | |
1175 | ||
50842bdc | 1176 | trace = bt_stream_class_borrow_trace(stream_class); |
cfe11c58 PP |
1177 | if (!trace) { |
1178 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " | |
1179 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1180 | "stream-name=\"%s\", stream-id=%" PRIu64, | |
50842bdc | 1181 | stream_class, bt_stream_class_get_name(stream_class), |
cfe11c58 PP |
1182 | name, id_param); |
1183 | goto end; | |
1184 | } | |
1185 | ||
1186 | if (trace->is_created_by_writer) { | |
50842bdc | 1187 | BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_stream_create(): " |
cfe11c58 PP |
1188 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
1189 | "stream-name=\"%s\", stream-id=%" PRIu64, | |
50842bdc | 1190 | stream_class, bt_stream_class_get_name(stream_class), |
cfe11c58 PP |
1191 | name, id_param); |
1192 | goto end; | |
1193 | } | |
1194 | ||
50842bdc | 1195 | stream = bt_stream_create_with_id_no_check(stream_class, |
cfe11c58 PP |
1196 | name, id_param); |
1197 | ||
1198 | end: | |
83509119 | 1199 | return stream; |
273b65be JG |
1200 | } |
1201 | ||
50842bdc PP |
1202 | struct bt_stream *bt_stream_create( |
1203 | struct bt_stream_class *stream_class, | |
cfe11c58 PP |
1204 | const char *name) |
1205 | { | |
50842bdc | 1206 | return bt_stream_create_with_id_no_check(stream_class, |
cfe11c58 PP |
1207 | name, -1ULL); |
1208 | } | |
1209 | ||
50842bdc PP |
1210 | struct bt_stream_class *bt_stream_get_class( |
1211 | struct bt_stream *stream) | |
3baf0856 | 1212 | { |
50842bdc | 1213 | struct bt_stream_class *stream_class = NULL; |
3baf0856 JG |
1214 | |
1215 | if (!stream) { | |
19abc2c6 | 1216 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
3baf0856 JG |
1217 | goto end; |
1218 | } | |
1219 | ||
1220 | stream_class = stream->stream_class; | |
83509119 | 1221 | bt_get(stream_class); |
3baf0856 JG |
1222 | end: |
1223 | return stream_class; | |
1224 | } | |
1225 | ||
50842bdc PP |
1226 | int bt_stream_get_discarded_events_count( |
1227 | struct bt_stream *stream, uint64_t *count) | |
a78a2e25 | 1228 | { |
37949e9e | 1229 | int ret = 0; |
a78a2e25 | 1230 | |
19abc2c6 PP |
1231 | if (!stream) { |
1232 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
37949e9e | 1233 | ret = -1; |
19abc2c6 PP |
1234 | goto end; |
1235 | } | |
1236 | ||
1237 | if (!count) { | |
1238 | BT_LOGW_STR("Invalid parameter: count is NULL."); | |
37949e9e | 1239 | ret = -1; |
19abc2c6 PP |
1240 | goto end; |
1241 | } | |
1242 | ||
b3376dd9 PP |
1243 | if (stream->pos.fd < 0) { |
1244 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
19abc2c6 | 1245 | "stream-addr=%p, stream-name=\"%s\"", |
50842bdc | 1246 | stream, bt_stream_get_name(stream)); |
37949e9e | 1247 | ret = -1; |
19abc2c6 PP |
1248 | goto end; |
1249 | } | |
1250 | ||
b3376dd9 PP |
1251 | *count = (uint64_t) stream->discarded_events; |
1252 | ||
1253 | end: | |
1254 | return ret; | |
1255 | } | |
1256 | ||
1257 | static | |
50842bdc | 1258 | int set_packet_context_events_discarded_field(struct bt_stream *stream, |
b3376dd9 PP |
1259 | uint64_t count) |
1260 | { | |
1261 | int ret = 0; | |
50842bdc | 1262 | struct bt_field *events_discarded_field = NULL; |
b3376dd9 PP |
1263 | |
1264 | if (!stream->packet_context) { | |
a78a2e25 JG |
1265 | goto end; |
1266 | } | |
1267 | ||
50842bdc | 1268 | events_discarded_field = bt_field_structure_get_field_by_name( |
12c8a1a3 JG |
1269 | stream->packet_context, "events_discarded"); |
1270 | if (!events_discarded_field) { | |
12c8a1a3 JG |
1271 | goto end; |
1272 | } | |
1273 | ||
50842bdc | 1274 | ret = bt_field_unsigned_integer_set_value( |
b3376dd9 PP |
1275 | events_discarded_field, count); |
1276 | if (ret) { | |
1277 | BT_LOGW("Cannot set packet context's `events_discarded` field: " | |
1278 | "field-addr=%p, value=%" PRIu64, | |
12c8a1a3 | 1279 | events_discarded_field, count); |
b3376dd9 | 1280 | goto end; |
12c8a1a3 | 1281 | } |
b3376dd9 | 1282 | |
a78a2e25 | 1283 | end: |
83509119 | 1284 | bt_put(events_discarded_field); |
a78a2e25 JG |
1285 | return ret; |
1286 | } | |
1287 | ||
50842bdc | 1288 | void bt_stream_append_discarded_events(struct bt_stream *stream, |
273b65be JG |
1289 | uint64_t event_count) |
1290 | { | |
12c8a1a3 | 1291 | int ret; |
12c8a1a3 | 1292 | uint64_t new_count; |
50842bdc | 1293 | struct bt_field *events_discarded_field = NULL; |
12c8a1a3 | 1294 | |
19abc2c6 PP |
1295 | if (!stream) { |
1296 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1297 | goto end; | |
1298 | } | |
1299 | ||
1300 | BT_LOGV("Appending discarded events to stream: " | |
1301 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, | |
50842bdc | 1302 | stream, bt_stream_get_name(stream), event_count); |
19abc2c6 PP |
1303 | |
1304 | if (!stream->packet_context) { | |
1305 | BT_LOGW_STR("Invalid parameter: stream has no packet context field."); | |
1306 | goto end; | |
1307 | } | |
1308 | ||
1309 | if (stream->pos.fd < 0) { | |
1310 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
12c8a1a3 JG |
1311 | goto end; |
1312 | } | |
1313 | ||
50842bdc | 1314 | events_discarded_field = bt_field_structure_get_field_by_name( |
12c8a1a3 JG |
1315 | stream->packet_context, "events_discarded"); |
1316 | if (!events_discarded_field) { | |
b3376dd9 | 1317 | BT_LOGW_STR("No field named `events_discarded` in stream's packet context."); |
12c8a1a3 JG |
1318 | goto end; |
1319 | } | |
1320 | ||
b3376dd9 PP |
1321 | new_count = stream->discarded_events + event_count; |
1322 | if (new_count < stream->discarded_events) { | |
1323 | BT_LOGW("New discarded events count is less than the stream's current discarded events count: " | |
1324 | "cur-count=%" PRIu64 ", new-count=%" PRIu64, | |
1325 | stream->discarded_events, new_count); | |
1326 | goto end; | |
1327 | } | |
1328 | ||
1329 | ret = set_packet_context_events_discarded_field(stream, new_count); | |
1330 | if (ret) { | |
1331 | /* set_packet_context_events_discarded_field() logs errors */ | |
1332 | goto end; | |
12c8a1a3 JG |
1333 | } |
1334 | ||
b3376dd9 | 1335 | stream->discarded_events = new_count; |
19abc2c6 PP |
1336 | BT_LOGV("Appended discarded events to stream: " |
1337 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, | |
50842bdc | 1338 | stream, bt_stream_get_name(stream), event_count); |
19abc2c6 | 1339 | |
12c8a1a3 | 1340 | end: |
83509119 | 1341 | bt_put(events_discarded_field); |
273b65be JG |
1342 | } |
1343 | ||
50842bdc PP |
1344 | static int auto_populate_event_header(struct bt_stream *stream, |
1345 | struct bt_event *event) | |
ac0c6bdd PP |
1346 | { |
1347 | int ret = 0; | |
50842bdc PP |
1348 | struct bt_field *id_field = NULL, *timestamp_field = NULL; |
1349 | struct bt_clock_class *mapped_clock_class = NULL; | |
fa2061de | 1350 | int64_t event_class_id; |
ac0c6bdd | 1351 | |
f6ccaed9 | 1352 | BT_ASSERT(event); |
19abc2c6 | 1353 | |
06b019a7 JD |
1354 | if (!event->event_header) { |
1355 | goto end; | |
1356 | } | |
1357 | ||
19abc2c6 PP |
1358 | if (event->frozen) { |
1359 | BT_LOGW_STR("Cannot populate event header field: event is frozen."); | |
ac0c6bdd PP |
1360 | ret = -1; |
1361 | goto end; | |
1362 | } | |
1363 | ||
b3376dd9 | 1364 | BT_LOGV("Automatically populating event's header field: " |
19abc2c6 | 1365 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
50842bdc | 1366 | stream, bt_stream_get_name(stream), event); |
19abc2c6 | 1367 | |
50842bdc PP |
1368 | id_field = bt_field_structure_get_field_by_name(event->event_header, "id"); |
1369 | event_class_id = bt_event_class_get_id(event->event_class); | |
f6ccaed9 | 1370 | BT_ASSERT(event_class_id >= 0); |
2a03740b | 1371 | |
50842bdc | 1372 | if (id_field && bt_field_type_is_integer(id_field->type)) { |
19abc2c6 | 1373 | ret = set_integer_field_value(id_field, event_class_id); |
ac0c6bdd | 1374 | if (ret) { |
19abc2c6 PP |
1375 | BT_LOGW("Cannot set event header's `id` field's value: " |
1376 | "addr=%p, value=%" PRIu64, id_field, | |
1377 | event_class_id); | |
ac0c6bdd PP |
1378 | goto end; |
1379 | } | |
1380 | } | |
1381 | ||
1382 | /* | |
1383 | * The conditions to automatically set the timestamp are: | |
1384 | * | |
1385 | * 1. The event header field "timestamp" exists and is an | |
1386 | * integer field. | |
1387 | * 2. This stream's class has a registered clock (set with | |
50842bdc | 1388 | * bt_stream_class_set_clock()). |
2a03740b | 1389 | * 3. The "timestamp" field is not set. |
ac0c6bdd | 1390 | */ |
2a03740b PP |
1391 | timestamp_field = bt_field_structure_get_field_by_name( |
1392 | event->event_header, "timestamp"); | |
b3376dd9 | 1393 | if (timestamp_field && stream->stream_class->clock && |
2a03740b | 1394 | bt_field_type_is_integer(timestamp_field->type) && |
f6ccaed9 | 1395 | !bt_field_is_set_recursive(timestamp_field)) { |
ac0c6bdd | 1396 | mapped_clock_class = |
50842bdc | 1397 | bt_field_type_integer_get_mapped_clock_class( |
b3376dd9 | 1398 | timestamp_field->type); |
2a03740b | 1399 | if (mapped_clock_class) { |
ac0c6bdd PP |
1400 | uint64_t timestamp; |
1401 | ||
f6ccaed9 PP |
1402 | BT_ASSERT(mapped_clock_class == |
1403 | stream->stream_class->clock->clock_class); | |
ac0c6bdd PP |
1404 | ret = bt_ctf_clock_get_value( |
1405 | stream->stream_class->clock, | |
1406 | ×tamp); | |
f6ccaed9 | 1407 | BT_ASSERT(ret == 0); |
ac0c6bdd PP |
1408 | ret = set_integer_field_value(timestamp_field, |
1409 | timestamp); | |
1410 | if (ret) { | |
19abc2c6 PP |
1411 | BT_LOGW("Cannot set event header's `timestamp` field's value: " |
1412 | "addr=%p, value=%" PRIu64, | |
1413 | timestamp_field, timestamp); | |
ac0c6bdd PP |
1414 | goto end; |
1415 | } | |
1416 | } | |
1417 | } | |
1418 | ||
b3376dd9 | 1419 | BT_LOGV("Automatically populated event's header field: " |
19abc2c6 | 1420 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
50842bdc | 1421 | stream, bt_stream_get_name(stream), event); |
19abc2c6 | 1422 | |
ac0c6bdd PP |
1423 | end: |
1424 | bt_put(id_field); | |
1425 | bt_put(timestamp_field); | |
1426 | bt_put(mapped_clock_class); | |
1427 | return ret; | |
1428 | } | |
1429 | ||
50842bdc PP |
1430 | int bt_stream_append_event(struct bt_stream *stream, |
1431 | struct bt_event *event) | |
273b65be JG |
1432 | { |
1433 | int ret = 0; | |
273b65be | 1434 | |
19abc2c6 PP |
1435 | if (!stream) { |
1436 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1437 | ret = -1; | |
1438 | goto end; | |
1439 | } | |
1440 | ||
1441 | if (!event) { | |
1442 | BT_LOGW_STR("Invalid parameter: event is NULL."); | |
1443 | ret = -1; | |
1444 | goto end; | |
1445 | } | |
1446 | ||
1447 | if (stream->pos.fd < 0) { | |
1448 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
273b65be JG |
1449 | ret = -1; |
1450 | goto end; | |
1451 | } | |
1452 | ||
19abc2c6 PP |
1453 | BT_LOGV("Appending event to stream: " |
1454 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " | |
1455 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
50842bdc PP |
1456 | stream, bt_stream_get_name(stream), event, |
1457 | bt_event_class_get_name(bt_event_borrow_event_class(event)), | |
1458 | bt_event_class_get_id(bt_event_borrow_event_class(event))); | |
19abc2c6 | 1459 | |
fa29ba83 PP |
1460 | /* |
1461 | * The event is not supposed to have a parent stream at this | |
1462 | * point. The only other way an event can have a parent stream | |
1463 | * is if it was assigned when setting a packet to the event, | |
1464 | * in which case the packet's stream is not a writer stream, | |
1465 | * and thus the user is trying to append an event which belongs | |
1466 | * to another stream. | |
1467 | */ | |
1468 | if (event->base.parent) { | |
1469 | ret = -1; | |
1470 | goto end; | |
1471 | } | |
1472 | ||
e6a8e8e4 | 1473 | bt_object_set_parent(event, stream); |
19abc2c6 | 1474 | BT_LOGV_STR("Automatically populating the header of the event to append."); |
ac0c6bdd | 1475 | ret = auto_populate_event_header(stream, event); |
662e778c | 1476 | if (ret) { |
19abc2c6 | 1477 | /* auto_populate_event_header() reports errors */ |
37f30168 | 1478 | goto error; |
662e778c JG |
1479 | } |
1480 | ||
5fd2e9fd | 1481 | /* Make sure the various scopes of the event are set */ |
19abc2c6 | 1482 | BT_LOGV_STR("Validating event to append."); |
50842bdc | 1483 | ret = bt_event_validate(event); |
273b65be | 1484 | if (ret) { |
37f30168 | 1485 | goto error; |
273b65be JG |
1486 | } |
1487 | ||
0d688c15 | 1488 | /* Save the new event and freeze it */ |
19abc2c6 | 1489 | BT_LOGV_STR("Freezing the event to append."); |
50842bdc | 1490 | bt_event_freeze(event); |
273b65be | 1491 | g_ptr_array_add(stream->events, event); |
5fd2e9fd | 1492 | |
e6a8e8e4 JG |
1493 | /* |
1494 | * Event had to hold a reference to its event class as long as it wasn't | |
1495 | * part of the same trace hierarchy. From now on, the event and its | |
1496 | * class share the same lifetime guarantees and the reference is no | |
1497 | * longer needed. | |
1498 | */ | |
19abc2c6 | 1499 | BT_LOGV_STR("Putting the event's class."); |
e6a8e8e4 | 1500 | bt_put(event->event_class); |
19abc2c6 PP |
1501 | BT_LOGV("Appended event to stream: " |
1502 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " | |
1503 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
50842bdc PP |
1504 | stream, bt_stream_get_name(stream), event, |
1505 | bt_event_class_get_name(bt_event_borrow_event_class(event)), | |
1506 | bt_event_class_get_id(bt_event_borrow_event_class(event))); | |
37f30168 | 1507 | |
273b65be | 1508 | end: |
37f30168 PP |
1509 | return ret; |
1510 | ||
1511 | error: | |
1512 | /* | |
1513 | * Orphan the event; we were not successful in associating it to | |
1514 | * a stream. | |
1515 | */ | |
1516 | bt_object_set_parent(event, NULL); | |
1517 | ||
273b65be JG |
1518 | return ret; |
1519 | } | |
1520 | ||
50842bdc | 1521 | struct bt_field *bt_stream_get_packet_context(struct bt_stream *stream) |
12c8a1a3 | 1522 | { |
50842bdc | 1523 | struct bt_field *packet_context = NULL; |
12c8a1a3 | 1524 | |
19abc2c6 PP |
1525 | if (!stream) { |
1526 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1527 | goto end; | |
1528 | } | |
1529 | ||
1530 | if (stream->pos.fd < 0) { | |
1531 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
1532 | "stream-addr=%p, stream-name=\"%s\"", stream, | |
50842bdc | 1533 | bt_stream_get_name(stream)); |
12c8a1a3 JG |
1534 | goto end; |
1535 | } | |
1536 | ||
1537 | packet_context = stream->packet_context; | |
12c8a1a3 | 1538 | if (packet_context) { |
83509119 | 1539 | bt_get(packet_context); |
12c8a1a3 | 1540 | } |
34629a55 | 1541 | end: |
12c8a1a3 JG |
1542 | return packet_context; |
1543 | } | |
1544 | ||
50842bdc PP |
1545 | int bt_stream_set_packet_context(struct bt_stream *stream, |
1546 | struct bt_field *field) | |
12c8a1a3 JG |
1547 | { |
1548 | int ret = 0; | |
50842bdc | 1549 | struct bt_field_type *field_type; |
12c8a1a3 | 1550 | |
19abc2c6 PP |
1551 | if (!stream) { |
1552 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1553 | ret = -1; | |
1554 | goto end; | |
1555 | } | |
1556 | ||
1557 | if (stream->pos.fd < 0) { | |
1558 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
12c8a1a3 JG |
1559 | ret = -1; |
1560 | goto end; | |
1561 | } | |
1562 | ||
50842bdc PP |
1563 | field_type = bt_field_get_type(field); |
1564 | if (bt_field_type_compare(field_type, | |
09840de5 | 1565 | stream->stream_class->packet_context_type)) { |
19abc2c6 PP |
1566 | BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: " |
1567 | "stream-addr=%p, stream-name=\"%s\", " | |
1568 | "packet-context-field-addr=%p, " | |
1569 | "packet-context-ft-addr=%p", | |
50842bdc | 1570 | stream, bt_stream_get_name(stream), |
19abc2c6 | 1571 | field, field_type); |
12c8a1a3 JG |
1572 | ret = -1; |
1573 | goto end; | |
1574 | } | |
1575 | ||
83509119 | 1576 | bt_put(field_type); |
83509119 | 1577 | bt_put(stream->packet_context); |
835b2d10 | 1578 | stream->packet_context = bt_get(field); |
19abc2c6 PP |
1579 | BT_LOGV("Set stream's packet context field: " |
1580 | "stream-addr=%p, stream-name=\"%s\", " | |
1581 | "packet-context-field-addr=%p", | |
50842bdc | 1582 | stream, bt_stream_get_name(stream), field); |
12c8a1a3 JG |
1583 | end: |
1584 | return ret; | |
1585 | } | |
1586 | ||
50842bdc | 1587 | struct bt_field *bt_stream_get_packet_header(struct bt_stream *stream) |
263a7df5 | 1588 | { |
50842bdc | 1589 | struct bt_field *packet_header = NULL; |
263a7df5 | 1590 | |
19abc2c6 PP |
1591 | if (!stream) { |
1592 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1593 | goto end; | |
1594 | } | |
1595 | ||
1596 | if (stream->pos.fd < 0) { | |
1597 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
1598 | "stream-addr=%p, stream-name=\"%s\"", stream, | |
50842bdc | 1599 | bt_stream_get_name(stream)); |
263a7df5 JG |
1600 | goto end; |
1601 | } | |
1602 | ||
1603 | packet_header = stream->packet_header; | |
1604 | if (packet_header) { | |
83509119 | 1605 | bt_get(packet_header); |
263a7df5 JG |
1606 | } |
1607 | end: | |
1608 | return packet_header; | |
1609 | } | |
1610 | ||
50842bdc PP |
1611 | int bt_stream_set_packet_header(struct bt_stream *stream, |
1612 | struct bt_field *field) | |
263a7df5 JG |
1613 | { |
1614 | int ret = 0; | |
50842bdc PP |
1615 | struct bt_trace *trace = NULL; |
1616 | struct bt_field_type *field_type = NULL; | |
263a7df5 | 1617 | |
19abc2c6 PP |
1618 | if (!stream) { |
1619 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1620 | ret = -1; | |
1621 | goto end; | |
1622 | } | |
1623 | ||
1624 | if (stream->pos.fd < 0) { | |
1625 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
263a7df5 JG |
1626 | ret = -1; |
1627 | goto end; | |
1628 | } | |
1629 | ||
50842bdc | 1630 | trace = (struct bt_trace *) bt_object_get_parent(stream); |
b3376dd9 PP |
1631 | |
1632 | if (!field) { | |
1633 | if (trace->packet_header_type) { | |
1634 | BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: " | |
1635 | "stream-addr=%p, stream-name=\"%s\", " | |
1636 | "packet-header-field-addr=%p, " | |
1637 | "expected-ft-addr=%p", | |
50842bdc | 1638 | stream, bt_stream_get_name(stream), |
b3376dd9 PP |
1639 | field, trace->packet_header_type); |
1640 | ret = -1; | |
1641 | goto end; | |
1642 | } | |
1643 | ||
1644 | goto skip_validation; | |
1645 | } | |
1646 | ||
50842bdc | 1647 | field_type = bt_field_get_type(field); |
f6ccaed9 | 1648 | BT_ASSERT(field_type); |
b3376dd9 | 1649 | |
50842bdc | 1650 | if (bt_field_type_compare(field_type, trace->packet_header_type)) { |
19abc2c6 PP |
1651 | BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: " |
1652 | "stream-addr=%p, stream-name=\"%s\", " | |
1653 | "packet-header-field-addr=%p, " | |
1654 | "packet-header-ft-addr=%p", | |
50842bdc | 1655 | stream, bt_stream_get_name(stream), |
19abc2c6 | 1656 | field, field_type); |
263a7df5 JG |
1657 | ret = -1; |
1658 | goto end; | |
1659 | } | |
1660 | ||
b3376dd9 | 1661 | skip_validation: |
83509119 | 1662 | bt_put(stream->packet_header); |
835b2d10 | 1663 | stream->packet_header = bt_get(field); |
19abc2c6 PP |
1664 | BT_LOGV("Set stream's packet header field: " |
1665 | "stream-addr=%p, stream-name=\"%s\", " | |
1666 | "packet-header-field-addr=%p", | |
50842bdc | 1667 | stream, bt_stream_get_name(stream), field); |
263a7df5 | 1668 | end: |
e6a8e8e4 | 1669 | BT_PUT(trace); |
83509119 | 1670 | bt_put(field_type); |
263a7df5 JG |
1671 | return ret; |
1672 | } | |
1673 | ||
c9af50d1 | 1674 | static |
50842bdc | 1675 | void reset_structure_field(struct bt_field *structure, const char *name) |
c9af50d1 | 1676 | { |
50842bdc | 1677 | struct bt_field *member; |
c9af50d1 | 1678 | |
50842bdc | 1679 | member = bt_field_structure_get_field_by_name(structure, name); |
db1af8cc | 1680 | if (member) { |
f6ccaed9 | 1681 | (void) bt_field_reset_recursive(member); |
db1af8cc JD |
1682 | bt_put(member); |
1683 | } | |
c9af50d1 JG |
1684 | } |
1685 | ||
50842bdc | 1686 | int bt_stream_flush(struct bt_stream *stream) |
273b65be JG |
1687 | { |
1688 | int ret = 0; | |
1689 | size_t i; | |
50842bdc PP |
1690 | struct bt_stream_pos packet_context_pos; |
1691 | struct bt_trace *trace; | |
1692 | enum bt_byte_order native_byte_order; | |
1c1d572f | 1693 | bool has_packet_size = false; |
273b65be | 1694 | |
19abc2c6 PP |
1695 | if (!stream) { |
1696 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1697 | ret = -1; | |
f0ea9318 | 1698 | goto end_no_stream; |
19abc2c6 PP |
1699 | } |
1700 | ||
1701 | if (stream->pos.fd < 0) { | |
1702 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
273b65be JG |
1703 | ret = -1; |
1704 | goto end; | |
1705 | } | |
1706 | ||
1c1d572f | 1707 | if (stream->packet_context) { |
50842bdc | 1708 | struct bt_field *packet_size_field; |
b3376dd9 | 1709 | |
50842bdc | 1710 | packet_size_field = bt_field_structure_get_field_by_name( |
1c1d572f JD |
1711 | stream->packet_context, "packet_size"); |
1712 | has_packet_size = (packet_size_field != NULL); | |
1713 | bt_put(packet_size_field); | |
1714 | } | |
1715 | ||
1716 | if (stream->flushed_packet_count == 1) { | |
b3376dd9 PP |
1717 | if (!stream->packet_context) { |
1718 | BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once."); | |
1719 | ret = -1; | |
1720 | goto end; | |
1721 | } | |
1722 | ||
1c1d572f | 1723 | if (!has_packet_size) { |
b3376dd9 PP |
1724 | BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once."); |
1725 | ret = -1; | |
1726 | goto end; | |
1727 | } | |
664f50c8 JG |
1728 | } |
1729 | ||
19abc2c6 PP |
1730 | BT_LOGV("Flushing stream's current packet: stream-addr=%p, " |
1731 | "stream-name=\"%s\", packet-index=%u", stream, | |
50842bdc PP |
1732 | bt_stream_get_name(stream), stream->flushed_packet_count); |
1733 | trace = bt_stream_class_borrow_trace(stream->stream_class); | |
f6ccaed9 | 1734 | BT_ASSERT(trace); |
50842bdc | 1735 | native_byte_order = bt_trace_get_native_byte_order(trace); |
d246b111 | 1736 | |
b3376dd9 | 1737 | ret = auto_populate_packet_header(stream); |
d246b111 | 1738 | if (ret) { |
b3376dd9 PP |
1739 | BT_LOGW_STR("Cannot automatically populate the stream's packet header field."); |
1740 | ret = -1; | |
d246b111 | 1741 | goto end; |
273b65be JG |
1742 | } |
1743 | ||
2a03740b | 1744 | ret = auto_populate_packet_context(stream, true); |
b3376dd9 PP |
1745 | if (ret) { |
1746 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); | |
1747 | ret = -1; | |
1748 | goto end; | |
1749 | } | |
98edd02c | 1750 | |
b3376dd9 PP |
1751 | /* mmap the next packet */ |
1752 | BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64, | |
1753 | stream->pos.offset); | |
50842bdc | 1754 | bt_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR); |
f6ccaed9 | 1755 | BT_ASSERT(stream->pos.packet_size % 8 == 0); |
273b65be | 1756 | |
b3376dd9 PP |
1757 | if (stream->packet_header) { |
1758 | BT_LOGV_STR("Serializing packet header field."); | |
f6ccaed9 PP |
1759 | ret = bt_field_serialize_recursive(stream->packet_header, |
1760 | &stream->pos, native_byte_order); | |
b3376dd9 PP |
1761 | if (ret) { |
1762 | BT_LOGW("Cannot serialize stream's packet header field: " | |
1763 | "field-addr=%p", stream->packet_header); | |
98edd02c JG |
1764 | goto end; |
1765 | } | |
b3376dd9 | 1766 | } |
12c8a1a3 | 1767 | |
b3376dd9 | 1768 | if (stream->packet_context) { |
10cb7b41 JG |
1769 | /* Write packet context */ |
1770 | memcpy(&packet_context_pos, &stream->pos, | |
dc3fffef | 1771 | sizeof(packet_context_pos)); |
19abc2c6 | 1772 | BT_LOGV_STR("Serializing packet context field."); |
f6ccaed9 | 1773 | ret = bt_field_serialize_recursive(stream->packet_context, |
dc3fffef | 1774 | &stream->pos, native_byte_order); |
98edd02c | 1775 | if (ret) { |
b3376dd9 | 1776 | BT_LOGW("Cannot serialize stream's packet context field: " |
19abc2c6 | 1777 | "field-addr=%p", stream->packet_context); |
98edd02c JG |
1778 | goto end; |
1779 | } | |
12c8a1a3 JG |
1780 | } |
1781 | ||
19abc2c6 PP |
1782 | BT_LOGV("Serializing events: count=%u", stream->events->len); |
1783 | ||
273b65be | 1784 | for (i = 0; i < stream->events->len; i++) { |
50842bdc | 1785 | struct bt_event *event = g_ptr_array_index( |
273b65be | 1786 | stream->events, i); |
50842bdc PP |
1787 | struct bt_event_class *event_class = |
1788 | bt_event_borrow_event_class(event); | |
19abc2c6 | 1789 | |
b3376dd9 | 1790 | BT_LOGV("Serializing event: index=%zu, event-addr=%p, " |
19abc2c6 PP |
1791 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
1792 | "pos-offset=%" PRId64 ", packet-size=%" PRIu64, | |
50842bdc PP |
1793 | i, event, bt_event_class_get_name(event_class), |
1794 | bt_event_class_get_id(event_class), | |
19abc2c6 | 1795 | stream->pos.offset, stream->pos.packet_size); |
273b65be | 1796 | |
273b65be | 1797 | /* Write event header */ |
06b019a7 JD |
1798 | if (event->event_header) { |
1799 | BT_LOGV_STR("Serializing event's header field."); | |
f6ccaed9 | 1800 | ret = bt_field_serialize_recursive(event->event_header, |
06b019a7 JD |
1801 | &stream->pos, native_byte_order); |
1802 | if (ret) { | |
1803 | BT_LOGW("Cannot serialize event's header field: " | |
1804 | "field-addr=%p", event->event_header); | |
1805 | goto end; | |
1806 | } | |
273b65be JG |
1807 | } |
1808 | ||
8bfa3f9c | 1809 | /* Write stream event context */ |
5fd2e9fd | 1810 | if (event->stream_event_context) { |
19abc2c6 | 1811 | BT_LOGV_STR("Serializing event's stream event context field."); |
f6ccaed9 | 1812 | ret = bt_field_serialize_recursive( |
dc3fffef PP |
1813 | event->stream_event_context, &stream->pos, |
1814 | native_byte_order); | |
8bfa3f9c | 1815 | if (ret) { |
b3376dd9 | 1816 | BT_LOGW("Cannot serialize event's stream event context field: " |
19abc2c6 | 1817 | "field-addr=%p", event->stream_event_context); |
8bfa3f9c JG |
1818 | goto end; |
1819 | } | |
1820 | } | |
1821 | ||
273b65be | 1822 | /* Write event content */ |
50842bdc | 1823 | ret = bt_event_serialize(event, &stream->pos, |
dc3fffef | 1824 | native_byte_order); |
273b65be | 1825 | if (ret) { |
50842bdc | 1826 | /* bt_event_serialize() logs errors */ |
273b65be JG |
1827 | goto end; |
1828 | } | |
1829 | } | |
1830 | ||
1c1d572f JD |
1831 | if (!has_packet_size && stream->pos.offset % 8 != 0) { |
1832 | BT_LOGW("Stream's packet context field type has no `packet_size` field, " | |
1833 | "but current content size is not a multiple of 8 bits: " | |
1834 | "content-size=%" PRId64 ", " | |
1835 | "packet-size=%" PRIu64, | |
1836 | stream->pos.offset, | |
1837 | stream->pos.packet_size); | |
1838 | ret = -1; | |
1839 | goto end; | |
1840 | } | |
1841 | ||
f6ccaed9 | 1842 | BT_ASSERT(stream->pos.packet_size % 8 == 0); |
0686ef94 | 1843 | |
1c1d572f JD |
1844 | /* |
1845 | * Remove extra padding bytes. | |
1846 | */ | |
1847 | stream->pos.packet_size = (stream->pos.offset + 7) & ~7; | |
1848 | ||
98edd02c JG |
1849 | if (stream->packet_context) { |
1850 | /* | |
b3376dd9 PP |
1851 | * The whole packet is serialized at this point. Make sure that, |
1852 | * if `packet_size` is missing, the current content size is | |
1853 | * equal to the current packet size. | |
98edd02c | 1854 | */ |
50842bdc | 1855 | struct bt_field *field = bt_field_structure_get_field_by_name( |
b3376dd9 PP |
1856 | stream->packet_context, "content_size"); |
1857 | ||
1858 | bt_put(field); | |
1859 | if (!field) { | |
1860 | if (stream->pos.offset != stream->pos.packet_size) { | |
1861 | BT_LOGW("Stream's packet context's `content_size` field is missing, " | |
1862 | "but current packet's content size is not equal to its packet size: " | |
1863 | "content-size=%" PRId64 ", " | |
1864 | "packet-size=%" PRIu64, | |
1865 | stream->pos.offset, | |
1866 | stream->pos.packet_size); | |
1867 | ret = -1; | |
c9af50d1 JG |
1868 | goto end; |
1869 | } | |
98edd02c | 1870 | } |
273b65be | 1871 | |
b3376dd9 PP |
1872 | /* |
1873 | * Overwrite the packet context now that the stream | |
1874 | * position's packet and content sizes have the correct | |
1875 | * values. | |
1876 | * | |
1877 | * Copy base_mma as the packet may have been remapped | |
1878 | * (e.g. when a packet is resized). | |
1879 | */ | |
1880 | packet_context_pos.base_mma = stream->pos.base_mma; | |
2a03740b | 1881 | ret = auto_populate_packet_context(stream, false); |
b3376dd9 PP |
1882 | if (ret) { |
1883 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); | |
1884 | ret = -1; | |
1885 | goto end; | |
98edd02c | 1886 | } |
273b65be | 1887 | |
19abc2c6 | 1888 | BT_LOGV("Rewriting (serializing) packet context field."); |
f6ccaed9 | 1889 | ret = bt_field_serialize_recursive(stream->packet_context, |
dc3fffef | 1890 | &packet_context_pos, native_byte_order); |
98edd02c | 1891 | if (ret) { |
b3376dd9 | 1892 | BT_LOGW("Cannot serialize stream's packet context field: " |
19abc2c6 | 1893 | "field-addr=%p", stream->packet_context); |
98edd02c JG |
1894 | goto end; |
1895 | } | |
273b65be JG |
1896 | } |
1897 | ||
1898 | g_ptr_array_set_size(stream->events, 0); | |
1899 | stream->flushed_packet_count++; | |
b3376dd9 | 1900 | stream->size += stream->pos.packet_size / CHAR_BIT; |
1c1d572f | 1901 | |
273b65be | 1902 | end: |
c9af50d1 | 1903 | /* Reset automatically-set fields. */ |
1c1d572f JD |
1904 | if (stream->packet_context) { |
1905 | reset_structure_field(stream->packet_context, "timestamp_begin"); | |
1906 | reset_structure_field(stream->packet_context, "timestamp_end"); | |
1907 | reset_structure_field(stream->packet_context, "packet_size"); | |
1908 | reset_structure_field(stream->packet_context, "content_size"); | |
1909 | reset_structure_field(stream->packet_context, "events_discarded"); | |
1910 | } | |
cd7d8fb7 JG |
1911 | |
1912 | if (ret < 0) { | |
1913 | /* | |
1914 | * We failed to write the packet. Its size is therefore set to 0 | |
1915 | * to ensure the next mapping is done in the same place rather | |
1916 | * than advancing by "stream->pos.packet_size", which would | |
1917 | * leave a corrupted packet in the trace. | |
1918 | */ | |
1919 | stream->pos.packet_size = 0; | |
19abc2c6 | 1920 | } else { |
b3376dd9 PP |
1921 | BT_LOGV("Flushed stream's current packet: content-size=%" PRId64 ", " |
1922 | "packet-size=%" PRIu64, | |
1923 | stream->pos.offset, stream->pos.packet_size); | |
cd7d8fb7 | 1924 | } |
f0ea9318 MD |
1925 | |
1926 | end_no_stream: | |
273b65be JG |
1927 | return ret; |
1928 | } | |
1929 | ||
19abc2c6 | 1930 | /* Pre-2.0 CTF writer backward compatibility */ |
50842bdc | 1931 | void bt_ctf_stream_get(struct bt_stream *stream) |
273b65be | 1932 | { |
83509119 | 1933 | bt_get(stream); |
273b65be JG |
1934 | } |
1935 | ||
19abc2c6 | 1936 | /* Pre-2.0 CTF writer backward compatibility */ |
50842bdc | 1937 | void bt_ctf_stream_put(struct bt_stream *stream) |
273b65be | 1938 | { |
83509119 | 1939 | bt_put(stream); |
273b65be JG |
1940 | } |
1941 | ||
1942 | static | |
50842bdc | 1943 | void bt_stream_destroy(struct bt_object *obj) |
273b65be | 1944 | { |
50842bdc | 1945 | struct bt_stream *stream; |
3230ee6b | 1946 | int i; |
273b65be | 1947 | |
50842bdc | 1948 | stream = container_of(obj, struct bt_stream, base); |
19abc2c6 | 1949 | BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"", |
50842bdc | 1950 | stream, bt_stream_get_name(stream)); |
3230ee6b PP |
1951 | |
1952 | /* Call destroy listeners in reverse registration order */ | |
1953 | for (i = stream->destroy_listeners->len - 1; i >= 0; i--) { | |
50842bdc | 1954 | struct bt_stream_destroy_listener *listener = |
3230ee6b | 1955 | &g_array_index(stream->destroy_listeners, |
50842bdc | 1956 | struct bt_stream_destroy_listener, i); |
3230ee6b | 1957 | |
19abc2c6 PP |
1958 | BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d", |
1959 | listener->func, listener->data, i); | |
3230ee6b PP |
1960 | listener->func(stream, listener->data); |
1961 | } | |
1962 | ||
50842bdc | 1963 | (void) bt_stream_pos_fini(&stream->pos); |
0686ef94 JG |
1964 | if (stream->pos.fd >= 0) { |
1965 | int ret; | |
1966 | ||
1967 | /* | |
1968 | * Truncate the file's size to the minimum required to fit the | |
1969 | * last packet as we might have grown it too much on the last | |
1970 | * mmap. | |
1971 | */ | |
1972 | do { | |
1973 | ret = ftruncate(stream->pos.fd, stream->size); | |
1974 | } while (ret == -1 && errno == EINTR); | |
1975 | if (ret) { | |
c606e638 PP |
1976 | BT_LOGE_ERRNO("Failed to truncate stream file", |
1977 | ": ret=%d, size=%" PRIu64, | |
1978 | ret, (uint64_t) stream->size); | |
0686ef94 JG |
1979 | } |
1980 | ||
1981 | if (close(stream->pos.fd)) { | |
c606e638 PP |
1982 | BT_LOGE_ERRNO("Failed to close stream file", |
1983 | ": ret=%d", ret); | |
0686ef94 | 1984 | } |
9f56e450 | 1985 | } |
12c8a1a3 | 1986 | |
12c8a1a3 | 1987 | if (stream->events) { |
19abc2c6 | 1988 | BT_LOGD_STR("Putting events."); |
12c8a1a3 JG |
1989 | g_ptr_array_free(stream->events, TRUE); |
1990 | } | |
b71d7298 PP |
1991 | |
1992 | if (stream->name) { | |
1993 | g_string_free(stream->name, TRUE); | |
1994 | } | |
41ac640a | 1995 | |
3230ee6b PP |
1996 | if (stream->destroy_listeners) { |
1997 | g_array_free(stream->destroy_listeners, TRUE); | |
1998 | } | |
1999 | ||
19abc2c6 | 2000 | BT_LOGD_STR("Putting packet header field."); |
83509119 | 2001 | bt_put(stream->packet_header); |
19abc2c6 | 2002 | BT_LOGD_STR("Putting packet context field."); |
83509119 | 2003 | bt_put(stream->packet_context); |
273b65be JG |
2004 | g_free(stream); |
2005 | } | |
2006 | ||
273b65be | 2007 | static |
50842bdc | 2008 | int _set_structure_field_integer(struct bt_field *structure, char *name, |
c55a9f58 | 2009 | uint64_t value, bt_bool force) |
273b65be JG |
2010 | { |
2011 | int ret = 0; | |
50842bdc PP |
2012 | struct bt_field_type *field_type = NULL; |
2013 | struct bt_field *integer; | |
12c8a1a3 | 2014 | |
f6ccaed9 PP |
2015 | BT_ASSERT(structure); |
2016 | BT_ASSERT(name); | |
273b65be | 2017 | |
50842bdc | 2018 | integer = bt_field_structure_get_field_by_name(structure, name); |
12c8a1a3 JG |
2019 | if (!integer) { |
2020 | /* Field not found, not an error. */ | |
19abc2c6 PP |
2021 | BT_LOGV("Field not found: struct-field-addr=%p, " |
2022 | "name=\"%s\", force=%d", structure, name, force); | |
12c8a1a3 JG |
2023 | goto end; |
2024 | } | |
2025 | ||
2026 | /* Make sure the payload has not already been set. */ | |
f6ccaed9 | 2027 | if (!force && bt_field_is_set_recursive(integer)) { |
12c8a1a3 | 2028 | /* Payload already set, not an error */ |
19abc2c6 PP |
2029 | BT_LOGV("Field's payload is already set: struct-field-addr=%p, " |
2030 | "name=\"%s\", force=%d", structure, name, force); | |
12c8a1a3 JG |
2031 | goto end; |
2032 | } | |
2033 | ||
50842bdc | 2034 | field_type = bt_field_get_type(integer); |
f6ccaed9 | 2035 | BT_ASSERT(field_type); |
50842bdc | 2036 | if (bt_field_type_get_type_id(field_type) != BT_FIELD_TYPE_ID_INTEGER) { |
d7b1ea66 JG |
2037 | /* |
2038 | * The user most likely meant for us to populate this field | |
2039 | * automatically. However, we can only do this if the field | |
2040 | * is an integer. Return an error. | |
2041 | */ | |
19abc2c6 PP |
2042 | BT_LOGW("Invalid parameter: field's type is not an integer field type: " |
2043 | "field-addr=%p, ft-addr=%p, ft-id=%s", | |
2044 | integer, field_type, | |
50842bdc | 2045 | bt_field_type_id_string(field_type->id)); |
d7b1ea66 JG |
2046 | ret = -1; |
2047 | goto end; | |
2048 | } | |
2049 | ||
50842bdc PP |
2050 | if (bt_field_type_integer_is_signed(field_type)) { |
2051 | ret = bt_field_signed_integer_set_value(integer, | |
d7b1ea66 JG |
2052 | (int64_t) value); |
2053 | } else { | |
50842bdc | 2054 | ret = bt_field_unsigned_integer_set_value(integer, value); |
d7b1ea66 | 2055 | } |
af9296f3 | 2056 | ret = !ret ? 1 : ret; |
273b65be | 2057 | end: |
83509119 JG |
2058 | bt_put(integer); |
2059 | bt_put(field_type); | |
273b65be JG |
2060 | return ret; |
2061 | } | |
b71d7298 | 2062 | |
af9296f3 JG |
2063 | /* |
2064 | * Returns the following codes: | |
2065 | * 1 if the field was found and set, | |
2066 | * 0 if nothing was done (field not found, or was already set), | |
2067 | * <0 if an error was encoutered | |
2068 | */ | |
2069 | static | |
50842bdc | 2070 | int try_set_structure_field_integer(struct bt_field *structure, char *name, |
af9296f3 JG |
2071 | uint64_t value) |
2072 | { | |
c55a9f58 | 2073 | return _set_structure_field_integer(structure, name, value, BT_FALSE); |
af9296f3 JG |
2074 | } |
2075 | ||
50842bdc | 2076 | const char *bt_stream_get_name(struct bt_stream *stream) |
b71d7298 PP |
2077 | { |
2078 | const char *name = NULL; | |
2079 | ||
2080 | if (!stream) { | |
19abc2c6 | 2081 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
b71d7298 PP |
2082 | goto end; |
2083 | } | |
2084 | ||
2085 | name = stream->name ? stream->name->str : NULL; | |
2086 | ||
2087 | end: | |
2088 | return name; | |
2089 | } | |
98a4cbef | 2090 | |
50842bdc | 2091 | int bt_stream_is_writer(struct bt_stream *stream) |
98a4cbef PP |
2092 | { |
2093 | int ret = -1; | |
2094 | ||
2095 | if (!stream) { | |
19abc2c6 | 2096 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
98a4cbef PP |
2097 | goto end; |
2098 | } | |
2099 | ||
2100 | ret = (stream->pos.fd >= 0); | |
2101 | ||
2102 | end: | |
2103 | return ret; | |
2104 | } | |
3230ee6b | 2105 | |
3230ee6b | 2106 | BT_HIDDEN |
50842bdc PP |
2107 | void bt_stream_add_destroy_listener(struct bt_stream *stream, |
2108 | bt_stream_destroy_listener_func func, void *data) | |
3230ee6b | 2109 | { |
50842bdc | 2110 | struct bt_stream_destroy_listener listener; |
3230ee6b | 2111 | |
f6ccaed9 PP |
2112 | BT_ASSERT(stream); |
2113 | BT_ASSERT(func); | |
3230ee6b PP |
2114 | listener.func = func; |
2115 | listener.data = data; | |
2116 | g_array_append_val(stream->destroy_listeners, listener); | |
19abc2c6 PP |
2117 | BT_LOGV("Added stream destroy listener: stream-addr=%p, " |
2118 | "stream-name=\"%s\", func=%p, data=%p", | |
50842bdc | 2119 | stream, bt_stream_get_name(stream), func, data); |
3230ee6b PP |
2120 | } |
2121 | ||
2122 | BT_HIDDEN | |
50842bdc PP |
2123 | void bt_stream_remove_destroy_listener(struct bt_stream *stream, |
2124 | bt_stream_destroy_listener_func func, void *data) | |
3230ee6b PP |
2125 | { |
2126 | size_t i; | |
2127 | ||
f6ccaed9 PP |
2128 | BT_ASSERT(stream); |
2129 | BT_ASSERT(func); | |
3230ee6b PP |
2130 | |
2131 | for (i = 0; i < stream->destroy_listeners->len; i++) { | |
50842bdc | 2132 | struct bt_stream_destroy_listener *listener = |
3230ee6b | 2133 | &g_array_index(stream->destroy_listeners, |
50842bdc | 2134 | struct bt_stream_destroy_listener, i); |
3230ee6b PP |
2135 | |
2136 | if (listener->func == func && listener->data == data) { | |
2137 | g_array_remove_index(stream->destroy_listeners, i); | |
2138 | i--; | |
19abc2c6 PP |
2139 | BT_LOGV("Removed stream destroy listener: stream-addr=%p, " |
2140 | "stream-name=\"%s\", func=%p, data=%p", | |
50842bdc | 2141 | stream, bt_stream_get_name(stream), |
19abc2c6 | 2142 | func, data); |
3230ee6b PP |
2143 | } |
2144 | } | |
2145 | } | |
cfe11c58 | 2146 | |
50842bdc | 2147 | int64_t bt_stream_get_id(struct bt_stream *stream) |
cfe11c58 PP |
2148 | { |
2149 | int64_t ret; | |
2150 | ||
2151 | if (!stream) { | |
2152 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
2153 | ret = (int64_t) -1; | |
2154 | goto end; | |
2155 | } | |
2156 | ||
2157 | ret = stream->id; | |
2158 | if (ret < 0) { | |
2159 | BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"", | |
50842bdc | 2160 | stream, bt_stream_get_name(stream)); |
cfe11c58 PP |
2161 | } |
2162 | ||
2163 | end: | |
2164 | return ret; | |
2165 | } |