| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include <Python.h> |
| 17 | #include "jpype.h" |
| 18 | #include "jp_tracer.h" |
| 19 | |
| 20 | // GCOVR_EXCL_START |
| 21 | |
| 22 | #if defined(_MSC_VER) && _MSC_VER<1700 |
| 23 | |
| 24 | // Welcome to the year 2008! |
| 25 | namespace std |
| 26 | { |
| 27 | |
| 28 | class mutex |
| 29 | { |
| 30 | } ; |
| 31 | |
| 32 | template <class T> class lock_guard |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | lock_guard(const T& mutex_type) |
| 37 | { |
| 38 | } |
| 39 | } ; |
| 40 | } |
| 41 | |
| 42 | #else |
| 43 | #include <mutex> |
| 44 | #endif |
| 45 | |
| 46 | static int jpype_traceLevel = 0; |
| 47 | static JPypeTracer* jpype_tracer_last = nullptr; |
| 48 | |
| 49 | std::mutex trace_lock; |
| 50 | |
| 51 | #define JPYPE_TRACING_OUTPUT std::cerr |
| 52 | static int INDENT_WIDTH = 2; |
| 53 | static const char *INDENT = |
| 54 | " "; |
| 55 | |
| 56 | static void jpype_indent(int space) |
| 57 | { |
| 58 | space *= INDENT_WIDTH; |
| 59 | while (space > 80) |
| 60 | { |
| 61 | JPYPE_TRACING_OUTPUT << INDENT; |
| 62 | space -= 80; |
| 63 | } |
| 64 | JPYPE_TRACING_OUTPUT << &INDENT[80 - space]; |
| 65 | } |
| 66 | |
| 67 | //This code is not thread safe, thus tracing a multithreaded code is likely |
| 68 | // to result in crashes. |
| 69 | |
| 70 | JPypeTracer::JPypeTracer(const char* name, void* reference) : m_Name(name) |
| 71 | { |
| 72 | m_Error = false; |
| 73 | m_Last = jpype_tracer_last; |
| 74 | jpype_tracer_last = this; |
| 75 | traceIn(name, reference); |
| 76 | } |
| 77 | |
| 78 | JPypeTracer::~JPypeTracer() |
| 79 | { |
| 80 | traceOut(m_Name.c_str(), m_Error); |
| 81 | jpype_tracer_last = m_Last; |
| 82 | } |
| 83 | |
| 84 | void JPypeTracer::traceIn(const char* msg, void* ref) |
| 85 | { |
| 86 | if (_PyJPModule_trace == 0) |
| 87 | return; |
| 88 | |
| 89 | if (jpype_traceLevel < 0) |
| 90 | jpype_traceLevel = 0; |
| 91 | |
| 92 | std::lock_guard<std::mutex> guard(trace_lock); |
| 93 | jpype_indent(jpype_traceLevel); |
| 94 | JPYPE_TRACING_OUTPUT << "> " << msg ; |
| 95 | if (ref != nullptr) |
| 96 | JPYPE_TRACING_OUTPUT << " id=\"" << ref << "\""; |
| 97 | JPYPE_TRACING_OUTPUT << std::endl; |
| 98 | JPYPE_TRACING_OUTPUT.flush(); |
| 99 | jpype_traceLevel++; |
| 100 | } |
| 101 | |
| 102 | void JPypeTracer::traceOut(const char* msg, bool error) |
| 103 | { |
| 104 | if (_PyJPModule_trace == 0) |
| 105 | return; |
| 106 | |
| 107 | std::lock_guard<std::mutex> guard(trace_lock); |
| 108 | jpype_traceLevel--; |
| 109 | jpype_indent(jpype_traceLevel); |
| 110 | if (error) |
| 111 | { |
| 112 | JPYPE_TRACING_OUTPUT << "EXCEPTION! " << msg << std::endl; |
| 113 | } else |
| 114 | { |
| 115 | JPYPE_TRACING_OUTPUT << "< " << msg << std::endl; |
| 116 | } |
| 117 | JPYPE_TRACING_OUTPUT.flush(); |
| 118 | } |
| 119 | |
| 120 | void JPypeTracer::traceJavaObject(const char* msg, const void* ref) |
| 121 | { |
| 122 | if ((_PyJPModule_trace & 4) == 0) |
| 123 | return; |
| 124 | if (ref == (void*) nullptr) |
| 125 | { |
| 126 | JPypeTracer::trace1("JNI", msg); |
| 127 | return; |
| 128 | } |
| 129 | if (ref == (void*) - 1) |
| 130 | { |
| 131 | JPypeTracer::trace1("+ JNI", msg); |
| 132 | jpype_traceLevel++; |
| 133 | return; |
| 134 | } |
| 135 | if (ref == (void*) - 2) |
| 136 | { |
| 137 | jpype_traceLevel--; |
| 138 | JPypeTracer::trace1("- JNI", msg); |
| 139 | return; |
| 140 | } |
| 141 | std::stringstream str; |
| 142 | str << msg << " " << (void*) ref ; |
| 143 | JPypeTracer::trace1("JNI", str.str().c_str()); |
| 144 | } |
| 145 | |
| 146 | void JPypeTracer::tracePythonObject(const char* msg, PyObject* ref) |
| 147 | { |
| 148 | if ((_PyJPModule_trace & 2) == 0) |
| 149 | return; |
| 150 | if (ref != nullptr) |
| 151 | { |
| 152 | std::stringstream str; |
| 153 | str << msg << " " << (void*) ref << " "<< |
| 154 | #ifndef Py_GIL_DISABLED |
| 155 | Py_REFCNT(ref) |
| 156 | #else |
| 157 | -1 |
| 158 | #endif |
| 159 | << " " << Py_TYPE(ref)->tp_name; |
| 160 | JPypeTracer::trace1("PY", str.str().c_str()); |
| 161 | |
| 162 | } else |
| 163 | { |
| 164 | std::stringstream str; |
| 165 | str << msg << " " << (void*) ref; |
| 166 | JPypeTracer::trace1("PY", str.str().c_str()); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void JPypeTracer::trace1(const char* source, const char* msg) |
| 171 | { |
| 172 | if (_PyJPModule_trace == 0) |
| 173 | return; |
| 174 | |
| 175 | std::lock_guard<std::mutex> guard(trace_lock); |
| 176 | string name = "unknown"; |
| 177 | |
| 178 | if (jpype_tracer_last != nullptr) |
| 179 | name = jpype_tracer_last->m_Name; |
| 180 | |
| 181 | jpype_indent(jpype_traceLevel); |
| 182 | if (source != nullptr) |
| 183 | JPYPE_TRACING_OUTPUT << source << ": "; |
| 184 | if (source == nullptr || (_PyJPModule_trace & 16) != 0) |
| 185 | JPYPE_TRACING_OUTPUT << name << ": "; |
| 186 | JPYPE_TRACING_OUTPUT << msg << std::endl; |
| 187 | JPYPE_TRACING_OUTPUT.flush(); |
| 188 | } |
| 189 | |
| 190 | void JPypeTracer::trace2(const char* msg1, const char* msg2) |
| 191 | { |
| 192 | if (_PyJPModule_trace == 0) |
| 193 | return; |
| 194 | |
| 195 | std::lock_guard<std::mutex> guard(trace_lock); |
| 196 | string name = "unknown"; |
| 197 | |
| 198 | if (jpype_tracer_last != nullptr) |
| 199 | name = jpype_tracer_last->m_Name; |
| 200 | |
| 201 | jpype_indent(jpype_traceLevel); |
| 202 | JPYPE_TRACING_OUTPUT << name << ": " << msg1 << " " << msg2 << std::endl; |
| 203 | JPYPE_TRACING_OUTPUT.flush(); |
| 204 | } |
| 205 | |
| 206 | void JPypeTracer::traceLocks(const string& msg, void* ref) |
| 207 | { |
| 208 | std::lock_guard<std::mutex> guard(trace_lock); |
| 209 | JPYPE_TRACING_OUTPUT << msg << ": " << ref << std::endl; |
| 210 | JPYPE_TRACING_OUTPUT.flush(); |
| 211 | } |
| 212 | |
| 213 | // GCOVR_EXCL_STOP |