| 1 | /* |
| 2 | * Copyright (c) 2019 EfficiOS Inc. and Linux Foundation |
| 3 | * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com> |
| 4 | * Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 26 | #define BT_LOG_TAG "PY-COMMON" |
| 27 | #include "logging/log.h" |
| 28 | |
| 29 | #include <Python.h> |
| 30 | |
| 31 | #include "common/assert.h" |
| 32 | #include "py-common.h" |
| 33 | |
| 34 | BT_HIDDEN |
| 35 | GString *bt_py_common_format_exception(int log_level) |
| 36 | { |
| 37 | PyObject *type = NULL; |
| 38 | PyObject *value = NULL; |
| 39 | PyObject *traceback = NULL; |
| 40 | PyObject *traceback_module = NULL; |
| 41 | PyObject *format_exception_func = NULL; |
| 42 | PyObject *exc_str_list = NULL; |
| 43 | GString *msg_buf = NULL; |
| 44 | const char *format_exc_func_name; |
| 45 | Py_ssize_t i; |
| 46 | |
| 47 | BT_ASSERT(PyErr_Occurred()); |
| 48 | PyErr_Fetch(&type, &value, &traceback); |
| 49 | BT_ASSERT(type); |
| 50 | |
| 51 | /* Make sure `value` is what we expected: an instance of `type` */ |
| 52 | PyErr_NormalizeException(&type, &value, &traceback); |
| 53 | |
| 54 | /* |
| 55 | * Import the standard `traceback` module which contains the |
| 56 | * functions to format an exception. |
| 57 | */ |
| 58 | traceback_module = PyImport_ImportModule("traceback"); |
| 59 | if (!traceback_module) { |
| 60 | BT_LOGE_STR("Failed to import `traceback` module."); |
| 61 | goto error; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * `traceback` can be `NULL`, when we fail to call a Python |
| 66 | * function from native code (there is no Python stack at that |
| 67 | * point). For example, a function which takes 5 positional |
| 68 | * arguments, but 8 were given. |
| 69 | */ |
| 70 | format_exc_func_name = traceback ? "format_exception" : |
| 71 | "format_exception_only"; |
| 72 | format_exception_func = PyObject_GetAttrString(traceback_module, |
| 73 | format_exc_func_name); |
| 74 | if (!format_exception_func) { |
| 75 | BT_LOGE("Cannot find `%s` attribute in `traceback` module.", |
| 76 | format_exc_func_name); |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | if (!PyCallable_Check(format_exception_func)) { |
| 81 | BT_LOGE("`traceback.%s` attribute is not callable.", |
| 82 | format_exc_func_name); |
| 83 | goto error; |
| 84 | } |
| 85 | |
| 86 | exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, |
| 87 | type, value, traceback, NULL); |
| 88 | if (!exc_str_list) { |
| 89 | if (BT_LOG_ON_ERROR) { |
| 90 | BT_LOGE("Failed to call `traceback.%s` function:", |
| 91 | format_exc_func_name); |
| 92 | PyErr_Print(); |
| 93 | } |
| 94 | |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | msg_buf = g_string_new(NULL); |
| 99 | |
| 100 | for (i = 0; i < PyList_Size(exc_str_list); i++) { |
| 101 | PyObject *exc_str; |
| 102 | const char *str; |
| 103 | |
| 104 | exc_str = PyList_GetItem(exc_str_list, i); |
| 105 | BT_ASSERT(exc_str); |
| 106 | |
| 107 | /* `str` is owned by `exc_str`, not by us */ |
| 108 | str = PyUnicode_AsUTF8(exc_str); |
| 109 | if (!str) { |
| 110 | if (BT_LOG_ON_ERROR) { |
| 111 | BT_LOGE_STR("PyUnicode_AsUTF8() failed:"); |
| 112 | PyErr_Print(); |
| 113 | } |
| 114 | |
| 115 | goto error; |
| 116 | } |
| 117 | |
| 118 | /* `str` has a trailing newline */ |
| 119 | g_string_append(msg_buf, str); |
| 120 | } |
| 121 | |
| 122 | if (msg_buf->len > 0) { |
| 123 | /* Remove trailing newline if any */ |
| 124 | if (msg_buf->str[msg_buf->len - 1] == '\n') { |
| 125 | g_string_truncate(msg_buf, msg_buf->len - 1); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | goto end; |
| 130 | |
| 131 | error: |
| 132 | if (msg_buf) { |
| 133 | g_string_free(msg_buf, TRUE); |
| 134 | msg_buf = NULL; |
| 135 | } |
| 136 | |
| 137 | end: |
| 138 | Py_XDECREF(exc_str_list); |
| 139 | Py_XDECREF(format_exception_func); |
| 140 | Py_XDECREF(traceback_module); |
| 141 | |
| 142 | /* |
| 143 | * We can safely call PyErr_Restore() because we always call |
| 144 | * PyErr_Fetch(), and having an error indicator is a function's |
| 145 | * precondition. |
| 146 | * |
| 147 | * PyErr_Restore() steals our references. |
| 148 | */ |
| 149 | PyErr_Restore(type, value, traceback); |
| 150 | |
| 151 | return msg_buf; |
| 152 | } |