|
| 1 | +/* -*- mode: c++; indent-tabs-mode: nil -*- */ |
| 2 | +/** @file QorePythonProgram.cpp defines the QorePythonProgram class */ |
| 3 | +/* |
| 4 | + QorePythonStackLocationHelper.cpp |
| 5 | +
|
| 6 | + Qore Programming Language |
| 7 | +
|
| 8 | + Copyright 2020 - 2022 Qore Technologies, s.r.o. |
| 9 | +
|
| 10 | + This library is free software; you can redistribute it and/or |
| 11 | + modify it under the terms of the GNU Lesser General Public |
| 12 | + License as published by the Free Software Foundation; either |
| 13 | + version 2.1 of the License, or (at your option) any later version. |
| 14 | +
|
| 15 | + This library is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + Lesser General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Lesser General Public |
| 21 | + License along with this library; if not, write to the Free Software |
| 22 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "QorePythonStackLocationHelper.h" |
| 26 | +#include "QorePythonProgram.h" |
| 27 | + |
| 28 | +#include <frameobject.h> |
| 29 | + |
| 30 | +std::string QorePythonStackLocationHelper::python_no_call_name = "<python_module_no_runtime_stack_info>"; |
| 31 | +QoreExternalProgramLocationWrapper QorePythonStackLocationHelper::python_loc_builtin("<python_module_unknown>", -1, |
| 32 | + -1); |
| 33 | + |
| 34 | +QorePythonReferenceHolder QorePythonStackLocationHelper::_getframe; |
| 35 | +QorePythonReferenceHolder QorePythonStackLocationHelper::normpath; |
| 36 | + |
| 37 | +int QorePythonStackLocationHelper::staticInit() { |
| 38 | + { |
| 39 | + QorePythonReferenceHolder sys(PyImport_ImportModule("sys")); |
| 40 | + if (!sys) { |
| 41 | + PyErr_Clear(); |
| 42 | + return -1; |
| 43 | + } |
| 44 | + _getframe = PyObject_GetAttrString(*sys, "_getframe"); |
| 45 | + if (!_getframe || !PyCallable_Check(*_getframe)) { |
| 46 | + PyErr_Clear(); |
| 47 | + return -1; |
| 48 | + } |
| 49 | + } |
| 50 | + { |
| 51 | + QorePythonReferenceHolder path(PyImport_ImportModule("os.path")); |
| 52 | + if (!path) { |
| 53 | + PyErr_Clear(); |
| 54 | + return -1; |
| 55 | + } |
| 56 | + normpath = PyObject_GetAttrString(*path, "normpath"); |
| 57 | + if (!normpath || !PyCallable_Check(*normpath)) { |
| 58 | + PyErr_Clear(); |
| 59 | + return -1; |
| 60 | + } |
| 61 | + } |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +QorePythonStackLocationHelper::QorePythonStackLocationHelper(QorePythonProgram* py_pgm) : py_pgm(py_pgm) { |
| 66 | +} |
| 67 | + |
| 68 | +const std::string& QorePythonStackLocationHelper::getCallName() const { |
| 69 | + if (tid != q_gettid()) { |
| 70 | + return python_no_call_name; |
| 71 | + } |
| 72 | + checkInit(); |
| 73 | + assert((unsigned)current < size()); |
| 74 | + //printd(5, "QorePythonStackLocationHelper::getCallName() this: %p %d/%d '%s'\n", this, (int)current, (int)size, |
| 75 | + // stack_call[current].c_str()); |
| 76 | + return stack_call[current]; |
| 77 | +} |
| 78 | + |
| 79 | +qore_call_t QorePythonStackLocationHelper::getCallType() const { |
| 80 | + if (tid != q_gettid()) { |
| 81 | + return CT_BUILTIN; |
| 82 | + } |
| 83 | + checkInit(); |
| 84 | + assert((unsigned)current < size()); |
| 85 | + return CT_USER; |
| 86 | +} |
| 87 | + |
| 88 | +const QoreProgramLocation& QorePythonStackLocationHelper::getLocation() const { |
| 89 | + if (tid != q_gettid()) { |
| 90 | + return python_loc_builtin.get(); |
| 91 | + } |
| 92 | + checkInit(); |
| 93 | + assert((unsigned)current < size()); |
| 94 | + //printd(5, "QorePythonStackLocationHelper::getLocation() %s:%d (%s)\n", stack_loc[current].getFile(), stack_loc[current].getStartLine()); |
| 95 | + return stack_loc[current].get(); |
| 96 | +} |
| 97 | + |
| 98 | +const QoreStackLocation* QorePythonStackLocationHelper::getNext() const { |
| 99 | + if (tid != q_gettid()) { |
| 100 | + return stack_next; |
| 101 | + } |
| 102 | + checkInit(); |
| 103 | + assert((unsigned)current < size()); |
| 104 | + // issue #3169: reset the pointer after iterating all the information in the stack |
| 105 | + // the exception stack can be iterated multiple times |
| 106 | + ++current; |
| 107 | + if ((unsigned)current < size()) { |
| 108 | + return this; |
| 109 | + } |
| 110 | + current = 0; |
| 111 | + return stack_next; |
| 112 | +} |
| 113 | + |
| 114 | +void QorePythonStackLocationHelper::checkInit() const { |
| 115 | + assert(tid == q_gettid()); |
| 116 | + if (init) { |
| 117 | + return; |
| 118 | + } |
| 119 | + init = true; |
| 120 | + |
| 121 | + QorePythonHelper qph(py_pgm); |
| 122 | + |
| 123 | + // start at depth = 1 or the first two entries will be identical |
| 124 | + int depth = 1; |
| 125 | + while (true) { |
| 126 | + QorePythonReferenceHolder args(PyTuple_New(1)); |
| 127 | + PyTuple_SET_ITEM(*args, 0, PyLong_FromLong(depth)); |
| 128 | + |
| 129 | + QorePythonReferenceHolder frame_obj(PyEval_CallObject(*_getframe, *args)); |
| 130 | + if (PyErr_Occurred()) { |
| 131 | + PyErr_Clear(); |
| 132 | + break; |
| 133 | + } |
| 134 | + |
| 135 | + PyFrameObject* frame = reinterpret_cast<PyFrameObject*>(*frame_obj); |
| 136 | + |
| 137 | + const char* filename; |
| 138 | + int line; |
| 139 | + const char* funcname; |
| 140 | +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 10 |
| 141 | + // get line number |
| 142 | + QorePythonReferenceHolder code((PyObject*)PyFrame_GetCode(frame)); |
| 143 | + line = PyCode_Addr2Line((PyCodeObject*)*code, PyFrame_GetLasti(frame)); |
| 144 | + QorePythonReferenceHolder filename_obj(PyObject_GetAttrString(*code, "co_filename")); |
| 145 | + filename = PyUnicode_AsUTF8(*filename_obj); |
| 146 | + |
| 147 | + QorePythonReferenceHolder funcname_obj(PyObject_GetAttrString(*code, "co_qualname")); |
| 148 | + funcname = PyUnicode_AsUTF8(*funcname_obj); |
| 149 | +#else |
| 150 | + line = PyCode_Addr2Line(frame->f_code, frame->f_lasti); |
| 151 | + filename = QorePythonProgram::getCString(frame->f_code->co_filename); |
| 152 | + funcname = QorePythonProgram::getCString(frame->f_code->co_name); |
| 153 | +#endif |
| 154 | + // get normalized path |
| 155 | + QorePythonReferenceHolder np_obj(normalizePath(filename)); |
| 156 | + if (!np_obj) { |
| 157 | + break; |
| 158 | + } |
| 159 | + filename = QorePythonProgram::getCString(*np_obj); |
| 160 | + |
| 161 | + QoreExternalProgramLocationWrapper loc(filename, line, line, nullptr, 0, QORE_PYTHON_LANG_NAME); |
| 162 | + stack_loc.push_back(loc); |
| 163 | + stack_call.push_back(funcname); |
| 164 | + ++depth; |
| 165 | + } |
| 166 | + |
| 167 | + if (!size()) { |
| 168 | + stack_call.push_back(python_no_call_name); |
| 169 | + stack_loc.push_back(python_loc_builtin); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +PyObject* QorePythonStackLocationHelper::normalizePath(const char* path) { |
| 174 | + // normalize path |
| 175 | + QorePythonReferenceHolder path_obj(PyUnicode_FromString(path)); |
| 176 | + return normalizePath(path_obj.release()); |
| 177 | +} |
| 178 | + |
| 179 | +PyObject* QorePythonStackLocationHelper::normalizePath(PyObject* path_obj) { |
| 180 | + // normalize path |
| 181 | + QorePythonReferenceHolder normpath_args(PyTuple_New(1)); |
| 182 | + PyTuple_SET_ITEM(*normpath_args, 0, path_obj); |
| 183 | + |
| 184 | + // get normalized path |
| 185 | + QorePythonReferenceHolder np_obj(PyEval_CallObject(*normpath, *normpath_args)); |
| 186 | + if (PyErr_Occurred()) { |
| 187 | + PyErr_Clear(); |
| 188 | + return nullptr; |
| 189 | + } |
| 190 | + return np_obj.release(); |
| 191 | +} |
0 commit comments