15ffd83dbSDimitry Andric //===-- Debugger.cpp ------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
1281ad6265SDimitry Andric #include "lldb/Core/DebuggerEvents.h"
130b57cec5SDimitry Andric #include "lldb/Core/FormatEntity.h"
140b57cec5SDimitry Andric #include "lldb/Core/Mangled.h"
150b57cec5SDimitry Andric #include "lldb/Core/ModuleList.h"
16bdd1243dSDimitry Andric #include "lldb/Core/ModuleSpec.h"
170b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
180b57cec5SDimitry Andric #include "lldb/Core/StreamAsynchronousIO.h"
190b57cec5SDimitry Andric #include "lldb/DataFormatters/DataVisualization.h"
200b57cec5SDimitry Andric #include "lldb/Expression/REPL.h"
210b57cec5SDimitry Andric #include "lldb/Host/File.h"
220b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
230b57cec5SDimitry Andric #include "lldb/Host/HostInfo.h"
24*c9157d92SDimitry Andric #include "lldb/Host/StreamFile.h"
250b57cec5SDimitry Andric #include "lldb/Host/Terminal.h"
260b57cec5SDimitry Andric #include "lldb/Host/ThreadLauncher.h"
270b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
28fe6060f1SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
290b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValue.h"
3004eeddc0SDimitry Andric #include "lldb/Interpreter/OptionValueLanguage.h"
310b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValueProperties.h"
320b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValueSInt64.h"
330b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValueString.h"
340b57cec5SDimitry Andric #include "lldb/Interpreter/Property.h"
350b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
360b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
370b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
380b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
390b57cec5SDimitry Andric #include "lldb/Target/Language.h"
400b57cec5SDimitry Andric #include "lldb/Target/Process.h"
410b57cec5SDimitry Andric #include "lldb/Target/StructuredDataPlugin.h"
420b57cec5SDimitry Andric #include "lldb/Target/Target.h"
430b57cec5SDimitry Andric #include "lldb/Target/TargetList.h"
440b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
450b57cec5SDimitry Andric #include "lldb/Target/ThreadList.h"
460b57cec5SDimitry Andric #include "lldb/Utility/AnsiTerminal.h"
470b57cec5SDimitry Andric #include "lldb/Utility/Event.h"
4881ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
490b57cec5SDimitry Andric #include "lldb/Utility/Listener.h"
500b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
510b57cec5SDimitry Andric #include "lldb/Utility/State.h"
520b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
530b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
54bdd1243dSDimitry Andric #include "lldb/lldb-enumerations.h"
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric #if defined(_WIN32)
570b57cec5SDimitry Andric #include "lldb/Host/windows/PosixApi.h"
580b57cec5SDimitry Andric #include "lldb/Host/windows/windows.h"
590b57cec5SDimitry Andric #endif
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
620b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
630b57cec5SDimitry Andric #include "llvm/ADT/iterator.h"
640b57cec5SDimitry Andric #include "llvm/Support/DynamicLibrary.h"
650b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
660b57cec5SDimitry Andric #include "llvm/Support/Process.h"
6781ad6265SDimitry Andric #include "llvm/Support/ThreadPool.h"
680b57cec5SDimitry Andric #include "llvm/Support/Threading.h"
690b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
700b57cec5SDimitry Andric
71fe6060f1SDimitry Andric #include <cstdio>
72fe6060f1SDimitry Andric #include <cstdlib>
73fe6060f1SDimitry Andric #include <cstring>
740b57cec5SDimitry Andric #include <list>
750b57cec5SDimitry Andric #include <memory>
760b57cec5SDimitry Andric #include <mutex>
77bdd1243dSDimitry Andric #include <optional>
780b57cec5SDimitry Andric #include <set>
790b57cec5SDimitry Andric #include <string>
800b57cec5SDimitry Andric #include <system_error>
810b57cec5SDimitry Andric
824824e7fdSDimitry Andric // Includes for pipe()
834824e7fdSDimitry Andric #if defined(_WIN32)
844824e7fdSDimitry Andric #include <fcntl.h>
854824e7fdSDimitry Andric #include <io.h>
864824e7fdSDimitry Andric #else
874824e7fdSDimitry Andric #include <unistd.h>
884824e7fdSDimitry Andric #endif
894824e7fdSDimitry Andric
900b57cec5SDimitry Andric namespace lldb_private {
910b57cec5SDimitry Andric class Address;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric using namespace lldb;
950b57cec5SDimitry Andric using namespace lldb_private;
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric static lldb::user_id_t g_unique_id = 1;
980b57cec5SDimitry Andric static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric #pragma mark Static Functions
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric static std::recursive_mutex *g_debugger_list_mutex_ptr =
1030b57cec5SDimitry Andric nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
104fe013be4SDimitry Andric static Debugger::DebuggerList *g_debugger_list_ptr =
1050b57cec5SDimitry Andric nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
106bdd1243dSDimitry Andric static llvm::ThreadPool *g_thread_pool = nullptr;
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_show_disassembly_enum_values[] = {
1099dba64beSDimitry Andric {
1109dba64beSDimitry Andric Debugger::eStopDisassemblyTypeNever,
1119dba64beSDimitry Andric "never",
1129dba64beSDimitry Andric "Never show disassembly when displaying a stop context.",
1139dba64beSDimitry Andric },
1149dba64beSDimitry Andric {
1159dba64beSDimitry Andric Debugger::eStopDisassemblyTypeNoDebugInfo,
1169dba64beSDimitry Andric "no-debuginfo",
1179dba64beSDimitry Andric "Show disassembly when there is no debug information.",
1189dba64beSDimitry Andric },
1199dba64beSDimitry Andric {
1209dba64beSDimitry Andric Debugger::eStopDisassemblyTypeNoSource,
1219dba64beSDimitry Andric "no-source",
1229dba64beSDimitry Andric "Show disassembly when there is no source information, or the source "
1239dba64beSDimitry Andric "file "
1249dba64beSDimitry Andric "is missing when displaying a stop context.",
1259dba64beSDimitry Andric },
1269dba64beSDimitry Andric {
1279dba64beSDimitry Andric Debugger::eStopDisassemblyTypeAlways,
1289dba64beSDimitry Andric "always",
1299dba64beSDimitry Andric "Always show disassembly when displaying a stop context.",
1309dba64beSDimitry Andric },
1319dba64beSDimitry Andric };
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_language_enumerators[] = {
1349dba64beSDimitry Andric {
1359dba64beSDimitry Andric eScriptLanguageNone,
1369dba64beSDimitry Andric "none",
1379dba64beSDimitry Andric "Disable scripting languages.",
1389dba64beSDimitry Andric },
1399dba64beSDimitry Andric {
1409dba64beSDimitry Andric eScriptLanguagePython,
1419dba64beSDimitry Andric "python",
1429dba64beSDimitry Andric "Select python as the default scripting language.",
1439dba64beSDimitry Andric },
1449dba64beSDimitry Andric {
1459dba64beSDimitry Andric eScriptLanguageDefault,
1469dba64beSDimitry Andric "default",
1479dba64beSDimitry Andric "Select the lldb default as the default scripting language.",
1489dba64beSDimitry Andric },
1499dba64beSDimitry Andric };
1500b57cec5SDimitry Andric
151bdd1243dSDimitry Andric static constexpr OptionEnumValueElement g_dwim_print_verbosities[] = {
152bdd1243dSDimitry Andric {eDWIMPrintVerbosityNone, "none",
153bdd1243dSDimitry Andric "Use no verbosity when running dwim-print."},
154bdd1243dSDimitry Andric {eDWIMPrintVerbosityExpression, "expression",
155bdd1243dSDimitry Andric "Use partial verbosity when running dwim-print - display a message when "
156bdd1243dSDimitry Andric "`expression` evaluation is used."},
157bdd1243dSDimitry Andric {eDWIMPrintVerbosityFull, "full",
158bdd1243dSDimitry Andric "Use full verbosity when running dwim-print."},
159bdd1243dSDimitry Andric };
160bdd1243dSDimitry Andric
1610b57cec5SDimitry Andric static constexpr OptionEnumValueElement s_stop_show_column_values[] = {
1629dba64beSDimitry Andric {
1639dba64beSDimitry Andric eStopShowColumnAnsiOrCaret,
1649dba64beSDimitry Andric "ansi-or-caret",
1659dba64beSDimitry Andric "Highlight the stop column with ANSI terminal codes when color/ANSI "
1669dba64beSDimitry Andric "mode is enabled; otherwise, fall back to using a text-only caret (^) "
1679dba64beSDimitry Andric "as if \"caret-only\" mode was selected.",
1689dba64beSDimitry Andric },
1699dba64beSDimitry Andric {
1709dba64beSDimitry Andric eStopShowColumnAnsi,
1719dba64beSDimitry Andric "ansi",
1729dba64beSDimitry Andric "Highlight the stop column with ANSI terminal codes when running LLDB "
1739dba64beSDimitry Andric "with color/ANSI enabled.",
1749dba64beSDimitry Andric },
1759dba64beSDimitry Andric {
1769dba64beSDimitry Andric eStopShowColumnCaret,
1779dba64beSDimitry Andric "caret",
1789dba64beSDimitry Andric "Highlight the stop column with a caret character (^) underneath the "
1799dba64beSDimitry Andric "stop column. This method introduces a new line in source listings "
1809dba64beSDimitry Andric "that display thread stop locations.",
1819dba64beSDimitry Andric },
1829dba64beSDimitry Andric {
1839dba64beSDimitry Andric eStopShowColumnNone,
1849dba64beSDimitry Andric "none",
1859dba64beSDimitry Andric "Do not highlight the stop column.",
1869dba64beSDimitry Andric },
1879dba64beSDimitry Andric };
1880b57cec5SDimitry Andric
1899dba64beSDimitry Andric #define LLDB_PROPERTIES_debugger
1909dba64beSDimitry Andric #include "CoreProperties.inc"
1910b57cec5SDimitry Andric
1920b57cec5SDimitry Andric enum {
1939dba64beSDimitry Andric #define LLDB_PROPERTIES_debugger
1949dba64beSDimitry Andric #include "CorePropertiesEnum.inc"
1950b57cec5SDimitry Andric };
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric LoadPluginCallbackType Debugger::g_load_plugin_callback = nullptr;
1980b57cec5SDimitry Andric
SetPropertyValue(const ExecutionContext * exe_ctx,VarSetOperationType op,llvm::StringRef property_path,llvm::StringRef value)1990b57cec5SDimitry Andric Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
2000b57cec5SDimitry Andric VarSetOperationType op,
2010b57cec5SDimitry Andric llvm::StringRef property_path,
2020b57cec5SDimitry Andric llvm::StringRef value) {
2039dba64beSDimitry Andric bool is_load_script =
2049dba64beSDimitry Andric (property_path == "target.load-script-from-symbol-file");
2059dba64beSDimitry Andric // These properties might change how we visualize data.
2069dba64beSDimitry Andric bool invalidate_data_vis = (property_path == "escape-non-printables");
2079dba64beSDimitry Andric invalidate_data_vis |=
2089dba64beSDimitry Andric (property_path == "target.max-zero-padding-in-float-format");
2099dba64beSDimitry Andric if (invalidate_data_vis) {
2109dba64beSDimitry Andric DataVisualization::ForceUpdate();
2119dba64beSDimitry Andric }
2129dba64beSDimitry Andric
2130b57cec5SDimitry Andric TargetSP target_sp;
214bdd1243dSDimitry Andric LoadScriptFromSymFile load_script_old_value = eLoadScriptFromSymFileFalse;
215fe013be4SDimitry Andric if (is_load_script && exe_ctx && exe_ctx->GetTargetSP()) {
2160b57cec5SDimitry Andric target_sp = exe_ctx->GetTargetSP();
2170b57cec5SDimitry Andric load_script_old_value =
2180b57cec5SDimitry Andric target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric Status error(Properties::SetPropertyValue(exe_ctx, op, property_path, value));
2210b57cec5SDimitry Andric if (error.Success()) {
2220b57cec5SDimitry Andric // FIXME it would be nice to have "on-change" callbacks for properties
2239dba64beSDimitry Andric if (property_path == g_debugger_properties[ePropertyPrompt].name) {
2240b57cec5SDimitry Andric llvm::StringRef new_prompt = GetPrompt();
2259dba64beSDimitry Andric std::string str = lldb_private::ansi::FormatAnsiTerminalCodes(
2260b57cec5SDimitry Andric new_prompt, GetUseColor());
2270b57cec5SDimitry Andric if (str.length())
2280b57cec5SDimitry Andric new_prompt = str;
2290b57cec5SDimitry Andric GetCommandInterpreter().UpdatePrompt(new_prompt);
2309dba64beSDimitry Andric auto bytes = std::make_unique<EventDataBytes>(new_prompt);
2310b57cec5SDimitry Andric auto prompt_change_event_sp = std::make_shared<Event>(
2320b57cec5SDimitry Andric CommandInterpreter::eBroadcastBitResetPrompt, bytes.release());
2330b57cec5SDimitry Andric GetCommandInterpreter().BroadcastEvent(prompt_change_event_sp);
2349dba64beSDimitry Andric } else if (property_path == g_debugger_properties[ePropertyUseColor].name) {
2350b57cec5SDimitry Andric // use-color changed. Ping the prompt so it can reset the ansi terminal
2360b57cec5SDimitry Andric // codes.
2370b57cec5SDimitry Andric SetPrompt(GetPrompt());
238fe013be4SDimitry Andric } else if (property_path ==
239*c9157d92SDimitry Andric g_debugger_properties[ePropertyPromptAnsiPrefix].name ||
240*c9157d92SDimitry Andric property_path ==
241*c9157d92SDimitry Andric g_debugger_properties[ePropertyPromptAnsiSuffix].name) {
242*c9157d92SDimitry Andric // Prompt colors changed. Ping the prompt so it can reset the ansi
243*c9157d92SDimitry Andric // terminal codes.
244*c9157d92SDimitry Andric SetPrompt(GetPrompt());
245*c9157d92SDimitry Andric } else if (property_path ==
246fe013be4SDimitry Andric g_debugger_properties[ePropertyUseSourceCache].name) {
247fe013be4SDimitry Andric // use-source-cache changed. Wipe out the cache contents if it was
248fe013be4SDimitry Andric // disabled.
2495ffd83dbSDimitry Andric if (!GetUseSourceCache()) {
2505ffd83dbSDimitry Andric m_source_file_cache.Clear();
2515ffd83dbSDimitry Andric }
2520b57cec5SDimitry Andric } else if (is_load_script && target_sp &&
2530b57cec5SDimitry Andric load_script_old_value == eLoadScriptFromSymFileWarn) {
2540b57cec5SDimitry Andric if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() ==
2550b57cec5SDimitry Andric eLoadScriptFromSymFileTrue) {
2560b57cec5SDimitry Andric std::list<Status> errors;
2570b57cec5SDimitry Andric StreamString feedback_stream;
258fe013be4SDimitry Andric if (!target_sp->LoadScriptingResources(errors, feedback_stream)) {
2599dba64beSDimitry Andric Stream &s = GetErrorStream();
2600b57cec5SDimitry Andric for (auto error : errors) {
2619dba64beSDimitry Andric s.Printf("%s\n", error.AsCString());
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric if (feedback_stream.GetSize())
2649dba64beSDimitry Andric s.PutCString(feedback_stream.GetString());
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric return error;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric
GetAutoConfirm() const2720b57cec5SDimitry Andric bool Debugger::GetAutoConfirm() const {
273fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyAutoConfirm;
274fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
275fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric
GetDisassemblyFormat() const2780b57cec5SDimitry Andric const FormatEntity::Entry *Debugger::GetDisassemblyFormat() const {
279fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyDisassemblyFormat;
280fe013be4SDimitry Andric return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric
GetFrameFormat() const2830b57cec5SDimitry Andric const FormatEntity::Entry *Debugger::GetFrameFormat() const {
284fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyFrameFormat;
285fe013be4SDimitry Andric return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric
GetFrameFormatUnique() const2880b57cec5SDimitry Andric const FormatEntity::Entry *Debugger::GetFrameFormatUnique() const {
289fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyFrameFormatUnique;
290fe013be4SDimitry Andric return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric
GetStopDisassemblyMaxSize() const293fe013be4SDimitry Andric uint64_t Debugger::GetStopDisassemblyMaxSize() const {
294fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyStopDisassemblyMaxSize;
295fe013be4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
296fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value);
297fe6060f1SDimitry Andric }
298fe6060f1SDimitry Andric
GetNotifyVoid() const2990b57cec5SDimitry Andric bool Debugger::GetNotifyVoid() const {
300fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyNotiftVoid;
301fe013be4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
302fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric
GetPrompt() const3050b57cec5SDimitry Andric llvm::StringRef Debugger::GetPrompt() const {
306fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyPrompt;
307fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
308fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric
GetPromptAnsiPrefix() const311*c9157d92SDimitry Andric llvm::StringRef Debugger::GetPromptAnsiPrefix() const {
312*c9157d92SDimitry Andric const uint32_t idx = ePropertyPromptAnsiPrefix;
313*c9157d92SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
314*c9157d92SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
315*c9157d92SDimitry Andric }
316*c9157d92SDimitry Andric
GetPromptAnsiSuffix() const317*c9157d92SDimitry Andric llvm::StringRef Debugger::GetPromptAnsiSuffix() const {
318*c9157d92SDimitry Andric const uint32_t idx = ePropertyPromptAnsiSuffix;
319*c9157d92SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
320*c9157d92SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
321*c9157d92SDimitry Andric }
322*c9157d92SDimitry Andric
SetPrompt(llvm::StringRef p)3230b57cec5SDimitry Andric void Debugger::SetPrompt(llvm::StringRef p) {
324fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyPrompt;
325fe013be4SDimitry Andric SetPropertyAtIndex(idx, p);
3260b57cec5SDimitry Andric llvm::StringRef new_prompt = GetPrompt();
3270b57cec5SDimitry Andric std::string str =
3289dba64beSDimitry Andric lldb_private::ansi::FormatAnsiTerminalCodes(new_prompt, GetUseColor());
3290b57cec5SDimitry Andric if (str.length())
3300b57cec5SDimitry Andric new_prompt = str;
3310b57cec5SDimitry Andric GetCommandInterpreter().UpdatePrompt(new_prompt);
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
GetThreadFormat() const3340b57cec5SDimitry Andric const FormatEntity::Entry *Debugger::GetThreadFormat() const {
335fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyThreadFormat;
336fe013be4SDimitry Andric return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric
GetThreadStopFormat() const3390b57cec5SDimitry Andric const FormatEntity::Entry *Debugger::GetThreadStopFormat() const {
340fe013be4SDimitry Andric constexpr uint32_t idx = ePropertyThreadStopFormat;
341fe013be4SDimitry Andric return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric
GetScriptLanguage() const3440b57cec5SDimitry Andric lldb::ScriptLanguage Debugger::GetScriptLanguage() const {
3450b57cec5SDimitry Andric const uint32_t idx = ePropertyScriptLanguage;
346fe013be4SDimitry Andric return GetPropertyAtIndexAs<lldb::ScriptLanguage>(
347fe013be4SDimitry Andric idx, static_cast<lldb::ScriptLanguage>(
348fe013be4SDimitry Andric g_debugger_properties[idx].default_uint_value));
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric
SetScriptLanguage(lldb::ScriptLanguage script_lang)3510b57cec5SDimitry Andric bool Debugger::SetScriptLanguage(lldb::ScriptLanguage script_lang) {
3520b57cec5SDimitry Andric const uint32_t idx = ePropertyScriptLanguage;
353fe013be4SDimitry Andric return SetPropertyAtIndex(idx, script_lang);
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
GetREPLLanguage() const35604eeddc0SDimitry Andric lldb::LanguageType Debugger::GetREPLLanguage() const {
35704eeddc0SDimitry Andric const uint32_t idx = ePropertyREPLLanguage;
358fe013be4SDimitry Andric return GetPropertyAtIndexAs<LanguageType>(idx, {});
35904eeddc0SDimitry Andric }
36004eeddc0SDimitry Andric
SetREPLLanguage(lldb::LanguageType repl_lang)36104eeddc0SDimitry Andric bool Debugger::SetREPLLanguage(lldb::LanguageType repl_lang) {
36204eeddc0SDimitry Andric const uint32_t idx = ePropertyREPLLanguage;
363fe013be4SDimitry Andric return SetPropertyAtIndex(idx, repl_lang);
36404eeddc0SDimitry Andric }
36504eeddc0SDimitry Andric
GetTerminalWidth() const366fe013be4SDimitry Andric uint64_t Debugger::GetTerminalWidth() const {
3670b57cec5SDimitry Andric const uint32_t idx = ePropertyTerminalWidth;
368fe013be4SDimitry Andric return GetPropertyAtIndexAs<int64_t>(
369fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value);
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric
SetTerminalWidth(uint64_t term_width)372fe013be4SDimitry Andric bool Debugger::SetTerminalWidth(uint64_t term_width) {
3735ffd83dbSDimitry Andric if (auto handler_sp = m_io_handler_stack.Top())
3745ffd83dbSDimitry Andric handler_sp->TerminalSizeChanged();
3755ffd83dbSDimitry Andric
3760b57cec5SDimitry Andric const uint32_t idx = ePropertyTerminalWidth;
377fe013be4SDimitry Andric return SetPropertyAtIndex(idx, term_width);
3780b57cec5SDimitry Andric }
3790b57cec5SDimitry Andric
GetUseExternalEditor() const3800b57cec5SDimitry Andric bool Debugger::GetUseExternalEditor() const {
3810b57cec5SDimitry Andric const uint32_t idx = ePropertyUseExternalEditor;
382fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
383fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric
SetUseExternalEditor(bool b)3860b57cec5SDimitry Andric bool Debugger::SetUseExternalEditor(bool b) {
3870b57cec5SDimitry Andric const uint32_t idx = ePropertyUseExternalEditor;
388fe013be4SDimitry Andric return SetPropertyAtIndex(idx, b);
389fe013be4SDimitry Andric }
390fe013be4SDimitry Andric
GetExternalEditor() const391fe013be4SDimitry Andric llvm::StringRef Debugger::GetExternalEditor() const {
392fe013be4SDimitry Andric const uint32_t idx = ePropertyExternalEditor;
393fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
394fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
395fe013be4SDimitry Andric }
396fe013be4SDimitry Andric
SetExternalEditor(llvm::StringRef editor)397fe013be4SDimitry Andric bool Debugger::SetExternalEditor(llvm::StringRef editor) {
398fe013be4SDimitry Andric const uint32_t idx = ePropertyExternalEditor;
399fe013be4SDimitry Andric return SetPropertyAtIndex(idx, editor);
4000b57cec5SDimitry Andric }
4010b57cec5SDimitry Andric
GetUseColor() const4020b57cec5SDimitry Andric bool Debugger::GetUseColor() const {
4030b57cec5SDimitry Andric const uint32_t idx = ePropertyUseColor;
404fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
405fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric
SetUseColor(bool b)4080b57cec5SDimitry Andric bool Debugger::SetUseColor(bool b) {
4090b57cec5SDimitry Andric const uint32_t idx = ePropertyUseColor;
410fe013be4SDimitry Andric bool ret = SetPropertyAtIndex(idx, b);
4110b57cec5SDimitry Andric SetPrompt(GetPrompt());
4120b57cec5SDimitry Andric return ret;
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric
GetShowProgress() const41581ad6265SDimitry Andric bool Debugger::GetShowProgress() const {
41681ad6265SDimitry Andric const uint32_t idx = ePropertyShowProgress;
417fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
418fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
41981ad6265SDimitry Andric }
42081ad6265SDimitry Andric
SetShowProgress(bool show_progress)42181ad6265SDimitry Andric bool Debugger::SetShowProgress(bool show_progress) {
42281ad6265SDimitry Andric const uint32_t idx = ePropertyShowProgress;
423fe013be4SDimitry Andric return SetPropertyAtIndex(idx, show_progress);
42481ad6265SDimitry Andric }
42581ad6265SDimitry Andric
GetShowProgressAnsiPrefix() const42681ad6265SDimitry Andric llvm::StringRef Debugger::GetShowProgressAnsiPrefix() const {
42781ad6265SDimitry Andric const uint32_t idx = ePropertyShowProgressAnsiPrefix;
428fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
429fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
43081ad6265SDimitry Andric }
43181ad6265SDimitry Andric
GetShowProgressAnsiSuffix() const43281ad6265SDimitry Andric llvm::StringRef Debugger::GetShowProgressAnsiSuffix() const {
43381ad6265SDimitry Andric const uint32_t idx = ePropertyShowProgressAnsiSuffix;
434fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
435fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
43681ad6265SDimitry Andric }
43781ad6265SDimitry Andric
GetUseAutosuggestion() const438e8d8bef9SDimitry Andric bool Debugger::GetUseAutosuggestion() const {
439e8d8bef9SDimitry Andric const uint32_t idx = ePropertyShowAutosuggestion;
440fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
441fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
442e8d8bef9SDimitry Andric }
443e8d8bef9SDimitry Andric
GetAutosuggestionAnsiPrefix() const44481ad6265SDimitry Andric llvm::StringRef Debugger::GetAutosuggestionAnsiPrefix() const {
44581ad6265SDimitry Andric const uint32_t idx = ePropertyShowAutosuggestionAnsiPrefix;
446fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
447fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
44881ad6265SDimitry Andric }
44981ad6265SDimitry Andric
GetAutosuggestionAnsiSuffix() const45081ad6265SDimitry Andric llvm::StringRef Debugger::GetAutosuggestionAnsiSuffix() const {
45181ad6265SDimitry Andric const uint32_t idx = ePropertyShowAutosuggestionAnsiSuffix;
452fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
453fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
45481ad6265SDimitry Andric }
45581ad6265SDimitry Andric
GetRegexMatchAnsiPrefix() const456*c9157d92SDimitry Andric llvm::StringRef Debugger::GetRegexMatchAnsiPrefix() const {
457*c9157d92SDimitry Andric const uint32_t idx = ePropertyShowRegexMatchAnsiPrefix;
458*c9157d92SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
459*c9157d92SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
460*c9157d92SDimitry Andric }
461*c9157d92SDimitry Andric
GetRegexMatchAnsiSuffix() const462*c9157d92SDimitry Andric llvm::StringRef Debugger::GetRegexMatchAnsiSuffix() const {
463*c9157d92SDimitry Andric const uint32_t idx = ePropertyShowRegexMatchAnsiSuffix;
464*c9157d92SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
465*c9157d92SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
466*c9157d92SDimitry Andric }
467*c9157d92SDimitry Andric
GetShowDontUsePoHint() const468*c9157d92SDimitry Andric bool Debugger::GetShowDontUsePoHint() const {
469*c9157d92SDimitry Andric const uint32_t idx = ePropertyShowDontUsePoHint;
470*c9157d92SDimitry Andric return GetPropertyAtIndexAs<bool>(
471*c9157d92SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
472*c9157d92SDimitry Andric }
473*c9157d92SDimitry Andric
GetUseSourceCache() const4745ffd83dbSDimitry Andric bool Debugger::GetUseSourceCache() const {
4755ffd83dbSDimitry Andric const uint32_t idx = ePropertyUseSourceCache;
476fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
477fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
4785ffd83dbSDimitry Andric }
4795ffd83dbSDimitry Andric
SetUseSourceCache(bool b)4805ffd83dbSDimitry Andric bool Debugger::SetUseSourceCache(bool b) {
4815ffd83dbSDimitry Andric const uint32_t idx = ePropertyUseSourceCache;
482fe013be4SDimitry Andric bool ret = SetPropertyAtIndex(idx, b);
4835ffd83dbSDimitry Andric if (!ret) {
4845ffd83dbSDimitry Andric m_source_file_cache.Clear();
4855ffd83dbSDimitry Andric }
4865ffd83dbSDimitry Andric return ret;
4875ffd83dbSDimitry Andric }
GetHighlightSource() const4880b57cec5SDimitry Andric bool Debugger::GetHighlightSource() const {
4890b57cec5SDimitry Andric const uint32_t idx = ePropertyHighlightSource;
490fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
491fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric
GetStopShowColumn() const4940b57cec5SDimitry Andric StopShowColumn Debugger::GetStopShowColumn() const {
4950b57cec5SDimitry Andric const uint32_t idx = ePropertyStopShowColumn;
496fe013be4SDimitry Andric return GetPropertyAtIndexAs<lldb::StopShowColumn>(
497fe013be4SDimitry Andric idx, static_cast<lldb::StopShowColumn>(
498fe013be4SDimitry Andric g_debugger_properties[idx].default_uint_value));
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric
GetStopShowColumnAnsiPrefix() const5010b57cec5SDimitry Andric llvm::StringRef Debugger::GetStopShowColumnAnsiPrefix() const {
5020b57cec5SDimitry Andric const uint32_t idx = ePropertyStopShowColumnAnsiPrefix;
503fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
504fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
5050b57cec5SDimitry Andric }
5060b57cec5SDimitry Andric
GetStopShowColumnAnsiSuffix() const5070b57cec5SDimitry Andric llvm::StringRef Debugger::GetStopShowColumnAnsiSuffix() const {
5080b57cec5SDimitry Andric const uint32_t idx = ePropertyStopShowColumnAnsiSuffix;
509fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
510fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric
GetStopShowLineMarkerAnsiPrefix() const5135ffd83dbSDimitry Andric llvm::StringRef Debugger::GetStopShowLineMarkerAnsiPrefix() const {
5145ffd83dbSDimitry Andric const uint32_t idx = ePropertyStopShowLineMarkerAnsiPrefix;
515fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
516fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
5175ffd83dbSDimitry Andric }
5185ffd83dbSDimitry Andric
GetStopShowLineMarkerAnsiSuffix() const5195ffd83dbSDimitry Andric llvm::StringRef Debugger::GetStopShowLineMarkerAnsiSuffix() const {
5205ffd83dbSDimitry Andric const uint32_t idx = ePropertyStopShowLineMarkerAnsiSuffix;
521fe013be4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
522fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_cstr_value);
5235ffd83dbSDimitry Andric }
5245ffd83dbSDimitry Andric
GetStopSourceLineCount(bool before) const525fe013be4SDimitry Andric uint64_t Debugger::GetStopSourceLineCount(bool before) const {
5260b57cec5SDimitry Andric const uint32_t idx =
5270b57cec5SDimitry Andric before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
528fe013be4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
529fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value);
5300b57cec5SDimitry Andric }
5310b57cec5SDimitry Andric
GetStopDisassemblyDisplay() const5320b57cec5SDimitry Andric Debugger::StopDisassemblyType Debugger::GetStopDisassemblyDisplay() const {
5330b57cec5SDimitry Andric const uint32_t idx = ePropertyStopDisassemblyDisplay;
534fe013be4SDimitry Andric return GetPropertyAtIndexAs<Debugger::StopDisassemblyType>(
535fe013be4SDimitry Andric idx, static_cast<Debugger::StopDisassemblyType>(
536fe013be4SDimitry Andric g_debugger_properties[idx].default_uint_value));
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric
GetDisassemblyLineCount() const539fe013be4SDimitry Andric uint64_t Debugger::GetDisassemblyLineCount() const {
5400b57cec5SDimitry Andric const uint32_t idx = ePropertyStopDisassemblyCount;
541fe013be4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
542fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value);
5430b57cec5SDimitry Andric }
5440b57cec5SDimitry Andric
GetAutoOneLineSummaries() const5450b57cec5SDimitry Andric bool Debugger::GetAutoOneLineSummaries() const {
5460b57cec5SDimitry Andric const uint32_t idx = ePropertyAutoOneLineSummaries;
547fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
548fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
5490b57cec5SDimitry Andric }
5500b57cec5SDimitry Andric
GetEscapeNonPrintables() const5510b57cec5SDimitry Andric bool Debugger::GetEscapeNonPrintables() const {
5520b57cec5SDimitry Andric const uint32_t idx = ePropertyEscapeNonPrintables;
553fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
554fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
5550b57cec5SDimitry Andric }
5560b57cec5SDimitry Andric
GetAutoIndent() const5570b57cec5SDimitry Andric bool Debugger::GetAutoIndent() const {
5580b57cec5SDimitry Andric const uint32_t idx = ePropertyAutoIndent;
559fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
560fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric
SetAutoIndent(bool b)5630b57cec5SDimitry Andric bool Debugger::SetAutoIndent(bool b) {
5640b57cec5SDimitry Andric const uint32_t idx = ePropertyAutoIndent;
565fe013be4SDimitry Andric return SetPropertyAtIndex(idx, b);
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric
GetPrintDecls() const5680b57cec5SDimitry Andric bool Debugger::GetPrintDecls() const {
5690b57cec5SDimitry Andric const uint32_t idx = ePropertyPrintDecls;
570fe013be4SDimitry Andric return GetPropertyAtIndexAs<bool>(
571fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value != 0);
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric
SetPrintDecls(bool b)5740b57cec5SDimitry Andric bool Debugger::SetPrintDecls(bool b) {
5750b57cec5SDimitry Andric const uint32_t idx = ePropertyPrintDecls;
576fe013be4SDimitry Andric return SetPropertyAtIndex(idx, b);
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric
GetTabSize() const579fe013be4SDimitry Andric uint64_t Debugger::GetTabSize() const {
5800b57cec5SDimitry Andric const uint32_t idx = ePropertyTabSize;
581fe013be4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
582fe013be4SDimitry Andric idx, g_debugger_properties[idx].default_uint_value);
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric
SetTabSize(uint64_t tab_size)585fe013be4SDimitry Andric bool Debugger::SetTabSize(uint64_t tab_size) {
5860b57cec5SDimitry Andric const uint32_t idx = ePropertyTabSize;
587fe013be4SDimitry Andric return SetPropertyAtIndex(idx, tab_size);
5880b57cec5SDimitry Andric }
5890b57cec5SDimitry Andric
GetDWIMPrintVerbosity() const590bdd1243dSDimitry Andric lldb::DWIMPrintVerbosity Debugger::GetDWIMPrintVerbosity() const {
591bdd1243dSDimitry Andric const uint32_t idx = ePropertyDWIMPrintVerbosity;
592fe013be4SDimitry Andric return GetPropertyAtIndexAs<lldb::DWIMPrintVerbosity>(
593fe013be4SDimitry Andric idx, static_cast<lldb::DWIMPrintVerbosity>(
594fe013be4SDimitry Andric g_debugger_properties[idx].default_uint_value));
595bdd1243dSDimitry Andric }
596bdd1243dSDimitry Andric
5970b57cec5SDimitry Andric #pragma mark Debugger
5980b57cec5SDimitry Andric
5990b57cec5SDimitry Andric // const DebuggerPropertiesSP &
6000b57cec5SDimitry Andric // Debugger::GetSettings() const
6010b57cec5SDimitry Andric //{
6020b57cec5SDimitry Andric // return m_properties_sp;
6030b57cec5SDimitry Andric //}
6040b57cec5SDimitry Andric //
6050b57cec5SDimitry Andric
Initialize(LoadPluginCallbackType load_plugin_callback)6060b57cec5SDimitry Andric void Debugger::Initialize(LoadPluginCallbackType load_plugin_callback) {
6070b57cec5SDimitry Andric assert(g_debugger_list_ptr == nullptr &&
6080b57cec5SDimitry Andric "Debugger::Initialize called more than once!");
6090b57cec5SDimitry Andric g_debugger_list_mutex_ptr = new std::recursive_mutex();
6100b57cec5SDimitry Andric g_debugger_list_ptr = new DebuggerList();
611bdd1243dSDimitry Andric g_thread_pool = new llvm::ThreadPool(llvm::optimal_concurrency());
6120b57cec5SDimitry Andric g_load_plugin_callback = load_plugin_callback;
6130b57cec5SDimitry Andric }
6140b57cec5SDimitry Andric
Terminate()6150b57cec5SDimitry Andric void Debugger::Terminate() {
6160b57cec5SDimitry Andric assert(g_debugger_list_ptr &&
6170b57cec5SDimitry Andric "Debugger::Terminate called without a matching Debugger::Initialize!");
6180b57cec5SDimitry Andric
619fe013be4SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
620fe013be4SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
621fe013be4SDimitry Andric for (const auto &debugger : *g_debugger_list_ptr)
622fe013be4SDimitry Andric debugger->HandleDestroyCallback();
623fe013be4SDimitry Andric }
624fe013be4SDimitry Andric
625bdd1243dSDimitry Andric if (g_thread_pool) {
626bdd1243dSDimitry Andric // The destructor will wait for all the threads to complete.
627bdd1243dSDimitry Andric delete g_thread_pool;
628bdd1243dSDimitry Andric }
629bdd1243dSDimitry Andric
6300b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
631349cc55cSDimitry Andric // Clear our global list of debugger objects
6320b57cec5SDimitry Andric {
6330b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
6340b57cec5SDimitry Andric for (const auto &debugger : *g_debugger_list_ptr)
6350b57cec5SDimitry Andric debugger->Clear();
6360b57cec5SDimitry Andric g_debugger_list_ptr->clear();
6370b57cec5SDimitry Andric }
6380b57cec5SDimitry Andric }
6390b57cec5SDimitry Andric }
6400b57cec5SDimitry Andric
SettingsInitialize()6410b57cec5SDimitry Andric void Debugger::SettingsInitialize() { Target::SettingsInitialize(); }
6420b57cec5SDimitry Andric
SettingsTerminate()6430b57cec5SDimitry Andric void Debugger::SettingsTerminate() { Target::SettingsTerminate(); }
6440b57cec5SDimitry Andric
LoadPlugin(const FileSpec & spec,Status & error)6450b57cec5SDimitry Andric bool Debugger::LoadPlugin(const FileSpec &spec, Status &error) {
6460b57cec5SDimitry Andric if (g_load_plugin_callback) {
6470b57cec5SDimitry Andric llvm::sys::DynamicLibrary dynlib =
6480b57cec5SDimitry Andric g_load_plugin_callback(shared_from_this(), spec, error);
6490b57cec5SDimitry Andric if (dynlib.isValid()) {
6500b57cec5SDimitry Andric m_loaded_plugins.push_back(dynlib);
6510b57cec5SDimitry Andric return true;
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric } else {
6540b57cec5SDimitry Andric // The g_load_plugin_callback is registered in SBDebugger::Initialize() and
6550b57cec5SDimitry Andric // if the public API layer isn't available (code is linking against all of
6560b57cec5SDimitry Andric // the internal LLDB static libraries), then we can't load plugins
6570b57cec5SDimitry Andric error.SetErrorString("Public API layer is not available");
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric return false;
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric static FileSystem::EnumerateDirectoryResult
LoadPluginCallback(void * baton,llvm::sys::fs::file_type ft,llvm::StringRef path)6630b57cec5SDimitry Andric LoadPluginCallback(void *baton, llvm::sys::fs::file_type ft,
6640b57cec5SDimitry Andric llvm::StringRef path) {
6650b57cec5SDimitry Andric Status error;
6660b57cec5SDimitry Andric
667fe013be4SDimitry Andric static constexpr llvm::StringLiteral g_dylibext(".dylib");
668fe013be4SDimitry Andric static constexpr llvm::StringLiteral g_solibext(".so");
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andric if (!baton)
6710b57cec5SDimitry Andric return FileSystem::eEnumerateDirectoryResultQuit;
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric Debugger *debugger = (Debugger *)baton;
6740b57cec5SDimitry Andric
6750b57cec5SDimitry Andric namespace fs = llvm::sys::fs;
6760b57cec5SDimitry Andric // If we have a regular file, a symbolic link or unknown file type, try and
6770b57cec5SDimitry Andric // process the file. We must handle unknown as sometimes the directory
6780b57cec5SDimitry Andric // enumeration might be enumerating a file system that doesn't have correct
6790b57cec5SDimitry Andric // file type information.
6800b57cec5SDimitry Andric if (ft == fs::file_type::regular_file || ft == fs::file_type::symlink_file ||
6810b57cec5SDimitry Andric ft == fs::file_type::type_unknown) {
6820b57cec5SDimitry Andric FileSpec plugin_file_spec(path);
6830b57cec5SDimitry Andric FileSystem::Instance().Resolve(plugin_file_spec);
6840b57cec5SDimitry Andric
6850b57cec5SDimitry Andric if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
6860b57cec5SDimitry Andric plugin_file_spec.GetFileNameExtension() != g_solibext) {
6870b57cec5SDimitry Andric return FileSystem::eEnumerateDirectoryResultNext;
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric
6900b57cec5SDimitry Andric Status plugin_load_error;
6910b57cec5SDimitry Andric debugger->LoadPlugin(plugin_file_spec, plugin_load_error);
6920b57cec5SDimitry Andric
6930b57cec5SDimitry Andric return FileSystem::eEnumerateDirectoryResultNext;
6940b57cec5SDimitry Andric } else if (ft == fs::file_type::directory_file ||
6950b57cec5SDimitry Andric ft == fs::file_type::symlink_file ||
6960b57cec5SDimitry Andric ft == fs::file_type::type_unknown) {
6970b57cec5SDimitry Andric // Try and recurse into anything that a directory or symbolic link. We must
6980b57cec5SDimitry Andric // also do this for unknown as sometimes the directory enumeration might be
6990b57cec5SDimitry Andric // enumerating a file system that doesn't have correct file type
7000b57cec5SDimitry Andric // information.
7010b57cec5SDimitry Andric return FileSystem::eEnumerateDirectoryResultEnter;
7020b57cec5SDimitry Andric }
7030b57cec5SDimitry Andric
7040b57cec5SDimitry Andric return FileSystem::eEnumerateDirectoryResultNext;
7050b57cec5SDimitry Andric }
7060b57cec5SDimitry Andric
InstanceInitialize()7070b57cec5SDimitry Andric void Debugger::InstanceInitialize() {
7080b57cec5SDimitry Andric const bool find_directories = true;
7090b57cec5SDimitry Andric const bool find_files = true;
7100b57cec5SDimitry Andric const bool find_other = true;
7110b57cec5SDimitry Andric char dir_path[PATH_MAX];
7120b57cec5SDimitry Andric if (FileSpec dir_spec = HostInfo::GetSystemPluginDir()) {
7130b57cec5SDimitry Andric if (FileSystem::Instance().Exists(dir_spec) &&
7140b57cec5SDimitry Andric dir_spec.GetPath(dir_path, sizeof(dir_path))) {
7150b57cec5SDimitry Andric FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,
7160b57cec5SDimitry Andric find_files, find_other,
7170b57cec5SDimitry Andric LoadPluginCallback, this);
7180b57cec5SDimitry Andric }
7190b57cec5SDimitry Andric }
7200b57cec5SDimitry Andric
7210b57cec5SDimitry Andric if (FileSpec dir_spec = HostInfo::GetUserPluginDir()) {
7220b57cec5SDimitry Andric if (FileSystem::Instance().Exists(dir_spec) &&
7230b57cec5SDimitry Andric dir_spec.GetPath(dir_path, sizeof(dir_path))) {
7240b57cec5SDimitry Andric FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,
7250b57cec5SDimitry Andric find_files, find_other,
7260b57cec5SDimitry Andric LoadPluginCallback, this);
7270b57cec5SDimitry Andric }
7280b57cec5SDimitry Andric }
7290b57cec5SDimitry Andric
7300b57cec5SDimitry Andric PluginManager::DebuggerInitialize(*this);
7310b57cec5SDimitry Andric }
7320b57cec5SDimitry Andric
CreateInstance(lldb::LogOutputCallback log_callback,void * baton)7330b57cec5SDimitry Andric DebuggerSP Debugger::CreateInstance(lldb::LogOutputCallback log_callback,
7340b57cec5SDimitry Andric void *baton) {
7350b57cec5SDimitry Andric DebuggerSP debugger_sp(new Debugger(log_callback, baton));
7360b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
7370b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
7380b57cec5SDimitry Andric g_debugger_list_ptr->push_back(debugger_sp);
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric debugger_sp->InstanceInitialize();
7410b57cec5SDimitry Andric return debugger_sp;
7420b57cec5SDimitry Andric }
7430b57cec5SDimitry Andric
HandleDestroyCallback()744fe013be4SDimitry Andric void Debugger::HandleDestroyCallback() {
745fe013be4SDimitry Andric if (m_destroy_callback) {
746fe013be4SDimitry Andric m_destroy_callback(GetID(), m_destroy_callback_baton);
747fe013be4SDimitry Andric m_destroy_callback = nullptr;
748fe013be4SDimitry Andric }
749fe013be4SDimitry Andric }
750fe013be4SDimitry Andric
Destroy(DebuggerSP & debugger_sp)7510b57cec5SDimitry Andric void Debugger::Destroy(DebuggerSP &debugger_sp) {
7520b57cec5SDimitry Andric if (!debugger_sp)
7530b57cec5SDimitry Andric return;
7540b57cec5SDimitry Andric
755fe013be4SDimitry Andric debugger_sp->HandleDestroyCallback();
756fe6060f1SDimitry Andric CommandInterpreter &cmd_interpreter = debugger_sp->GetCommandInterpreter();
757fe6060f1SDimitry Andric
758fe6060f1SDimitry Andric if (cmd_interpreter.GetSaveSessionOnQuit()) {
759fe6060f1SDimitry Andric CommandReturnObject result(debugger_sp->GetUseColor());
760fe6060f1SDimitry Andric cmd_interpreter.SaveTranscript(result);
761fe6060f1SDimitry Andric if (result.Succeeded())
76281ad6265SDimitry Andric (*debugger_sp->GetAsyncOutputStream()) << result.GetOutputData() << '\n';
763fe6060f1SDimitry Andric else
76481ad6265SDimitry Andric (*debugger_sp->GetAsyncErrorStream()) << result.GetErrorData() << '\n';
765fe6060f1SDimitry Andric }
766fe6060f1SDimitry Andric
7670b57cec5SDimitry Andric debugger_sp->Clear();
7680b57cec5SDimitry Andric
7690b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
7700b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
7710b57cec5SDimitry Andric DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
7720b57cec5SDimitry Andric for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
7730b57cec5SDimitry Andric if ((*pos).get() == debugger_sp.get()) {
7740b57cec5SDimitry Andric g_debugger_list_ptr->erase(pos);
7750b57cec5SDimitry Andric return;
7760b57cec5SDimitry Andric }
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric }
7790b57cec5SDimitry Andric }
7800b57cec5SDimitry Andric
781fe013be4SDimitry Andric DebuggerSP
FindDebuggerWithInstanceName(llvm::StringRef instance_name)782fe013be4SDimitry Andric Debugger::FindDebuggerWithInstanceName(llvm::StringRef instance_name) {
783fe013be4SDimitry Andric if (!g_debugger_list_ptr || !g_debugger_list_mutex_ptr)
784fe013be4SDimitry Andric return DebuggerSP();
785fe013be4SDimitry Andric
7860b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
787fe013be4SDimitry Andric for (const DebuggerSP &debugger_sp : *g_debugger_list_ptr) {
788fe013be4SDimitry Andric if (!debugger_sp)
789fe013be4SDimitry Andric continue;
790fe013be4SDimitry Andric
791fe013be4SDimitry Andric if (llvm::StringRef(debugger_sp->GetInstanceName()) == instance_name)
7920b57cec5SDimitry Andric return debugger_sp;
7930b57cec5SDimitry Andric }
794fe013be4SDimitry Andric return DebuggerSP();
795fe013be4SDimitry Andric }
7960b57cec5SDimitry Andric
FindTargetWithProcessID(lldb::pid_t pid)7970b57cec5SDimitry Andric TargetSP Debugger::FindTargetWithProcessID(lldb::pid_t pid) {
7980b57cec5SDimitry Andric TargetSP target_sp;
7990b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
8000b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
8010b57cec5SDimitry Andric DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
8020b57cec5SDimitry Andric for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
8030b57cec5SDimitry Andric target_sp = (*pos)->GetTargetList().FindTargetWithProcessID(pid);
8040b57cec5SDimitry Andric if (target_sp)
8050b57cec5SDimitry Andric break;
8060b57cec5SDimitry Andric }
8070b57cec5SDimitry Andric }
8080b57cec5SDimitry Andric return target_sp;
8090b57cec5SDimitry Andric }
8100b57cec5SDimitry Andric
FindTargetWithProcess(Process * process)8110b57cec5SDimitry Andric TargetSP Debugger::FindTargetWithProcess(Process *process) {
8120b57cec5SDimitry Andric TargetSP target_sp;
8130b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
8140b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
8150b57cec5SDimitry Andric DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
8160b57cec5SDimitry Andric for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
8170b57cec5SDimitry Andric target_sp = (*pos)->GetTargetList().FindTargetWithProcess(process);
8180b57cec5SDimitry Andric if (target_sp)
8190b57cec5SDimitry Andric break;
8200b57cec5SDimitry Andric }
8210b57cec5SDimitry Andric }
8220b57cec5SDimitry Andric return target_sp;
8230b57cec5SDimitry Andric }
8240b57cec5SDimitry Andric
GetStaticBroadcasterClass()825fe6060f1SDimitry Andric ConstString Debugger::GetStaticBroadcasterClass() {
826fe6060f1SDimitry Andric static ConstString class_name("lldb.debugger");
827fe6060f1SDimitry Andric return class_name;
828fe6060f1SDimitry Andric }
829fe6060f1SDimitry Andric
Debugger(lldb::LogOutputCallback log_callback,void * baton)8300b57cec5SDimitry Andric Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton)
8310b57cec5SDimitry Andric : UserID(g_unique_id++),
8320b57cec5SDimitry Andric Properties(std::make_shared<OptionValueProperties>()),
8339dba64beSDimitry Andric m_input_file_sp(std::make_shared<NativeFile>(stdin, false)),
8349dba64beSDimitry Andric m_output_stream_sp(std::make_shared<StreamFile>(stdout, false)),
8359dba64beSDimitry Andric m_error_stream_sp(std::make_shared<StreamFile>(stderr, false)),
8360b57cec5SDimitry Andric m_input_recorder(nullptr),
8370b57cec5SDimitry Andric m_broadcaster_manager_sp(BroadcasterManager::MakeBroadcasterManager()),
8380b57cec5SDimitry Andric m_terminal_state(), m_target_list(*this), m_platform_list(),
8390b57cec5SDimitry Andric m_listener_sp(Listener::MakeListener("lldb.Debugger")),
8400b57cec5SDimitry Andric m_source_manager_up(), m_source_file_cache(),
8410b57cec5SDimitry Andric m_command_interpreter_up(
8429dba64beSDimitry Andric std::make_unique<CommandInterpreter>(*this, false)),
843fe013be4SDimitry Andric m_io_handler_stack(),
844fe013be4SDimitry Andric m_instance_name(llvm::formatv("debugger_{0}", GetID()).str()),
845fe013be4SDimitry Andric m_loaded_plugins(), m_event_handler_thread(), m_io_handler_thread(),
8460b57cec5SDimitry Andric m_sync_broadcaster(nullptr, "lldb.debugger.sync"),
847fe6060f1SDimitry Andric m_broadcaster(m_broadcaster_manager_sp,
848fe6060f1SDimitry Andric GetStaticBroadcasterClass().AsCString()),
8490b57cec5SDimitry Andric m_forward_listener_sp(), m_clear_once() {
850fe013be4SDimitry Andric // Initialize the debugger properties as early as possible as other parts of
851fe013be4SDimitry Andric // LLDB will start querying them during construction.
852fe013be4SDimitry Andric m_collection_sp->Initialize(g_debugger_properties);
853fe013be4SDimitry Andric m_collection_sp->AppendProperty(
854fe013be4SDimitry Andric "target", "Settings specify to debugging targets.", true,
855fe013be4SDimitry Andric Target::GetGlobalProperties().GetValueProperties());
856fe013be4SDimitry Andric m_collection_sp->AppendProperty(
857fe013be4SDimitry Andric "platform", "Platform settings.", true,
858fe013be4SDimitry Andric Platform::GetGlobalPlatformProperties().GetValueProperties());
859fe013be4SDimitry Andric m_collection_sp->AppendProperty(
860fe013be4SDimitry Andric "symbols", "Symbol lookup and cache settings.", true,
861fe013be4SDimitry Andric ModuleList::GetGlobalModuleListProperties().GetValueProperties());
862fe013be4SDimitry Andric if (m_command_interpreter_up) {
863fe013be4SDimitry Andric m_collection_sp->AppendProperty(
864fe013be4SDimitry Andric "interpreter",
865fe013be4SDimitry Andric "Settings specify to the debugger's command interpreter.", true,
866fe013be4SDimitry Andric m_command_interpreter_up->GetValueProperties());
867fe013be4SDimitry Andric }
8680b57cec5SDimitry Andric if (log_callback)
86981ad6265SDimitry Andric m_callback_handler_sp =
87081ad6265SDimitry Andric std::make_shared<CallbackLogHandler>(log_callback, baton);
8710b57cec5SDimitry Andric m_command_interpreter_up->Initialize();
8720b57cec5SDimitry Andric // Always add our default platform to the platform list
8730b57cec5SDimitry Andric PlatformSP default_platform_sp(Platform::GetHostPlatform());
8740b57cec5SDimitry Andric assert(default_platform_sp);
8750b57cec5SDimitry Andric m_platform_list.Append(default_platform_sp, true);
8760b57cec5SDimitry Andric
877e8d8bef9SDimitry Andric // Create the dummy target.
878e8d8bef9SDimitry Andric {
879e8d8bef9SDimitry Andric ArchSpec arch(Target::GetDefaultArchitecture());
880e8d8bef9SDimitry Andric if (!arch.IsValid())
881e8d8bef9SDimitry Andric arch = HostInfo::GetArchitecture();
882e8d8bef9SDimitry Andric assert(arch.IsValid() && "No valid default or host archspec");
883e8d8bef9SDimitry Andric const bool is_dummy_target = true;
884e8d8bef9SDimitry Andric m_dummy_target_sp.reset(
885e8d8bef9SDimitry Andric new Target(*this, arch, default_platform_sp, is_dummy_target));
886e8d8bef9SDimitry Andric }
8879dba64beSDimitry Andric assert(m_dummy_target_sp.get() && "Couldn't construct dummy target?");
8889dba64beSDimitry Andric
8890b57cec5SDimitry Andric OptionValueSInt64 *term_width =
8900b57cec5SDimitry Andric m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64(
891fe013be4SDimitry Andric ePropertyTerminalWidth);
8920b57cec5SDimitry Andric term_width->SetMinimumValue(10);
8930b57cec5SDimitry Andric term_width->SetMaximumValue(1024);
8940b57cec5SDimitry Andric
8950b57cec5SDimitry Andric // Turn off use-color if this is a dumb terminal.
8960b57cec5SDimitry Andric const char *term = getenv("TERM");
8970b57cec5SDimitry Andric if (term && !strcmp(term, "dumb"))
8980b57cec5SDimitry Andric SetUseColor(false);
8990b57cec5SDimitry Andric // Turn off use-color if we don't write to a terminal with color support.
9009dba64beSDimitry Andric if (!GetOutputFile().GetIsTerminalWithColors())
9010b57cec5SDimitry Andric SetUseColor(false);
9020b57cec5SDimitry Andric
903fe013be4SDimitry Andric if (Diagnostics::Enabled()) {
904fe013be4SDimitry Andric m_diagnostics_callback_id = Diagnostics::Instance().AddCallback(
905fe013be4SDimitry Andric [this](const FileSpec &dir) -> llvm::Error {
906fe013be4SDimitry Andric for (auto &entry : m_stream_handlers) {
907fe013be4SDimitry Andric llvm::StringRef log_path = entry.first();
908fe013be4SDimitry Andric llvm::StringRef file_name = llvm::sys::path::filename(log_path);
909fe013be4SDimitry Andric FileSpec destination = dir.CopyByAppendingPathComponent(file_name);
910fe013be4SDimitry Andric std::error_code ec =
911fe013be4SDimitry Andric llvm::sys::fs::copy_file(log_path, destination.GetPath());
912fe013be4SDimitry Andric if (ec)
913fe013be4SDimitry Andric return llvm::errorCodeToError(ec);
914fe013be4SDimitry Andric }
915fe013be4SDimitry Andric return llvm::Error::success();
916fe013be4SDimitry Andric });
917fe013be4SDimitry Andric }
918fe013be4SDimitry Andric
9190b57cec5SDimitry Andric #if defined(_WIN32) && defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
9200b57cec5SDimitry Andric // Enabling use of ANSI color codes because LLDB is using them to highlight
9210b57cec5SDimitry Andric // text.
9220b57cec5SDimitry Andric llvm::sys::Process::UseANSIEscapeCodes(true);
9230b57cec5SDimitry Andric #endif
9240b57cec5SDimitry Andric }
9250b57cec5SDimitry Andric
~Debugger()9260b57cec5SDimitry Andric Debugger::~Debugger() { Clear(); }
9270b57cec5SDimitry Andric
Clear()9280b57cec5SDimitry Andric void Debugger::Clear() {
9290b57cec5SDimitry Andric // Make sure we call this function only once. With the C++ global destructor
9300b57cec5SDimitry Andric // chain having a list of debuggers and with code that can be running on
9310b57cec5SDimitry Andric // other threads, we need to ensure this doesn't happen multiple times.
9320b57cec5SDimitry Andric //
9330b57cec5SDimitry Andric // The following functions call Debugger::Clear():
9340b57cec5SDimitry Andric // Debugger::~Debugger();
9350b57cec5SDimitry Andric // static void Debugger::Destroy(lldb::DebuggerSP &debugger_sp);
9360b57cec5SDimitry Andric // static void Debugger::Terminate();
9370b57cec5SDimitry Andric llvm::call_once(m_clear_once, [this]() {
9380b57cec5SDimitry Andric ClearIOHandlers();
9390b57cec5SDimitry Andric StopIOHandlerThread();
9400b57cec5SDimitry Andric StopEventHandlerThread();
9410b57cec5SDimitry Andric m_listener_sp->Clear();
942fe6060f1SDimitry Andric for (TargetSP target_sp : m_target_list.Targets()) {
9430b57cec5SDimitry Andric if (target_sp) {
944fe6060f1SDimitry Andric if (ProcessSP process_sp = target_sp->GetProcessSP())
945*c9157d92SDimitry Andric process_sp->Finalize(false /* not destructing */);
9460b57cec5SDimitry Andric target_sp->Destroy();
9470b57cec5SDimitry Andric }
9480b57cec5SDimitry Andric }
9490b57cec5SDimitry Andric m_broadcaster_manager_sp->Clear();
9500b57cec5SDimitry Andric
9510b57cec5SDimitry Andric // Close the input file _before_ we close the input read communications
9520b57cec5SDimitry Andric // class as it does NOT own the input file, our m_input_file does.
9530b57cec5SDimitry Andric m_terminal_state.Clear();
9549dba64beSDimitry Andric GetInputFile().Close();
9550b57cec5SDimitry Andric
9560b57cec5SDimitry Andric m_command_interpreter_up->Clear();
957fe013be4SDimitry Andric
958fe013be4SDimitry Andric if (Diagnostics::Enabled())
959fe013be4SDimitry Andric Diagnostics::Instance().RemoveCallback(m_diagnostics_callback_id);
9600b57cec5SDimitry Andric });
9610b57cec5SDimitry Andric }
9620b57cec5SDimitry Andric
GetAsyncExecution()9630b57cec5SDimitry Andric bool Debugger::GetAsyncExecution() {
9640b57cec5SDimitry Andric return !m_command_interpreter_up->GetSynchronous();
9650b57cec5SDimitry Andric }
9660b57cec5SDimitry Andric
SetAsyncExecution(bool async_execution)9670b57cec5SDimitry Andric void Debugger::SetAsyncExecution(bool async_execution) {
9680b57cec5SDimitry Andric m_command_interpreter_up->SetSynchronous(!async_execution);
9690b57cec5SDimitry Andric }
9700b57cec5SDimitry Andric
GetInputRecorder()9710b57cec5SDimitry Andric repro::DataRecorder *Debugger::GetInputRecorder() { return m_input_recorder; }
9720b57cec5SDimitry Andric
OpenPipe(int fds[2],std::size_t size)9734824e7fdSDimitry Andric static inline int OpenPipe(int fds[2], std::size_t size) {
9744824e7fdSDimitry Andric #ifdef _WIN32
9754824e7fdSDimitry Andric return _pipe(fds, size, O_BINARY);
9764824e7fdSDimitry Andric #else
9774824e7fdSDimitry Andric (void)size;
9784824e7fdSDimitry Andric return pipe(fds);
9794824e7fdSDimitry Andric #endif
9804824e7fdSDimitry Andric }
9814824e7fdSDimitry Andric
SetInputString(const char * data)9824824e7fdSDimitry Andric Status Debugger::SetInputString(const char *data) {
9834824e7fdSDimitry Andric Status result;
9844824e7fdSDimitry Andric enum PIPES { READ, WRITE }; // Indexes for the read and write fds
9854824e7fdSDimitry Andric int fds[2] = {-1, -1};
9864824e7fdSDimitry Andric
9874824e7fdSDimitry Andric if (data == nullptr) {
9884824e7fdSDimitry Andric result.SetErrorString("String data is null");
9894824e7fdSDimitry Andric return result;
9904824e7fdSDimitry Andric }
9914824e7fdSDimitry Andric
9924824e7fdSDimitry Andric size_t size = strlen(data);
9934824e7fdSDimitry Andric if (size == 0) {
9944824e7fdSDimitry Andric result.SetErrorString("String data is empty");
9954824e7fdSDimitry Andric return result;
9964824e7fdSDimitry Andric }
9974824e7fdSDimitry Andric
9984824e7fdSDimitry Andric if (OpenPipe(fds, size) != 0) {
9994824e7fdSDimitry Andric result.SetErrorString(
10004824e7fdSDimitry Andric "can't create pipe file descriptors for LLDB commands");
10014824e7fdSDimitry Andric return result;
10024824e7fdSDimitry Andric }
10034824e7fdSDimitry Andric
100481ad6265SDimitry Andric int r = write(fds[WRITE], data, size);
100581ad6265SDimitry Andric (void)r;
10064824e7fdSDimitry Andric // Close the write end of the pipe, so that the command interpreter will exit
10074824e7fdSDimitry Andric // when it consumes all the data.
10084824e7fdSDimitry Andric llvm::sys::Process::SafelyCloseFileDescriptor(fds[WRITE]);
10094824e7fdSDimitry Andric
10104824e7fdSDimitry Andric // Open the read file descriptor as a FILE * that we can return as an input
10114824e7fdSDimitry Andric // handle.
10124824e7fdSDimitry Andric FILE *commands_file = fdopen(fds[READ], "rb");
10134824e7fdSDimitry Andric if (commands_file == nullptr) {
10144824e7fdSDimitry Andric result.SetErrorStringWithFormat("fdopen(%i, \"rb\") failed (errno = %i) "
10154824e7fdSDimitry Andric "when trying to open LLDB commands pipe",
10164824e7fdSDimitry Andric fds[READ], errno);
10174824e7fdSDimitry Andric llvm::sys::Process::SafelyCloseFileDescriptor(fds[READ]);
10184824e7fdSDimitry Andric return result;
10194824e7fdSDimitry Andric }
10204824e7fdSDimitry Andric
1021bdd1243dSDimitry Andric SetInputFile((FileSP)std::make_shared<NativeFile>(commands_file, true));
1022bdd1243dSDimitry Andric return result;
10234824e7fdSDimitry Andric }
10244824e7fdSDimitry Andric
SetInputFile(FileSP file_sp)1025bdd1243dSDimitry Andric void Debugger::SetInputFile(FileSP file_sp) {
10269dba64beSDimitry Andric assert(file_sp && file_sp->IsValid());
1027e8d8bef9SDimitry Andric m_input_file_sp = std::move(file_sp);
10280b57cec5SDimitry Andric // Save away the terminal state if that is relevant, so that we can restore
10290b57cec5SDimitry Andric // it in RestoreInputState.
10300b57cec5SDimitry Andric SaveInputTerminalState();
10310b57cec5SDimitry Andric }
10320b57cec5SDimitry Andric
SetOutputFile(FileSP file_sp)10339dba64beSDimitry Andric void Debugger::SetOutputFile(FileSP file_sp) {
10349dba64beSDimitry Andric assert(file_sp && file_sp->IsValid());
10359dba64beSDimitry Andric m_output_stream_sp = std::make_shared<StreamFile>(file_sp);
10360b57cec5SDimitry Andric }
10370b57cec5SDimitry Andric
SetErrorFile(FileSP file_sp)10389dba64beSDimitry Andric void Debugger::SetErrorFile(FileSP file_sp) {
10399dba64beSDimitry Andric assert(file_sp && file_sp->IsValid());
10409dba64beSDimitry Andric m_error_stream_sp = std::make_shared<StreamFile>(file_sp);
10410b57cec5SDimitry Andric }
10420b57cec5SDimitry Andric
SaveInputTerminalState()10430b57cec5SDimitry Andric void Debugger::SaveInputTerminalState() {
10449dba64beSDimitry Andric int fd = GetInputFile().GetDescriptor();
10459dba64beSDimitry Andric if (fd != File::kInvalidDescriptor)
10469dba64beSDimitry Andric m_terminal_state.Save(fd, true);
10470b57cec5SDimitry Andric }
10480b57cec5SDimitry Andric
RestoreInputTerminalState()10490b57cec5SDimitry Andric void Debugger::RestoreInputTerminalState() { m_terminal_state.Restore(); }
10500b57cec5SDimitry Andric
GetSelectedExecutionContext()10510b57cec5SDimitry Andric ExecutionContext Debugger::GetSelectedExecutionContext() {
1052fe6060f1SDimitry Andric bool adopt_selected = true;
1053fe6060f1SDimitry Andric ExecutionContextRef exe_ctx_ref(GetSelectedTarget().get(), adopt_selected);
1054fe6060f1SDimitry Andric return ExecutionContext(exe_ctx_ref);
10550b57cec5SDimitry Andric }
10560b57cec5SDimitry Andric
DispatchInputInterrupt()10570b57cec5SDimitry Andric void Debugger::DispatchInputInterrupt() {
10585ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
10595ffd83dbSDimitry Andric IOHandlerSP reader_sp(m_io_handler_stack.Top());
10600b57cec5SDimitry Andric if (reader_sp)
10610b57cec5SDimitry Andric reader_sp->Interrupt();
10620b57cec5SDimitry Andric }
10630b57cec5SDimitry Andric
DispatchInputEndOfFile()10640b57cec5SDimitry Andric void Debugger::DispatchInputEndOfFile() {
10655ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
10665ffd83dbSDimitry Andric IOHandlerSP reader_sp(m_io_handler_stack.Top());
10670b57cec5SDimitry Andric if (reader_sp)
10680b57cec5SDimitry Andric reader_sp->GotEOF();
10690b57cec5SDimitry Andric }
10700b57cec5SDimitry Andric
ClearIOHandlers()10710b57cec5SDimitry Andric void Debugger::ClearIOHandlers() {
10720b57cec5SDimitry Andric // The bottom input reader should be the main debugger input reader. We do
10730b57cec5SDimitry Andric // not want to close that one here.
10745ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
10755ffd83dbSDimitry Andric while (m_io_handler_stack.GetSize() > 1) {
10765ffd83dbSDimitry Andric IOHandlerSP reader_sp(m_io_handler_stack.Top());
10770b57cec5SDimitry Andric if (reader_sp)
10780b57cec5SDimitry Andric PopIOHandler(reader_sp);
10790b57cec5SDimitry Andric }
10800b57cec5SDimitry Andric }
10810b57cec5SDimitry Andric
RunIOHandlers()10825ffd83dbSDimitry Andric void Debugger::RunIOHandlers() {
10835ffd83dbSDimitry Andric IOHandlerSP reader_sp = m_io_handler_stack.Top();
10840b57cec5SDimitry Andric while (true) {
10850b57cec5SDimitry Andric if (!reader_sp)
10860b57cec5SDimitry Andric break;
10870b57cec5SDimitry Andric
10880b57cec5SDimitry Andric reader_sp->Run();
10895ffd83dbSDimitry Andric {
10905ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(
10915ffd83dbSDimitry Andric m_io_handler_synchronous_mutex);
10920b57cec5SDimitry Andric
10930b57cec5SDimitry Andric // Remove all input readers that are done from the top of the stack
10940b57cec5SDimitry Andric while (true) {
10955ffd83dbSDimitry Andric IOHandlerSP top_reader_sp = m_io_handler_stack.Top();
10960b57cec5SDimitry Andric if (top_reader_sp && top_reader_sp->GetIsDone())
10970b57cec5SDimitry Andric PopIOHandler(top_reader_sp);
10980b57cec5SDimitry Andric else
10990b57cec5SDimitry Andric break;
11000b57cec5SDimitry Andric }
11015ffd83dbSDimitry Andric reader_sp = m_io_handler_stack.Top();
11025ffd83dbSDimitry Andric }
11030b57cec5SDimitry Andric }
11040b57cec5SDimitry Andric ClearIOHandlers();
11050b57cec5SDimitry Andric }
11060b57cec5SDimitry Andric
RunIOHandlerSync(const IOHandlerSP & reader_sp)11075ffd83dbSDimitry Andric void Debugger::RunIOHandlerSync(const IOHandlerSP &reader_sp) {
11085ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_io_handler_synchronous_mutex);
11090b57cec5SDimitry Andric
11100b57cec5SDimitry Andric PushIOHandler(reader_sp);
11110b57cec5SDimitry Andric IOHandlerSP top_reader_sp = reader_sp;
11125ffd83dbSDimitry Andric
11130b57cec5SDimitry Andric while (top_reader_sp) {
11145ffd83dbSDimitry Andric if (!top_reader_sp)
11155ffd83dbSDimitry Andric break;
11165ffd83dbSDimitry Andric
11170b57cec5SDimitry Andric top_reader_sp->Run();
11180b57cec5SDimitry Andric
11195ffd83dbSDimitry Andric // Don't unwind past the starting point.
11200b57cec5SDimitry Andric if (top_reader_sp.get() == reader_sp.get()) {
11210b57cec5SDimitry Andric if (PopIOHandler(reader_sp))
11220b57cec5SDimitry Andric break;
11230b57cec5SDimitry Andric }
11240b57cec5SDimitry Andric
11255ffd83dbSDimitry Andric // If we pushed new IO handlers, pop them if they're done or restart the
11265ffd83dbSDimitry Andric // loop to run them if they're not.
11270b57cec5SDimitry Andric while (true) {
11285ffd83dbSDimitry Andric top_reader_sp = m_io_handler_stack.Top();
11295ffd83dbSDimitry Andric if (top_reader_sp && top_reader_sp->GetIsDone()) {
11300b57cec5SDimitry Andric PopIOHandler(top_reader_sp);
11315ffd83dbSDimitry Andric // Don't unwind past the starting point.
11325ffd83dbSDimitry Andric if (top_reader_sp.get() == reader_sp.get())
11335ffd83dbSDimitry Andric return;
11345ffd83dbSDimitry Andric } else {
11350b57cec5SDimitry Andric break;
11360b57cec5SDimitry Andric }
11370b57cec5SDimitry Andric }
11380b57cec5SDimitry Andric }
11395ffd83dbSDimitry Andric }
11405ffd83dbSDimitry Andric
IsTopIOHandler(const lldb::IOHandlerSP & reader_sp)11415ffd83dbSDimitry Andric bool Debugger::IsTopIOHandler(const lldb::IOHandlerSP &reader_sp) {
11425ffd83dbSDimitry Andric return m_io_handler_stack.IsTop(reader_sp);
11435ffd83dbSDimitry Andric }
11445ffd83dbSDimitry Andric
CheckTopIOHandlerTypes(IOHandler::Type top_type,IOHandler::Type second_top_type)11455ffd83dbSDimitry Andric bool Debugger::CheckTopIOHandlerTypes(IOHandler::Type top_type,
11465ffd83dbSDimitry Andric IOHandler::Type second_top_type) {
11475ffd83dbSDimitry Andric return m_io_handler_stack.CheckTopIOHandlerTypes(top_type, second_top_type);
11485ffd83dbSDimitry Andric }
11495ffd83dbSDimitry Andric
PrintAsync(const char * s,size_t len,bool is_stdout)11505ffd83dbSDimitry Andric void Debugger::PrintAsync(const char *s, size_t len, bool is_stdout) {
115181ad6265SDimitry Andric bool printed = m_io_handler_stack.PrintAsync(s, len, is_stdout);
115281ad6265SDimitry Andric if (!printed) {
115381ad6265SDimitry Andric lldb::StreamFileSP stream =
115481ad6265SDimitry Andric is_stdout ? m_output_stream_sp : m_error_stream_sp;
115581ad6265SDimitry Andric stream->Write(s, len);
115681ad6265SDimitry Andric }
11575ffd83dbSDimitry Andric }
11585ffd83dbSDimitry Andric
GetTopIOHandlerControlSequence(char ch)1159fe013be4SDimitry Andric llvm::StringRef Debugger::GetTopIOHandlerControlSequence(char ch) {
11605ffd83dbSDimitry Andric return m_io_handler_stack.GetTopIOHandlerControlSequence(ch);
11615ffd83dbSDimitry Andric }
11625ffd83dbSDimitry Andric
GetIOHandlerCommandPrefix()11635ffd83dbSDimitry Andric const char *Debugger::GetIOHandlerCommandPrefix() {
11645ffd83dbSDimitry Andric return m_io_handler_stack.GetTopIOHandlerCommandPrefix();
11655ffd83dbSDimitry Andric }
11665ffd83dbSDimitry Andric
GetIOHandlerHelpPrologue()11675ffd83dbSDimitry Andric const char *Debugger::GetIOHandlerHelpPrologue() {
11685ffd83dbSDimitry Andric return m_io_handler_stack.GetTopIOHandlerHelpPrologue();
11695ffd83dbSDimitry Andric }
11705ffd83dbSDimitry Andric
RemoveIOHandler(const IOHandlerSP & reader_sp)11715ffd83dbSDimitry Andric bool Debugger::RemoveIOHandler(const IOHandlerSP &reader_sp) {
11725ffd83dbSDimitry Andric return PopIOHandler(reader_sp);
11735ffd83dbSDimitry Andric }
11745ffd83dbSDimitry Andric
RunIOHandlerAsync(const IOHandlerSP & reader_sp,bool cancel_top_handler)11755ffd83dbSDimitry Andric void Debugger::RunIOHandlerAsync(const IOHandlerSP &reader_sp,
11765ffd83dbSDimitry Andric bool cancel_top_handler) {
11775ffd83dbSDimitry Andric PushIOHandler(reader_sp, cancel_top_handler);
11785ffd83dbSDimitry Andric }
11790b57cec5SDimitry Andric
AdoptTopIOHandlerFilesIfInvalid(FileSP & in,StreamFileSP & out,StreamFileSP & err)11809dba64beSDimitry Andric void Debugger::AdoptTopIOHandlerFilesIfInvalid(FileSP &in, StreamFileSP &out,
11810b57cec5SDimitry Andric StreamFileSP &err) {
11820b57cec5SDimitry Andric // Before an IOHandler runs, it must have in/out/err streams. This function
11830b57cec5SDimitry Andric // is called when one ore more of the streams are nullptr. We use the top
11840b57cec5SDimitry Andric // input reader's in/out/err streams, or fall back to the debugger file
11850b57cec5SDimitry Andric // handles, or we fall back onto stdin/stdout/stderr as a last resort.
11860b57cec5SDimitry Andric
11875ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
11885ffd83dbSDimitry Andric IOHandlerSP top_reader_sp(m_io_handler_stack.Top());
11890b57cec5SDimitry Andric // If no STDIN has been set, then set it appropriately
11909dba64beSDimitry Andric if (!in || !in->IsValid()) {
11910b57cec5SDimitry Andric if (top_reader_sp)
11929dba64beSDimitry Andric in = top_reader_sp->GetInputFileSP();
11930b57cec5SDimitry Andric else
11949dba64beSDimitry Andric in = GetInputFileSP();
11950b57cec5SDimitry Andric // If there is nothing, use stdin
11960b57cec5SDimitry Andric if (!in)
11979dba64beSDimitry Andric in = std::make_shared<NativeFile>(stdin, false);
11980b57cec5SDimitry Andric }
11990b57cec5SDimitry Andric // If no STDOUT has been set, then set it appropriately
12009dba64beSDimitry Andric if (!out || !out->GetFile().IsValid()) {
12010b57cec5SDimitry Andric if (top_reader_sp)
12029dba64beSDimitry Andric out = top_reader_sp->GetOutputStreamFileSP();
12030b57cec5SDimitry Andric else
12049dba64beSDimitry Andric out = GetOutputStreamSP();
12050b57cec5SDimitry Andric // If there is nothing, use stdout
12060b57cec5SDimitry Andric if (!out)
12070b57cec5SDimitry Andric out = std::make_shared<StreamFile>(stdout, false);
12080b57cec5SDimitry Andric }
12090b57cec5SDimitry Andric // If no STDERR has been set, then set it appropriately
12109dba64beSDimitry Andric if (!err || !err->GetFile().IsValid()) {
12110b57cec5SDimitry Andric if (top_reader_sp)
12129dba64beSDimitry Andric err = top_reader_sp->GetErrorStreamFileSP();
12130b57cec5SDimitry Andric else
12149dba64beSDimitry Andric err = GetErrorStreamSP();
12150b57cec5SDimitry Andric // If there is nothing, use stderr
12160b57cec5SDimitry Andric if (!err)
12179dba64beSDimitry Andric err = std::make_shared<StreamFile>(stderr, false);
12180b57cec5SDimitry Andric }
12190b57cec5SDimitry Andric }
12200b57cec5SDimitry Andric
PushIOHandler(const IOHandlerSP & reader_sp,bool cancel_top_handler)12210b57cec5SDimitry Andric void Debugger::PushIOHandler(const IOHandlerSP &reader_sp,
12220b57cec5SDimitry Andric bool cancel_top_handler) {
12230b57cec5SDimitry Andric if (!reader_sp)
12240b57cec5SDimitry Andric return;
12250b57cec5SDimitry Andric
12265ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
12270b57cec5SDimitry Andric
12280b57cec5SDimitry Andric // Get the current top input reader...
12295ffd83dbSDimitry Andric IOHandlerSP top_reader_sp(m_io_handler_stack.Top());
12300b57cec5SDimitry Andric
12310b57cec5SDimitry Andric // Don't push the same IO handler twice...
12320b57cec5SDimitry Andric if (reader_sp == top_reader_sp)
12330b57cec5SDimitry Andric return;
12340b57cec5SDimitry Andric
12350b57cec5SDimitry Andric // Push our new input reader
12365ffd83dbSDimitry Andric m_io_handler_stack.Push(reader_sp);
12370b57cec5SDimitry Andric reader_sp->Activate();
12380b57cec5SDimitry Andric
12390b57cec5SDimitry Andric // Interrupt the top input reader to it will exit its Run() function and let
12400b57cec5SDimitry Andric // this new input reader take over
12410b57cec5SDimitry Andric if (top_reader_sp) {
12420b57cec5SDimitry Andric top_reader_sp->Deactivate();
12430b57cec5SDimitry Andric if (cancel_top_handler)
12440b57cec5SDimitry Andric top_reader_sp->Cancel();
12450b57cec5SDimitry Andric }
12460b57cec5SDimitry Andric }
12470b57cec5SDimitry Andric
PopIOHandler(const IOHandlerSP & pop_reader_sp)12480b57cec5SDimitry Andric bool Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp) {
12490b57cec5SDimitry Andric if (!pop_reader_sp)
12500b57cec5SDimitry Andric return false;
12510b57cec5SDimitry Andric
12525ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_io_handler_stack.GetMutex());
12530b57cec5SDimitry Andric
12540b57cec5SDimitry Andric // The reader on the stop of the stack is done, so let the next read on the
12550b57cec5SDimitry Andric // stack refresh its prompt and if there is one...
12565ffd83dbSDimitry Andric if (m_io_handler_stack.IsEmpty())
12570b57cec5SDimitry Andric return false;
12580b57cec5SDimitry Andric
12595ffd83dbSDimitry Andric IOHandlerSP reader_sp(m_io_handler_stack.Top());
12600b57cec5SDimitry Andric
12610b57cec5SDimitry Andric if (pop_reader_sp != reader_sp)
12620b57cec5SDimitry Andric return false;
12630b57cec5SDimitry Andric
12640b57cec5SDimitry Andric reader_sp->Deactivate();
12650b57cec5SDimitry Andric reader_sp->Cancel();
12665ffd83dbSDimitry Andric m_io_handler_stack.Pop();
12670b57cec5SDimitry Andric
12685ffd83dbSDimitry Andric reader_sp = m_io_handler_stack.Top();
12690b57cec5SDimitry Andric if (reader_sp)
12700b57cec5SDimitry Andric reader_sp->Activate();
12710b57cec5SDimitry Andric
12720b57cec5SDimitry Andric return true;
12730b57cec5SDimitry Andric }
12740b57cec5SDimitry Andric
GetAsyncOutputStream()12750b57cec5SDimitry Andric StreamSP Debugger::GetAsyncOutputStream() {
127681ad6265SDimitry Andric return std::make_shared<StreamAsynchronousIO>(*this, true, GetUseColor());
12770b57cec5SDimitry Andric }
12780b57cec5SDimitry Andric
GetAsyncErrorStream()12790b57cec5SDimitry Andric StreamSP Debugger::GetAsyncErrorStream() {
128081ad6265SDimitry Andric return std::make_shared<StreamAsynchronousIO>(*this, false, GetUseColor());
12810b57cec5SDimitry Andric }
12820b57cec5SDimitry Andric
RequestInterrupt()1283fe013be4SDimitry Andric void Debugger::RequestInterrupt() {
1284fe013be4SDimitry Andric std::lock_guard<std::mutex> guard(m_interrupt_mutex);
1285fe013be4SDimitry Andric m_interrupt_requested++;
1286fe013be4SDimitry Andric }
1287fe013be4SDimitry Andric
CancelInterruptRequest()1288fe013be4SDimitry Andric void Debugger::CancelInterruptRequest() {
1289fe013be4SDimitry Andric std::lock_guard<std::mutex> guard(m_interrupt_mutex);
1290fe013be4SDimitry Andric if (m_interrupt_requested > 0)
1291fe013be4SDimitry Andric m_interrupt_requested--;
1292fe013be4SDimitry Andric }
1293fe013be4SDimitry Andric
InterruptRequested()1294fe013be4SDimitry Andric bool Debugger::InterruptRequested() {
1295fe013be4SDimitry Andric // This is the one we should call internally. This will return true either
1296fe013be4SDimitry Andric // if there's a debugger interrupt and we aren't on the IOHandler thread,
1297fe013be4SDimitry Andric // or if we are on the IOHandler thread and there's a CommandInterpreter
1298fe013be4SDimitry Andric // interrupt.
1299fe013be4SDimitry Andric if (!IsIOHandlerThreadCurrentThread()) {
1300fe013be4SDimitry Andric std::lock_guard<std::mutex> guard(m_interrupt_mutex);
1301fe013be4SDimitry Andric return m_interrupt_requested != 0;
1302fe013be4SDimitry Andric }
1303fe013be4SDimitry Andric return GetCommandInterpreter().WasInterrupted();
1304fe013be4SDimitry Andric }
1305fe013be4SDimitry Andric
InterruptionReport(std::string function_name,const llvm::formatv_object_base & payload)1306*c9157d92SDimitry Andric Debugger::InterruptionReport::InterruptionReport(
1307*c9157d92SDimitry Andric std::string function_name, const llvm::formatv_object_base &payload)
1308*c9157d92SDimitry Andric : m_function_name(std::move(function_name)),
1309fe013be4SDimitry Andric m_interrupt_time(std::chrono::system_clock::now()),
1310fe013be4SDimitry Andric m_thread_id(llvm::get_threadid()) {
1311fe013be4SDimitry Andric llvm::raw_string_ostream desc(m_description);
1312fe013be4SDimitry Andric desc << payload << "\n";
1313fe013be4SDimitry Andric }
1314fe013be4SDimitry Andric
ReportInterruption(const InterruptionReport & report)1315fe013be4SDimitry Andric void Debugger::ReportInterruption(const InterruptionReport &report) {
1316fe013be4SDimitry Andric // For now, just log the description:
1317fe013be4SDimitry Andric Log *log = GetLog(LLDBLog::Host);
1318fe013be4SDimitry Andric LLDB_LOG(log, "Interruption: {0}", report.m_description);
1319fe013be4SDimitry Andric }
1320fe013be4SDimitry Andric
DebuggersRequestingInterruption()1321fe013be4SDimitry Andric Debugger::DebuggerList Debugger::DebuggersRequestingInterruption() {
1322fe013be4SDimitry Andric DebuggerList result;
1323fe013be4SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
1324fe013be4SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1325fe013be4SDimitry Andric for (auto debugger_sp : *g_debugger_list_ptr) {
1326fe013be4SDimitry Andric if (debugger_sp->InterruptRequested())
1327fe013be4SDimitry Andric result.push_back(debugger_sp);
1328fe013be4SDimitry Andric }
1329fe013be4SDimitry Andric }
1330fe013be4SDimitry Andric return result;
1331fe013be4SDimitry Andric }
1332fe013be4SDimitry Andric
GetNumDebuggers()13330b57cec5SDimitry Andric size_t Debugger::GetNumDebuggers() {
13340b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
13350b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
13360b57cec5SDimitry Andric return g_debugger_list_ptr->size();
13370b57cec5SDimitry Andric }
13380b57cec5SDimitry Andric return 0;
13390b57cec5SDimitry Andric }
13400b57cec5SDimitry Andric
GetDebuggerAtIndex(size_t index)13410b57cec5SDimitry Andric lldb::DebuggerSP Debugger::GetDebuggerAtIndex(size_t index) {
13420b57cec5SDimitry Andric DebuggerSP debugger_sp;
13430b57cec5SDimitry Andric
13440b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
13450b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
13460b57cec5SDimitry Andric if (index < g_debugger_list_ptr->size())
13470b57cec5SDimitry Andric debugger_sp = g_debugger_list_ptr->at(index);
13480b57cec5SDimitry Andric }
13490b57cec5SDimitry Andric
13500b57cec5SDimitry Andric return debugger_sp;
13510b57cec5SDimitry Andric }
13520b57cec5SDimitry Andric
FindDebuggerWithID(lldb::user_id_t id)13530b57cec5SDimitry Andric DebuggerSP Debugger::FindDebuggerWithID(lldb::user_id_t id) {
13540b57cec5SDimitry Andric DebuggerSP debugger_sp;
13550b57cec5SDimitry Andric
13560b57cec5SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
13570b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
13580b57cec5SDimitry Andric DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
13590b57cec5SDimitry Andric for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) {
13600b57cec5SDimitry Andric if ((*pos)->GetID() == id) {
13610b57cec5SDimitry Andric debugger_sp = *pos;
13620b57cec5SDimitry Andric break;
13630b57cec5SDimitry Andric }
13640b57cec5SDimitry Andric }
13650b57cec5SDimitry Andric }
13660b57cec5SDimitry Andric return debugger_sp;
13670b57cec5SDimitry Andric }
13680b57cec5SDimitry Andric
FormatDisassemblerAddress(const FormatEntity::Entry * format,const SymbolContext * sc,const SymbolContext * prev_sc,const ExecutionContext * exe_ctx,const Address * addr,Stream & s)13690b57cec5SDimitry Andric bool Debugger::FormatDisassemblerAddress(const FormatEntity::Entry *format,
13700b57cec5SDimitry Andric const SymbolContext *sc,
13710b57cec5SDimitry Andric const SymbolContext *prev_sc,
13720b57cec5SDimitry Andric const ExecutionContext *exe_ctx,
13730b57cec5SDimitry Andric const Address *addr, Stream &s) {
13740b57cec5SDimitry Andric FormatEntity::Entry format_entry;
13750b57cec5SDimitry Andric
13760b57cec5SDimitry Andric if (format == nullptr) {
13770b57cec5SDimitry Andric if (exe_ctx != nullptr && exe_ctx->HasTargetScope())
13780b57cec5SDimitry Andric format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
13790b57cec5SDimitry Andric if (format == nullptr) {
13800b57cec5SDimitry Andric FormatEntity::Parse("${addr}: ", format_entry);
13810b57cec5SDimitry Andric format = &format_entry;
13820b57cec5SDimitry Andric }
13830b57cec5SDimitry Andric }
13840b57cec5SDimitry Andric bool function_changed = false;
13850b57cec5SDimitry Andric bool initial_function = false;
13860b57cec5SDimitry Andric if (prev_sc && (prev_sc->function || prev_sc->symbol)) {
13870b57cec5SDimitry Andric if (sc && (sc->function || sc->symbol)) {
13880b57cec5SDimitry Andric if (prev_sc->symbol && sc->symbol) {
13890b57cec5SDimitry Andric if (!sc->symbol->Compare(prev_sc->symbol->GetName(),
13900b57cec5SDimitry Andric prev_sc->symbol->GetType())) {
13910b57cec5SDimitry Andric function_changed = true;
13920b57cec5SDimitry Andric }
13930b57cec5SDimitry Andric } else if (prev_sc->function && sc->function) {
13940b57cec5SDimitry Andric if (prev_sc->function->GetMangled() != sc->function->GetMangled()) {
13950b57cec5SDimitry Andric function_changed = true;
13960b57cec5SDimitry Andric }
13970b57cec5SDimitry Andric }
13980b57cec5SDimitry Andric }
13990b57cec5SDimitry Andric }
14000b57cec5SDimitry Andric // The first context on a list of instructions will have a prev_sc that has
14010b57cec5SDimitry Andric // no Function or Symbol -- if SymbolContext had an IsValid() method, it
14020b57cec5SDimitry Andric // would return false. But we do get a prev_sc pointer.
14030b57cec5SDimitry Andric if ((sc && (sc->function || sc->symbol)) && prev_sc &&
14040b57cec5SDimitry Andric (prev_sc->function == nullptr && prev_sc->symbol == nullptr)) {
14050b57cec5SDimitry Andric initial_function = true;
14060b57cec5SDimitry Andric }
14070b57cec5SDimitry Andric return FormatEntity::Format(*format, s, sc, exe_ctx, addr, nullptr,
14080b57cec5SDimitry Andric function_changed, initial_function);
14090b57cec5SDimitry Andric }
14100b57cec5SDimitry Andric
AssertCallback(llvm::StringRef message,llvm::StringRef backtrace,llvm::StringRef prompt)1411fe013be4SDimitry Andric void Debugger::AssertCallback(llvm::StringRef message,
1412fe013be4SDimitry Andric llvm::StringRef backtrace,
1413fe013be4SDimitry Andric llvm::StringRef prompt) {
1414fe013be4SDimitry Andric Debugger::ReportError(
1415fe013be4SDimitry Andric llvm::formatv("{0}\n{1}{2}", message, backtrace, prompt).str());
1416fe013be4SDimitry Andric }
1417fe013be4SDimitry Andric
SetLoggingCallback(lldb::LogOutputCallback log_callback,void * baton)14180b57cec5SDimitry Andric void Debugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
14190b57cec5SDimitry Andric void *baton) {
14200b57cec5SDimitry Andric // For simplicity's sake, I am not going to deal with how to close down any
14210b57cec5SDimitry Andric // open logging streams, I just redirect everything from here on out to the
14220b57cec5SDimitry Andric // callback.
142381ad6265SDimitry Andric m_callback_handler_sp =
142481ad6265SDimitry Andric std::make_shared<CallbackLogHandler>(log_callback, baton);
1425fe6060f1SDimitry Andric }
1426fe6060f1SDimitry Andric
SetDestroyCallback(lldb_private::DebuggerDestroyCallback destroy_callback,void * baton)1427fe013be4SDimitry Andric void Debugger::SetDestroyCallback(
1428fe013be4SDimitry Andric lldb_private::DebuggerDestroyCallback destroy_callback, void *baton) {
1429fe013be4SDimitry Andric m_destroy_callback = destroy_callback;
1430fe013be4SDimitry Andric m_destroy_callback_baton = baton;
1431fe013be4SDimitry Andric }
1432fe013be4SDimitry Andric
PrivateReportProgress(Debugger & debugger,uint64_t progress_id,std::string title,std::string details,uint64_t completed,uint64_t total,bool is_debugger_specific)1433fe6060f1SDimitry Andric static void PrivateReportProgress(Debugger &debugger, uint64_t progress_id,
1434fe013be4SDimitry Andric std::string title, std::string details,
1435fe6060f1SDimitry Andric uint64_t completed, uint64_t total,
1436fe6060f1SDimitry Andric bool is_debugger_specific) {
1437fe6060f1SDimitry Andric // Only deliver progress events if we have any progress listeners.
1438fe6060f1SDimitry Andric const uint32_t event_type = Debugger::eBroadcastBitProgress;
1439fe6060f1SDimitry Andric if (!debugger.GetBroadcaster().EventTypeHasListeners(event_type))
1440fe6060f1SDimitry Andric return;
144181ad6265SDimitry Andric EventSP event_sp(new Event(
1442fe013be4SDimitry Andric event_type,
1443fe013be4SDimitry Andric new ProgressEventData(progress_id, std::move(title), std::move(details),
1444fe013be4SDimitry Andric completed, total, is_debugger_specific)));
1445fe6060f1SDimitry Andric debugger.GetBroadcaster().BroadcastEvent(event_sp);
1446fe6060f1SDimitry Andric }
1447fe6060f1SDimitry Andric
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)1448fe013be4SDimitry Andric void Debugger::ReportProgress(uint64_t progress_id, std::string title,
1449fe013be4SDimitry Andric std::string details, uint64_t completed,
1450fe013be4SDimitry Andric uint64_t total,
1451bdd1243dSDimitry Andric std::optional<lldb::user_id_t> debugger_id) {
1452fe6060f1SDimitry Andric // Check if this progress is for a specific debugger.
145381ad6265SDimitry Andric if (debugger_id) {
1454fe6060f1SDimitry Andric // It is debugger specific, grab it and deliver the event if the debugger
1455fe6060f1SDimitry Andric // still exists.
1456fe6060f1SDimitry Andric DebuggerSP debugger_sp = FindDebuggerWithID(*debugger_id);
1457fe6060f1SDimitry Andric if (debugger_sp)
1458fe013be4SDimitry Andric PrivateReportProgress(*debugger_sp, progress_id, std::move(title),
1459fe013be4SDimitry Andric std::move(details), completed, total,
1460fe013be4SDimitry Andric /*is_debugger_specific*/ true);
1461fe6060f1SDimitry Andric return;
1462fe6060f1SDimitry Andric }
1463fe6060f1SDimitry Andric // The progress event is not debugger specific, iterate over all debuggers
1464fe6060f1SDimitry Andric // and deliver a progress event to each one.
1465fe6060f1SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
1466fe6060f1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1467fe6060f1SDimitry Andric DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
1468fe6060f1SDimitry Andric for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos)
1469fe013be4SDimitry Andric PrivateReportProgress(*(*pos), progress_id, title, details, completed,
1470fe013be4SDimitry Andric total, /*is_debugger_specific*/ false);
1471fe6060f1SDimitry Andric }
1472fe6060f1SDimitry Andric }
1473fe6060f1SDimitry Andric
PrivateReportDiagnostic(Debugger & debugger,DiagnosticEventData::Type type,std::string message,bool debugger_specific)147481ad6265SDimitry Andric static void PrivateReportDiagnostic(Debugger &debugger,
147581ad6265SDimitry Andric DiagnosticEventData::Type type,
147681ad6265SDimitry Andric std::string message,
147781ad6265SDimitry Andric bool debugger_specific) {
147881ad6265SDimitry Andric uint32_t event_type = 0;
147981ad6265SDimitry Andric switch (type) {
1480bdd1243dSDimitry Andric case DiagnosticEventData::Type::Info:
1481bdd1243dSDimitry Andric assert(false && "DiagnosticEventData::Type::Info should not be broadcast");
1482bdd1243dSDimitry Andric return;
148381ad6265SDimitry Andric case DiagnosticEventData::Type::Warning:
148481ad6265SDimitry Andric event_type = Debugger::eBroadcastBitWarning;
148581ad6265SDimitry Andric break;
148681ad6265SDimitry Andric case DiagnosticEventData::Type::Error:
148781ad6265SDimitry Andric event_type = Debugger::eBroadcastBitError;
148881ad6265SDimitry Andric break;
148981ad6265SDimitry Andric }
149081ad6265SDimitry Andric
149181ad6265SDimitry Andric Broadcaster &broadcaster = debugger.GetBroadcaster();
149281ad6265SDimitry Andric if (!broadcaster.EventTypeHasListeners(event_type)) {
149381ad6265SDimitry Andric // Diagnostics are too important to drop. If nobody is listening, print the
149481ad6265SDimitry Andric // diagnostic directly to the debugger's error stream.
149581ad6265SDimitry Andric DiagnosticEventData event_data(type, std::move(message), debugger_specific);
149681ad6265SDimitry Andric StreamSP stream = debugger.GetAsyncErrorStream();
149781ad6265SDimitry Andric event_data.Dump(stream.get());
149881ad6265SDimitry Andric return;
149981ad6265SDimitry Andric }
150081ad6265SDimitry Andric EventSP event_sp = std::make_shared<Event>(
150181ad6265SDimitry Andric event_type,
150281ad6265SDimitry Andric new DiagnosticEventData(type, std::move(message), debugger_specific));
150381ad6265SDimitry Andric broadcaster.BroadcastEvent(event_sp);
150481ad6265SDimitry Andric }
150581ad6265SDimitry Andric
ReportDiagnosticImpl(DiagnosticEventData::Type type,std::string message,std::optional<lldb::user_id_t> debugger_id,std::once_flag * once)150681ad6265SDimitry Andric void Debugger::ReportDiagnosticImpl(DiagnosticEventData::Type type,
150781ad6265SDimitry Andric std::string message,
1508bdd1243dSDimitry Andric std::optional<lldb::user_id_t> debugger_id,
150981ad6265SDimitry Andric std::once_flag *once) {
151081ad6265SDimitry Andric auto ReportDiagnosticLambda = [&]() {
1511bdd1243dSDimitry Andric // The diagnostic subsystem is optional but we still want to broadcast
1512bdd1243dSDimitry Andric // events when it's disabled.
1513bdd1243dSDimitry Andric if (Diagnostics::Enabled())
1514bdd1243dSDimitry Andric Diagnostics::Instance().Report(message);
1515bdd1243dSDimitry Andric
1516bdd1243dSDimitry Andric // We don't broadcast info events.
1517bdd1243dSDimitry Andric if (type == DiagnosticEventData::Type::Info)
1518bdd1243dSDimitry Andric return;
1519bdd1243dSDimitry Andric
1520bdd1243dSDimitry Andric // Check if this diagnostic is for a specific debugger.
152181ad6265SDimitry Andric if (debugger_id) {
152281ad6265SDimitry Andric // It is debugger specific, grab it and deliver the event if the debugger
152381ad6265SDimitry Andric // still exists.
152481ad6265SDimitry Andric DebuggerSP debugger_sp = FindDebuggerWithID(*debugger_id);
152581ad6265SDimitry Andric if (debugger_sp)
152681ad6265SDimitry Andric PrivateReportDiagnostic(*debugger_sp, type, std::move(message), true);
152781ad6265SDimitry Andric return;
152881ad6265SDimitry Andric }
1529bdd1243dSDimitry Andric // The diagnostic event is not debugger specific, iterate over all debuggers
1530bdd1243dSDimitry Andric // and deliver a diagnostic event to each one.
153181ad6265SDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
153281ad6265SDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
153381ad6265SDimitry Andric for (const auto &debugger : *g_debugger_list_ptr)
153481ad6265SDimitry Andric PrivateReportDiagnostic(*debugger, type, message, false);
153581ad6265SDimitry Andric }
153681ad6265SDimitry Andric };
153781ad6265SDimitry Andric
153881ad6265SDimitry Andric if (once)
153981ad6265SDimitry Andric std::call_once(*once, ReportDiagnosticLambda);
154081ad6265SDimitry Andric else
154181ad6265SDimitry Andric ReportDiagnosticLambda();
154281ad6265SDimitry Andric }
154381ad6265SDimitry Andric
ReportWarning(std::string message,std::optional<lldb::user_id_t> debugger_id,std::once_flag * once)154481ad6265SDimitry Andric void Debugger::ReportWarning(std::string message,
1545bdd1243dSDimitry Andric std::optional<lldb::user_id_t> debugger_id,
154681ad6265SDimitry Andric std::once_flag *once) {
154781ad6265SDimitry Andric ReportDiagnosticImpl(DiagnosticEventData::Type::Warning, std::move(message),
154881ad6265SDimitry Andric debugger_id, once);
154981ad6265SDimitry Andric }
155081ad6265SDimitry Andric
ReportError(std::string message,std::optional<lldb::user_id_t> debugger_id,std::once_flag * once)155181ad6265SDimitry Andric void Debugger::ReportError(std::string message,
1552bdd1243dSDimitry Andric std::optional<lldb::user_id_t> debugger_id,
155381ad6265SDimitry Andric std::once_flag *once) {
155481ad6265SDimitry Andric ReportDiagnosticImpl(DiagnosticEventData::Type::Error, std::move(message),
155581ad6265SDimitry Andric debugger_id, once);
155681ad6265SDimitry Andric }
155781ad6265SDimitry Andric
ReportInfo(std::string message,std::optional<lldb::user_id_t> debugger_id,std::once_flag * once)1558bdd1243dSDimitry Andric void Debugger::ReportInfo(std::string message,
1559bdd1243dSDimitry Andric std::optional<lldb::user_id_t> debugger_id,
1560bdd1243dSDimitry Andric std::once_flag *once) {
1561bdd1243dSDimitry Andric ReportDiagnosticImpl(DiagnosticEventData::Type::Info, std::move(message),
1562bdd1243dSDimitry Andric debugger_id, once);
1563bdd1243dSDimitry Andric }
1564bdd1243dSDimitry Andric
ReportSymbolChange(const ModuleSpec & module_spec)1565bdd1243dSDimitry Andric void Debugger::ReportSymbolChange(const ModuleSpec &module_spec) {
1566bdd1243dSDimitry Andric if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) {
1567bdd1243dSDimitry Andric std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1568bdd1243dSDimitry Andric for (DebuggerSP debugger_sp : *g_debugger_list_ptr) {
1569bdd1243dSDimitry Andric EventSP event_sp = std::make_shared<Event>(
1570bdd1243dSDimitry Andric Debugger::eBroadcastSymbolChange,
1571bdd1243dSDimitry Andric new SymbolChangeEventData(debugger_sp, module_spec));
1572bdd1243dSDimitry Andric debugger_sp->GetBroadcaster().BroadcastEvent(event_sp);
1573bdd1243dSDimitry Andric }
1574bdd1243dSDimitry Andric }
1575bdd1243dSDimitry Andric }
1576bdd1243dSDimitry Andric
157781ad6265SDimitry Andric static std::shared_ptr<LogHandler>
CreateLogHandler(LogHandlerKind log_handler_kind,int fd,bool should_close,size_t buffer_size)157881ad6265SDimitry Andric CreateLogHandler(LogHandlerKind log_handler_kind, int fd, bool should_close,
157981ad6265SDimitry Andric size_t buffer_size) {
158081ad6265SDimitry Andric switch (log_handler_kind) {
158181ad6265SDimitry Andric case eLogHandlerStream:
158281ad6265SDimitry Andric return std::make_shared<StreamLogHandler>(fd, should_close, buffer_size);
158381ad6265SDimitry Andric case eLogHandlerCircular:
158481ad6265SDimitry Andric return std::make_shared<RotatingLogHandler>(buffer_size);
158581ad6265SDimitry Andric case eLogHandlerSystem:
158681ad6265SDimitry Andric return std::make_shared<SystemLogHandler>();
158781ad6265SDimitry Andric case eLogHandlerCallback:
158881ad6265SDimitry Andric return {};
158981ad6265SDimitry Andric }
159081ad6265SDimitry Andric return {};
159181ad6265SDimitry Andric }
159281ad6265SDimitry Andric
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)15930b57cec5SDimitry Andric bool Debugger::EnableLog(llvm::StringRef channel,
15940b57cec5SDimitry Andric llvm::ArrayRef<const char *> categories,
15950b57cec5SDimitry Andric llvm::StringRef log_file, uint32_t log_options,
159681ad6265SDimitry Andric size_t buffer_size, LogHandlerKind log_handler_kind,
15970b57cec5SDimitry Andric llvm::raw_ostream &error_stream) {
15980b57cec5SDimitry Andric
159981ad6265SDimitry Andric std::shared_ptr<LogHandler> log_handler_sp;
160081ad6265SDimitry Andric if (m_callback_handler_sp) {
160181ad6265SDimitry Andric log_handler_sp = m_callback_handler_sp;
16020b57cec5SDimitry Andric // For now when using the callback mode you always get thread & timestamp.
16030b57cec5SDimitry Andric log_options |=
16040b57cec5SDimitry Andric LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
16050b57cec5SDimitry Andric } else if (log_file.empty()) {
160681ad6265SDimitry Andric log_handler_sp =
160781ad6265SDimitry Andric CreateLogHandler(log_handler_kind, GetOutputFile().GetDescriptor(),
160881ad6265SDimitry Andric /*should_close=*/false, buffer_size);
16090b57cec5SDimitry Andric } else {
161081ad6265SDimitry Andric auto pos = m_stream_handlers.find(log_file);
161181ad6265SDimitry Andric if (pos != m_stream_handlers.end())
161281ad6265SDimitry Andric log_handler_sp = pos->second.lock();
161381ad6265SDimitry Andric if (!log_handler_sp) {
16145ffd83dbSDimitry Andric File::OpenOptions flags =
1615349cc55cSDimitry Andric File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate;
16160b57cec5SDimitry Andric if (log_options & LLDB_LOG_OPTION_APPEND)
16175ffd83dbSDimitry Andric flags |= File::eOpenOptionAppend;
16185ffd83dbSDimitry Andric else
16195ffd83dbSDimitry Andric flags |= File::eOpenOptionTruncate;
1620e8d8bef9SDimitry Andric llvm::Expected<FileUP> file = FileSystem::Instance().Open(
16215ffd83dbSDimitry Andric FileSpec(log_file), flags, lldb::eFilePermissionsFileDefault, false);
16225ffd83dbSDimitry Andric if (!file) {
1623e8d8bef9SDimitry Andric error_stream << "Unable to open log file '" << log_file
1624e8d8bef9SDimitry Andric << "': " << llvm::toString(file.takeError()) << "\n";
16250b57cec5SDimitry Andric return false;
16260b57cec5SDimitry Andric }
16275ffd83dbSDimitry Andric
162881ad6265SDimitry Andric log_handler_sp =
162981ad6265SDimitry Andric CreateLogHandler(log_handler_kind, (*file)->GetDescriptor(),
163081ad6265SDimitry Andric /*should_close=*/true, buffer_size);
163181ad6265SDimitry Andric m_stream_handlers[log_file] = log_handler_sp;
16320b57cec5SDimitry Andric }
16330b57cec5SDimitry Andric }
163481ad6265SDimitry Andric assert(log_handler_sp);
16350b57cec5SDimitry Andric
16360b57cec5SDimitry Andric if (log_options == 0)
163781ad6265SDimitry Andric log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
16380b57cec5SDimitry Andric
163981ad6265SDimitry Andric return Log::EnableLogChannel(log_handler_sp, log_options, channel, categories,
16400b57cec5SDimitry Andric error_stream);
16410b57cec5SDimitry Andric }
16420b57cec5SDimitry Andric
1643480093f4SDimitry Andric ScriptInterpreter *
GetScriptInterpreter(bool can_create,std::optional<lldb::ScriptLanguage> language)1644480093f4SDimitry Andric Debugger::GetScriptInterpreter(bool can_create,
1645bdd1243dSDimitry Andric std::optional<lldb::ScriptLanguage> language) {
16460b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> locker(m_script_interpreter_mutex);
1647480093f4SDimitry Andric lldb::ScriptLanguage script_language =
1648480093f4SDimitry Andric language ? *language : GetScriptLanguage();
16490b57cec5SDimitry Andric
1650480093f4SDimitry Andric if (!m_script_interpreters[script_language]) {
16510b57cec5SDimitry Andric if (!can_create)
16520b57cec5SDimitry Andric return nullptr;
1653480093f4SDimitry Andric m_script_interpreters[script_language] =
1654480093f4SDimitry Andric PluginManager::GetScriptInterpreterForLanguage(script_language, *this);
16550b57cec5SDimitry Andric }
16560b57cec5SDimitry Andric
1657480093f4SDimitry Andric return m_script_interpreters[script_language].get();
16580b57cec5SDimitry Andric }
16590b57cec5SDimitry Andric
GetSourceManager()16600b57cec5SDimitry Andric SourceManager &Debugger::GetSourceManager() {
16610b57cec5SDimitry Andric if (!m_source_manager_up)
16629dba64beSDimitry Andric m_source_manager_up = std::make_unique<SourceManager>(shared_from_this());
16630b57cec5SDimitry Andric return *m_source_manager_up;
16640b57cec5SDimitry Andric }
16650b57cec5SDimitry Andric
16660b57cec5SDimitry Andric // This function handles events that were broadcast by the process.
HandleBreakpointEvent(const EventSP & event_sp)16670b57cec5SDimitry Andric void Debugger::HandleBreakpointEvent(const EventSP &event_sp) {
16680b57cec5SDimitry Andric using namespace lldb;
16690b57cec5SDimitry Andric const uint32_t event_type =
16700b57cec5SDimitry Andric Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent(
16710b57cec5SDimitry Andric event_sp);
16720b57cec5SDimitry Andric
16730b57cec5SDimitry Andric // if (event_type & eBreakpointEventTypeAdded
16740b57cec5SDimitry Andric // || event_type & eBreakpointEventTypeRemoved
16750b57cec5SDimitry Andric // || event_type & eBreakpointEventTypeEnabled
16760b57cec5SDimitry Andric // || event_type & eBreakpointEventTypeDisabled
16770b57cec5SDimitry Andric // || event_type & eBreakpointEventTypeCommandChanged
16780b57cec5SDimitry Andric // || event_type & eBreakpointEventTypeConditionChanged
16790b57cec5SDimitry Andric // || event_type & eBreakpointEventTypeIgnoreChanged
16800b57cec5SDimitry Andric // || event_type & eBreakpointEventTypeLocationsResolved)
16810b57cec5SDimitry Andric // {
16820b57cec5SDimitry Andric // // Don't do anything about these events, since the breakpoint
16830b57cec5SDimitry Andric // commands already echo these actions.
16840b57cec5SDimitry Andric // }
16850b57cec5SDimitry Andric //
16860b57cec5SDimitry Andric if (event_type & eBreakpointEventTypeLocationsAdded) {
16870b57cec5SDimitry Andric uint32_t num_new_locations =
16880b57cec5SDimitry Andric Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(
16890b57cec5SDimitry Andric event_sp);
16900b57cec5SDimitry Andric if (num_new_locations > 0) {
16910b57cec5SDimitry Andric BreakpointSP breakpoint =
16920b57cec5SDimitry Andric Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
16930b57cec5SDimitry Andric StreamSP output_sp(GetAsyncOutputStream());
16940b57cec5SDimitry Andric if (output_sp) {
16950b57cec5SDimitry Andric output_sp->Printf("%d location%s added to breakpoint %d\n",
16960b57cec5SDimitry Andric num_new_locations, num_new_locations == 1 ? "" : "s",
16970b57cec5SDimitry Andric breakpoint->GetID());
16980b57cec5SDimitry Andric output_sp->Flush();
16990b57cec5SDimitry Andric }
17000b57cec5SDimitry Andric }
17010b57cec5SDimitry Andric }
17020b57cec5SDimitry Andric // else if (event_type & eBreakpointEventTypeLocationsRemoved)
17030b57cec5SDimitry Andric // {
17040b57cec5SDimitry Andric // // These locations just get disabled, not sure it is worth spamming
17050b57cec5SDimitry Andric // folks about this on the command line.
17060b57cec5SDimitry Andric // }
17070b57cec5SDimitry Andric // else if (event_type & eBreakpointEventTypeLocationsResolved)
17080b57cec5SDimitry Andric // {
17090b57cec5SDimitry Andric // // This might be an interesting thing to note, but I'm going to
17100b57cec5SDimitry Andric // leave it quiet for now, it just looked noisy.
17110b57cec5SDimitry Andric // }
17120b57cec5SDimitry Andric }
17130b57cec5SDimitry Andric
FlushProcessOutput(Process & process,bool flush_stdout,bool flush_stderr)17149dba64beSDimitry Andric void Debugger::FlushProcessOutput(Process &process, bool flush_stdout,
17159dba64beSDimitry Andric bool flush_stderr) {
17169dba64beSDimitry Andric const auto &flush = [&](Stream &stream,
17179dba64beSDimitry Andric size_t (Process::*get)(char *, size_t, Status &)) {
17180b57cec5SDimitry Andric Status error;
17190b57cec5SDimitry Andric size_t len;
17209dba64beSDimitry Andric char buffer[1024];
17219dba64beSDimitry Andric while ((len = (process.*get)(buffer, sizeof(buffer), error)) > 0)
17229dba64beSDimitry Andric stream.Write(buffer, len);
17239dba64beSDimitry Andric stream.Flush();
17249dba64beSDimitry Andric };
17250b57cec5SDimitry Andric
17269dba64beSDimitry Andric std::lock_guard<std::mutex> guard(m_output_flush_mutex);
17279dba64beSDimitry Andric if (flush_stdout)
17289dba64beSDimitry Andric flush(*GetAsyncOutputStream(), &Process::GetSTDOUT);
17299dba64beSDimitry Andric if (flush_stderr)
17309dba64beSDimitry Andric flush(*GetAsyncErrorStream(), &Process::GetSTDERR);
17310b57cec5SDimitry Andric }
17320b57cec5SDimitry Andric
17330b57cec5SDimitry Andric // This function handles events that were broadcast by the process.
HandleProcessEvent(const EventSP & event_sp)17340b57cec5SDimitry Andric void Debugger::HandleProcessEvent(const EventSP &event_sp) {
17350b57cec5SDimitry Andric using namespace lldb;
17360b57cec5SDimitry Andric const uint32_t event_type = event_sp->GetType();
17370b57cec5SDimitry Andric ProcessSP process_sp =
17380b57cec5SDimitry Andric (event_type == Process::eBroadcastBitStructuredData)
17390b57cec5SDimitry Andric ? EventDataStructuredData::GetProcessFromEvent(event_sp.get())
17400b57cec5SDimitry Andric : Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
17410b57cec5SDimitry Andric
17420b57cec5SDimitry Andric StreamSP output_stream_sp = GetAsyncOutputStream();
17430b57cec5SDimitry Andric StreamSP error_stream_sp = GetAsyncErrorStream();
17440b57cec5SDimitry Andric const bool gui_enabled = IsForwardingEvents();
17450b57cec5SDimitry Andric
17460b57cec5SDimitry Andric if (!gui_enabled) {
17470b57cec5SDimitry Andric bool pop_process_io_handler = false;
17480b57cec5SDimitry Andric assert(process_sp);
17490b57cec5SDimitry Andric
17500b57cec5SDimitry Andric bool state_is_stopped = false;
17510b57cec5SDimitry Andric const bool got_state_changed =
17520b57cec5SDimitry Andric (event_type & Process::eBroadcastBitStateChanged) != 0;
17530b57cec5SDimitry Andric const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
17540b57cec5SDimitry Andric const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
17550b57cec5SDimitry Andric const bool got_structured_data =
17560b57cec5SDimitry Andric (event_type & Process::eBroadcastBitStructuredData) != 0;
17570b57cec5SDimitry Andric
17580b57cec5SDimitry Andric if (got_state_changed) {
17590b57cec5SDimitry Andric StateType event_state =
17600b57cec5SDimitry Andric Process::ProcessEventData::GetStateFromEvent(event_sp.get());
17610b57cec5SDimitry Andric state_is_stopped = StateIsStoppedState(event_state, false);
17620b57cec5SDimitry Andric }
17630b57cec5SDimitry Andric
17640b57cec5SDimitry Andric // Display running state changes first before any STDIO
17650b57cec5SDimitry Andric if (got_state_changed && !state_is_stopped) {
1766fe013be4SDimitry Andric // This is a public stop which we are going to announce to the user, so
1767fe013be4SDimitry Andric // we should force the most relevant frame selection here.
17680b57cec5SDimitry Andric Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(),
1769fe013be4SDimitry Andric SelectMostRelevantFrame,
17700b57cec5SDimitry Andric pop_process_io_handler);
17710b57cec5SDimitry Andric }
17720b57cec5SDimitry Andric
17739dba64beSDimitry Andric // Now display STDOUT and STDERR
17749dba64beSDimitry Andric FlushProcessOutput(*process_sp, got_stdout || got_state_changed,
17759dba64beSDimitry Andric got_stderr || got_state_changed);
17760b57cec5SDimitry Andric
17770b57cec5SDimitry Andric // Give structured data events an opportunity to display.
17780b57cec5SDimitry Andric if (got_structured_data) {
17790b57cec5SDimitry Andric StructuredDataPluginSP plugin_sp =
17800b57cec5SDimitry Andric EventDataStructuredData::GetPluginFromEvent(event_sp.get());
17810b57cec5SDimitry Andric if (plugin_sp) {
17820b57cec5SDimitry Andric auto structured_data_sp =
17830b57cec5SDimitry Andric EventDataStructuredData::GetObjectFromEvent(event_sp.get());
17840b57cec5SDimitry Andric if (output_stream_sp) {
17850b57cec5SDimitry Andric StreamString content_stream;
17860b57cec5SDimitry Andric Status error =
17870b57cec5SDimitry Andric plugin_sp->GetDescription(structured_data_sp, content_stream);
17880b57cec5SDimitry Andric if (error.Success()) {
17890b57cec5SDimitry Andric if (!content_stream.GetString().empty()) {
17900b57cec5SDimitry Andric // Add newline.
17910b57cec5SDimitry Andric content_stream.PutChar('\n');
17920b57cec5SDimitry Andric content_stream.Flush();
17930b57cec5SDimitry Andric
17940b57cec5SDimitry Andric // Print it.
17950b57cec5SDimitry Andric output_stream_sp->PutCString(content_stream.GetString());
17960b57cec5SDimitry Andric }
17970b57cec5SDimitry Andric } else {
1798349cc55cSDimitry Andric error_stream_sp->Format("Failed to print structured "
1799349cc55cSDimitry Andric "data with plugin {0}: {1}",
1800349cc55cSDimitry Andric plugin_sp->GetPluginName(), error);
18010b57cec5SDimitry Andric }
18020b57cec5SDimitry Andric }
18030b57cec5SDimitry Andric }
18040b57cec5SDimitry Andric }
18050b57cec5SDimitry Andric
18060b57cec5SDimitry Andric // Now display any stopped state changes after any STDIO
18070b57cec5SDimitry Andric if (got_state_changed && state_is_stopped) {
18080b57cec5SDimitry Andric Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(),
1809fe013be4SDimitry Andric SelectMostRelevantFrame,
18100b57cec5SDimitry Andric pop_process_io_handler);
18110b57cec5SDimitry Andric }
18120b57cec5SDimitry Andric
18130b57cec5SDimitry Andric output_stream_sp->Flush();
18140b57cec5SDimitry Andric error_stream_sp->Flush();
18150b57cec5SDimitry Andric
18160b57cec5SDimitry Andric if (pop_process_io_handler)
18170b57cec5SDimitry Andric process_sp->PopProcessIOHandler();
18180b57cec5SDimitry Andric }
18190b57cec5SDimitry Andric }
18200b57cec5SDimitry Andric
HandleThreadEvent(const EventSP & event_sp)18210b57cec5SDimitry Andric void Debugger::HandleThreadEvent(const EventSP &event_sp) {
18220b57cec5SDimitry Andric // At present the only thread event we handle is the Frame Changed event, and
18230b57cec5SDimitry Andric // all we do for that is just reprint the thread status for that thread.
18240b57cec5SDimitry Andric using namespace lldb;
18250b57cec5SDimitry Andric const uint32_t event_type = event_sp->GetType();
18260b57cec5SDimitry Andric const bool stop_format = true;
18270b57cec5SDimitry Andric if (event_type == Thread::eBroadcastBitStackChanged ||
18280b57cec5SDimitry Andric event_type == Thread::eBroadcastBitThreadSelected) {
18290b57cec5SDimitry Andric ThreadSP thread_sp(
18300b57cec5SDimitry Andric Thread::ThreadEventData::GetThreadFromEvent(event_sp.get()));
18310b57cec5SDimitry Andric if (thread_sp) {
18320b57cec5SDimitry Andric thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1, stop_format);
18330b57cec5SDimitry Andric }
18340b57cec5SDimitry Andric }
18350b57cec5SDimitry Andric }
18360b57cec5SDimitry Andric
IsForwardingEvents()18370b57cec5SDimitry Andric bool Debugger::IsForwardingEvents() { return (bool)m_forward_listener_sp; }
18380b57cec5SDimitry Andric
EnableForwardEvents(const ListenerSP & listener_sp)18390b57cec5SDimitry Andric void Debugger::EnableForwardEvents(const ListenerSP &listener_sp) {
18400b57cec5SDimitry Andric m_forward_listener_sp = listener_sp;
18410b57cec5SDimitry Andric }
18420b57cec5SDimitry Andric
CancelForwardEvents(const ListenerSP & listener_sp)18430b57cec5SDimitry Andric void Debugger::CancelForwardEvents(const ListenerSP &listener_sp) {
18440b57cec5SDimitry Andric m_forward_listener_sp.reset();
18450b57cec5SDimitry Andric }
18460b57cec5SDimitry Andric
DefaultEventHandler()184781ad6265SDimitry Andric lldb::thread_result_t Debugger::DefaultEventHandler() {
18480b57cec5SDimitry Andric ListenerSP listener_sp(GetListener());
18490b57cec5SDimitry Andric ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
18500b57cec5SDimitry Andric ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
18510b57cec5SDimitry Andric ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
18520b57cec5SDimitry Andric BroadcastEventSpec target_event_spec(broadcaster_class_target,
18530b57cec5SDimitry Andric Target::eBroadcastBitBreakpointChanged);
18540b57cec5SDimitry Andric
18550b57cec5SDimitry Andric BroadcastEventSpec process_event_spec(
18560b57cec5SDimitry Andric broadcaster_class_process,
18570b57cec5SDimitry Andric Process::eBroadcastBitStateChanged | Process::eBroadcastBitSTDOUT |
18580b57cec5SDimitry Andric Process::eBroadcastBitSTDERR | Process::eBroadcastBitStructuredData);
18590b57cec5SDimitry Andric
18600b57cec5SDimitry Andric BroadcastEventSpec thread_event_spec(broadcaster_class_thread,
18610b57cec5SDimitry Andric Thread::eBroadcastBitStackChanged |
18620b57cec5SDimitry Andric Thread::eBroadcastBitThreadSelected);
18630b57cec5SDimitry Andric
18640b57cec5SDimitry Andric listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp,
18650b57cec5SDimitry Andric target_event_spec);
18660b57cec5SDimitry Andric listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp,
18670b57cec5SDimitry Andric process_event_spec);
18680b57cec5SDimitry Andric listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp,
18690b57cec5SDimitry Andric thread_event_spec);
18700b57cec5SDimitry Andric listener_sp->StartListeningForEvents(
18710b57cec5SDimitry Andric m_command_interpreter_up.get(),
18720b57cec5SDimitry Andric CommandInterpreter::eBroadcastBitQuitCommandReceived |
18730b57cec5SDimitry Andric CommandInterpreter::eBroadcastBitAsynchronousOutputData |
18740b57cec5SDimitry Andric CommandInterpreter::eBroadcastBitAsynchronousErrorData);
18750b57cec5SDimitry Andric
187681ad6265SDimitry Andric listener_sp->StartListeningForEvents(
1877bdd1243dSDimitry Andric &m_broadcaster, eBroadcastBitProgress | eBroadcastBitWarning |
1878bdd1243dSDimitry Andric eBroadcastBitError | eBroadcastSymbolChange);
187981ad6265SDimitry Andric
18800b57cec5SDimitry Andric // Let the thread that spawned us know that we have started up and that we
18810b57cec5SDimitry Andric // are now listening to all required events so no events get missed
18820b57cec5SDimitry Andric m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
18830b57cec5SDimitry Andric
18840b57cec5SDimitry Andric bool done = false;
18850b57cec5SDimitry Andric while (!done) {
18860b57cec5SDimitry Andric EventSP event_sp;
1887bdd1243dSDimitry Andric if (listener_sp->GetEvent(event_sp, std::nullopt)) {
18880b57cec5SDimitry Andric if (event_sp) {
18890b57cec5SDimitry Andric Broadcaster *broadcaster = event_sp->GetBroadcaster();
18900b57cec5SDimitry Andric if (broadcaster) {
18910b57cec5SDimitry Andric uint32_t event_type = event_sp->GetType();
18920b57cec5SDimitry Andric ConstString broadcaster_class(broadcaster->GetBroadcasterClass());
18930b57cec5SDimitry Andric if (broadcaster_class == broadcaster_class_process) {
18940b57cec5SDimitry Andric HandleProcessEvent(event_sp);
18950b57cec5SDimitry Andric } else if (broadcaster_class == broadcaster_class_target) {
18960b57cec5SDimitry Andric if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(
18970b57cec5SDimitry Andric event_sp.get())) {
18980b57cec5SDimitry Andric HandleBreakpointEvent(event_sp);
18990b57cec5SDimitry Andric }
19000b57cec5SDimitry Andric } else if (broadcaster_class == broadcaster_class_thread) {
19010b57cec5SDimitry Andric HandleThreadEvent(event_sp);
19020b57cec5SDimitry Andric } else if (broadcaster == m_command_interpreter_up.get()) {
19030b57cec5SDimitry Andric if (event_type &
19040b57cec5SDimitry Andric CommandInterpreter::eBroadcastBitQuitCommandReceived) {
19050b57cec5SDimitry Andric done = true;
19060b57cec5SDimitry Andric } else if (event_type &
19070b57cec5SDimitry Andric CommandInterpreter::eBroadcastBitAsynchronousErrorData) {
1908480093f4SDimitry Andric const char *data = static_cast<const char *>(
19090b57cec5SDimitry Andric EventDataBytes::GetBytesFromEvent(event_sp.get()));
19100b57cec5SDimitry Andric if (data && data[0]) {
19110b57cec5SDimitry Andric StreamSP error_sp(GetAsyncErrorStream());
19120b57cec5SDimitry Andric if (error_sp) {
19130b57cec5SDimitry Andric error_sp->PutCString(data);
19140b57cec5SDimitry Andric error_sp->Flush();
19150b57cec5SDimitry Andric }
19160b57cec5SDimitry Andric }
19170b57cec5SDimitry Andric } else if (event_type & CommandInterpreter::
19180b57cec5SDimitry Andric eBroadcastBitAsynchronousOutputData) {
1919480093f4SDimitry Andric const char *data = static_cast<const char *>(
19200b57cec5SDimitry Andric EventDataBytes::GetBytesFromEvent(event_sp.get()));
19210b57cec5SDimitry Andric if (data && data[0]) {
19220b57cec5SDimitry Andric StreamSP output_sp(GetAsyncOutputStream());
19230b57cec5SDimitry Andric if (output_sp) {
19240b57cec5SDimitry Andric output_sp->PutCString(data);
19250b57cec5SDimitry Andric output_sp->Flush();
19260b57cec5SDimitry Andric }
19270b57cec5SDimitry Andric }
19280b57cec5SDimitry Andric }
192981ad6265SDimitry Andric } else if (broadcaster == &m_broadcaster) {
193081ad6265SDimitry Andric if (event_type & Debugger::eBroadcastBitProgress)
193181ad6265SDimitry Andric HandleProgressEvent(event_sp);
193281ad6265SDimitry Andric else if (event_type & Debugger::eBroadcastBitWarning)
193381ad6265SDimitry Andric HandleDiagnosticEvent(event_sp);
193481ad6265SDimitry Andric else if (event_type & Debugger::eBroadcastBitError)
193581ad6265SDimitry Andric HandleDiagnosticEvent(event_sp);
19360b57cec5SDimitry Andric }
19370b57cec5SDimitry Andric }
19380b57cec5SDimitry Andric
19390b57cec5SDimitry Andric if (m_forward_listener_sp)
19400b57cec5SDimitry Andric m_forward_listener_sp->AddEvent(event_sp);
19410b57cec5SDimitry Andric }
19420b57cec5SDimitry Andric }
19430b57cec5SDimitry Andric }
19440b57cec5SDimitry Andric return {};
19450b57cec5SDimitry Andric }
19460b57cec5SDimitry Andric
StartEventHandlerThread()19470b57cec5SDimitry Andric bool Debugger::StartEventHandlerThread() {
19480b57cec5SDimitry Andric if (!m_event_handler_thread.IsJoinable()) {
19490b57cec5SDimitry Andric // We must synchronize with the DefaultEventHandler() thread to ensure it
19500b57cec5SDimitry Andric // is up and running and listening to events before we return from this
19510b57cec5SDimitry Andric // function. We do this by listening to events for the
19520b57cec5SDimitry Andric // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
19530b57cec5SDimitry Andric ConstString full_name("lldb.debugger.event-handler");
19540b57cec5SDimitry Andric ListenerSP listener_sp(Listener::MakeListener(full_name.AsCString()));
19550b57cec5SDimitry Andric listener_sp->StartListeningForEvents(&m_sync_broadcaster,
19560b57cec5SDimitry Andric eBroadcastBitEventThreadIsListening);
19570b57cec5SDimitry Andric
19585ffd83dbSDimitry Andric llvm::StringRef thread_name =
19599dba64beSDimitry Andric full_name.GetLength() < llvm::get_max_thread_name_length()
19605ffd83dbSDimitry Andric ? full_name.GetStringRef()
19619dba64beSDimitry Andric : "dbg.evt-handler";
19620b57cec5SDimitry Andric
19630b57cec5SDimitry Andric // Use larger 8MB stack for this thread
19640b57cec5SDimitry Andric llvm::Expected<HostThread> event_handler_thread =
196581ad6265SDimitry Andric ThreadLauncher::LaunchThread(
196681ad6265SDimitry Andric thread_name, [this] { return DefaultEventHandler(); },
19670b57cec5SDimitry Andric g_debugger_event_thread_stack_bytes);
19680b57cec5SDimitry Andric
19690b57cec5SDimitry Andric if (event_handler_thread) {
19700b57cec5SDimitry Andric m_event_handler_thread = *event_handler_thread;
19710b57cec5SDimitry Andric } else {
1972fe013be4SDimitry Andric LLDB_LOG_ERROR(GetLog(LLDBLog::Host), event_handler_thread.takeError(),
1973fe013be4SDimitry Andric "failed to launch host thread: {0}");
19740b57cec5SDimitry Andric }
19750b57cec5SDimitry Andric
19760b57cec5SDimitry Andric // Make sure DefaultEventHandler() is running and listening to events
19770b57cec5SDimitry Andric // before we return from this function. We are only listening for events of
19780b57cec5SDimitry Andric // type eBroadcastBitEventThreadIsListening so we don't need to check the
19790b57cec5SDimitry Andric // event, we just need to wait an infinite amount of time for it (nullptr
19800b57cec5SDimitry Andric // timeout as the first parameter)
19810b57cec5SDimitry Andric lldb::EventSP event_sp;
1982bdd1243dSDimitry Andric listener_sp->GetEvent(event_sp, std::nullopt);
19830b57cec5SDimitry Andric }
19840b57cec5SDimitry Andric return m_event_handler_thread.IsJoinable();
19850b57cec5SDimitry Andric }
19860b57cec5SDimitry Andric
StopEventHandlerThread()19870b57cec5SDimitry Andric void Debugger::StopEventHandlerThread() {
19880b57cec5SDimitry Andric if (m_event_handler_thread.IsJoinable()) {
19890b57cec5SDimitry Andric GetCommandInterpreter().BroadcastEvent(
19900b57cec5SDimitry Andric CommandInterpreter::eBroadcastBitQuitCommandReceived);
19910b57cec5SDimitry Andric m_event_handler_thread.Join(nullptr);
19920b57cec5SDimitry Andric }
19930b57cec5SDimitry Andric }
19940b57cec5SDimitry Andric
IOHandlerThread()199581ad6265SDimitry Andric lldb::thread_result_t Debugger::IOHandlerThread() {
199681ad6265SDimitry Andric RunIOHandlers();
199781ad6265SDimitry Andric StopEventHandlerThread();
19980b57cec5SDimitry Andric return {};
19990b57cec5SDimitry Andric }
20000b57cec5SDimitry Andric
HandleProgressEvent(const lldb::EventSP & event_sp)200181ad6265SDimitry Andric void Debugger::HandleProgressEvent(const lldb::EventSP &event_sp) {
200281ad6265SDimitry Andric auto *data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
200381ad6265SDimitry Andric if (!data)
200481ad6265SDimitry Andric return;
200581ad6265SDimitry Andric
200681ad6265SDimitry Andric // Do some bookkeeping for the current event, regardless of whether we're
200781ad6265SDimitry Andric // going to show the progress.
200881ad6265SDimitry Andric const uint64_t id = data->GetID();
200981ad6265SDimitry Andric if (m_current_event_id) {
2010753f127fSDimitry Andric Log *log = GetLog(LLDBLog::Events);
2011753f127fSDimitry Andric if (log && log->GetVerbose()) {
2012753f127fSDimitry Andric StreamString log_stream;
2013753f127fSDimitry Andric log_stream.AsRawOstream()
2014753f127fSDimitry Andric << static_cast<void *>(this) << " Debugger(" << GetID()
2015753f127fSDimitry Andric << ")::HandleProgressEvent( m_current_event_id = "
2016753f127fSDimitry Andric << *m_current_event_id << ", data = { ";
2017753f127fSDimitry Andric data->Dump(&log_stream);
2018753f127fSDimitry Andric log_stream << " } )";
2019753f127fSDimitry Andric log->PutString(log_stream.GetString());
2020753f127fSDimitry Andric }
202181ad6265SDimitry Andric if (id != *m_current_event_id)
202281ad6265SDimitry Andric return;
2023753f127fSDimitry Andric if (data->GetCompleted() == data->GetTotal())
202481ad6265SDimitry Andric m_current_event_id.reset();
202581ad6265SDimitry Andric } else {
202681ad6265SDimitry Andric m_current_event_id = id;
202781ad6265SDimitry Andric }
202881ad6265SDimitry Andric
202981ad6265SDimitry Andric // Decide whether we actually are going to show the progress. This decision
203081ad6265SDimitry Andric // can change between iterations so check it inside the loop.
203181ad6265SDimitry Andric if (!GetShowProgress())
203281ad6265SDimitry Andric return;
203381ad6265SDimitry Andric
203481ad6265SDimitry Andric // Determine whether the current output file is an interactive terminal with
203581ad6265SDimitry Andric // color support. We assume that if we support ANSI escape codes we support
203681ad6265SDimitry Andric // vt100 escape codes.
203781ad6265SDimitry Andric File &file = GetOutputFile();
203881ad6265SDimitry Andric if (!file.GetIsInteractive() || !file.GetIsTerminalWithColors())
203981ad6265SDimitry Andric return;
204081ad6265SDimitry Andric
204181ad6265SDimitry Andric StreamSP output = GetAsyncOutputStream();
204281ad6265SDimitry Andric
204381ad6265SDimitry Andric // Print over previous line, if any.
204481ad6265SDimitry Andric output->Printf("\r");
204581ad6265SDimitry Andric
2046753f127fSDimitry Andric if (data->GetCompleted() == data->GetTotal()) {
204781ad6265SDimitry Andric // Clear the current line.
204881ad6265SDimitry Andric output->Printf("\x1B[2K");
204981ad6265SDimitry Andric output->Flush();
205081ad6265SDimitry Andric return;
205181ad6265SDimitry Andric }
205281ad6265SDimitry Andric
205381ad6265SDimitry Andric // Trim the progress message if it exceeds the window's width and print it.
205481ad6265SDimitry Andric std::string message = data->GetMessage();
205581ad6265SDimitry Andric if (data->IsFinite())
205681ad6265SDimitry Andric message = llvm::formatv("[{0}/{1}] {2}", data->GetCompleted(),
205781ad6265SDimitry Andric data->GetTotal(), message)
205881ad6265SDimitry Andric .str();
205981ad6265SDimitry Andric
206081ad6265SDimitry Andric // Trim the progress message if it exceeds the window's width and print it.
206181ad6265SDimitry Andric const uint32_t term_width = GetTerminalWidth();
206281ad6265SDimitry Andric const uint32_t ellipsis = 3;
206381ad6265SDimitry Andric if (message.size() + ellipsis >= term_width)
206481ad6265SDimitry Andric message = message.substr(0, term_width - ellipsis);
206581ad6265SDimitry Andric
206681ad6265SDimitry Andric const bool use_color = GetUseColor();
206781ad6265SDimitry Andric llvm::StringRef ansi_prefix = GetShowProgressAnsiPrefix();
206881ad6265SDimitry Andric if (!ansi_prefix.empty())
206981ad6265SDimitry Andric output->Printf(
207081ad6265SDimitry Andric "%s", ansi::FormatAnsiTerminalCodes(ansi_prefix, use_color).c_str());
207181ad6265SDimitry Andric
207281ad6265SDimitry Andric output->Printf("%s...", message.c_str());
207381ad6265SDimitry Andric
207481ad6265SDimitry Andric llvm::StringRef ansi_suffix = GetShowProgressAnsiSuffix();
207581ad6265SDimitry Andric if (!ansi_suffix.empty())
207681ad6265SDimitry Andric output->Printf(
207781ad6265SDimitry Andric "%s", ansi::FormatAnsiTerminalCodes(ansi_suffix, use_color).c_str());
207881ad6265SDimitry Andric
207981ad6265SDimitry Andric // Clear until the end of the line.
208081ad6265SDimitry Andric output->Printf("\x1B[K\r");
208181ad6265SDimitry Andric
208281ad6265SDimitry Andric // Flush the output.
208381ad6265SDimitry Andric output->Flush();
208481ad6265SDimitry Andric }
208581ad6265SDimitry Andric
HandleDiagnosticEvent(const lldb::EventSP & event_sp)208681ad6265SDimitry Andric void Debugger::HandleDiagnosticEvent(const lldb::EventSP &event_sp) {
208781ad6265SDimitry Andric auto *data = DiagnosticEventData::GetEventDataFromEvent(event_sp.get());
208881ad6265SDimitry Andric if (!data)
208981ad6265SDimitry Andric return;
209081ad6265SDimitry Andric
209181ad6265SDimitry Andric StreamSP stream = GetAsyncErrorStream();
209281ad6265SDimitry Andric data->Dump(stream.get());
209381ad6265SDimitry Andric }
209481ad6265SDimitry Andric
HasIOHandlerThread() const2095fe013be4SDimitry Andric bool Debugger::HasIOHandlerThread() const {
2096fe013be4SDimitry Andric return m_io_handler_thread.IsJoinable();
2097fe013be4SDimitry Andric }
2098fe013be4SDimitry Andric
SetIOHandlerThread(HostThread & new_thread)2099fe013be4SDimitry Andric HostThread Debugger::SetIOHandlerThread(HostThread &new_thread) {
2100fe013be4SDimitry Andric HostThread old_host = m_io_handler_thread;
2101fe013be4SDimitry Andric m_io_handler_thread = new_thread;
2102fe013be4SDimitry Andric return old_host;
2103fe013be4SDimitry Andric }
21040b57cec5SDimitry Andric
StartIOHandlerThread()21050b57cec5SDimitry Andric bool Debugger::StartIOHandlerThread() {
21060b57cec5SDimitry Andric if (!m_io_handler_thread.IsJoinable()) {
21070b57cec5SDimitry Andric llvm::Expected<HostThread> io_handler_thread = ThreadLauncher::LaunchThread(
210881ad6265SDimitry Andric "lldb.debugger.io-handler", [this] { return IOHandlerThread(); },
21090b57cec5SDimitry Andric 8 * 1024 * 1024); // Use larger 8MB stack for this thread
21100b57cec5SDimitry Andric if (io_handler_thread) {
21110b57cec5SDimitry Andric m_io_handler_thread = *io_handler_thread;
21120b57cec5SDimitry Andric } else {
2113fe013be4SDimitry Andric LLDB_LOG_ERROR(GetLog(LLDBLog::Host), io_handler_thread.takeError(),
2114fe013be4SDimitry Andric "failed to launch host thread: {0}");
21150b57cec5SDimitry Andric }
21160b57cec5SDimitry Andric }
21170b57cec5SDimitry Andric return m_io_handler_thread.IsJoinable();
21180b57cec5SDimitry Andric }
21190b57cec5SDimitry Andric
StopIOHandlerThread()21200b57cec5SDimitry Andric void Debugger::StopIOHandlerThread() {
21210b57cec5SDimitry Andric if (m_io_handler_thread.IsJoinable()) {
21229dba64beSDimitry Andric GetInputFile().Close();
21230b57cec5SDimitry Andric m_io_handler_thread.Join(nullptr);
21240b57cec5SDimitry Andric }
21250b57cec5SDimitry Andric }
21260b57cec5SDimitry Andric
JoinIOHandlerThread()21270b57cec5SDimitry Andric void Debugger::JoinIOHandlerThread() {
21280b57cec5SDimitry Andric if (HasIOHandlerThread()) {
21290b57cec5SDimitry Andric thread_result_t result;
21300b57cec5SDimitry Andric m_io_handler_thread.Join(&result);
21310b57cec5SDimitry Andric m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
21320b57cec5SDimitry Andric }
21330b57cec5SDimitry Andric }
21340b57cec5SDimitry Andric
IsIOHandlerThreadCurrentThread() const2135fe013be4SDimitry Andric bool Debugger::IsIOHandlerThreadCurrentThread() const {
2136fe013be4SDimitry Andric if (!HasIOHandlerThread())
2137fe013be4SDimitry Andric return false;
2138fe013be4SDimitry Andric return m_io_handler_thread.EqualsThread(Host::GetCurrentThread());
2139fe013be4SDimitry Andric }
2140fe013be4SDimitry Andric
GetSelectedOrDummyTarget(bool prefer_dummy)2141e8d8bef9SDimitry Andric Target &Debugger::GetSelectedOrDummyTarget(bool prefer_dummy) {
21420b57cec5SDimitry Andric if (!prefer_dummy) {
2143e8d8bef9SDimitry Andric if (TargetSP target = m_target_list.GetSelectedTarget())
2144e8d8bef9SDimitry Andric return *target;
21450b57cec5SDimitry Andric }
21460b57cec5SDimitry Andric return GetDummyTarget();
21470b57cec5SDimitry Andric }
21480b57cec5SDimitry Andric
RunREPL(LanguageType language,const char * repl_options)21490b57cec5SDimitry Andric Status Debugger::RunREPL(LanguageType language, const char *repl_options) {
21500b57cec5SDimitry Andric Status err;
21510b57cec5SDimitry Andric FileSpec repl_executable;
21520b57cec5SDimitry Andric
215304eeddc0SDimitry Andric if (language == eLanguageTypeUnknown)
215404eeddc0SDimitry Andric language = GetREPLLanguage();
215504eeddc0SDimitry Andric
21560b57cec5SDimitry Andric if (language == eLanguageTypeUnknown) {
21579dba64beSDimitry Andric LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
21580b57cec5SDimitry Andric
21599dba64beSDimitry Andric if (auto single_lang = repl_languages.GetSingularLanguage()) {
21609dba64beSDimitry Andric language = *single_lang;
21619dba64beSDimitry Andric } else if (repl_languages.Empty()) {
216204eeddc0SDimitry Andric err.SetErrorString(
21630b57cec5SDimitry Andric "LLDB isn't configured with REPL support for any languages.");
21640b57cec5SDimitry Andric return err;
21650b57cec5SDimitry Andric } else {
216604eeddc0SDimitry Andric err.SetErrorString(
21670b57cec5SDimitry Andric "Multiple possible REPL languages. Please specify a language.");
21680b57cec5SDimitry Andric return err;
21690b57cec5SDimitry Andric }
21700b57cec5SDimitry Andric }
21710b57cec5SDimitry Andric
21720b57cec5SDimitry Andric Target *const target =
21730b57cec5SDimitry Andric nullptr; // passing in an empty target means the REPL must create one
21740b57cec5SDimitry Andric
21750b57cec5SDimitry Andric REPLSP repl_sp(REPL::Create(err, language, this, target, repl_options));
21760b57cec5SDimitry Andric
21770b57cec5SDimitry Andric if (!err.Success()) {
21780b57cec5SDimitry Andric return err;
21790b57cec5SDimitry Andric }
21800b57cec5SDimitry Andric
21810b57cec5SDimitry Andric if (!repl_sp) {
21820b57cec5SDimitry Andric err.SetErrorStringWithFormat("couldn't find a REPL for %s",
21830b57cec5SDimitry Andric Language::GetNameForLanguageType(language));
21840b57cec5SDimitry Andric return err;
21850b57cec5SDimitry Andric }
21860b57cec5SDimitry Andric
21870b57cec5SDimitry Andric repl_sp->SetCompilerOptions(repl_options);
21880b57cec5SDimitry Andric repl_sp->RunLoop();
21890b57cec5SDimitry Andric
21900b57cec5SDimitry Andric return err;
21910b57cec5SDimitry Andric }
219281ad6265SDimitry Andric
GetThreadPool()219381ad6265SDimitry Andric llvm::ThreadPool &Debugger::GetThreadPool() {
2194bdd1243dSDimitry Andric assert(g_thread_pool &&
2195bdd1243dSDimitry Andric "Debugger::GetThreadPool called before Debugger::Initialize");
219681ad6265SDimitry Andric return *g_thread_pool;
219781ad6265SDimitry Andric }
2198