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