Commit | Line | Data |
---|---|---|
553c4bab PP |
1 | /* |
2 | * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
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: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
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 | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_TAG "LIB/CUR-THREAD" | |
24 | #include "lib/logging.h" | |
25 | ||
4fa90f32 | 26 | #include <babeltrace2/babeltrace.h> |
553c4bab PP |
27 | #include <stdint.h> |
28 | #include <stdarg.h> | |
29 | ||
30 | #include "error.h" | |
31 | #include "common/assert.h" | |
32 | #include "lib/assert-pre.h" | |
33 | #include "lib/func-status.h" | |
34 | ||
35 | /* | |
36 | * This points to the thread's error object, or it's `NULL` if there's | |
37 | * no current error object. | |
38 | */ | |
39 | static __thread struct bt_error *thread_error; | |
40 | ||
41 | const struct bt_error *bt_current_thread_take_error(void) | |
42 | { | |
43 | struct bt_error *error = thread_error; | |
44 | ||
45 | thread_error = NULL; | |
46 | BT_LOGD("Took current thread's error object: addr=%p", | |
47 | error); | |
48 | return error; | |
49 | } | |
50 | ||
51 | void bt_current_thread_clear_error(void) | |
52 | { | |
53 | bt_error_destroy(thread_error); | |
54 | BT_LOGD("Cleared current thread's error object: addr=%p", | |
55 | thread_error); | |
56 | thread_error = NULL; | |
57 | } | |
58 | ||
59 | void bt_current_thread_move_error(const struct bt_error *error) | |
60 | { | |
c77d03eb | 61 | BT_ASSERT_PRE_NON_NULL(error, "Error"); |
553c4bab PP |
62 | bt_current_thread_clear_error(); |
63 | thread_error = (void *) error; | |
64 | BT_LOGD("Moved error object as current thread's error: addr=%p", | |
65 | thread_error); | |
66 | } | |
67 | ||
68 | /* | |
69 | * Creates the current thread's error object if it does not already | |
70 | * exist. | |
71 | */ | |
72 | static | |
73 | int try_create_thread_error(void) | |
74 | { | |
75 | int status = BT_FUNC_STATUS_OK; | |
76 | ||
77 | if (thread_error) { | |
78 | goto end; | |
79 | } | |
80 | ||
81 | BT_LOGD_STR("Creating current thread's error object."); | |
82 | thread_error = bt_error_create(); | |
83 | if (!thread_error) { | |
84 | /* bt_error_create() logs errors */ | |
85 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
86 | goto end; | |
87 | } | |
88 | ||
89 | BT_LOGD("Created current thread's error object: addr=%p", thread_error); | |
90 | ||
91 | end: | |
92 | return status; | |
93 | } | |
94 | ||
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, ...) | |
99 | { | |
100 | enum bt_current_thread_error_append_cause_status status = | |
101 | try_create_thread_error(); | |
102 | va_list args; | |
103 | ||
c77d03eb PP |
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"); | |
107 | ||
553c4bab PP |
108 | if (status) { |
109 | goto end; | |
110 | } | |
111 | ||
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); | |
117 | va_end(args); | |
118 | ||
119 | end: | |
120 | return status; | |
121 | } | |
122 | ||
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, ...) | |
127 | { | |
128 | enum bt_current_thread_error_append_cause_status status = | |
129 | try_create_thread_error(); | |
130 | va_list args; | |
131 | ||
c77d03eb PP |
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"); | |
135 | ||
553c4bab PP |
136 | if (status) { |
137 | goto end; | |
138 | } | |
139 | ||
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); | |
145 | va_end(args); | |
146 | ||
147 | end: | |
148 | return status; | |
149 | } | |
150 | ||
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, ...) | |
155 | { | |
156 | enum bt_current_thread_error_append_cause_status status = | |
157 | try_create_thread_error(); | |
158 | va_list args; | |
159 | ||
c77d03eb PP |
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"); | |
163 | ||
553c4bab PP |
164 | if (status) { |
165 | goto end; | |
166 | } | |
167 | ||
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); | |
173 | va_end(args); | |
174 | ||
175 | end: | |
176 | return status; | |
177 | } | |
178 | ||
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, ...) | |
183 | { | |
184 | enum bt_current_thread_error_append_cause_status status = | |
185 | try_create_thread_error(); | |
186 | va_list args; | |
187 | ||
c77d03eb PP |
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"); | |
191 | ||
553c4bab PP |
192 | if (status) { |
193 | goto end; | |
194 | } | |
195 | ||
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); | |
201 | va_end(args); | |
202 | ||
203 | end: | |
204 | return status; | |
205 | } |