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 | ||
ac0c6bdd PP |
29 | #include <babeltrace/ctf-ir/clock-class.h> |
30 | #include <babeltrace/ctf-writer/clock.h> | |
31 | #include <babeltrace/ctf-writer/clock-internal.h> | |
273b65be | 32 | #include <babeltrace/ctf-writer/event.h> |
adc315b8 | 33 | #include <babeltrace/ctf-ir/event-internal.h> |
2e33ac5a PP |
34 | #include <babeltrace/ctf-ir/field-types-internal.h> |
35 | #include <babeltrace/ctf-ir/fields-internal.h> | |
3f043b05 JG |
36 | #include <babeltrace/ctf-ir/stream.h> |
37 | #include <babeltrace/ctf-ir/stream-internal.h> | |
adc315b8 | 38 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
319fd969 PP |
39 | #include <babeltrace/ctf-ir/trace-internal.h> |
40 | #include <babeltrace/ctf-writer/writer-internal.h> | |
3230ee6b | 41 | #include <babeltrace/graph/component-internal.h> |
83509119 | 42 | #include <babeltrace/ref.h> |
273b65be | 43 | #include <babeltrace/ctf-writer/functor-internal.h> |
3d9990ac PP |
44 | #include <babeltrace/compiler-internal.h> |
45 | #include <babeltrace/align-internal.h> | |
dc3fffef | 46 | #include <inttypes.h> |
273b65be JG |
47 | |
48 | static | |
83509119 | 49 | void bt_ctf_stream_destroy(struct bt_object *obj); |
273b65be | 50 | static |
af9296f3 JG |
51 | int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
52 | static | |
273b65be JG |
53 | int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
54 | ||
ac0c6bdd PP |
55 | static |
56 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) | |
57 | { | |
58 | int ret = 0; | |
59 | struct bt_ctf_field_type *field_type = NULL; | |
60 | ||
61 | if (!field) { | |
62 | ret = -1; | |
63 | goto end; | |
64 | } | |
65 | ||
66 | field_type = bt_ctf_field_get_type(field); | |
67 | assert(field_type); | |
68 | ||
69 | if (bt_ctf_field_type_get_type_id(field_type) != | |
1487a16a | 70 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
ac0c6bdd PP |
71 | /* Not an integer and the value is unset, error. */ |
72 | ret = -1; | |
73 | goto end; | |
74 | } | |
75 | ||
76 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
77 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); | |
78 | if (ret) { | |
79 | /* Value is out of range, error. */ | |
80 | goto end; | |
81 | } | |
82 | } else { | |
83 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); | |
84 | if (ret) { | |
85 | /* Value is out of range, error. */ | |
86 | goto end; | |
87 | } | |
88 | } | |
89 | end: | |
90 | bt_put(field_type); | |
91 | return ret; | |
92 | } | |
93 | ||
d246b111 JG |
94 | static |
95 | int set_packet_header_magic(struct bt_ctf_stream *stream) | |
96 | { | |
97 | int ret = 0; | |
98 | struct bt_ctf_field_type *magic_field_type = NULL; | |
99 | struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field( | |
100 | stream->packet_header, "magic"); | |
101 | ||
102 | if (!magic_field) { | |
103 | /* No magic field found. Not an error, skip. */ | |
104 | goto end; | |
105 | } | |
106 | ||
fc47320a | 107 | if (bt_ctf_field_is_set(magic_field)) { |
d246b111 JG |
108 | /* Value already set. Not an error, skip. */ |
109 | goto end; | |
110 | } | |
111 | ||
112 | magic_field_type = bt_ctf_field_get_type(magic_field); | |
113 | assert(magic_field_type); | |
114 | ||
115 | if (bt_ctf_field_type_get_type_id(magic_field_type) != | |
1487a16a | 116 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d246b111 JG |
117 | /* Magic field is not an integer. Not an error, skip. */ |
118 | goto end; | |
119 | } | |
120 | ||
121 | if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) { | |
122 | /* | |
123 | * Magic field is not of the expected size. | |
124 | * Not an error, skip. | |
125 | */ | |
126 | goto end; | |
127 | } | |
128 | ||
129 | ret = bt_ctf_field_type_integer_get_signed(magic_field_type); | |
130 | assert(ret >= 0); | |
131 | if (ret) { | |
132 | ret = bt_ctf_field_signed_integer_set_value(magic_field, | |
133 | (int64_t) 0xC1FC1FC1); | |
134 | } else { | |
135 | ret = bt_ctf_field_unsigned_integer_set_value(magic_field, | |
136 | (uint64_t) 0xC1FC1FC1); | |
137 | } | |
138 | end: | |
83509119 JG |
139 | bt_put(magic_field); |
140 | bt_put(magic_field_type); | |
d246b111 JG |
141 | return ret; |
142 | } | |
143 | ||
144 | static | |
145 | int set_packet_header_uuid(struct bt_ctf_stream *stream) | |
146 | { | |
147 | int i, ret = 0; | |
e6a8e8e4 | 148 | struct bt_ctf_trace *trace = NULL; |
d246b111 JG |
149 | struct bt_ctf_field_type *uuid_field_type = NULL; |
150 | struct bt_ctf_field_type *element_field_type = NULL; | |
151 | struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field( | |
152 | stream->packet_header, "uuid"); | |
153 | ||
154 | if (!uuid_field) { | |
155 | /* No uuid field found. Not an error, skip. */ | |
156 | goto end; | |
157 | } | |
158 | ||
fc47320a | 159 | if (bt_ctf_field_is_set(uuid_field)) { |
d246b111 JG |
160 | /* Value already set. Not an error, skip. */ |
161 | goto end; | |
162 | } | |
163 | ||
164 | uuid_field_type = bt_ctf_field_get_type(uuid_field); | |
165 | assert(uuid_field_type); | |
166 | if (bt_ctf_field_type_get_type_id(uuid_field_type) != | |
1487a16a | 167 | BT_CTF_FIELD_TYPE_ID_ARRAY) { |
d246b111 JG |
168 | /* UUID field is not an array. Not an error, skip. */ |
169 | goto end; | |
170 | } | |
171 | ||
172 | if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) { | |
173 | /* | |
174 | * UUID field is not of the expected size. | |
175 | * Not an error, skip. | |
176 | */ | |
177 | goto end; | |
178 | } | |
179 | ||
180 | element_field_type = bt_ctf_field_type_array_get_element_type( | |
181 | uuid_field_type); | |
182 | assert(element_field_type); | |
183 | if (bt_ctf_field_type_get_type_id(element_field_type) != | |
1487a16a | 184 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d246b111 JG |
185 | /* UUID array elements are not integers. Not an error, skip */ |
186 | goto end; | |
187 | } | |
188 | ||
e6a8e8e4 | 189 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
d246b111 JG |
190 | for (i = 0; i < 16; i++) { |
191 | struct bt_ctf_field *uuid_element = | |
192 | bt_ctf_field_array_get_field(uuid_field, i); | |
193 | ||
194 | ret = bt_ctf_field_type_integer_get_signed(element_field_type); | |
195 | assert(ret >= 0); | |
196 | ||
197 | if (ret) { | |
198 | ret = bt_ctf_field_signed_integer_set_value( | |
e6a8e8e4 | 199 | uuid_element, (int64_t) trace->uuid[i]); |
d246b111 JG |
200 | } else { |
201 | ret = bt_ctf_field_unsigned_integer_set_value( | |
202 | uuid_element, | |
e6a8e8e4 | 203 | (uint64_t) trace->uuid[i]); |
d246b111 | 204 | } |
83509119 | 205 | bt_put(uuid_element); |
d246b111 JG |
206 | if (ret) { |
207 | goto end; | |
208 | } | |
209 | } | |
210 | ||
211 | end: | |
83509119 JG |
212 | bt_put(uuid_field); |
213 | bt_put(uuid_field_type); | |
214 | bt_put(element_field_type); | |
e6a8e8e4 | 215 | BT_PUT(trace); |
d246b111 JG |
216 | return ret; |
217 | } | |
218 | static | |
219 | int set_packet_header_stream_id(struct bt_ctf_stream *stream) | |
220 | { | |
221 | int ret = 0; | |
222 | uint32_t stream_id; | |
223 | struct bt_ctf_field_type *stream_id_field_type = NULL; | |
224 | struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field( | |
225 | stream->packet_header, "stream_id"); | |
226 | ||
227 | if (!stream_id_field) { | |
228 | /* No stream_id field found. Not an error, skip. */ | |
229 | goto end; | |
230 | } | |
231 | ||
fc47320a | 232 | if (bt_ctf_field_is_set(stream_id_field)) { |
d246b111 JG |
233 | /* Value already set. Not an error, skip. */ |
234 | goto end; | |
235 | } | |
236 | ||
237 | stream_id_field_type = bt_ctf_field_get_type(stream_id_field); | |
238 | assert(stream_id_field_type); | |
239 | if (bt_ctf_field_type_get_type_id(stream_id_field_type) != | |
1487a16a | 240 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d246b111 JG |
241 | /* stream_id field is not an integer. Not an error, skip. */ |
242 | goto end; | |
243 | } | |
244 | ||
245 | stream_id = stream->stream_class->id; | |
246 | ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type); | |
247 | assert(ret >= 0); | |
248 | if (ret) { | |
249 | ret = bt_ctf_field_signed_integer_set_value(stream_id_field, | |
250 | (int64_t) stream_id); | |
251 | } else { | |
252 | ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field, | |
253 | (uint64_t) stream_id); | |
254 | } | |
255 | end: | |
83509119 JG |
256 | bt_put(stream_id_field); |
257 | bt_put(stream_id_field_type); | |
d246b111 JG |
258 | return ret; |
259 | } | |
260 | ||
261 | static | |
262 | int set_packet_header(struct bt_ctf_stream *stream) | |
263 | { | |
264 | int ret; | |
265 | ||
266 | ret = set_packet_header_magic(stream); | |
267 | if (ret) { | |
268 | goto end; | |
269 | } | |
270 | ||
271 | ret = set_packet_header_uuid(stream); | |
272 | if (ret) { | |
273 | goto end; | |
274 | } | |
275 | ||
276 | ret = set_packet_header_stream_id(stream); | |
277 | if (ret) { | |
278 | goto end; | |
279 | } | |
280 | end: | |
281 | return ret; | |
282 | } | |
283 | ||
123fbdec | 284 | static |
e6a8e8e4 | 285 | void release_event(struct bt_ctf_event *event) |
123fbdec | 286 | { |
e6a8e8e4 JG |
287 | if (bt_object_get_ref_count(event)) { |
288 | /* | |
289 | * The event is being orphaned, but it must guarantee the | |
290 | * existence of its event class for the duration of its | |
291 | * lifetime. | |
292 | */ | |
293 | bt_get(event->event_class); | |
294 | BT_PUT(event->base.parent); | |
295 | } else { | |
296 | bt_object_release(event); | |
297 | } | |
123fbdec JG |
298 | } |
299 | ||
319fd969 PP |
300 | static |
301 | int create_stream_file(struct bt_ctf_writer *writer, | |
302 | struct bt_ctf_stream *stream) | |
303 | { | |
304 | int fd; | |
305 | GString *filename = g_string_new(stream->stream_class->name->str); | |
306 | ||
307 | if (stream->stream_class->name->len == 0) { | |
308 | int64_t ret; | |
309 | ||
310 | ret = bt_ctf_stream_class_get_id(stream->stream_class); | |
311 | if (ret < 0) { | |
312 | fd = -1; | |
313 | goto error; | |
314 | } | |
315 | ||
316 | g_string_printf(filename, "stream_%" PRId64, ret); | |
317 | } | |
318 | ||
319 | g_string_append_printf(filename, "_%" PRIu32, stream->id); | |
320 | fd = openat(writer->trace_dir_fd, filename->str, | |
321 | O_RDWR | O_CREAT | O_TRUNC, | |
322 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
323 | error: | |
324 | g_string_free(filename, TRUE); | |
325 | return fd; | |
326 | } | |
327 | ||
328 | static | |
329 | int set_stream_fd(struct bt_ctf_stream *stream, int fd) | |
330 | { | |
331 | int ret = 0; | |
332 | ||
333 | if (stream->pos.fd != -1) { | |
334 | ret = -1; | |
335 | goto end; | |
336 | } | |
337 | ||
dc3fffef | 338 | (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR); |
319fd969 PP |
339 | stream->pos.fd = fd; |
340 | end: | |
341 | return ret; | |
342 | } | |
343 | ||
3230ee6b PP |
344 | static |
345 | void component_destroy_listener(struct bt_component *component, void *data) | |
346 | { | |
347 | struct bt_ctf_stream *stream = data; | |
348 | ||
349 | g_hash_table_remove(stream->comp_cur_port, component); | |
350 | } | |
351 | ||
273b65be | 352 | struct bt_ctf_stream *bt_ctf_stream_create( |
b71d7298 PP |
353 | struct bt_ctf_stream_class *stream_class, |
354 | const char *name) | |
273b65be | 355 | { |
12c8a1a3 | 356 | int ret; |
273b65be | 357 | struct bt_ctf_stream *stream = NULL; |
319fd969 PP |
358 | struct bt_ctf_trace *trace = NULL; |
359 | struct bt_ctf_writer *writer = NULL; | |
273b65be | 360 | |
319fd969 | 361 | if (!stream_class) { |
b71d7298 | 362 | goto error; |
319fd969 PP |
363 | } |
364 | ||
365 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
366 | if (!trace) { | |
b71d7298 | 367 | goto error; |
273b65be JG |
368 | } |
369 | ||
370 | stream = g_new0(struct bt_ctf_stream, 1); | |
371 | if (!stream) { | |
b71d7298 | 372 | goto error; |
273b65be JG |
373 | } |
374 | ||
83509119 | 375 | bt_object_init(stream, bt_ctf_stream_destroy); |
e6a8e8e4 JG |
376 | /* |
377 | * Acquire reference to parent since stream will become publicly | |
378 | * reachable; it needs its parent to remain valid. | |
379 | */ | |
380 | bt_object_set_parent(stream, trace); | |
273b65be JG |
381 | stream->id = stream_class->next_stream_id++; |
382 | stream->stream_class = stream_class; | |
319fd969 | 383 | stream->pos.fd = -1; |
d246b111 | 384 | |
3230ee6b PP |
385 | stream->destroy_listeners = g_array_new(FALSE, TRUE, |
386 | sizeof(struct bt_ctf_stream_destroy_listener)); | |
387 | if (!stream->destroy_listeners) { | |
388 | goto error; | |
389 | } | |
390 | ||
b71d7298 PP |
391 | if (name) { |
392 | stream->name = g_string_new(name); | |
393 | if (!stream->name) { | |
394 | goto error; | |
395 | } | |
396 | } | |
397 | ||
319fd969 PP |
398 | if (trace->is_created_by_writer) { |
399 | int fd; | |
400 | writer = (struct bt_ctf_writer *) | |
401 | bt_object_get_parent(trace); | |
402 | ||
403 | assert(writer); | |
98edd02c JG |
404 | if (stream_class->packet_context_type) { |
405 | stream->packet_context = bt_ctf_field_create( | |
406 | stream_class->packet_context_type); | |
407 | if (!stream->packet_context) { | |
408 | goto error; | |
409 | } | |
319fd969 | 410 | |
98edd02c | 411 | /* Initialize events_discarded */ |
af9296f3 JG |
412 | ret = try_set_structure_field_integer( |
413 | stream->packet_context, | |
98edd02c | 414 | "events_discarded", 0); |
af9296f3 | 415 | if (ret != 1) { |
98edd02c JG |
416 | goto error; |
417 | } | |
319fd969 PP |
418 | } |
419 | ||
420 | stream->events = g_ptr_array_new_with_free_func( | |
421 | (GDestroyNotify) release_event); | |
422 | if (!stream->events) { | |
423 | goto error; | |
424 | } | |
425 | ||
426 | /* A trace is not allowed to have a NULL packet header */ | |
427 | assert(trace->packet_header_type); | |
428 | stream->packet_header = | |
429 | bt_ctf_field_create(trace->packet_header_type); | |
430 | if (!stream->packet_header) { | |
431 | goto error; | |
432 | } | |
433 | ||
434 | /* | |
435 | * Attempt to populate the default trace packet header fields | |
436 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
437 | * fields not be found or be of an incompatible type; they will | |
438 | * simply not be populated automatically. The user will have to | |
439 | * make sure to set the trace packet header fields himself | |
440 | * before flushing. | |
441 | */ | |
442 | ret = set_packet_header(stream); | |
443 | if (ret) { | |
444 | goto error; | |
445 | } | |
446 | ||
447 | /* Create file associated with this stream */ | |
448 | fd = create_stream_file(writer, stream); | |
449 | if (fd < 0) { | |
450 | goto error; | |
451 | } | |
452 | ||
453 | ret = set_stream_fd(stream, fd); | |
454 | if (ret) { | |
455 | goto error; | |
456 | } | |
457 | ||
458 | /* Freeze the writer */ | |
459 | bt_ctf_writer_freeze(writer); | |
460 | } else { | |
461 | /* Non-writer stream indicated by a negative FD */ | |
462 | ret = set_stream_fd(stream, -1); | |
463 | if (ret) { | |
464 | goto error; | |
465 | } | |
3230ee6b PP |
466 | |
467 | stream->comp_cur_port = g_hash_table_new(g_direct_hash, | |
468 | g_direct_equal); | |
469 | if (!stream->comp_cur_port) { | |
470 | goto error; | |
471 | } | |
d246b111 | 472 | } |
319fd969 PP |
473 | |
474 | /* Add this stream to the trace's streams */ | |
475 | g_ptr_array_add(trace->streams, stream); | |
476 | ||
319fd969 PP |
477 | BT_PUT(trace); |
478 | BT_PUT(writer); | |
273b65be | 479 | return stream; |
83509119 JG |
480 | error: |
481 | BT_PUT(stream); | |
319fd969 PP |
482 | BT_PUT(trace); |
483 | BT_PUT(writer); | |
83509119 | 484 | return stream; |
273b65be JG |
485 | } |
486 | ||
3baf0856 JG |
487 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( |
488 | struct bt_ctf_stream *stream) | |
489 | { | |
490 | struct bt_ctf_stream_class *stream_class = NULL; | |
491 | ||
492 | if (!stream) { | |
493 | goto end; | |
494 | } | |
495 | ||
496 | stream_class = stream->stream_class; | |
83509119 | 497 | bt_get(stream_class); |
3baf0856 JG |
498 | end: |
499 | return stream_class; | |
500 | } | |
501 | ||
a78a2e25 JG |
502 | int bt_ctf_stream_get_discarded_events_count( |
503 | struct bt_ctf_stream *stream, uint64_t *count) | |
504 | { | |
505 | int64_t ret = 0; | |
12c8a1a3 JG |
506 | int field_signed; |
507 | struct bt_ctf_field *events_discarded_field = NULL; | |
508 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
a78a2e25 | 509 | |
1579cde5 PP |
510 | if (!stream || !count || !stream->packet_context || |
511 | stream->pos.fd < 0) { | |
a78a2e25 JG |
512 | ret = -1; |
513 | goto end; | |
514 | } | |
515 | ||
12c8a1a3 JG |
516 | events_discarded_field = bt_ctf_field_structure_get_field( |
517 | stream->packet_context, "events_discarded"); | |
518 | if (!events_discarded_field) { | |
519 | ret = -1; | |
520 | goto end; | |
521 | } | |
522 | ||
523 | events_discarded_field_type = bt_ctf_field_get_type( | |
524 | events_discarded_field); | |
525 | if (!events_discarded_field_type) { | |
526 | ret = -1; | |
527 | goto end; | |
528 | } | |
529 | ||
530 | field_signed = bt_ctf_field_type_integer_get_signed( | |
531 | events_discarded_field_type); | |
532 | if (field_signed < 0) { | |
533 | ret = field_signed; | |
534 | goto end; | |
535 | } | |
536 | ||
537 | if (field_signed) { | |
538 | int64_t signed_count; | |
539 | ||
540 | ret = bt_ctf_field_signed_integer_get_value( | |
541 | events_discarded_field, &signed_count); | |
542 | if (ret) { | |
543 | goto end; | |
544 | } | |
545 | if (signed_count < 0) { | |
546 | /* Invalid value */ | |
547 | ret = -1; | |
548 | goto end; | |
549 | } | |
550 | *count = (uint64_t) signed_count; | |
551 | } else { | |
552 | ret = bt_ctf_field_unsigned_integer_get_value( | |
553 | events_discarded_field, count); | |
554 | if (ret) { | |
555 | goto end; | |
556 | } | |
557 | } | |
a78a2e25 | 558 | end: |
83509119 JG |
559 | bt_put(events_discarded_field); |
560 | bt_put(events_discarded_field_type); | |
a78a2e25 JG |
561 | return ret; |
562 | } | |
563 | ||
273b65be JG |
564 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
565 | uint64_t event_count) | |
566 | { | |
12c8a1a3 JG |
567 | int ret; |
568 | int field_signed; | |
569 | uint64_t previous_count; | |
570 | uint64_t new_count; | |
571 | struct bt_ctf_field *events_discarded_field = NULL; | |
572 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
573 | ||
1579cde5 | 574 | if (!stream || !stream->packet_context || stream->pos.fd < 0) { |
12c8a1a3 JG |
575 | goto end; |
576 | } | |
577 | ||
578 | ret = bt_ctf_stream_get_discarded_events_count(stream, | |
579 | &previous_count); | |
580 | if (ret) { | |
581 | goto end; | |
582 | } | |
583 | ||
584 | events_discarded_field = bt_ctf_field_structure_get_field( | |
585 | stream->packet_context, "events_discarded"); | |
586 | if (!events_discarded_field) { | |
587 | goto end; | |
588 | } | |
589 | ||
590 | events_discarded_field_type = bt_ctf_field_get_type( | |
591 | events_discarded_field); | |
592 | if (!events_discarded_field_type) { | |
593 | goto end; | |
594 | } | |
595 | ||
596 | field_signed = bt_ctf_field_type_integer_get_signed( | |
597 | events_discarded_field_type); | |
598 | if (field_signed < 0) { | |
599 | goto end; | |
273b65be JG |
600 | } |
601 | ||
12c8a1a3 JG |
602 | new_count = previous_count + event_count; |
603 | if (field_signed) { | |
604 | ret = bt_ctf_field_signed_integer_set_value( | |
605 | events_discarded_field, (int64_t) new_count); | |
606 | if (ret) { | |
607 | goto end; | |
608 | } | |
609 | } else { | |
610 | ret = bt_ctf_field_unsigned_integer_set_value( | |
611 | events_discarded_field, new_count); | |
612 | if (ret) { | |
613 | goto end; | |
614 | } | |
615 | } | |
616 | ||
617 | end: | |
83509119 JG |
618 | bt_put(events_discarded_field); |
619 | bt_put(events_discarded_field_type); | |
273b65be JG |
620 | } |
621 | ||
ac0c6bdd PP |
622 | static int auto_populate_event_header(struct bt_ctf_stream *stream, |
623 | struct bt_ctf_event *event) | |
624 | { | |
625 | int ret = 0; | |
626 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; | |
627 | struct bt_ctf_clock_class *mapped_clock_class = NULL; | |
628 | ||
629 | if (!event || event->frozen) { | |
630 | ret = -1; | |
631 | goto end; | |
632 | } | |
633 | ||
634 | /* | |
635 | * The condition to automatically set the ID are: | |
636 | * | |
637 | * 1. The event header field "id" exists and is an integer | |
638 | * field. | |
639 | * 2. The event header field "id" is NOT set. | |
640 | */ | |
641 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); | |
642 | if (id_field && !bt_ctf_field_is_set(id_field)) { | |
643 | ret = set_integer_field_value(id_field, | |
644 | (uint64_t) bt_ctf_event_class_get_id( | |
645 | event->event_class)); | |
646 | if (ret) { | |
647 | goto end; | |
648 | } | |
649 | } | |
650 | ||
651 | /* | |
652 | * The conditions to automatically set the timestamp are: | |
653 | * | |
654 | * 1. The event header field "timestamp" exists and is an | |
655 | * integer field. | |
656 | * 2. This stream's class has a registered clock (set with | |
657 | * bt_ctf_stream_class_set_clock()). | |
658 | * 3. The event header field "timestamp" has its type mapped to | |
659 | * a clock class which is also the clock class of this | |
660 | * stream's class's registered clock. | |
661 | * 4. The event header field "timestamp" is NOT set. | |
662 | */ | |
663 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, | |
664 | "timestamp"); | |
665 | if (timestamp_field && !bt_ctf_field_is_set(timestamp_field) && | |
666 | stream->stream_class->clock) { | |
667 | struct bt_ctf_clock_class *stream_class_clock_class = | |
668 | stream->stream_class->clock->clock_class; | |
669 | struct bt_ctf_field_type *timestamp_field_type = | |
670 | bt_ctf_field_get_type(timestamp_field); | |
671 | ||
672 | assert(timestamp_field_type); | |
673 | mapped_clock_class = | |
674 | bt_ctf_field_type_integer_get_mapped_clock_class( | |
675 | timestamp_field_type); | |
676 | BT_PUT(timestamp_field_type); | |
677 | if (mapped_clock_class == stream_class_clock_class) { | |
678 | uint64_t timestamp; | |
679 | ||
680 | ret = bt_ctf_clock_get_value( | |
681 | stream->stream_class->clock, | |
682 | ×tamp); | |
683 | if (ret) { | |
684 | goto end; | |
685 | } | |
686 | ||
687 | ret = set_integer_field_value(timestamp_field, | |
688 | timestamp); | |
689 | if (ret) { | |
690 | goto end; | |
691 | } | |
692 | } | |
693 | } | |
694 | ||
695 | end: | |
696 | bt_put(id_field); | |
697 | bt_put(timestamp_field); | |
698 | bt_put(mapped_clock_class); | |
699 | return ret; | |
700 | } | |
701 | ||
273b65be JG |
702 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, |
703 | struct bt_ctf_event *event) | |
704 | { | |
705 | int ret = 0; | |
273b65be | 706 | |
1579cde5 | 707 | if (!stream || !event || stream->pos.fd < 0) { |
273b65be JG |
708 | ret = -1; |
709 | goto end; | |
710 | } | |
711 | ||
fa29ba83 PP |
712 | /* |
713 | * The event is not supposed to have a parent stream at this | |
714 | * point. The only other way an event can have a parent stream | |
715 | * is if it was assigned when setting a packet to the event, | |
716 | * in which case the packet's stream is not a writer stream, | |
717 | * and thus the user is trying to append an event which belongs | |
718 | * to another stream. | |
719 | */ | |
720 | if (event->base.parent) { | |
721 | ret = -1; | |
722 | goto end; | |
723 | } | |
724 | ||
e6a8e8e4 | 725 | bt_object_set_parent(event, stream); |
ac0c6bdd | 726 | ret = auto_populate_event_header(stream, event); |
662e778c | 727 | if (ret) { |
37f30168 | 728 | goto error; |
662e778c JG |
729 | } |
730 | ||
5fd2e9fd | 731 | /* Make sure the various scopes of the event are set */ |
273b65be JG |
732 | ret = bt_ctf_event_validate(event); |
733 | if (ret) { | |
37f30168 | 734 | goto error; |
273b65be JG |
735 | } |
736 | ||
0d688c15 PP |
737 | /* Save the new event and freeze it */ |
738 | bt_ctf_event_freeze(event); | |
273b65be | 739 | g_ptr_array_add(stream->events, event); |
5fd2e9fd | 740 | |
e6a8e8e4 JG |
741 | /* |
742 | * Event had to hold a reference to its event class as long as it wasn't | |
743 | * part of the same trace hierarchy. From now on, the event and its | |
744 | * class share the same lifetime guarantees and the reference is no | |
745 | * longer needed. | |
746 | */ | |
747 | bt_put(event->event_class); | |
37f30168 | 748 | |
273b65be | 749 | end: |
37f30168 PP |
750 | return ret; |
751 | ||
752 | error: | |
753 | /* | |
754 | * Orphan the event; we were not successful in associating it to | |
755 | * a stream. | |
756 | */ | |
757 | bt_object_set_parent(event, NULL); | |
758 | ||
273b65be JG |
759 | return ret; |
760 | } | |
761 | ||
12c8a1a3 JG |
762 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( |
763 | struct bt_ctf_stream *stream) | |
764 | { | |
765 | struct bt_ctf_field *packet_context = NULL; | |
766 | ||
1579cde5 | 767 | if (!stream || stream->pos.fd < 0) { |
12c8a1a3 JG |
768 | goto end; |
769 | } | |
770 | ||
771 | packet_context = stream->packet_context; | |
12c8a1a3 | 772 | if (packet_context) { |
83509119 | 773 | bt_get(packet_context); |
12c8a1a3 | 774 | } |
34629a55 | 775 | end: |
12c8a1a3 JG |
776 | return packet_context; |
777 | } | |
778 | ||
779 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, | |
780 | struct bt_ctf_field *field) | |
781 | { | |
782 | int ret = 0; | |
783 | struct bt_ctf_field_type *field_type; | |
784 | ||
835b2d10 | 785 | if (!stream || stream->pos.fd < 0) { |
12c8a1a3 JG |
786 | ret = -1; |
787 | goto end; | |
788 | } | |
789 | ||
790 | field_type = bt_ctf_field_get_type(field); | |
09840de5 PP |
791 | if (bt_ctf_field_type_compare(field_type, |
792 | stream->stream_class->packet_context_type)) { | |
12c8a1a3 JG |
793 | ret = -1; |
794 | goto end; | |
795 | } | |
796 | ||
83509119 | 797 | bt_put(field_type); |
83509119 | 798 | bt_put(stream->packet_context); |
835b2d10 | 799 | stream->packet_context = bt_get(field); |
12c8a1a3 JG |
800 | end: |
801 | return ret; | |
802 | } | |
803 | ||
263a7df5 JG |
804 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
805 | struct bt_ctf_stream *stream) | |
806 | { | |
807 | struct bt_ctf_field *packet_header = NULL; | |
808 | ||
1579cde5 | 809 | if (!stream || stream->pos.fd < 0) { |
263a7df5 JG |
810 | goto end; |
811 | } | |
812 | ||
813 | packet_header = stream->packet_header; | |
814 | if (packet_header) { | |
83509119 | 815 | bt_get(packet_header); |
263a7df5 JG |
816 | } |
817 | end: | |
818 | return packet_header; | |
819 | } | |
820 | ||
821 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, | |
822 | struct bt_ctf_field *field) | |
823 | { | |
824 | int ret = 0; | |
e6a8e8e4 | 825 | struct bt_ctf_trace *trace = NULL; |
263a7df5 JG |
826 | struct bt_ctf_field_type *field_type = NULL; |
827 | ||
835b2d10 | 828 | if (!stream || stream->pos.fd < 0) { |
263a7df5 JG |
829 | ret = -1; |
830 | goto end; | |
831 | } | |
832 | ||
e6a8e8e4 | 833 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
263a7df5 | 834 | field_type = bt_ctf_field_get_type(field); |
09840de5 | 835 | if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) { |
263a7df5 JG |
836 | ret = -1; |
837 | goto end; | |
838 | } | |
839 | ||
83509119 | 840 | bt_put(stream->packet_header); |
835b2d10 | 841 | stream->packet_header = bt_get(field); |
263a7df5 | 842 | end: |
e6a8e8e4 | 843 | BT_PUT(trace); |
83509119 | 844 | bt_put(field_type); |
263a7df5 JG |
845 | return ret; |
846 | } | |
847 | ||
9a220c32 JG |
848 | static |
849 | int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp) | |
850 | { | |
851 | int ret = 0; | |
852 | struct bt_ctf_field *timestamp_field = NULL; | |
853 | struct bt_ctf_field_type *timestamp_field_type = NULL; | |
854 | ||
855 | timestamp_field = bt_ctf_field_structure_get_field(event_header, | |
856 | "timestamp"); | |
857 | if (!timestamp_field) { | |
858 | ret = -1; | |
859 | goto end; | |
860 | } | |
861 | ||
862 | timestamp_field_type = bt_ctf_field_get_type(timestamp_field); | |
863 | assert(timestamp_field_type); | |
864 | if (bt_ctf_field_type_get_type_id(timestamp_field_type) != | |
1487a16a | 865 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
9a220c32 JG |
866 | ret = -1; |
867 | goto end; | |
868 | } | |
869 | ||
870 | if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) { | |
871 | int64_t val; | |
872 | ||
873 | ret = bt_ctf_field_signed_integer_get_value(timestamp_field, | |
874 | &val); | |
875 | if (ret) { | |
876 | goto end; | |
877 | } | |
878 | *timestamp = (uint64_t) val; | |
879 | } else { | |
880 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, | |
881 | timestamp); | |
882 | if (ret) { | |
883 | goto end; | |
884 | } | |
885 | } | |
886 | end: | |
83509119 JG |
887 | bt_put(timestamp_field); |
888 | bt_put(timestamp_field_type); | |
9a220c32 JG |
889 | return ret; |
890 | } | |
891 | ||
c9af50d1 JG |
892 | static |
893 | void reset_structure_field(struct bt_ctf_field *structure, const char *name) | |
894 | { | |
895 | struct bt_ctf_field *member; | |
896 | ||
897 | member = bt_ctf_field_structure_get_field(structure, name); | |
898 | assert(member); | |
899 | (void) bt_ctf_field_reset(member); | |
e027ff8c | 900 | bt_put(member); |
c9af50d1 JG |
901 | } |
902 | ||
273b65be JG |
903 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
904 | { | |
905 | int ret = 0; | |
906 | size_t i; | |
c9af50d1 | 907 | uint64_t timestamp_begin, timestamp_end; |
273b65be | 908 | struct bt_ctf_field *integer = NULL; |
dc3fffef PP |
909 | struct bt_ctf_stream_pos packet_context_pos; |
910 | struct bt_ctf_trace *trace; | |
911 | enum bt_ctf_byte_order native_byte_order; | |
98edd02c | 912 | bool empty_packet; |
0686ef94 | 913 | uint64_t packet_size_bits; |
c9af50d1 JG |
914 | struct { |
915 | bool timestamp_begin; | |
916 | bool timestamp_end; | |
917 | bool content_size; | |
918 | bool packet_size; | |
919 | } auto_set_fields = { 0 }; | |
273b65be | 920 | |
bc37ae52 JG |
921 | if (!stream || stream->pos.fd < 0) { |
922 | /* | |
923 | * Stream does not have an associated fd. It is, | |
924 | * therefore, not a stream being used to write events. | |
925 | */ | |
273b65be JG |
926 | ret = -1; |
927 | goto end; | |
928 | } | |
929 | ||
664f50c8 JG |
930 | if (!stream->packet_context && stream->flushed_packet_count > 0) { |
931 | /* | |
932 | * A stream without a packet context, and thus without | |
933 | * content and packet size members, can't have more than | |
934 | * one packet. | |
935 | */ | |
936 | ret = -1; | |
937 | goto end; | |
938 | } | |
939 | ||
dc3fffef PP |
940 | trace = bt_ctf_stream_class_borrow_trace(stream->stream_class); |
941 | assert(trace); | |
942 | native_byte_order = bt_ctf_trace_get_byte_order(trace); | |
98edd02c | 943 | empty_packet = (stream->events->len == 0); |
d246b111 | 944 | |
28362f2b | 945 | /* mmap the next packet */ |
dc3fffef PP |
946 | bt_ctf_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR); |
947 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos, | |
948 | native_byte_order); | |
d246b111 JG |
949 | if (ret) { |
950 | goto end; | |
273b65be JG |
951 | } |
952 | ||
98edd02c JG |
953 | if (stream->packet_context) { |
954 | /* Set the default context attributes if present and unset. */ | |
955 | if (!empty_packet && !get_event_header_timestamp( | |
956 | ((struct bt_ctf_event *) g_ptr_array_index( | |
957 | stream->events, 0))->event_header, ×tamp_begin)) { | |
af9296f3 JG |
958 | ret = try_set_structure_field_integer( |
959 | stream->packet_context, | |
98edd02c | 960 | "timestamp_begin", timestamp_begin); |
af9296f3 | 961 | if (ret < 0) { |
98edd02c JG |
962 | goto end; |
963 | } | |
c9af50d1 | 964 | auto_set_fields.timestamp_begin = ret == 1; |
98edd02c JG |
965 | } |
966 | ||
967 | if (!empty_packet && !get_event_header_timestamp( | |
968 | ((struct bt_ctf_event *) g_ptr_array_index( | |
969 | stream->events, stream->events->len - 1))->event_header, | |
970 | ×tamp_end)) { | |
971 | ||
af9296f3 JG |
972 | ret = try_set_structure_field_integer( |
973 | stream->packet_context, | |
98edd02c | 974 | "timestamp_end", timestamp_end); |
af9296f3 | 975 | if (ret < 0) { |
98edd02c JG |
976 | goto end; |
977 | } | |
c9af50d1 | 978 | auto_set_fields.timestamp_end = ret == 1; |
98edd02c | 979 | } |
af9296f3 | 980 | ret = try_set_structure_field_integer(stream->packet_context, |
98edd02c | 981 | "content_size", UINT64_MAX); |
af9296f3 | 982 | if (ret < 0) { |
9a220c32 JG |
983 | goto end; |
984 | } | |
c9af50d1 | 985 | auto_set_fields.content_size = ret == 1; |
273b65be | 986 | |
af9296f3 | 987 | ret = try_set_structure_field_integer(stream->packet_context, |
98edd02c | 988 | "packet_size", UINT64_MAX); |
af9296f3 | 989 | if (ret < 0) { |
98edd02c JG |
990 | goto end; |
991 | } | |
c9af50d1 | 992 | auto_set_fields.packet_size = ret == 1; |
12c8a1a3 | 993 | |
10cb7b41 JG |
994 | /* Write packet context */ |
995 | memcpy(&packet_context_pos, &stream->pos, | |
dc3fffef | 996 | sizeof(packet_context_pos)); |
10cb7b41 | 997 | ret = bt_ctf_field_serialize(stream->packet_context, |
dc3fffef | 998 | &stream->pos, native_byte_order); |
98edd02c JG |
999 | if (ret) { |
1000 | goto end; | |
1001 | } | |
12c8a1a3 JG |
1002 | } |
1003 | ||
273b65be JG |
1004 | for (i = 0; i < stream->events->len; i++) { |
1005 | struct bt_ctf_event *event = g_ptr_array_index( | |
1006 | stream->events, i); | |
273b65be | 1007 | |
273b65be | 1008 | /* Write event header */ |
662e778c | 1009 | ret = bt_ctf_field_serialize(event->event_header, |
dc3fffef | 1010 | &stream->pos, native_byte_order); |
273b65be JG |
1011 | if (ret) { |
1012 | goto end; | |
1013 | } | |
1014 | ||
8bfa3f9c | 1015 | /* Write stream event context */ |
5fd2e9fd | 1016 | if (event->stream_event_context) { |
8bfa3f9c | 1017 | ret = bt_ctf_field_serialize( |
dc3fffef PP |
1018 | event->stream_event_context, &stream->pos, |
1019 | native_byte_order); | |
8bfa3f9c JG |
1020 | if (ret) { |
1021 | goto end; | |
1022 | } | |
1023 | } | |
1024 | ||
273b65be | 1025 | /* Write event content */ |
dc3fffef PP |
1026 | ret = bt_ctf_event_serialize(event, &stream->pos, |
1027 | native_byte_order); | |
273b65be JG |
1028 | if (ret) { |
1029 | goto end; | |
1030 | } | |
1031 | } | |
1032 | ||
0686ef94 JG |
1033 | /* Rounded-up in case content_size is not byte-aligned. */ |
1034 | packet_size_bits = (stream->pos.offset + (CHAR_BIT - 1)) & | |
1035 | ~(CHAR_BIT - 1); | |
1036 | stream->pos.packet_size = packet_size_bits; | |
1037 | ||
98edd02c JG |
1038 | if (stream->packet_context) { |
1039 | /* | |
1040 | * Update the packet total size and content size and overwrite | |
1041 | * the packet context. | |
1042 | * Copy base_mma as the packet may have been remapped (e.g. when | |
1043 | * a packet is resized). | |
1044 | */ | |
1045 | packet_context_pos.base_mma = stream->pos.base_mma; | |
c9af50d1 JG |
1046 | if (auto_set_fields.content_size) { |
1047 | ret = set_structure_field_integer( | |
1048 | stream->packet_context, | |
1049 | "content_size", stream->pos.offset); | |
1050 | if (ret < 0) { | |
1051 | goto end; | |
1052 | } | |
98edd02c | 1053 | } |
273b65be | 1054 | |
c9af50d1 JG |
1055 | if (auto_set_fields.packet_size) { |
1056 | ret = set_structure_field_integer(stream->packet_context, | |
1057 | "packet_size", packet_size_bits); | |
1058 | if (ret < 0) { | |
1059 | goto end; | |
1060 | } | |
98edd02c | 1061 | } |
273b65be | 1062 | |
98edd02c | 1063 | ret = bt_ctf_field_serialize(stream->packet_context, |
dc3fffef | 1064 | &packet_context_pos, native_byte_order); |
98edd02c JG |
1065 | if (ret) { |
1066 | goto end; | |
1067 | } | |
273b65be JG |
1068 | } |
1069 | ||
1070 | g_ptr_array_set_size(stream->events, 0); | |
1071 | stream->flushed_packet_count++; | |
0686ef94 | 1072 | stream->size += packet_size_bits / CHAR_BIT; |
273b65be | 1073 | end: |
c9af50d1 JG |
1074 | /* Reset automatically-set fields. */ |
1075 | if (auto_set_fields.timestamp_begin) { | |
1076 | reset_structure_field(stream->packet_context, | |
1077 | "timestamp_begin"); | |
1078 | } | |
1079 | if (auto_set_fields.timestamp_end) { | |
1080 | reset_structure_field(stream->packet_context, | |
1081 | "timestamp_end"); | |
1082 | } | |
1083 | if (auto_set_fields.packet_size) { | |
1084 | reset_structure_field(stream->packet_context, | |
1085 | "packet_size"); | |
1086 | } | |
1087 | if (auto_set_fields.content_size) { | |
1088 | reset_structure_field(stream->packet_context, | |
1089 | "content_size"); | |
1090 | } | |
83509119 | 1091 | bt_put(integer); |
cd7d8fb7 JG |
1092 | |
1093 | if (ret < 0) { | |
1094 | /* | |
1095 | * We failed to write the packet. Its size is therefore set to 0 | |
1096 | * to ensure the next mapping is done in the same place rather | |
1097 | * than advancing by "stream->pos.packet_size", which would | |
1098 | * leave a corrupted packet in the trace. | |
1099 | */ | |
1100 | stream->pos.packet_size = 0; | |
1101 | } | |
273b65be JG |
1102 | return ret; |
1103 | } | |
1104 | ||
1105 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) | |
1106 | { | |
83509119 | 1107 | bt_get(stream); |
273b65be JG |
1108 | } |
1109 | ||
1110 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) | |
1111 | { | |
83509119 | 1112 | bt_put(stream); |
273b65be JG |
1113 | } |
1114 | ||
1115 | static | |
83509119 | 1116 | void bt_ctf_stream_destroy(struct bt_object *obj) |
273b65be JG |
1117 | { |
1118 | struct bt_ctf_stream *stream; | |
3230ee6b | 1119 | int i; |
273b65be | 1120 | |
83509119 | 1121 | stream = container_of(obj, struct bt_ctf_stream, base); |
3230ee6b PP |
1122 | |
1123 | /* Call destroy listeners in reverse registration order */ | |
1124 | for (i = stream->destroy_listeners->len - 1; i >= 0; i--) { | |
1125 | struct bt_ctf_stream_destroy_listener *listener = | |
1126 | &g_array_index(stream->destroy_listeners, | |
1127 | struct bt_ctf_stream_destroy_listener, i); | |
1128 | ||
1129 | listener->func(stream, listener->data); | |
1130 | } | |
1131 | ||
dc3fffef | 1132 | (void) bt_ctf_stream_pos_fini(&stream->pos); |
0686ef94 JG |
1133 | if (stream->pos.fd >= 0) { |
1134 | int ret; | |
1135 | ||
1136 | /* | |
1137 | * Truncate the file's size to the minimum required to fit the | |
1138 | * last packet as we might have grown it too much on the last | |
1139 | * mmap. | |
1140 | */ | |
1141 | do { | |
1142 | ret = ftruncate(stream->pos.fd, stream->size); | |
1143 | } while (ret == -1 && errno == EINTR); | |
1144 | if (ret) { | |
1145 | perror("ftruncate"); | |
1146 | } | |
1147 | ||
1148 | if (close(stream->pos.fd)) { | |
1149 | perror("close"); | |
1150 | } | |
9f56e450 | 1151 | } |
12c8a1a3 | 1152 | |
12c8a1a3 JG |
1153 | if (stream->events) { |
1154 | g_ptr_array_free(stream->events, TRUE); | |
1155 | } | |
b71d7298 PP |
1156 | |
1157 | if (stream->name) { | |
1158 | g_string_free(stream->name, TRUE); | |
1159 | } | |
41ac640a | 1160 | |
3230ee6b PP |
1161 | if (stream->comp_cur_port) { |
1162 | GHashTableIter ht_iter; | |
1163 | gpointer comp_gptr, port_gptr; | |
1164 | ||
1165 | /* | |
1166 | * Since we're destroying the stream, remove the destroy | |
1167 | * listeners that it registered for each component in | |
1168 | * its component-port mapping hash table. Otherwise they | |
1169 | * would be called and the stream would be accessed once | |
1170 | * it's freed or another stream would be accessed. | |
1171 | */ | |
1172 | g_hash_table_iter_init(&ht_iter, stream->comp_cur_port); | |
1173 | ||
1174 | while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) { | |
1175 | assert(comp_gptr); | |
1176 | bt_component_remove_destroy_listener((void *) comp_gptr, | |
1177 | component_destroy_listener, stream); | |
1178 | } | |
1179 | ||
1180 | g_hash_table_destroy(stream->comp_cur_port); | |
1181 | } | |
1182 | ||
1183 | if (stream->destroy_listeners) { | |
1184 | g_array_free(stream->destroy_listeners, TRUE); | |
1185 | } | |
1186 | ||
83509119 JG |
1187 | bt_put(stream->packet_header); |
1188 | bt_put(stream->packet_context); | |
273b65be JG |
1189 | g_free(stream); |
1190 | } | |
1191 | ||
273b65be | 1192 | static |
af9296f3 JG |
1193 | int _set_structure_field_integer(struct bt_ctf_field *structure, char *name, |
1194 | uint64_t value, bool force) | |
273b65be JG |
1195 | { |
1196 | int ret = 0; | |
d7b1ea66 | 1197 | struct bt_ctf_field_type *field_type = NULL; |
273b65be JG |
1198 | struct bt_ctf_field *integer = |
1199 | bt_ctf_field_structure_get_field(structure, name); | |
12c8a1a3 JG |
1200 | |
1201 | if (!structure || !name) { | |
273b65be JG |
1202 | ret = -1; |
1203 | goto end; | |
1204 | } | |
1205 | ||
12c8a1a3 JG |
1206 | if (!integer) { |
1207 | /* Field not found, not an error. */ | |
1208 | goto end; | |
1209 | } | |
1210 | ||
1211 | /* Make sure the payload has not already been set. */ | |
af9296f3 | 1212 | if (!force && bt_ctf_field_is_set(integer)) { |
12c8a1a3 JG |
1213 | /* Payload already set, not an error */ |
1214 | goto end; | |
1215 | } | |
1216 | ||
d7b1ea66 | 1217 | field_type = bt_ctf_field_get_type(integer); |
d7b1ea66 | 1218 | assert(field_type); |
1487a16a | 1219 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d7b1ea66 JG |
1220 | /* |
1221 | * The user most likely meant for us to populate this field | |
1222 | * automatically. However, we can only do this if the field | |
1223 | * is an integer. Return an error. | |
1224 | */ | |
1225 | ret = -1; | |
1226 | goto end; | |
1227 | } | |
1228 | ||
1229 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
1230 | ret = bt_ctf_field_signed_integer_set_value(integer, | |
1231 | (int64_t) value); | |
1232 | } else { | |
1233 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); | |
1234 | } | |
af9296f3 | 1235 | ret = !ret ? 1 : ret; |
273b65be | 1236 | end: |
83509119 JG |
1237 | bt_put(integer); |
1238 | bt_put(field_type); | |
273b65be JG |
1239 | return ret; |
1240 | } | |
b71d7298 | 1241 | |
af9296f3 JG |
1242 | static |
1243 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
1244 | uint64_t value) | |
1245 | { | |
1246 | return _set_structure_field_integer(structure, name, value, true); | |
1247 | } | |
1248 | ||
1249 | /* | |
1250 | * Returns the following codes: | |
1251 | * 1 if the field was found and set, | |
1252 | * 0 if nothing was done (field not found, or was already set), | |
1253 | * <0 if an error was encoutered | |
1254 | */ | |
1255 | static | |
1256 | int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
1257 | uint64_t value) | |
1258 | { | |
1259 | return _set_structure_field_integer(structure, name, value, false); | |
1260 | } | |
1261 | ||
b71d7298 PP |
1262 | const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream) |
1263 | { | |
1264 | const char *name = NULL; | |
1265 | ||
1266 | if (!stream) { | |
1267 | goto end; | |
1268 | } | |
1269 | ||
1270 | name = stream->name ? stream->name->str : NULL; | |
1271 | ||
1272 | end: | |
1273 | return name; | |
1274 | } | |
98a4cbef PP |
1275 | |
1276 | int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream) | |
1277 | { | |
1278 | int ret = -1; | |
1279 | ||
1280 | if (!stream) { | |
1281 | goto end; | |
1282 | } | |
1283 | ||
1284 | ret = (stream->pos.fd >= 0); | |
1285 | ||
1286 | end: | |
1287 | return ret; | |
1288 | } | |
3230ee6b PP |
1289 | |
1290 | BT_HIDDEN | |
1291 | void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream *stream, | |
1292 | struct bt_component *comp, | |
1293 | struct bt_port *port) | |
1294 | { | |
1295 | assert(stream); | |
1296 | assert(comp); | |
1297 | assert(port); | |
1298 | assert(stream->comp_cur_port); | |
1299 | ||
1300 | /* | |
1301 | * Do not take a reference to the component here because we | |
1302 | * don't want the component to exist as long as this stream | |
1303 | * exists. Instead, keep a weak reference, but add a destroy | |
1304 | * listener so that we remove this hash table entry when we know | |
1305 | * the component is destroyed. | |
1306 | */ | |
1307 | bt_component_add_destroy_listener(comp, component_destroy_listener, | |
1308 | stream); | |
1309 | g_hash_table_insert(stream->comp_cur_port, comp, port); | |
1310 | } | |
1311 | ||
1312 | BT_HIDDEN | |
1313 | struct bt_port *bt_ctf_stream_port_for_component(struct bt_ctf_stream *stream, | |
1314 | struct bt_component *comp) | |
1315 | { | |
1316 | assert(stream); | |
1317 | assert(comp); | |
1318 | assert(stream->comp_cur_port); | |
1319 | return g_hash_table_lookup(stream->comp_cur_port, comp); | |
1320 | } | |
1321 | ||
1322 | BT_HIDDEN | |
1323 | void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream *stream, | |
1324 | bt_ctf_stream_destroy_listener_func func, void *data) | |
1325 | { | |
1326 | struct bt_ctf_stream_destroy_listener listener; | |
1327 | ||
1328 | assert(stream); | |
1329 | assert(func); | |
1330 | listener.func = func; | |
1331 | listener.data = data; | |
1332 | g_array_append_val(stream->destroy_listeners, listener); | |
1333 | } | |
1334 | ||
1335 | BT_HIDDEN | |
1336 | void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream *stream, | |
1337 | bt_ctf_stream_destroy_listener_func func, void *data) | |
1338 | { | |
1339 | size_t i; | |
1340 | ||
1341 | assert(stream); | |
1342 | assert(func); | |
1343 | ||
1344 | for (i = 0; i < stream->destroy_listeners->len; i++) { | |
1345 | struct bt_ctf_stream_destroy_listener *listener = | |
1346 | &g_array_index(stream->destroy_listeners, | |
1347 | struct bt_ctf_stream_destroy_listener, i); | |
1348 | ||
1349 | if (listener->func == func && listener->data == data) { | |
1350 | g_array_remove_index(stream->destroy_listeners, i); | |
1351 | i--; | |
1352 | } | |
1353 | } | |
1354 | } |