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