forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugAdapterClient.cpp
More file actions
1484 lines (1280 loc) · 52 KB
/
Copy pathDebugAdapterClient.cpp
File metadata and controls
1484 lines (1280 loc) · 52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// file name : LLDBPlugin.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "DebugAdapterClient.hpp"
#include "AsyncProcess/asyncprocess.h"
#include "AsyncProcess/processreaderthread.h"
#include "DAPBreakpointsView.h"
#include "DAPConsoleOutput.hpp"
#include "DAPDebuggerPane.h"
#include "DAPMainView.h"
#include "DAPOutputPane.hpp"
#include "DAPTextView.h"
#include "DAPTooltip.hpp"
#include "DAPWatchesView.h"
#include "DapDebuggerSettingsDlg.h"
#include "DapLocator.hpp"
#include "Debugger/debuggermanager.h"
#include "FileSystemWorkspace/clFileSystemWorkspace.hpp"
#include "StringUtils.h"
#include "clResizableTooltip.h"
#include "clWorkspaceManager.h"
#include "environmentconfig.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "globals.h"
#include "macromanager.h"
#include "wx/msgqueue.h"
#include <wx/aui/framemanager.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
#include <wx/stc/stc.h>
#include <wx/xrc/xmlres.h>
#if USE_SFTP
#include "sftp_settings.h"
#endif
namespace
{
clModuleLogger LOG;
#ifdef __WXMSW__
constexpr bool IS_WINDOWS = true;
#else
constexpr bool IS_WINDOWS = false;
#endif
const wxString DAP_DEBUGGER_PANE = _("Debugger Client");
const wxString DAP_MESSAGE_BOX_TITLE = "CodeLite - Debug Adapter Client";
// Reusing gdb ids so global debugger menu and accelerators work.
const int lldbRunToCursorContextMenuId = XRCID("dbg_run_to_cursor");
const int lldbJumpToCursorContextMenuId = XRCID("dbg_jump_cursor");
const int lldbAddWatchContextMenuId = XRCID("lldb_add_watch");
std::vector<wxString> to_string_array(const clEnvList_t& env_list)
{
std::vector<wxString> arr;
arr.reserve(env_list.size());
for (const auto& vt : env_list) {
arr.emplace_back(vt.first + "=" + vt.second);
}
return arr;
}
wxString get_dap_settings_file()
{
wxFileName fn(clStandardPaths::Get().GetUserDataDir(), "debug-adapter-client.conf");
fn.AppendDir("config");
return fn.GetFullPath();
}
class StdioTransport : public dap::Transport
{
public:
StdioTransport() {}
~StdioTransport() override {}
void SetProcess(DapProcess::Ptr_t process) { m_dap_server = process; }
/**
* @brief return from the network with a given timeout
* @returns true on success, false in case of an error. True is also returned when timeout occurs, check the buffer
* length if it is 0, timeout occurred
*/
bool Read(std::string& buffer, int msTimeout) override
{
if (wxThread::IsMain()) {
LOG_ERROR(LOG) << "StdioTransport::Read is called from the main thread!" << endl;
return false;
}
std::string msg;
switch (m_dap_server->Queue().ReceiveTimeout(msTimeout, msg)) {
case wxMSGQUEUE_NO_ERROR:
case wxMSGQUEUE_TIMEOUT:
buffer.swap(msg);
return true;
default:
return false;
}
}
/**
* @brief send data over the network
* @return number of bytes written
*/
size_t Send(const std::string& buffer) override
{
if (!m_dap_server->Write(buffer)) {
return 0;
}
return buffer.length();
}
private:
DapProcess::Ptr_t m_dap_server;
};
} // namespace
#define CHECK_IS_DAP_CONNECTED() \
if (!m_client.IsConnected()) { \
event.Skip(); \
return; \
}
// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager) { return new DebugAdapterClient(manager); }
CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
static PluginInfo info;
info.SetAuthor(wxT("eran"));
info.SetName(wxT("DebugAdapterClient"));
info.SetDescription(_("Debug Adapter Client"));
info.SetVersion(wxT("v1.0"));
return &info;
}
CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }
DebugAdapterClient::DebugAdapterClient(IManager* manager)
: IPlugin(manager)
, m_terminal_helper(LOG)
, m_isPerspectiveLoaded(false)
{
// setup custom logger for this module
wxFileName logfile(clStandardPaths::Get().GetUserDataDir(), "dap.log");
logfile.AppendDir("logs");
LOG.Open(logfile);
LOG.SetModule("dap");
// even though set to DBG, the check is done against the global log verbosity
LOG.SetCurrentLogLevel(FileLogger::Dbg);
LOG_DEBUG(LOG) << "Debug Adapter Client startd" << endl;
m_longName = _("Debug Adapter Client");
m_shortName = wxT("DebugAdapterClient");
// load settings
m_dap_store.Load(get_dap_settings_file());
RegisterDebuggers();
Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &DebugAdapterClient::OnProcessOutput, this);
Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &DebugAdapterClient::OnProcessTerminated, this);
// UI events
EventNotifier::Get()->Bind(wxEVT_FILE_LOADED, &DebugAdapterClient::OnFileLoaded, this);
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_LOADED, &DebugAdapterClient::OnWorkspaceLoaded, this);
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &DebugAdapterClient::OnWorkspaceClosed, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_START, &DebugAdapterClient::OnDebugStart, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_CONTINUE, &DebugAdapterClient::OnDebugContinue, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_NEXT, &DebugAdapterClient::OnDebugNext, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_STEP_IN, &DebugAdapterClient::OnDebugStepIn, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_STEP_OUT, &DebugAdapterClient::OnDebugStepOut, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_STOP, &DebugAdapterClient::OnDebugStop, this);
EventNotifier::Get()->Bind(wxEVT_DBG_IS_RUNNING, &DebugAdapterClient::OnDebugIsRunning, this);
EventNotifier::Get()->Bind(wxEVT_DBG_CAN_INTERACT, &DebugAdapterClient::OnDebugCanInteract, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_INTERRUPT, &DebugAdapterClient::OnToggleInterrupt, this);
EventNotifier::Get()->Bind(wxEVT_BUILD_STARTING, &DebugAdapterClient::OnBuildStarting, this);
EventNotifier::Get()->Bind(wxEVT_INIT_DONE, &DebugAdapterClient::OnInitDone, this);
EventNotifier::Get()->Bind(wxEVT_DBG_EXPR_TOOLTIP, &DebugAdapterClient::OnDebugTooltip, this);
EventNotifier::Get()->Bind(wxEVT_QUICK_DEBUG, &DebugAdapterClient::OnDebugQuickDebug, this);
EventNotifier::Get()->Bind(wxEVT_TOOLTIP_DESTROY, &DebugAdapterClient::OnDestroyTip, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_CORE_FILE, &DebugAdapterClient::OnDebugCoreFile, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_DELETE_ALL_BREAKPOINTS, &DebugAdapterClient::OnDebugDeleteAllBreakpoints,
this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_ATTACH_TO_PROCESS, &DebugAdapterClient::OnDebugAttachToProcess, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_ENABLE_ALL_BREAKPOINTS, &DebugAdapterClient::OnDebugEnableAllBreakpoints,
this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_DISABLE_ALL_BREAKPOINTS, &DebugAdapterClient::OnDebugDisableAllBreakpoints,
this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_NEXT_INST, &DebugAdapterClient::OnDebugNextInst, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_STEP_I, &DebugAdapterClient::OnDebugVOID, this); // not supported
EventNotifier::Get()->Bind(wxEVT_DBG_UI_SHOW_CURSOR, &DebugAdapterClient::OnDebugShowCursor, this);
wxTheApp->Bind(wxEVT_MENU, &DebugAdapterClient::OnSettings, this, XRCID("lldb_settings"));
wxTheApp->Bind(wxEVT_COMMAND_MENU_SELECTED, &DebugAdapterClient::OnAddWatch, this, lldbAddWatchContextMenuId);
wxTheApp->Bind(wxEVT_IDLE, &DebugAdapterClient::OnIdle, this);
dap::Initialize(); // register all dap objects
m_client.SetWantsLogEvents(true);
m_client.Bind(wxEVT_DAP_INITIALIZE_RESPONSE, &DebugAdapterClient::OnDapInitializeResponse, this);
m_client.Bind(wxEVT_DAP_INITIALIZED_EVENT, &DebugAdapterClient::OnDapInitializedEvent, this);
m_client.Bind(wxEVT_DAP_RUN_IN_TERMINAL_REQUEST, &DebugAdapterClient::OnDapRunInTerminal, this);
m_client.Bind(wxEVT_DAP_EXITED_EVENT, &DebugAdapterClient::OnDapExited, this);
m_client.Bind(wxEVT_DAP_TERMINATED_EVENT, &DebugAdapterClient::OnDapExited, this);
m_client.Bind(wxEVT_DAP_LAUNCH_RESPONSE, &DebugAdapterClient::OnDapLaunchResponse, this);
m_client.Bind(wxEVT_DAP_STOPPED_EVENT, &DebugAdapterClient::OnDapStoppedEvent, this);
m_client.Bind(wxEVT_DAP_THREADS_RESPONSE, &DebugAdapterClient::OnDapThreadsResponse, this);
m_client.Bind(wxEVT_DAP_STACKTRACE_RESPONSE, &DebugAdapterClient::OnDapStackTraceResponse, this);
m_client.Bind(wxEVT_DAP_SCOPES_RESPONSE, &DebugAdapterClient::OnDapScopesResponse, this);
m_client.Bind(wxEVT_DAP_VARIABLES_RESPONSE, &DebugAdapterClient::OnDapVariablesResponse, this);
m_client.Bind(wxEVT_DAP_SET_FUNCTION_BREAKPOINT_RESPONSE, &DebugAdapterClient::OnDapSetFunctionBreakpointResponse,
this);
m_client.Bind(wxEVT_DAP_SET_SOURCE_BREAKPOINT_RESPONSE, &DebugAdapterClient::OnDapSetSourceBreakpointResponse,
this);
m_client.Bind(wxEVT_DAP_LOG_EVENT, &DebugAdapterClient::OnDapLog, this);
m_client.Bind(wxEVT_DAP_BREAKPOINT_EVENT, &DebugAdapterClient::OnDapBreakpointEvent, this);
m_client.Bind(wxEVT_DAP_OUTPUT_EVENT, &DebugAdapterClient::OnDapOutputEvent, this);
m_client.Bind(wxEVT_DAP_MODULE_EVENT, &DebugAdapterClient::OnDapModuleEvent, this);
EventNotifier::Get()->Bind(wxEVT_NOTIFY_PAGE_CLOSING, &DebugAdapterClient::OnPageClosing, this);
}
void DebugAdapterClient::UnPlug()
{
wxDELETE(m_breakpointsHelper);
wxTheApp->Unbind(wxEVT_IDLE, &DebugAdapterClient::OnIdle, this);
// DestroyUI();
DebuggerMgr::Get().UnregisterDebuggers(m_shortName);
// UI events
EventNotifier::Get()->Unbind(wxEVT_FILE_LOADED, &DebugAdapterClient::OnFileLoaded, this);
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_LOADED, &DebugAdapterClient::OnWorkspaceLoaded, this);
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &DebugAdapterClient::OnWorkspaceClosed, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_START, &DebugAdapterClient::OnDebugStart, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_CONTINUE, &DebugAdapterClient::OnDebugContinue, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_NEXT, &DebugAdapterClient::OnDebugNext, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_STOP, &DebugAdapterClient::OnDebugStop, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_IS_RUNNING, &DebugAdapterClient::OnDebugIsRunning, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_CAN_INTERACT, &DebugAdapterClient::OnDebugCanInteract, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_STEP_IN, &DebugAdapterClient::OnDebugStepIn, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_STEP_OUT, &DebugAdapterClient::OnDebugStepOut, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_INTERRUPT, &DebugAdapterClient::OnToggleInterrupt, this);
EventNotifier::Get()->Unbind(wxEVT_BUILD_STARTING, &DebugAdapterClient::OnBuildStarting, this);
EventNotifier::Get()->Unbind(wxEVT_INIT_DONE, &DebugAdapterClient::OnInitDone, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_EXPR_TOOLTIP, &DebugAdapterClient::OnDebugTooltip, this);
EventNotifier::Get()->Unbind(wxEVT_QUICK_DEBUG, &DebugAdapterClient::OnDebugQuickDebug, this);
EventNotifier::Get()->Unbind(wxEVT_TOOLTIP_DESTROY, &DebugAdapterClient::OnDestroyTip, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_CORE_FILE, &DebugAdapterClient::OnDebugCoreFile, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_DELETE_ALL_BREAKPOINTS, &DebugAdapterClient::OnDebugDeleteAllBreakpoints,
this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_ATTACH_TO_PROCESS, &DebugAdapterClient::OnDebugAttachToProcess, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_ENABLE_ALL_BREAKPOINTS, &DebugAdapterClient::OnDebugEnableAllBreakpoints,
this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_DISABLE_ALL_BREAKPOINTS,
&DebugAdapterClient::OnDebugDisableAllBreakpoints, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_STEP_I, &DebugAdapterClient::OnDebugVOID, this); // Not supported
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_NEXT_INST, &DebugAdapterClient::OnDebugNextInst, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_SHOW_CURSOR, &DebugAdapterClient::OnDebugShowCursor, this);
wxTheApp->Unbind(wxEVT_MENU, &DebugAdapterClient::OnSettings, this, XRCID("lldb_settings"));
// Dap events
m_client.Unbind(wxEVT_DAP_INITIALIZE_RESPONSE, &DebugAdapterClient::OnDapInitializeResponse, this);
m_client.Unbind(wxEVT_DAP_INITIALIZED_EVENT, &DebugAdapterClient::OnDapInitializedEvent, this);
m_client.Unbind(wxEVT_DAP_RUN_IN_TERMINAL_REQUEST, &DebugAdapterClient::OnDapRunInTerminal, this);
m_client.Unbind(wxEVT_DAP_EXITED_EVENT, &DebugAdapterClient::OnDapExited, this);
m_client.Unbind(wxEVT_DAP_TERMINATED_EVENT, &DebugAdapterClient::OnDapExited, this);
m_client.Unbind(wxEVT_DAP_LAUNCH_RESPONSE, &DebugAdapterClient::OnDapLaunchResponse, this);
m_client.Unbind(wxEVT_DAP_STOPPED_EVENT, &DebugAdapterClient::OnDapStoppedEvent, this);
m_client.Unbind(wxEVT_DAP_THREADS_RESPONSE, &DebugAdapterClient::OnDapThreadsResponse, this);
m_client.Unbind(wxEVT_DAP_STACKTRACE_RESPONSE, &DebugAdapterClient::OnDapStackTraceResponse, this);
m_client.Unbind(wxEVT_DAP_SCOPES_RESPONSE, &DebugAdapterClient::OnDapScopesResponse, this);
m_client.Unbind(wxEVT_DAP_VARIABLES_RESPONSE, &DebugAdapterClient::OnDapVariablesResponse, this);
m_client.Unbind(wxEVT_DAP_SET_FUNCTION_BREAKPOINT_RESPONSE, &DebugAdapterClient::OnDapSetFunctionBreakpointResponse,
this);
m_client.Unbind(wxEVT_DAP_SET_SOURCE_BREAKPOINT_RESPONSE, &DebugAdapterClient::OnDapSetSourceBreakpointResponse,
this);
m_client.Unbind(wxEVT_DAP_LOG_EVENT, &DebugAdapterClient::OnDapLog, this);
m_client.Unbind(wxEVT_DAP_BREAKPOINT_EVENT, &DebugAdapterClient::OnDapBreakpointEvent, this);
m_client.Unbind(wxEVT_DAP_OUTPUT_EVENT, &DebugAdapterClient::OnDapOutputEvent, this);
m_client.Unbind(wxEVT_DAP_MODULE_EVENT, &DebugAdapterClient::OnDapModuleEvent, this);
EventNotifier::Get()->Unbind(wxEVT_NOTIFY_PAGE_CLOSING, &DebugAdapterClient::OnPageClosing, this);
}
DebugAdapterClient::~DebugAdapterClient() {}
void DebugAdapterClient::RegisterDebuggers()
{
wxArrayString debuggers;
debuggers.reserve(m_dap_store.GetEntries().size());
for (const auto& entry : m_dap_store.GetEntries()) {
debuggers.Add(entry.first);
}
DebuggerMgr::Get().RegisterDebuggers(m_shortName, debuggers);
}
void DebugAdapterClient::CreateToolBar(clToolBarGeneric* toolbar) { wxUnusedVar(toolbar); }
void DebugAdapterClient::CreatePluginMenu(wxMenu* pluginsMenu)
{
// We want to add an entry in the global settings menu
// Menu Bar > Settings > LLDB Settings
// Get the main frame's menubar
auto mb = clGetManager()->GetMenuBar();
if (mb) {
wxMenu* settingsMenu(NULL);
int menuPos = mb->FindMenu(_("Settings"));
if (menuPos != wxNOT_FOUND) {
settingsMenu = mb->GetMenu(menuPos);
if (settingsMenu) {
settingsMenu->Append(XRCID("lldb_settings"), _("Debug Adapter Client..."));
}
}
}
}
void DebugAdapterClient::HookPopupMenu(wxMenu* menu, MenuType type)
{
wxUnusedVar(type);
wxUnusedVar(menu);
}
void DebugAdapterClient::ClearDebuggerMarker()
{
IEditor::List_t editors;
clGetManager()->GetAllEditors(editors);
for (auto editor : editors) {
DAPTextView::ClearMarker(editor->GetCtrl());
}
}
void DebugAdapterClient::RefreshBreakpointsView()
{
if (GetBreakpointsView()) {
GetBreakpointsView()->RefreshView(m_sessionBreakpoints);
}
// clear all breakpoint markers
IEditor::List_t editors;
clGetManager()->GetAllEditors(editors);
for (auto editor : editors) {
editor->DeleteBreakpointMarkers();
}
// update the open editors with breakpoint markers
for (const auto& bp : m_sessionBreakpoints.get_breakpoints()) {
wxString path = NormaliseReceivedPath(bp.source.path);
auto editor = clGetManager()->FindEditor(path);
if (!editor) {
continue;
}
editor->SetBreakpointMarker(bp.line - 1);
}
}
void DebugAdapterClient::OnDebugContinue(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
// call continue
m_client.Continue();
LOG_DEBUG(LOG) << "Sending 'continue' command" << endl;
}
void DebugAdapterClient::OnDebugStart(clDebugEvent& event)
{
if (m_client.IsConnected()) {
// already running - assume "continue"
OnDebugContinue(event);
return;
}
LOG_DEBUG(LOG) << "debug-start event is called for debugger:" << event.GetDebuggerName() << endl;
if (!IsDebuggerOwnedByPlugin(event.GetDebuggerName())) {
event.Skip();
LOG_DEBUG(LOG) << "Not a dap debugger (" << event.GetDebuggerName() << ")" << endl;
return;
}
// fetch the requested debugger details
DapEntry dap_server;
m_dap_store.Get(event.GetDebuggerName(), &dap_server);
LOG_DEBUG(LOG) << "working directory is:" << ::wxGetCwd() << endl;
// the following 4 variables are used for launching the debugger
wxString working_directory;
wxString exepath;
wxString args;
clEnvList_t env;
wxString ssh_account;
if (clCxxWorkspaceST::Get()->IsOpen()) {
//
// standard C++ workspace
//
ProjectPtr project = clCxxWorkspaceST::Get()->GetActiveProject();
if (!project) {
::wxMessageBox(wxString() << _("Could not locate project: ")
<< clCxxWorkspaceST::Get()->GetActiveProjectName(),
DAP_MESSAGE_BOX_TITLE, wxICON_ERROR | wxOK | wxCENTER);
LOG_ERROR(LOG) << "unable to locate project:" << clCxxWorkspaceST::Get()->GetActiveProjectName() << endl;
return;
}
BuildConfigPtr bldConf = project->GetBuildConfiguration();
if (!bldConf) {
::wxMessageBox(wxString() << _("Could not locate the requested build configuration"), DAP_MESSAGE_BOX_TITLE,
wxICON_ERROR | wxOK | wxCENTER);
return;
}
// Determine the executable to debug, working directory and arguments
LOG_DEBUG(LOG) << "Preparing environment variables.." << endl;
env = bldConf->GetEnvironment(project.get());
LOG_DEBUG(LOG) << "Success" << endl;
exepath = bldConf->GetCommand();
// Get the debugging arguments.
if (bldConf->GetUseSeparateDebugArgs()) {
args = bldConf->GetDebugArgs();
} else {
args = bldConf->GetCommandArguments();
}
working_directory = MacroManager::Instance()->Expand(bldConf->GetWorkingDirectory(), m_mgr, project->GetName());
exepath = MacroManager::Instance()->Expand(exepath, m_mgr, project->GetName());
if (working_directory.empty()) {
working_directory = wxGetCwd();
}
wxFileName fn(exepath);
if (fn.IsRelative()) {
fn.MakeAbsolute(working_directory);
}
exepath = fn.GetFullPath();
} else if (clFileSystemWorkspace::Get().IsOpen()) {
//
// Handle file system workspace
//
auto conf = clFileSystemWorkspace::Get().GetSettings().GetSelectedConfig();
if (!conf) {
LOG_ERROR(LOG) << "No active configuration found!" << endl;
return;
}
auto workspace = clWorkspaceManager::Get().GetWorkspace();
bool is_remote = workspace->IsRemote();
ssh_account = workspace->GetSshAccount();
clFileSystemWorkspace::Get().GetExecutable(exepath, args, working_directory);
if (is_remote) {
env = StringUtils::BuildEnvFromString(conf->GetEnvironment());
} else {
env = StringUtils::ResolveEnvList(conf->GetEnvironment());
wxFileName fnExepath(exepath);
if (fnExepath.IsRelative()) {
fnExepath.MakeAbsolute(workspace->GetDir());
}
exepath = fnExepath.GetFullPath();
}
}
if (working_directory.empty()) {
// always pass a working directory
working_directory =
clWorkspaceManager::Get().IsWorkspaceOpened()
? wxFileName(clWorkspaceManager::Get().GetWorkspace()->GetFileName()).GetPath(wxPATH_UNIX)
: ::wxGetCwd();
}
// start the debugger
LOG_DEBUG(LOG) << "Initializing debugger for executable:" << exepath << endl;
if (!InitialiseSession(dap_server, exepath, args, working_directory, ssh_account, env)) {
return;
}
StartAndConnectToDapServer();
}
void DebugAdapterClient::OnDebugNext(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
LOG_DEBUG(LOG) << "-> Next" << endl;
m_client.Next();
}
void DebugAdapterClient::OnDebugStop(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
LOG_DEBUG(LOG) << "-> Stop" << endl;
DoCleanup();
}
void DebugAdapterClient::OnDebugIsRunning(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
event.SetAnswer(m_client.IsConnected());
}
void DebugAdapterClient::OnDebugCanInteract(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
event.SetAnswer(m_client.IsConnected() && m_client.CanInteract());
}
void DebugAdapterClient::OnDebugStepIn(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
m_client.StepIn();
LOG_DEBUG(LOG) << "-> StopIn" << endl;
}
void DebugAdapterClient::OnDebugStepOut(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
m_client.StepOut();
LOG_DEBUG(LOG) << "-> StopOut" << endl;
}
void DebugAdapterClient::RestoreUI()
{
// Save current perspective before destroying the session
if (m_isPerspectiveLoaded) {
m_mgr->SavePerspective("DAP");
// Restore the old perspective
m_mgr->LoadPerspective("Default");
m_isPerspectiveLoaded = false;
}
HideDebuggerUI();
}
void DebugAdapterClient::LoadPerspective()
{
// Save the current persepctive we start debguging
m_mgr->SavePerspective("Default");
// Hide all the panes
auto& panes = m_mgr->GetDockingManager()->GetAllPanes();
for (size_t i = 0; i < panes.size(); ++i) {
auto& pane = panes[i];
if (pane.dock_direction != wxAUI_DOCK_CENTER) {
pane.Hide();
}
}
m_mgr->LoadPerspective("DAP");
m_isPerspectiveLoaded = true;
// Make sure that all the panes are visible
ShowPane(DAP_DEBUGGER_PANE, true);
// Hide the output pane
wxAuiPaneInfo& pi = m_mgr->GetDockingManager()->GetPane("Output View");
if (pi.IsOk() && pi.IsShown()) {
pi.Hide();
}
m_mgr->GetDockingManager()->Update();
}
void DebugAdapterClient::ShowPane(const wxString& paneName, bool show)
{
wxAuiPaneInfo& pi = m_mgr->GetDockingManager()->GetPane(paneName);
if (pi.IsOk()) {
if (show) {
if (!pi.IsShown()) {
pi.Show();
}
} else {
if (pi.IsShown()) {
pi.Hide();
}
}
}
}
void DebugAdapterClient::HideDebuggerUI()
{
// Destroy the callstack window
if (m_debuggerPane) {
wxAuiPaneInfo& pi = m_mgr->GetDockingManager()->GetPane(DAP_DEBUGGER_PANE);
if (pi.IsOk()) {
m_mgr->GetDockingManager()->DetachPane(m_debuggerPane);
}
m_debuggerPane->Destroy();
m_debuggerPane = nullptr;
}
if (m_textView) {
int index = clGetManager()->GetMainNotebook()->FindPage(m_textView);
if (index != wxNOT_FOUND) {
clGetManager()->GetMainNotebook()->RemovePage(index, false);
}
m_textView->Destroy();
m_textView = nullptr;
}
DestroyTooltip();
ClearDebuggerMarker();
m_mgr->GetDockingManager()->Update();
EventNotifier::Get()->TopFrame()->PostSizeEvent();
}
void DebugAdapterClient::InitializeUI()
{
wxWindow* parent = m_mgr->GetDockingManager()->GetManagedWindow();
if (!m_debuggerPane) {
m_debuggerPane = new DAPDebuggerPane(parent, this, LOG);
m_mgr->GetDockingManager()->AddPane(m_debuggerPane, wxAuiPaneInfo()
.MinSize(300, 300)
.Layer(10)
.Bottom()
.Position(1)
.CloseButton(false)
.Caption(DAP_DEBUGGER_PANE)
.Name(DAP_DEBUGGER_PANE));
}
if (!m_textView) {
m_textView = new DAPTextView(clGetManager()->GetMainNotebook());
clGetManager()->GetMainNotebook()->AddPage(m_textView, _("Debug Adapter Client"), true);
}
}
void DebugAdapterClient::DoCleanup()
{
m_client.Reset();
ClearDebuggerMarker();
m_raisOnBpHit = false;
StopProcess();
m_session.Clear();
m_terminal_helper.Terminate();
m_sessionBreakpoints.clear();
wxDELETE(m_breakpointsHelper);
// clear all breakpoint markers
IEditor::List_t editors;
clGetManager()->GetAllEditors(editors);
for (auto editor : editors) {
editor->DeleteBreakpointMarkers();
}
clDebuggerBreakpoint::Vec_t all_bps;
clGetManager()->GetAllBreakpoints(all_bps);
for (const auto& bp : all_bps) {
if (bp.file.empty()) {
continue;
}
auto editor = clGetManager()->FindEditor(bp.file);
if (!editor) {
continue;
}
editor->SetBreakpointMarker(bp.lineno - 1);
}
}
void DebugAdapterClient::OnWorkspaceClosed(clWorkspaceEvent& event)
{
event.Skip();
DoCleanup();
}
void DebugAdapterClient::OnWorkspaceLoaded(clWorkspaceEvent& event) { event.Skip(); }
void DebugAdapterClient::OnToggleInterrupt(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
event.Skip();
m_client.Pause();
}
void DebugAdapterClient::OnBuildStarting(clBuildEvent& event)
{
if (m_client.IsConnected()) {
// lldb session is active, prompt the user
if (::wxMessageBox(_("A debug session is running\nCancel debug session and continue building?"),
DAP_MESSAGE_BOX_TITLE, wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT | wxCENTER) == wxYES) {
clDebugEvent dummy;
OnDebugStop(dummy);
event.Skip();
} else {
// do nothing - this will cancel the build
}
} else {
event.Skip();
}
}
void DebugAdapterClient::OnAddWatch(wxCommandEvent& event)
{
CHECK_IS_DAP_CONNECTED();
// FIXME
}
void DebugAdapterClient::OnSettings(wxCommandEvent& event)
{
event.Skip();
clDapSettingsStore store = m_dap_store;
DapDebuggerSettingsDlg dlg(EventNotifier::Get()->TopFrame(), store);
if (dlg.ShowModal() != wxID_OK) {
return;
}
m_dap_store = store;
m_dap_store.Save(get_dap_settings_file());
// refresh the list of debuggers we are registering by this plugin
RegisterDebuggers();
}
void DebugAdapterClient::OnInitDone(wxCommandEvent& event)
{
event.Skip();
if (!m_dap_store.empty()) {
return;
}
// this seems like a good time to scan for available debuggers
DapLocator locator;
std::vector<DapEntry> entries;
if (locator.Locate(&entries) > 0) {
m_dap_store.Set(entries);
m_dap_store.Save(get_dap_settings_file());
LOG_SYSTEM(LOG) << "Found and configured" << entries.size() << "dap servers" << endl;
RegisterDebuggers();
}
}
void DebugAdapterClient::OnDebugTooltip(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
DestroyTooltip();
wxString word = event.GetString();
int frame_id = GetThreadsView()->GetCurrentFrameId();
m_client.EvaluateExpression(
word, frame_id, dap::EvaluateContext::HOVER,
[this, word](bool success, const wxString& result, const wxString& type, int variablesReference) {
if (!success) {
clGetManager()->SetStatusMessage(_("Failed to evaluate expression: ") + word);
return;
}
auto editor = clGetManager()->GetActiveEditor();
CHECK_PTR_RET(editor);
m_tooltip = new DAPTooltip(&m_client, word, result, type, variablesReference);
m_tooltip->Move(::wxGetMousePosition());
m_tooltip->Show();
});
}
void DebugAdapterClient::OnDestroyTip(clCommandEvent& event)
{
event.Skip();
DestroyTooltip();
}
void DebugAdapterClient::OnDebugQuickDebug(clDebugEvent& event)
{
if (!IsDebuggerOwnedByPlugin(event.GetDebuggerName())) {
event.Skip();
return;
}
// ours to handle
event.Skip(false);
wxString exe_to_debug = event.GetExecutableName();
const wxString& working_dir = event.GetWorkingDirectory();
const wxString& args = event.GetArguments();
wxFileName fnExepath(exe_to_debug);
if (fnExepath.IsRelative()) {
wxString cwd;
if (clFileSystemWorkspace::Get().IsOpen()) {
cwd = clFileSystemWorkspace::Get().GetDir();
}
fnExepath.MakeAbsolute(cwd);
}
#ifdef __WXMSW__
fnExepath.SetExt("exe");
#endif
exe_to_debug = fnExepath.GetFullPath();
// fetch the requested debugger details
DapEntry dap_server;
m_dap_store.Get(event.GetDebuggerName(), &dap_server);
auto env = PrepareEnvForFileSystemWorkspace(dap_server, !event.IsSSHDebugging());
if (!InitialiseSession(dap_server, exe_to_debug, args, working_dir, event.GetSshAccount(), env)) {
return;
}
StartAndConnectToDapServer();
}
void DebugAdapterClient::OnDebugCoreFile(clDebugEvent& event)
{
// FIXME
event.Skip();
}
void DebugAdapterClient::OnDebugAttachToProcess(clDebugEvent& event)
{
// FIXME
event.Skip();
}
void DebugAdapterClient::OnDebugDeleteAllBreakpoints(clDebugEvent& event)
{
event.Skip();
// FIXME
}
void DebugAdapterClient::OnDebugDisableAllBreakpoints(clDebugEvent& event) { event.Skip(); }
void DebugAdapterClient::OnDebugEnableAllBreakpoints(clDebugEvent& event) { event.Skip(); }
void DebugAdapterClient::OnDebugVOID(clDebugEvent& event) { CHECK_IS_DAP_CONNECTED(); }
void DebugAdapterClient::OnDebugNextInst(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
m_client.Next(wxNOT_FOUND, true, dap::SteppingGranularity::INSTRUCTION);
}
void DebugAdapterClient::OnDebugShowCursor(clDebugEvent& event)
{
CHECK_IS_DAP_CONNECTED();
// FIXME
}
/// --------------------------------------------------------------------------
/// dap events starting here
/// --------------------------------------------------------------------------
void DebugAdapterClient::OnDapExited(DAPEvent& event)
{
event.Skip();
LOG_DEBUG(LOG) << "dap-server exited" << endl;
DoCleanup();
}
void DebugAdapterClient::OnDapLog(DAPEvent& event)
{
event.Skip();
LOG_DEBUG(LOG) << event.GetString() << endl;
}
void DebugAdapterClient::OnDapOutputEvent(DAPEvent& event)
{
if (GetOutputView()) {
GetOutputView()->AddEvent(event.GetDapEvent()->As<dap::OutputEvent>());
}
}
void DebugAdapterClient::OnDapModuleEvent(DAPEvent& event)
{
CHECK_IS_DAP_CONNECTED();
if (GetOutputView()) {
GetOutputView()->AddEvent(event.GetDapEvent()->As<dap::ModuleEvent>());
}
}
void DebugAdapterClient::OnDapLaunchResponse(DAPEvent& event)
{
// Check that the debugee was started successfully
dap::LaunchResponse* resp = event.GetDapResponse()->As<dap::LaunchResponse>();
if (resp && !resp->success) {
// launch failed!
wxMessageBox("Failed to launch debuggee: " + resp->message, DAP_MESSAGE_BOX_TITLE,
wxICON_ERROR | wxOK | wxOK_DEFAULT | wxCENTRE);
CallAfter(&DebugAdapterClient::DoCleanup);
}
}
void DebugAdapterClient::OnDapInitializeResponse(DAPEvent& event)
{
if (m_session.working_directory.empty() && m_session.dap_server.GetLaunchType() == DapLaunchType::LAUNCH) {
// ensure we have a working directory
m_session.working_directory =
clWorkspaceManager::Get().IsWorkspaceOpened()
? wxFileName(clWorkspaceManager::Get().GetWorkspace()->GetFileName()).GetPath(wxPATH_UNIX)
: ::wxGetCwd();
}
LOG_DEBUG(LOG) << "got initialize response" << endl;
LOG_DEBUG(LOG) << "Starting debugger for command:" << endl;
LOG_DEBUG(LOG) << m_session.command << endl;
LOG_DEBUG(LOG) << "working directory:" << m_session.working_directory << endl;
// FIXME: apply the environment here
LOG_DEBUG(LOG) << "Calling Launch() with command:" << m_session.command << endl;
if (m_session.dap_server.GetLaunchType() == DapLaunchType::LAUNCH) {
auto v = m_session.command;
m_client.Launch(std::move(v), m_session.working_directory, m_session.MakeEnvironment());
} else {
auto v = m_session.command;
v.erase(v.begin()); // remove the exe and pass just the arguments
m_client.Attach(m_session.m_pid, v);
}
}
/// DAP server responded to our `initialize` request
void DebugAdapterClient::OnDapInitializedEvent(DAPEvent& event)
{
// place a single breakpoint on main
dap::FunctionBreakpoint main_bp{ "main" };
m_session.need_to_set_breakpoints = true;
m_client.SetFunctionBreakpoints({ main_bp });
if (m_breakpointsHelper) {
m_breakpointsHelper->ApplyBreakpoints(wxEmptyString);
}
// place all breakpoints
m_client.ConfigurationDone();
}
void DebugAdapterClient::OnDapStoppedEvent(DAPEvent& event)
{
// raise CodeLite
EventNotifier::Get()->TopFrame()->Raise();
// got stopped event
if (m_session.need_to_set_breakpoints) {
if (m_breakpointsHelper) {
m_breakpointsHelper->ApplyBreakpoints(wxEmptyString);
}
m_session.need_to_set_breakpoints = false;
}
LOG_DEBUG(LOG) << " *** DAP Stopped Event *** " << endl;
dap::StoppedEvent* stopped_data = event.GetDapEvent()->As<dap::StoppedEvent>();
if (stopped_data) {
m_client.GetThreads();
}
// update watches if needed
UpdateWatches();
}
void DebugAdapterClient::OnDapThreadsResponse(DAPEvent& event)
{
CHECK_PTR_RET(GetThreadsView());
auto response = event.GetDapResponse()->As<dap::ThreadsResponse>();
CHECK_PTR_RET(response);
GetThreadsView()->UpdateThreads(m_client.GetActiveThreadId(), response);
// get the frames for the active thread
m_client.GetFrames();
}
void DebugAdapterClient::OnDapStackTraceResponse(DAPEvent& event)
{
CHECK_PTR_RET(GetThreadsView());
auto response = event.GetDapResponse()->As<dap::StackTraceResponse>();
CHECK_PTR_RET(response);
GetThreadsView()->UpdateFrames(response->refId, response);
if (!response->stackFrames.empty()) {
auto frame = response->stackFrames[0];