Document libbabeltrace2's C API
[babeltrace.git] / src / lib / trace-ir / trace.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/TRACE"
25 #include "lib/logging.h"
26
27 #include "lib/assert-pre.h"
28 #include "lib/assert-post.h"
29 #include <babeltrace2/trace-ir/trace.h>
30 #include <babeltrace2/trace-ir/event-class.h>
31 #include "ctf-writer/functor.h"
32 #include "ctf-writer/clock.h"
33 #include "compat/compiler.h"
34 #include <babeltrace2/value.h>
35 #include "lib/value.h"
36 #include <babeltrace2/types.h>
37 #include "compat/endian.h"
38 #include "common/assert.h"
39 #include "compat/glib.h"
40 #include <inttypes.h>
41 #include <stdint.h>
42 #include <string.h>
43 #include <stdbool.h>
44 #include <stdlib.h>
45
46 #include "attributes.h"
47 #include "clock-class.h"
48 #include "event-class.h"
49 #include "event.h"
50 #include "field-class.h"
51 #include "field-wrapper.h"
52 #include "resolve-field-path.h"
53 #include "stream-class.h"
54 #include "stream.h"
55 #include "trace-class.h"
56 #include "trace.h"
57 #include "utils.h"
58 #include "lib/value.h"
59 #include "lib/func-status.h"
60
61 struct bt_trace_destruction_listener_elem {
62 bt_trace_destruction_listener_func func;
63 void *data;
64 };
65
66 #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
67 BT_ASSERT_PRE_DEV_HOT((_trace), "Trace", ": %!+t", (_trace))
68
69 static
70 void destroy_trace(struct bt_object *obj)
71 {
72 struct bt_trace *trace = (void *) obj;
73
74 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
75 BT_OBJECT_PUT_REF_AND_RESET(trace->user_attributes);
76
77 /*
78 * Call destruction listener functions so that everything else
79 * still exists in the trace.
80 */
81 if (trace->destruction_listeners) {
82 uint64_t i;
83 const struct bt_error *saved_error;
84
85 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace);
86
87 /*
88 * The trace's reference count is 0 if we're here. Increment
89 * it to avoid a double-destroy (possibly infinitely recursive).
90 * This could happen for example if a destruction listener did
91 * bt_object_get_ref() (or anything that causes
92 * bt_object_get_ref() to be called) on the trace (ref.
93 * count goes from 0 to 1), and then bt_object_put_ref(): the
94 * reference count would go from 1 to 0 again and this function
95 * would be called again.
96 */
97 trace->base.ref_count++;
98
99 saved_error = bt_current_thread_take_error();
100
101 /* Call all the trace destruction listeners */
102 for (i = 0; i < trace->destruction_listeners->len; i++) {
103 struct bt_trace_destruction_listener_elem elem =
104 g_array_index(trace->destruction_listeners,
105 struct bt_trace_destruction_listener_elem, i);
106
107 if (elem.func) {
108 elem.func(trace, elem.data);
109 BT_ASSERT_POST_NO_ERROR();
110 }
111
112 /*
113 * The destruction listener should not have kept a
114 * reference to the trace.
115 */
116 BT_ASSERT_POST(trace->base.ref_count == 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace);
117 }
118 g_array_free(trace->destruction_listeners, TRUE);
119 trace->destruction_listeners = NULL;
120
121 if (saved_error) {
122 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
123 }
124 }
125
126 if (trace->name.str) {
127 g_string_free(trace->name.str, TRUE);
128 trace->name.str = NULL;
129 trace->name.value = NULL;
130 }
131
132 if (trace->environment) {
133 BT_LOGD_STR("Destroying environment attributes.");
134 bt_attributes_destroy(trace->environment);
135 trace->environment = NULL;
136 }
137
138 if (trace->streams) {
139 BT_LOGD_STR("Destroying streams.");
140 g_ptr_array_free(trace->streams, TRUE);
141 trace->streams = NULL;
142 }
143
144 if (trace->stream_classes_stream_count) {
145 g_hash_table_destroy(trace->stream_classes_stream_count);
146 trace->stream_classes_stream_count = NULL;
147 }
148
149 BT_LOGD_STR("Putting trace's class.");
150 bt_object_put_ref(trace->class);
151 trace->class = NULL;
152 g_free(trace);
153 }
154
155 struct bt_trace *bt_trace_create(struct bt_trace_class *tc)
156 {
157 struct bt_trace *trace = NULL;
158
159 BT_ASSERT_PRE_NO_ERROR();
160
161 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc);
162 trace = g_new0(struct bt_trace, 1);
163 if (!trace) {
164 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
165 goto error;
166 }
167
168 bt_object_init_shared(&trace->base, destroy_trace);
169 trace->user_attributes = bt_value_map_create();
170 if (!trace->user_attributes) {
171 BT_LIB_LOGE_APPEND_CAUSE(
172 "Failed to create a map value object.");
173 goto error;
174 }
175
176 trace->streams = g_ptr_array_new_with_free_func(
177 (GDestroyNotify) bt_object_try_spec_release);
178 if (!trace->streams) {
179 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
180 goto error;
181 }
182
183 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
184 g_direct_equal);
185 if (!trace->stream_classes_stream_count) {
186 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
187 goto error;
188 }
189
190 trace->name.str = g_string_new(NULL);
191 if (!trace->name.str) {
192 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
193 goto error;
194 }
195
196 trace->environment = bt_attributes_create();
197 if (!trace->environment) {
198 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
199 goto error;
200 }
201
202 trace->destruction_listeners = g_array_new(FALSE, TRUE,
203 sizeof(struct bt_trace_destruction_listener_elem));
204 if (!trace->destruction_listeners) {
205 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
206 goto error;
207 }
208
209 trace->class = tc;
210 bt_object_get_ref_no_null_check(trace->class);
211 BT_LIB_LOGD("Created trace object: %!+t", trace);
212 goto end;
213
214 error:
215 BT_OBJECT_PUT_REF_AND_RESET(trace);
216
217 end:
218 return trace;
219 }
220
221 const char *bt_trace_get_name(const struct bt_trace *trace)
222 {
223 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
224 return trace->name.value;
225 }
226
227 enum bt_trace_set_name_status bt_trace_set_name(struct bt_trace *trace,
228 const char *name)
229 {
230 BT_ASSERT_PRE_NO_ERROR();
231 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
232 BT_ASSERT_PRE_NON_NULL(name, "Name");
233 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
234 g_string_assign(trace->name.str, name);
235 trace->name.value = trace->name.str->str;
236 BT_LIB_LOGD("Set trace's name: %!+t", trace);
237 return BT_FUNC_STATUS_OK;
238 }
239
240 bt_uuid bt_trace_get_uuid(const struct bt_trace *trace)
241 {
242 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
243 return trace->uuid.value;
244 }
245
246 void bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
247 {
248 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
249 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
250 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
251 bt_uuid_copy(trace->uuid.uuid, uuid);
252 trace->uuid.value = trace->uuid.uuid;
253 BT_LIB_LOGD("Set trace's UUID: %!+t", trace);
254 }
255
256 static
257 bool trace_has_environment_entry(const struct bt_trace *trace, const char *name)
258 {
259 BT_ASSERT(trace);
260
261 return bt_attributes_borrow_field_value_by_name(
262 trace->environment, name);
263 }
264
265 static
266 enum bt_trace_set_environment_entry_status set_environment_entry(
267 struct bt_trace *trace,
268 const char *name, struct bt_value *value)
269 {
270 int ret;
271
272 BT_ASSERT(trace);
273 BT_ASSERT(name);
274 BT_ASSERT(value);
275 BT_ASSERT_PRE(!trace->frozen ||
276 !trace_has_environment_entry(trace, name),
277 "Trace is frozen: cannot replace environment entry: "
278 "%![trace-]+t, entry-name=\"%s\"", trace, name);
279 ret = bt_attributes_set_field_value(trace->environment, name,
280 value);
281 if (ret) {
282 ret = BT_FUNC_STATUS_MEMORY_ERROR;
283 BT_LIB_LOGE_APPEND_CAUSE(
284 "Cannot set trace's environment entry: "
285 "%![trace-]+t, entry-name=\"%s\"", trace, name);
286 } else {
287 bt_value_freeze(value);
288 BT_LIB_LOGD("Set trace's environment entry: "
289 "%![trace-]+t, entry-name=\"%s\"", trace, name);
290 }
291
292 return ret;
293 }
294
295 enum bt_trace_set_environment_entry_status
296 bt_trace_set_environment_entry_string(
297 struct bt_trace *trace, const char *name, const char *value)
298 {
299 int ret;
300 struct bt_value *value_obj;
301
302 BT_ASSERT_PRE_NO_ERROR();
303 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
304 BT_ASSERT_PRE_NON_NULL(name, "Name");
305 BT_ASSERT_PRE_NON_NULL(value, "Value");
306
307 value_obj = bt_value_string_create_init(value);
308 if (!value_obj) {
309 BT_LIB_LOGE_APPEND_CAUSE(
310 "Cannot create a string value object.");
311 ret = -1;
312 goto end;
313 }
314
315 /* set_environment_entry() logs errors */
316 ret = set_environment_entry(trace, name, value_obj);
317
318 end:
319 bt_object_put_ref(value_obj);
320 return ret;
321 }
322
323 enum bt_trace_set_environment_entry_status
324 bt_trace_set_environment_entry_integer(
325 struct bt_trace *trace, const char *name, int64_t value)
326 {
327 int ret;
328 struct bt_value *value_obj;
329
330 BT_ASSERT_PRE_NO_ERROR();
331 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
332 BT_ASSERT_PRE_NON_NULL(name, "Name");
333
334 value_obj = bt_value_integer_signed_create_init(value);
335 if (!value_obj) {
336 BT_LIB_LOGE_APPEND_CAUSE(
337 "Cannot create an integer value object.");
338 ret = BT_FUNC_STATUS_MEMORY_ERROR;
339 goto end;
340 }
341
342 /* set_environment_entry() logs errors */
343 ret = set_environment_entry(trace, name, value_obj);
344
345 end:
346 bt_object_put_ref(value_obj);
347 return ret;
348 }
349
350 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace *trace)
351 {
352 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
353 return bt_attributes_get_count(trace->environment);
354 }
355
356 void bt_trace_borrow_environment_entry_by_index_const(
357 const struct bt_trace *trace, uint64_t index,
358 const char **name, const struct bt_value **value)
359 {
360 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
361 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
362 BT_ASSERT_PRE_DEV_NON_NULL(value, "Value");
363 BT_ASSERT_PRE_DEV_VALID_INDEX(index,
364 bt_attributes_get_count(trace->environment));
365 *value = bt_attributes_borrow_field_value(trace->environment, index);
366 BT_ASSERT(*value);
367 *name = bt_attributes_get_field_name(trace->environment, index);
368 BT_ASSERT(*name);
369 }
370
371 const struct bt_value *bt_trace_borrow_environment_entry_value_by_name_const(
372 const struct bt_trace *trace, const char *name)
373 {
374 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
375 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
376 return bt_attributes_borrow_field_value_by_name(trace->environment,
377 name);
378 }
379
380 uint64_t bt_trace_get_stream_count(const struct bt_trace *trace)
381 {
382 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
383 return (uint64_t) trace->streams->len;
384 }
385
386 struct bt_stream *bt_trace_borrow_stream_by_index(
387 struct bt_trace *trace, uint64_t index)
388 {
389 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
390 BT_ASSERT_PRE_DEV_VALID_INDEX(index, trace->streams->len);
391 return g_ptr_array_index(trace->streams, index);
392 }
393
394 const struct bt_stream *bt_trace_borrow_stream_by_index_const(
395 const struct bt_trace *trace, uint64_t index)
396 {
397 return bt_trace_borrow_stream_by_index((void *) trace, index);
398 }
399
400 struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace,
401 uint64_t id)
402 {
403 struct bt_stream *stream = NULL;
404 uint64_t i;
405
406 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
407
408 for (i = 0; i < trace->streams->len; i++) {
409 struct bt_stream *stream_candidate =
410 g_ptr_array_index(trace->streams, i);
411
412 if (stream_candidate->id == id) {
413 stream = stream_candidate;
414 goto end;
415 }
416 }
417
418 end:
419 return stream;
420 }
421
422 const struct bt_stream *bt_trace_borrow_stream_by_id_const(
423 const struct bt_trace *trace, uint64_t id)
424 {
425 return bt_trace_borrow_stream_by_id((void *) trace, id);
426 }
427
428 enum bt_trace_add_listener_status bt_trace_add_destruction_listener(
429 const struct bt_trace *c_trace,
430 bt_trace_destruction_listener_func listener,
431 void *data, bt_listener_id *listener_id)
432 {
433 struct bt_trace *trace = (void *) c_trace;
434 uint64_t i;
435 struct bt_trace_destruction_listener_elem new_elem = {
436 .func = listener,
437 .data = data,
438 };
439
440 BT_ASSERT_PRE_NO_ERROR();
441 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
442 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
443
444 /* Find the next available spot */
445 for (i = 0; i < trace->destruction_listeners->len; i++) {
446 struct bt_trace_destruction_listener_elem elem =
447 g_array_index(trace->destruction_listeners,
448 struct bt_trace_destruction_listener_elem, i);
449
450 if (!elem.func) {
451 break;
452 }
453 }
454
455 if (i == trace->destruction_listeners->len) {
456 g_array_append_val(trace->destruction_listeners, new_elem);
457 } else {
458 g_array_insert_val(trace->destruction_listeners, i, new_elem);
459 }
460
461 if (listener_id) {
462 *listener_id = i;
463 }
464
465 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
466 "listener-id=%" PRIu64, trace, i);
467 return BT_FUNC_STATUS_OK;
468 }
469
470 static
471 bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id)
472 {
473 BT_ASSERT(listener_id < trace->destruction_listeners->len);
474 return (&g_array_index(trace->destruction_listeners,
475 struct bt_trace_destruction_listener_elem,
476 listener_id))->func;
477 }
478
479 enum bt_trace_remove_listener_status bt_trace_remove_destruction_listener(
480 const struct bt_trace *c_trace, bt_listener_id listener_id)
481 {
482 struct bt_trace *trace = (void *) c_trace;
483 struct bt_trace_destruction_listener_elem *elem;
484
485 BT_ASSERT_PRE_NO_ERROR();
486 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
487 BT_ASSERT_PRE(has_listener_id(trace, listener_id),
488 "Trace has no such trace destruction listener ID: "
489 "%![trace-]+t, %" PRIu64, trace, listener_id);
490 elem = &g_array_index(trace->destruction_listeners,
491 struct bt_trace_destruction_listener_elem,
492 listener_id);
493 BT_ASSERT(elem->func);
494
495 elem->func = NULL;
496 elem->data = NULL;
497 BT_LIB_LOGD("Removed \"trace destruction listener: "
498 "%![trace-]+t, listener-id=%" PRIu64,
499 trace, listener_id);
500 return BT_FUNC_STATUS_OK;
501 }
502
503 BT_HIDDEN
504 void _bt_trace_freeze(const struct bt_trace *trace)
505 {
506 BT_ASSERT(trace);
507 BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class);
508 bt_trace_class_freeze(trace->class);
509 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
510 trace->user_attributes);
511 bt_value_freeze(trace->user_attributes);
512 BT_LIB_LOGD("Freezing trace: %!+t", trace);
513 ((struct bt_trace *) trace)->frozen = true;
514 }
515
516 BT_HIDDEN
517 void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
518 {
519 guint count = 0;
520
521 bt_object_set_parent(&stream->base, &trace->base);
522 g_ptr_array_add(trace->streams, stream);
523 bt_trace_freeze(trace);
524
525 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
526 stream->class)) {
527 count = GPOINTER_TO_UINT(g_hash_table_lookup(
528 trace->stream_classes_stream_count, stream->class));
529 }
530
531 g_hash_table_insert(trace->stream_classes_stream_count,
532 stream->class, GUINT_TO_POINTER(count + 1));
533 }
534
535 BT_HIDDEN
536 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace,
537 const struct bt_stream_class *stream_class)
538 {
539 gpointer orig_key;
540 gpointer value;
541 uint64_t id = 0;
542
543 BT_ASSERT(stream_class);
544 BT_ASSERT(trace);
545 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
546 stream_class, &orig_key, &value)) {
547 id = (uint64_t) GPOINTER_TO_UINT(value);
548 }
549
550 return id;
551 }
552
553 struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace)
554 {
555 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
556 return trace->class;
557 }
558
559 const struct bt_trace_class *bt_trace_borrow_class_const(
560 const struct bt_trace *trace)
561 {
562 return bt_trace_borrow_class((void *) trace);
563 }
564
565 const struct bt_value *bt_trace_borrow_user_attributes_const(
566 const struct bt_trace *trace)
567 {
568 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
569 return trace->user_attributes;
570 }
571
572 struct bt_value *bt_trace_borrow_user_attributes(struct bt_trace *trace)
573 {
574 return (void *) bt_trace_borrow_user_attributes_const((void *) trace);
575 }
576
577 void bt_trace_set_user_attributes(
578 struct bt_trace *trace,
579 const struct bt_value *user_attributes)
580 {
581 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
582 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
583 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
584 "User attributes object is not a map value object.");
585 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
586 bt_object_put_ref_no_null_check(trace->user_attributes);
587 trace->user_attributes = (void *) user_attributes;
588 bt_object_get_ref_no_null_check(trace->user_attributes);
589 }
590
591 void bt_trace_get_ref(const struct bt_trace *trace)
592 {
593 bt_object_get_ref(trace);
594 }
595
596 void bt_trace_put_ref(const struct bt_trace *trace)
597 {
598 bt_object_put_ref(trace);
599 }
This page took 0.041555 seconds and 4 git commands to generate.