afc37d9cfbf43dbc08fd8b15f5d53ee9585dc44b
[babeltrace.git] / lib / trace-ir / trace.c
1 /*
2 * trace.c
3 *
4 * Babeltrace trace IR - Trace
5 *
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
29 #define BT_LOG_TAG "TRACE"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/trace-ir/trace-internal.h>
34 #include <babeltrace/trace-ir/clock-class-internal.h>
35 #include <babeltrace/trace-ir/stream-internal.h>
36 #include <babeltrace/trace-ir/stream-class-internal.h>
37 #include <babeltrace/trace-ir/event-internal.h>
38 #include <babeltrace/trace-ir/event-class.h>
39 #include <babeltrace/trace-ir/event-class-internal.h>
40 #include <babeltrace/ctf-writer/functor-internal.h>
41 #include <babeltrace/ctf-writer/clock-internal.h>
42 #include <babeltrace/trace-ir/field-wrapper-internal.h>
43 #include <babeltrace/trace-ir/field-classes-internal.h>
44 #include <babeltrace/trace-ir/attributes-internal.h>
45 #include <babeltrace/trace-ir/utils-internal.h>
46 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
47 #include <babeltrace/compiler-internal.h>
48 #include <babeltrace/values.h>
49 #include <babeltrace/private-values.h>
50 #include <babeltrace/values-internal.h>
51 #include <babeltrace/object.h>
52 #include <babeltrace/types.h>
53 #include <babeltrace/endian-internal.h>
54 #include <babeltrace/assert-internal.h>
55 #include <babeltrace/compat/glib-internal.h>
56 #include <inttypes.h>
57 #include <stdint.h>
58 #include <string.h>
59 #include <stdlib.h>
60
61 struct bt_trace_is_static_listener_elem {
62 bt_trace_is_static_listener func;
63 bt_trace_listener_removed removed;
64 void *data;
65 };
66
67 #define BT_ASSERT_PRE_TRACE_HOT(_trace) \
68 BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
69
70 static
71 void destroy_trace(struct bt_object *obj)
72 {
73 struct bt_trace *trace = (void *) obj;
74
75 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
76
77 /*
78 * Call remove listeners first so that everything else still
79 * exists in the trace.
80 */
81 if (trace->is_static_listeners) {
82 size_t i;
83
84 for (i = 0; i < trace->is_static_listeners->len; i++) {
85 struct bt_trace_is_static_listener_elem elem =
86 g_array_index(trace->is_static_listeners,
87 struct bt_trace_is_static_listener_elem, i);
88
89 if (elem.removed) {
90 elem.removed(trace, elem.data);
91 }
92 }
93
94 g_array_free(trace->is_static_listeners, TRUE);
95 }
96
97 bt_object_pool_finalize(&trace->packet_header_field_pool);
98
99 if (trace->environment) {
100 BT_LOGD_STR("Destroying environment attributes.");
101 bt_attributes_destroy(trace->environment);
102 }
103
104 if (trace->name.str) {
105 g_string_free(trace->name.str, TRUE);
106 }
107
108 if (trace->streams) {
109 BT_LOGD_STR("Destroying streams.");
110 g_ptr_array_free(trace->streams, TRUE);
111 }
112
113 if (trace->stream_classes) {
114 BT_LOGD_STR("Destroying stream classes.");
115 g_ptr_array_free(trace->stream_classes, TRUE);
116 }
117
118 if (trace->stream_classes_stream_count) {
119 g_hash_table_destroy(trace->stream_classes_stream_count);
120 }
121
122 BT_LOGD_STR("Putting packet header field classe.");
123 bt_object_put_ref(trace->packet_header_fc);
124 g_free(trace);
125 }
126
127 static
128 void free_packet_header_field(struct bt_field_wrapper *field_wrapper,
129 struct bt_trace *trace)
130 {
131 bt_field_wrapper_destroy(field_wrapper);
132 }
133
134 struct bt_trace *bt_trace_create(void)
135 {
136 struct bt_trace *trace = NULL;
137 int ret;
138
139 BT_LOGD_STR("Creating default trace object.");
140 trace = g_new0(struct bt_trace, 1);
141 if (!trace) {
142 BT_LOGE_STR("Failed to allocate one trace.");
143 goto error;
144 }
145
146 bt_object_init_shared_with_parent(&trace->base, destroy_trace);
147 trace->streams = g_ptr_array_new_with_free_func(
148 (GDestroyNotify) bt_object_try_spec_release);
149 if (!trace->streams) {
150 BT_LOGE_STR("Failed to allocate one GPtrArray.");
151 goto error;
152 }
153
154 trace->stream_classes = g_ptr_array_new_with_free_func(
155 (GDestroyNotify) bt_object_try_spec_release);
156 if (!trace->stream_classes) {
157 BT_LOGE_STR("Failed to allocate one GPtrArray.");
158 goto error;
159 }
160
161 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
162 g_direct_equal);
163 if (!trace->stream_classes_stream_count) {
164 BT_LOGE_STR("Failed to allocate one GHashTable.");
165 goto error;
166 }
167
168 trace->name.str = g_string_new(NULL);
169 if (!trace->name.str) {
170 BT_LOGE_STR("Failed to allocate one GString.");
171 goto error;
172 }
173
174 trace->environment = bt_attributes_create();
175 if (!trace->environment) {
176 BT_LOGE_STR("Cannot create empty attributes object.");
177 goto error;
178 }
179
180 trace->is_static_listeners = g_array_new(FALSE, TRUE,
181 sizeof(struct bt_trace_is_static_listener_elem));
182 if (!trace->is_static_listeners) {
183 BT_LOGE_STR("Failed to allocate one GArray.");
184 goto error;
185 }
186
187 trace->assigns_automatic_stream_class_id = true;
188 ret = bt_object_pool_initialize(&trace->packet_header_field_pool,
189 (bt_object_pool_new_object_func) bt_field_wrapper_new,
190 (bt_object_pool_destroy_object_func) free_packet_header_field,
191 trace);
192 if (ret) {
193 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
194 ret);
195 goto error;
196 }
197
198 BT_LIB_LOGD("Created trace object: %!+t", trace);
199 goto end;
200
201 error:
202 BT_OBJECT_PUT_REF_AND_RESET(trace);
203
204 end:
205 return trace;
206 }
207
208 const char *bt_trace_get_name(struct bt_trace *trace)
209 {
210 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
211 return trace->name.value;
212 }
213
214 int bt_trace_set_name(struct bt_trace *trace, const char *name)
215 {
216 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
217 BT_ASSERT_PRE_NON_NULL(name, "Name");
218 BT_ASSERT_PRE_TRACE_HOT(trace);
219 g_string_assign(trace->name.str, name);
220 trace->name.value = trace->name.str->str;
221 BT_LIB_LOGV("Set trace's name: %!+t", trace);
222 return 0;
223 }
224
225 bt_uuid bt_trace_get_uuid(struct bt_trace *trace)
226 {
227 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
228 return trace->uuid.value;
229 }
230
231 int bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
232 {
233 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
234 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
235 BT_ASSERT_PRE_TRACE_HOT(trace);
236 memcpy(trace->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
237 trace->uuid.value = trace->uuid.uuid;
238 BT_LIB_LOGV("Set trace's UUID: %!+t", trace);
239 return 0;
240 }
241
242 BT_ASSERT_FUNC
243 static
244 bool trace_has_environment_entry(struct bt_trace *trace, const char *name)
245 {
246 BT_ASSERT(trace);
247
248 return bt_attributes_borrow_field_value_by_name(
249 trace->environment, name) != NULL;
250 }
251
252 static
253 int set_environment_entry(struct bt_trace *trace, const char *name,
254 struct bt_private_value *value)
255 {
256 int ret;
257
258 BT_ASSERT(trace);
259 BT_ASSERT(name);
260 BT_ASSERT(value);
261 BT_ASSERT_PRE(!trace->frozen ||
262 !trace_has_environment_entry(trace, name),
263 "Trace is frozen: cannot replace environment entry: "
264 "%![trace-]+t, entry-name=\"%s\"", trace, name);
265 ret = bt_attributes_set_field_value(trace->environment, name,
266 value);
267 bt_value_freeze(bt_value_borrow_from_private(value));
268 if (ret) {
269 BT_LIB_LOGE("Cannot set trace's environment entry: "
270 "%![trace-]+t, entry-name=\"%s\"", trace, name);
271 } else {
272 BT_LIB_LOGV("Set trace's environment entry: "
273 "%![trace-]+t, entry-name=\"%s\"", trace, name);
274 }
275
276 return ret;
277 }
278
279 int bt_trace_set_environment_entry_string(struct bt_trace *trace,
280 const char *name, const char *value)
281 {
282 int ret;
283 struct bt_private_value *value_obj;
284
285 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
286 BT_ASSERT_PRE_NON_NULL(name, "Name");
287 BT_ASSERT_PRE_NON_NULL(value, "Value");
288 value_obj = bt_private_value_string_create_init(value);
289 if (!value_obj) {
290 BT_LOGE_STR("Cannot create a string value object.");
291 ret = -1;
292 goto end;
293 }
294
295 /* set_environment_entry() logs errors */
296 ret = set_environment_entry(trace, name, value_obj);
297
298 end:
299 bt_object_put_ref(value_obj);
300 return ret;
301 }
302
303 int bt_trace_set_environment_entry_integer(
304 struct bt_trace *trace, const char *name, int64_t value)
305 {
306 int ret;
307 struct bt_private_value *value_obj;
308
309 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
310 BT_ASSERT_PRE_NON_NULL(name, "Name");
311 value_obj = bt_private_value_integer_create_init(value);
312 if (!value_obj) {
313 BT_LOGE_STR("Cannot create an integer value object.");
314 ret = -1;
315 goto end;
316 }
317
318 /* set_environment_entry() logs errors */
319 ret = set_environment_entry(trace, name, value_obj);
320
321 end:
322 bt_object_put_ref(value_obj);
323 return ret;
324 }
325
326 uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace)
327 {
328 int64_t ret;
329
330 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
331 ret = bt_attributes_get_count(trace->environment);
332 BT_ASSERT(ret >= 0);
333 return (uint64_t) ret;
334 }
335
336 void bt_trace_borrow_environment_entry_by_index(
337 struct bt_trace *trace, uint64_t index,
338 const char **name, struct bt_value **value)
339 {
340 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
341 BT_ASSERT_PRE_NON_NULL(name, "Name");
342 BT_ASSERT_PRE_NON_NULL(value, "Value");
343 BT_ASSERT_PRE_VALID_INDEX(index,
344 bt_attributes_get_count(trace->environment));
345 *value = bt_value_borrow_from_private(
346 bt_attributes_borrow_field_value(trace->environment, index));
347 BT_ASSERT(*value);
348 *name = bt_attributes_get_field_name(trace->environment, index);
349 BT_ASSERT(*name);
350 }
351
352 struct bt_value *bt_trace_borrow_environment_entry_value_by_name(
353 struct bt_trace *trace, const char *name)
354 {
355 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
356 BT_ASSERT_PRE_NON_NULL(name, "Name");
357 return bt_value_borrow_from_private(
358 bt_attributes_borrow_field_value_by_name(trace->environment,
359 name));
360 }
361
362 uint64_t bt_trace_get_stream_count(struct bt_trace *trace)
363 {
364 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
365 return (uint64_t) trace->streams->len;
366 }
367
368 struct bt_stream *bt_trace_borrow_stream_by_index(
369 struct bt_trace *trace, uint64_t index)
370 {
371 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
372 BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len);
373 return g_ptr_array_index(trace->streams, index);
374 }
375
376 struct bt_stream *bt_trace_borrow_stream_by_id(
377 struct bt_trace *trace, uint64_t id)
378 {
379 struct bt_stream *stream = NULL;
380 uint64_t i;
381
382 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
383
384 for (i = 0; i < trace->streams->len; i++) {
385 struct bt_stream *stream_candidate =
386 g_ptr_array_index(trace->streams, i);
387
388 if (stream_candidate->id == id) {
389 stream = stream_candidate;
390 goto end;
391 }
392 }
393
394 end:
395 return stream;
396 }
397
398 uint64_t bt_trace_get_stream_class_count(struct bt_trace *trace)
399 {
400 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
401 return (uint64_t) trace->stream_classes->len;
402 }
403
404 struct bt_stream_class *bt_trace_borrow_stream_class_by_index(
405 struct bt_trace *trace, uint64_t index)
406 {
407 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
408 BT_ASSERT_PRE_VALID_INDEX(index, trace->stream_classes->len);
409 return g_ptr_array_index(trace->stream_classes, index);
410 }
411
412 struct bt_stream_class *bt_trace_borrow_stream_class_by_id(
413 struct bt_trace *trace, uint64_t id)
414 {
415 struct bt_stream_class *stream_class = NULL;
416 uint64_t i;
417
418 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
419
420 for (i = 0; i < trace->stream_classes->len; i++) {
421 struct bt_stream_class *stream_class_candidate =
422 g_ptr_array_index(trace->stream_classes, i);
423
424 if (stream_class_candidate->id == id) {
425 stream_class = stream_class_candidate;
426 goto end;
427 }
428 }
429
430 end:
431 return stream_class;
432 }
433
434 struct bt_field_class *bt_trace_borrow_packet_header_field_class(
435 struct bt_trace *trace)
436 {
437 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
438 return trace->packet_header_fc;
439 }
440
441 int bt_trace_set_packet_header_field_class(struct bt_trace *trace,
442 struct bt_field_class *field_class)
443 {
444 int ret;
445 struct bt_resolve_field_path_context resolve_ctx = {
446 .packet_header = field_class,
447 .packet_context = NULL,
448 .event_header = NULL,
449 .event_common_context = NULL,
450 .event_specific_context = NULL,
451 .event_payload = NULL,
452 };
453
454 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
455 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
456 BT_ASSERT_PRE_TRACE_HOT(trace);
457 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
458 BT_FIELD_CLASS_TYPE_STRUCTURE,
459 "Packet header field classe is not a structure field classe: %!+F",
460 field_class);
461 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
462 if (ret) {
463 goto end;
464 }
465
466 bt_field_class_make_part_of_trace(field_class);
467 bt_object_put_ref(trace->packet_header_fc);
468 trace->packet_header_fc = bt_object_get_ref(field_class);
469 bt_field_class_freeze(field_class);
470 BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace);
471
472 end:
473 return ret;
474 }
475
476 bt_bool bt_trace_is_static(struct bt_trace *trace)
477 {
478 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
479 return (bt_bool) trace->is_static;
480 }
481
482 int bt_trace_make_static(struct bt_trace *trace)
483 {
484 uint64_t i;
485
486 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
487 trace->is_static = true;
488 bt_trace_freeze(trace);
489 BT_LIB_LOGV("Trace is now static: %!+t", trace);
490
491 /* Call all the "trace is static" listeners */
492 for (i = 0; i < trace->is_static_listeners->len; i++) {
493 struct bt_trace_is_static_listener_elem elem =
494 g_array_index(trace->is_static_listeners,
495 struct bt_trace_is_static_listener_elem, i);
496
497 if (elem.func) {
498 elem.func(trace, elem.data);
499 }
500 }
501
502 return 0;
503 }
504
505 int bt_trace_add_is_static_listener(struct bt_trace *trace,
506 bt_trace_is_static_listener listener,
507 bt_trace_listener_removed listener_removed, void *data,
508 uint64_t *listener_id)
509 {
510 uint64_t i;
511 struct bt_trace_is_static_listener_elem new_elem = {
512 .func = listener,
513 .removed = listener_removed,
514 .data = data,
515 };
516
517 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
518 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
519 BT_ASSERT_PRE(!trace->is_static,
520 "Trace is already static: %!+t", trace);
521 BT_ASSERT_PRE(trace->in_remove_listener,
522 "Cannot call this function while executing a "
523 "remove listener: %!+t", trace);
524
525 /* Find the next available spot */
526 for (i = 0; i < trace->is_static_listeners->len; i++) {
527 struct bt_trace_is_static_listener_elem elem =
528 g_array_index(trace->is_static_listeners,
529 struct bt_trace_is_static_listener_elem, i);
530
531 if (!elem.func) {
532 break;
533 }
534 }
535
536 if (i == trace->is_static_listeners->len) {
537 g_array_append_val(trace->is_static_listeners, new_elem);
538 } else {
539 g_array_insert_val(trace->is_static_listeners, i, new_elem);
540 }
541
542 if (listener_id) {
543 *listener_id = i;
544 }
545
546 BT_LIB_LOGV("Added \"trace is static\" listener: "
547 "%![trace-]+t, listener-id=%" PRIu64, trace, i);
548 return 0;
549 }
550
551 BT_ASSERT_PRE_FUNC
552 static
553 bool has_listener_id(struct bt_trace *trace, uint64_t listener_id)
554 {
555 BT_ASSERT(listener_id < trace->is_static_listeners->len);
556 return (&g_array_index(trace->is_static_listeners,
557 struct bt_trace_is_static_listener_elem,
558 listener_id))->func != NULL;
559 }
560
561 int bt_trace_remove_is_static_listener(
562 struct bt_trace *trace, uint64_t listener_id)
563 {
564 struct bt_trace_is_static_listener_elem *elem;
565
566 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
567 BT_ASSERT_PRE(!trace->is_static,
568 "Trace is already static: %!+t", trace);
569 BT_ASSERT_PRE(trace->in_remove_listener,
570 "Cannot call this function while executing a "
571 "remove listener: %!+t", trace);
572 BT_ASSERT_PRE(has_listener_id(trace, listener_id),
573 "Trace has no such \"trace is static\" listener ID: "
574 "%![trace-]+t, %" PRIu64, trace, listener_id);
575 elem = &g_array_index(trace->is_static_listeners,
576 struct bt_trace_is_static_listener_elem,
577 listener_id);
578 BT_ASSERT(elem->func);
579
580 if (elem->removed) {
581 /* Call remove listener */
582 BT_LIB_LOGV("Calling remove listener: "
583 "%![trace-]+t, listener-id=%" PRIu64,
584 trace, listener_id);
585 trace->in_remove_listener = true;
586 elem->removed(trace, elem->data);
587 trace->in_remove_listener = false;
588 }
589
590 elem->func = NULL;
591 elem->removed = NULL;
592 elem->data = NULL;
593 BT_LIB_LOGV("Removed \"trace is static\" listener: "
594 "%![trace-]+t, listener-id=%" PRIu64,
595 trace, listener_id);
596 return 0;
597 }
598
599 BT_HIDDEN
600 void _bt_trace_freeze(struct bt_trace *trace)
601 {
602 /* The packet header field classe is already frozen */
603 BT_ASSERT(trace);
604 BT_LIB_LOGD("Freezing trace: %!+t", trace);
605 trace->frozen = true;
606 }
607
608 bt_bool bt_trace_assigns_automatic_stream_class_id(struct bt_trace *trace)
609 {
610 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
611 return (bt_bool) trace->assigns_automatic_stream_class_id;
612 }
613
614 int bt_trace_set_assigns_automatic_stream_class_id(
615 struct bt_trace *trace, bt_bool value)
616 {
617 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
618 BT_ASSERT_PRE_TRACE_HOT(trace);
619 trace->assigns_automatic_stream_class_id = (bool) value;
620 BT_LIB_LOGV("Set trace's automatic stream class ID "
621 "assignment property: %!+t", trace);
622 return 0;
623 }
624
625 BT_HIDDEN
626 void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
627 {
628 guint count = 0;
629
630 bt_object_set_parent(&stream->base, &trace->base);
631 g_ptr_array_add(trace->streams, stream);
632 bt_trace_freeze(trace);
633
634 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
635 stream->class)) {
636 count = GPOINTER_TO_UINT(g_hash_table_lookup(
637 trace->stream_classes_stream_count, stream->class));
638 }
639
640 g_hash_table_insert(trace->stream_classes_stream_count,
641 stream->class, GUINT_TO_POINTER(count + 1));
642 }
643
644 BT_HIDDEN
645 uint64_t bt_trace_get_automatic_stream_id(struct bt_trace *trace,
646 struct bt_stream_class *stream_class)
647 {
648 gpointer orig_key;
649 gpointer value;
650 uint64_t id = 0;
651
652 BT_ASSERT(stream_class);
653 BT_ASSERT(trace);
654 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
655 stream_class, &orig_key, &value)) {
656 id = (uint64_t) GPOINTER_TO_UINT(value);
657 }
658
659 return id;
660 }
This page took 0.043649 seconds and 3 git commands to generate.