2 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "LIB/CUR-THREAD"
24 #include "lib/logging.h"
26 #include <babeltrace2/babeltrace.h>
31 #include "common/assert.h"
32 #include "lib/assert-pre.h"
33 #include "lib/func-status.h"
36 * This points to the thread's error object, or it's `NULL` if there's
37 * no current error object.
39 static __thread
struct bt_error
*thread_error
;
41 const struct bt_error
*bt_current_thread_take_error(void)
43 struct bt_error
*error
= thread_error
;
46 BT_LOGD("Took current thread's error object: addr=%p",
51 void bt_current_thread_clear_error(void)
53 bt_error_destroy(thread_error
);
54 BT_LOGD("Cleared current thread's error object: addr=%p",
59 void bt_current_thread_move_error(const struct bt_error
*error
)
61 BT_ASSERT_PRE_NON_NULL(error
, "Error");
62 bt_current_thread_clear_error();
63 thread_error
= (void *) error
;
64 BT_LOGD("Moved error object as current thread's error: addr=%p",
69 * Creates the current thread's error object if it does not already
73 int try_create_thread_error(void)
75 int status
= BT_FUNC_STATUS_OK
;
81 BT_LOGD_STR("Creating current thread's error object.");
82 thread_error
= bt_error_create();
84 /* bt_error_create() logs errors */
85 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
89 BT_LOGD("Created current thread's error object: addr=%p", thread_error
);
95 enum bt_current_thread_error_append_cause_status
96 bt_current_thread_error_append_cause_from_unknown(
97 const char *module_name
, const char *file_name
,
98 uint64_t line_no
, const char *msg_fmt
, ...)
100 enum bt_current_thread_error_append_cause_status status
=
101 try_create_thread_error();
104 BT_ASSERT_PRE_NON_NULL(module_name
, "Module name");
105 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
106 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
112 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
113 "error-addr=%p", thread_error
);
114 va_start(args
, msg_fmt
);
115 status
= bt_error_append_cause_from_unknown(thread_error
, module_name
,
116 file_name
, line_no
, msg_fmt
, args
);
123 enum bt_current_thread_error_append_cause_status
124 bt_current_thread_error_append_cause_from_component(
125 bt_self_component
*self_comp
, const char *file_name
,
126 uint64_t line_no
, const char *msg_fmt
, ...)
128 enum bt_current_thread_error_append_cause_status status
=
129 try_create_thread_error();
132 BT_ASSERT_PRE_NON_NULL(self_comp
, "Component");
133 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
134 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
140 BT_LOGD("Appending error cause to current thread's error from component: "
141 "error-addr=%p", thread_error
);
142 va_start(args
, msg_fmt
);
143 status
= bt_error_append_cause_from_component(thread_error
, self_comp
,
144 file_name
, line_no
, msg_fmt
, args
);
151 enum bt_current_thread_error_append_cause_status
152 bt_current_thread_error_append_cause_from_component_class(
153 bt_self_component_class
*self_comp_class
, const char *file_name
,
154 uint64_t line_no
, const char *msg_fmt
, ...)
156 enum bt_current_thread_error_append_cause_status status
=
157 try_create_thread_error();
160 BT_ASSERT_PRE_NON_NULL(self_comp_class
, "Component class");
161 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
162 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
168 BT_LOGD("Appending error cause to current thread's error from component class actor: "
169 "error-addr=%p", thread_error
);
170 va_start(args
, msg_fmt
);
171 status
= bt_error_append_cause_from_component_class(thread_error
,
172 self_comp_class
, file_name
, line_no
, msg_fmt
, args
);
179 enum bt_current_thread_error_append_cause_status
180 bt_current_thread_error_append_cause_from_message_iterator(
181 bt_self_message_iterator
*self_iter
, const char *file_name
,
182 uint64_t line_no
, const char *msg_fmt
, ...)
184 enum bt_current_thread_error_append_cause_status status
=
185 try_create_thread_error();
188 BT_ASSERT_PRE_NON_NULL(self_iter
, "Message iterator");
189 BT_ASSERT_PRE_NON_NULL(file_name
, "File name");
190 BT_ASSERT_PRE_NON_NULL(msg_fmt
, "Message format string");
196 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
197 "error-addr=%p", thread_error
);
198 va_start(args
, msg_fmt
);
199 status
= bt_error_append_cause_from_message_iterator(thread_error
,
200 self_iter
, file_name
, line_no
, msg_fmt
, args
);