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