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