Visibility hidden by default
[babeltrace.git] / src / lib / current-thread.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/CUR-THREAD"
8 #include "lib/logging.h"
9
10 #include <babeltrace2/babeltrace.h>
11 #include <stdint.h>
12 #include <stdarg.h>
13
14 #include "error.h"
15 #include "common/assert.h"
16 #include "lib/assert-cond.h"
17 #include "lib/func-status.h"
18
19 #define BT_ASSERT_PRE_FILE_NAME_NON_NULL(_file_name) \
20 BT_ASSERT_PRE_NON_NULL("file-name", (_file_name), "File name");
21
22 #define BT_ASSERT_PRE_MSG_FMT_NON_NULL(_msg_fmt) \
23 BT_ASSERT_PRE_NON_NULL("message-format", (_msg_fmt), "Message format");
24
25 /*
26 * This points to the thread's error object, or it's `NULL` if there's
27 * no current error object.
28 */
29 static __thread struct bt_error *thread_error;
30
31 BT_EXPORT
32 const struct bt_error *bt_current_thread_take_error(void)
33 {
34 struct bt_error *error = thread_error;
35
36 thread_error = NULL;
37 BT_LOGD("Took current thread's error object: addr=%p",
38 error);
39 return error;
40 }
41
42 BT_EXPORT
43 void bt_current_thread_clear_error(void)
44 {
45 bt_error_destroy(thread_error);
46 BT_LOGD("Cleared current thread's error object: addr=%p",
47 thread_error);
48 thread_error = NULL;
49 }
50
51 BT_EXPORT
52 void bt_current_thread_move_error(const struct bt_error *error)
53 {
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",
58 thread_error);
59 }
60
61 /*
62 * Creates the current thread's error object if it does not already
63 * exist.
64 */
65 static
66 int try_create_thread_error(void)
67 {
68 int status = BT_FUNC_STATUS_OK;
69
70 if (thread_error) {
71 goto end;
72 }
73
74 BT_LOGD_STR("Creating current thread's error object.");
75 thread_error = bt_error_create();
76 if (!thread_error) {
77 /* bt_error_create() logs errors */
78 status = BT_FUNC_STATUS_MEMORY_ERROR;
79 goto end;
80 }
81
82 BT_LOGD("Created current thread's error object: addr=%p", thread_error);
83
84 end:
85 return status;
86 }
87
88 BT_EXPORT
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, ...)
93 {
94 enum bt_current_thread_error_append_cause_status status =
95 try_create_thread_error();
96 va_list args;
97
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);
101
102 if (status) {
103 goto end;
104 }
105
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);
111 va_end(args);
112
113 end:
114 return status;
115 }
116
117 BT_EXPORT
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, ...)
122 {
123 enum bt_current_thread_error_append_cause_status status =
124 try_create_thread_error();
125 va_list args;
126
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);
130
131 if (status) {
132 goto end;
133 }
134
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);
140 va_end(args);
141
142 end:
143 return status;
144 }
145
146 BT_EXPORT
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, ...)
151 {
152 enum bt_current_thread_error_append_cause_status status =
153 try_create_thread_error();
154 va_list args;
155
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);
159
160 if (status) {
161 goto end;
162 }
163
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);
169 va_end(args);
170
171 end:
172 return status;
173 }
174
175 BT_EXPORT
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, ...)
180 {
181 enum bt_current_thread_error_append_cause_status status =
182 try_create_thread_error();
183 va_list args;
184
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);
188
189 if (status) {
190 goto end;
191 }
192
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);
198 va_end(args);
199
200 end:
201 return status;
202 }
This page took 0.033519 seconds and 4 git commands to generate.