File indexing completed on 2024-05-12 04:37:36

0001 /*
0002     SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "idebugsession.h"
0008 #include "iframestackmodel.h"
0009 #include "ivariablecontroller.h"
0010 #include <debug.h>
0011 
0012 #include <QFileInfo>
0013 
0014 namespace {
0015 struct PrintPosition
0016 {
0017     const QUrl& url;
0018     const int line;
0019     const QString& addr;
0020 };
0021 
0022 QDebug operator<<(QDebug debug, const PrintPosition& p)
0023 {
0024     const QDebugStateSaver saver(debug);
0025     debug.noquote().nospace() << p.url.toString(QUrl::PreferLocalFile) << ':' << p.line << ", addr: " << p.addr;
0026     return debug;
0027 }
0028 } // unnamed namespace
0029 
0030 namespace KDevelop {
0031 
0032 class IDebugSessionPrivate
0033 {
0034 public:
0035     explicit IDebugSessionPrivate(IDebugSession* q) : q(q) {}
0036 
0037     void slotStateChanged(IDebugSession::DebuggerState state);
0038 
0039     IDebugSession* q;
0040 
0041     /// Current position in debugged program, gets set when the state changes
0042     QUrl m_url;
0043     int m_line = -1;
0044     QString m_addr;
0045 };
0046 
0047 IDebugSession::IDebugSession()
0048     : d_ptr(new IDebugSessionPrivate(this))
0049 {
0050     connect(this, &IDebugSession::stateChanged,
0051             this, [this](IDebugSession::DebuggerState state) { Q_D(IDebugSession); d->slotStateChanged(state); });
0052 }
0053 
0054 IDebugSession::~IDebugSession()
0055 {
0056 }
0057 
0058 bool IDebugSession::isRunning() const
0059 {
0060     DebuggerState s = state();
0061     return (s == ActiveState || s == PausedState);
0062 }
0063 
0064 void IDebugSession::raiseEvent(event_t e)
0065 {
0066     if (IFrameStackModel* model = frameStackModel()) {
0067         model->handleEvent(e);
0068     }
0069     if (IVariableController* variables = variableController()) {
0070         variables->handleEvent(e);
0071     }
0072     // FIXME: consider if we actually need signals
0073     emit event(e);
0074 }
0075 
0076 QPair<QUrl, int> IDebugSession::convertToLocalUrl(const QPair<QUrl, int> &remoteUrl) const
0077 {
0078     return remoteUrl;
0079 }
0080 
0081 QPair<QUrl, int> IDebugSession::convertToRemoteUrl(const QPair<QUrl, int>& localUrl) const
0082 {
0083     return localUrl;
0084 }
0085 
0086 void IDebugSession::clearCurrentPosition()
0087 {
0088     Q_D(IDebugSession);
0089 
0090     // Pad output with spaces to align the printed position and facilitate comparison with setCurrentPosition().
0091     qCDebug(DEBUGGER) << "clearing current position:  " << PrintPosition{d->m_url, d->m_line, d->m_addr};
0092 
0093     d->m_url.clear();
0094     d->m_addr.clear();
0095     d->m_line = -1;
0096     emit clearExecutionPoint();
0097 }
0098 
0099 void IDebugSession::setCurrentPosition(const QUrl& url, int line, const QString& addr)
0100 {
0101     Q_D(IDebugSession);
0102 
0103     qCDebug(DEBUGGER) << "setting current position to:" << PrintPosition{url, line, addr};
0104 
0105     if (url.isEmpty() || !QFileInfo::exists(convertToLocalUrl(qMakePair(url,line)).first.path())) {
0106         clearCurrentPosition();
0107         d->m_addr = addr;
0108         emit showStepInDisassemble(addr);
0109     } else {
0110         d->m_url = url;
0111         d->m_line = line;
0112         d->m_addr = addr;
0113         emit showStepInSource(url, line, addr);
0114     }
0115 }
0116 
0117 QUrl IDebugSession::currentUrl() const
0118 {
0119     Q_D(const IDebugSession);
0120 
0121     return d->m_url;
0122 }
0123 
0124 int IDebugSession::currentLine() const
0125 {
0126     Q_D(const IDebugSession);
0127 
0128     return d->m_line;
0129 }
0130 
0131 QString IDebugSession::currentAddr() const
0132 {
0133     Q_D(const IDebugSession);
0134 
0135     return d->m_addr;
0136 }
0137 
0138 void IDebugSessionPrivate::slotStateChanged(IDebugSession::DebuggerState state)
0139 {
0140     if (state != IDebugSession::PausedState) {
0141         q->clearCurrentPosition();
0142     }
0143 }
0144 
0145 }
0146 
0147 #include "moc_idebugsession.cpp"