2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
7 #define BT_LOG_TAG "LIB/ERROR"
8 #include "lib/logging.h"
12 #include <babeltrace2/babeltrace.h>
15 #include "graph/message/iterator.h"
16 #include "graph/component.h"
17 #include "graph/component-class.h"
18 #include "common/assert.h"
19 #include "lib/assert-cond.h"
20 #include "lib/func-status.h"
22 #define BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(_cause, _exp_type_name, _exp_type) \
23 BT_ASSERT_PRE("error-cause-has-" _exp_type_name "-actor", \
24 ((const struct bt_error_cause *) (_cause))->actor_type == _exp_type, \
25 "Unexpected error cause's actor type: type=%s, exp-type=%s", \
26 bt_error_cause_actor_type_string(((const struct bt_error_cause *) (_cause))->actor_type), \
27 bt_error_cause_actor_type_string(_exp_type))
30 void fini_component_class_id(
31 struct bt_error_cause_component_class_id
*comp_class_id
)
33 BT_ASSERT(comp_class_id
);
35 if (comp_class_id
->name
) {
36 g_string_free(comp_class_id
->name
, TRUE
);
37 comp_class_id
->name
= NULL
;
40 if (comp_class_id
->plugin_name
) {
41 g_string_free(comp_class_id
->plugin_name
, TRUE
);
42 comp_class_id
->plugin_name
= NULL
;
47 void fini_error_cause(struct bt_error_cause
*cause
)
50 BT_LIB_LOGD("Finalizing error cause: %!+r", cause
);
52 if (cause
->module_name
) {
53 g_string_free(cause
->module_name
, TRUE
);
54 cause
->module_name
= NULL
;
57 if (cause
->file_name
) {
58 g_string_free(cause
->file_name
, TRUE
);
59 cause
->file_name
= NULL
;
63 g_string_free(cause
->message
, TRUE
);
64 cause
->message
= NULL
;
69 void destroy_error_cause(struct bt_error_cause
*cause
)
75 BT_LIB_LOGD("Destroying error cause: %!+r", cause
);
77 switch (cause
->actor_type
) {
78 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
80 struct bt_error_cause_component_actor
*spec_cause
=
83 if (spec_cause
->comp_name
) {
84 g_string_free(spec_cause
->comp_name
, TRUE
);
85 spec_cause
->comp_name
= NULL
;
88 fini_component_class_id(&spec_cause
->comp_class_id
);
91 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
93 struct bt_error_cause_component_class_actor
*spec_cause
=
96 fini_component_class_id(&spec_cause
->comp_class_id
);
99 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
101 struct bt_error_cause_message_iterator_actor
*spec_cause
=
104 if (spec_cause
->comp_name
) {
105 g_string_free(spec_cause
->comp_name
, TRUE
);
106 spec_cause
->comp_name
= NULL
;
109 if (spec_cause
->output_port_name
) {
110 g_string_free(spec_cause
->output_port_name
, TRUE
);
111 spec_cause
->output_port_name
= NULL
;
114 fini_component_class_id(&spec_cause
->comp_class_id
);
121 fini_error_cause(cause
);
129 int init_error_cause(struct bt_error_cause
*cause
,
130 enum bt_error_cause_actor_type actor_type
)
135 BT_LIB_LOGD("Initializing error cause: %!+r", cause
);
136 cause
->actor_type
= actor_type
;
137 cause
->module_name
= g_string_new(NULL
);
138 if (!cause
->module_name
) {
139 BT_LOGE_STR("Failed to allocate one GString.");
144 cause
->message
= g_string_new(NULL
);
145 if (!cause
->message
) {
146 BT_LOGE_STR("Failed to allocate one GString.");
151 cause
->file_name
= g_string_new(NULL
);
152 if (!cause
->file_name
) {
153 BT_LOGE_STR("Failed to allocate one GString.");
158 BT_LIB_LOGD("Initialized error cause: %!+r", cause
);
165 int init_component_class_id(
166 struct bt_error_cause_component_class_id
*comp_class_id
,
167 struct bt_component_class
*comp_cls
)
171 BT_ASSERT(comp_class_id
);
172 comp_class_id
->type
= comp_cls
->type
;
173 comp_class_id
->name
= g_string_new(comp_cls
->name
->str
);
174 if (!comp_class_id
->name
) {
175 BT_LOGE_STR("Failed to allocate one GString.");
180 comp_class_id
->plugin_name
= g_string_new(comp_cls
->plugin_name
->str
);
181 if (!comp_class_id
->plugin_name
) {
182 BT_LOGE_STR("Failed to allocate one GString.");
192 void set_error_cause_props(struct bt_error_cause
*cause
,
193 const char *file_name
, uint64_t line_no
)
196 g_string_assign(cause
->file_name
, file_name
);
197 cause
->line_no
= line_no
;
201 struct bt_error_cause
*create_error_cause(const char *module_name
,
202 const char *file_name
, uint64_t line_no
)
204 struct bt_error_cause
*cause
= g_new0(struct bt_error_cause
, 1);
207 BT_LOGD_STR("Creating error cause (unknown actor).");
210 BT_LOGE_STR("Failed to allocate one error cause.");
214 ret
= init_error_cause(cause
, BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
);
219 g_string_assign(cause
->module_name
, module_name
);
220 set_error_cause_props(cause
, file_name
, line_no
);
221 BT_LIB_LOGD("Created error cause: %!+r", cause
);
225 destroy_error_cause(cause
);
233 void append_component_class_id_str(GString
*str
,
234 struct bt_error_cause_component_class_id
*comp_class_id
)
236 const char *type_str
= NULL
;
238 switch (comp_class_id
->type
) {
239 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
242 case BT_COMPONENT_CLASS_TYPE_FILTER
:
245 case BT_COMPONENT_CLASS_TYPE_SINK
:
252 if (comp_class_id
->plugin_name
->len
> 0) {
253 g_string_append_printf(str
, "%s.%s.%s",
254 type_str
, comp_class_id
->plugin_name
->str
,
255 comp_class_id
->name
->str
);
257 g_string_append_printf(str
, "%s.%s",
258 type_str
, comp_class_id
->name
->str
);
263 struct bt_error_cause_component_actor
*create_error_cause_component_actor(
264 struct bt_component
*comp
, const char *file_name
,
267 struct bt_error_cause_component_actor
*cause
=
268 g_new0(struct bt_error_cause_component_actor
, 1);
271 BT_LOGD_STR("Creating error cause object (component actor).");
277 ret
= init_error_cause(&cause
->base
,
278 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
);
283 set_error_cause_props(&cause
->base
, file_name
, line_no
);
284 cause
->comp_name
= g_string_new(comp
->name
->str
);
285 if (!cause
->comp_name
) {
286 BT_LOGE_STR("Failed to allocate one GString.");
290 ret
= init_component_class_id(&cause
->comp_class_id
, comp
->class);
295 g_string_append_printf(cause
->base
.module_name
, "%s: ",
297 append_component_class_id_str(cause
->base
.module_name
,
298 &cause
->comp_class_id
);
299 BT_LIB_LOGD("Created error cause object: %!+r", cause
);
304 destroy_error_cause(&cause
->base
);
313 struct bt_error_cause_component_class_actor
*
314 create_error_cause_component_class_actor(struct bt_component_class
*comp_cls
,
315 const char *file_name
, uint64_t line_no
)
317 struct bt_error_cause_component_class_actor
*cause
=
318 g_new0(struct bt_error_cause_component_class_actor
, 1);
321 BT_LOGD_STR("Creating error cause object (component class actor).");
324 BT_LOGE_STR("Failed to allocate one error cause object.");
328 ret
= init_error_cause(&cause
->base
,
329 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
);
334 set_error_cause_props(&cause
->base
, file_name
, line_no
);
335 ret
= init_component_class_id(&cause
->comp_class_id
, comp_cls
);
340 append_component_class_id_str(cause
->base
.module_name
,
341 &cause
->comp_class_id
);
342 BT_LIB_LOGD("Created error cause object: %!+r", cause
);
347 destroy_error_cause(&cause
->base
);
356 struct bt_error_cause_message_iterator_actor
*
357 create_error_cause_message_iterator_actor(struct bt_message_iterator
*iter
,
358 const char *file_name
, uint64_t line_no
)
360 struct bt_error_cause_message_iterator_actor
*cause
;
361 struct bt_message_iterator
*input_port_iter
;
364 BT_LOGD_STR("Creating error cause object (message iterator actor).");
367 * This can only be created from within a graph, from a user
368 * message iterator, which is a self component port input
371 input_port_iter
= (void *) iter
;
372 cause
= g_new0(struct bt_error_cause_message_iterator_actor
, 1);
374 BT_LOGE_STR("Failed to allocate one error cause object.");
378 ret
= init_error_cause(&cause
->base
,
379 BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
);
384 set_error_cause_props(&cause
->base
, file_name
, line_no
);
385 cause
->comp_name
= g_string_new(
386 input_port_iter
->upstream_component
->name
->str
);
387 if (!cause
->comp_name
) {
388 BT_LOGE_STR("Failed to allocate one GString.");
392 cause
->output_port_name
= g_string_new(
393 input_port_iter
->upstream_port
->name
->str
);
394 if (!cause
->output_port_name
) {
395 BT_LOGE_STR("Failed to allocate one GString.");
399 ret
= init_component_class_id(&cause
->comp_class_id
,
400 input_port_iter
->upstream_component
->class);
405 g_string_append_printf(cause
->base
.module_name
, "%s (%s): ",
406 input_port_iter
->upstream_component
->name
->str
,
407 input_port_iter
->upstream_port
->name
->str
);
408 append_component_class_id_str(cause
->base
.module_name
,
409 &cause
->comp_class_id
);
410 BT_LIB_LOGD("Created error cause object: %!+r", cause
);
415 destroy_error_cause(&cause
->base
);
424 struct bt_error
*bt_error_create(void)
426 struct bt_error
*error
;
428 BT_LOGD_STR("Creating error object.");
429 error
= g_new0(struct bt_error
, 1);
431 BT_LOGE_STR("Failed to allocate one error object.");
435 error
->causes
= g_ptr_array_new_with_free_func(
436 (GDestroyNotify
) destroy_error_cause
);
437 if (!error
->causes
) {
438 BT_LOGE_STR("Failed to allocate one GPtrArray.");
442 BT_LOGD("Created error object: addr=%p", error
);
446 bt_error_destroy(error
);
454 void bt_error_destroy(struct bt_error
*error
)
461 g_ptr_array_free(error
->causes
, TRUE
);
462 error
->causes
= NULL
;
472 int bt_error_append_cause_from_unknown(struct bt_error
*error
,
473 const char *module_name
, const char *file_name
,
474 uint64_t line_no
, const char *msg_fmt
, va_list args
)
476 struct bt_error_cause
*cause
= NULL
;
477 int status
= BT_FUNC_STATUS_OK
;
480 BT_ASSERT(module_name
);
481 BT_ASSERT(file_name
);
483 BT_LOGD("Appending error cause from unknown actor: "
484 "module-name=\"%s\", func-name=\"%s\", line-no=%" PRIu64
,
485 module_name
, file_name
, line_no
);
486 cause
= create_error_cause(module_name
, file_name
, line_no
);
488 /* create_error_cause() logs errors */
489 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
493 g_string_append_vprintf(cause
->message
, msg_fmt
, args
);
494 g_ptr_array_add(error
->causes
, cause
);
495 BT_LIB_LOGD("Appended error cause: %!+r", cause
);
499 destroy_error_cause(cause
);
504 int bt_error_append_cause_from_component(
505 struct bt_error
*error
, bt_self_component
*self_comp
,
506 const char *file_name
, uint64_t line_no
,
507 const char *msg_fmt
, va_list args
)
509 struct bt_error_cause_component_actor
*cause
= NULL
;
510 int status
= BT_FUNC_STATUS_OK
;
513 BT_ASSERT(self_comp
);
514 BT_ASSERT(file_name
);
516 BT_LIB_LOGD("Appending error cause from component actor: %![comp-]+c",
518 cause
= create_error_cause_component_actor((void *) self_comp
,
521 /* create_error_cause_component_actor() logs errors */
522 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
526 g_string_append_vprintf(cause
->base
.message
, msg_fmt
, args
);
527 g_ptr_array_add(error
->causes
, cause
);
528 BT_LIB_LOGD("Appended error cause: %!+r", cause
);
533 destroy_error_cause(&cause
->base
);
540 int bt_error_append_cause_from_component_class(
541 struct bt_error
*error
,
542 bt_self_component_class
*self_comp_class
,
543 const char *file_name
, uint64_t line_no
,
544 const char *msg_fmt
, va_list args
)
546 struct bt_error_cause_component_class_actor
*cause
= NULL
;
547 int status
= BT_FUNC_STATUS_OK
;
550 BT_ASSERT(self_comp_class
);
551 BT_ASSERT(file_name
);
553 BT_LIB_LOGD("Appending error cause from component class actor: "
554 "%![comp-cls-]+C", self_comp_class
);
555 cause
= create_error_cause_component_class_actor(
556 (void *) self_comp_class
, file_name
, line_no
);
558 /* create_error_cause_component_class_actor() logs errors */
559 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
563 g_string_append_vprintf(cause
->base
.message
, msg_fmt
, args
);
564 g_ptr_array_add(error
->causes
, cause
);
565 BT_LIB_LOGD("Appended error cause: %!+r", cause
);
570 destroy_error_cause(&cause
->base
);
577 int bt_error_append_cause_from_message_iterator(
578 struct bt_error
*error
, bt_self_message_iterator
*self_iter
,
579 const char *file_name
, uint64_t line_no
,
580 const char *msg_fmt
, va_list args
)
582 struct bt_error_cause_message_iterator_actor
*cause
= NULL
;
583 int status
= BT_FUNC_STATUS_OK
;
586 BT_ASSERT(self_iter
);
587 BT_ASSERT(file_name
);
589 BT_LIB_LOGD("Appending error cause from message iterator actor: "
590 "%![comp-]+i", self_iter
);
591 cause
= create_error_cause_message_iterator_actor(
592 (void *) self_iter
, file_name
, line_no
);
594 /* create_error_cause_message_iterator_actor() logs errors */
595 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
599 g_string_append_vprintf(cause
->base
.message
, msg_fmt
, args
);
600 g_ptr_array_add(error
->causes
, cause
);
601 BT_LIB_LOGD("Appended error cause: %!+r", cause
);
606 destroy_error_cause(&cause
->base
);
613 uint64_t error_cause_count(const bt_error
*error
)
615 return error
->causes
? error
->causes
->len
: 0;
618 uint64_t bt_error_get_cause_count(const bt_error
*error
)
620 BT_ASSERT_PRE_ERROR_NON_NULL(error
);
621 return error_cause_count(error
);
624 void bt_error_release(const struct bt_error
*error
)
626 BT_ASSERT_PRE_ERROR_NON_NULL(error
);
627 bt_error_destroy((void *) error
);
630 const struct bt_error_cause
*bt_error_borrow_cause_by_index(
631 const bt_error
*error
, uint64_t index
)
633 BT_ASSERT_PRE_ERROR_NON_NULL(error
);
634 BT_ASSERT_PRE_VALID_INDEX(index
, error_cause_count(error
));
635 return error
->causes
->pdata
[index
];
638 enum bt_error_cause_actor_type
bt_error_cause_get_actor_type(
639 const struct bt_error_cause
*cause
)
641 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
642 return cause
->actor_type
;
645 const char *bt_error_cause_get_message(const struct bt_error_cause
*cause
)
647 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
648 return cause
->message
->str
;
651 const char *bt_error_cause_get_module_name(const struct bt_error_cause
*cause
)
653 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
654 return cause
->module_name
->str
;
657 const char *bt_error_cause_get_file_name(const struct bt_error_cause
*cause
)
659 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
660 return cause
->file_name
->str
;
663 uint64_t bt_error_cause_get_line_number(const bt_error_cause
*cause
)
665 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
666 return cause
->line_no
;
669 const char *bt_error_cause_component_actor_get_component_name(
670 const struct bt_error_cause
*cause
)
672 const struct bt_error_cause_component_actor
*spec_cause
=
673 (const void *) cause
;
675 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
676 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "component",
677 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
);
678 return spec_cause
->comp_name
->str
;
681 bt_component_class_type
bt_error_cause_component_actor_get_component_class_type(
682 const struct bt_error_cause
*cause
)
684 const struct bt_error_cause_component_actor
*spec_cause
=
685 (const void *) cause
;
687 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
688 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "component",
689 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
);
690 return spec_cause
->comp_class_id
.type
;
693 const char *bt_error_cause_component_actor_get_component_class_name(
694 const struct bt_error_cause
*cause
)
696 const struct bt_error_cause_component_actor
*spec_cause
=
697 (const void *) cause
;
699 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
700 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "component",
701 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
);
702 return spec_cause
->comp_class_id
.name
->str
;
705 const char *bt_error_cause_component_actor_get_plugin_name(
706 const struct bt_error_cause
*cause
)
708 const struct bt_error_cause_component_actor
*spec_cause
=
709 (const void *) cause
;
711 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
712 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "component",
713 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
);
714 return spec_cause
->comp_class_id
.plugin_name
->len
> 0 ?
715 spec_cause
->comp_class_id
.plugin_name
->str
: NULL
;
718 bt_component_class_type
719 bt_error_cause_component_class_actor_get_component_class_type(
720 const struct bt_error_cause
*cause
)
722 const struct bt_error_cause_component_class_actor
*spec_cause
=
723 (const void *) cause
;
725 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
726 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "component-class",
727 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
);
728 return spec_cause
->comp_class_id
.type
;
731 const char *bt_error_cause_component_class_actor_get_component_class_name(
732 const struct bt_error_cause
*cause
)
734 const struct bt_error_cause_component_class_actor
*spec_cause
=
735 (const void *) cause
;
737 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
738 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "component-class",
739 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
);
740 return spec_cause
->comp_class_id
.name
->str
;
743 const char *bt_error_cause_component_class_actor_get_plugin_name(
744 const struct bt_error_cause
*cause
)
746 const struct bt_error_cause_component_class_actor
*spec_cause
=
747 (const void *) cause
;
749 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
750 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "component-class",
751 BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
);
752 return spec_cause
->comp_class_id
.plugin_name
->len
> 0 ?
753 spec_cause
->comp_class_id
.plugin_name
->str
: NULL
;
756 const char *bt_error_cause_message_iterator_actor_get_component_name(
757 const struct bt_error_cause
*cause
)
759 const struct bt_error_cause_message_iterator_actor
*spec_cause
=
760 (const void *) cause
;
762 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
763 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "message-iterator",
764 BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
);
765 return spec_cause
->comp_name
->str
;
769 bt_error_cause_message_iterator_actor_get_component_output_port_name(
770 const struct bt_error_cause
*cause
)
772 const struct bt_error_cause_message_iterator_actor
*spec_cause
=
773 (const void *) cause
;
775 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
776 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "message-iterator",
777 BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
);
778 return spec_cause
->output_port_name
->str
;
781 bt_component_class_type
782 bt_error_cause_message_iterator_actor_get_component_class_type(
783 const struct bt_error_cause
*cause
)
785 const struct bt_error_cause_message_iterator_actor
*spec_cause
=
786 (const void *) cause
;
788 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
789 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "message-iterator",
790 BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
);
791 return spec_cause
->comp_class_id
.type
;
794 const char *bt_error_cause_message_iterator_actor_get_component_class_name(
795 const struct bt_error_cause
*cause
)
797 const struct bt_error_cause_message_iterator_actor
*spec_cause
=
798 (const void *) cause
;
800 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
801 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "message-iterator",
802 BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
);
803 return spec_cause
->comp_class_id
.name
->str
;
806 const char *bt_error_cause_message_iterator_actor_get_plugin_name(
807 const struct bt_error_cause
*cause
)
809 const struct bt_error_cause_message_iterator_actor
*spec_cause
=
810 (const void *) cause
;
812 BT_ASSERT_PRE_ERROR_CAUSE_NON_NULL(cause
);
813 BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause
, "message-iterator",
814 BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
);
815 return spec_cause
->comp_class_id
.plugin_name
->len
> 0 ?
816 spec_cause
->comp_class_id
.plugin_name
->str
: NULL
;