forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathDebugger.cpp
2338 lines (2038 loc) · 81.5 KB
/
Debugger.cpp
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
//===-- Debugger.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/Debugger.h"
#include "lldb/Breakpoint/Breakpoint.h"
#include "lldb/Core/DebuggerEvents.h"
#include "lldb/Core/FormatEntity.h"
#include "lldb/Core/Mangled.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Progress.h"
#include "lldb/Core/StreamAsynchronousIO.h"
#include "lldb/DataFormatters/DataVisualization.h"
#include "lldb/Expression/REPL.h"
#include "lldb/Host/File.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/StreamFile.h"
#include "lldb/Host/Terminal.h"
#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionValue.h"
#include "lldb/Interpreter/OptionValueLanguage.h"
#include "lldb/Interpreter/OptionValueProperties.h"
#include "lldb/Interpreter/OptionValueSInt64.h"
#include "lldb/Interpreter/OptionValueString.h"
#include "lldb/Interpreter/Property.h"
#include "lldb/Interpreter/ScriptInterpreter.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/Language.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StructuredDataPlugin.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/TargetList.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadList.h"
#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/Event.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Listener.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/State.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Version/Version.h"
#include "lldb/lldb-enumerations.h"
#if defined(_WIN32)
#include "lldb/Host/windows/PosixApi.h"
#include "lldb/Host/windows/windows.h"
#endif
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <list>
#include <memory>
#include <mutex>
#include <optional>
#include <set>
#include <string>
#include <system_error>
// Includes for pipe()
#if defined(_WIN32)
#include <fcntl.h>
#include <io.h>
#else
#include <unistd.h>
#endif
namespace lldb_private {
class Address;
}
using namespace lldb;
using namespace lldb_private;
static lldb::user_id_t g_unique_id = 1;
static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
#pragma mark Static Functions
static std::recursive_mutex *g_debugger_list_mutex_ptr =
nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
static Debugger::DebuggerList *g_debugger_list_ptr =
nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
static llvm::DefaultThreadPool *g_thread_pool = nullptr;
static constexpr OptionEnumValueElement g_show_disassembly_enum_values[] = {
{
Debugger::eStopDisassemblyTypeNever,
"never",
"Never show disassembly when displaying a stop context.",
},
{
Debugger::eStopDisassemblyTypeNoDebugInfo,
"no-debuginfo",
"Show disassembly when there is no debug information.",
},
{
Debugger::eStopDisassemblyTypeNoSource,
"no-source",
"Show disassembly when there is no source information, or the source "
"file "
"is missing when displaying a stop context.",
},
{
Debugger::eStopDisassemblyTypeAlways,
"always",
"Always show disassembly when displaying a stop context.",
},
};
static constexpr OptionEnumValueElement g_language_enumerators[] = {
{
eScriptLanguageNone,
"none",
"Disable scripting languages.",
},
{
eScriptLanguagePython,
"python",
"Select python as the default scripting language.",
},
{
eScriptLanguageDefault,
"default",
"Select the lldb default as the default scripting language.",
},
};
static constexpr OptionEnumValueElement g_dwim_print_verbosities[] = {
{eDWIMPrintVerbosityNone, "none",
"Use no verbosity when running dwim-print."},
{eDWIMPrintVerbosityExpression, "expression",
"Use partial verbosity when running dwim-print - display a message when "
"`expression` evaluation is used."},
{eDWIMPrintVerbosityFull, "full",
"Use full verbosity when running dwim-print."},
};
static constexpr OptionEnumValueElement s_stop_show_column_values[] = {
{
eStopShowColumnAnsiOrCaret,
"ansi-or-caret",
"Highlight the stop column with ANSI terminal codes when color/ANSI "
"mode is enabled; otherwise, fall back to using a text-only caret (^) "
"as if \"caret-only\" mode was selected.",
},
{
eStopShowColumnAnsi,
"ansi",
"Highlight the stop column with ANSI terminal codes when running LLDB "
"with color/ANSI enabled.",
},
{
eStopShowColumnCaret,
"caret",
"Highlight the stop column with a caret character (^) underneath the "
"stop column. This method introduces a new line in source listings "
"that display thread stop locations.",
},
{
eStopShowColumnNone,
"none",
"Do not highlight the stop column.",
},
};
#define LLDB_PROPERTIES_debugger
#include "CoreProperties.inc"
enum {
#define LLDB_PROPERTIES_debugger
#include "CorePropertiesEnum.inc"
};
LoadPluginCallbackType Debugger::g_load_plugin_callback = nullptr;
Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
VarSetOperationType op,
llvm::StringRef property_path,
llvm::StringRef value) {
bool is_load_script =
(property_path == "target.load-script-from-symbol-file");
// These properties might change how we visualize data.
bool invalidate_data_vis = (property_path == "escape-non-printables");
invalidate_data_vis |=
(property_path == "target.max-zero-padding-in-float-format");
if (invalidate_data_vis) {
DataVisualization::ForceUpdate();
}
TargetSP target_sp;
LoadScriptFromSymFile load_script_old_value = eLoadScriptFromSymFileFalse;
if (is_load_script && exe_ctx && exe_ctx->GetTargetSP()) {
target_sp = exe_ctx->GetTargetSP();
load_script_old_value =
target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
}
Status error(Properties::SetPropertyValue(exe_ctx, op, property_path, value));
if (error.Success()) {
// FIXME it would be nice to have "on-change" callbacks for properties
if (property_path == g_debugger_properties[ePropertyPrompt].name) {
llvm::StringRef new_prompt = GetPrompt();
std::string str = lldb_private::ansi::FormatAnsiTerminalCodes(
new_prompt, GetUseColor());
if (str.length())
new_prompt = str;
GetCommandInterpreter().UpdatePrompt(new_prompt);
auto bytes = std::make_unique<EventDataBytes>(new_prompt);
auto prompt_change_event_sp = std::make_shared<Event>(
CommandInterpreter::eBroadcastBitResetPrompt, bytes.release());
GetCommandInterpreter().BroadcastEvent(prompt_change_event_sp);
} else if (property_path == g_debugger_properties[ePropertyUseColor].name) {
// use-color changed. Ping the prompt so it can reset the ansi terminal
// codes.
SetPrompt(GetPrompt());
} else if (property_path ==
g_debugger_properties[ePropertyPromptAnsiPrefix].name ||
property_path ==
g_debugger_properties[ePropertyPromptAnsiSuffix].name) {
// Prompt colors changed. Ping the prompt so it can reset the ansi
// terminal codes.
SetPrompt(GetPrompt());
} else if (property_path ==
g_debugger_properties[ePropertyShowStatusline].name) {
// Statusline setting changed. If we have a statusline instance, update it
// now. Otherwise it will get created in the default event handler.
if (StatuslineSupported())
m_statusline.emplace(*this);
else
m_statusline.reset();
} else if (property_path ==
g_debugger_properties[ePropertyStatuslineFormat].name) {
// Statusline format changed. Redraw the statusline.
RedrawStatusline();
} else if (property_path ==
g_debugger_properties[ePropertyUseSourceCache].name) {
// use-source-cache changed. Wipe out the cache contents if it was
// disabled.
if (!GetUseSourceCache()) {
m_source_file_cache.Clear();
}
} else if (is_load_script && target_sp &&
load_script_old_value == eLoadScriptFromSymFileWarn) {
if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() ==
eLoadScriptFromSymFileTrue) {
std::list<Status> errors;
StreamString feedback_stream;
if (!target_sp->LoadScriptingResources(errors, feedback_stream)) {
lldb::StreamUP s = GetAsyncErrorStream();
for (auto &error : errors)
s->Printf("%s\n", error.AsCString());
if (feedback_stream.GetSize())
s->PutCString(feedback_stream.GetString());
}
}
}
}
return error;
}
bool Debugger::GetAutoConfirm() const {
constexpr uint32_t idx = ePropertyAutoConfirm;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
const FormatEntity::Entry *Debugger::GetDisassemblyFormat() const {
constexpr uint32_t idx = ePropertyDisassemblyFormat;
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
}
const FormatEntity::Entry *Debugger::GetFrameFormat() const {
constexpr uint32_t idx = ePropertyFrameFormat;
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
}
const FormatEntity::Entry *Debugger::GetFrameFormatUnique() const {
constexpr uint32_t idx = ePropertyFrameFormatUnique;
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
}
uint64_t Debugger::GetStopDisassemblyMaxSize() const {
constexpr uint32_t idx = ePropertyStopDisassemblyMaxSize;
return GetPropertyAtIndexAs<uint64_t>(
idx, g_debugger_properties[idx].default_uint_value);
}
bool Debugger::GetNotifyVoid() const {
constexpr uint32_t idx = ePropertyNotiftVoid;
return GetPropertyAtIndexAs<uint64_t>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
llvm::StringRef Debugger::GetPrompt() const {
constexpr uint32_t idx = ePropertyPrompt;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetPromptAnsiPrefix() const {
const uint32_t idx = ePropertyPromptAnsiPrefix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetPromptAnsiSuffix() const {
const uint32_t idx = ePropertyPromptAnsiSuffix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
void Debugger::SetPrompt(llvm::StringRef p) {
constexpr uint32_t idx = ePropertyPrompt;
SetPropertyAtIndex(idx, p);
llvm::StringRef new_prompt = GetPrompt();
std::string str =
lldb_private::ansi::FormatAnsiTerminalCodes(new_prompt, GetUseColor());
if (str.length())
new_prompt = str;
GetCommandInterpreter().UpdatePrompt(new_prompt);
}
const FormatEntity::Entry *Debugger::GetThreadFormat() const {
constexpr uint32_t idx = ePropertyThreadFormat;
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
}
const FormatEntity::Entry *Debugger::GetThreadStopFormat() const {
constexpr uint32_t idx = ePropertyThreadStopFormat;
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
}
lldb::ScriptLanguage Debugger::GetScriptLanguage() const {
const uint32_t idx = ePropertyScriptLanguage;
return GetPropertyAtIndexAs<lldb::ScriptLanguage>(
idx, static_cast<lldb::ScriptLanguage>(
g_debugger_properties[idx].default_uint_value));
}
bool Debugger::SetScriptLanguage(lldb::ScriptLanguage script_lang) {
const uint32_t idx = ePropertyScriptLanguage;
return SetPropertyAtIndex(idx, script_lang);
}
lldb::LanguageType Debugger::GetREPLLanguage() const {
const uint32_t idx = ePropertyREPLLanguage;
return GetPropertyAtIndexAs<LanguageType>(idx, {});
}
bool Debugger::SetREPLLanguage(lldb::LanguageType repl_lang) {
const uint32_t idx = ePropertyREPLLanguage;
return SetPropertyAtIndex(idx, repl_lang);
}
uint64_t Debugger::GetTerminalWidth() const {
const uint32_t idx = ePropertyTerminalWidth;
return GetPropertyAtIndexAs<uint64_t>(
idx, g_debugger_properties[idx].default_uint_value);
}
bool Debugger::SetTerminalWidth(uint64_t term_width) {
const uint32_t idx = ePropertyTerminalWidth;
const bool success = SetPropertyAtIndex(idx, term_width);
if (auto handler_sp = m_io_handler_stack.Top())
handler_sp->TerminalSizeChanged();
if (m_statusline)
m_statusline->TerminalSizeChanged();
return success;
}
uint64_t Debugger::GetTerminalHeight() const {
const uint32_t idx = ePropertyTerminalHeight;
return GetPropertyAtIndexAs<uint64_t>(
idx, g_debugger_properties[idx].default_uint_value);
}
bool Debugger::SetTerminalHeight(uint64_t term_height) {
const uint32_t idx = ePropertyTerminalHeight;
const bool success = SetPropertyAtIndex(idx, term_height);
if (auto handler_sp = m_io_handler_stack.Top())
handler_sp->TerminalSizeChanged();
if (m_statusline)
m_statusline->TerminalSizeChanged();
return success;
}
bool Debugger::GetUseExternalEditor() const {
const uint32_t idx = ePropertyUseExternalEditor;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::SetUseExternalEditor(bool b) {
const uint32_t idx = ePropertyUseExternalEditor;
return SetPropertyAtIndex(idx, b);
}
llvm::StringRef Debugger::GetExternalEditor() const {
const uint32_t idx = ePropertyExternalEditor;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
bool Debugger::SetExternalEditor(llvm::StringRef editor) {
const uint32_t idx = ePropertyExternalEditor;
return SetPropertyAtIndex(idx, editor);
}
bool Debugger::GetUseColor() const {
const uint32_t idx = ePropertyUseColor;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::SetUseColor(bool b) {
const uint32_t idx = ePropertyUseColor;
bool ret = SetPropertyAtIndex(idx, b);
SetPrompt(GetPrompt());
return ret;
}
bool Debugger::GetShowProgress() const {
const uint32_t idx = ePropertyShowProgress;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::SetShowProgress(bool show_progress) {
const uint32_t idx = ePropertyShowProgress;
return SetPropertyAtIndex(idx, show_progress);
}
llvm::StringRef Debugger::GetShowProgressAnsiPrefix() const {
const uint32_t idx = ePropertyShowProgressAnsiPrefix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetShowProgressAnsiSuffix() const {
const uint32_t idx = ePropertyShowProgressAnsiSuffix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
bool Debugger::GetShowStatusline() const {
const uint32_t idx = ePropertyShowStatusline;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
const FormatEntity::Entry *Debugger::GetStatuslineFormat() const {
constexpr uint32_t idx = ePropertyStatuslineFormat;
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
}
bool Debugger::GetUseAutosuggestion() const {
const uint32_t idx = ePropertyShowAutosuggestion;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
llvm::StringRef Debugger::GetAutosuggestionAnsiPrefix() const {
const uint32_t idx = ePropertyShowAutosuggestionAnsiPrefix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetAutosuggestionAnsiSuffix() const {
const uint32_t idx = ePropertyShowAutosuggestionAnsiSuffix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetRegexMatchAnsiPrefix() const {
const uint32_t idx = ePropertyShowRegexMatchAnsiPrefix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetRegexMatchAnsiSuffix() const {
const uint32_t idx = ePropertyShowRegexMatchAnsiSuffix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
bool Debugger::GetShowDontUsePoHint() const {
const uint32_t idx = ePropertyShowDontUsePoHint;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::GetUseSourceCache() const {
const uint32_t idx = ePropertyUseSourceCache;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::SetUseSourceCache(bool b) {
const uint32_t idx = ePropertyUseSourceCache;
bool ret = SetPropertyAtIndex(idx, b);
if (!ret) {
m_source_file_cache.Clear();
}
return ret;
}
bool Debugger::GetHighlightSource() const {
const uint32_t idx = ePropertyHighlightSource;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
StopShowColumn Debugger::GetStopShowColumn() const {
const uint32_t idx = ePropertyStopShowColumn;
return GetPropertyAtIndexAs<lldb::StopShowColumn>(
idx, static_cast<lldb::StopShowColumn>(
g_debugger_properties[idx].default_uint_value));
}
llvm::StringRef Debugger::GetStopShowColumnAnsiPrefix() const {
const uint32_t idx = ePropertyStopShowColumnAnsiPrefix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetStopShowColumnAnsiSuffix() const {
const uint32_t idx = ePropertyStopShowColumnAnsiSuffix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetStopShowLineMarkerAnsiPrefix() const {
const uint32_t idx = ePropertyStopShowLineMarkerAnsiPrefix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
llvm::StringRef Debugger::GetStopShowLineMarkerAnsiSuffix() const {
const uint32_t idx = ePropertyStopShowLineMarkerAnsiSuffix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}
uint64_t Debugger::GetStopSourceLineCount(bool before) const {
const uint32_t idx =
before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
return GetPropertyAtIndexAs<uint64_t>(
idx, g_debugger_properties[idx].default_uint_value);
}
Debugger::StopDisassemblyType Debugger::GetStopDisassemblyDisplay() const {
const uint32_t idx = ePropertyStopDisassemblyDisplay;
return GetPropertyAtIndexAs<Debugger::StopDisassemblyType>(
idx, static_cast<Debugger::StopDisassemblyType>(
g_debugger_properties[idx].default_uint_value));
}
uint64_t Debugger::GetDisassemblyLineCount() const {
const uint32_t idx = ePropertyStopDisassemblyCount;
return GetPropertyAtIndexAs<uint64_t>(
idx, g_debugger_properties[idx].default_uint_value);
}
bool Debugger::GetAutoOneLineSummaries() const {
const uint32_t idx = ePropertyAutoOneLineSummaries;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::GetEscapeNonPrintables() const {
const uint32_t idx = ePropertyEscapeNonPrintables;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::GetAutoIndent() const {
const uint32_t idx = ePropertyAutoIndent;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::SetAutoIndent(bool b) {
const uint32_t idx = ePropertyAutoIndent;
return SetPropertyAtIndex(idx, b);
}
bool Debugger::GetPrintDecls() const {
const uint32_t idx = ePropertyPrintDecls;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value != 0);
}
bool Debugger::SetPrintDecls(bool b) {
const uint32_t idx = ePropertyPrintDecls;
return SetPropertyAtIndex(idx, b);
}
uint64_t Debugger::GetTabSize() const {
const uint32_t idx = ePropertyTabSize;
return GetPropertyAtIndexAs<uint64_t>(
idx, g_debugger_properties[idx].default_uint_value);
}
bool Debugger::SetTabSize(uint64_t tab_size) {
const uint32_t idx = ePropertyTabSize;
return SetPropertyAtIndex(idx, tab_size);
}
lldb::DWIMPrintVerbosity Debugger::GetDWIMPrintVerbosity() const {
const uint32_t idx = ePropertyDWIMPrintVerbosity;
return GetPropertyAtIndexAs<lldb::DWIMPrintVerbosity>(
idx, static_cast<lldb::DWIMPrintVerbosity>(
g_debugger_properties[idx].default_uint_value != 0));
}
bool Debugger::GetShowInlineDiagnostics() const {
const uint32_t idx = ePropertyShowInlineDiagnostics;
return GetPropertyAtIndexAs<bool>(
idx, g_debugger_properties[idx].default_uint_value);
}
bool Debugger::SetShowInlineDiagnostics(bool b) {
const uint32_t idx = ePropertyShowInlineDiagnostics;
return SetPropertyAtIndex(idx, b);
}
#pragma mark Debugger
// const DebuggerPropertiesSP &
// Debugger::GetSettings() const
//{
// return m_properties_sp;
//}
//
void Debugger::Initialize(LoadPluginCallbackType load_plugin_callback) {
assert(g_debugger_list_ptr == nullptr &&
"Debugger::Initialize called more than once!");
g_debugger_list_mutex_ptr = new std::recursive_mutex();
g_debugger_list_ptr = new DebuggerList();
g_thread_pool = new llvm::DefaultThreadPool(llvm::optimal_concurrency());
g_load_plugin_callback = load_plugin_callback;
}
void Debugger::Terminate() {
assert(g_debugger_list_ptr &&
"Debugger::Terminate called without a matching Debugger::Initialize!");
if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
for (const auto &debugger : *g_debugger_list_ptr)
debugger->HandleDestroyCallback();
}
if (g_thread_pool) {
// The destructor will wait for all the threads to complete.
delete g_thread_pool;
}
if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
// Clear our global list of debugger objects
{
std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
for (const auto &debugger : *g_debugger_list_ptr)
debugger->Clear();
g_debugger_list_ptr->clear();
}
}
}
void Debugger::SettingsInitialize() { Target::SettingsInitialize(); }
void Debugger::SettingsTerminate() { Target::SettingsTerminate(); }
bool Debugger::LoadPlugin(const FileSpec &spec, Status &error) {
if (g_load_plugin_callback) {
llvm::sys::DynamicLibrary dynlib =
g_load_plugin_callback(shared_from_this(), spec, error);
if (dynlib.isValid()) {
m_loaded_plugins.push_back(dynlib);
return true;
}
} else {
// The g_load_plugin_callback is registered in SBDebugger::Initialize() and
// if the public API layer isn't available (code is linking against all of
// the internal LLDB static libraries), then we can't load plugins
error = Status::FromErrorString("Public API layer is not available");
}
return false;
}
static FileSystem::EnumerateDirectoryResult
LoadPluginCallback(void *baton, llvm::sys::fs::file_type ft,
llvm::StringRef path) {
Status error;
static constexpr llvm::StringLiteral g_dylibext(".dylib");
static constexpr llvm::StringLiteral g_solibext(".so");
if (!baton)
return FileSystem::eEnumerateDirectoryResultQuit;
Debugger *debugger = (Debugger *)baton;
namespace fs = llvm::sys::fs;
// If we have a regular file, a symbolic link or unknown file type, try and
// process the file. We must handle unknown as sometimes the directory
// enumeration might be enumerating a file system that doesn't have correct
// file type information.
if (ft == fs::file_type::regular_file || ft == fs::file_type::symlink_file ||
ft == fs::file_type::type_unknown) {
FileSpec plugin_file_spec(path);
FileSystem::Instance().Resolve(plugin_file_spec);
if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
plugin_file_spec.GetFileNameExtension() != g_solibext) {
return FileSystem::eEnumerateDirectoryResultNext;
}
Status plugin_load_error;
debugger->LoadPlugin(plugin_file_spec, plugin_load_error);
return FileSystem::eEnumerateDirectoryResultNext;
} else if (ft == fs::file_type::directory_file ||
ft == fs::file_type::symlink_file ||
ft == fs::file_type::type_unknown) {
// Try and recurse into anything that a directory or symbolic link. We must
// also do this for unknown as sometimes the directory enumeration might be
// enumerating a file system that doesn't have correct file type
// information.
return FileSystem::eEnumerateDirectoryResultEnter;
}
return FileSystem::eEnumerateDirectoryResultNext;
}
void Debugger::InstanceInitialize() {
const bool find_directories = true;
const bool find_files = true;
const bool find_other = true;
char dir_path[PATH_MAX];
if (FileSpec dir_spec = HostInfo::GetSystemPluginDir()) {
if (FileSystem::Instance().Exists(dir_spec) &&
dir_spec.GetPath(dir_path, sizeof(dir_path))) {
FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,
find_files, find_other,
LoadPluginCallback, this);
}
}
if (FileSpec dir_spec = HostInfo::GetUserPluginDir()) {
if (FileSystem::Instance().Exists(dir_spec) &&
dir_spec.GetPath(dir_path, sizeof(dir_path))) {
FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,
find_files, find_other,
LoadPluginCallback, this);
}
}
PluginManager::DebuggerInitialize(*this);
}
DebuggerSP Debugger::CreateInstance(lldb::LogOutputCallback log_callback,
void *baton) {
DebuggerSP debugger_sp(new Debugger(log_callback, baton));
if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
g_debugger_list_ptr->push_back(debugger_sp);
}
debugger_sp->InstanceInitialize();
return debugger_sp;
}
void Debugger::HandleDestroyCallback() {
const lldb::user_id_t user_id = GetID();
// Invoke and remove all the callbacks in an FIFO order. Callbacks which are
// added during this loop will be appended, invoked and then removed last.
// Callbacks which are removed during this loop will not be invoked.
while (true) {
DestroyCallbackInfo callback_info;
{
std::lock_guard<std::mutex> guard(m_destroy_callback_mutex);
if (m_destroy_callbacks.empty())
break;
// Pop the first item in the list
callback_info = m_destroy_callbacks.front();
m_destroy_callbacks.erase(m_destroy_callbacks.begin());
}
// Call the destroy callback with user id and baton
callback_info.callback(user_id, callback_info.baton);
}
}
void Debugger::Destroy(DebuggerSP &debugger_sp) {
if (!debugger_sp)
return;
debugger_sp->HandleDestroyCallback();
CommandInterpreter &cmd_interpreter = debugger_sp->GetCommandInterpreter();
if (cmd_interpreter.GetSaveSessionOnQuit()) {
CommandReturnObject result(debugger_sp->GetUseColor());
cmd_interpreter.SaveTranscript(result);
if (result.Succeeded())
(*debugger_sp->GetAsyncOutputStream())
<< result.GetOutputString() << '\n';
else
(*debugger_sp->GetAsyncErrorStream()) << result.GetErrorString() << '\n';
}
debugger_sp->Clear();
if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
if ((*pos).get() == debugger_sp.get()) {
g_debugger_list_ptr->erase(pos);
return;
}
}
}
}
DebuggerSP
Debugger::FindDebuggerWithInstanceName(llvm::StringRef instance_name) {
if (!g_debugger_list_ptr || !g_debugger_list_mutex_ptr)
return DebuggerSP();
std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
for (const DebuggerSP &debugger_sp : *g_debugger_list_ptr) {
if (!debugger_sp)
continue;
if (llvm::StringRef(debugger_sp->GetInstanceName()) == instance_name)
return debugger_sp;
}
return DebuggerSP();
}
TargetSP Debugger::FindTargetWithProcessID(lldb::pid_t pid) {
TargetSP target_sp;
if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
target_sp = (*pos)->GetTargetList().FindTargetWithProcessID(pid);
if (target_sp)
break;
}
}
return target_sp;
}
TargetSP Debugger::FindTargetWithProcess(Process *process) {
TargetSP target_sp;
if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
target_sp = (*pos)->GetTargetList().FindTargetWithProcess(process);
if (target_sp)
break;
}
}
return target_sp;
}
llvm::StringRef Debugger::GetStaticBroadcasterClass() {
static constexpr llvm::StringLiteral class_name("lldb.debugger");
return class_name;
}
Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton)
: UserID(g_unique_id++),
Properties(std::make_shared<OptionValueProperties>()),
m_input_file_sp(std::make_shared<NativeFile>(stdin, NativeFile::Unowned)),
m_output_stream_sp(std::make_shared<LockableStreamFile>(
stdout, NativeFile::Unowned, m_output_mutex)),
m_error_stream_sp(std::make_shared<LockableStreamFile>(
stderr, NativeFile::Unowned, m_output_mutex)),
m_input_recorder(nullptr),
m_broadcaster_manager_sp(BroadcasterManager::MakeBroadcasterManager()),
m_terminal_state(), m_target_list(*this), m_platform_list(),
m_listener_sp(Listener::MakeListener("lldb.Debugger")),
m_source_manager_up(), m_source_file_cache(),
m_command_interpreter_up(
std::make_unique<CommandInterpreter>(*this, false)),
m_io_handler_stack(),
m_instance_name(llvm::formatv("debugger_{0}", GetID()).str()),
m_loaded_plugins(), m_event_handler_thread(), m_io_handler_thread(),
m_sync_broadcaster(nullptr, "lldb.debugger.sync"),
m_broadcaster(m_broadcaster_manager_sp,
GetStaticBroadcasterClass().str()),
m_forward_listener_sp(), m_clear_once() {
// Initialize the debugger properties as early as possible as other parts of
// LLDB will start querying them during construction.
m_collection_sp->Initialize(g_debugger_properties);
m_collection_sp->AppendProperty(
"target", "Settings specify to debugging targets.", true,
Target::GetGlobalProperties().GetValueProperties());
m_collection_sp->AppendProperty(
"platform", "Platform settings.", true,
Platform::GetGlobalPlatformProperties().GetValueProperties());
m_collection_sp->AppendProperty(
"symbols", "Symbol lookup and cache settings.", true,
ModuleList::GetGlobalModuleListProperties().GetValueProperties());
m_collection_sp->AppendProperty(
LanguageProperties::GetSettingName(), "Language settings.", true,
Language::GetGlobalLanguageProperties().GetValueProperties());
if (m_command_interpreter_up) {
m_collection_sp->AppendProperty(
"interpreter",
"Settings specify to the debugger's command interpreter.", true,
m_command_interpreter_up->GetValueProperties());
}
if (log_callback)
m_callback_handler_sp =
std::make_shared<CallbackLogHandler>(log_callback, baton);
m_command_interpreter_up->Initialize();
// Always add our default platform to the platform list
PlatformSP default_platform_sp(Platform::GetHostPlatform());
assert(default_platform_sp);
m_platform_list.Append(default_platform_sp, true);
// Create the dummy target.
{
ArchSpec arch(Target::GetDefaultArchitecture());
if (!arch.IsValid())
arch = HostInfo::GetArchitecture();
assert(arch.IsValid() && "No valid default or host archspec");
const bool is_dummy_target = true;
m_dummy_target_sp.reset(
new Target(*this, arch, default_platform_sp, is_dummy_target));
}
assert(m_dummy_target_sp.get() && "Couldn't construct dummy target?");
OptionValueUInt64 *term_width =
m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64(
ePropertyTerminalWidth);
term_width->SetMinimumValue(10);
OptionValueUInt64 *term_height =
m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64(
ePropertyTerminalHeight);
term_height->SetMinimumValue(10);
// Turn off use-color if this is a dumb terminal.
const char *term = getenv("TERM");
if (term && !strcmp(term, "dumb"))
SetUseColor(false);
// Turn off use-color if we don't write to a terminal with color support.
if (!GetOutputFileSP()->GetIsTerminalWithColors())
SetUseColor(false);
if (Diagnostics::Enabled()) {
m_diagnostics_callback_id = Diagnostics::Instance().AddCallback(
[this](const FileSpec &dir) -> llvm::Error {
for (auto &entry : m_stream_handlers) {
llvm::StringRef log_path = entry.first();
llvm::StringRef file_name = llvm::sys::path::filename(log_path);
FileSpec destination = dir.CopyByAppendingPathComponent(file_name);
std::error_code ec =
llvm::sys::fs::copy_file(log_path, destination.GetPath());
if (ec)
return llvm::errorCodeToError(ec);
}
return llvm::Error::success();
});
}
#if defined(_WIN32) && defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
// Enabling use of ANSI color codes because LLDB is using them to highlight