Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Debugger.cpp --------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Greg Clayton | 4a33d31 | 2011-06-23 17:59:56 +0000 | [diff] [blame] | 9 | #include "lldb/Core/Debugger.h" |
| 10 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 11 | #include "lldb/Breakpoint/Breakpoint.h" |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 12 | #include "lldb/Core/FormatEntity.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Mangled.h" |
| 14 | #include "lldb/Core/ModuleList.h" |
Greg Clayton | e8cd0c9 | 2012-10-19 18:02:49 +0000 | [diff] [blame] | 15 | #include "lldb/Core/PluginManager.h" |
Jim Ingham | 5b52f0c | 2011-06-02 23:58:26 +0000 | [diff] [blame] | 16 | #include "lldb/Core/StreamAsynchronousIO.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StreamFile.h" |
Enrico Granata | 5548cb5 | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 18 | #include "lldb/DataFormatters/DataVisualization.h" |
Sean Callanan | 3e7e915 | 2015-10-20 00:23:46 +0000 | [diff] [blame] | 19 | #include "lldb/Expression/REPL.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 20 | #include "lldb/Host/File.h" |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 21 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 22 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | a340661 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 23 | #include "lldb/Host/Terminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 24 | #include "lldb/Host/ThreadLauncher.h" |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandInterpreter.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/OptionValue.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 27 | #include "lldb/Interpreter/OptionValueProperties.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/OptionValueSInt64.h" |
| 29 | #include "lldb/Interpreter/OptionValueString.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 30 | #include "lldb/Interpreter/Property.h" |
| 31 | #include "lldb/Interpreter/ScriptInterpreter.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 32 | #include "lldb/Symbol/Function.h" |
| 33 | #include "lldb/Symbol/Symbol.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 34 | #include "lldb/Symbol/SymbolContext.h" |
Sean Callanan | 3e7e915 | 2015-10-20 00:23:46 +0000 | [diff] [blame] | 35 | #include "lldb/Target/Language.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Target/Process.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 37 | #include "lldb/Target/StructuredDataPlugin.h" |
Enrico Granata | 84a53df | 2013-05-20 22:29:23 +0000 | [diff] [blame] | 38 | #include "lldb/Target/Target.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | #include "lldb/Target/TargetList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | #include "lldb/Target/Thread.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 41 | #include "lldb/Target/ThreadList.h" |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 42 | #include "lldb/Utility/AnsiTerminal.h" |
Pavel Labath | 181b823 | 2018-12-14 15:59:49 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/Event.h" |
| 44 | #include "lldb/Utility/Listener.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 45 | #include "lldb/Utility/Log.h" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 46 | #include "lldb/Utility/Reproducer.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 47 | #include "lldb/Utility/State.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 48 | #include "lldb/Utility/Stream.h" |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 49 | #include "lldb/Utility/StreamCallback.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 50 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 51 | |
Nico Weber | b1cb0b79 | 2018-04-10 13:33:45 +0000 | [diff] [blame] | 52 | #if defined(_WIN32) |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 53 | #include "lldb/Host/windows/PosixApi.h" |
David Bolvansky | 8aa2361 | 2018-09-05 22:06:58 +0000 | [diff] [blame] | 54 | #include "lldb/Host/windows/windows.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/None.h" |
| 58 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/StringRef.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/iterator.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 61 | #include "llvm/Support/DynamicLibrary.h" |
| 62 | #include "llvm/Support/FileSystem.h" |
David Bolvansky | 8aa2361 | 2018-09-05 22:06:58 +0000 | [diff] [blame] | 63 | #include "llvm/Support/Process.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 64 | #include "llvm/Support/Threading.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 65 | #include "llvm/Support/raw_ostream.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 66 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 67 | #include <list> |
| 68 | #include <memory> |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 69 | #include <mutex> |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 70 | #include <set> |
| 71 | #include <stdio.h> |
| 72 | #include <stdlib.h> |
| 73 | #include <string.h> |
| 74 | #include <string> |
| 75 | #include <system_error> |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 76 | |
| 77 | namespace lldb_private { |
| 78 | class Address; |
| 79 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | |
| 81 | using namespace lldb; |
| 82 | using namespace lldb_private; |
| 83 | |
Caroline Tice | ebc1bb2 | 2010-06-30 16:22:25 +0000 | [diff] [blame] | 84 | static lldb::user_id_t g_unique_id = 1; |
Zachary Turner | 7c2896a | 2014-10-24 22:06:29 +0000 | [diff] [blame] | 85 | static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024; |
Caroline Tice | ebc1bb2 | 2010-06-30 16:22:25 +0000 | [diff] [blame] | 86 | |
Greg Clayton | 1b65488 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 87 | #pragma mark Static Functions |
| 88 | |
Greg Clayton | 1b65488 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 89 | typedef std::vector<DebuggerSP> DebuggerList; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | static std::recursive_mutex *g_debugger_list_mutex_ptr = |
| 91 | nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain |
| 92 | static DebuggerList *g_debugger_list_ptr = |
| 93 | nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain |
Greg Clayton | e372b98 | 2011-11-21 21:44:34 +0000 | [diff] [blame] | 94 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 95 | static constexpr OptionEnumValueElement g_show_disassembly_enum_values[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | {Debugger::eStopDisassemblyTypeNever, "never", |
| 97 | "Never show disassembly when displaying a stop context."}, |
| 98 | {Debugger::eStopDisassemblyTypeNoDebugInfo, "no-debuginfo", |
| 99 | "Show disassembly when there is no debug information."}, |
| 100 | {Debugger::eStopDisassemblyTypeNoSource, "no-source", |
| 101 | "Show disassembly when there is no source information, or the source file " |
| 102 | "is missing when displaying a stop context."}, |
| 103 | {Debugger::eStopDisassemblyTypeAlways, "always", |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 104 | "Always show disassembly when displaying a stop context."} }; |
Greg Clayton | e372b98 | 2011-11-21 21:44:34 +0000 | [diff] [blame] | 105 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 106 | static constexpr OptionEnumValueElement g_language_enumerators[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | {eScriptLanguageNone, "none", "Disable scripting languages."}, |
| 108 | {eScriptLanguagePython, "python", |
| 109 | "Select python as the default scripting language."}, |
| 110 | {eScriptLanguageDefault, "default", |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 111 | "Select the lldb default as the default scripting language."} }; |
Greg Clayton | e372b98 | 2011-11-21 21:44:34 +0000 | [diff] [blame] | 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | #define MODULE_WITH_FUNC \ |
| 114 | "{ " \ |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 115 | "${module.file.basename}{`${function.name-with-args}" \ |
| 116 | "{${frame.no-debug}${function.pc-offset}}}}" |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 117 | |
| 118 | #define MODULE_WITH_FUNC_NO_ARGS \ |
| 119 | "{ " \ |
| 120 | "${module.file.basename}{`${function.name-without-args}" \ |
| 121 | "{${frame.no-debug}${function.pc-offset}}}}" |
| 122 | |
Adrian Prantl | 4954f6a | 2018-09-05 23:52:08 +0000 | [diff] [blame] | 123 | #define FILE_AND_LINE \ |
Jonas Devlieghere | f9626f2 | 2019-06-17 19:53:11 +0000 | [diff] [blame] | 124 | "{ at ${ansi.fg.cyan}${line.file.basename}${ansi.normal}" \ |
| 125 | ":${ansi.fg.yellow}${line.number}${ansi.normal}" \ |
| 126 | "{:${ansi.fg.yellow}${line.column}${ansi.normal}}}" |
| 127 | |
Jason Molenda | 6ab659a | 2015-07-29 00:42:47 +0000 | [diff] [blame] | 128 | #define IS_OPTIMIZED "{${function.is-optimized} [opt]}" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 129 | |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 +0000 | [diff] [blame] | 130 | #define IS_ARTIFICIAL "{${frame.is-artificial} [artificial]}" |
| 131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | #define DEFAULT_THREAD_FORMAT \ |
| 133 | "thread #${thread.index}: tid = ${thread.id%tid}" \ |
| 134 | "{, ${frame.pc}}" MODULE_WITH_FUNC FILE_AND_LINE \ |
Jonas Devlieghere | f9626f2 | 2019-06-17 19:53:11 +0000 | [diff] [blame] | 135 | "{, name = ${ansi.fg.green}'${thread.name}'${ansi.normal}}" \ |
| 136 | "{, queue = ${ansi.fg.green}'${thread.queue}'${ansi.normal}}" \ |
| 137 | "{, activity = " \ |
| 138 | "${ansi.fg.green}'${thread.info.activity.name}'${ansi.normal}}" \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | "{, ${thread.info.trace_messages} messages}" \ |
Jonas Devlieghere | f9626f2 | 2019-06-17 19:53:11 +0000 | [diff] [blame] | 140 | "{, stop reason = ${ansi.fg.red}${thread.stop-reason}${ansi.normal}}" \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | "{\\nReturn value: ${thread.return-value}}" \ |
| 142 | "{\\nCompleted expression: ${thread.completed-expression}}" \ |
| 143 | "\\n" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 144 | |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 145 | #define DEFAULT_THREAD_STOP_FORMAT \ |
| 146 | "thread #${thread.index}{, name = '${thread.name}'}" \ |
Jonas Devlieghere | f9626f2 | 2019-06-17 19:53:11 +0000 | [diff] [blame] | 147 | "{, queue = ${ansi.fg.green}'${thread.queue}'${ansi.normal}}" \ |
| 148 | "{, activity = " \ |
| 149 | "${ansi.fg.green}'${thread.info.activity.name}'${ansi.normal}}" \ |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 150 | "{, ${thread.info.trace_messages} messages}" \ |
Jonas Devlieghere | f9626f2 | 2019-06-17 19:53:11 +0000 | [diff] [blame] | 151 | "{, stop reason = ${ansi.fg.red}${thread.stop-reason}${ansi.normal}}" \ |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 152 | "{\\nReturn value: ${thread.return-value}}" \ |
| 153 | "{\\nCompleted expression: ${thread.completed-expression}}" \ |
| 154 | "\\n" |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | #define DEFAULT_FRAME_FORMAT \ |
Jonas Devlieghere | f9626f2 | 2019-06-17 19:53:11 +0000 | [diff] [blame] | 157 | "frame #${frame.index}: " \ |
| 158 | "${ansi.fg.yellow}${frame.pc}${ansi.normal}" MODULE_WITH_FUNC FILE_AND_LINE \ |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 +0000 | [diff] [blame] | 159 | IS_OPTIMIZED IS_ARTIFICIAL "\\n" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 160 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 161 | #define DEFAULT_FRAME_FORMAT_NO_ARGS \ |
Jonas Devlieghere | f9626f2 | 2019-06-17 19:53:11 +0000 | [diff] [blame] | 162 | "frame #${frame.index}: " \ |
| 163 | "${ansi.fg.yellow}${frame.pc}${ansi.normal}" MODULE_WITH_FUNC_NO_ARGS \ |
| 164 | FILE_AND_LINE IS_OPTIMIZED IS_ARTIFICIAL "\\n" |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 165 | |
Jason Molenda | c980fa9 | 2015-02-13 23:24:21 +0000 | [diff] [blame] | 166 | // Three parts to this disassembly format specification: |
| 167 | // 1. If this is a new function/symbol (no previous symbol/function), print |
| 168 | // dylib`funcname:\n |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | // 2. If this is a symbol context change (different from previous |
| 170 | // symbol/function), print |
Jason Molenda | c980fa9 | 2015-02-13 23:24:21 +0000 | [diff] [blame] | 171 | // dylib`funcname:\n |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | // 3. print |
| 173 | // address <+offset>: |
| 174 | #define DEFAULT_DISASSEMBLY_FORMAT \ |
| 175 | "{${function.initial-function}{${module.file.basename}`}{${function.name-" \ |
Jonas Devlieghere | e43be15 | 2018-10-01 13:20:15 +0000 | [diff] [blame] | 176 | "without-args}}:\\n}{${function.changed}\\n{${module.file.basename}`}{${" \ |
| 177 | "function.name-without-args}}:\\n}{${current-pc-arrow} " \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | "}${addr-file-or-load}{ " \ |
| 179 | "<${function.concrete-only-addr-offset-no-padding}>}: " |
Jason Molenda | c980fa9 | 2015-02-13 23:24:21 +0000 | [diff] [blame] | 180 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 181 | // gdb's disassembly format can be emulated with ${current-pc-arrow}${addr- |
| 182 | // file-or-load}{ <${function.name-without-args}${function.concrete-only-addr- |
| 183 | // offset-no-padding}>}: |
Jason Molenda | c980fa9 | 2015-02-13 23:24:21 +0000 | [diff] [blame] | 184 | |
| 185 | // lldb's original format for disassembly would look like this format string - |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 186 | // {${function.initial-function}{${module.file.basename}`}{${function.name- |
| 187 | // without- |
| 188 | // args}}:\n}{${function.changed}\n{${module.file.basename}`}{${function.name- |
| 189 | // without-args}}:\n}{${current-pc-arrow} }{${addr-file-or-load}}: |
Jason Molenda | c980fa9 | 2015-02-13 23:24:21 +0000 | [diff] [blame] | 190 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 191 | static constexpr OptionEnumValueElement s_stop_show_column_values[] = { |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 192 | {eStopShowColumnAnsiOrCaret, "ansi-or-caret", |
| 193 | "Highlight the stop column with ANSI terminal codes when color/ANSI mode " |
| 194 | "is enabled; otherwise, fall back to using a text-only caret (^) as if " |
| 195 | "\"caret-only\" mode was selected."}, |
| 196 | {eStopShowColumnAnsi, "ansi", "Highlight the stop column with ANSI " |
| 197 | "terminal codes when running LLDB with " |
| 198 | "color/ANSI enabled."}, |
| 199 | {eStopShowColumnCaret, "caret", |
| 200 | "Highlight the stop column with a caret character (^) underneath the stop " |
| 201 | "column. This method introduces a new line in source listings that " |
| 202 | "display thread stop locations."}, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 203 | {eStopShowColumnNone, "none", "Do not highlight the stop column."}}; |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 204 | |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 205 | #define LLDB_PROPERTIES_debugger |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame] | 206 | #include "CoreProperties.inc" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | enum { |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 209 | #define LLDB_PROPERTIES_debugger |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame] | 210 | #include "CorePropertiesEnum.inc" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Eugene Zelenko | df37055 | 2016-03-02 02:18:18 +0000 | [diff] [blame] | 213 | LoadPluginCallbackType Debugger::g_load_plugin_callback = nullptr; |
Greg Clayton | 4c05410 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 214 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 215 | Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx, |
| 216 | VarSetOperationType op, |
| 217 | llvm::StringRef property_path, |
| 218 | llvm::StringRef value) { |
Zachary Turner | 31d97a5 | 2016-11-17 18:08:12 +0000 | [diff] [blame] | 219 | bool is_load_script = (property_path == "target.load-script-from-symbol-file"); |
| 220 | bool is_escape_non_printables = (property_path == "escape-non-printables"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | TargetSP target_sp; |
| 222 | LoadScriptFromSymFile load_script_old_value; |
| 223 | if (is_load_script && exe_ctx->GetTargetSP()) { |
| 224 | target_sp = exe_ctx->GetTargetSP(); |
| 225 | load_script_old_value = |
| 226 | target_sp->TargetProperties::GetLoadScriptFromSymbolFile(); |
| 227 | } |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 228 | Status error(Properties::SetPropertyValue(exe_ctx, op, property_path, value)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | if (error.Success()) { |
| 230 | // FIXME it would be nice to have "on-change" callbacks for properties |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 231 | if (property_path == g_debugger_properties[ePropertyPrompt].name) { |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 232 | llvm::StringRef new_prompt = GetPrompt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes( |
| 234 | new_prompt, GetUseColor()); |
| 235 | if (str.length()) |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 236 | new_prompt = str; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | GetCommandInterpreter().UpdatePrompt(new_prompt); |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 238 | auto bytes = llvm::make_unique<EventDataBytes>(new_prompt); |
| 239 | auto prompt_change_event_sp = std::make_shared<Event>( |
| 240 | CommandInterpreter::eBroadcastBitResetPrompt, bytes.release()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | GetCommandInterpreter().BroadcastEvent(prompt_change_event_sp); |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 242 | } else if (property_path == g_debugger_properties[ePropertyUseColor].name) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | // use-color changed. Ping the prompt so it can reset the ansi terminal |
| 244 | // codes. |
| 245 | SetPrompt(GetPrompt()); |
| 246 | } else if (is_load_script && target_sp && |
| 247 | load_script_old_value == eLoadScriptFromSymFileWarn) { |
| 248 | if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == |
| 249 | eLoadScriptFromSymFileTrue) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 250 | std::list<Status> errors; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 251 | StreamString feedback_stream; |
| 252 | if (!target_sp->LoadScriptingResources(errors, &feedback_stream)) { |
| 253 | StreamFileSP stream_sp(GetErrorFile()); |
| 254 | if (stream_sp) { |
| 255 | for (auto error : errors) { |
| 256 | stream_sp->Printf("%s\n", error.AsCString()); |
| 257 | } |
| 258 | if (feedback_stream.GetSize()) |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 259 | stream_sp->PutCString(feedback_stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | } else if (is_escape_non_printables) { |
| 264 | DataVisualization::ForceUpdate(); |
| 265 | } |
| 266 | } |
| 267 | return error; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | bool Debugger::GetAutoConfirm() const { |
| 271 | const uint32_t idx = ePropertyAutoConfirm; |
| 272 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 273 | nullptr, idx, g_debugger_properties[idx].default_uint_value != 0); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 276 | const FormatEntity::Entry *Debugger::GetDisassemblyFormat() const { |
| 277 | const uint32_t idx = ePropertyDisassemblyFormat; |
| 278 | return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | const FormatEntity::Entry *Debugger::GetFrameFormat() const { |
| 282 | const uint32_t idx = ePropertyFrameFormat; |
| 283 | return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 286 | const FormatEntity::Entry *Debugger::GetFrameFormatUnique() const { |
| 287 | const uint32_t idx = ePropertyFrameFormatUnique; |
| 288 | return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx); |
| 289 | } |
| 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | bool Debugger::GetNotifyVoid() const { |
| 292 | const uint32_t idx = ePropertyNotiftVoid; |
| 293 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 294 | nullptr, idx, g_debugger_properties[idx].default_uint_value != 0); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 297 | llvm::StringRef Debugger::GetPrompt() const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | const uint32_t idx = ePropertyPrompt; |
| 299 | return m_collection_sp->GetPropertyAtIndexAsString( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 300 | nullptr, idx, g_debugger_properties[idx].default_cstr_value); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 303 | void Debugger::SetPrompt(llvm::StringRef p) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 304 | const uint32_t idx = ePropertyPrompt; |
| 305 | m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p); |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 306 | llvm::StringRef new_prompt = GetPrompt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 307 | std::string str = |
| 308 | lldb_utility::ansi::FormatAnsiTerminalCodes(new_prompt, GetUseColor()); |
| 309 | if (str.length()) |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 310 | new_prompt = str; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | GetCommandInterpreter().UpdatePrompt(new_prompt); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 314 | llvm::StringRef Debugger::GetReproducerPath() const { |
| 315 | auto &r = repro::Reproducer::Instance(); |
| 316 | return r.GetReproducerPath().GetCString(); |
| 317 | } |
| 318 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 319 | const FormatEntity::Entry *Debugger::GetThreadFormat() const { |
| 320 | const uint32_t idx = ePropertyThreadFormat; |
| 321 | return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 324 | const FormatEntity::Entry *Debugger::GetThreadStopFormat() const { |
| 325 | const uint32_t idx = ePropertyThreadStopFormat; |
| 326 | return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx); |
| 327 | } |
| 328 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | lldb::ScriptLanguage Debugger::GetScriptLanguage() const { |
| 330 | const uint32_t idx = ePropertyScriptLanguage; |
| 331 | return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 332 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Michael Sartain | c3ce7f27 | 2013-05-23 20:47:45 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | bool Debugger::SetScriptLanguage(lldb::ScriptLanguage script_lang) { |
| 336 | const uint32_t idx = ePropertyScriptLanguage; |
| 337 | return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, |
| 338 | script_lang); |
Michael Sartain | c3ce7f27 | 2013-05-23 20:47:45 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | uint32_t Debugger::GetTerminalWidth() const { |
| 342 | const uint32_t idx = ePropertyTerminalWidth; |
| 343 | return m_collection_sp->GetPropertyAtIndexAsSInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 344 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | bool Debugger::SetTerminalWidth(uint32_t term_width) { |
| 348 | const uint32_t idx = ePropertyTerminalWidth; |
| 349 | return m_collection_sp->SetPropertyAtIndexAsSInt64(nullptr, idx, term_width); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | bool Debugger::GetUseExternalEditor() const { |
| 353 | const uint32_t idx = ePropertyUseExternalEditor; |
| 354 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 355 | nullptr, idx, g_debugger_properties[idx].default_uint_value != 0); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 356 | } |
Greg Clayton | e372b98 | 2011-11-21 21:44:34 +0000 | [diff] [blame] | 357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 358 | bool Debugger::SetUseExternalEditor(bool b) { |
| 359 | const uint32_t idx = ePropertyUseExternalEditor; |
| 360 | return m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); |
Enrico Granata | ebdc1ac | 2014-11-05 21:20:48 +0000 | [diff] [blame] | 361 | } |
Enrico Granata | 553fad5 | 2013-10-25 23:09:40 +0000 | [diff] [blame] | 362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 363 | bool Debugger::GetUseColor() const { |
| 364 | const uint32_t idx = ePropertyUseColor; |
| 365 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 366 | nullptr, idx, g_debugger_properties[idx].default_uint_value != 0); |
Enrico Granata | 553fad5 | 2013-10-25 23:09:40 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | bool Debugger::SetUseColor(bool b) { |
| 370 | const uint32_t idx = ePropertyUseColor; |
| 371 | bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); |
| 372 | SetPrompt(GetPrompt()); |
| 373 | return ret; |
Sean Callanan | 6681041 | 2015-10-19 23:11:07 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 376 | bool Debugger::GetHighlightSource() const { |
| 377 | const uint32_t idx = ePropertyHighlightSource; |
| 378 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 379 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 382 | StopShowColumn Debugger::GetStopShowColumn() const { |
| 383 | const uint32_t idx = ePropertyStopShowColumn; |
| 384 | return (lldb::StopShowColumn)m_collection_sp->GetPropertyAtIndexAsEnumeration( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 385 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Raphael Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 388 | llvm::StringRef Debugger::GetStopShowColumnAnsiPrefix() const { |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 389 | const uint32_t idx = ePropertyStopShowColumnAnsiPrefix; |
Raphael Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 390 | return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, ""); |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Raphael Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 393 | llvm::StringRef Debugger::GetStopShowColumnAnsiSuffix() const { |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 394 | const uint32_t idx = ePropertyStopShowColumnAnsiSuffix; |
Raphael Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 395 | return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, ""); |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | uint32_t Debugger::GetStopSourceLineCount(bool before) const { |
| 399 | const uint32_t idx = |
| 400 | before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter; |
| 401 | return m_collection_sp->GetPropertyAtIndexAsSInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 402 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Sean Callanan | 6681041 | 2015-10-19 23:11:07 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | Debugger::StopDisassemblyType Debugger::GetStopDisassemblyDisplay() const { |
| 406 | const uint32_t idx = ePropertyStopDisassemblyDisplay; |
| 407 | return (Debugger::StopDisassemblyType) |
| 408 | m_collection_sp->GetPropertyAtIndexAsEnumeration( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 409 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Sean Callanan | 6681041 | 2015-10-19 23:11:07 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 412 | uint32_t Debugger::GetDisassemblyLineCount() const { |
| 413 | const uint32_t idx = ePropertyStopDisassemblyCount; |
| 414 | return m_collection_sp->GetPropertyAtIndexAsSInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 415 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Sean Callanan | 6681041 | 2015-10-19 23:11:07 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | bool Debugger::GetAutoOneLineSummaries() const { |
| 419 | const uint32_t idx = ePropertyAutoOneLineSummaries; |
| 420 | return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true); |
Sean Callanan | 6681041 | 2015-10-19 23:11:07 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | bool Debugger::GetEscapeNonPrintables() const { |
| 424 | const uint32_t idx = ePropertyEscapeNonPrintables; |
| 425 | return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true); |
| 426 | } |
| 427 | |
| 428 | bool Debugger::GetAutoIndent() const { |
| 429 | const uint32_t idx = ePropertyAutoIndent; |
| 430 | return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true); |
| 431 | } |
| 432 | |
| 433 | bool Debugger::SetAutoIndent(bool b) { |
| 434 | const uint32_t idx = ePropertyAutoIndent; |
| 435 | return m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); |
| 436 | } |
| 437 | |
| 438 | bool Debugger::GetPrintDecls() const { |
| 439 | const uint32_t idx = ePropertyPrintDecls; |
| 440 | return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true); |
| 441 | } |
| 442 | |
| 443 | bool Debugger::SetPrintDecls(bool b) { |
| 444 | const uint32_t idx = ePropertyPrintDecls; |
| 445 | return m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); |
| 446 | } |
| 447 | |
| 448 | uint32_t Debugger::GetTabSize() const { |
| 449 | const uint32_t idx = ePropertyTabSize; |
| 450 | return m_collection_sp->GetPropertyAtIndexAsUInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 451 | nullptr, idx, g_debugger_properties[idx].default_uint_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | bool Debugger::SetTabSize(uint32_t tab_size) { |
| 455 | const uint32_t idx = ePropertyTabSize; |
| 456 | return m_collection_sp->SetPropertyAtIndexAsUInt64(nullptr, idx, tab_size); |
Sean Callanan | 6681041 | 2015-10-19 23:11:07 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Greg Clayton | 1b65488 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 459 | #pragma mark Debugger |
| 460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | // const DebuggerPropertiesSP & |
| 462 | // Debugger::GetSettings() const |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 463 | //{ |
| 464 | // return m_properties_sp; |
| 465 | //} |
| 466 | // |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 467 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 468 | void Debugger::Initialize(LoadPluginCallbackType load_plugin_callback) { |
| 469 | assert(g_debugger_list_ptr == nullptr && |
| 470 | "Debugger::Initialize called more than once!"); |
| 471 | g_debugger_list_mutex_ptr = new std::recursive_mutex(); |
| 472 | g_debugger_list_ptr = new DebuggerList(); |
| 473 | g_load_plugin_callback = load_plugin_callback; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 476 | void Debugger::Terminate() { |
| 477 | assert(g_debugger_list_ptr && |
| 478 | "Debugger::Terminate called without a matching Debugger::Initialize!"); |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 479 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 480 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 481 | // Clear our master list of debugger objects |
Greg Clayton | 6c42e06 | 2016-05-26 16:51:23 +0000 | [diff] [blame] | 482 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 483 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 484 | for (const auto &debugger : *g_debugger_list_ptr) |
| 485 | debugger->Clear(); |
| 486 | g_debugger_list_ptr->clear(); |
Greg Clayton | 6c42e06 | 2016-05-26 16:51:23 +0000 | [diff] [blame] | 487 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 491 | void Debugger::SettingsInitialize() { Target::SettingsInitialize(); } |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 492 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | void Debugger::SettingsTerminate() { Target::SettingsTerminate(); } |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 494 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 495 | bool Debugger::LoadPlugin(const FileSpec &spec, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 496 | if (g_load_plugin_callback) { |
| 497 | llvm::sys::DynamicLibrary dynlib = |
| 498 | g_load_plugin_callback(shared_from_this(), spec, error); |
| 499 | if (dynlib.isValid()) { |
| 500 | m_loaded_plugins.push_back(dynlib); |
| 501 | return true; |
Enrico Granata | e743c78 | 2013-04-24 21:29:08 +0000 | [diff] [blame] | 502 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 503 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 504 | // The g_load_plugin_callback is registered in SBDebugger::Initialize() and |
| 505 | // if the public API layer isn't available (code is linking against all of |
| 506 | // the internal LLDB static libraries), then we can't load plugins |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 507 | error.SetErrorString("Public API layer is not available"); |
| 508 | } |
| 509 | return false; |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 512 | static FileSystem::EnumerateDirectoryResult |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 513 | LoadPluginCallback(void *baton, llvm::sys::fs::file_type ft, |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 514 | llvm::StringRef path) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 515 | Status error; |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 516 | |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 517 | static ConstString g_dylibext(".dylib"); |
| 518 | static ConstString g_solibext(".so"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | |
| 520 | if (!baton) |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 521 | return FileSystem::eEnumerateDirectoryResultQuit; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 522 | |
| 523 | Debugger *debugger = (Debugger *)baton; |
| 524 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 525 | namespace fs = llvm::sys::fs; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 526 | // If we have a regular file, a symbolic link or unknown file type, try and |
| 527 | // process the file. We must handle unknown as sometimes the directory |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 528 | // enumeration might be enumerating a file system that doesn't have correct |
| 529 | // file type information. |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 530 | if (ft == fs::file_type::regular_file || ft == fs::file_type::symlink_file || |
| 531 | ft == fs::file_type::type_unknown) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 532 | FileSpec plugin_file_spec(path); |
| 533 | FileSystem::Instance().Resolve(plugin_file_spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 534 | |
| 535 | if (plugin_file_spec.GetFileNameExtension() != g_dylibext && |
| 536 | plugin_file_spec.GetFileNameExtension() != g_solibext) { |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 537 | return FileSystem::eEnumerateDirectoryResultNext; |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 538 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 540 | Status plugin_load_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 541 | debugger->LoadPlugin(plugin_file_spec, plugin_load_error); |
| 542 | |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 543 | return FileSystem::eEnumerateDirectoryResultNext; |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 544 | } else if (ft == fs::file_type::directory_file || |
| 545 | ft == fs::file_type::symlink_file || |
| 546 | ft == fs::file_type::type_unknown) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 547 | // Try and recurse into anything that a directory or symbolic link. We must |
| 548 | // also do this for unknown as sometimes the directory enumeration might be |
| 549 | // enumerating a file system that doesn't have correct file type |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | // information. |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 551 | return FileSystem::eEnumerateDirectoryResultEnter; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 554 | return FileSystem::eEnumerateDirectoryResultNext; |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 557 | void Debugger::InstanceInitialize() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | const bool find_directories = true; |
| 559 | const bool find_files = true; |
| 560 | const bool find_other = true; |
| 561 | char dir_path[PATH_MAX]; |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame] | 562 | if (FileSpec dir_spec = HostInfo::GetSystemPluginDir()) { |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 563 | if (FileSystem::Instance().Exists(dir_spec) && |
| 564 | dir_spec.GetPath(dir_path, sizeof(dir_path))) { |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 565 | FileSystem::Instance().EnumerateDirectory(dir_path, find_directories, |
| 566 | find_files, find_other, |
| 567 | LoadPluginCallback, this); |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 568 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 569 | } |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 570 | |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame] | 571 | if (FileSpec dir_spec = HostInfo::GetUserPluginDir()) { |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 572 | if (FileSystem::Instance().Exists(dir_spec) && |
| 573 | dir_spec.GetPath(dir_path, sizeof(dir_path))) { |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 574 | FileSystem::Instance().EnumerateDirectory(dir_path, find_directories, |
| 575 | find_files, find_other, |
| 576 | LoadPluginCallback, this); |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 577 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | PluginManager::DebuggerInitialize(*this); |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 583 | DebuggerSP Debugger::CreateInstance(lldb::LogOutputCallback log_callback, |
| 584 | void *baton) { |
| 585 | DebuggerSP debugger_sp(new Debugger(log_callback, baton)); |
| 586 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 587 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 588 | g_debugger_list_ptr->push_back(debugger_sp); |
| 589 | } |
| 590 | debugger_sp->InstanceInitialize(); |
| 591 | return debugger_sp; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 594 | void Debugger::Destroy(DebuggerSP &debugger_sp) { |
| 595 | if (!debugger_sp) |
| 596 | return; |
| 597 | |
| 598 | debugger_sp->Clear(); |
| 599 | |
| 600 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 601 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 602 | DebuggerList::iterator pos, end = g_debugger_list_ptr->end(); |
| 603 | for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) { |
| 604 | if ((*pos).get() == debugger_sp.get()) { |
| 605 | g_debugger_list_ptr->erase(pos); |
Caroline Tice | e02657b | 2011-01-22 01:02:07 +0000 | [diff] [blame] | 606 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 607 | } |
Caroline Tice | e02657b | 2011-01-22 01:02:07 +0000 | [diff] [blame] | 608 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 609 | } |
Caroline Tice | e02657b | 2011-01-22 01:02:07 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Greg Clayton | 4d122c4 | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 612 | DebuggerSP |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 613 | Debugger::FindDebuggerWithInstanceName(ConstString instance_name) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 614 | DebuggerSP debugger_sp; |
| 615 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 616 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 617 | DebuggerList::iterator pos, end = g_debugger_list_ptr->end(); |
| 618 | for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) { |
| 619 | if ((*pos)->m_instance_name == instance_name) { |
| 620 | debugger_sp = *pos; |
| 621 | break; |
| 622 | } |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 623 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 624 | } |
| 625 | return debugger_sp; |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 626 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 627 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 628 | TargetSP Debugger::FindTargetWithProcessID(lldb::pid_t pid) { |
| 629 | TargetSP target_sp; |
| 630 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 631 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 632 | DebuggerList::iterator pos, end = g_debugger_list_ptr->end(); |
| 633 | for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) { |
| 634 | target_sp = (*pos)->GetTargetList().FindTargetWithProcessID(pid); |
| 635 | if (target_sp) |
| 636 | break; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 637 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 638 | } |
| 639 | return target_sp; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 642 | TargetSP Debugger::FindTargetWithProcess(Process *process) { |
| 643 | TargetSP target_sp; |
| 644 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 645 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 646 | DebuggerList::iterator pos, end = g_debugger_list_ptr->end(); |
| 647 | for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) { |
| 648 | target_sp = (*pos)->GetTargetList().FindTargetWithProcess(process); |
| 649 | if (target_sp) |
| 650 | break; |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 651 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 652 | } |
| 653 | return target_sp; |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) |
| 657 | : UserID(g_unique_id++), |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 658 | Properties(std::make_shared<OptionValueProperties>()), |
| 659 | m_input_file_sp(std::make_shared<StreamFile>(stdin, false)), |
| 660 | m_output_file_sp(std::make_shared<StreamFile>(stdout, false)), |
| 661 | m_error_file_sp(std::make_shared<StreamFile>(stderr, false)), |
Jonas Devlieghere | d77c2e0 | 2019-03-02 00:20:26 +0000 | [diff] [blame] | 662 | m_input_recorder(nullptr), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 663 | m_broadcaster_manager_sp(BroadcasterManager::MakeBroadcasterManager()), |
| 664 | m_terminal_state(), m_target_list(*this), m_platform_list(), |
| 665 | m_listener_sp(Listener::MakeListener("lldb.Debugger")), |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 666 | m_source_manager_up(), m_source_file_cache(), |
Jonas Devlieghere | f20dd1d | 2019-04-26 20:03:22 +0000 | [diff] [blame] | 667 | m_command_interpreter_up( |
| 668 | llvm::make_unique<CommandInterpreter>(*this, false)), |
Jonas Devlieghere | 2b29b43 | 2019-04-26 22:43:16 +0000 | [diff] [blame] | 669 | m_script_interpreter_sp(), m_input_reader_stack(), m_instance_name(), |
| 670 | m_loaded_plugins(), m_event_handler_thread(), m_io_handler_thread(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 671 | m_sync_broadcaster(nullptr, "lldb.debugger.sync"), |
| 672 | m_forward_listener_sp(), m_clear_once() { |
| 673 | char instance_cstr[256]; |
| 674 | snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID()); |
| 675 | m_instance_name.SetCString(instance_cstr); |
| 676 | if (log_callback) |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 677 | m_log_callback_stream_sp = |
| 678 | std::make_shared<StreamCallback>(log_callback, baton); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 679 | m_command_interpreter_up->Initialize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 680 | // Always add our default platform to the platform list |
| 681 | PlatformSP default_platform_sp(Platform::GetHostPlatform()); |
| 682 | assert(default_platform_sp); |
| 683 | m_platform_list.Append(default_platform_sp, true); |
Michael Sartain | c3ce7f27 | 2013-05-23 20:47:45 +0000 | [diff] [blame] | 684 | |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 685 | m_collection_sp->Initialize(g_debugger_properties); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 686 | m_collection_sp->AppendProperty( |
| 687 | ConstString("target"), |
| 688 | ConstString("Settings specify to debugging targets."), true, |
| 689 | Target::GetGlobalProperties()->GetValueProperties()); |
| 690 | m_collection_sp->AppendProperty( |
| 691 | ConstString("platform"), ConstString("Platform settings."), true, |
| 692 | Platform::GetGlobalPlatformProperties()->GetValueProperties()); |
Adrian Prantl | 235354be | 2018-03-02 22:42:44 +0000 | [diff] [blame] | 693 | m_collection_sp->AppendProperty( |
Adrian Prantl | 3cd29bc | 2018-03-10 01:11:25 +0000 | [diff] [blame] | 694 | ConstString("symbols"), ConstString("Symbol lookup and cache settings."), |
| 695 | true, ModuleList::GetGlobalModuleListProperties().GetValueProperties()); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 696 | if (m_command_interpreter_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 697 | m_collection_sp->AppendProperty( |
| 698 | ConstString("interpreter"), |
| 699 | ConstString("Settings specify to the debugger's command interpreter."), |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 700 | true, m_command_interpreter_up->GetValueProperties()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 701 | } |
| 702 | OptionValueSInt64 *term_width = |
| 703 | m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64( |
| 704 | nullptr, ePropertyTerminalWidth); |
| 705 | term_width->SetMinimumValue(10); |
| 706 | term_width->SetMaximumValue(1024); |
| 707 | |
| 708 | // Turn off use-color if this is a dumb terminal. |
| 709 | const char *term = getenv("TERM"); |
| 710 | if (term && !strcmp(term, "dumb")) |
| 711 | SetUseColor(false); |
Raphael Isemann | eca9ce1 | 2018-08-27 15:16:25 +0000 | [diff] [blame] | 712 | // Turn off use-color if we don't write to a terminal with color support. |
| 713 | if (!m_output_file_sp->GetFile().GetIsTerminalWithColors()) |
| 714 | SetUseColor(false); |
David Bolvansky | 8aa2361 | 2018-09-05 22:06:58 +0000 | [diff] [blame] | 715 | |
| 716 | #if defined(_WIN32) && defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
| 717 | // Enabling use of ANSI color codes because LLDB is using them to highlight |
| 718 | // text. |
| 719 | llvm::sys::Process::UseANSIEscapeCodes(true); |
David Bolvansky | 8aa2361 | 2018-09-05 22:06:58 +0000 | [diff] [blame] | 720 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | Debugger::~Debugger() { Clear(); } |
Jim Ingham | 8314c52 | 2011-09-15 21:36:42 +0000 | [diff] [blame] | 724 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 725 | void Debugger::Clear() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 726 | // Make sure we call this function only once. With the C++ global destructor |
| 727 | // chain having a list of debuggers and with code that can be running on |
| 728 | // other threads, we need to ensure this doesn't happen multiple times. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 729 | // |
| 730 | // The following functions call Debugger::Clear(): |
| 731 | // Debugger::~Debugger(); |
| 732 | // static void Debugger::Destroy(lldb::DebuggerSP &debugger_sp); |
| 733 | // static void Debugger::Terminate(); |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 734 | llvm::call_once(m_clear_once, [this]() { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 735 | ClearIOHandlers(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 736 | StopIOHandlerThread(); |
| 737 | StopEventHandlerThread(); |
| 738 | m_listener_sp->Clear(); |
| 739 | int num_targets = m_target_list.GetNumTargets(); |
| 740 | for (int i = 0; i < num_targets; i++) { |
| 741 | TargetSP target_sp(m_target_list.GetTargetAtIndex(i)); |
| 742 | if (target_sp) { |
| 743 | ProcessSP process_sp(target_sp->GetProcessSP()); |
| 744 | if (process_sp) |
| 745 | process_sp->Finalize(); |
| 746 | target_sp->Destroy(); |
| 747 | } |
Greg Clayton | 577508d | 2014-06-20 00:23:57 +0000 | [diff] [blame] | 748 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 749 | m_broadcaster_manager_sp->Clear(); |
| 750 | |
| 751 | // Close the input file _before_ we close the input read communications |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 752 | // class as it does NOT own the input file, our m_input_file does. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | m_terminal_state.Clear(); |
| 754 | if (m_input_file_sp) |
| 755 | m_input_file_sp->GetFile().Close(); |
| 756 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 757 | m_command_interpreter_up->Clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | }); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 761 | bool Debugger::GetCloseInputOnEOF() const { |
| 762 | // return m_input_comm.GetCloseOnEOF(); |
| 763 | return false; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 766 | void Debugger::SetCloseInputOnEOF(bool b) { |
| 767 | // m_input_comm.SetCloseOnEOF(b); |
| 768 | } |
| 769 | |
| 770 | bool Debugger::GetAsyncExecution() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 771 | return !m_command_interpreter_up->GetSynchronous(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void Debugger::SetAsyncExecution(bool async_execution) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 775 | m_command_interpreter_up->SetSynchronous(!async_execution); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Jonas Devlieghere | d77c2e0 | 2019-03-02 00:20:26 +0000 | [diff] [blame] | 778 | repro::DataRecorder *Debugger::GetInputRecorder() { return m_input_recorder; } |
| 779 | |
| 780 | void Debugger::SetInputFileHandle(FILE *fh, bool tranfer_ownership, |
| 781 | repro::DataRecorder *recorder) { |
| 782 | m_input_recorder = recorder; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 783 | if (m_input_file_sp) |
| 784 | m_input_file_sp->GetFile().SetStream(fh, tranfer_ownership); |
| 785 | else |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 786 | m_input_file_sp = std::make_shared<StreamFile>(fh, tranfer_ownership); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | |
| 788 | File &in_file = m_input_file_sp->GetFile(); |
| 789 | if (!in_file.IsValid()) |
| 790 | in_file.SetStream(stdin, true); |
| 791 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 792 | // Save away the terminal state if that is relevant, so that we can restore |
| 793 | // it in RestoreInputState. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 794 | SaveInputTerminalState(); |
| 795 | } |
| 796 | |
| 797 | void Debugger::SetOutputFileHandle(FILE *fh, bool tranfer_ownership) { |
| 798 | if (m_output_file_sp) |
| 799 | m_output_file_sp->GetFile().SetStream(fh, tranfer_ownership); |
| 800 | else |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 801 | m_output_file_sp = std::make_shared<StreamFile>(fh, tranfer_ownership); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 802 | |
| 803 | File &out_file = m_output_file_sp->GetFile(); |
| 804 | if (!out_file.IsValid()) |
| 805 | out_file.SetStream(stdout, false); |
| 806 | |
Jonas Devlieghere | 2b29b43 | 2019-04-26 22:43:16 +0000 | [diff] [blame] | 807 | // Do not create the ScriptInterpreter just for setting the output file |
| 808 | // handle as the constructor will know how to do the right thing on its own. |
| 809 | if (ScriptInterpreter *script_interpreter = |
| 810 | GetScriptInterpreter(/*can_create=*/false)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 811 | script_interpreter->ResetOutputFileHandle(fh); |
| 812 | } |
| 813 | |
| 814 | void Debugger::SetErrorFileHandle(FILE *fh, bool tranfer_ownership) { |
| 815 | if (m_error_file_sp) |
| 816 | m_error_file_sp->GetFile().SetStream(fh, tranfer_ownership); |
| 817 | else |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 818 | m_error_file_sp = std::make_shared<StreamFile>(fh, tranfer_ownership); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 819 | |
| 820 | File &err_file = m_error_file_sp->GetFile(); |
| 821 | if (!err_file.IsValid()) |
| 822 | err_file.SetStream(stderr, false); |
| 823 | } |
| 824 | |
| 825 | void Debugger::SaveInputTerminalState() { |
| 826 | if (m_input_file_sp) { |
| 827 | File &in_file = m_input_file_sp->GetFile(); |
| 828 | if (in_file.GetDescriptor() != File::kInvalidDescriptor) |
| 829 | m_terminal_state.Save(in_file.GetDescriptor(), true); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | void Debugger::RestoreInputTerminalState() { m_terminal_state.Restore(); } |
| 834 | |
| 835 | ExecutionContext Debugger::GetSelectedExecutionContext() { |
| 836 | ExecutionContext exe_ctx; |
| 837 | TargetSP target_sp(GetSelectedTarget()); |
| 838 | exe_ctx.SetTargetSP(target_sp); |
| 839 | |
| 840 | if (target_sp) { |
| 841 | ProcessSP process_sp(target_sp->GetProcessSP()); |
| 842 | exe_ctx.SetProcessSP(process_sp); |
| 843 | if (process_sp && !process_sp->IsRunning()) { |
| 844 | ThreadSP thread_sp(process_sp->GetThreadList().GetSelectedThread()); |
| 845 | if (thread_sp) { |
| 846 | exe_ctx.SetThreadSP(thread_sp); |
| 847 | exe_ctx.SetFrameSP(thread_sp->GetSelectedFrame()); |
| 848 | if (exe_ctx.GetFramePtr() == nullptr) |
| 849 | exe_ctx.SetFrameSP(thread_sp->GetStackFrameAtIndex(0)); |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | return exe_ctx; |
| 854 | } |
| 855 | |
| 856 | void Debugger::DispatchInputInterrupt() { |
| 857 | std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex()); |
| 858 | IOHandlerSP reader_sp(m_input_reader_stack.Top()); |
| 859 | if (reader_sp) |
| 860 | reader_sp->Interrupt(); |
| 861 | } |
| 862 | |
| 863 | void Debugger::DispatchInputEndOfFile() { |
| 864 | std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex()); |
| 865 | IOHandlerSP reader_sp(m_input_reader_stack.Top()); |
| 866 | if (reader_sp) |
| 867 | reader_sp->GotEOF(); |
| 868 | } |
| 869 | |
| 870 | void Debugger::ClearIOHandlers() { |
| 871 | // The bottom input reader should be the main debugger input reader. We do |
| 872 | // not want to close that one here. |
| 873 | std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex()); |
| 874 | while (m_input_reader_stack.GetSize() > 1) { |
| 875 | IOHandlerSP reader_sp(m_input_reader_stack.Top()); |
| 876 | if (reader_sp) |
| 877 | PopIOHandler(reader_sp); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void Debugger::ExecuteIOHandlers() { |
| 882 | while (true) { |
| 883 | IOHandlerSP reader_sp(m_input_reader_stack.Top()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 884 | if (!reader_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 885 | break; |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 886 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 887 | reader_sp->Run(); |
Pavel Labath | 4446487 | 2015-05-27 12:40:32 +0000 | [diff] [blame] | 888 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 889 | // Remove all input readers that are done from the top of the stack |
| 890 | while (true) { |
| 891 | IOHandlerSP top_reader_sp = m_input_reader_stack.Top(); |
| 892 | if (top_reader_sp && top_reader_sp->GetIsDone()) |
| 893 | PopIOHandler(top_reader_sp); |
| 894 | else |
| 895 | break; |
| 896 | } |
| 897 | } |
| 898 | ClearIOHandlers(); |
| 899 | } |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 900 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 901 | bool Debugger::IsTopIOHandler(const lldb::IOHandlerSP &reader_sp) { |
| 902 | return m_input_reader_stack.IsTop(reader_sp); |
| 903 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 904 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 905 | bool Debugger::CheckTopIOHandlerTypes(IOHandler::Type top_type, |
| 906 | IOHandler::Type second_top_type) { |
| 907 | return m_input_reader_stack.CheckTopIOHandlerTypes(top_type, second_top_type); |
| 908 | } |
| 909 | |
| 910 | void Debugger::PrintAsync(const char *s, size_t len, bool is_stdout) { |
| 911 | lldb::StreamFileSP stream = is_stdout ? GetOutputFile() : GetErrorFile(); |
| 912 | m_input_reader_stack.PrintAsync(stream.get(), s, len); |
| 913 | } |
| 914 | |
| 915 | ConstString Debugger::GetTopIOHandlerControlSequence(char ch) { |
| 916 | return m_input_reader_stack.GetTopIOHandlerControlSequence(ch); |
| 917 | } |
| 918 | |
| 919 | const char *Debugger::GetIOHandlerCommandPrefix() { |
| 920 | return m_input_reader_stack.GetTopIOHandlerCommandPrefix(); |
| 921 | } |
| 922 | |
| 923 | const char *Debugger::GetIOHandlerHelpPrologue() { |
| 924 | return m_input_reader_stack.GetTopIOHandlerHelpPrologue(); |
| 925 | } |
| 926 | |
| 927 | void Debugger::RunIOHandler(const IOHandlerSP &reader_sp) { |
| 928 | PushIOHandler(reader_sp); |
| 929 | |
| 930 | IOHandlerSP top_reader_sp = reader_sp; |
| 931 | while (top_reader_sp) { |
| 932 | top_reader_sp->Run(); |
| 933 | |
| 934 | if (top_reader_sp.get() == reader_sp.get()) { |
| 935 | if (PopIOHandler(reader_sp)) |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | while (true) { |
| 940 | top_reader_sp = m_input_reader_stack.Top(); |
| 941 | if (top_reader_sp && top_reader_sp->GetIsDone()) |
| 942 | PopIOHandler(top_reader_sp); |
| 943 | else |
| 944 | break; |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | void Debugger::AdoptTopIOHandlerFilesIfInvalid(StreamFileSP &in, |
| 950 | StreamFileSP &out, |
| 951 | StreamFileSP &err) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 952 | // Before an IOHandler runs, it must have in/out/err streams. This function |
| 953 | // is called when one ore more of the streams are nullptr. We use the top |
| 954 | // input reader's in/out/err streams, or fall back to the debugger file |
| 955 | // handles, or we fall back onto stdin/stdout/stderr as a last resort. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 956 | |
| 957 | std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex()); |
| 958 | IOHandlerSP top_reader_sp(m_input_reader_stack.Top()); |
| 959 | // If no STDIN has been set, then set it appropriately |
| 960 | if (!in) { |
| 961 | if (top_reader_sp) |
| 962 | in = top_reader_sp->GetInputStreamFile(); |
| 963 | else |
| 964 | in = GetInputFile(); |
| 965 | |
| 966 | // If there is nothing, use stdin |
| 967 | if (!in) |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 968 | in = std::make_shared<StreamFile>(stdin, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 969 | } |
| 970 | // If no STDOUT has been set, then set it appropriately |
| 971 | if (!out) { |
| 972 | if (top_reader_sp) |
| 973 | out = top_reader_sp->GetOutputStreamFile(); |
| 974 | else |
| 975 | out = GetOutputFile(); |
| 976 | |
| 977 | // If there is nothing, use stdout |
| 978 | if (!out) |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 979 | out = std::make_shared<StreamFile>(stdout, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 980 | } |
| 981 | // If no STDERR has been set, then set it appropriately |
| 982 | if (!err) { |
| 983 | if (top_reader_sp) |
| 984 | err = top_reader_sp->GetErrorStreamFile(); |
| 985 | else |
| 986 | err = GetErrorFile(); |
| 987 | |
| 988 | // If there is nothing, use stderr |
| 989 | if (!err) |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 990 | err = std::make_shared<StreamFile>(stdout, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 994 | void Debugger::PushIOHandler(const IOHandlerSP &reader_sp, |
| 995 | bool cancel_top_handler) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 996 | if (!reader_sp) |
| 997 | return; |
| 998 | |
| 999 | std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex()); |
| 1000 | |
| 1001 | // Get the current top input reader... |
| 1002 | IOHandlerSP top_reader_sp(m_input_reader_stack.Top()); |
| 1003 | |
| 1004 | // Don't push the same IO handler twice... |
| 1005 | if (reader_sp == top_reader_sp) |
| 1006 | return; |
| 1007 | |
| 1008 | // Push our new input reader |
| 1009 | m_input_reader_stack.Push(reader_sp); |
| 1010 | reader_sp->Activate(); |
| 1011 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1012 | // Interrupt the top input reader to it will exit its Run() function and let |
| 1013 | // this new input reader take over |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1014 | if (top_reader_sp) { |
| 1015 | top_reader_sp->Deactivate(); |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 1016 | if (cancel_top_handler) |
| 1017 | top_reader_sp->Cancel(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | bool Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp) { |
| 1022 | if (!pop_reader_sp) |
| 1023 | return false; |
| 1024 | |
| 1025 | std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex()); |
| 1026 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1027 | // The reader on the stop of the stack is done, so let the next read on the |
| 1028 | // stack refresh its prompt and if there is one... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1029 | if (m_input_reader_stack.IsEmpty()) |
| 1030 | return false; |
| 1031 | |
| 1032 | IOHandlerSP reader_sp(m_input_reader_stack.Top()); |
| 1033 | |
| 1034 | if (pop_reader_sp != reader_sp) |
| 1035 | return false; |
| 1036 | |
| 1037 | reader_sp->Deactivate(); |
| 1038 | reader_sp->Cancel(); |
| 1039 | m_input_reader_stack.Pop(); |
| 1040 | |
| 1041 | reader_sp = m_input_reader_stack.Top(); |
| 1042 | if (reader_sp) |
Pavel Labath | 4446487 | 2015-05-27 12:40:32 +0000 | [diff] [blame] | 1043 | reader_sp->Activate(); |
| 1044 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | StreamSP Debugger::GetAsyncOutputStream() { |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 1049 | return std::make_shared<StreamAsynchronousIO>(*this, true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | StreamSP Debugger::GetAsyncErrorStream() { |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 1053 | return std::make_shared<StreamAsynchronousIO>(*this, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | size_t Debugger::GetNumDebuggers() { |
| 1057 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 1058 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 1059 | return g_debugger_list_ptr->size(); |
| 1060 | } |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | lldb::DebuggerSP Debugger::GetDebuggerAtIndex(size_t index) { |
| 1065 | DebuggerSP debugger_sp; |
| 1066 | |
| 1067 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 1068 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 1069 | if (index < g_debugger_list_ptr->size()) |
| 1070 | debugger_sp = g_debugger_list_ptr->at(index); |
| 1071 | } |
| 1072 | |
| 1073 | return debugger_sp; |
| 1074 | } |
| 1075 | |
| 1076 | DebuggerSP Debugger::FindDebuggerWithID(lldb::user_id_t id) { |
| 1077 | DebuggerSP debugger_sp; |
| 1078 | |
| 1079 | if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { |
| 1080 | std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr); |
| 1081 | DebuggerList::iterator pos, end = g_debugger_list_ptr->end(); |
| 1082 | for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) { |
| 1083 | if ((*pos)->GetID() == id) { |
| 1084 | debugger_sp = *pos; |
| 1085 | break; |
| 1086 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 1087 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1088 | } |
| 1089 | return debugger_sp; |
Caroline Tice | ebc1bb2 | 2010-06-30 16:22:25 +0000 | [diff] [blame] | 1090 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1091 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1092 | bool Debugger::FormatDisassemblerAddress(const FormatEntity::Entry *format, |
| 1093 | const SymbolContext *sc, |
| 1094 | const SymbolContext *prev_sc, |
| 1095 | const ExecutionContext *exe_ctx, |
| 1096 | const Address *addr, Stream &s) { |
| 1097 | FormatEntity::Entry format_entry; |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 1098 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1099 | if (format == nullptr) { |
| 1100 | if (exe_ctx != nullptr && exe_ctx->HasTargetScope()) |
| 1101 | format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat(); |
| 1102 | if (format == nullptr) { |
| 1103 | FormatEntity::Parse("${addr}: ", format_entry); |
| 1104 | format = &format_entry; |
| 1105 | } |
| 1106 | } |
| 1107 | bool function_changed = false; |
| 1108 | bool initial_function = false; |
| 1109 | if (prev_sc && (prev_sc->function || prev_sc->symbol)) { |
| 1110 | if (sc && (sc->function || sc->symbol)) { |
| 1111 | if (prev_sc->symbol && sc->symbol) { |
| 1112 | if (!sc->symbol->Compare(prev_sc->symbol->GetName(), |
| 1113 | prev_sc->symbol->GetType())) { |
| 1114 | function_changed = true; |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 1115 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1116 | } else if (prev_sc->function && sc->function) { |
| 1117 | if (prev_sc->function->GetMangled() != sc->function->GetMangled()) { |
| 1118 | function_changed = true; |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 1119 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1120 | } |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 1121 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1122 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1123 | // The first context on a list of instructions will have a prev_sc that has |
| 1124 | // no Function or Symbol -- if SymbolContext had an IsValid() method, it |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1125 | // would return false. But we do get a prev_sc pointer. |
| 1126 | if ((sc && (sc->function || sc->symbol)) && prev_sc && |
| 1127 | (prev_sc->function == nullptr && prev_sc->symbol == nullptr)) { |
| 1128 | initial_function = true; |
| 1129 | } |
| 1130 | return FormatEntity::Format(*format, s, sc, exe_ctx, addr, nullptr, |
| 1131 | function_changed, initial_function); |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1134 | void Debugger::SetLoggingCallback(lldb::LogOutputCallback log_callback, |
| 1135 | void *baton) { |
| 1136 | // For simplicity's sake, I am not going to deal with how to close down any |
| 1137 | // open logging streams, I just redirect everything from here on out to the |
| 1138 | // callback. |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 1139 | m_log_callback_stream_sp = |
| 1140 | std::make_shared<StreamCallback>(log_callback, baton); |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 1143 | bool Debugger::EnableLog(llvm::StringRef channel, |
| 1144 | llvm::ArrayRef<const char *> categories, |
| 1145 | llvm::StringRef log_file, uint32_t log_options, |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 1146 | llvm::raw_ostream &error_stream) { |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 1147 | const bool should_close = true; |
| 1148 | const bool unbuffered = true; |
| 1149 | |
| 1150 | std::shared_ptr<llvm::raw_ostream> log_stream_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1151 | if (m_log_callback_stream_sp) { |
| 1152 | log_stream_sp = m_log_callback_stream_sp; |
| 1153 | // For now when using the callback mode you always get thread & timestamp. |
| 1154 | log_options |= |
| 1155 | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 1156 | } else if (log_file.empty()) { |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 1157 | log_stream_sp = std::make_shared<llvm::raw_fd_ostream>( |
| 1158 | GetOutputFile()->GetFile().GetDescriptor(), !should_close, unbuffered); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1159 | } else { |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 1160 | auto pos = m_log_streams.find(log_file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1161 | if (pos != m_log_streams.end()) |
| 1162 | log_stream_sp = pos->second.lock(); |
| 1163 | if (!log_stream_sp) { |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 1164 | llvm::sys::fs::OpenFlags flags = llvm::sys::fs::F_Text; |
| 1165 | if (log_options & LLDB_LOG_OPTION_APPEND) |
| 1166 | flags |= llvm::sys::fs::F_Append; |
| 1167 | int FD; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1168 | if (std::error_code ec = llvm::sys::fs::openFileForWrite( |
| 1169 | log_file, FD, llvm::sys::fs::CD_CreateAlways, flags)) { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 1170 | error_stream << "Unable to open log file: " << ec.message(); |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 1173 | log_stream_sp = |
| 1174 | std::make_shared<llvm::raw_fd_ostream>(FD, should_close, unbuffered); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1175 | m_log_streams[log_file] = log_stream_sp; |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 1176 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1177 | } |
| 1178 | assert(log_stream_sp); |
| 1179 | |
| 1180 | if (log_options == 0) |
| 1181 | log_options = |
| 1182 | LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE; |
| 1183 | |
| 1184 | return Log::EnableLogChannel(log_stream_sp, log_options, channel, categories, |
| 1185 | error_stream); |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Jonas Devlieghere | 2b29b43 | 2019-04-26 22:43:16 +0000 | [diff] [blame] | 1188 | ScriptInterpreter *Debugger::GetScriptInterpreter(bool can_create) { |
| 1189 | std::lock_guard<std::recursive_mutex> locker(m_script_interpreter_mutex); |
| 1190 | |
| 1191 | if (!m_script_interpreter_sp) { |
| 1192 | if (!can_create) |
| 1193 | return nullptr; |
| 1194 | m_script_interpreter_sp = PluginManager::GetScriptInterpreterForLanguage( |
| 1195 | GetScriptLanguage(), *this); |
| 1196 | } |
| 1197 | |
| 1198 | return m_script_interpreter_sp.get(); |
| 1199 | } |
| 1200 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1201 | SourceManager &Debugger::GetSourceManager() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1202 | if (!m_source_manager_up) |
| 1203 | m_source_manager_up = llvm::make_unique<SourceManager>(shared_from_this()); |
| 1204 | return *m_source_manager_up; |
Greg Clayton | 9585fbf | 2013-03-19 00:20:55 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1207 | // This function handles events that were broadcast by the process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1208 | void Debugger::HandleBreakpointEvent(const EventSP &event_sp) { |
| 1209 | using namespace lldb; |
| 1210 | const uint32_t event_type = |
| 1211 | Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent( |
| 1212 | event_sp); |
| 1213 | |
| 1214 | // if (event_type & eBreakpointEventTypeAdded |
| 1215 | // || event_type & eBreakpointEventTypeRemoved |
| 1216 | // || event_type & eBreakpointEventTypeEnabled |
| 1217 | // || event_type & eBreakpointEventTypeDisabled |
| 1218 | // || event_type & eBreakpointEventTypeCommandChanged |
| 1219 | // || event_type & eBreakpointEventTypeConditionChanged |
| 1220 | // || event_type & eBreakpointEventTypeIgnoreChanged |
| 1221 | // || event_type & eBreakpointEventTypeLocationsResolved) |
| 1222 | // { |
| 1223 | // // Don't do anything about these events, since the breakpoint |
| 1224 | // commands already echo these actions. |
| 1225 | // } |
| 1226 | // |
| 1227 | if (event_type & eBreakpointEventTypeLocationsAdded) { |
| 1228 | uint32_t num_new_locations = |
| 1229 | Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent( |
| 1230 | event_sp); |
| 1231 | if (num_new_locations > 0) { |
| 1232 | BreakpointSP breakpoint = |
| 1233 | Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp); |
| 1234 | StreamSP output_sp(GetAsyncOutputStream()); |
| 1235 | if (output_sp) { |
| 1236 | output_sp->Printf("%d location%s added to breakpoint %d\n", |
| 1237 | num_new_locations, num_new_locations == 1 ? "" : "s", |
| 1238 | breakpoint->GetID()); |
| 1239 | output_sp->Flush(); |
| 1240 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1241 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1242 | } |
| 1243 | // else if (event_type & eBreakpointEventTypeLocationsRemoved) |
| 1244 | // { |
| 1245 | // // These locations just get disabled, not sure it is worth spamming |
| 1246 | // folks about this on the command line. |
| 1247 | // } |
| 1248 | // else if (event_type & eBreakpointEventTypeLocationsResolved) |
| 1249 | // { |
| 1250 | // // This might be an interesting thing to note, but I'm going to |
| 1251 | // leave it quiet for now, it just looked noisy. |
| 1252 | // } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Pavel Labath | a9d5843 | 2019-07-31 12:06:50 +0000 | [diff] [blame] | 1255 | void Debugger::FlushProcessOutput(Process &process, bool flush_stdout, |
| 1256 | bool flush_stderr) { |
| 1257 | const auto &flush = [&](Stream &stream, |
| 1258 | size_t (Process::*get)(char *, size_t, Status &)) { |
| 1259 | Status error; |
| 1260 | size_t len; |
| 1261 | char buffer[1024]; |
| 1262 | while ((len = (process.*get)(buffer, sizeof(buffer), error)) > 0) |
| 1263 | stream.Write(buffer, len); |
| 1264 | stream.Flush(); |
| 1265 | }; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1266 | |
Pavel Labath | a9d5843 | 2019-07-31 12:06:50 +0000 | [diff] [blame] | 1267 | std::lock_guard<std::mutex> guard(m_output_flush_mutex); |
| 1268 | if (flush_stdout) |
| 1269 | flush(*GetAsyncOutputStream(), &Process::GetSTDOUT); |
| 1270 | if (flush_stderr) |
| 1271 | flush(*GetAsyncErrorStream(), &Process::GetSTDERR); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | // This function handles events that were broadcast by the process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1275 | void Debugger::HandleProcessEvent(const EventSP &event_sp) { |
| 1276 | using namespace lldb; |
| 1277 | const uint32_t event_type = event_sp->GetType(); |
| 1278 | ProcessSP process_sp = |
| 1279 | (event_type == Process::eBroadcastBitStructuredData) |
| 1280 | ? EventDataStructuredData::GetProcessFromEvent(event_sp.get()) |
| 1281 | : Process::ProcessEventData::GetProcessFromEvent(event_sp.get()); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1283 | StreamSP output_stream_sp = GetAsyncOutputStream(); |
| 1284 | StreamSP error_stream_sp = GetAsyncErrorStream(); |
| 1285 | const bool gui_enabled = IsForwardingEvents(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1287 | if (!gui_enabled) { |
| 1288 | bool pop_process_io_handler = false; |
| 1289 | assert(process_sp); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1291 | bool state_is_stopped = false; |
| 1292 | const bool got_state_changed = |
| 1293 | (event_type & Process::eBroadcastBitStateChanged) != 0; |
| 1294 | const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0; |
| 1295 | const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0; |
| 1296 | const bool got_structured_data = |
| 1297 | (event_type & Process::eBroadcastBitStructuredData) != 0; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 1298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1299 | if (got_state_changed) { |
| 1300 | StateType event_state = |
| 1301 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1302 | state_is_stopped = StateIsStoppedState(event_state, false); |
| 1303 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1305 | // Display running state changes first before any STDIO |
| 1306 | if (got_state_changed && !state_is_stopped) { |
| 1307 | Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(), |
| 1308 | pop_process_io_handler); |
| 1309 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1310 | |
Pavel Labath | a9d5843 | 2019-07-31 12:06:50 +0000 | [diff] [blame] | 1311 | // Now display STDOUT and STDERR |
| 1312 | FlushProcessOutput(*process_sp, got_stdout || got_state_changed, |
| 1313 | got_stderr || got_state_changed); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 1314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1315 | // Give structured data events an opportunity to display. |
| 1316 | if (got_structured_data) { |
| 1317 | StructuredDataPluginSP plugin_sp = |
| 1318 | EventDataStructuredData::GetPluginFromEvent(event_sp.get()); |
| 1319 | if (plugin_sp) { |
| 1320 | auto structured_data_sp = |
| 1321 | EventDataStructuredData::GetObjectFromEvent(event_sp.get()); |
| 1322 | if (output_stream_sp) { |
| 1323 | StreamString content_stream; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1324 | Status error = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1325 | plugin_sp->GetDescription(structured_data_sp, content_stream); |
| 1326 | if (error.Success()) { |
| 1327 | if (!content_stream.GetString().empty()) { |
| 1328 | // Add newline. |
| 1329 | content_stream.PutChar('\n'); |
| 1330 | content_stream.Flush(); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 1331 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1332 | // Print it. |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 1333 | output_stream_sp->PutCString(content_stream.GetString()); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 1334 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1335 | } else { |
| 1336 | error_stream_sp->Printf("Failed to print structured " |
| 1337 | "data with plugin %s: %s", |
| 1338 | plugin_sp->GetPluginName().AsCString(), |
| 1339 | error.AsCString()); |
| 1340 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 1341 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1342 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 1343 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1344 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1345 | // Now display any stopped state changes after any STDIO |
| 1346 | if (got_state_changed && state_is_stopped) { |
| 1347 | Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(), |
| 1348 | pop_process_io_handler); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1349 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1350 | |
| 1351 | output_stream_sp->Flush(); |
| 1352 | error_stream_sp->Flush(); |
| 1353 | |
| 1354 | if (pop_process_io_handler) |
| 1355 | process_sp->PopProcessIOHandler(); |
| 1356 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1359 | void Debugger::HandleThreadEvent(const EventSP &event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1360 | // At present the only thread event we handle is the Frame Changed event, and |
| 1361 | // all we do for that is just reprint the thread status for that thread. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1362 | using namespace lldb; |
| 1363 | const uint32_t event_type = event_sp->GetType(); |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1364 | const bool stop_format = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1365 | if (event_type == Thread::eBroadcastBitStackChanged || |
| 1366 | event_type == Thread::eBroadcastBitThreadSelected) { |
| 1367 | ThreadSP thread_sp( |
| 1368 | Thread::ThreadEventData::GetThreadFromEvent(event_sp.get())); |
| 1369 | if (thread_sp) { |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1370 | thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1, stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1371 | } |
| 1372 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1375 | bool Debugger::IsForwardingEvents() { return (bool)m_forward_listener_sp; } |
| 1376 | |
| 1377 | void Debugger::EnableForwardEvents(const ListenerSP &listener_sp) { |
| 1378 | m_forward_listener_sp = listener_sp; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1381 | void Debugger::CancelForwardEvents(const ListenerSP &listener_sp) { |
| 1382 | m_forward_listener_sp.reset(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1385 | void Debugger::DefaultEventHandler() { |
| 1386 | ListenerSP listener_sp(GetListener()); |
| 1387 | ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass()); |
| 1388 | ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass()); |
| 1389 | ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass()); |
| 1390 | BroadcastEventSpec target_event_spec(broadcaster_class_target, |
| 1391 | Target::eBroadcastBitBreakpointChanged); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1393 | BroadcastEventSpec process_event_spec( |
| 1394 | broadcaster_class_process, |
| 1395 | Process::eBroadcastBitStateChanged | Process::eBroadcastBitSTDOUT | |
| 1396 | Process::eBroadcastBitSTDERR | Process::eBroadcastBitStructuredData); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1397 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1398 | BroadcastEventSpec thread_event_spec(broadcaster_class_thread, |
| 1399 | Thread::eBroadcastBitStackChanged | |
| 1400 | Thread::eBroadcastBitThreadSelected); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1401 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1402 | listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp, |
| 1403 | target_event_spec); |
| 1404 | listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp, |
| 1405 | process_event_spec); |
| 1406 | listener_sp->StartListeningForEventSpec(m_broadcaster_manager_sp, |
| 1407 | thread_event_spec); |
| 1408 | listener_sp->StartListeningForEvents( |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1409 | m_command_interpreter_up.get(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1410 | CommandInterpreter::eBroadcastBitQuitCommandReceived | |
| 1411 | CommandInterpreter::eBroadcastBitAsynchronousOutputData | |
| 1412 | CommandInterpreter::eBroadcastBitAsynchronousErrorData); |
Greg Clayton | afa91e33 | 2014-12-01 22:41:27 +0000 | [diff] [blame] | 1413 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1414 | // Let the thread that spawned us know that we have started up and that we |
| 1415 | // are now listening to all required events so no events get missed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1416 | m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening); |
Greg Clayton | afa91e33 | 2014-12-01 22:41:27 +0000 | [diff] [blame] | 1417 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1418 | bool done = false; |
| 1419 | while (!done) { |
| 1420 | EventSP event_sp; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1421 | if (listener_sp->GetEvent(event_sp, llvm::None)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1422 | if (event_sp) { |
| 1423 | Broadcaster *broadcaster = event_sp->GetBroadcaster(); |
| 1424 | if (broadcaster) { |
| 1425 | uint32_t event_type = event_sp->GetType(); |
| 1426 | ConstString broadcaster_class(broadcaster->GetBroadcasterClass()); |
| 1427 | if (broadcaster_class == broadcaster_class_process) { |
| 1428 | HandleProcessEvent(event_sp); |
| 1429 | } else if (broadcaster_class == broadcaster_class_target) { |
| 1430 | if (Breakpoint::BreakpointEventData::GetEventDataFromEvent( |
| 1431 | event_sp.get())) { |
| 1432 | HandleBreakpointEvent(event_sp); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1433 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1434 | } else if (broadcaster_class == broadcaster_class_thread) { |
| 1435 | HandleThreadEvent(event_sp); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1436 | } else if (broadcaster == m_command_interpreter_up.get()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1437 | if (event_type & |
| 1438 | CommandInterpreter::eBroadcastBitQuitCommandReceived) { |
| 1439 | done = true; |
| 1440 | } else if (event_type & |
| 1441 | CommandInterpreter::eBroadcastBitAsynchronousErrorData) { |
| 1442 | const char *data = reinterpret_cast<const char *>( |
| 1443 | EventDataBytes::GetBytesFromEvent(event_sp.get())); |
| 1444 | if (data && data[0]) { |
| 1445 | StreamSP error_sp(GetAsyncErrorStream()); |
| 1446 | if (error_sp) { |
| 1447 | error_sp->PutCString(data); |
| 1448 | error_sp->Flush(); |
| 1449 | } |
| 1450 | } |
| 1451 | } else if (event_type & CommandInterpreter:: |
| 1452 | eBroadcastBitAsynchronousOutputData) { |
| 1453 | const char *data = reinterpret_cast<const char *>( |
| 1454 | EventDataBytes::GetBytesFromEvent(event_sp.get())); |
| 1455 | if (data && data[0]) { |
| 1456 | StreamSP output_sp(GetAsyncOutputStream()); |
| 1457 | if (output_sp) { |
| 1458 | output_sp->PutCString(data); |
| 1459 | output_sp->Flush(); |
| 1460 | } |
| 1461 | } |
| 1462 | } |
| 1463 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1464 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1465 | |
| 1466 | if (m_forward_listener_sp) |
| 1467 | m_forward_listener_sp->AddEvent(event_sp); |
| 1468 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1469 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1470 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | lldb::thread_result_t Debugger::EventHandlerThread(lldb::thread_arg_t arg) { |
| 1474 | ((Debugger *)arg)->DefaultEventHandler(); |
Konrad Kleine | 8520064 | 2019-05-23 15:17:39 +0000 | [diff] [blame] | 1475 | return {}; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1478 | bool Debugger::StartEventHandlerThread() { |
| 1479 | if (!m_event_handler_thread.IsJoinable()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1480 | // We must synchronize with the DefaultEventHandler() thread to ensure it |
| 1481 | // is up and running and listening to events before we return from this |
| 1482 | // function. We do this by listening to events for the |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1483 | // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster |
Tatyana Krasnukha | 1a728f6 | 2018-07-13 11:21:06 +0000 | [diff] [blame] | 1484 | ConstString full_name("lldb.debugger.event-handler"); |
| 1485 | ListenerSP listener_sp(Listener::MakeListener(full_name.AsCString())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1486 | listener_sp->StartListeningForEvents(&m_sync_broadcaster, |
| 1487 | eBroadcastBitEventThreadIsListening); |
Greg Clayton | afa91e33 | 2014-12-01 22:41:27 +0000 | [diff] [blame] | 1488 | |
Tatyana Krasnukha | 1a728f6 | 2018-07-13 11:21:06 +0000 | [diff] [blame] | 1489 | auto thread_name = |
| 1490 | full_name.GetLength() < llvm::get_max_thread_name_length() ? |
| 1491 | full_name.AsCString() : "dbg.evt-handler"; |
| 1492 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1493 | // Use larger 8MB stack for this thread |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 1494 | llvm::Expected<HostThread> event_handler_thread = |
| 1495 | ThreadLauncher::LaunchThread(thread_name, EventHandlerThread, this, |
| 1496 | g_debugger_event_thread_stack_bytes); |
| 1497 | |
| 1498 | if (event_handler_thread) { |
| 1499 | m_event_handler_thread = *event_handler_thread; |
| 1500 | } else { |
| 1501 | LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), |
| 1502 | "failed to launch host thread: {}", |
| 1503 | llvm::toString(event_handler_thread.takeError())); |
| 1504 | } |
Greg Clayton | afa91e33 | 2014-12-01 22:41:27 +0000 | [diff] [blame] | 1505 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1506 | // Make sure DefaultEventHandler() is running and listening to events |
| 1507 | // before we return from this function. We are only listening for events of |
| 1508 | // type eBroadcastBitEventThreadIsListening so we don't need to check the |
| 1509 | // event, we just need to wait an infinite amount of time for it (nullptr |
| 1510 | // timeout as the first parameter) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1511 | lldb::EventSP event_sp; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1512 | listener_sp->GetEvent(event_sp, llvm::None); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1513 | } |
| 1514 | return m_event_handler_thread.IsJoinable(); |
| 1515 | } |
| 1516 | |
| 1517 | void Debugger::StopEventHandlerThread() { |
| 1518 | if (m_event_handler_thread.IsJoinable()) { |
| 1519 | GetCommandInterpreter().BroadcastEvent( |
| 1520 | CommandInterpreter::eBroadcastBitQuitCommandReceived); |
| 1521 | m_event_handler_thread.Join(nullptr); |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | lldb::thread_result_t Debugger::IOHandlerThread(lldb::thread_arg_t arg) { |
| 1526 | Debugger *debugger = (Debugger *)arg; |
| 1527 | debugger->ExecuteIOHandlers(); |
| 1528 | debugger->StopEventHandlerThread(); |
Konrad Kleine | 8520064 | 2019-05-23 15:17:39 +0000 | [diff] [blame] | 1529 | return {}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | bool Debugger::HasIOHandlerThread() { return m_io_handler_thread.IsJoinable(); } |
| 1533 | |
| 1534 | bool Debugger::StartIOHandlerThread() { |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 1535 | if (!m_io_handler_thread.IsJoinable()) { |
| 1536 | llvm::Expected<HostThread> io_handler_thread = ThreadLauncher::LaunchThread( |
| 1537 | "lldb.debugger.io-handler", IOHandlerThread, this, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1538 | 8 * 1024 * 1024); // Use larger 8MB stack for this thread |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 1539 | if (io_handler_thread) { |
| 1540 | m_io_handler_thread = *io_handler_thread; |
| 1541 | } else { |
| 1542 | LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), |
| 1543 | "failed to launch host thread: {}", |
| 1544 | llvm::toString(io_handler_thread.takeError())); |
| 1545 | } |
| 1546 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1547 | return m_io_handler_thread.IsJoinable(); |
| 1548 | } |
| 1549 | |
| 1550 | void Debugger::StopIOHandlerThread() { |
| 1551 | if (m_io_handler_thread.IsJoinable()) { |
| 1552 | if (m_input_file_sp) |
| 1553 | m_input_file_sp->GetFile().Close(); |
| 1554 | m_io_handler_thread.Join(nullptr); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | void Debugger::JoinIOHandlerThread() { |
| 1559 | if (HasIOHandlerThread()) { |
| 1560 | thread_result_t result; |
| 1561 | m_io_handler_thread.Join(&result); |
| 1562 | m_io_handler_thread = LLDB_INVALID_HOST_THREAD; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | Target *Debugger::GetDummyTarget() { |
| 1567 | return m_target_list.GetDummyTarget(*this).get(); |
| 1568 | } |
| 1569 | |
| 1570 | Target *Debugger::GetSelectedOrDummyTarget(bool prefer_dummy) { |
| 1571 | Target *target = nullptr; |
| 1572 | if (!prefer_dummy) { |
| 1573 | target = m_target_list.GetSelectedTarget().get(); |
| 1574 | if (target) |
| 1575 | return target; |
| 1576 | } |
| 1577 | |
| 1578 | return GetDummyTarget(); |
| 1579 | } |
| 1580 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1581 | Status Debugger::RunREPL(LanguageType language, const char *repl_options) { |
| 1582 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1583 | FileSpec repl_executable; |
| 1584 | |
| 1585 | if (language == eLanguageTypeUnknown) { |
| 1586 | std::set<LanguageType> repl_languages; |
| 1587 | |
| 1588 | Language::GetLanguagesSupportingREPLs(repl_languages); |
| 1589 | |
| 1590 | if (repl_languages.size() == 1) { |
| 1591 | language = *repl_languages.begin(); |
| 1592 | } else if (repl_languages.empty()) { |
| 1593 | err.SetErrorStringWithFormat( |
| 1594 | "LLDB isn't configured with REPL support for any languages."); |
| 1595 | return err; |
| 1596 | } else { |
| 1597 | err.SetErrorStringWithFormat( |
| 1598 | "Multiple possible REPL languages. Please specify a language."); |
| 1599 | return err; |
Greg Clayton | 807b6b3 | 2014-10-15 18:03:59 +0000 | [diff] [blame] | 1600 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1601 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1602 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1603 | Target *const target = |
| 1604 | nullptr; // passing in an empty target means the REPL must create one |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1605 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1606 | REPLSP repl_sp(REPL::Create(err, language, this, target, repl_options)); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1607 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1608 | if (!err.Success()) { |
Sean Callanan | 3e7e915 | 2015-10-20 00:23:46 +0000 | [diff] [blame] | 1609 | return err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | if (!repl_sp) { |
| 1613 | err.SetErrorStringWithFormat("couldn't find a REPL for %s", |
| 1614 | Language::GetNameForLanguageType(language)); |
| 1615 | return err; |
| 1616 | } |
| 1617 | |
| 1618 | repl_sp->SetCompilerOptions(repl_options); |
| 1619 | repl_sp->RunLoop(); |
| 1620 | |
| 1621 | return err; |
Sean Callanan | 3e7e915 | 2015-10-20 00:23:46 +0000 | [diff] [blame] | 1622 | } |