| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef GUI_PYTHONDEBUG_H |
| 24 |
#define GUI_PYTHONDEBUG_H |
| 25 |
|
| 26 |
#include <CXX/Extensions.hxx> |
| 27 |
#include <frameobject.h> |
| 28 |
#include <set> |
| 29 |
#include <QObject> |
| 30 |
#include <FCGlobal.h> |
| 31 |
|
| 32 |
|
| 33 |
namespace Gui |
| 34 |
{ |
| 35 |
|
| 36 |
class Breakpoint |
| 37 |
{ |
| 38 |
public: |
| 39 |
Breakpoint(); |
| 40 |
Breakpoint(const Breakpoint&); |
| 41 |
Breakpoint& operator=(const Breakpoint&); |
| 42 |
|
| 43 |
~Breakpoint(); |
| 44 |
|
| 45 |
const QString& filename() const; |
| 46 |
void setFilename(const QString& fn); |
| 47 |
|
| 48 |
bool operator==(const Breakpoint& bp); |
| 49 |
bool operator==(const QString& fn); |
| 50 |
|
| 51 |
void addLine(int line); |
| 52 |
void removeLine(int line); |
| 53 |
bool checkLine(int line); |
| 54 |
|
| 55 |
int countLines() const; |
| 56 |
int lineIndex(int ind) const; |
| 57 |
|
| 58 |
bool checkBreakpoint(const QString& fn, int line); |
| 59 |
|
| 60 |
private: |
| 61 |
QString _filename; |
| 62 |
std::set<int> _linenums; |
| 63 |
}; |
| 64 |
|
| 65 |
inline const QString& Breakpoint::filename() const |
| 66 |
{ |
| 67 |
return _filename; |
| 68 |
} |
| 69 |
|
| 70 |
inline int Breakpoint::countLines() const |
| 71 |
{ |
| 72 |
return static_cast<int>(_linenums.size()); |
| 73 |
} |
| 74 |
|
| 75 |
inline bool Breakpoint::checkBreakpoint(const QString& fn, int line) |
| 76 |
{ |
| 77 |
assert(!_filename.isEmpty()); |
| 78 |
if (_linenums.find(line) != _linenums.end()) { |
| 79 |
return fn == _filename; |
| 80 |
} |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
inline bool Breakpoint::operator==(const Breakpoint& bp) |
| 85 |
{ |
| 86 |
return _filename == bp._filename; |
| 87 |
} |
| 88 |
|
| 89 |
inline bool Breakpoint::operator==(const QString& fn) |
| 90 |
{ |
| 91 |
return _filename == fn; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @author Werner Mayer |
| 96 |
*/ |
| 97 |
class GuiExport PythonDebugModule: public Py::ExtensionModule<PythonDebugModule> |
| 98 |
{ |
| 99 |
public: |
| 100 |
static void init_module(); |
| 101 |
|
| 102 |
PythonDebugModule(); |
| 103 |
~PythonDebugModule() override; |
| 104 |
|
| 105 |
private: |
| 106 |
Py::Object getFunctionCallCount(const Py::Tuple& a); |
| 107 |
Py::Object getExceptionCount(const Py::Tuple& a); |
| 108 |
Py::Object getLineCount(const Py::Tuple& a); |
| 109 |
Py::Object getFunctionReturnCount(const Py::Tuple& a); |
| 110 |
}; |
| 111 |
|
| 112 |
/** |
| 113 |
* @author Werner Mayer |
| 114 |
*/ |
| 115 |
class GuiExport PythonDebugStdout: public Py::PythonExtension<PythonDebugStdout> |
| 116 |
{ |
| 117 |
public: |
| 118 |
static void init_type(); // announce properties and methods |
| 119 |
|
| 120 |
PythonDebugStdout(); |
| 121 |
~PythonDebugStdout() override; |
| 122 |
|
| 123 |
Py::Object repr() override; |
| 124 |
Py::Object write(const Py::Tuple&); |
| 125 |
Py::Object flush(const Py::Tuple&); |
| 126 |
}; |
| 127 |
|
| 128 |
/** |
| 129 |
* @author Werner Mayer |
| 130 |
*/ |
| 131 |
class GuiExport PythonDebugStderr: public Py::PythonExtension<PythonDebugStderr> |
| 132 |
{ |
| 133 |
public: |
| 134 |
static void init_type(); // announce properties and methods |
| 135 |
|
| 136 |
PythonDebugStderr(); |
| 137 |
~PythonDebugStderr() override; |
| 138 |
|
| 139 |
Py::Object repr() override; |
| 140 |
Py::Object write(const Py::Tuple&); |
| 141 |
}; |
| 142 |
|
| 143 |
/** |
| 144 |
* @author Werner Mayer |
| 145 |
*/ |
| 146 |
class GuiExport PythonDebugExcept: public Py::PythonExtension<PythonDebugExcept> |
| 147 |
{ |
| 148 |
public: |
| 149 |
static void init_type(); // announce properties and methods |
| 150 |
|
| 151 |
PythonDebugExcept(); |
| 152 |
~PythonDebugExcept() override; |
| 153 |
|
| 154 |
Py::Object repr() override; |
| 155 |
Py::Object excepthook(const Py::Tuple&); |
| 156 |
}; |
| 157 |
|
| 158 |
class GuiExport PythonDebugger: public QObject |
| 159 |
{ |
| 160 |
Q_OBJECT |
| 161 |
|
| 162 |
public: |
| 163 |
PythonDebugger(); |
| 164 |
~PythonDebugger() override; |
| 165 |
Breakpoint getBreakpoint(const QString&) const; |
| 166 |
bool toggleBreakpoint(int line, const QString&); |
| 167 |
void runFile(const QString& fn); |
| 168 |
bool isRunning() const; |
| 169 |
bool start(); |
| 170 |
bool stop(); |
| 171 |
void tryStop(); |
| 172 |
void stepOver(); |
| 173 |
void stepInto(); |
| 174 |
void stepRun(); |
| 175 |
void showDebugMarker(const QString&, int line); |
| 176 |
void hideDebugMarker(const QString&); |
| 177 |
|
| 178 |
Q_SIGNALS: |
| 179 |
void signalNextStep(); |
| 180 |
|
| 181 |
private: |
| 182 |
static int tracer_callback(PyObject* obj, PyFrameObject* frame, int what, PyObject* arg); |
| 183 |
|
| 184 |
private: |
| 185 |
struct PythonDebuggerP* d; |
| 186 |
}; |
| 187 |
|
| 188 |
} // namespace Gui |
| 189 |
|
| 190 |
#endif // GUI_PYTHONDEBUG_H |