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 "common/assert.h"
16 #include "lib/assert-cond.h"
17 #include "lib/func-status.h"
19 #define BT_ASSERT_PRE_FILE_NAME_NON_NULL(_file_name) \
20 BT_ASSERT_PRE_NON_NULL("file-name", (_file_name), "File name");
22 #define BT_ASSERT_PRE_MSG_FMT_NON_NULL(_msg_fmt) \
23 BT_ASSERT_PRE_NON_NULL("message-format", (_msg_fmt), "Message format");
26 * This points to the thread's error object, or it's `NULL` if there's
27 * no current error object.
29 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",
41 void bt_current_thread_clear_error(void)
43 bt_error_destroy(thread_error
);
44 BT_LOGD("Cleared current thread's error object: addr=%p",
49 void bt_current_thread_move_error(const struct bt_error
*error
)
51 BT_ASSERT_PRE_ERROR_NON_NULL(error
);
52 bt_current_thread_clear_error();
53 thread_error
= (void *) error
;
54 BT_LOGD("Moved error object as current thread's error: addr=%p",
59 * Creates the current thread's error object if it does not already
63 int try_create_thread_error(void)
65 int status
= BT_FUNC_STATUS_OK
;
71 BT_LOGD_STR("Creating current thread's error object.");
72 thread_error
= bt_error_create();
74 /* bt_error_create() logs errors */
75 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
79 BT_LOGD("Created current thread's error object: addr=%p", thread_error
);
85 enum bt_current_thread_error_append_cause_status
86 bt_current_thread_error_append_cause_from_unknown(
87 const char *module_name
, const char *file_name
,
88 uint64_t line_no
, const char *msg_fmt
, ...)
90 enum bt_current_thread_error_append_cause_status status
=
91 try_create_thread_error();
94 BT_ASSERT_PRE_NON_NULL("module-name", module_name
, "Module name");
95 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
96 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
102 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
103 "error-addr=%p", thread_error
);
104 va_start(args
, msg_fmt
);
105 status
= bt_error_append_cause_from_unknown(thread_error
, module_name
,
106 file_name
, line_no
, msg_fmt
, args
);
113 enum bt_current_thread_error_append_cause_status
114 bt_current_thread_error_append_cause_from_component(
115 bt_self_component
*self_comp
, const char *file_name
,
116 uint64_t line_no
, const char *msg_fmt
, ...)
118 enum bt_current_thread_error_append_cause_status status
=
119 try_create_thread_error();
122 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
123 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
124 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
130 BT_LOGD("Appending error cause to current thread's error from component: "
131 "error-addr=%p", thread_error
);
132 va_start(args
, msg_fmt
);
133 status
= bt_error_append_cause_from_component(thread_error
, self_comp
,
134 file_name
, line_no
, msg_fmt
, args
);
141 enum bt_current_thread_error_append_cause_status
142 bt_current_thread_error_append_cause_from_component_class(
143 bt_self_component_class
*self_comp_class
, const char *file_name
,
144 uint64_t line_no
, const char *msg_fmt
, ...)
146 enum bt_current_thread_error_append_cause_status status
=
147 try_create_thread_error();
150 BT_ASSERT_PRE_COMP_CLS_NON_NULL(self_comp_class
);
151 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
152 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
158 BT_LOGD("Appending error cause to current thread's error from component class actor: "
159 "error-addr=%p", thread_error
);
160 va_start(args
, msg_fmt
);
161 status
= bt_error_append_cause_from_component_class(thread_error
,
162 self_comp_class
, file_name
, line_no
, msg_fmt
, args
);
169 enum bt_current_thread_error_append_cause_status
170 bt_current_thread_error_append_cause_from_message_iterator(
171 bt_self_message_iterator
*self_iter
, const char *file_name
,
172 uint64_t line_no
, const char *msg_fmt
, ...)
174 enum bt_current_thread_error_append_cause_status status
=
175 try_create_thread_error();
178 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_iter
);
179 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
180 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
186 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
187 "error-addr=%p", thread_error
);
188 va_start(args
, msg_fmt
);
189 status
= bt_error_append_cause_from_message_iterator(thread_error
,
190 self_iter
, file_name
, line_no
, msg_fmt
, args
);