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/current-thread.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_current_thread_clear_error();
62 thread_error
= (void *) error
;
63 BT_LOGD("Moved error object as current thread's error: addr=%p",
68 * Creates the current thread's error object if it does not already
72 int try_create_thread_error(void)
74 int status
= BT_FUNC_STATUS_OK
;
80 BT_LOGD_STR("Creating current thread's error object.");
81 thread_error
= bt_error_create();
83 /* bt_error_create() logs errors */
84 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
88 BT_LOGD("Created current thread's error object: addr=%p", thread_error
);
94 enum bt_current_thread_error_append_cause_status
95 bt_current_thread_error_append_cause_from_unknown(
96 const char *module_name
, const char *file_name
,
97 uint64_t line_no
, const char *msg_fmt
, ...)
99 enum bt_current_thread_error_append_cause_status status
=
100 try_create_thread_error();
107 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
108 "error-addr=%p", thread_error
);
109 va_start(args
, msg_fmt
);
110 status
= bt_error_append_cause_from_unknown(thread_error
, module_name
,
111 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();
131 BT_LOGD("Appending error cause to current thread's error from component: "
132 "error-addr=%p", thread_error
);
133 va_start(args
, msg_fmt
);
134 status
= bt_error_append_cause_from_component(thread_error
, self_comp
,
135 file_name
, line_no
, msg_fmt
, args
);
142 enum bt_current_thread_error_append_cause_status
143 bt_current_thread_error_append_cause_from_component_class(
144 bt_self_component_class
*self_comp_class
, const char *file_name
,
145 uint64_t line_no
, const char *msg_fmt
, ...)
147 enum bt_current_thread_error_append_cause_status status
=
148 try_create_thread_error();
155 BT_LOGD("Appending error cause to current thread's error from component class actor: "
156 "error-addr=%p", thread_error
);
157 va_start(args
, msg_fmt
);
158 status
= bt_error_append_cause_from_component_class(thread_error
,
159 self_comp_class
, file_name
, line_no
, msg_fmt
, args
);
166 enum bt_current_thread_error_append_cause_status
167 bt_current_thread_error_append_cause_from_message_iterator(
168 bt_self_message_iterator
*self_iter
, const char *file_name
,
169 uint64_t line_no
, const char *msg_fmt
, ...)
171 enum bt_current_thread_error_append_cause_status status
=
172 try_create_thread_error();
179 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
180 "error-addr=%p", thread_error
);
181 va_start(args
, msg_fmt
);
182 status
= bt_error_append_cause_from_message_iterator(thread_error
,
183 self_iter
, file_name
, line_no
, msg_fmt
, args
);