Commit | Line | Data |
---|---|---|
7085eeaa PP |
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 | ||
c4f23e30 FD |
29 | #include <stdbool.h> |
30 | ||
7085eeaa PP |
31 | #include <Python.h> |
32 | ||
33 | #include "common/assert.h" | |
34 | #include "py-common.h" | |
35 | ||
36 | BT_HIDDEN | |
cacd0713 SM |
37 | GString *bt_py_common_format_exception(PyObject *py_exc_type, |
38 | PyObject *py_exc_value, PyObject *py_exc_tb, | |
39 | int log_level, bool chain) | |
7085eeaa | 40 | { |
7085eeaa PP |
41 | PyObject *traceback_module = NULL; |
42 | PyObject *format_exception_func = NULL; | |
43 | PyObject *exc_str_list = NULL; | |
44 | GString *msg_buf = NULL; | |
45 | const char *format_exc_func_name; | |
46 | Py_ssize_t i; | |
47 | ||
7085eeaa PP |
48 | /* |
49 | * Import the standard `traceback` module which contains the | |
50 | * functions to format an exception. | |
51 | */ | |
52 | traceback_module = PyImport_ImportModule("traceback"); | |
53 | if (!traceback_module) { | |
54 | BT_LOGE_STR("Failed to import `traceback` module."); | |
55 | goto error; | |
56 | } | |
57 | ||
58 | /* | |
cacd0713 | 59 | * `py_exc_tb` can be `NULL`, when we fail to call a Python |
7085eeaa PP |
60 | * function from native code (there is no Python stack at that |
61 | * point). For example, a function which takes 5 positional | |
62 | * arguments, but 8 were given. | |
63 | */ | |
cacd0713 | 64 | format_exc_func_name = py_exc_tb ? "format_exception" : |
7085eeaa PP |
65 | "format_exception_only"; |
66 | format_exception_func = PyObject_GetAttrString(traceback_module, | |
67 | format_exc_func_name); | |
68 | if (!format_exception_func) { | |
69 | BT_LOGE("Cannot find `%s` attribute in `traceback` module.", | |
70 | format_exc_func_name); | |
71 | goto error; | |
72 | } | |
73 | ||
74 | if (!PyCallable_Check(format_exception_func)) { | |
75 | BT_LOGE("`traceback.%s` attribute is not callable.", | |
76 | format_exc_func_name); | |
77 | goto error; | |
78 | } | |
79 | ||
cacd0713 SM |
80 | /* |
81 | * If we are calling format_exception_only, `py_exc_tb` is NULL, so the | |
82 | * effective argument list is of length 2. | |
83 | */ | |
7085eeaa | 84 | exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, |
cacd0713 SM |
85 | py_exc_type, py_exc_value, py_exc_tb, Py_None /* limit */, |
86 | chain ? Py_True : Py_False /* chain */, NULL); | |
7085eeaa PP |
87 | if (!exc_str_list) { |
88 | if (BT_LOG_ON_ERROR) { | |
89 | BT_LOGE("Failed to call `traceback.%s` function:", | |
90 | format_exc_func_name); | |
91 | PyErr_Print(); | |
92 | } | |
93 | ||
94 | goto error; | |
95 | } | |
96 | ||
97 | msg_buf = g_string_new(NULL); | |
98 | ||
99 | for (i = 0; i < PyList_Size(exc_str_list); i++) { | |
100 | PyObject *exc_str; | |
101 | const char *str; | |
102 | ||
103 | exc_str = PyList_GetItem(exc_str_list, i); | |
104 | BT_ASSERT(exc_str); | |
105 | ||
106 | /* `str` is owned by `exc_str`, not by us */ | |
107 | str = PyUnicode_AsUTF8(exc_str); | |
108 | if (!str) { | |
109 | if (BT_LOG_ON_ERROR) { | |
110 | BT_LOGE_STR("PyUnicode_AsUTF8() failed:"); | |
111 | PyErr_Print(); | |
112 | } | |
113 | ||
114 | goto error; | |
115 | } | |
116 | ||
117 | /* `str` has a trailing newline */ | |
118 | g_string_append(msg_buf, str); | |
119 | } | |
120 | ||
121 | if (msg_buf->len > 0) { | |
122 | /* Remove trailing newline if any */ | |
123 | if (msg_buf->str[msg_buf->len - 1] == '\n') { | |
124 | g_string_truncate(msg_buf, msg_buf->len - 1); | |
125 | } | |
126 | } | |
127 | ||
128 | goto end; | |
129 | ||
130 | error: | |
131 | if (msg_buf) { | |
132 | g_string_free(msg_buf, TRUE); | |
133 | msg_buf = NULL; | |
134 | } | |
135 | ||
136 | end: | |
137 | Py_XDECREF(exc_str_list); | |
138 | Py_XDECREF(format_exception_func); | |
139 | Py_XDECREF(traceback_module); | |
140 | ||
cacd0713 SM |
141 | return msg_buf; |
142 | } | |
143 | ||
144 | BT_HIDDEN | |
145 | GString *bt_py_common_format_current_exception(int log_level) | |
146 | { | |
147 | GString *result; | |
148 | PyObject *py_exc_type = NULL; | |
149 | PyObject *py_exc_value = NULL; | |
150 | PyObject *py_exc_tb = NULL; | |
151 | ||
152 | BT_ASSERT(PyErr_Occurred()); | |
153 | PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb); | |
154 | BT_ASSERT(py_exc_type); | |
155 | ||
156 | /* | |
157 | * Make sure `py_exc_value` is what we expected: an instance of | |
158 | * `py_exc_type`. | |
159 | */ | |
160 | PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb); | |
161 | ||
162 | result = bt_py_common_format_exception(py_exc_type, py_exc_value, | |
163 | py_exc_tb, log_level, true); | |
164 | ||
7085eeaa PP |
165 | /* |
166 | * We can safely call PyErr_Restore() because we always call | |
167 | * PyErr_Fetch(), and having an error indicator is a function's | |
168 | * precondition. | |
169 | * | |
170 | * PyErr_Restore() steals our references. | |
171 | */ | |
cacd0713 | 172 | PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb); |
7085eeaa | 173 | |
cacd0713 | 174 | return result; |
7085eeaa | 175 | } |