1 /*
2     SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "idebugsession.h"
8 #include "iframestackmodel.h"
9 #include "ivariablecontroller.h"
10 #include <debug.h>
11 
12 #include <QFileInfo>
13 
14 
15 namespace KDevelop {
16 
17 class IDebugSessionPrivate
18 {
19 public:
IDebugSessionPrivate(IDebugSession * q)20     explicit IDebugSessionPrivate(IDebugSession* q) : q(q) {}
21 
22     void slotStateChanged(IDebugSession::DebuggerState state);
23 
24     IDebugSession* q;
25 
26     /// Current position in debugged program, gets set when the state changes
27     QUrl m_url;
28     int m_line;
29     QString m_addr;
30 };
31 
IDebugSession()32 IDebugSession::IDebugSession()
33     : d_ptr(new IDebugSessionPrivate(this))
34 {
35     connect(this, &IDebugSession::stateChanged,
36             this, [this](IDebugSession::DebuggerState state) { Q_D(IDebugSession); d->slotStateChanged(state); });
37 }
38 
~IDebugSession()39 IDebugSession::~IDebugSession()
40 {
41 }
42 
isRunning() const43 bool IDebugSession::isRunning() const
44 {
45     DebuggerState s = state();
46     return (s == ActiveState || s == PausedState);
47 }
48 
raiseEvent(event_t e)49 void IDebugSession::raiseEvent(event_t e)
50 {
51     if (IFrameStackModel* model = frameStackModel()) {
52         model->handleEvent(e);
53     }
54     if (IVariableController* variables = variableController()) {
55         variables->handleEvent(e);
56     }
57     // FIXME: consider if we actually need signals
58     emit event(e);
59 }
60 
convertToLocalUrl(const QPair<QUrl,int> & remoteUrl) const61 QPair<QUrl, int> IDebugSession::convertToLocalUrl(const QPair<QUrl, int> &remoteUrl) const
62 {
63     return remoteUrl;
64 }
65 
convertToRemoteUrl(const QPair<QUrl,int> & localUrl) const66 QPair<QUrl, int> IDebugSession::convertToRemoteUrl(const QPair<QUrl, int>& localUrl) const
67 {
68     return localUrl;
69 }
70 
clearCurrentPosition()71 void IDebugSession::clearCurrentPosition()
72 {
73     Q_D(IDebugSession);
74 
75     qCDebug(DEBUGGER);
76     d->m_url.clear();
77     d->m_addr.clear();
78     d->m_line = -1;
79     emit clearExecutionPoint();
80 }
81 
setCurrentPosition(const QUrl & url,int line,const QString & addr)82 void IDebugSession::setCurrentPosition(const QUrl& url, int line, const QString& addr)
83 {
84     Q_D(IDebugSession);
85 
86     qCDebug(DEBUGGER) << url << line << addr;
87 
88     if (url.isEmpty() || !QFileInfo::exists(convertToLocalUrl(qMakePair(url,line)).first.path())) {
89         clearCurrentPosition();
90         d->m_addr = addr;
91         emit showStepInDisassemble(addr);
92     } else {
93         d->m_url = url;
94         d->m_line = line;
95         d->m_addr = addr;
96         emit showStepInSource(url, line, addr);
97     }
98 }
99 
currentUrl() const100 QUrl IDebugSession::currentUrl() const
101 {
102     Q_D(const IDebugSession);
103 
104     return d->m_url;
105 }
106 
currentLine() const107 int IDebugSession::currentLine() const
108 {
109     Q_D(const IDebugSession);
110 
111     return d->m_line;
112 }
113 
currentAddr() const114 QString IDebugSession::currentAddr() const
115 {
116     Q_D(const IDebugSession);
117 
118     return d->m_addr;
119 }
120 
slotStateChanged(IDebugSession::DebuggerState state)121 void IDebugSessionPrivate::slotStateChanged(IDebugSession::DebuggerState state)
122 {
123     if (state != IDebugSession::PausedState) {
124         q->clearCurrentPosition();
125     }
126 }
127 
128 }
129 
130 #include "moc_idebugsession.cpp"
131