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"
20 * This points to the thread's error object, or it's `NULL` if there's
21 * no current error object.
23 static __thread
struct bt_error
*thread_error
;
25 const struct bt_error
*bt_current_thread_take_error(void)
27 struct bt_error
*error
= thread_error
;
30 BT_LOGD("Took current thread's error object: addr=%p",
35 void bt_current_thread_clear_error(void)
37 bt_error_destroy(thread_error
);
38 BT_LOGD("Cleared current thread's error object: addr=%p",
43 void bt_current_thread_move_error(const struct bt_error
*error
)
45 BT_ASSERT_PRE_ERROR_NON_NULL(error
);
46 bt_current_thread_clear_error();
47 thread_error
= (void *) error
;
48 BT_LOGD("Moved error object as current thread's error: addr=%p",
53 * Creates the current thread's error object if it does not already
57 int try_create_thread_error(void)
59 int status
= BT_FUNC_STATUS_OK
;
65 BT_LOGD_STR("Creating current thread's error object.");
66 thread_error
= bt_error_create();
68 /* bt_error_create() logs errors */
69 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
73 BT_LOGD("Created current thread's error object: addr=%p", thread_error
);
79 enum bt_current_thread_error_append_cause_status
80 bt_current_thread_error_append_cause_from_unknown(
81 const char *module_name
, const char *file_name
,
82 uint64_t line_no
, const char *msg_fmt
, ...)
84 enum bt_current_thread_error_append_cause_status status
=
85 try_create_thread_error();
88 BT_ASSERT_PRE_NON_NULL(module_name
, "Module name");
89 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
90 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
96 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
97 "error-addr=%p", thread_error
);
98 va_start(args
, msg_fmt
);
99 status
= bt_error_append_cause_from_unknown(thread_error
, module_name
,
100 file_name
, line_no
, msg_fmt
, args
);
107 enum bt_current_thread_error_append_cause_status
108 bt_current_thread_error_append_cause_from_component(
109 bt_self_component
*self_comp
, const char *file_name
,
110 uint64_t line_no
, const char *msg_fmt
, ...)
112 enum bt_current_thread_error_append_cause_status status
=
113 try_create_thread_error();
116 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
117 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
118 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
124 BT_LOGD("Appending error cause to current thread's error from component: "
125 "error-addr=%p", thread_error
);
126 va_start(args
, msg_fmt
);
127 status
= bt_error_append_cause_from_component(thread_error
, self_comp
,
128 file_name
, line_no
, msg_fmt
, args
);
135 enum bt_current_thread_error_append_cause_status
136 bt_current_thread_error_append_cause_from_component_class(
137 bt_self_component_class
*self_comp_class
, const char *file_name
,
138 uint64_t line_no
, const char *msg_fmt
, ...)
140 enum bt_current_thread_error_append_cause_status status
=
141 try_create_thread_error();
144 BT_ASSERT_PRE_COMP_CLS_NON_NULL(self_comp_class
);
145 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
146 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
152 BT_LOGD("Appending error cause to current thread's error from component class actor: "
153 "error-addr=%p", thread_error
);
154 va_start(args
, msg_fmt
);
155 status
= bt_error_append_cause_from_component_class(thread_error
,
156 self_comp_class
, file_name
, line_no
, msg_fmt
, args
);
163 enum bt_current_thread_error_append_cause_status
164 bt_current_thread_error_append_cause_from_message_iterator(
165 bt_self_message_iterator
*self_iter
, const char *file_name
,
166 uint64_t line_no
, const char *msg_fmt
, ...)
168 enum bt_current_thread_error_append_cause_status status
=
169 try_create_thread_error();
172 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_iter
);
173 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
174 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
180 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
181 "error-addr=%p", thread_error
);
182 va_start(args
, msg_fmt
);
183 status
= bt_error_append_cause_from_message_iterator(thread_error
,
184 self_iter
, file_name
, line_no
, msg_fmt
, args
);