1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#ifndef DEBUGSESSION_H
6#define DEBUGSESSION_H
7
8#include "rawdebugsession.h"
9#include "dap/session.h"
10#include "dap/protocol.h"
11#include "debug.h"
12#include "interface/variable.h"
13
14#include <QSharedPointer>
15#include <QPointer>
16#include <QWidget>
17
18#include <memory>
19
20namespace dap {
21class RawDebugSession;
22}
23
24class RunTimeCfgProvider;
25
26namespace DEBUG_NAMESPACE {
27
28class DebugModel;
29class DebugSession : public QObject, public IDebugSession
30{
31 Q_OBJECT
32public:
33 DebugSession(DebugModel *, QObject *parent = nullptr);
34 ~DebugSession() override;
35
36 const dap::Capabilities &capabilities() const override;
37
38 bool initialize(const char *ip, int port, dap::InitializeRequest &iniRequest) override;
39 bool launch(dap::LaunchRequest &config) override;
40 bool attach(dap::AttachRequest &config) override;
41
42 void restart() override;
43 void terminate(bool restart = false) override;
44 void disconnect(bool terminateDebuggee = true, bool restart = false) override;
45
46 void sendBreakpoints(const QString &sourcePath, dap::array<IBreakpoint> &breakpointsToSend) override;
47 void sendFunctionBreakpoints(dap::array<IFunctionBreakpoint> &fbpts) override;
48 void sendExceptionBreakpoints(dap::array<IExceptionBreakpoint> &exbpts) override;
49 dap::optional<dap::DataBreakpointInfoResponse> dataBreakpointInfo(
50 dap::string &name, dap::optional<dapNumber> variablesReference) override;
51 void sendDataBreakpoints(dap::array<IDataBreakpoint> dataBreakpoints) override;
52 void sendInstructionBreakpoints(dap::array<IInstructionBreakpoint> instructionBreakpoints) override;
53 // dap::array<IPosition> breakpointsLocations(URI uri, number lineNumber);
54 dap::optional<dap::Breakpoint> getDebugProtocolBreakpoint(dap::string &breakpointId) override;
55 // dap::optional<dap::Response> customRequest(dap::string &request, dap::any args);
56 dap::optional<dap::StackTraceResponse> stackTrace(dapNumber threadId, dapNumber startFrame, dapNumber levels) override;
57 dap::optional<IExceptionInfo> exceptionInfo(dapNumber threadId) override;
58 dap::optional<dap::ScopesResponse> scopes(dapNumber frameId, dapNumber threadId) override;
59 dap::optional<dap::VariablesResponse> variables(dapNumber variablesReference,
60 dap::optional<dapNumber> threadId,
61 dap::optional<dap::string> filter,
62 dap::optional<dapNumber> start,
63 dap::optional<dapNumber> count) override;
64 dap::optional<dap::EvaluateResponse> evaluate(
65 dap::string &expression, dapNumber frameId, dap::optional<dap::string> context) override;
66 void restartFrame(dapNumber frameId, dapNumber threadId) override;
67 void setLastSteppingGranularity(dapNumber threadId, dap::optional<dap::SteppingGranularity> granularity) override;
68
69 void next(dap::integer threadId, dap::optional<dap::SteppingGranularity> granularity) override;
70 void stepIn(dap::integer threadId, dap::optional<dap::integer> targetId, dap::optional<dap::SteppingGranularity> granularity) override;
71 void stepOut(dap::integer threadId, dap::optional<dap::SteppingGranularity> granularity) override;
72 void stepBack(dapNumber threadId, dap::optional<dap::SteppingGranularity> granularity) override;
73 void continueDbg(dap::integer threadId) override;
74 void reverseContinue(dapNumber threadId) override;
75 void pause(dap::integer threadId) override;
76 void terminateThreads(dap::array<dapNumber> &threadIds) override;
77 dap::optional<dap::SetVariableResponse> setVariable(
78 dapNumber variablesReference, dap::string &name, dap::string &value) override;
79 dap::optional<dap::SetExpressionResponse> setExpression(
80 dapNumber frameId, dap::string &expression, dap::string &value) override;
81 dap::optional<dap::GotoTargetsResponse> gotoTargets(
82 dap::Source &source, dapNumber line, dapNumber column) override;
83 dap::optional<dap::GotoResponse> goto_(dapNumber threadId, dapNumber targetId) override;
84 dap::optional<dap::StepInTargetsResponse> stepInTargets(dapNumber frameId) override;
85 dap::optional<dap::CancelResponse> cancel(dap::string &progressId) override;
86 // threads.
87 dap::optional<Thread *> getThread(dapNumber threadId) override;
88 dap::optional<dap::array<IThread *>> getAllThreads() const override;
89 void clearThreads(bool removeThreads, dap::optional<dapNumber> reference) override;
90 dap::array<IRawStoppedDetails *> &getStoppedDetails() override;
91 void rawUpdate(IRawModelUpdate *data) override;
92 void fetchThreads(dap::optional<IRawStoppedDetails> stoppedDetails) override;
93 dap::optional<dap::Source> getSourceForUri(QUrl &uri) override;
94 Source *getSource(dap::optional<dap::Source> raw) override;
95
96 dap::string getId() override;
97 dap::integer getThreadId() override;
98 dap::string getLabel() const override;
99 void setName(dap::string &name) override;
100
101 bool getLocals(dap::integer frameId, IVariables *out) override;
102
103 dap::Session *getDapSession() const;
104 dap::RawDebugSession *getRawSession() const;
105
106 dap::array<dap::Thread> fetchThreads(IRawStoppedDetails *stoppedDetails);
107
108 bool launchJavaDap(const QString &workDir,
109 const QString &mainClass,
110 const QString &projectName,
111 const QStringList &classPaths,
112 const QString &javaExec);
113 bool attachPythonDap(int port,
114 const QString &workspace);
115 void closeSession();
116 void disassemble(const dap::string &address);
117signals:
118 void sigRegisterHandlers();
119
120public slots:
121
122private:
123 void shutdown();
124 void onBreakpointHit(const dap::StoppedEvent &event);
125 void onStep(const dap::StoppedEvent &event);
126
127 dap::Source getRawSource(QUrl &uri);
128 void cancelAllRequests();
129
130 bool getVariables(dap::integer variablesRef, IVariables *out, dap::integer depth = 0);
131
132 QSharedPointer<dap::RawDebugSession> raw;
133 QSharedPointer<RunTimeCfgProvider> rtCfgProvider;
134
135 bool initialized = false;
136
137 std::shared_ptr<dap::Session> session;
138
139 std::string id;
140
141 dap::integer threadId = 0;
142 std::map<dapNumber, Thread *> threads;
143 dap::array<dapNumber> threadIds;
144
145 DebugModel *model = nullptr;
146
147 dap::optional<dap::string> name;
148
149 dap::array<IRawStoppedDetails *> stoppedDetails;
150
151 std::map<dap::string, Source *> sources;
152
153 QPointer<QWidget> alertBox;
154};
155}
156
157#endif // DEBUGSESSION_H
158