LLDB mainline
Debugger.cpp
Go to the documentation of this file.
1//===-- Debugger.cpp ------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
14#include "lldb/Core/Mangled.h"
18#include "lldb/Core/Progress.h"
21#include "lldb/Core/Telemetry.h"
24#include "lldb/Host/File.h"
26#include "lldb/Host/HostInfo.h"
28#include "lldb/Host/Terminal.h"
40#include "lldb/Symbol/Symbol.h"
43#include "lldb/Target/Process.h"
45#include "lldb/Target/Target.h"
47#include "lldb/Target/Thread.h"
50#include "lldb/Utility/Event.h"
53#include "lldb/Utility/Log.h"
54#include "lldb/Utility/State.h"
55#include "lldb/Utility/Stream.h"
59
60#if defined(_WIN32)
63#endif
64
65#include "llvm/ADT/STLExtras.h"
66#include "llvm/ADT/StringRef.h"
67#include "llvm/ADT/iterator.h"
68#include "llvm/Config/llvm-config.h"
69#include "llvm/Support/DynamicLibrary.h"
70#include "llvm/Support/FileSystem.h"
71#include "llvm/Support/Process.h"
72#include "llvm/Support/ThreadPool.h"
73#include "llvm/Support/Threading.h"
74#include "llvm/Support/raw_ostream.h"
75
76#include <chrono>
77#include <cstdio>
78#include <cstdlib>
79#include <cstring>
80#include <list>
81#include <memory>
82#include <mutex>
83#include <optional>
84#include <set>
85#include <string>
86#include <system_error>
87
88// Includes for pipe()
89#if defined(_WIN32)
90#include <fcntl.h>
91#include <io.h>
92#else
93#include <unistd.h>
94#endif
95
96namespace lldb_private {
97class Address;
98}
99
100using namespace lldb;
101using namespace lldb_private;
102
104static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
105
106#pragma mark Static Functions
107
108static std::recursive_mutex *g_debugger_list_mutex_ptr =
109 nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
111 nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
112static llvm::DefaultThreadPool *g_thread_pool = nullptr;
113
115 {
117 "never",
118 "Never show disassembly when displaying a stop context.",
119 },
120 {
122 "no-debuginfo",
123 "Show disassembly when there is no debug information.",
124 },
125 {
127 "no-source",
128 "Show disassembly when there is no source information, or the source "
129 "file "
130 "is missing when displaying a stop context.",
131 },
132 {
134 "always",
135 "Always show disassembly when displaying a stop context.",
136 },
137};
138
140 {
142 "none",
143 "Disable scripting languages.",
144 },
145 {
147 "python",
148 "Select python as the default scripting language.",
149 },
150 {
152 "default",
153 "Select the lldb default as the default scripting language.",
154 },
155};
156
159 "Use no verbosity when running dwim-print."},
160 {eDWIMPrintVerbosityExpression, "expression",
161 "Use partial verbosity when running dwim-print - display a message when "
162 "`expression` evaluation is used."},
164 "Use full verbosity when running dwim-print."},
165};
166
168 {
170 "ansi-or-caret",
171 "Highlight the stop column with ANSI terminal codes when color/ANSI "
172 "mode is enabled; otherwise, fall back to using a text-only caret (^) "
173 "as if \"caret-only\" mode was selected.",
174 },
175 {
177 "ansi",
178 "Highlight the stop column with ANSI terminal codes when running LLDB "
179 "with color/ANSI enabled.",
180 },
181 {
183 "caret",
184 "Highlight the stop column with a caret character (^) underneath the "
185 "stop column. This method introduces a new line in source listings "
186 "that display thread stop locations.",
187 },
188 {
190 "none",
191 "Do not highlight the stop column.",
192 },
193};
194
195#define LLDB_PROPERTIES_debugger
196#include "CoreProperties.inc"
197
198enum {
199#define LLDB_PROPERTIES_debugger
200#include "CorePropertiesEnum.inc"
201};
202
204
207 llvm::StringRef property_path,
208 llvm::StringRef value) {
209 bool is_load_script =
210 (property_path == "target.load-script-from-symbol-file");
211 // These properties might change how we visualize data.
212 bool invalidate_data_vis = (property_path == "escape-non-printables");
213 invalidate_data_vis |=
214 (property_path == "target.max-zero-padding-in-float-format");
215 if (invalidate_data_vis) {
217 }
218
219 TargetSP target_sp;
221 if (is_load_script && exe_ctx && exe_ctx->GetTargetSP()) {
222 target_sp = exe_ctx->GetTargetSP();
223 load_script_old_value =
224 target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
225 }
226 Status error(Properties::SetPropertyValue(exe_ctx, op, property_path, value));
227 if (error.Success()) {
228 // FIXME it would be nice to have "on-change" callbacks for properties
229 if (property_path == g_debugger_properties[ePropertyPrompt].name) {
230 llvm::StringRef new_prompt = GetPrompt();
232 new_prompt, GetUseColor());
233 if (str.length())
234 new_prompt = str;
236 auto bytes = std::make_unique<EventDataBytes>(new_prompt);
237 auto prompt_change_event_sp = std::make_shared<Event>(
239 GetCommandInterpreter().BroadcastEvent(prompt_change_event_sp);
240 } else if (property_path == g_debugger_properties[ePropertyUseColor].name) {
241 // use-color changed. set use-color, this also pings the prompt so it can
242 // reset the ansi terminal codes.
244 } else if (property_path ==
245 g_debugger_properties[ePropertyPromptAnsiPrefix].name ||
246 property_path ==
247 g_debugger_properties[ePropertyPromptAnsiSuffix].name) {
248 // Prompt color changed. set use-color, this also pings the prompt so it
249 // can reset the ansi terminal codes.
251 } else if (property_path ==
252 g_debugger_properties[ePropertyShowStatusline].name) {
253 // Statusline setting changed. If we have a statusline instance, update it
254 // now. Otherwise it will get created in the default event handler.
255 std::lock_guard<std::mutex> guard(m_statusline_mutex);
256 if (StatuslineSupported()) {
257 m_statusline.emplace(*this);
259 } else {
260 m_statusline.reset();
261 }
262 } else if (property_path ==
263 g_debugger_properties[ePropertyStatuslineFormat].name ||
264 property_path ==
265 g_debugger_properties[ePropertySeparator].name) {
266 // Statusline format changed. Redraw the statusline.
267 RedrawStatusline(std::nullopt);
268 } else if (property_path ==
269 g_debugger_properties[ePropertyUseSourceCache].name) {
270 // use-source-cache changed. Wipe out the cache contents if it was
271 // disabled.
272 if (!GetUseSourceCache()) {
273 m_source_file_cache.Clear();
274 }
275 } else if (is_load_script && target_sp &&
276 load_script_old_value == eLoadScriptFromSymFileWarn) {
277 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() ==
279 std::list<Status> errors;
280 StreamString feedback_stream;
281 if (!target_sp->LoadScriptingResources(errors, feedback_stream)) {
283 for (auto &error : errors)
284 s->Printf("%s\n", error.AsCString());
285 if (feedback_stream.GetSize())
286 s->PutCString(feedback_stream.GetString());
287 }
288 }
289 }
290 }
291 return error;
292}
293
295 constexpr uint32_t idx = ePropertyAutoConfirm;
297 idx, g_debugger_properties[idx].default_uint_value != 0);
298}
299
301 constexpr uint32_t idx = ePropertyDisassemblyFormat;
303}
304
306 constexpr uint32_t idx = ePropertyFrameFormat;
308}
309
311 constexpr uint32_t idx = ePropertyFrameFormatUnique;
313}
314
316 constexpr uint32_t idx = ePropertyStopDisassemblyMaxSize;
318 idx, g_debugger_properties[idx].default_uint_value);
319}
320
322 constexpr uint32_t idx = ePropertyNotiftVoid;
324 idx, g_debugger_properties[idx].default_uint_value != 0);
325}
326
327llvm::StringRef Debugger::GetPrompt() const {
328 constexpr uint32_t idx = ePropertyPrompt;
330 idx, g_debugger_properties[idx].default_cstr_value);
331}
332
333llvm::StringRef Debugger::GetPromptAnsiPrefix() const {
334 const uint32_t idx = ePropertyPromptAnsiPrefix;
336 idx, g_debugger_properties[idx].default_cstr_value);
337}
338
339llvm::StringRef Debugger::GetPromptAnsiSuffix() const {
340 const uint32_t idx = ePropertyPromptAnsiSuffix;
342 idx, g_debugger_properties[idx].default_cstr_value);
343}
344
345void Debugger::SetPrompt(llvm::StringRef p) {
346 constexpr uint32_t idx = ePropertyPrompt;
347 SetPropertyAtIndex(idx, p);
348 llvm::StringRef new_prompt = GetPrompt();
349 std::string str =
351 if (str.length())
352 new_prompt = str;
354}
355
357 constexpr uint32_t idx = ePropertyThreadFormat;
359}
360
362 constexpr uint32_t idx = ePropertyThreadStopFormat;
364}
365
367 const uint32_t idx = ePropertyScriptLanguage;
369 idx, static_cast<lldb::ScriptLanguage>(
370 g_debugger_properties[idx].default_uint_value));
371}
372
374 const uint32_t idx = ePropertyScriptLanguage;
375 return SetPropertyAtIndex(idx, script_lang);
376}
377
379 const uint32_t idx = ePropertyREPLLanguage;
381}
382
384 const uint32_t idx = ePropertyREPLLanguage;
385 return SetPropertyAtIndex(idx, repl_lang);
386}
387
389 const uint32_t idx = ePropertyTerminalWidth;
391 idx, g_debugger_properties[idx].default_uint_value);
392}
393
394bool Debugger::SetTerminalWidth(uint64_t term_width) {
395 const uint32_t idx = ePropertyTerminalWidth;
396 const bool success = SetPropertyAtIndex(idx, term_width);
397
398 if (auto handler_sp = m_io_handler_stack.Top())
399 handler_sp->TerminalSizeChanged();
400
401 {
402 std::lock_guard<std::mutex> guard(m_statusline_mutex);
403 if (m_statusline)
404 m_statusline->TerminalSizeChanged();
405 }
406
407 return success;
408}
409
411 const uint32_t idx = ePropertyTerminalHeight;
413 idx, g_debugger_properties[idx].default_uint_value);
414}
415
416bool Debugger::SetTerminalHeight(uint64_t term_height) {
417 const uint32_t idx = ePropertyTerminalHeight;
418 const bool success = SetPropertyAtIndex(idx, term_height);
419
420 if (auto handler_sp = m_io_handler_stack.Top())
421 handler_sp->TerminalSizeChanged();
422
423 {
424 std::lock_guard<std::mutex> guard(m_statusline_mutex);
425 if (m_statusline)
426 m_statusline->TerminalSizeChanged();
427 }
428
429 return success;
430}
431
433 const uint32_t idx = ePropertyUseExternalEditor;
435 idx, g_debugger_properties[idx].default_uint_value != 0);
436}
437
439 const uint32_t idx = ePropertyUseExternalEditor;
440 return SetPropertyAtIndex(idx, b);
441}
442
443llvm::StringRef Debugger::GetExternalEditor() const {
444 const uint32_t idx = ePropertyExternalEditor;
446 idx, g_debugger_properties[idx].default_cstr_value);
447}
448
449bool Debugger::SetExternalEditor(llvm::StringRef editor) {
450 const uint32_t idx = ePropertyExternalEditor;
451 return SetPropertyAtIndex(idx, editor);
452}
453
455 const uint32_t idx = ePropertyUseColor;
457 idx, g_debugger_properties[idx].default_uint_value != 0);
458}
459
461 const uint32_t idx = ePropertyUseColor;
462 bool ret = SetPropertyAtIndex(idx, b);
463
466 return ret;
467}
468
470 const uint32_t idx = ePropertyShowProgress;
472 idx, g_debugger_properties[idx].default_uint_value != 0);
473}
474
475bool Debugger::SetShowProgress(bool show_progress) {
476 const uint32_t idx = ePropertyShowProgress;
477 return SetPropertyAtIndex(idx, show_progress);
478}
479
480llvm::StringRef Debugger::GetShowProgressAnsiPrefix() const {
481 const uint32_t idx = ePropertyShowProgressAnsiPrefix;
483 idx, g_debugger_properties[idx].default_cstr_value);
484}
485
486llvm::StringRef Debugger::GetShowProgressAnsiSuffix() const {
487 const uint32_t idx = ePropertyShowProgressAnsiSuffix;
489 idx, g_debugger_properties[idx].default_cstr_value);
490}
491
493 const uint32_t idx = ePropertyShowStatusline;
495 idx, g_debugger_properties[idx].default_uint_value != 0);
496}
497
499 constexpr uint32_t idx = ePropertyStatuslineFormat;
501}
502
504 constexpr uint32_t idx = ePropertyStatuslineFormat;
505 bool ret = SetPropertyAtIndex(idx, format);
506 RedrawStatusline(std::nullopt);
507 return ret;
508}
509
510llvm::StringRef Debugger::GetSeparator() const {
511 constexpr uint32_t idx = ePropertySeparator;
513 idx, g_debugger_properties[idx].default_cstr_value);
514}
515
516llvm::StringRef Debugger::GetDisabledAnsiPrefix() const {
517 const uint32_t idx = ePropertyShowDisabledAnsiPrefix;
519 idx, g_debugger_properties[idx].default_cstr_value);
520}
521
522llvm::StringRef Debugger::GetDisabledAnsiSuffix() const {
523 const uint32_t idx = ePropertyShowDisabledAnsiSuffix;
525 idx, g_debugger_properties[idx].default_cstr_value);
526}
527
528bool Debugger::SetSeparator(llvm::StringRef s) {
529 constexpr uint32_t idx = ePropertySeparator;
530 bool ret = SetPropertyAtIndex(idx, s);
531 RedrawStatusline(std::nullopt);
532 return ret;
533}
534
536 const uint32_t idx = ePropertyShowAutosuggestion;
538 idx, g_debugger_properties[idx].default_uint_value != 0);
539}
540
542 const uint32_t idx = ePropertyShowAutosuggestionAnsiPrefix;
544 idx, g_debugger_properties[idx].default_cstr_value);
545}
546
548 const uint32_t idx = ePropertyShowAutosuggestionAnsiSuffix;
550 idx, g_debugger_properties[idx].default_cstr_value);
551}
552
553llvm::StringRef Debugger::GetRegexMatchAnsiPrefix() const {
554 const uint32_t idx = ePropertyShowRegexMatchAnsiPrefix;
556 idx, g_debugger_properties[idx].default_cstr_value);
557}
558
559llvm::StringRef Debugger::GetRegexMatchAnsiSuffix() const {
560 const uint32_t idx = ePropertyShowRegexMatchAnsiSuffix;
562 idx, g_debugger_properties[idx].default_cstr_value);
563}
564
566 const uint32_t idx = ePropertyShowDontUsePoHint;
568 idx, g_debugger_properties[idx].default_uint_value != 0);
569}
570
572 const uint32_t idx = ePropertyUseSourceCache;
574 idx, g_debugger_properties[idx].default_uint_value != 0);
575}
576
578 const uint32_t idx = ePropertyUseSourceCache;
579 bool ret = SetPropertyAtIndex(idx, b);
580 if (!ret) {
581 m_source_file_cache.Clear();
582 }
583 return ret;
584}
586 const uint32_t idx = ePropertyHighlightSource;
588 idx, g_debugger_properties[idx].default_uint_value != 0);
589}
590
592 const uint32_t idx = ePropertyStopShowColumn;
594 idx, static_cast<lldb::StopShowColumn>(
595 g_debugger_properties[idx].default_uint_value));
596}
597
599 const uint32_t idx = ePropertyStopShowColumnAnsiPrefix;
601 idx, g_debugger_properties[idx].default_cstr_value);
602}
603
605 const uint32_t idx = ePropertyStopShowColumnAnsiSuffix;
607 idx, g_debugger_properties[idx].default_cstr_value);
608}
609
611 const uint32_t idx = ePropertyStopShowLineMarkerAnsiPrefix;
613 idx, g_debugger_properties[idx].default_cstr_value);
614}
615
617 const uint32_t idx = ePropertyStopShowLineMarkerAnsiSuffix;
619 idx, g_debugger_properties[idx].default_cstr_value);
620}
621
622uint64_t Debugger::GetStopSourceLineCount(bool before) const {
623 const uint32_t idx =
624 before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
626 idx, g_debugger_properties[idx].default_uint_value);
627}
628
630 const uint32_t idx = ePropertyStopDisassemblyDisplay;
632 idx, static_cast<lldb::StopDisassemblyType>(
633 g_debugger_properties[idx].default_uint_value));
634}
635
637 const uint32_t idx = ePropertyStopDisassemblyCount;
639 idx, g_debugger_properties[idx].default_uint_value);
640}
641
643 const uint32_t idx = ePropertyAutoOneLineSummaries;
645 idx, g_debugger_properties[idx].default_uint_value != 0);
646}
647
649 const uint32_t idx = ePropertyEscapeNonPrintables;
651 idx, g_debugger_properties[idx].default_uint_value != 0);
652}
653
655 const uint32_t idx = ePropertyAutoIndent;
657 idx, g_debugger_properties[idx].default_uint_value != 0);
658}
659
661 const uint32_t idx = ePropertyAutoIndent;
662 return SetPropertyAtIndex(idx, b);
663}
664
666 const uint32_t idx = ePropertyPrintDecls;
668 idx, g_debugger_properties[idx].default_uint_value != 0);
669}
670
672 const uint32_t idx = ePropertyPrintDecls;
673 return SetPropertyAtIndex(idx, b);
674}
675
676uint64_t Debugger::GetTabSize() const {
677 const uint32_t idx = ePropertyTabSize;
679 idx, g_debugger_properties[idx].default_uint_value);
680}
681
682bool Debugger::SetTabSize(uint64_t tab_size) {
683 const uint32_t idx = ePropertyTabSize;
684 return SetPropertyAtIndex(idx, tab_size);
685}
686
688 const uint32_t idx = ePropertyDWIMPrintVerbosity;
690 idx, static_cast<lldb::DWIMPrintVerbosity>(
691 g_debugger_properties[idx].default_uint_value != 0));
692}
693
695 const uint32_t idx = ePropertyShowInlineDiagnostics;
697 idx, g_debugger_properties[idx].default_uint_value);
698}
699
701 const uint32_t idx = ePropertyShowInlineDiagnostics;
702 return SetPropertyAtIndex(idx, b);
703}
704
705#pragma mark Debugger
706
707// const DebuggerPropertiesSP &
708// Debugger::GetSettings() const
709//{
710// return m_properties_sp;
711//}
712//
713
715 assert(g_debugger_list_ptr == nullptr &&
716 "Debugger::Initialize called more than once!");
717 g_debugger_list_mutex_ptr = new std::recursive_mutex();
719 g_thread_pool = new llvm::DefaultThreadPool(llvm::optimal_concurrency());
720 g_load_plugin_callback = load_plugin_callback;
721}
722
724 assert(g_debugger_list_ptr &&
725 "Debugger::Terminate called without a matching Debugger::Initialize!");
726
728 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
729 for (const auto &debugger : *g_debugger_list_ptr)
730 debugger->HandleDestroyCallback();
731 }
732
733 if (g_thread_pool) {
734 // The destructor will wait for all the threads to complete.
735 delete g_thread_pool;
736 }
737
739 // Clear our global list of debugger objects
740 {
741 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
742 for (const auto &debugger : *g_debugger_list_ptr)
743 debugger->Clear();
744 g_debugger_list_ptr->clear();
745 }
746 }
747}
748
750
752
755 llvm::sys::DynamicLibrary dynlib =
756 g_load_plugin_callback(shared_from_this(), spec, error);
757 if (dynlib.isValid()) {
758 m_loaded_plugins.push_back(dynlib);
759 return true;
760 }
761 } else {
762 // The g_load_plugin_callback is registered in SBDebugger::Initialize() and
763 // if the public API layer isn't available (code is linking against all of
764 // the internal LLDB static libraries), then we can't load plugins
765 error = Status::FromErrorString("Public API layer is not available");
766 }
767 return false;
768}
769
771LoadPluginCallback(void *baton, llvm::sys::fs::file_type ft,
772 llvm::StringRef path) {
774
775 static constexpr llvm::StringLiteral g_dylibext(".dylib");
776 static constexpr llvm::StringLiteral g_solibext(".so");
777
778 if (!baton)
780
781 Debugger *debugger = (Debugger *)baton;
782
783 namespace fs = llvm::sys::fs;
784 // If we have a regular file, a symbolic link or unknown file type, try and
785 // process the file. We must handle unknown as sometimes the directory
786 // enumeration might be enumerating a file system that doesn't have correct
787 // file type information.
788 if (ft == fs::file_type::regular_file || ft == fs::file_type::symlink_file ||
789 ft == fs::file_type::type_unknown) {
790 FileSpec plugin_file_spec(path);
791 FileSystem::Instance().Resolve(plugin_file_spec);
792
793 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
794 plugin_file_spec.GetFileNameExtension() != g_solibext) {
796 }
797
798 Status plugin_load_error;
799 debugger->LoadPlugin(plugin_file_spec, plugin_load_error);
800
802 } else if (ft == fs::file_type::directory_file ||
803 ft == fs::file_type::symlink_file ||
804 ft == fs::file_type::type_unknown) {
805 // Try and recurse into anything that a directory or symbolic link. We must
806 // also do this for unknown as sometimes the directory enumeration might be
807 // enumerating a file system that doesn't have correct file type
808 // information.
810 }
811
813}
814
816 const bool find_directories = true;
817 const bool find_files = true;
818 const bool find_other = true;
819 char dir_path[PATH_MAX];
820 if (FileSpec dir_spec = HostInfo::GetSystemPluginDir()) {
821 if (FileSystem::Instance().Exists(dir_spec) &&
822 dir_spec.GetPath(dir_path, sizeof(dir_path))) {
823 FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,
824 find_files, find_other,
825 LoadPluginCallback, this);
826 }
827 }
828
829 if (FileSpec dir_spec = HostInfo::GetUserPluginDir()) {
830 if (FileSystem::Instance().Exists(dir_spec) &&
831 dir_spec.GetPath(dir_path, sizeof(dir_path))) {
832 FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,
833 find_files, find_other,
834 LoadPluginCallback, this);
835 }
836 }
837
839}
840
842 void *baton) {
845 helper([](lldb_private::telemetry::DebuggerInfo *entry) {
847 });
848 DebuggerSP debugger_sp(new Debugger(log_callback, baton));
849 helper.SetDebugger(debugger_sp.get());
850
852 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
853 g_debugger_list_ptr->push_back(debugger_sp);
854 }
855 debugger_sp->InstanceInitialize();
856 return debugger_sp;
857}
858
864
866 const lldb::user_id_t user_id = GetID();
867 // Invoke and remove all the callbacks in an FIFO order. Callbacks which are
868 // added during this loop will be appended, invoked and then removed last.
869 // Callbacks which are removed during this loop will not be invoked.
870 while (true) {
871 DestroyCallbackInfo callback_info;
872 {
873 std::lock_guard<std::mutex> guard(m_destroy_callback_mutex);
874 if (m_destroy_callbacks.empty())
875 break;
876 // Pop the first item in the list
877 callback_info = m_destroy_callbacks.front();
879 }
880 // Call the destroy callback with user id and baton
881 callback_info.callback(user_id, callback_info.baton);
882 }
883}
884
885void Debugger::Destroy(DebuggerSP &debugger_sp) {
886 if (!debugger_sp)
887 return;
888
889 debugger_sp->HandleDestroyCallback();
890 CommandInterpreter &cmd_interpreter = debugger_sp->GetCommandInterpreter();
891
892 if (cmd_interpreter.GetSaveSessionOnQuit()) {
893 CommandReturnObject result(debugger_sp->GetUseColor());
894 cmd_interpreter.SaveTranscript(result);
895 if (result.Succeeded())
896 (*debugger_sp->GetAsyncOutputStream())
897 << result.GetOutputString() << '\n';
898 else
899 (*debugger_sp->GetAsyncErrorStream()) << result.GetErrorString() << '\n';
900 }
901
902 debugger_sp->Clear();
903
905 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
906 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
907 for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
908 if ((*pos).get() == debugger_sp.get()) {
909 g_debugger_list_ptr->erase(pos);
910 return;
911 }
912 }
913 }
914}
915
917Debugger::FindDebuggerWithInstanceName(llvm::StringRef instance_name) {
919 return DebuggerSP();
920
921 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
922 for (const DebuggerSP &debugger_sp : *g_debugger_list_ptr) {
923 if (!debugger_sp)
924 continue;
925
926 if (llvm::StringRef(debugger_sp->GetInstanceName()) == instance_name)
927 return debugger_sp;
928 }
929 return DebuggerSP();
930}
931
933 TargetSP target_sp;
935 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
936 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
937 for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
938 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID(pid);
939 if (target_sp)
940 break;
941 }
942 }
943 return target_sp;
944}
945
947 TargetSP target_sp;
949 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
950 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
951 for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
952 target_sp = (*pos)->GetTargetList().FindTargetWithProcess(process);
953 if (target_sp)
954 break;
955 }
956 }
957 return target_sp;
958}
959
961 static constexpr llvm::StringLiteral class_name("lldb.debugger");
962 return class_name;
963}
964
966 : UserID(g_unique_id++),
967 Properties(std::make_shared<OptionValueProperties>()),
968 m_input_file_sp(std::make_shared<NativeFile>(
969 stdin, File::eOpenOptionReadOnly, NativeFile::Unowned)),
970 m_output_stream_sp(std::make_shared<LockableStreamFile>(
971 stdout, NativeFile::Unowned, m_output_mutex)),
972 m_error_stream_sp(std::make_shared<LockableStreamFile>(
973 stderr, NativeFile::Unowned, m_output_mutex)),
974 m_broadcaster_manager_sp(BroadcasterManager::MakeBroadcasterManager()),
976 m_listener_sp(Listener::MakeListener("lldb.Debugger")),
979 std::make_unique<CommandInterpreter>(*this, false)),
981 m_instance_name(llvm::formatv("debugger_{0}", GetID()).str()),
983 m_sync_broadcaster(nullptr, "lldb.debugger.sync"),
987 // Initialize the debugger properties as early as possible as other parts of
988 // LLDB will start querying them during construction.
989 m_collection_sp->Initialize(g_debugger_properties);
990 m_collection_sp->AppendProperty(
991 "target", "Settings specify to debugging targets.", true,
993 m_collection_sp->AppendProperty(
994 "platform", "Platform settings.", true,
996 m_collection_sp->AppendProperty(
997 "symbols", "Symbol lookup and cache settings.", true,
999 m_collection_sp->AppendProperty(
1000 LanguageProperties::GetSettingName(), "Language settings.", true,
1003 m_collection_sp->AppendProperty(
1004 "interpreter",
1005 "Settings specify to the debugger's command interpreter.", true,
1006 m_command_interpreter_up->GetValueProperties());
1007 }
1008 if (log_callback)
1010 std::make_shared<CallbackLogHandler>(log_callback, baton);
1011 m_command_interpreter_up->Initialize();
1012 // Always add our default platform to the platform list
1013 PlatformSP default_platform_sp(Platform::GetHostPlatform());
1014 assert(default_platform_sp);
1015 m_platform_list.Append(default_platform_sp, true);
1016
1017 // Create the dummy target.
1018 {
1020 if (!arch.IsValid())
1021 arch = HostInfo::GetArchitecture();
1022 assert(arch.IsValid() && "No valid default or host archspec");
1023 const bool is_dummy_target = true;
1024 m_dummy_target_sp.reset(
1025 new Target(*this, arch, default_platform_sp, is_dummy_target));
1026 }
1027 assert(m_dummy_target_sp.get() && "Couldn't construct dummy target?");
1028
1029 OptionValueUInt64 *term_width =
1030 m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64(
1031 ePropertyTerminalWidth);
1032 term_width->SetMinimumValue(10);
1033
1034 OptionValueUInt64 *term_height =
1035 m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64(
1036 ePropertyTerminalHeight);
1037 term_height->SetMinimumValue(10);
1038
1039 // Turn off use-color if this is a dumb terminal.
1040 const char *term = getenv("TERM");
1041 auto disable_color = [&]() {
1042 SetUseColor(false);
1043 SetSeparator("| ");
1044 };
1045
1046 if (term && !strcmp(term, "dumb"))
1047 disable_color();
1048 // Turn off use-color if we don't write to a terminal with color support.
1049 if (!GetOutputFileSP()->GetIsTerminalWithColors())
1050 disable_color();
1051
1052 if (Diagnostics::Enabled()) {
1054 [this](const FileSpec &dir) -> llvm::Error {
1055 for (auto &entry : m_stream_handlers) {
1056 llvm::StringRef log_path = entry.first();
1057 llvm::StringRef file_name = llvm::sys::path::filename(log_path);
1058 FileSpec destination = dir.CopyByAppendingPathComponent(file_name);
1059 std::error_code ec =
1060 llvm::sys::fs::copy_file(log_path, destination.GetPath());
1061 if (ec)
1062 return llvm::errorCodeToError(ec);
1063 }
1064 return llvm::Error::success();
1065 });
1066 }
1067
1068#if defined(_WIN32) && defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
1069 // Enabling use of ANSI color codes because LLDB is using them to highlight
1070 // text.
1071 llvm::sys::Process::UseANSIEscapeCodes(true);
1072#endif
1073}
1074
1076
1078 // Make sure we call this function only once. With the C++ global destructor
1079 // chain having a list of debuggers and with code that can be running on
1080 // other threads, we need to ensure this doesn't happen multiple times.
1081 //
1082 // The following functions call Debugger::Clear():
1083 // Debugger::~Debugger();
1084 // static void Debugger::Destroy(lldb::DebuggerSP &debugger_sp);
1085 // static void Debugger::Terminate();
1086 llvm::call_once(m_clear_once, [this]() {
1089 assert(this == info->debugger);
1090 (void)this;
1091 info->is_exit_entry = true;
1092 },
1093 this);
1097 m_listener_sp->Clear();
1098 for (TargetSP target_sp : m_target_list.Targets()) {
1099 if (target_sp) {
1100 if (ProcessSP process_sp = target_sp->GetProcessSP())
1101 process_sp->Finalize(false /* not destructing */);
1102 target_sp->Destroy();
1103 }
1104 }
1105 m_broadcaster_manager_sp->Clear();
1106
1107 // Close the input file _before_ we close the input read communications
1108 // class as it does NOT own the input file, our m_input_file does.
1109 m_terminal_state.Clear();
1110 GetInputFile().Close();
1111
1112 m_command_interpreter_up->Clear();
1113
1116 });
1117}
1118
1120 return !m_command_interpreter_up->GetSynchronous();
1121}
1122
1123void Debugger::SetAsyncExecution(bool async_execution) {
1124 m_command_interpreter_up->SetSynchronous(!async_execution);
1125}
1126
1127static inline int OpenPipe(int fds[2], std::size_t size) {
1128#ifdef _WIN32
1129 return _pipe(fds, size, O_BINARY);
1130#else
1131 (void)size;
1132 return pipe(fds);
1133#endif
1134}
1135
1137 Status result;
1138 enum PIPES { READ, WRITE }; // Indexes for the read and write fds
1139 int fds[2] = {-1, -1};
1140
1141 if (data == nullptr) {
1142 result = Status::FromErrorString("String data is null");
1143 return result;
1144 }
1145
1146 size_t size = strlen(data);
1147 if (size == 0) {
1148 result = Status::FromErrorString("String data is empty");
1149 return result;
1150 }
1151
1152 if (OpenPipe(fds, size) != 0) {
1153 result = Status::FromErrorString(
1154 "can't create pipe file descriptors for LLDB commands");
1155 return result;
1156 }
1157
1158 int r = write(fds[WRITE], data, size);
1159 (void)r;
1160 // Close the write end of the pipe, so that the command interpreter will exit
1161 // when it consumes all the data.
1162 llvm::sys::Process::SafelyCloseFileDescriptor(fds[WRITE]);
1163
1164 // Open the read file descriptor as a FILE * that we can return as an input
1165 // handle.
1166 FILE *commands_file = fdopen(fds[READ], "rb");
1167 if (commands_file == nullptr) {
1169 "fdopen(%i, \"rb\") failed (errno = %i) "
1170 "when trying to open LLDB commands pipe",
1171 fds[READ], errno);
1172 llvm::sys::Process::SafelyCloseFileDescriptor(fds[READ]);
1173 return result;
1174 }
1175
1176 SetInputFile((FileSP)std::make_shared<NativeFile>(
1177 commands_file, File::eOpenOptionReadOnly, true));
1178 return result;
1179}
1180
1182 assert(file_sp && file_sp->IsValid());
1183 m_input_file_sp = std::move(file_sp);
1184 // Save away the terminal state if that is relevant, so that we can restore
1185 // it in RestoreInputState.
1187}
1188
1190 assert(file_sp && file_sp->IsValid());
1192 std::make_shared<LockableStreamFile>(file_sp, m_output_mutex);
1193}
1194
1196 assert(file_sp && file_sp->IsValid());
1198 std::make_shared<LockableStreamFile>(file_sp, m_output_mutex);
1199}
1200
1202 {
1203 std::lock_guard<std::mutex> guard(m_statusline_mutex);
1204 if (m_statusline)
1205 m_statusline->Disable();
1206 }
1207 int fd = GetInputFile().GetDescriptor();
1208 if (fd != File::kInvalidDescriptor)
1209 m_terminal_state.Save(fd, true);
1210}
1211
1213 m_terminal_state.Restore();
1214 {
1215 std::lock_guard<std::mutex> guard(m_statusline_mutex);
1216 if (m_statusline)
1218 }
1219}
1220
1222 std::optional<ExecutionContextRef> exe_ctx_ref) {
1223 std::lock_guard<std::mutex> guard(m_statusline_mutex);
1224
1225 if (!m_statusline)
1226 return;
1227
1228 m_statusline->Redraw(exe_ctx_ref);
1229}
1230
1232 bool adopt_selected = true;
1233 ExecutionContextRef exe_ctx_ref(GetSelectedTarget().get(), adopt_selected);
1234 return ExecutionContext(exe_ctx_ref);
1235}
1236
1238 if (TargetSP selected_target_sp = GetSelectedTarget())
1239 return ExecutionContextRef(selected_target_sp.get(),
1240 /*adopt_selected=*/true);
1241 return ExecutionContextRef(m_dummy_target_sp.get(), /*adopt_selected=*/false);
1242}
1243
1245 std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
1246 IOHandlerSP reader_sp(m_io_handler_stack.Top());
1247 if (reader_sp)
1248 reader_sp->Interrupt();
1249}
1250
1252 std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
1253 IOHandlerSP reader_sp(m_io_handler_stack.Top());
1254 if (reader_sp)
1255 reader_sp->GotEOF();
1256}
1257
1259 // The bottom input reader should be the main debugger input reader. We do
1260 // not want to close that one here.
1261 std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
1262 while (m_io_handler_stack.GetSize() > 1) {
1263 IOHandlerSP reader_sp(m_io_handler_stack.Top());
1264 if (reader_sp)
1265 PopIOHandler(reader_sp);
1266 }
1267}
1268
1270 IOHandlerSP reader_sp = m_io_handler_stack.Top();
1271 while (true) {
1272 if (!reader_sp)
1273 break;
1274
1275 reader_sp->Run();
1276 {
1277 std::lock_guard<std::recursive_mutex> guard(
1279
1280 // Remove all input readers that are done from the top of the stack
1281 while (true) {
1282 IOHandlerSP top_reader_sp = m_io_handler_stack.Top();
1283 if (top_reader_sp && top_reader_sp->GetIsDone())
1284 PopIOHandler(top_reader_sp);
1285 else
1286 break;
1287 }
1288 reader_sp = m_io_handler_stack.Top();
1289 }
1290 }
1292}
1293
1295 std::lock_guard<std::recursive_mutex> guard(m_io_handler_synchronous_mutex);
1296
1297 PushIOHandler(reader_sp);
1298 IOHandlerSP top_reader_sp = reader_sp;
1299
1300 while (top_reader_sp) {
1301 top_reader_sp->Run();
1302
1303 // Don't unwind past the starting point.
1304 if (top_reader_sp.get() == reader_sp.get()) {
1305 if (PopIOHandler(reader_sp))
1306 break;
1307 }
1308
1309 // If we pushed new IO handlers, pop them if they're done or restart the
1310 // loop to run them if they're not.
1311 while (true) {
1312 top_reader_sp = m_io_handler_stack.Top();
1313 if (top_reader_sp && top_reader_sp->GetIsDone()) {
1314 PopIOHandler(top_reader_sp);
1315 // Don't unwind past the starting point.
1316 if (top_reader_sp.get() == reader_sp.get())
1317 return;
1318 } else {
1319 break;
1320 }
1321 }
1322 }
1323}
1324
1326 return m_io_handler_stack.IsTop(reader_sp);
1327}
1328
1330 IOHandler::Type second_top_type) {
1331 return m_io_handler_stack.CheckTopIOHandlerTypes(top_type, second_top_type);
1332}
1333
1334void Debugger::PrintAsync(const char *s, size_t len, bool is_stdout) {
1335 bool printed = m_io_handler_stack.PrintAsync(s, len, is_stdout);
1336 if (!printed) {
1337 LockableStreamFileSP stream_sp =
1339 LockedStreamFile locked_stream = stream_sp->Lock();
1340 locked_stream.Write(s, len);
1341 }
1342}
1343
1345 return m_io_handler_stack.GetTopIOHandlerControlSequence(ch);
1346}
1347
1349 return m_io_handler_stack.GetTopIOHandlerCommandPrefix();
1350}
1351
1353 return m_io_handler_stack.GetTopIOHandlerHelpPrologue();
1354}
1355
1357 return PopIOHandler(reader_sp);
1358}
1359
1361 bool cancel_top_handler) {
1362 PushIOHandler(reader_sp, cancel_top_handler);
1363}
1364
1367 LockableStreamFileSP &err) {
1368 // Before an IOHandler runs, it must have in/out/err streams. This function
1369 // is called when one ore more of the streams are nullptr. We use the top
1370 // input reader's in/out/err streams, or fall back to the debugger file
1371 // handles, or we fall back onto stdin/stdout/stderr as a last resort.
1372
1373 std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
1374 IOHandlerSP top_reader_sp(m_io_handler_stack.Top());
1375 // If no STDIN has been set, then set it appropriately
1376 if (!in || !in->IsValid()) {
1377 if (top_reader_sp)
1378 in = top_reader_sp->GetInputFileSP();
1379 else
1380 in = GetInputFileSP();
1381 // If there is nothing, use stdin
1382 if (!in)
1383 in = std::make_shared<NativeFile>(stdin, File::eOpenOptionReadOnly,
1385 }
1386 // If no STDOUT has been set, then set it appropriately
1387 if (!out || !out->GetUnlockedFile().IsValid()) {
1388 if (top_reader_sp)
1389 out = top_reader_sp->GetOutputStreamFileSP();
1390 else
1391 out = GetOutputStreamSP();
1392 // If there is nothing, use stdout
1393 if (!out)
1394 out = std::make_shared<LockableStreamFile>(stdout, NativeFile::Unowned,
1396 }
1397 // If no STDERR has been set, then set it appropriately
1398 if (!err || !err->GetUnlockedFile().IsValid()) {
1399 if (top_reader_sp)
1400 err = top_reader_sp->GetErrorStreamFileSP();
1401 else
1402 err = GetErrorStreamSP();
1403 // If there is nothing, use stderr
1404 if (!err)
1405 err = std::make_shared<LockableStreamFile>(stderr, NativeFile::Unowned,
1407 }
1408}
1409
1411 bool cancel_top_handler) {
1412 if (!reader_sp)
1413 return;
1414
1415 std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
1416
1417 // Get the current top input reader...
1418 IOHandlerSP top_reader_sp(m_io_handler_stack.Top());
1419
1420 // Don't push the same IO handler twice...
1421 if (reader_sp == top_reader_sp)
1422 return;
1423
1424 // Push our new input reader
1425 m_io_handler_stack.Push(reader_sp);
1426 reader_sp->Activate();
1427
1428 // Interrupt the top input reader to it will exit its Run() function and let
1429 // this new input reader take over
1430 if (top_reader_sp) {
1431 top_reader_sp->Deactivate();
1432 if (cancel_top_handler)
1433 top_reader_sp->Cancel();
1434 }
1435}
1436
1437bool Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp) {
1438 if (!pop_reader_sp)
1439 return false;
1440
1441 std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
1442
1443 // The reader on the stop of the stack is done, so let the next read on the
1444 // stack refresh its prompt and if there is one...
1445 if (m_io_handler_stack.IsEmpty())
1446 return false;
1447
1448 IOHandlerSP reader_sp(m_io_handler_stack.Top());
1449
1450 if (pop_reader_sp != reader_sp)
1451 return false;
1452
1453 reader_sp->Deactivate();
1454 reader_sp->Cancel();
1455 m_io_handler_stack.Pop();
1456
1457 reader_sp = m_io_handler_stack.Top();
1458 if (reader_sp)
1459 reader_sp->Activate();
1460
1461 return true;
1462}
1463
1465 std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
1466 IOHandlerSP reader_sp(m_io_handler_stack.Top());
1467 if (reader_sp)
1468 reader_sp->Refresh();
1469}
1470
1472 return std::make_unique<StreamAsynchronousIO>(*this,
1474}
1475
1477 return std::make_unique<StreamAsynchronousIO>(*this,
1479}
1480
1482 std::lock_guard<std::mutex> guard(m_interrupt_mutex);
1484}
1485
1487 std::lock_guard<std::mutex> guard(m_interrupt_mutex);
1488 if (m_interrupt_requested > 0)
1490}
1491
1493 // This is the one we should call internally. This will return true either
1494 // if there's a debugger interrupt and we aren't on the IOHandler thread,
1495 // or if we are on the IOHandler thread and there's a CommandInterpreter
1496 // interrupt.
1498 std::lock_guard<std::mutex> guard(m_interrupt_mutex);
1499 return m_interrupt_requested != 0;
1500 }
1502}
1503
1505 std::string function_name, const llvm::formatv_object_base &payload)
1506 : m_function_name(std::move(function_name)),
1507 m_interrupt_time(std::chrono::system_clock::now()),
1508 m_thread_id(llvm::get_threadid()) {
1509 llvm::raw_string_ostream desc(m_description);
1510 desc << payload << "\n";
1511}
1512
1514 // For now, just log the description:
1515 Log *log = GetLog(LLDBLog::Host);
1516 LLDB_LOG(log, "Interruption: {0}", report.m_description);
1517}
1518
1520 DebuggerList result;
1522 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1523 for (auto debugger_sp : *g_debugger_list_ptr) {
1524 if (debugger_sp->InterruptRequested())
1525 result.push_back(debugger_sp);
1526 }
1527 }
1528 return result;
1529}
1530
1533 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1534 return g_debugger_list_ptr->size();
1535 }
1536 return 0;
1537}
1538
1540 DebuggerSP debugger_sp;
1541
1543 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1544 if (index < g_debugger_list_ptr->size())
1545 debugger_sp = g_debugger_list_ptr->at(index);
1546 }
1547
1548 return debugger_sp;
1549}
1550
1552 DebuggerSP debugger_sp;
1553
1555 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1556 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
1557 for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
1558 if ((*pos)->GetID() == id) {
1559 debugger_sp = *pos;
1560 break;
1561 }
1562 }
1563 }
1564 return debugger_sp;
1565}
1566
1568 const SymbolContext *sc,
1569 const SymbolContext *prev_sc,
1570 const ExecutionContext *exe_ctx,
1571 const Address *addr, Stream &s) {
1572 FormatEntity::Entry format_entry;
1573
1574 if (format == nullptr) {
1575 if (exe_ctx != nullptr && exe_ctx->HasTargetScope()) {
1576 format_entry =
1578 format = &format_entry;
1579 }
1580 if (format == nullptr) {
1581 FormatEntity::Parse("${addr}: ", format_entry);
1582 format = &format_entry;
1583 }
1584 }
1585 bool function_changed = false;
1586 bool initial_function = false;
1587 if (prev_sc && (prev_sc->function || prev_sc->symbol)) {
1588 if (sc && (sc->function || sc->symbol)) {
1589 if (prev_sc->symbol && sc->symbol) {
1590 if (!sc->symbol->Compare(prev_sc->symbol->GetName(),
1591 prev_sc->symbol->GetType())) {
1592 function_changed = true;
1593 }
1594 } else if (prev_sc->function && sc->function) {
1595 if (prev_sc->function->GetMangled() != sc->function->GetMangled()) {
1596 function_changed = true;
1597 }
1598 }
1599 }
1600 }
1601 // The first context on a list of instructions will have a prev_sc that has
1602 // no Function or Symbol -- if SymbolContext had an IsValid() method, it
1603 // would return false. But we do get a prev_sc pointer.
1604 if ((sc && (sc->function || sc->symbol)) && prev_sc &&
1605 (prev_sc->function == nullptr && prev_sc->symbol == nullptr)) {
1606 initial_function = true;
1607 }
1608 return FormatEntity::Format(*format, s, sc, exe_ctx, addr, nullptr,
1609 function_changed, initial_function);
1610}
1611
1612void Debugger::AssertCallback(llvm::StringRef message,
1613 llvm::StringRef backtrace,
1614 llvm::StringRef prompt) {
1615 Debugger::ReportError(llvm::formatv("{0}\n{1}{2}\n{3}", message, backtrace,
1616 GetVersion(), prompt)
1617 .str());
1618}
1619
1621 void *baton) {
1622 // For simplicity's sake, I am not going to deal with how to close down any
1623 // open logging streams, I just redirect everything from here on out to the
1624 // callback.
1626 std::make_shared<CallbackLogHandler>(log_callback, baton);
1627}
1628
1630 lldb_private::DebuggerDestroyCallback destroy_callback, void *baton) {
1631 std::lock_guard<std::mutex> guard(m_destroy_callback_mutex);
1632 m_destroy_callbacks.clear();
1634 m_destroy_callbacks.emplace_back(token, destroy_callback, baton);
1635}
1636
1638 lldb_private::DebuggerDestroyCallback destroy_callback, void *baton) {
1639 std::lock_guard<std::mutex> guard(m_destroy_callback_mutex);
1641 m_destroy_callbacks.emplace_back(token, destroy_callback, baton);
1642 return token;
1643}
1644
1646 std::lock_guard<std::mutex> guard(m_destroy_callback_mutex);
1647 for (auto it = m_destroy_callbacks.begin(); it != m_destroy_callbacks.end();
1648 ++it) {
1649 if (it->token == token) {
1650 m_destroy_callbacks.erase(it);
1651 return true;
1652 }
1653 }
1654 return false;
1655}
1656
1657static void PrivateReportProgress(Debugger &debugger, uint64_t progress_id,
1658 std::string title, std::string details,
1659 uint64_t completed, uint64_t total,
1660 bool is_debugger_specific,
1661 uint32_t progress_broadcast_bit) {
1662 // Only deliver progress events if we have any progress listeners.
1663 if (!debugger.GetBroadcaster().EventTypeHasListeners(progress_broadcast_bit))
1664 return;
1665
1666 EventSP event_sp(new Event(
1667 progress_broadcast_bit,
1668 new ProgressEventData(progress_id, std::move(title), std::move(details),
1669 completed, total, is_debugger_specific)));
1670 debugger.GetBroadcaster().BroadcastEvent(event_sp);
1671}
1672
1673void Debugger::ReportProgress(uint64_t progress_id, std::string title,
1674 std::string details, uint64_t completed,
1675 uint64_t total,
1676 std::optional<lldb::user_id_t> debugger_id,
1677 uint32_t progress_broadcast_bit) {
1678 // Check if this progress is for a specific debugger.
1679 if (debugger_id) {
1680 // It is debugger specific, grab it and deliver the event if the debugger
1681 // still exists.
1682 DebuggerSP debugger_sp = FindDebuggerWithID(*debugger_id);
1683 if (debugger_sp)
1684 PrivateReportProgress(*debugger_sp, progress_id, std::move(title),
1685 std::move(details), completed, total,
1686 /*is_debugger_specific*/ true,
1687 progress_broadcast_bit);
1688 return;
1689 }
1690 // The progress event is not debugger specific, iterate over all debuggers
1691 // and deliver a progress event to each one.
1693 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1694 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
1695 for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos)
1696 PrivateReportProgress(*(*pos), progress_id, title, details, completed,
1697 total, /*is_debugger_specific*/ false,
1698 progress_broadcast_bit);
1699 }
1700}
1701
1702static void PrivateReportDiagnostic(Debugger &debugger, Severity severity,
1703 std::string message,
1704 bool debugger_specific) {
1705 uint32_t event_type = 0;
1706 switch (severity) {
1707 case eSeverityInfo:
1708 assert(false && "eSeverityInfo should not be broadcast");
1709 return;
1710 case eSeverityWarning:
1711 event_type = lldb::eBroadcastBitWarning;
1712 break;
1713 case eSeverityError:
1714 event_type = lldb::eBroadcastBitError;
1715 break;
1716 }
1717
1718 Broadcaster &broadcaster = debugger.GetBroadcaster();
1719 if (!broadcaster.EventTypeHasListeners(event_type)) {
1720 // Diagnostics are too important to drop. If nobody is listening, print the
1721 // diagnostic directly to the debugger's error stream.
1722 DiagnosticEventData event_data(severity, std::move(message),
1723 debugger_specific);
1724 event_data.Dump(debugger.GetAsyncErrorStream().get());
1725 return;
1726 }
1727 EventSP event_sp = std::make_shared<Event>(
1728 event_type,
1729 new DiagnosticEventData(severity, std::move(message), debugger_specific));
1730 broadcaster.BroadcastEvent(event_sp);
1731}
1732
1733void Debugger::ReportDiagnosticImpl(Severity severity, std::string message,
1734 std::optional<lldb::user_id_t> debugger_id,
1735 std::once_flag *once) {
1736 auto ReportDiagnosticLambda = [&]() {
1737 // Always log diagnostics to the system log.
1738 Host::SystemLog(severity, message);
1739
1740 // The diagnostic subsystem is optional but we still want to broadcast
1741 // events when it's disabled.
1743 Diagnostics::Instance().Report(message);
1744
1745 // We don't broadcast info events.
1746 if (severity == lldb::eSeverityInfo)
1747 return;
1748
1749 // Check if this diagnostic is for a specific debugger.
1750 if (debugger_id) {
1751 // It is debugger specific, grab it and deliver the event if the debugger
1752 // still exists.
1753 DebuggerSP debugger_sp = FindDebuggerWithID(*debugger_id);
1754 if (debugger_sp)
1755 PrivateReportDiagnostic(*debugger_sp, severity, std::move(message),
1756 true);
1757 return;
1758 }
1759 // The diagnostic event is not debugger specific, iterate over all debuggers
1760 // and deliver a diagnostic event to each one.
1762 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1763 for (const auto &debugger : *g_debugger_list_ptr)
1764 PrivateReportDiagnostic(*debugger, severity, message, false);
1765 }
1766 };
1767
1768 if (once)
1769 std::call_once(*once, ReportDiagnosticLambda);
1770 else
1771 ReportDiagnosticLambda();
1772}
1773
1774void Debugger::ReportWarning(std::string message,
1775 std::optional<lldb::user_id_t> debugger_id,
1776 std::once_flag *once) {
1777 ReportDiagnosticImpl(eSeverityWarning, std::move(message), debugger_id, once);
1778}
1779
1780void Debugger::ReportError(std::string message,
1781 std::optional<lldb::user_id_t> debugger_id,
1782 std::once_flag *once) {
1783 ReportDiagnosticImpl(eSeverityError, std::move(message), debugger_id, once);
1784}
1785
1786void Debugger::ReportInfo(std::string message,
1787 std::optional<lldb::user_id_t> debugger_id,
1788 std::once_flag *once) {
1789 ReportDiagnosticImpl(eSeverityInfo, std::move(message), debugger_id, once);
1790}
1791
1794 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1795 for (DebuggerSP debugger_sp : *g_debugger_list_ptr) {
1796 EventSP event_sp = std::make_shared<Event>(
1798 new SymbolChangeEventData(debugger_sp, module_spec));
1799 debugger_sp->GetBroadcaster().BroadcastEvent(event_sp);
1800 }
1801 }
1802}
1803
1804static std::shared_ptr<LogHandler>
1805CreateLogHandler(LogHandlerKind log_handler_kind, int fd, bool should_close,
1806 size_t buffer_size) {
1807 switch (log_handler_kind) {
1808 case eLogHandlerStream:
1809 return std::make_shared<StreamLogHandler>(fd, should_close, buffer_size);
1811 return std::make_shared<RotatingLogHandler>(buffer_size);
1812 case eLogHandlerSystem:
1813 return std::make_shared<SystemLogHandler>();
1815 return {};
1816 }
1817 return {};
1818}
1819
1820bool Debugger::EnableLog(llvm::StringRef channel,
1821 llvm::ArrayRef<const char *> categories,
1822 llvm::StringRef log_file, uint32_t log_options,
1823 size_t buffer_size, LogHandlerKind log_handler_kind,
1824 llvm::raw_ostream &error_stream) {
1825
1826 std::shared_ptr<LogHandler> log_handler_sp;
1828 log_handler_sp = m_callback_handler_sp;
1829 // For now when using the callback mode you always get thread & timestamp.
1830 log_options |=
1832 } else if (log_file.empty()) {
1833 log_handler_sp =
1834 CreateLogHandler(log_handler_kind, GetOutputFileSP()->GetDescriptor(),
1835 /*should_close=*/false, buffer_size);
1836 } else {
1837 auto pos = m_stream_handlers.find(log_file);
1838 if (pos != m_stream_handlers.end())
1839 log_handler_sp = pos->second.lock();
1840 if (!log_handler_sp) {
1841 File::OpenOptions flags =
1843 if (log_options & LLDB_LOG_OPTION_APPEND)
1844 flags |= File::eOpenOptionAppend;
1845 else
1847 llvm::Expected<FileUP> file = FileSystem::Instance().Open(
1848 FileSpec(log_file), flags, lldb::eFilePermissionsFileDefault, false);
1849 if (!file) {
1850 error_stream << "Unable to open log file '" << log_file
1851 << "': " << llvm::toString(file.takeError()) << "\n";
1852 return false;
1853 }
1854
1855 log_handler_sp =
1856 CreateLogHandler(log_handler_kind, (*file)->GetDescriptor(),
1857 /*should_close=*/true, buffer_size);
1858 m_stream_handlers[log_file] = log_handler_sp;
1859 }
1860 }
1861 assert(log_handler_sp);
1862
1863 if (log_options == 0)
1865
1866 return Log::EnableLogChannel(log_handler_sp, log_options, channel, categories,
1867 error_stream);
1868}
1869
1872 std::optional<lldb::ScriptLanguage> language) {
1873 std::lock_guard<std::recursive_mutex> locker(m_script_interpreter_mutex);
1874 lldb::ScriptLanguage script_language =
1875 language ? *language : GetScriptLanguage();
1876
1877 if (!m_script_interpreters[script_language]) {
1878 if (!can_create)
1879 return nullptr;
1880 m_script_interpreters[script_language] =
1881 PluginManager::GetScriptInterpreterForLanguage(script_language, *this);
1882 }
1883
1884 return m_script_interpreters[script_language].get();
1885}
1886
1889 m_source_manager_up = std::make_unique<SourceManager>(shared_from_this());
1890 return *m_source_manager_up;
1891}
1892
1893// This function handles events that were broadcast by the process.
1895 using namespace lldb;
1896 const uint32_t event_type =
1898 event_sp);
1899
1900 // if (event_type & eBreakpointEventTypeAdded
1901 // || event_type & eBreakpointEventTypeRemoved
1902 // || event_type & eBreakpointEventTypeEnabled
1903 // || event_type & eBreakpointEventTypeDisabled
1904 // || event_type & eBreakpointEventTypeCommandChanged
1905 // || event_type & eBreakpointEventTypeConditionChanged
1906 // || event_type & eBreakpointEventTypeIgnoreChanged
1907 // || event_type & eBreakpointEventTypeLocationsResolved)
1908 // {
1909 // // Don't do anything about these events, since the breakpoint
1910 // commands already echo these actions.
1911 // }
1912 //
1913 if (event_type & eBreakpointEventTypeLocationsAdded) {
1914 uint32_t num_new_locations =
1916 event_sp);
1917 if (num_new_locations > 0) {
1918 BreakpointSP breakpoint =
1920 if (StreamUP output_up = GetAsyncOutputStream()) {
1921 output_up->Printf("%d location%s added to breakpoint %d\n",
1922 num_new_locations, num_new_locations == 1 ? "" : "s",
1923 breakpoint->GetID());
1924 output_up->Flush();
1925 }
1926 }
1927 }
1928 // else if (event_type & eBreakpointEventTypeLocationsRemoved)
1929 // {
1930 // // These locations just get disabled, not sure it is worth spamming
1931 // folks about this on the command line.
1932 // }
1933 // else if (event_type & eBreakpointEventTypeLocationsResolved)
1934 // {
1935 // // This might be an interesting thing to note, but I'm going to
1936 // leave it quiet for now, it just looked noisy.
1937 // }
1938}
1939
1940void Debugger::FlushProcessOutput(Process &process, bool flush_stdout,
1941 bool flush_stderr) {
1942 const auto &flush = [&](Stream &stream,
1943 size_t (Process::*get)(char *, size_t, Status &)) {
1944 Status error;
1945 size_t len;
1946 char buffer[1024];
1947 while ((len = (process.*get)(buffer, sizeof(buffer), error)) > 0)
1948 stream.Write(buffer, len);
1949 stream.Flush();
1950 };
1951
1952 std::lock_guard<std::mutex> guard(m_output_flush_mutex);
1953 if (flush_stdout)
1955 if (flush_stderr)
1957}
1958
1959// This function handles events that were broadcast by the process.
1961 const uint32_t event_type = event_sp->GetType();
1962 ProcessSP process_sp =
1966
1967 StreamUP output_stream_up = GetAsyncOutputStream();
1968 StreamUP error_stream_up = GetAsyncErrorStream();
1969 const bool gui_enabled = IsForwardingEvents();
1970
1971 if (!gui_enabled) {
1972 bool pop_process_io_handler = false;
1973 assert(process_sp);
1974
1975 bool state_is_stopped = false;
1976 const bool got_state_changed =
1977 (event_type & Process::eBroadcastBitStateChanged) != 0;
1978 const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
1979 const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
1980 const bool got_structured_data =
1981 (event_type & Process::eBroadcastBitStructuredData) != 0;
1982
1983 if (got_state_changed) {
1984 StateType event_state =
1986 state_is_stopped = StateIsStoppedState(event_state, false);
1987 }
1988
1989 // Display running state changes first before any STDIO
1990 if (got_state_changed && !state_is_stopped) {
1991 // This is a public stop which we are going to announce to the user, so
1992 // we should force the most relevant frame selection here.
1993 Process::HandleProcessStateChangedEvent(event_sp, output_stream_up.get(),
1995 pop_process_io_handler);
1996 }
1997
1998 // Now display STDOUT and STDERR
1999 FlushProcessOutput(*process_sp, got_stdout || got_state_changed,
2000 got_stderr || got_state_changed);
2001
2002 // Give structured data events an opportunity to display.
2003 if (got_structured_data) {
2004 StructuredDataPluginSP plugin_sp =
2006 if (plugin_sp) {
2007 auto structured_data_sp =
2009 StreamString content_stream;
2010 Status error =
2011 plugin_sp->GetDescription(structured_data_sp, content_stream);
2012 if (error.Success()) {
2013 if (!content_stream.GetString().empty()) {
2014 // Add newline.
2015 content_stream.PutChar('\n');
2016 content_stream.Flush();
2017
2018 // Print it.
2019 output_stream_up->PutCString(content_stream.GetString());
2020 }
2021 } else {
2022 error_stream_up->Format("Failed to print structured "
2023 "data with plugin {0}: {1}",
2024 plugin_sp->GetPluginName(), error);
2025 }
2026 }
2027 }
2028
2029 // Now display any stopped state changes after any STDIO
2030 if (got_state_changed && state_is_stopped) {
2031 Process::HandleProcessStateChangedEvent(event_sp, output_stream_up.get(),
2033 pop_process_io_handler);
2034 }
2035
2036 output_stream_up->Flush();
2037 error_stream_up->Flush();
2038
2039 if (pop_process_io_handler)
2040 process_sp->PopProcessIOHandler();
2041 }
2042 return process_sp;
2043}
2044
2046 // At present the only thread event we handle is the Frame Changed event, and
2047 // all we do for that is just reprint the thread status for that thread.
2048 const uint32_t event_type = event_sp->GetType();
2049 const bool stop_format = true;
2050 ThreadSP thread_sp;
2051 if (event_type == Thread::eBroadcastBitStackChanged ||
2053 thread_sp = Thread::ThreadEventData::GetThreadFromEvent(event_sp.get());
2054 if (thread_sp) {
2055 thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1, stop_format,
2056 /*show_hidden*/ true);
2057 }
2058 }
2059 return thread_sp;
2060}
2061
2063
2065 m_forward_listener_sp = listener_sp;
2066}
2067
2069 m_forward_listener_sp.reset();
2070}
2071
2074 File &file = stream_sp->GetUnlockedFile();
2075 return file.GetIsInteractive() && file.GetIsRealTerminal() &&
2077 }
2078 return false;
2079}
2080
2082// We have trouble with the contol codes on Windows, see
2083// https://github.com/llvm/llvm-project/issues/134846.
2084#ifndef _WIN32
2086#else
2087 return false;
2088#endif
2089}
2090
2091static bool RequiresFollowChildWorkaround(const Process &process) {
2092 // FIXME: https://github.com/llvm/llvm-project/issues/160216
2093 return process.GetFollowForkMode() == eFollowChild;
2094}
2095
2097 ListenerSP listener_sp(GetListener());
2098 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
2099 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
2100 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
2101 BroadcastEventSpec target_event_spec(broadcaster_class_target,
2103
2104 BroadcastEventSpec process_event_spec(
2105 broadcaster_class_process,
2108
2109 BroadcastEventSpec thread_event_spec(broadcaster_class_thread,
2112
2113 listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp,
2114 target_event_spec);
2115 listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp,
2116 process_event_spec);
2117 listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp,
2118 thread_event_spec);
2119 listener_sp->StartListeningForEvents(
2124
2125 listener_sp->StartListeningForEvents(
2130
2131 // Let the thread that spawned us know that we have started up and that we
2132 // are now listening to all required events so no events get missed
2134
2135 if (StatuslineSupported()) {
2136 std::lock_guard<std::mutex> guard(m_statusline_mutex);
2137 if (!m_statusline) {
2138 m_statusline.emplace(*this);
2140 }
2141 }
2142
2143 bool done = false;
2144 while (!done) {
2145 EventSP event_sp;
2146 if (listener_sp->GetEvent(event_sp, std::nullopt)) {
2147 std::optional<ExecutionContextRef> exe_ctx_ref = std::nullopt;
2148 if (event_sp) {
2149 Broadcaster *broadcaster = event_sp->GetBroadcaster();
2150 if (broadcaster) {
2151 uint32_t event_type = event_sp->GetType();
2152 ConstString broadcaster_class(broadcaster->GetBroadcasterClass());
2153 if (broadcaster_class == broadcaster_class_process) {
2154 if (ProcessSP process_sp = HandleProcessEvent(event_sp))
2155 if (!RequiresFollowChildWorkaround(*process_sp))
2156 exe_ctx_ref = ExecutionContextRef(process_sp.get(),
2157 /*adopt_selected=*/true);
2158 } else if (broadcaster_class == broadcaster_class_target) {
2160 event_sp.get())) {
2161 HandleBreakpointEvent(event_sp);
2162 }
2163 } else if (broadcaster_class == broadcaster_class_thread) {
2164 if (ThreadSP thread_sp = HandleThreadEvent(event_sp))
2165 if (!RequiresFollowChildWorkaround(*thread_sp->GetProcess()))
2166 exe_ctx_ref = ExecutionContextRef(thread_sp.get(),
2167 /*adopt_selected=*/true);
2168 } else if (broadcaster == m_command_interpreter_up.get()) {
2169 if (event_type &
2171 done = true;
2172 } else if (event_type &
2174 const char *data = static_cast<const char *>(
2175 EventDataBytes::GetBytesFromEvent(event_sp.get()));
2176 if (data && data[0]) {
2177 StreamUP error_up = GetAsyncErrorStream();
2178 error_up->PutCString(data);
2179 error_up->Flush();
2180 }
2181 } else if (event_type & CommandInterpreter::
2182 eBroadcastBitAsynchronousOutputData) {
2183 const char *data = static_cast<const char *>(
2184 EventDataBytes::GetBytesFromEvent(event_sp.get()));
2185 if (data && data[0]) {
2186 StreamUP output_up = GetAsyncOutputStream();
2187 output_up->PutCString(data);
2188 output_up->Flush();
2189 }
2190 }
2191 } else if (broadcaster == &m_broadcaster) {
2192 if (event_type & lldb::eBroadcastBitProgress ||
2194 HandleProgressEvent(event_sp);
2195 else if (event_type & lldb::eBroadcastBitWarning)
2196 HandleDiagnosticEvent(event_sp);
2197 else if (event_type & lldb::eBroadcastBitError)
2198 HandleDiagnosticEvent(event_sp);
2199 }
2200 }
2201
2203 m_forward_listener_sp->AddEvent(event_sp);
2204 }
2205 RedrawStatusline(exe_ctx_ref);
2206 }
2207 }
2208
2209 {
2210 std::lock_guard<std::mutex> guard(m_statusline_mutex);
2211 if (m_statusline)
2212 m_statusline.reset();
2213 }
2214
2215 return {};
2216}
2217
2219 if (!m_event_handler_thread.IsJoinable()) {
2220 // We must synchronize with the DefaultEventHandler() thread to ensure it
2221 // is up and running and listening to events before we return from this
2222 // function. We do this by listening to events for the
2223 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
2224 ConstString full_name("lldb.debugger.event-handler");
2225 ListenerSP listener_sp(Listener::MakeListener(full_name.AsCString()));
2226 listener_sp->StartListeningForEvents(&m_sync_broadcaster,
2228
2229 llvm::StringRef thread_name =
2230 full_name.GetLength() < llvm::get_max_thread_name_length()
2231 ? full_name.GetStringRef()
2232 : "dbg.evt-handler";
2233
2234 // Use larger 8MB stack for this thread
2235 llvm::Expected<HostThread> event_handler_thread =
2237 thread_name, [this] { return DefaultEventHandler(); },
2239
2240 if (event_handler_thread) {
2241 m_event_handler_thread = *event_handler_thread;
2242 } else {
2243 LLDB_LOG_ERROR(GetLog(LLDBLog::Host), event_handler_thread.takeError(),
2244 "failed to launch host thread: {0}");
2245 }
2246
2247 // Make sure DefaultEventHandler() is running and listening to events
2248 // before we return from this function. We are only listening for events of
2249 // type eBroadcastBitEventThreadIsListening so we don't need to check the
2250 // event, we just need to wait an infinite amount of time for it (nullptr
2251 // timeout as the first parameter)
2252 lldb::EventSP event_sp;
2253 listener_sp->GetEvent(event_sp, std::nullopt);
2254 }
2255 return m_event_handler_thread.IsJoinable();
2256}
2257
2265
2271
2273 auto *data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
2274 if (!data)
2275 return;
2276
2277 // Make a local copy of the incoming progress report that we'll store.
2278 ProgressReport progress_report{data->GetID(), data->GetCompleted(),
2279 data->GetTotal(), data->GetMessage()};
2280
2281 {
2282 std::lock_guard<std::mutex> guard(m_progress_reports_mutex);
2283
2284 // Do some bookkeeping regardless of whether we're going to display
2285 // progress reports.
2286 auto it = llvm::find_if(m_progress_reports, [&](const auto &report) {
2287 return report.id == progress_report.id;
2288 });
2289 if (it != m_progress_reports.end()) {
2290 const bool complete = data->GetCompleted() == data->GetTotal();
2291 if (complete)
2292 m_progress_reports.erase(it);
2293 else
2294 *it = progress_report;
2295 } else {
2296 m_progress_reports.push_back(progress_report);
2297 }
2298
2299 // Show progress using Operating System Command (OSC) sequences.
2302
2303 // Clear progress if this was the last progress event.
2304 if (m_progress_reports.empty()) {
2305 stream_sp->Lock() << OSC_PROGRESS_REMOVE;
2306 return;
2307 }
2308
2309 const ProgressReport &report = m_progress_reports.back();
2310
2311 // Show indeterminate progress.
2312 if (report.total == UINT64_MAX) {
2313 stream_sp->Lock() << OSC_PROGRESS_INDETERMINATE;
2314 return;
2315 }
2316
2317 // Compute and show the progress value (0-100).
2318 const unsigned value = (report.completed / report.total) * 100;
2319 stream_sp->Lock().Printf(OSC_PROGRESS_SHOW, value);
2320 }
2321 }
2322 }
2323}
2324
2325std::optional<Debugger::ProgressReport>
2327 std::lock_guard<std::mutex> guard(m_progress_reports_mutex);
2328 if (m_progress_reports.empty())
2329 return std::nullopt;
2330 return m_progress_reports.back();
2331}
2332
2334 auto *data = DiagnosticEventData::GetEventDataFromEvent(event_sp.get());
2335 if (!data)
2336 return;
2337
2338 data->Dump(GetAsyncErrorStream().get());
2339}
2340
2342 return m_io_handler_thread.IsJoinable();
2343}
2344
2347 m_io_handler_thread = new_thread;
2348 return old_host;
2349}
2350
2352 if (!m_io_handler_thread.IsJoinable()) {
2353 llvm::Expected<HostThread> io_handler_thread = ThreadLauncher::LaunchThread(
2354 "lldb.debugger.io-handler", [this] { return IOHandlerThread(); },
2355 8 * 1024 * 1024); // Use larger 8MB stack for this thread
2356 if (io_handler_thread) {
2357 m_io_handler_thread = *io_handler_thread;
2358 } else {
2359 LLDB_LOG_ERROR(GetLog(LLDBLog::Host), io_handler_thread.takeError(),
2360 "failed to launch host thread: {0}");
2361 }
2362 }
2363 return m_io_handler_thread.IsJoinable();
2364}
2365
2367 if (m_io_handler_thread.IsJoinable()) {
2368 GetInputFile().Close();
2369 m_io_handler_thread.Join(nullptr);
2370 }
2371}
2372
2380
2382 if (!HasIOHandlerThread())
2383 return false;
2384 return m_io_handler_thread.EqualsThread(Host::GetCurrentThread());
2385}
2386
2388 if (!prefer_dummy) {
2389 if (TargetSP target = m_target_list.GetSelectedTarget())
2390 return *target;
2391 }
2392 return GetDummyTarget();
2393}
2394
2395Status Debugger::RunREPL(LanguageType language, const char *repl_options) {
2396 Status err;
2397 FileSpec repl_executable;
2398
2399 if (language == eLanguageTypeUnknown)
2400 language = GetREPLLanguage();
2401
2402 if (language == eLanguageTypeUnknown) {
2404
2405 if (auto single_lang = repl_languages.GetSingularLanguage()) {
2406 language = *single_lang;
2407 } else if (repl_languages.Empty()) {
2409 "LLDB isn't configured with REPL support for any languages.");
2410 return err;
2411 } else {
2413 "Multiple possible REPL languages. Please specify a language.");
2414 return err;
2415 }
2416 }
2417
2418 Target *const target =
2419 nullptr; // passing in an empty target means the REPL must create one
2420
2421 REPLSP repl_sp(REPL::Create(err, language, this, target, repl_options));
2422
2423 if (!err.Success()) {
2424 return err;
2425 }
2426
2427 if (!repl_sp) {
2429 "couldn't find a REPL for %s",
2431 return err;
2432 }
2433
2434 repl_sp->SetCompilerOptions(repl_options);
2435 repl_sp->RunLoop();
2436
2437 return err;
2438}
2439
2440llvm::ThreadPoolInterface &Debugger::GetThreadPool() {
2441 assert(g_thread_pool &&
2442 "Debugger::GetThreadPool called before Debugger::Initialize");
2443 return *g_thread_pool;
2444}
#define OSC_PROGRESS_REMOVE
#define OSC_PROGRESS_INDETERMINATE
#define OSC_PROGRESS_SHOW
static llvm::raw_ostream & error(Stream &strm)
static bool RequiresFollowChildWorkaround(const Process &process)
static std::recursive_mutex * g_debugger_list_mutex_ptr
Definition Debugger.cpp:108
static llvm::DefaultThreadPool * g_thread_pool
Definition Debugger.cpp:112
static constexpr OptionEnumValueElement g_show_disassembly_enum_values[]
Definition Debugger.cpp:114
static lldb::user_id_t g_unique_id
Definition Debugger.cpp:103
static constexpr OptionEnumValueElement g_language_enumerators[]
Definition Debugger.cpp:139
static void PrivateReportDiagnostic(Debugger &debugger, Severity severity, std::string message, bool debugger_specific)
static constexpr OptionEnumValueElement g_dwim_print_verbosities[]
Definition Debugger.cpp:157
static constexpr OptionEnumValueElement s_stop_show_column_values[]
Definition Debugger.cpp:167
static void PrivateReportProgress(Debugger &debugger, uint64_t progress_id, std::string title, std::string details, uint64_t completed, uint64_t total, bool is_debugger_specific, uint32_t progress_broadcast_bit)
static Debugger::DebuggerList * g_debugger_list_ptr
Definition Debugger.cpp:110
static int OpenPipe(int fds[2], std::size_t size)
static size_t g_debugger_event_thread_stack_bytes
Definition Debugger.cpp:104
static FileSystem::EnumerateDirectoryResult LoadPluginCallback(void *baton, llvm::sys::fs::file_type ft, llvm::StringRef path)
Definition Debugger.cpp:771
static std::shared_ptr< LogHandler > CreateLogHandler(LogHandlerKind log_handler_kind, int fd, bool should_close, size_t buffer_size)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOG_OPTION_APPEND
Definition Log.h:42
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP
Definition Log.h:38
#define LLDB_LOG_OPTION_PREPEND_THREAD_NAME
Definition Log.h:40
PIPES
Definition PipePosix.cpp:32
@ WRITE
Definition PipePosix.cpp:32
@ READ
Definition PipePosix.cpp:32
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:31
bool IsValid() const
Tests if this ArchSpec is valid.
Definition ArchSpec.h:366
static lldb::BreakpointEventType GetBreakpointEventTypeFromEvent(const lldb::EventSP &event_sp)
static lldb::BreakpointSP GetBreakpointFromEvent(const lldb::EventSP &event_sp)
static const BreakpointEventData * GetEventDataFromEvent(const Event *event_sp)
static size_t GetNumBreakpointLocationsFromEvent(const lldb::EventSP &event_sp)
lldb::BroadcastEventSpec
Definition Broadcaster.h:40
An event broadcasting class.
bool EventTypeHasListeners(uint32_t event_type)
virtual llvm::StringRef GetBroadcasterClass() const
This needs to be filled in if you are going to register the broadcaster with the broadcaster manager ...
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
void UpdatePrompt(llvm::StringRef prompt)
bool SaveTranscript(CommandReturnObject &result, std::optional< std::string > output_file=std::nullopt)
Save the current debugger session transcript to a file on disk.
std::string GetErrorString(bool with_diagnostics=true) const
Return the errors as a string.
llvm::StringRef GetOutputString() const
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
size_t GetLength() const
Get the length in bytes of string value.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const std::chrono::time_point< std::chrono::system_clock > m_interrupt_time
Definition Debugger.h:493
InterruptionReport(std::string function_name, std::string description)
Definition Debugger.h:475
A class to manage flag bits.
Definition Debugger.h:80
static void AssertCallback(llvm::StringRef message, llvm::StringRef backtrace, llvm::StringRef prompt)
llvm::StringRef GetAutosuggestionAnsiPrefix() const
Definition Debugger.cpp:541
bool SetUseExternalEditor(bool use_external_editor_p)
Definition Debugger.cpp:438
PlatformList m_platform_list
Definition Debugger.h:741
HostThread m_event_handler_thread
Definition Debugger.h:771
static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid)
Definition Debugger.cpp:932
FormatEntity::Entry GetDisassemblyFormat() const
Definition Debugger.cpp:300
uint64_t GetDisassemblyLineCount() const
Definition Debugger.cpp:636
lldb::TargetSP GetSelectedTarget()
Definition Debugger.h:180
llvm::StringRef GetDisabledAnsiSuffix() const
Definition Debugger.cpp:522
uint64_t GetTerminalHeight() const
Definition Debugger.cpp:410
lldb::LockableStreamFileSP m_output_stream_sp
Definition Debugger.h:728
bool SetExternalEditor(llvm::StringRef editor)
Definition Debugger.cpp:449
void RequestInterrupt()
Interruption in LLDB:
void ReportInterruption(const InterruptionReport &report)
ExecutionContext GetSelectedExecutionContext()
Get the execution context representing the selected entities in the selected target.
const std::string m_instance_name
Definition Debugger.h:767
static void Terminate()
Definition Debugger.cpp:723
lldb::ThreadSP HandleThreadEvent(const lldb::EventSP &event_sp)
void HandleProgressEvent(const lldb::EventSP &event_sp)
SourceManager & GetSourceManager()
bool SetShowProgress(bool show_progress)
Definition Debugger.cpp:475
bool StartEventHandlerThread()
Manually start the global event handler thread.
bool SetUseSourceCache(bool use_source_cache)
Definition Debugger.cpp:577
void StopEventHandlerThread()
Manually stop the debugger's default event handler.
static void ReportInfo(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report info events.
void SetAsyncExecution(bool async)
HostThread SetIOHandlerThread(HostThread &new_thread)
void CancelForwardEvents(const lldb::ListenerSP &listener_sp)
bool GetPrintDecls() const
Definition Debugger.cpp:665
lldb::FileSP GetInputFileSP()
Definition Debugger.h:136
bool GetHighlightSource() const
Definition Debugger.cpp:585
llvm::StringMap< std::weak_ptr< LogHandler > > m_stream_handlers
Definition Debugger.h:765
CommandInterpreter & GetCommandInterpreter()
Definition Debugger.h:163
FormatEntity::Entry GetStatuslineFormat() const
Definition Debugger.cpp:498
LoadedPluginsList m_loaded_plugins
Definition Debugger.h:770
bool GetShowInlineDiagnostics() const
Definition Debugger.cpp:694
bool SetTabSize(uint64_t tab_size)
Definition Debugger.cpp:682
void HandleDiagnosticEvent(const lldb::EventSP &event_sp)
static lldb::DebuggerSP GetDebuggerAtIndex(size_t index)
llvm::StringRef GetAutosuggestionAnsiSuffix() const
Definition Debugger.cpp:547
lldb::ListenerSP m_listener_sp
Definition Debugger.h:742
void PushIOHandler(const lldb::IOHandlerSP &reader_sp, bool cancel_top_handler=true)
std::mutex m_destroy_callback_mutex
Definition Debugger.h:786
lldb::callback_token_t AddDestroyCallback(lldb_private::DebuggerDestroyCallback destroy_callback, void *baton)
Add a callback for when the debugger is destroyed.
lldb::thread_result_t IOHandlerThread()
FormatEntity::Entry GetFrameFormatUnique() const
Definition Debugger.cpp:310
std::optional< ProgressReport > GetCurrentProgressReport() const
static lldb::TargetSP FindTargetWithProcess(Process *process)
Definition Debugger.cpp:946
llvm::StringRef GetDisabledAnsiPrefix() const
Definition Debugger.cpp:516
bool GetUseExternalEditor() const
Definition Debugger.cpp:432
TerminalState m_terminal_state
Definition Debugger.h:738
lldb::StreamUP GetAsyncErrorStream()
bool GetEscapeNonPrintables() const
Definition Debugger.cpp:648
lldb::TargetSP m_dummy_target_sp
Definition Debugger.h:777
llvm::SmallVector< DestroyCallbackInfo, 2 > m_destroy_callbacks
Definition Debugger.h:798
std::unique_ptr< CommandInterpreter > m_command_interpreter_up
Definition Debugger.h:752
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
static bool FormatDisassemblerAddress(const FormatEntity::Entry *format, const SymbolContext *sc, const SymbolContext *prev_sc, const ExecutionContext *exe_ctx, const Address *addr, Stream &s)
static void SettingsInitialize()
Definition Debugger.cpp:749
LockableStreamFile::Mutex m_output_mutex
Definition Debugger.h:730
static llvm::ThreadPoolInterface & GetThreadPool()
Shared thread pool. Use only with ThreadPoolTaskGroup.
llvm::StringRef GetShowProgressAnsiSuffix() const
Definition Debugger.cpp:486
bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp)
bool HasIOHandlerThread() const
llvm::SmallVector< ProgressReport, 4 > m_progress_reports
Bookkeeping for command line progress events.
Definition Debugger.h:782
bool IsIOHandlerThreadCurrentThread() const
void DispatchClientTelemetry(const lldb_private::StructuredDataImpl &entry)
Definition Debugger.cpp:859
std::mutex m_progress_reports_mutex
Definition Debugger.h:783
lldb::ProcessSP HandleProcessEvent(const lldb::EventSP &event_sp)
lldb::ListenerSP m_forward_listener_sp
Definition Debugger.h:775
std::array< lldb::ScriptInterpreterSP, lldb::eScriptLanguageUnknown > m_script_interpreters
Definition Debugger.h:756
std::shared_ptr< CallbackLogHandler > m_callback_handler_sp
Definition Debugger.h:766
void SetDestroyCallback(lldb_private::DebuggerDestroyCallback destroy_callback, void *baton)
DEPRECATED: We used to only support one Destroy callback.
lldb::FileSP GetOutputFileSP()
Definition Debugger.h:139
Broadcaster m_broadcaster
Public Debugger event broadcaster.
Definition Debugger.h:774
static void Initialize(LoadPluginCallbackType load_plugin_callback)
Definition Debugger.cpp:714
static lldb::DebuggerSP FindDebuggerWithInstanceName(llvm::StringRef instance_name)
Definition Debugger.cpp:917
uint64_t GetTerminalWidth() const
Definition Debugger.cpp:388
std::mutex m_interrupt_mutex
Definition Debugger.h:801
std::recursive_mutex m_io_handler_synchronous_mutex
Definition Debugger.h:759
bool RemoveIOHandler(const lldb::IOHandlerSP &reader_sp)
Remove the given IO handler if it's currently active.
Diagnostics::CallbackID m_diagnostics_callback_id
Definition Debugger.h:778
bool GetAutoOneLineSummaries() const
Definition Debugger.cpp:642
const char * GetIOHandlerCommandPrefix()
std::optional< Statusline > m_statusline
Definition Debugger.h:763
bool GetUseColor() const
Definition Debugger.cpp:454
lldb::BroadcasterManagerSP m_broadcaster_manager_sp
Definition Debugger.h:732
Status RunREPL(lldb::LanguageType language, const char *repl_options)
bool PopIOHandler(const lldb::IOHandlerSP &reader_sp)
bool RemoveDestroyCallback(lldb::callback_token_t token)
Remove the specified callback. Return true if successful.
static LoadPluginCallbackType g_load_plugin_callback
Definition Debugger.h:768
bool GetShowStatusline() const
Definition Debugger.cpp:492
std::recursive_mutex m_script_interpreter_mutex
Definition Debugger.h:754
void RunIOHandlerAsync(const lldb::IOHandlerSP &reader_sp, bool cancel_top_handler=true)
Run the given IO handler and return immediately.
void SetInputFile(lldb::FileSP file)
lldb::LockableStreamFileSP GetOutputStreamSP()
Except for Debugger and IOHandler, GetOutputStreamSP and GetErrorStreamSP should not be used directly...
Definition Debugger.h:681
void SetPrompt(llvm::StringRef p)
Definition Debugger.cpp:345
llvm::StringRef GetSeparator() const
Definition Debugger.cpp:510
bool EnableLog(llvm::StringRef channel, llvm::ArrayRef< const char * > categories, llvm::StringRef log_file, uint32_t log_options, size_t buffer_size, LogHandlerKind log_handler_kind, llvm::raw_ostream &error_stream)
uint64_t GetStopDisassemblyMaxSize() const
Definition Debugger.cpp:315
bool SetTerminalHeight(uint64_t term_height)
Definition Debugger.cpp:416
Broadcaster m_sync_broadcaster
Private debugger synchronization.
Definition Debugger.h:773
bool GetUseSourceCache() const
Definition Debugger.cpp:571
HostThread m_io_handler_thread
Definition Debugger.h:772
llvm::StringRef GetRegexMatchAnsiSuffix() const
Definition Debugger.cpp:559
static llvm::StringRef GetStaticBroadcasterClass()
Definition Debugger.cpp:960
lldb::FileSP m_input_file_sp
Definition Debugger.h:727
bool SetREPLLanguage(lldb::LanguageType repl_lang)
Definition Debugger.cpp:383
bool GetAutoIndent() const
Definition Debugger.cpp:654
llvm::StringRef GetStopShowColumnAnsiSuffix() const
Definition Debugger.cpp:604
Status SetPropertyValue(const ExecutionContext *exe_ctx, VarSetOperationType op, llvm::StringRef property_path, llvm::StringRef value) override
Definition Debugger.cpp:205
llvm::StringRef GetPromptAnsiSuffix() const
Definition Debugger.cpp:339
FormatEntity::Entry GetThreadStopFormat() const
Definition Debugger.cpp:361
void SetErrorFile(lldb::FileSP file)
bool GetAutoConfirm() const
Definition Debugger.cpp:294
TargetList m_target_list
Definition Debugger.h:739
lldb::ScriptLanguage GetScriptLanguage() const
Definition Debugger.cpp:366
lldb::callback_token_t m_destroy_callback_next_token
Definition Debugger.h:787
std::unique_ptr< SourceManager > m_source_manager_up
Definition Debugger.h:743
static lldb::DebuggerSP CreateInstance(lldb::LogOutputCallback log_callback=nullptr, void *baton=nullptr)
Definition Debugger.cpp:841
lldb::StopShowColumn GetStopShowColumn() const
Definition Debugger.cpp:591
static void ReportSymbolChange(const ModuleSpec &module_spec)
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
bool SetPrintDecls(bool b)
Definition Debugger.cpp:671
bool SetScriptLanguage(lldb::ScriptLanguage script_lang)
Definition Debugger.cpp:373
lldb::LockableStreamFileSP m_error_stream_sp
Definition Debugger.h:729
bool SetShowInlineDiagnostics(bool)
Definition Debugger.cpp:700
bool LoadPlugin(const FileSpec &spec, Status &error)
Definition Debugger.cpp:753
void SetOutputFile(lldb::FileSP file)
uint64_t GetStopSourceLineCount(bool before) const
Definition Debugger.cpp:622
bool SetStatuslineFormat(const FormatEntity::Entry &format)
Definition Debugger.cpp:503
void EnableForwardEvents(const lldb::ListenerSP &listener_sp)
static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id)
void HandleBreakpointEvent(const lldb::EventSP &event_sp)
bool CheckTopIOHandlerTypes(IOHandler::Type top_type, IOHandler::Type second_top_type)
Target & GetDummyTarget()
Definition Debugger.h:508
lldb::ListenerSP GetListener()
Definition Debugger.h:172
static void Destroy(lldb::DebuggerSP &debugger_sp)
Definition Debugger.cpp:885
SourceManager::SourceFileCache m_source_file_cache
Definition Debugger.h:747
ExecutionContextRef GetSelectedExecutionContextRef()
Similar to GetSelectedExecutionContext but returns a ExecutionContextRef, and will hold the dummy tar...
llvm::StringRef GetPromptAnsiPrefix() const
Definition Debugger.cpp:333
lldb::StreamUP GetAsyncOutputStream()
void RedrawStatusline(std::optional< ExecutionContextRef > exe_ctx_ref)
Redraw the statusline if enabled.
void RunIOHandlerSync(const lldb::IOHandlerSP &reader_sp)
Run the given IO handler and block until it's complete.
Broadcaster & GetBroadcaster()
Get the public broadcaster for this debugger.
Definition Debugger.h:87
llvm::StringRef GetStopShowLineMarkerAnsiPrefix() const
Definition Debugger.cpp:610
bool GetShowDontUsePoHint() const
Definition Debugger.cpp:565
uint64_t GetTabSize() const
Definition Debugger.cpp:676
bool GetShowProgress() const
Definition Debugger.cpp:469
llvm::StringRef GetRegexMatchAnsiPrefix() const
Definition Debugger.cpp:553
std::mutex m_output_flush_mutex
Definition Debugger.h:722
llvm::StringRef GetStopShowLineMarkerAnsiSuffix() const
Definition Debugger.cpp:616
bool SetSeparator(llvm::StringRef s)
Definition Debugger.cpp:528
bool SetUseColor(bool use_color)
Definition Debugger.cpp:460
std::mutex m_statusline_mutex
Mutex protecting the m_statusline member.
Definition Debugger.h:762
const char * GetIOHandlerHelpPrologue()
Status SetInputString(const char *data)
bool GetUseAutosuggestion() const
Definition Debugger.cpp:535
Target & GetSelectedOrDummyTarget(bool prefer_dummy=false)
IOHandlerStack m_io_handler_stack
Definition Debugger.h:758
llvm::once_flag m_clear_once
Definition Debugger.h:776
static void SettingsTerminate()
Definition Debugger.cpp:751
static void ReportProgress(uint64_t progress_id, std::string title, std::string details, uint64_t completed, uint64_t total, std::optional< lldb::user_id_t > debugger_id, uint32_t progress_category_bit=lldb::eBroadcastBitProgress)
Report progress events.
lldb::LanguageType GetREPLLanguage() const
Definition Debugger.cpp:378
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton)
bool SetTerminalWidth(uint64_t term_width)
Definition Debugger.cpp:394
Debugger(lldb::LogOutputCallback m_log_callback, void *baton)
Definition Debugger.cpp:965
llvm::StringRef GetExternalEditor() const
Definition Debugger.cpp:443
static size_t GetNumDebuggers()
FormatEntity::Entry GetThreadFormat() const
Definition Debugger.cpp:356
uint32_t m_interrupt_requested
Tracks interrupt requests.
Definition Debugger.h:800
lldb::DWIMPrintVerbosity GetDWIMPrintVerbosity() const
Definition Debugger.cpp:687
lldb::thread_result_t DefaultEventHandler()
void PrintAsync(const char *s, size_t len, bool is_stdout)
lldb::LockableStreamFileSP GetErrorStreamSP()
Definition Debugger.h:682
llvm::StringRef GetPrompt() const
Definition Debugger.cpp:327
bool GetNotifyVoid() const
Definition Debugger.cpp:321
llvm::StringRef GetShowProgressAnsiPrefix() const
Definition Debugger.cpp:480
void AdoptTopIOHandlerFilesIfInvalid(lldb::FileSP &in, lldb::LockableStreamFileSP &out, lldb::LockableStreamFileSP &err)
FormatEntity::Entry GetFrameFormat() const
Definition Debugger.cpp:305
lldb::StopDisassemblyType GetStopDisassemblyDisplay() const
Definition Debugger.cpp:629
llvm::StringRef GetTopIOHandlerControlSequence(char ch)
void FlushProcessOutput(Process &process, bool flush_stdout, bool flush_stderr)
Force flushing the process's pending stdout and stderr to the debugger's asynchronous stdout and stde...
void CancelInterruptRequest()
Decrement the "interrupt requested" counter.
static void ReportDiagnosticImpl(lldb::Severity severity, std::string message, std::optional< lldb::user_id_t > debugger_id, std::once_flag *once)
std::vector< lldb::DebuggerSP > DebuggerList
Definition Debugger.h:82
bool SetAutoIndent(bool b)
Definition Debugger.cpp:660
friend class CommandInterpreter
Definition Debugger.h:628
llvm::StringRef GetStopShowColumnAnsiPrefix() const
Definition Debugger.cpp:598
static DebuggerList DebuggersRequestingInterruption()
void Dump(Stream *s) const override
static const DiagnosticEventData * GetEventDataFromEvent(const Event *event_ptr)
CallbackID AddCallback(Callback callback)
void Report(llvm::StringRef message)
void RemoveCallback(CallbackID id)
static Diagnostics & Instance()
static const void * GetBytesFromEvent(const Event *event_ptr)
Definition Event.cpp:146
static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr)
Definition Event.cpp:247
static lldb::StructuredDataPluginSP GetPluginFromEvent(const Event *event_ptr)
Definition Event.cpp:265
static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr)
Definition Event.cpp:256
Execution context objects refer to objects in the execution of the program that is being debugged.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const lldb::TargetSP & GetTargetSP() const
Get accessor to get the target shared pointer.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
Target & GetTargetRef() const
Returns a reference to the target object.
A file utility class.
Definition FileSpec.h:57
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
Definition FileSpec.cpp:425
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
llvm::StringRef GetFileNameExtension() const
Extract the extension of the file.
Definition FileSpec.cpp:410
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
void EnumerateDirectory(llvm::Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton)
@ eEnumerateDirectoryResultEnter
Recurse into the current entry if it is a directory or symlink, or next if not.
Definition FileSystem.h:181
@ eEnumerateDirectoryResultNext
Enumerate next entry in the current directory.
Definition FileSystem.h:178
@ eEnumerateDirectoryResultQuit
Stop directory enumerations at any level.
Definition FileSystem.h:183
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
static FileSystem & Instance()
An abstract base class for files.
Definition File.h:36
bool GetIsRealTerminal()
Return true if this file from a real terminal.
Definition File.cpp:199
static int kInvalidDescriptor
Definition File.h:38
virtual int GetDescriptor() const
Get underlying OS file descriptor for this file, or kInvalidDescriptor.
Definition File.cpp:126
bool GetIsTerminalWithColors()
Return true if this file is a terminal which supports colors.
Definition File.cpp:205
Status Close() override
Flush any buffers and release any resources owned by the file.
Definition File.cpp:115
@ eOpenOptionReadOnly
Definition File.h:51
@ eOpenOptionWriteOnly
Definition File.h:52
@ eOpenOptionCanCreate
Definition File.h:56
@ eOpenOptionTruncate
Definition File.h:57
bool GetIsInteractive()
Return true if this file is interactive.
Definition File.cpp:193
const Mangled & GetMangled() const
Definition Function.h:534
static void SystemLog(lldb::Severity severity, llvm::StringRef message)
Emit the given message to the operating system log.
static lldb::thread_t GetCurrentThread()
Get the thread token (the one returned by ThreadCreate when the thread was created) for the calling t...
static llvm::StringRef GetSettingName()
Definition Language.cpp:45
static LanguageSet GetLanguagesSupportingREPLs()
Definition Language.cpp:479
static const char * GetNameForLanguageType(lldb::LanguageType language)
Returns the internal LLDB name for the specified language.
Definition Language.cpp:309
static LanguageProperties & GetGlobalLanguageProperties()
Definition Language.cpp:40
static lldb::ListenerSP MakeListener(const char *name)
Definition Listener.cpp:375
static bool EnableLogChannel(const std::shared_ptr< LogHandler > &log_handler_sp, uint32_t log_options, llvm::StringRef channel, llvm::ArrayRef< const char * > categories, llvm::raw_ostream &error_stream)
Definition Log.cpp:234
static ModuleListProperties & GetGlobalModuleListProperties()
static lldb::PlatformSP GetHostPlatform()
Get the native host platform plug-in.
Definition Platform.cpp:134
static PlatformProperties & GetGlobalPlatformProperties()
Definition Platform.cpp:140
static lldb::ScriptInterpreterSP GetScriptInterpreterForLanguage(lldb::ScriptLanguage script_lang, Debugger &debugger)
static void DebuggerInitialize(Debugger &debugger)
FollowForkMode GetFollowForkMode() const
Definition Process.cpp:367
static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr)
Definition Process.cpp:4448
static lldb::StateType GetStateFromEvent(const Event *event_ptr)
Definition Process.cpp:4456
A plug-in interface definition class for debugging a process.
Definition Process.h:354
static bool HandleProcessStateChangedEvent(const lldb::EventSP &event_sp, Stream *stream, SelectMostRelevant select_most_relevant, bool &pop_process_io_handler)
Centralize the code that handles and prints descriptions for process state changes.
Definition Process.cpp:731
static llvm::StringRef GetStaticBroadcasterClass()
Definition Process.cpp:421
virtual size_t GetSTDERR(char *buf, size_t buf_size, Status &error)
Get any available STDERR.
Definition Process.cpp:4651
virtual size_t GetSTDOUT(char *buf, size_t buf_size, Status &error)
Get any available STDOUT.
Definition Process.cpp:4632
static const ProgressEventData * GetEventDataFromEvent(const Event *event_ptr)
lldb::OptionValuePropertiesSP m_collection_sp
virtual Status SetPropertyValue(const ExecutionContext *exe_ctx, VarSetOperationType op, llvm::StringRef property_path, llvm::StringRef value)
T GetPropertyAtIndexAs(uint32_t idx, T default_value, const ExecutionContext *exe_ctx=nullptr) const
bool SetPropertyAtIndex(uint32_t idx, T t, const ExecutionContext *exe_ctx=nullptr) const
lldb::OptionValuePropertiesSP GetValueProperties() const
static lldb::REPLSP Create(Status &Status, lldb::LanguageType language, Debugger *debugger, Target *target, const char *repl_options)
Get a REPL with an existing target (or, failing that, a debugger to use), and (optional) extra argume...
Definition REPL.cpp:38
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Success() const
Test for success condition.
Definition Status.cpp:304
void Flush() override
Flush the stream.
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition Stream.h:112
size_t PutChar(char ch)
Definition Stream.cpp:131
virtual void Flush()=0
Flush the stream.
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
Symbol * symbol
The Symbol for a given query.
bool Compare(ConstString name, lldb::SymbolType type) const
Definition Symbol.cpp:386
ConstString GetName() const
Definition Symbol.cpp:511
lldb::SymbolType GetType() const
Definition Symbol.h:169
Debugger & GetDebugger() const
Definition Target.h:1194
static void SettingsTerminate()
Definition Target.cpp:2795
static llvm::StringRef GetStaticBroadcasterClass()
Definition Target.cpp:169
static TargetProperties & GetGlobalProperties()
Definition Target.cpp:3287
static ArchSpec GetDefaultArchitecture()
Definition Target.cpp:2805
@ eBroadcastBitBreakpointChanged
Definition Target.h:535
static void SettingsInitialize()
Definition Target.cpp:2793
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, std::function< lldb::thread_result_t()> thread_function, size_t min_stack_byte_size=0)
static lldb::ThreadSP GetThreadFromEvent(const Event *event_ptr)
Definition Thread.cpp:187
@ eBroadcastBitThreadSelected
Definition Thread.h:77
static llvm::StringRef GetStaticBroadcasterClass()
Definition Thread.cpp:219
virtual void DispatchClientTelemetry(const lldb_private::StructuredDataImpl &entry, Debugger *debugger)
static TelemetryManager * GetInstance()
#define UINT64_MAX
@ SelectMostRelevantFrame
#define LLDB_INVALID_HOST_THREAD
Definition lldb-types.h:69
Status Parse(const llvm::StringRef &format, Entry &entry)
bool Format(const Entry &entry, Stream &s, const SymbolContext *sc, const ExecutionContext *exe_ctx, const Address *addr, ValueObject *valobj, bool function_changed, bool initial_function)
std::string FormatAnsiTerminalCodes(llvm::StringRef format, bool do_color=true)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
LoadScriptFromSymFile
Definition Target.h:55
@ eLoadScriptFromSymFileTrue
Definition Target.h:56
@ eLoadScriptFromSymFileFalse
Definition Target.h:57
@ eLoadScriptFromSymFileWarn
Definition Target.h:58
const char * GetVersion()
Retrieves a string representing the complete LLDB version, which includes the lldb version number,...
Definition Version.cpp:38
void(* DebuggerDestroyCallback)(lldb::user_id_t debugger_id, void *baton)
bool StateIsStoppedState(lldb::StateType state, bool must_exist)
Check if a state represents a state where the process or thread is stopped.
Definition State.cpp:89
llvm::sys::DynamicLibrary(* LoadPluginCallbackType)(const lldb::DebuggerSP &debugger_sp, const FileSpec &spec, Status &error)
VarSetOperationType
Settable state variable types.
ScriptLanguage
Script interpreter types.
@ eScriptLanguageDefault
@ eScriptLanguageNone
@ eScriptLanguagePython
Severity
Used for expressing severity in logs and diagnostics.
@ eBroadcastBitExternalProgress
@ eBroadcastBitProgress
@ eBroadcastSymbolChange
std::shared_ptr< lldb_private::IOHandler > IOHandlerSP
std::shared_ptr< lldb_private::Thread > ThreadSP
void * thread_result_t
Definition lldb-types.h:62
std::shared_ptr< lldb_private::Platform > PlatformSP
DWIMPrintVerbosity
Enum to control the verbosity level of dwim-print execution.
@ eDWIMPrintVerbosityFull
Always print a message indicating how dwim-print is evaluating its expression.
@ eDWIMPrintVerbosityNone
Run dwim-print with no verbosity.
@ eDWIMPrintVerbosityExpression
Print a message when dwim-print uses expression evaluation.
StateType
Process and Thread States.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::StructuredDataPlugin > StructuredDataPluginSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
StopDisassemblyType
Used to determine when to show disassembly.
@ eStopDisassemblyTypeNever
@ eStopDisassemblyTypeNoSource
@ eStopDisassemblyTypeAlways
@ eStopDisassemblyTypeNoDebugInfo
@ eStopShowColumnAnsi
@ eStopShowColumnCaret
@ eStopShowColumnNone
@ eStopShowColumnAnsiOrCaret
std::shared_ptr< lldb_private::Event > EventSP
uint64_t pid_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::Listener > ListenerSP
int32_t callback_token_t
Definition lldb-types.h:81
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::LockableStreamFile > LockableStreamFileSP
void(* LogOutputCallback)(const char *, void *baton)
Definition lldb-types.h:73
std::shared_ptr< lldb_private::Target > TargetSP
std::unique_ptr< lldb_private::Stream > StreamUP
std::shared_ptr< lldb_private::File > FileSP
std::shared_ptr< lldb_private::REPL > REPLSP
lldb_private::DebuggerDestroyCallback callback
Definition Debugger.h:795
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition Type.h:38
std::optional< lldb::LanguageType > GetSingularLanguage()
If the set contains a single language only, return it.
UserID(lldb::user_id_t uid=LLDB_INVALID_UID)
Construct with optional user ID.
Definition UserID.h:33
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition UserID.h:47
Helper RAII class for collecting telemetry.
Definition Telemetry.h:269
#define PATH_MAX