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