Commit | Line | Data |
---|---|---|
612a9870 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
612a9870 PP |
3 | * |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
612a9870 PP |
5 | */ |
6 | ||
c4f23e30 FD |
7 | #include <stdbool.h> |
8 | ||
612a9870 PP |
9 | #include "logging/comp-logging.h" |
10 | ||
11 | static | |
12 | void restore_current_thread_error_and_append_exception_chain_recursive( | |
13 | int active_log_level, PyObject *py_exc_value, | |
14 | bt_self_component_class *self_component_class, | |
15 | bt_self_component *self_component, | |
16 | bt_self_message_iterator *self_message_iterator, | |
17 | const char *module_name) | |
18 | { | |
19 | PyObject *py_exc_cause_value; | |
20 | PyObject *py_exc_type = NULL; | |
21 | PyObject *py_exc_tb = NULL; | |
332c7e11 | 22 | PyObject *py_bt_error_msg = NULL; |
612a9870 PP |
23 | GString *gstr = NULL; |
24 | ||
332c7e11 | 25 | /* If this exception has a (Python) cause, handle that one first. */ |
612a9870 PP |
26 | py_exc_cause_value = PyException_GetCause(py_exc_value); |
27 | if (py_exc_cause_value) { | |
28 | restore_current_thread_error_and_append_exception_chain_recursive( | |
29 | active_log_level, py_exc_cause_value, | |
30 | self_component_class, self_component, | |
31 | self_message_iterator, module_name); | |
32 | } | |
33 | ||
332c7e11 SM |
34 | py_exc_tb = PyException_GetTraceback(py_exc_value); |
35 | ||
612a9870 | 36 | if (PyErr_GivenExceptionMatches(py_exc_value, py_mod_bt2_exc_error_type)) { |
332c7e11 SM |
37 | /* |
38 | * If the raised exception is a bt2._Error, restore the wrapped | |
39 | * error. | |
40 | */ | |
612a9870 PP |
41 | PyObject *py_error_swig_ptr; |
42 | const bt_error *error; | |
43 | int ret; | |
44 | ||
45 | /* | |
332c7e11 SM |
46 | * We never raise a bt2._Error with a (Python) cause: it should |
47 | * be the end of the chain. | |
612a9870 PP |
48 | */ |
49 | BT_ASSERT(!py_exc_cause_value); | |
50 | ||
51 | /* | |
52 | * We steal the error object from the exception, to move | |
53 | * it back as the current thread's error. | |
54 | */ | |
55 | py_error_swig_ptr = PyObject_GetAttrString(py_exc_value, "_ptr"); | |
56 | BT_ASSERT(py_error_swig_ptr); | |
57 | ||
58 | ret = PyObject_SetAttrString(py_exc_value, "_ptr", Py_None); | |
59 | BT_ASSERT(ret == 0); | |
60 | ||
61 | ret = SWIG_ConvertPtr(py_error_swig_ptr, (void **) &error, | |
62 | SWIGTYPE_p_bt_error, 0); | |
63 | BT_ASSERT(ret == 0); | |
64 | ||
332c7e11 SM |
65 | Py_DECREF(py_error_swig_ptr); |
66 | ||
612a9870 PP |
67 | BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error); |
68 | ||
332c7e11 SM |
69 | /* |
70 | * Append a cause with just the traceback and message, not the | |
71 | * full str() of the bt2._Error. We don't want the causes of | |
72 | * this bt2._Error to be included in the cause we create. | |
73 | */ | |
74 | gstr = bt_py_common_format_tb(py_exc_tb, active_log_level); | |
75 | if (!gstr) { | |
76 | /* bt_py_common_format_tb has already warned. */ | |
77 | goto end; | |
78 | } | |
612a9870 | 79 | |
332c7e11 SM |
80 | g_string_prepend(gstr, "Traceback (most recent call last):\n"); |
81 | ||
82 | py_bt_error_msg = PyObject_GetAttrString(py_exc_value, "_msg"); | |
83 | BT_ASSERT(py_bt_error_msg); | |
612a9870 | 84 | |
332c7e11 SM |
85 | g_string_append_printf(gstr, "\nbt2._Error: %s", |
86 | PyUnicode_AsUTF8(py_bt_error_msg)); | |
87 | } else { | |
88 | py_exc_type = PyObject_Type(py_exc_value); | |
89 | ||
90 | gstr = bt_py_common_format_exception(py_exc_type, py_exc_value, | |
91 | py_exc_tb, active_log_level, false); | |
92 | if (!gstr) { | |
93 | /* bt_py_common_format_exception has already warned. */ | |
94 | goto end; | |
95 | } | |
612a9870 PP |
96 | } |
97 | ||
98 | if (self_component_class) { | |
99 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( | |
100 | self_component_class, "%s", gstr->str); | |
101 | } else if (self_component) { | |
102 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( | |
103 | self_component, "%s", gstr->str); | |
104 | } else if (self_message_iterator) { | |
105 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
106 | self_message_iterator, "%s", gstr->str); | |
107 | } else { | |
108 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( | |
109 | module_name, "%s", gstr->str); | |
110 | } | |
111 | ||
112 | end: | |
113 | if (gstr) { | |
114 | g_string_free(gstr, TRUE); | |
115 | } | |
116 | ||
117 | Py_XDECREF(py_exc_cause_value); | |
118 | Py_XDECREF(py_exc_type); | |
119 | Py_XDECREF(py_exc_tb); | |
332c7e11 | 120 | Py_XDECREF(py_bt_error_msg); |
612a9870 PP |
121 | } |
122 | ||
123 | /* | |
124 | * If you have the following code: | |
125 | * | |
126 | * try: | |
127 | * try: | |
128 | * something_that_raises_bt2_error() | |
129 | * except bt2._Error as e1: | |
130 | * raise ValueError from e1 | |
131 | * except ValueError as e2: | |
132 | * raise TypeError from e2 | |
133 | * | |
134 | * We will have the following exception chain: | |
135 | * | |
136 | * TypeError -> ValueError -> bt2._Error | |
137 | * | |
138 | * Where the TypeError is the current exception (obtained from PyErr_Fetch). | |
139 | * | |
140 | * The bt2._Error contains a `struct bt_error *` that used to be the current | |
141 | * thread's error, at the moment the exception was raised. | |
142 | * | |
143 | * This function gets to the bt2._Error and restores the wrapped | |
144 | * `struct bt_error *` as the current thread's error. | |
145 | * | |
146 | * Then, for each exception in the chain, starting with the oldest one, it adds | |
147 | * an error cause to the current thread's error. | |
148 | */ | |
149 | static | |
150 | void restore_bt_error_and_append_current_exception_chain( | |
151 | int active_log_level, | |
152 | bt_self_component_class *self_component_class, | |
153 | bt_self_component *self_component, | |
154 | bt_self_message_iterator *self_message_iterator, | |
155 | const char *module_name) | |
156 | { | |
157 | BT_ASSERT(PyErr_Occurred()); | |
158 | ||
159 | /* Used to access and restore the current exception. */ | |
160 | PyObject *py_exc_type; | |
161 | PyObject *py_exc_value; | |
162 | PyObject *py_exc_tb; | |
163 | ||
164 | /* Fetch and normalize the Python exception. */ | |
165 | PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb); | |
166 | PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb); | |
167 | BT_ASSERT(py_exc_type); | |
168 | BT_ASSERT(py_exc_value); | |
169 | BT_ASSERT(py_exc_tb); | |
170 | ||
171 | /* | |
172 | * Set the exception's traceback so it's possible to get it using | |
173 | * PyException_GetTraceback in | |
174 | * restore_current_thread_error_and_append_exception_chain_recursive. | |
175 | */ | |
176 | PyException_SetTraceback(py_exc_value, py_exc_tb); | |
177 | ||
178 | restore_current_thread_error_and_append_exception_chain_recursive( | |
179 | active_log_level, py_exc_value, self_component_class, | |
180 | self_component, self_message_iterator, module_name); | |
181 | ||
182 | PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb); | |
183 | } | |
184 | ||
185 | static inline | |
84e438af SM |
186 | void log_exception_and_maybe_append_cause( |
187 | int func_log_level, | |
188 | int active_log_level, | |
189 | bool append_error, | |
612a9870 PP |
190 | bt_self_component_class *self_component_class, |
191 | bt_self_component *self_component, | |
192 | bt_self_message_iterator *self_message_iterator, | |
193 | const char *module_name) | |
194 | { | |
195 | GString *gstr; | |
196 | ||
197 | BT_ASSERT(PyErr_Occurred()); | |
198 | gstr = bt_py_common_format_current_exception(active_log_level); | |
199 | if (!gstr) { | |
200 | /* bt_py_common_format_current_exception() logs errors */ | |
201 | goto end; | |
202 | } | |
203 | ||
204 | BT_COMP_LOG_CUR_LVL(func_log_level, active_log_level, self_component, | |
205 | "%s", gstr->str); | |
206 | ||
207 | if (append_error) { | |
208 | restore_bt_error_and_append_current_exception_chain( | |
209 | active_log_level, self_component_class, self_component, | |
210 | self_message_iterator, module_name); | |
211 | ||
212 | } | |
213 | ||
214 | end: | |
215 | if (gstr) { | |
216 | g_string_free(gstr, TRUE); | |
217 | } | |
218 | } | |
219 | ||
220 | static | |
221 | bt_logging_level get_self_component_log_level(bt_self_component *self_comp) | |
222 | { | |
223 | return bt_component_get_logging_level( | |
224 | bt_self_component_as_component(self_comp)); | |
225 | } | |
226 | ||
227 | static | |
228 | bt_logging_level get_self_message_iterator_log_level( | |
229 | bt_self_message_iterator *self_msg_iter) | |
230 | { | |
231 | bt_self_component *self_comp = | |
232 | bt_self_message_iterator_borrow_component(self_msg_iter); | |
233 | ||
234 | return get_self_component_log_level(self_comp); | |
235 | } | |
236 | ||
237 | static inline | |
981f33dd | 238 | void loge_exception_append_cause_clear(const char *module_name, int active_log_level) |
612a9870 | 239 | { |
84e438af | 240 | log_exception_and_maybe_append_cause(BT_LOG_ERROR, active_log_level, |
612a9870 | 241 | true, NULL, NULL, NULL, module_name); |
981f33dd | 242 | PyErr_Clear(); |
612a9870 PP |
243 | } |
244 | ||
612a9870 | 245 | static inline |
981f33dd | 246 | void logw_exception_clear(int active_log_level) |
612a9870 | 247 | { |
84e438af | 248 | log_exception_and_maybe_append_cause(BT_LOG_WARNING, active_log_level, |
612a9870 | 249 | false, NULL, NULL, NULL, NULL); |
981f33dd | 250 | PyErr_Clear(); |
612a9870 | 251 | } |