2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
7 #define BT_LOG_TAG "LIB/CUR-THREAD"
8 #include "lib/logging.h"
10 #include <babeltrace2/babeltrace.h>
15 #include "lib/assert-cond.h"
16 #include "lib/func-status.h"
18 #define BT_ASSERT_PRE_FILE_NAME_NON_NULL(_file_name) \
19 BT_ASSERT_PRE_NON_NULL("file-name", (_file_name), "File name");
21 #define BT_ASSERT_PRE_MSG_FMT_NON_NULL(_msg_fmt) \
22 BT_ASSERT_PRE_NON_NULL("message-format", (_msg_fmt), "Message format");
25 * This points to the thread's error object, or it's `NULL` if there's
26 * no current error object.
28 static __thread
struct bt_error
*thread_error
;
31 const struct bt_error
*bt_current_thread_take_error(void)
33 struct bt_error
*error
= thread_error
;
36 BT_LOGD("Took current thread's error object: addr=%p",
42 void bt_current_thread_clear_error(void)
44 bt_error_destroy(thread_error
);
45 BT_LOGD("Cleared current thread's error object: addr=%p",
51 void bt_current_thread_move_error(const struct bt_error
*error
)
53 BT_ASSERT_PRE_ERROR_NON_NULL(error
);
54 bt_current_thread_clear_error();
55 thread_error
= (void *) error
;
56 BT_LOGD("Moved error object as current thread's error: addr=%p",
61 * Creates the current thread's error object if it does not already
65 int try_create_thread_error(void)
67 int status
= BT_FUNC_STATUS_OK
;
73 BT_LOGD_STR("Creating current thread's error object.");
74 thread_error
= bt_error_create();
76 /* bt_error_create() logs errors */
77 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
81 BT_LOGD("Created current thread's error object: addr=%p", thread_error
);
88 enum bt_current_thread_error_append_cause_status
89 bt_current_thread_error_append_cause_from_unknown(
90 const char *module_name
, const char *file_name
,
91 uint64_t line_no
, const char *msg_fmt
, ...)
93 enum bt_current_thread_error_append_cause_status status
=
94 try_create_thread_error();
97 BT_ASSERT_PRE_NON_NULL("module-name", module_name
, "Module name");
98 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
99 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
105 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
106 "error-addr=%p", thread_error
);
107 va_start(args
, msg_fmt
);
108 status
= bt_error_append_cause_from_unknown(thread_error
, module_name
,
109 file_name
, line_no
, msg_fmt
, args
);
117 enum bt_current_thread_error_append_cause_status
118 bt_current_thread_error_append_cause_from_component(
119 bt_self_component
*self_comp
, const char *file_name
,
120 uint64_t line_no
, const char *msg_fmt
, ...)
122 enum bt_current_thread_error_append_cause_status status
=
123 try_create_thread_error();
126 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
127 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
128 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
134 BT_LOGD("Appending error cause to current thread's error from component: "
135 "error-addr=%p", thread_error
);
136 va_start(args
, msg_fmt
);
137 status
= bt_error_append_cause_from_component(thread_error
, self_comp
,
138 file_name
, line_no
, msg_fmt
, args
);
146 enum bt_current_thread_error_append_cause_status
147 bt_current_thread_error_append_cause_from_component_class(
148 bt_self_component_class
*self_comp_class
, const char *file_name
,
149 uint64_t line_no
, const char *msg_fmt
, ...)
151 enum bt_current_thread_error_append_cause_status status
=
152 try_create_thread_error();
155 BT_ASSERT_PRE_COMP_CLS_NON_NULL(self_comp_class
);
156 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
157 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
163 BT_LOGD("Appending error cause to current thread's error from component class actor: "
164 "error-addr=%p", thread_error
);
165 va_start(args
, msg_fmt
);
166 status
= bt_error_append_cause_from_component_class(thread_error
,
167 self_comp_class
, file_name
, line_no
, msg_fmt
, args
);
175 enum bt_current_thread_error_append_cause_status
176 bt_current_thread_error_append_cause_from_message_iterator(
177 bt_self_message_iterator
*self_iter
, const char *file_name
,
178 uint64_t line_no
, const char *msg_fmt
, ...)
180 enum bt_current_thread_error_append_cause_status status
=
181 try_create_thread_error();
184 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_iter
);
185 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
186 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
192 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
193 "error-addr=%p", thread_error
);
194 va_start(args
, msg_fmt
);
195 status
= bt_error_append_cause_from_message_iterator(thread_error
,
196 self_iter
, file_name
, line_no
, msg_fmt
, args
);