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
;
32 const struct bt_error
*bt_current_thread_take_error(void)
34 struct bt_error
*error
= thread_error
;
37 BT_LOGD("Took current thread's error object: addr=%p",
43 void bt_current_thread_clear_error(void)
45 bt_error_destroy(thread_error
);
46 BT_LOGD("Cleared current thread's error object: addr=%p",
52 void bt_current_thread_move_error(const struct bt_error
*error
)
54 BT_ASSERT_PRE_ERROR_NON_NULL(error
);
55 bt_current_thread_clear_error();
56 thread_error
= (void *) error
;
57 BT_LOGD("Moved error object as current thread's error: addr=%p",
62 * Creates the current thread's error object if it does not already
66 int try_create_thread_error(void)
68 int status
= BT_FUNC_STATUS_OK
;
74 BT_LOGD_STR("Creating current thread's error object.");
75 thread_error
= bt_error_create();
77 /* bt_error_create() logs errors */
78 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
82 BT_LOGD("Created current thread's error object: addr=%p", thread_error
);
89 enum bt_current_thread_error_append_cause_status
90 bt_current_thread_error_append_cause_from_unknown(
91 const char *module_name
, const char *file_name
,
92 uint64_t line_no
, const char *msg_fmt
, ...)
94 enum bt_current_thread_error_append_cause_status status
=
95 try_create_thread_error();
98 BT_ASSERT_PRE_NON_NULL("module-name", module_name
, "Module name");
99 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
100 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
106 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
107 "error-addr=%p", thread_error
);
108 va_start(args
, msg_fmt
);
109 status
= bt_error_append_cause_from_unknown(thread_error
, module_name
,
110 file_name
, line_no
, msg_fmt
, args
);
118 enum bt_current_thread_error_append_cause_status
119 bt_current_thread_error_append_cause_from_component(
120 bt_self_component
*self_comp
, const char *file_name
,
121 uint64_t line_no
, const char *msg_fmt
, ...)
123 enum bt_current_thread_error_append_cause_status status
=
124 try_create_thread_error();
127 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
128 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
129 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
135 BT_LOGD("Appending error cause to current thread's error from component: "
136 "error-addr=%p", thread_error
);
137 va_start(args
, msg_fmt
);
138 status
= bt_error_append_cause_from_component(thread_error
, self_comp
,
139 file_name
, line_no
, msg_fmt
, args
);
147 enum bt_current_thread_error_append_cause_status
148 bt_current_thread_error_append_cause_from_component_class(
149 bt_self_component_class
*self_comp_class
, const char *file_name
,
150 uint64_t line_no
, const char *msg_fmt
, ...)
152 enum bt_current_thread_error_append_cause_status status
=
153 try_create_thread_error();
156 BT_ASSERT_PRE_COMP_CLS_NON_NULL(self_comp_class
);
157 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
158 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
164 BT_LOGD("Appending error cause to current thread's error from component class actor: "
165 "error-addr=%p", thread_error
);
166 va_start(args
, msg_fmt
);
167 status
= bt_error_append_cause_from_component_class(thread_error
,
168 self_comp_class
, file_name
, line_no
, msg_fmt
, args
);
176 enum bt_current_thread_error_append_cause_status
177 bt_current_thread_error_append_cause_from_message_iterator(
178 bt_self_message_iterator
*self_iter
, const char *file_name
,
179 uint64_t line_no
, const char *msg_fmt
, ...)
181 enum bt_current_thread_error_append_cause_status status
=
182 try_create_thread_error();
185 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_iter
);
186 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name
);
187 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt
);
193 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
194 "error-addr=%p", thread_error
);
195 va_start(args
, msg_fmt
);
196 status
= bt_error_append_cause_from_message_iterator(thread_error
,
197 self_iter
, file_name
, line_no
, msg_fmt
, args
);