blob: 2b8e822c4a22d7a633234fc3bed7835a64937b0f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Debugger.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton4a33d312011-06-23 17:59:56 +000010#include "lldb/Core/Debugger.h"
11
12#include <map>
13
Jason Molenda705b1802014-06-13 02:37:02 +000014#include "llvm/ADT/StringRef.h"
Enrico Granata4becb372011-06-29 22:27:15 +000015
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/lldb-private.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000017#include "lldb/Core/FormatEntity.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/Module.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000019#include "lldb/Core/PluginInterface.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000020#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000021#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000024#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000025#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000026#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000027#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000029#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000030#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000031#include "lldb/DataFormatters/DataVisualization.h"
32#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000033#include "lldb/DataFormatters/TypeSummary.h"
Sean Callanan3e7e9152015-10-20 00:23:46 +000034#include "lldb/Expression/REPL.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000035#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000036#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000037#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000038#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000039#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000040#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000041#include "lldb/Interpreter/OptionValueSInt64.h"
42#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/Function.h"
45#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000046#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Target/TargetList.h"
Sean Callanan3e7e9152015-10-20 00:23:46 +000048#include "lldb/Target/Language.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000050#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000051#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000052#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000053#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000055#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Zachary Turner58a559c2014-08-27 20:15:09 +000057#include "llvm/Support/DynamicLibrary.h"
58
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059using namespace lldb;
60using namespace lldb_private;
61
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Caroline Ticeebc1bb22010-06-30 16:22:25 +000063static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000064static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000065
Greg Clayton1b654882010-09-19 02:33:57 +000066#pragma mark Static Functions
67
68static Mutex &
69GetDebuggerListMutex ()
70{
71 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
72 return g_mutex;
73}
74
75typedef std::vector<DebuggerSP> DebuggerList;
76
77static DebuggerList &
78GetDebuggerList()
79{
80 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000081 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000082 static DebuggerList g_list;
83 return g_list;
84}
Greg Claytone372b982011-11-21 21:44:34 +000085
86OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000087g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000088{
Greg Clayton67cc0632012-08-22 17:17:09 +000089 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
90 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
91 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000092 { 0, NULL, NULL }
93};
94
Greg Clayton67cc0632012-08-22 17:17:09 +000095OptionEnumValueElement
96g_language_enumerators[] =
97{
98 { eScriptLanguageNone, "none", "Disable scripting languages."},
99 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
100 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +0000101 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000102};
Greg Claytone372b982011-11-21 21:44:34 +0000103
Greg Clayton67cc0632012-08-22 17:17:09 +0000104#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
105#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Jason Molenda6ab659a2015-07-29 00:42:47 +0000106#define IS_OPTIMIZED "{${function.is-optimized} [opt]}"
Greg Clayton67cc0632012-08-22 17:17:09 +0000107
Michael Sartain0769b2b2013-07-30 16:44:36 +0000108#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000109 "{, ${frame.pc}}"\
110 MODULE_WITH_FUNC\
111 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000112 "{, name = '${thread.name}'}"\
113 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000114 "{, activity = '${thread.info.activity.name}'}" \
115 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000116 "{, stop reason = ${thread.stop-reason}}"\
117 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000118 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000119 "\\n"
120
121#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
122 MODULE_WITH_FUNC\
123 FILE_AND_LINE\
Jason Molenda6ab659a2015-07-29 00:42:47 +0000124 IS_OPTIMIZED\
Greg Clayton67cc0632012-08-22 17:17:09 +0000125 "\\n"
126
Jason Molendac980fa92015-02-13 23:24:21 +0000127// Three parts to this disassembly format specification:
128// 1. If this is a new function/symbol (no previous symbol/function), print
129// dylib`funcname:\n
130// 2. If this is a symbol context change (different from previous symbol/function), print
131// dylib`funcname:\n
132// 3. print
133// address <+offset>:
134#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}>}: "
135
136// gdb's disassembly format can be emulated with
137// ${current-pc-arrow}${addr-file-or-load}{ <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>}:
138
139// lldb's original format for disassembly would look like this format string -
140// {${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}}:
141
Greg Clayton67cc0632012-08-22 17:17:09 +0000142
Greg Clayton754a9362012-08-23 00:22:02 +0000143static PropertyDefinition
144g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000145{
Greg Clayton554f68d2015-02-04 22:00:53 +0000146{ "auto-confirm", OptionValue::eTypeBoolean , true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
147{ "disassembly-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." },
148{ "frame-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
149{ "notify-void", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
150{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
151{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
152{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
153{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
154{ "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." },
155{ "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." },
156{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
157{ "thread-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
158{ "use-external-editor", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Whether to use an external editor or not." },
159{ "use-color", OptionValue::eTypeBoolean , true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
160{ "auto-one-line-summaries", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
Sean Callanan66810412015-10-19 23:11:07 +0000161{ "auto-indent", OptionValue::eTypeBoolean , true, true , NULL, NULL, "If true, LLDB will auto indent/outdent code. Currently only supported in the REPL (default: true)." },
162{ "print-decls", OptionValue::eTypeBoolean , true, true , NULL, NULL, "If true, LLDB will print the values of variables declared in an expression. Currently only supported in the REPL (default: true)." },
163{ "tab-size", OptionValue::eTypeUInt64 , true, 4 , NULL, NULL, "The tab size to use when indenting code in multi-line input mode (default: 4)." },
Greg Clayton554f68d2015-02-04 22:00:53 +0000164{ "escape-non-printables", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically escape non-printable and escape characters when formatting strings." },
165{ NULL, OptionValue::eTypeInvalid , true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000166};
167
168enum
169{
170 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000171 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000172 ePropertyFrameFormat,
173 ePropertyNotiftVoid,
174 ePropertyPrompt,
175 ePropertyScriptLanguage,
176 ePropertyStopDisassemblyCount,
177 ePropertyStopDisassemblyDisplay,
178 ePropertyStopLineCountAfter,
179 ePropertyStopLineCountBefore,
180 ePropertyTerminalWidth,
181 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000182 ePropertyUseExternalEditor,
183 ePropertyUseColor,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000184 ePropertyAutoOneLineSummaries,
Sean Callanan66810412015-10-19 23:11:07 +0000185 ePropertyAutoIndent,
186 ePropertyPrintDecls,
187 ePropertyTabSize,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000188 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000189};
190
Zachary Turner3a006912015-03-19 22:00:21 +0000191LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000192
193Error
194Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
195 VarSetOperationType op,
196 const char *property_path,
197 const char *value)
198{
Enrico Granata84a53df2013-05-20 22:29:23 +0000199 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000200 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000201 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000202 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000203 if (is_load_script && exe_ctx->GetTargetSP())
204 {
205 target_sp = exe_ctx->GetTargetSP();
206 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
207 }
Greg Clayton4c054102012-09-01 00:38:36 +0000208 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
209 if (error.Success())
210 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000211 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000212 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
213 {
214 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000215 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
216 if (str.length())
217 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000218 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000219 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
220 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
221 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000222 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
223 {
224 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
225 SetPrompt (GetPrompt());
226 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000227 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000228 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000229 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000230 {
231 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000232 StreamString feedback_stream;
233 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000234 {
Greg Clayton44d93782014-01-27 23:43:24 +0000235 StreamFileSP stream_sp (GetErrorFile());
236 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000237 {
Greg Clayton44d93782014-01-27 23:43:24 +0000238 for (auto error : errors)
239 {
240 stream_sp->Printf("%s\n",error.AsCString());
241 }
242 if (feedback_stream.GetSize())
243 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000244 }
245 }
246 }
247 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000248 else if (is_escape_non_printables)
249 {
250 DataVisualization::ForceUpdate();
251 }
Greg Clayton4c054102012-09-01 00:38:36 +0000252 }
253 return error;
254}
255
Greg Clayton67cc0632012-08-22 17:17:09 +0000256bool
257Debugger::GetAutoConfirm () const
258{
259 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000260 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000261}
262
Greg Clayton554f68d2015-02-04 22:00:53 +0000263const FormatEntity::Entry *
Jason Molendaaff1b352014-10-10 23:07:36 +0000264Debugger::GetDisassemblyFormat() const
265{
266 const uint32_t idx = ePropertyDisassemblyFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000267 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Jason Molendaaff1b352014-10-10 23:07:36 +0000268}
269
Greg Clayton554f68d2015-02-04 22:00:53 +0000270const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000271Debugger::GetFrameFormat() const
272{
273 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000274 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000275}
276
277bool
278Debugger::GetNotifyVoid () const
279{
280 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000281 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000282}
283
284const char *
285Debugger::GetPrompt() const
286{
287 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000288 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000289}
290
291void
292Debugger::SetPrompt(const char *p)
293{
294 const uint32_t idx = ePropertyPrompt;
295 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
296 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000297 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
298 if (str.length())
299 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000300 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000301}
302
Greg Clayton554f68d2015-02-04 22:00:53 +0000303const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000304Debugger::GetThreadFormat() const
305{
306 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000307 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000308}
309
310lldb::ScriptLanguage
311Debugger::GetScriptLanguage() const
312{
313 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000314 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000315}
316
317bool
318Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
319{
320 const uint32_t idx = ePropertyScriptLanguage;
321 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
322}
323
324uint32_t
325Debugger::GetTerminalWidth () const
326{
327 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000328 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000329}
330
331bool
332Debugger::SetTerminalWidth (uint32_t term_width)
333{
334 const uint32_t idx = ePropertyTerminalWidth;
335 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
336}
337
338bool
339Debugger::GetUseExternalEditor () const
340{
341 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000342 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000343}
344
345bool
346Debugger::SetUseExternalEditor (bool b)
347{
348 const uint32_t idx = ePropertyUseExternalEditor;
349 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
350}
351
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000352bool
353Debugger::GetUseColor () const
354{
355 const uint32_t idx = ePropertyUseColor;
356 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
357}
358
359bool
360Debugger::SetUseColor (bool b)
361{
362 const uint32_t idx = ePropertyUseColor;
363 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
364 SetPrompt (GetPrompt());
365 return ret;
366}
367
Greg Clayton67cc0632012-08-22 17:17:09 +0000368uint32_t
369Debugger::GetStopSourceLineCount (bool before) const
370{
371 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000372 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000373}
374
375Debugger::StopDisassemblyType
376Debugger::GetStopDisassemblyDisplay () const
377{
378 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000379 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000380}
381
382uint32_t
383Debugger::GetDisassemblyLineCount () const
384{
385 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000386 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000387}
Greg Claytone372b982011-11-21 21:44:34 +0000388
Enrico Granata553fad52013-10-25 23:09:40 +0000389bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000390Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000391{
Enrico Granata90a8db32013-10-31 21:01:07 +0000392 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000393 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000394}
Enrico Granata553fad52013-10-25 23:09:40 +0000395
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000396bool
397Debugger::GetEscapeNonPrintables () const
398{
399 const uint32_t idx = ePropertyEscapeNonPrintables;
400 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000401}
402
Sean Callanan66810412015-10-19 23:11:07 +0000403bool
404Debugger::GetAutoIndent () const
405{
406 const uint32_t idx = ePropertyAutoIndent;
407 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
408}
409
410bool
411Debugger::SetAutoIndent (bool b)
412{
413 const uint32_t idx = ePropertyAutoIndent;
414 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
415}
416
417bool
418Debugger::GetPrintDecls () const
419{
420 const uint32_t idx = ePropertyPrintDecls;
421 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
422}
423
424bool
425Debugger::SetPrintDecls (bool b)
426{
427 const uint32_t idx = ePropertyPrintDecls;
428 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
429}
430
431uint32_t
432Debugger::GetTabSize () const
433{
434 const uint32_t idx = ePropertyTabSize;
435 return m_collection_sp->GetPropertyAtIndexAsUInt64 (NULL, idx, g_properties[idx].default_uint_value);
436}
437
438bool
439Debugger::SetTabSize (uint32_t tab_size)
440{
441 const uint32_t idx = ePropertyTabSize;
442 return m_collection_sp->SetPropertyAtIndexAsUInt64 (NULL, idx, tab_size);
443}
444
445
Greg Clayton1b654882010-09-19 02:33:57 +0000446#pragma mark Debugger
447
Greg Clayton67cc0632012-08-22 17:17:09 +0000448//const DebuggerPropertiesSP &
449//Debugger::GetSettings() const
450//{
451// return m_properties_sp;
452//}
453//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000454
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000455static bool lldb_initialized = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456void
Zachary Turner3a006912015-03-19 22:00:21 +0000457Debugger::Initialize(LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000459 assert(!lldb_initialized && "Debugger::Initialize called more than once!");
460
Robert Flackf196c932015-03-10 18:07:47 +0000461 lldb_initialized = true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000462 g_load_plugin_callback = load_plugin_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463}
464
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000465void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466Debugger::Terminate ()
467{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000468 assert(lldb_initialized && "Debugger::Terminate called without a matching Debugger::Initialize!");
469
470 // Clear our master list of debugger objects
471 Mutex::Locker locker (GetDebuggerListMutex ());
Oleksiy Vyalovf3cd1812015-09-08 16:26:32 +0000472 auto& debuggers = GetDebuggerList();
473 for (const auto& debugger: debuggers)
474 debugger->Clear();
475
476 debuggers.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477}
478
Caroline Tice20bd37f2011-03-10 22:14:10 +0000479void
480Debugger::SettingsInitialize ()
481{
Greg Clayton6920b522012-08-22 18:39:03 +0000482 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000483}
484
485void
486Debugger::SettingsTerminate ()
487{
Greg Clayton6920b522012-08-22 18:39:03 +0000488 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000489}
490
Enrico Granata21dfcd92012-09-28 23:57:51 +0000491bool
Enrico Granatae743c782013-04-24 21:29:08 +0000492Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000493{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000494 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000495 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000496 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
497 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000498 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000499 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000500 return true;
501 }
Enrico Granatae743c782013-04-24 21:29:08 +0000502 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000503 else
Enrico Granatae743c782013-04-24 21:29:08 +0000504 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000505 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
506 // and if the public API layer isn't available (code is linking against
507 // all of the internal LLDB static libraries), then we can't load plugins
508 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000509 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000510 return false;
511}
512
513static FileSpec::EnumerateDirectoryResult
514LoadPluginCallback
515(
516 void *baton,
517 FileSpec::FileType file_type,
518 const FileSpec &file_spec
519 )
520{
521 Error error;
522
523 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000524 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000525
526 if (!baton)
527 return FileSpec::eEnumerateDirectoryResultQuit;
528
529 Debugger *debugger = (Debugger*)baton;
530
531 // If we have a regular file, a symbolic link or unknown file type, try
532 // and process the file. We must handle unknown as sometimes the directory
533 // enumeration might be enumerating a file system that doesn't have correct
534 // file type information.
535 if (file_type == FileSpec::eFileTypeRegular ||
536 file_type == FileSpec::eFileTypeSymbolicLink ||
537 file_type == FileSpec::eFileTypeUnknown )
538 {
539 FileSpec plugin_file_spec (file_spec);
540 plugin_file_spec.ResolvePath ();
541
Michael Sartain3cf443d2013-07-17 00:26:30 +0000542 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
543 plugin_file_spec.GetFileNameExtension() != g_solibext)
544 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000545 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000546 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000547
Enrico Granatae743c782013-04-24 21:29:08 +0000548 Error plugin_load_error;
549 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000550
551 return FileSpec::eEnumerateDirectoryResultNext;
552 }
553
554 else if (file_type == FileSpec::eFileTypeUnknown ||
555 file_type == FileSpec::eFileTypeDirectory ||
556 file_type == FileSpec::eFileTypeSymbolicLink )
557 {
558 // Try and recurse into anything that a directory or symbolic link.
559 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000560 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000561 // information.
562 return FileSpec::eEnumerateDirectoryResultEnter;
563 }
564
565 return FileSpec::eEnumerateDirectoryResultNext;
566}
567
568void
569Debugger::InstanceInitialize ()
570{
571 FileSpec dir_spec;
572 const bool find_directories = true;
573 const bool find_files = true;
574 const bool find_other = true;
575 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000576 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000577 {
578 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
579 {
580 FileSpec::EnumerateDirectory (dir_path,
581 find_directories,
582 find_files,
583 find_other,
584 LoadPluginCallback,
585 this);
586 }
587 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000588
589 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000590 {
591 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
592 {
593 FileSpec::EnumerateDirectory (dir_path,
594 find_directories,
595 find_files,
596 find_other,
597 LoadPluginCallback,
598 this);
599 }
600 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000601
602 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000603}
604
Greg Clayton66111032010-06-23 01:19:29 +0000605DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000606Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000607{
Jim Ingham228063c2012-02-21 02:23:08 +0000608 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000609 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000610 {
611 Mutex::Locker locker (GetDebuggerListMutex ());
612 GetDebuggerList().push_back(debugger_sp);
613 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000614 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000615 return debugger_sp;
616}
617
Caroline Ticee02657b2011-01-22 01:02:07 +0000618void
Greg Clayton4d122c42011-09-17 08:33:22 +0000619Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000620{
621 if (debugger_sp.get() == NULL)
622 return;
623
Jim Ingham8314c522011-09-15 21:36:42 +0000624 debugger_sp->Clear();
625
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000626 if (lldb_initialized)
Caroline Ticee02657b2011-01-22 01:02:07 +0000627 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000628 Mutex::Locker locker (GetDebuggerListMutex ());
629 DebuggerList &debugger_list = GetDebuggerList ();
630 DebuggerList::iterator pos, end = debugger_list.end();
631 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000632 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000633 if ((*pos).get() == debugger_sp.get())
634 {
635 debugger_list.erase (pos);
636 return;
637 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000638 }
639 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000640}
641
Greg Clayton4d122c42011-09-17 08:33:22 +0000642DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000643Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
644{
Greg Clayton4d122c42011-09-17 08:33:22 +0000645 DebuggerSP debugger_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000646 if (lldb_initialized)
Greg Clayton6920b522012-08-22 18:39:03 +0000647 {
648 Mutex::Locker locker (GetDebuggerListMutex ());
649 DebuggerList &debugger_list = GetDebuggerList();
650 DebuggerList::iterator pos, end = debugger_list.end();
651
652 for (pos = debugger_list.begin(); pos != end; ++pos)
653 {
654 if ((*pos).get()->m_instance_name == instance_name)
655 {
656 debugger_sp = *pos;
657 break;
658 }
659 }
660 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000661 return debugger_sp;
662}
Greg Clayton66111032010-06-23 01:19:29 +0000663
664TargetSP
665Debugger::FindTargetWithProcessID (lldb::pid_t pid)
666{
Greg Clayton4d122c42011-09-17 08:33:22 +0000667 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000668 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000669 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000670 Mutex::Locker locker (GetDebuggerListMutex ());
671 DebuggerList &debugger_list = GetDebuggerList();
672 DebuggerList::iterator pos, end = debugger_list.end();
673 for (pos = debugger_list.begin(); pos != end; ++pos)
674 {
675 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
676 if (target_sp)
677 break;
678 }
Greg Clayton66111032010-06-23 01:19:29 +0000679 }
680 return target_sp;
681}
682
Greg Claytone4e45922011-11-16 05:37:56 +0000683TargetSP
684Debugger::FindTargetWithProcess (Process *process)
685{
686 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000687 if (lldb_initialized)
Greg Claytone4e45922011-11-16 05:37:56 +0000688 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000689 Mutex::Locker locker (GetDebuggerListMutex ());
690 DebuggerList &debugger_list = GetDebuggerList();
691 DebuggerList::iterator pos, end = debugger_list.end();
692 for (pos = debugger_list.begin(); pos != end; ++pos)
693 {
694 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
695 if (target_sp)
696 break;
697 }
Greg Claytone4e45922011-11-16 05:37:56 +0000698 }
699 return target_sp;
700}
701
Jason Molendae6481c72014-09-12 01:50:46 +0000702Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
703 UserID(g_unique_id++),
704 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
705 m_input_file_sp(new StreamFile(stdin, false)),
706 m_output_file_sp(new StreamFile(stdout, false)),
707 m_error_file_sp(new StreamFile(stderr, false)),
708 m_terminal_state(),
709 m_target_list(*this),
710 m_platform_list(),
711 m_listener("lldb.Debugger"),
712 m_source_manager_ap(),
713 m_source_file_cache(),
714 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
715 m_input_reader_stack(),
716 m_instance_name(),
Greg Claytonafa91e332014-12-01 22:41:27 +0000717 m_loaded_plugins(),
718 m_event_handler_thread (),
719 m_io_handler_thread (),
720 m_sync_broadcaster (NULL, "lldb.debugger.sync")
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721{
Greg Clayton67cc0632012-08-22 17:17:09 +0000722 char instance_cstr[256];
723 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
724 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000725 if (log_callback)
726 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000727 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000728 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000729 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000730 assert (default_platform_sp.get());
731 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000732
Greg Clayton754a9362012-08-23 00:22:02 +0000733 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000734 m_collection_sp->AppendProperty (ConstString("target"),
735 ConstString("Settings specify to debugging targets."),
736 true,
737 Target::GetGlobalProperties()->GetValueProperties());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000738 m_collection_sp->AppendProperty (ConstString("platform"),
739 ConstString("Platform settings."),
740 true,
741 Platform::GetGlobalPlatformProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000742 if (m_command_interpreter_ap.get())
743 {
744 m_collection_sp->AppendProperty (ConstString("interpreter"),
745 ConstString("Settings specify to the debugger's command interpreter."),
746 true,
747 m_command_interpreter_ap->GetValueProperties());
748 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000749 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
750 term_width->SetMinimumValue(10);
751 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000752
753 // Turn off use-color if this is a dumb terminal.
754 const char *term = getenv ("TERM");
755 if (term && !strcmp (term, "dumb"))
756 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757}
758
759Debugger::~Debugger ()
760{
Jim Ingham8314c522011-09-15 21:36:42 +0000761 Clear();
762}
763
764void
765Debugger::Clear()
766{
Greg Clayton44d93782014-01-27 23:43:24 +0000767 ClearIOHandlers();
768 StopIOHandlerThread();
769 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000770 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000771 int num_targets = m_target_list.GetNumTargets();
772 for (int i = 0; i < num_targets; i++)
773 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000774 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
775 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000776 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000777 ProcessSP process_sp (target_sp->GetProcessSP());
778 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000779 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000780 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000781 }
Greg Clayton66111032010-06-23 01:19:29 +0000782 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000783 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000784
785 // Close the input file _before_ we close the input read communications class
786 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000787 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000788 if (m_input_file_sp)
789 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000790
791 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000792}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793
794bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000795Debugger::GetCloseInputOnEOF () const
796{
Greg Clayton44d93782014-01-27 23:43:24 +0000797// return m_input_comm.GetCloseOnEOF();
798 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000799}
800
801void
802Debugger::SetCloseInputOnEOF (bool b)
803{
Greg Clayton44d93782014-01-27 23:43:24 +0000804// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000805}
806
807bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808Debugger::GetAsyncExecution ()
809{
Greg Clayton66111032010-06-23 01:19:29 +0000810 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811}
812
813void
814Debugger::SetAsyncExecution (bool async_execution)
815{
Greg Clayton66111032010-06-23 01:19:29 +0000816 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817}
818
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819
820void
821Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
822{
Greg Clayton44d93782014-01-27 23:43:24 +0000823 if (m_input_file_sp)
824 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
825 else
826 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
827
828 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000829 if (in_file.IsValid() == false)
830 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831
Jim Inghamc5917d92012-11-30 20:23:19 +0000832 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
833 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834}
835
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836void
837Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
838{
Greg Clayton44d93782014-01-27 23:43:24 +0000839 if (m_output_file_sp)
840 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
841 else
842 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
843
844 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000845 if (out_file.IsValid() == false)
846 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000847
Enrico Granatab5887262012-10-29 21:18:03 +0000848 // do not create the ScriptInterpreter just for setting the output file handle
849 // as the constructor will know how to do the right thing on its own
850 const bool can_create = false;
851 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
852 if (script_interpreter)
853 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854}
855
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856void
857Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
858{
Greg Clayton44d93782014-01-27 23:43:24 +0000859 if (m_error_file_sp)
860 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
861 else
862 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
863
864 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000865 if (err_file.IsValid() == false)
866 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867}
868
Jim Inghamc5917d92012-11-30 20:23:19 +0000869void
870Debugger::SaveInputTerminalState ()
871{
Greg Clayton44d93782014-01-27 23:43:24 +0000872 if (m_input_file_sp)
873 {
874 File &in_file = m_input_file_sp->GetFile();
875 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
876 m_terminal_state.Save(in_file.GetDescriptor(), true);
877 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000878}
879
880void
881Debugger::RestoreInputTerminalState ()
882{
883 m_terminal_state.Restore();
884}
885
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000887Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888{
889 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000890 TargetSP target_sp(GetSelectedTarget());
891 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892
893 if (target_sp)
894 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000895 ProcessSP process_sp (target_sp->GetProcessSP());
896 exe_ctx.SetProcessSP (process_sp);
897 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000899 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
900 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000902 exe_ctx.SetThreadSP (thread_sp);
903 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
904 if (exe_ctx.GetFramePtr() == NULL)
905 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906 }
907 }
908 }
909 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910}
911
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912void
Caroline Ticeefed6132010-11-19 20:47:54 +0000913Debugger::DispatchInputInterrupt ()
914{
Greg Clayton44d93782014-01-27 23:43:24 +0000915 Mutex::Locker locker (m_input_reader_stack.GetMutex());
916 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000917 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000918 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000919}
920
921void
922Debugger::DispatchInputEndOfFile ()
923{
Greg Clayton44d93782014-01-27 23:43:24 +0000924 Mutex::Locker locker (m_input_reader_stack.GetMutex());
925 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000926 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000927 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000928}
929
930void
Greg Clayton44d93782014-01-27 23:43:24 +0000931Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000932{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000933 // The bottom input reader should be the main debugger input reader. We do not want to close that one here.
Greg Clayton44d93782014-01-27 23:43:24 +0000934 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000935 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000936 {
Greg Clayton44d93782014-01-27 23:43:24 +0000937 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000938 if (reader_sp)
Pavel Labath44464872015-05-27 12:40:32 +0000939 PopIOHandler (reader_sp);
Caroline Tice3d6086f2010-12-20 18:35:50 +0000940 }
941}
942
943void
Siva Chandra9aaab552015-02-26 19:26:36 +0000944Debugger::ExecuteIOHandlers()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000945{
Greg Clayton44d93782014-01-27 23:43:24 +0000946 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947 {
Greg Clayton44d93782014-01-27 23:43:24 +0000948 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 if (!reader_sp)
950 break;
951
Greg Clayton44d93782014-01-27 23:43:24 +0000952 reader_sp->Run();
Greg Clayton44d93782014-01-27 23:43:24 +0000953
954 // Remove all input readers that are done from the top of the stack
955 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956 {
Greg Clayton44d93782014-01-27 23:43:24 +0000957 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
958 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +0000959 PopIOHandler (top_reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000960 else
961 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 }
963 }
Greg Clayton44d93782014-01-27 23:43:24 +0000964 ClearIOHandlers();
965}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966
Greg Clayton44d93782014-01-27 23:43:24 +0000967bool
968Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
969{
970 return m_input_reader_stack.IsTop (reader_sp);
971}
972
Sean Callanan66810412015-10-19 23:11:07 +0000973bool
974Debugger::CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
975{
976 return m_input_reader_stack.CheckTopIOHandlerTypes (top_type, second_top_type);
977}
978
Pavel Labath44464872015-05-27 12:40:32 +0000979void
980Debugger::PrintAsync (const char *s, size_t len, bool is_stdout)
981{
982 lldb::StreamFileSP stream = is_stdout ? GetOutputFile() : GetErrorFile();
983 m_input_reader_stack.PrintAsync(stream.get(), s, len);
984}
Greg Clayton44d93782014-01-27 23:43:24 +0000985
986ConstString
987Debugger::GetTopIOHandlerControlSequence(char ch)
988{
989 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990}
991
Kate Stonea487aa42015-01-15 00:52:41 +0000992const char *
993Debugger::GetIOHandlerCommandPrefix()
994{
995 return m_input_reader_stack.GetTopIOHandlerCommandPrefix();
996}
997
998const char *
999Debugger::GetIOHandlerHelpPrologue()
1000{
1001 return m_input_reader_stack.GetTopIOHandlerHelpPrologue();
1002}
1003
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004void
Greg Clayton44d93782014-01-27 23:43:24 +00001005Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
1006{
Greg Clayton44d93782014-01-27 23:43:24 +00001007 PushIOHandler (reader_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001008
Greg Clayton577508d2014-06-20 00:23:57 +00001009 IOHandlerSP top_reader_sp = reader_sp;
1010 while (top_reader_sp)
1011 {
Greg Clayton577508d2014-06-20 00:23:57 +00001012 top_reader_sp->Run();
Pavel Labath44464872015-05-27 12:40:32 +00001013
Greg Clayton577508d2014-06-20 00:23:57 +00001014 if (top_reader_sp.get() == reader_sp.get())
1015 {
1016 if (PopIOHandler (reader_sp))
1017 break;
1018 }
Pavel Labath44464872015-05-27 12:40:32 +00001019
Greg Clayton577508d2014-06-20 00:23:57 +00001020 while (1)
1021 {
1022 top_reader_sp = m_input_reader_stack.Top();
1023 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +00001024 PopIOHandler (top_reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +00001025 else
1026 break;
1027 }
1028 }
Greg Clayton44d93782014-01-27 23:43:24 +00001029}
1030
1031void
1032Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
1033{
1034 // Before an IOHandler runs, it must have in/out/err streams.
1035 // This function is called when one ore more of the streams
1036 // are NULL. We use the top input reader's in/out/err streams,
1037 // or fall back to the debugger file handles, or we fall back
1038 // onto stdin/stdout/stderr as a last resort.
1039
1040 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1041 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
1042 // If no STDIN has been set, then set it appropriately
1043 if (!in)
1044 {
1045 if (top_reader_sp)
1046 in = top_reader_sp->GetInputStreamFile();
1047 else
1048 in = GetInputFile();
1049
1050 // If there is nothing, use stdin
1051 if (!in)
1052 in = StreamFileSP(new StreamFile(stdin, false));
1053 }
1054 // If no STDOUT has been set, then set it appropriately
1055 if (!out)
1056 {
1057 if (top_reader_sp)
1058 out = top_reader_sp->GetOutputStreamFile();
1059 else
1060 out = GetOutputFile();
1061
1062 // If there is nothing, use stdout
1063 if (!out)
1064 out = StreamFileSP(new StreamFile(stdout, false));
1065 }
1066 // If no STDERR has been set, then set it appropriately
1067 if (!err)
1068 {
1069 if (top_reader_sp)
1070 err = top_reader_sp->GetErrorStreamFile();
1071 else
1072 err = GetErrorFile();
1073
1074 // If there is nothing, use stderr
1075 if (!err)
1076 err = StreamFileSP(new StreamFile(stdout, false));
1077
1078 }
1079}
1080
1081void
1082Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083{
1084 if (!reader_sp)
1085 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +00001086
Pavel Labath44464872015-05-27 12:40:32 +00001087 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1088
1089 // Get the current top input reader...
Greg Clayton44d93782014-01-27 23:43:24 +00001090 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001091
Greg Claytonb4874f12014-02-28 18:22:24 +00001092 // Don't push the same IO handler twice...
Pavel Labath44464872015-05-27 12:40:32 +00001093 if (reader_sp == top_reader_sp)
1094 return;
Greg Clayton44d93782014-01-27 23:43:24 +00001095
Pavel Labath44464872015-05-27 12:40:32 +00001096 // Push our new input reader
1097 m_input_reader_stack.Push (reader_sp);
1098 reader_sp->Activate();
1099
1100 // Interrupt the top input reader to it will exit its Run() function
1101 // and let this new input reader take over
1102 if (top_reader_sp)
1103 {
1104 top_reader_sp->Deactivate();
1105 top_reader_sp->Cancel();
Greg Claytonb4874f12014-02-28 18:22:24 +00001106 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107}
1108
1109bool
Greg Clayton44d93782014-01-27 23:43:24 +00001110Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111{
Pavel Labath44464872015-05-27 12:40:32 +00001112 if (! pop_reader_sp)
1113 return false;
1114
Greg Clayton44d93782014-01-27 23:43:24 +00001115 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116
1117 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001118 // read on the stack refresh its prompt and if there is one...
Pavel Labath44464872015-05-27 12:40:32 +00001119 if (m_input_reader_stack.IsEmpty())
1120 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121
Greg Clayton44d93782014-01-27 23:43:24 +00001122 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Greg Clayton66111032010-06-23 01:19:29 +00001123
Pavel Labath44464872015-05-27 12:40:32 +00001124 if (pop_reader_sp != reader_sp)
1125 return false;
1126
1127 reader_sp->Deactivate();
1128 reader_sp->Cancel();
1129 m_input_reader_stack.Pop ();
1130
1131 reader_sp = m_input_reader_stack.Top();
1132 if (reader_sp)
1133 reader_sp->Activate();
1134
1135 return true;
1136}
Greg Clayton44d93782014-01-27 23:43:24 +00001137
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001138StreamSP
1139Debugger::GetAsyncOutputStream ()
1140{
Pavel Labath44464872015-05-27 12:40:32 +00001141 return StreamSP (new StreamAsynchronousIO (*this, true));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001142}
1143
1144StreamSP
1145Debugger::GetAsyncErrorStream ()
1146{
Pavel Labath44464872015-05-27 12:40:32 +00001147 return StreamSP (new StreamAsynchronousIO (*this, false));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001148}
1149
Greg Claytonc7bece562013-01-25 18:06:21 +00001150size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001151Debugger::GetNumDebuggers()
1152{
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001153 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001154 {
1155 Mutex::Locker locker (GetDebuggerListMutex ());
1156 return GetDebuggerList().size();
1157 }
1158 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001159}
1160
1161lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001162Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001163{
1164 DebuggerSP debugger_sp;
1165
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001166 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001167 {
1168 Mutex::Locker locker (GetDebuggerListMutex ());
1169 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001170
Greg Claytonc15f55e2012-03-30 20:53:46 +00001171 if (index < debugger_list.size())
1172 debugger_sp = debugger_list[index];
1173 }
1174
Enrico Granata061858c2012-02-15 02:34:21 +00001175 return debugger_sp;
1176}
1177
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001178DebuggerSP
1179Debugger::FindDebuggerWithID (lldb::user_id_t id)
1180{
Greg Clayton4d122c42011-09-17 08:33:22 +00001181 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001182
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001183 if (lldb_initialized)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001184 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001185 Mutex::Locker locker (GetDebuggerListMutex ());
1186 DebuggerList &debugger_list = GetDebuggerList();
1187 DebuggerList::iterator pos, end = debugger_list.end();
1188 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001189 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001190 if ((*pos).get()->GetID() == id)
1191 {
1192 debugger_sp = *pos;
1193 break;
1194 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001195 }
1196 }
1197 return debugger_sp;
1198}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001199
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001200#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001201static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001202TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001203{
1204 if (frame == NULL)
1205 return;
1206
1207 StreamString s;
1208 const char *prompt_format =
1209 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001210 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1211 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001212 "{process.id = '${process.id}'\n}"
1213 "{process.name = '${process.name}'\n}"
1214 "{process.file.basename = '${process.file.basename}'\n}"
1215 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1216 "{thread.id = '${thread.id}'\n}"
1217 "{thread.index = '${thread.index}'\n}"
1218 "{thread.name = '${thread.name}'\n}"
1219 "{thread.queue = '${thread.queue}'\n}"
1220 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1221 "{target.arch = '${target.arch}'\n}"
1222 "{module.file.basename = '${module.file.basename}'\n}"
1223 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1224 "{file.basename = '${file.basename}'\n}"
1225 "{file.fullpath = '${file.fullpath}'\n}"
1226 "{frame.index = '${frame.index}'\n}"
1227 "{frame.pc = '${frame.pc}'\n}"
1228 "{frame.sp = '${frame.sp}'\n}"
1229 "{frame.fp = '${frame.fp}'\n}"
1230 "{frame.flags = '${frame.flags}'\n}"
1231 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1232 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1233 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1234 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1235 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1236 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1237 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1238 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001239 "{function.changed = '${function.changed}'\n}"
1240 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001241 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001242 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001243 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001244 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001245 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001246 "{function.line-offset = '${function.line-offset}'\n}"
1247 "{function.pc-offset = '${function.pc-offset}'\n}"
1248 "{line.file.basename = '${line.file.basename}'\n}"
1249 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1250 "{line.number = '${line.number}'\n}"
1251 "{line.start-addr = '${line.start-addr}'\n}"
1252 "{line.end-addr = '${line.end-addr}'\n}"
1253;
1254
1255 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1256 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001257 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001258 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001259 {
1260 printf("%s\n", s.GetData());
1261 }
1262 else
1263 {
Greg Clayton1b654882010-09-19 02:33:57 +00001264 printf ("what we got: %s\n", s.GetData());
1265 }
1266}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001267#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001268
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001269bool
Greg Clayton554f68d2015-02-04 22:00:53 +00001270Debugger::FormatDisassemblerAddress (const FormatEntity::Entry *format,
Jason Molendaaff1b352014-10-10 23:07:36 +00001271 const SymbolContext *sc,
1272 const SymbolContext *prev_sc,
1273 const ExecutionContext *exe_ctx,
1274 const Address *addr,
1275 Stream &s)
1276{
Greg Clayton554f68d2015-02-04 22:00:53 +00001277 FormatEntity::Entry format_entry;
1278
1279 if (format == NULL)
Jason Molendaaff1b352014-10-10 23:07:36 +00001280 {
Greg Clayton554f68d2015-02-04 22:00:53 +00001281 if (exe_ctx != NULL && exe_ctx->HasTargetScope())
1282 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1283 if (format == NULL)
1284 {
1285 FormatEntity::Parse("${addr}: ", format_entry);
1286 format = &format_entry;
1287 }
Jason Molendaaff1b352014-10-10 23:07:36 +00001288 }
1289 bool function_changed = false;
1290 bool initial_function = false;
1291 if (prev_sc && (prev_sc->function || prev_sc->symbol))
1292 {
1293 if (sc && (sc->function || sc->symbol))
1294 {
1295 if (prev_sc->symbol && sc->symbol)
1296 {
1297 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
1298 {
1299 function_changed = true;
1300 }
1301 }
1302 else if (prev_sc->function && sc->function)
1303 {
1304 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
1305 {
1306 function_changed = true;
1307 }
1308 }
1309 }
1310 }
1311 // The first context on a list of instructions will have a prev_sc that
1312 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
1313 // would return false. But we do get a prev_sc pointer.
1314 if ((sc && (sc->function || sc->symbol))
1315 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
1316 {
1317 initial_function = true;
1318 }
Greg Clayton554f68d2015-02-04 22:00:53 +00001319 return FormatEntity::Format(*format, s, sc, exe_ctx, addr, NULL, function_changed, initial_function);
Jason Molendaaff1b352014-10-10 23:07:36 +00001320}
1321
1322
Jim Ingham228063c2012-02-21 02:23:08 +00001323void
1324Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1325{
Jim Ingham4f02b222012-02-22 22:49:20 +00001326 // For simplicity's sake, I am not going to deal with how to close down any
1327 // open logging streams, I just redirect everything from here on out to the
1328 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00001329 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
1330}
1331
1332bool
1333Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
1334{
Jim Ingham228063c2012-02-21 02:23:08 +00001335 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001336 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001337 {
1338 log_stream_sp = m_log_callback_stream_sp;
1339 // For now when using the callback mode you always get thread & timestamp.
1340 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1341 }
1342 else if (log_file == NULL || *log_file == '\0')
1343 {
Greg Clayton44d93782014-01-27 23:43:24 +00001344 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00001345 }
1346 else
1347 {
1348 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001349 if (pos != m_log_streams.end())
1350 log_stream_sp = pos->second.lock();
1351 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001352 {
Pavel Labath8ac06992015-03-20 09:43:20 +00001353 uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate
1354 | File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
1355 if (! (log_options & LLDB_LOG_OPTION_APPEND))
1356 options |= File::eOpenOptionTruncate;
1357
1358 log_stream_sp.reset (new StreamFile (log_file, options));
Jim Ingham228063c2012-02-21 02:23:08 +00001359 m_log_streams[log_file] = log_stream_sp;
1360 }
Jim Ingham228063c2012-02-21 02:23:08 +00001361 }
1362 assert (log_stream_sp.get());
1363
1364 if (log_options == 0)
1365 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
1366
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +00001367 return Log::EnableLogChannel(log_stream_sp, log_options, channel, categories, error_stream);
Jim Ingham228063c2012-02-21 02:23:08 +00001368}
1369
Greg Clayton9585fbf2013-03-19 00:20:55 +00001370SourceManager &
1371Debugger::GetSourceManager ()
1372{
1373 if (m_source_manager_ap.get() == NULL)
1374 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
1375 return *m_source_manager_ap;
1376}
1377
1378
Greg Clayton44d93782014-01-27 23:43:24 +00001379
1380// This function handles events that were broadcast by the process.
1381void
1382Debugger::HandleBreakpointEvent (const EventSP &event_sp)
1383{
1384 using namespace lldb;
1385 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
1386
1387// if (event_type & eBreakpointEventTypeAdded
1388// || event_type & eBreakpointEventTypeRemoved
1389// || event_type & eBreakpointEventTypeEnabled
1390// || event_type & eBreakpointEventTypeDisabled
1391// || event_type & eBreakpointEventTypeCommandChanged
1392// || event_type & eBreakpointEventTypeConditionChanged
1393// || event_type & eBreakpointEventTypeIgnoreChanged
1394// || event_type & eBreakpointEventTypeLocationsResolved)
1395// {
1396// // Don't do anything about these events, since the breakpoint commands already echo these actions.
1397// }
1398//
1399 if (event_type & eBreakpointEventTypeLocationsAdded)
1400 {
1401 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
1402 if (num_new_locations > 0)
1403 {
1404 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001405 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001406 if (output_sp)
1407 {
1408 output_sp->Printf("%d location%s added to breakpoint %d\n",
1409 num_new_locations,
1410 num_new_locations == 1 ? "" : "s",
1411 breakpoint->GetID());
Pavel Labath44464872015-05-27 12:40:32 +00001412 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001413 }
1414 }
1415 }
1416// else if (event_type & eBreakpointEventTypeLocationsRemoved)
1417// {
1418// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
1419// }
1420// else if (event_type & eBreakpointEventTypeLocationsResolved)
1421// {
1422// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
1423// }
1424}
1425
1426size_t
1427Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
1428{
1429 size_t total_bytes = 0;
1430 if (stream == NULL)
1431 stream = GetOutputFile().get();
1432
1433 if (stream)
1434 {
1435 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
1436 if (process == NULL)
1437 {
1438 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1439 if (target_sp)
1440 process = target_sp->GetProcessSP().get();
1441 }
1442 if (process)
1443 {
1444 Error error;
1445 size_t len;
1446 char stdio_buffer[1024];
1447 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1448 {
1449 stream->Write(stdio_buffer, len);
1450 total_bytes += len;
1451 }
1452 }
1453 stream->Flush();
1454 }
1455 return total_bytes;
1456}
1457
1458size_t
1459Debugger::GetProcessSTDERR (Process *process, Stream *stream)
1460{
1461 size_t total_bytes = 0;
1462 if (stream == NULL)
1463 stream = GetOutputFile().get();
1464
1465 if (stream)
1466 {
1467 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
1468 if (process == NULL)
1469 {
1470 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1471 if (target_sp)
1472 process = target_sp->GetProcessSP().get();
1473 }
1474 if (process)
1475 {
1476 Error error;
1477 size_t len;
1478 char stdio_buffer[1024];
1479 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1480 {
1481 stream->Write(stdio_buffer, len);
1482 total_bytes += len;
1483 }
1484 }
1485 stream->Flush();
1486 }
1487 return total_bytes;
1488}
1489
Greg Claytondc6224e2014-10-21 01:00:42 +00001490
Greg Clayton44d93782014-01-27 23:43:24 +00001491// This function handles events that were broadcast by the process.
1492void
1493Debugger::HandleProcessEvent (const EventSP &event_sp)
1494{
1495 using namespace lldb;
1496 const uint32_t event_type = event_sp->GetType();
1497 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00001498
Pavel Labath44464872015-05-27 12:40:32 +00001499 StreamSP output_stream_sp = GetAsyncOutputStream();
1500 StreamSP error_stream_sp = GetAsyncErrorStream();
Greg Clayton44d93782014-01-27 23:43:24 +00001501 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00001502
Greg Claytonb4874f12014-02-28 18:22:24 +00001503 if (!gui_enabled)
1504 {
1505 bool pop_process_io_handler = false;
1506 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00001507
Pavel Labath44464872015-05-27 12:40:32 +00001508 bool state_is_stopped = false;
1509 const bool got_state_changed = (event_type & Process::eBroadcastBitStateChanged) != 0;
1510 const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
1511 const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
1512 if (got_state_changed)
Greg Clayton44d93782014-01-27 23:43:24 +00001513 {
Pavel Labath44464872015-05-27 12:40:32 +00001514 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
1515 state_is_stopped = StateIsStoppedState(event_state, false);
Greg Claytonb4874f12014-02-28 18:22:24 +00001516 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001517
Pavel Labath44464872015-05-27 12:40:32 +00001518 // Display running state changes first before any STDIO
1519 if (got_state_changed && !state_is_stopped)
Greg Claytonb4874f12014-02-28 18:22:24 +00001520 {
Pavel Labath44464872015-05-27 12:40:32 +00001521 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
Greg Claytonb4874f12014-02-28 18:22:24 +00001522 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001523
Pavel Labath44464872015-05-27 12:40:32 +00001524 // Now display and STDOUT
1525 if (got_stdout || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001526 {
Pavel Labath44464872015-05-27 12:40:32 +00001527 GetProcessSTDOUT (process_sp.get(), output_stream_sp.get());
Greg Clayton44d93782014-01-27 23:43:24 +00001528 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001529
Pavel Labath44464872015-05-27 12:40:32 +00001530 // Now display and STDERR
1531 if (got_stderr || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001532 {
Pavel Labath44464872015-05-27 12:40:32 +00001533 GetProcessSTDERR (process_sp.get(), error_stream_sp.get());
Greg Claytonb4874f12014-02-28 18:22:24 +00001534 }
1535
Pavel Labath44464872015-05-27 12:40:32 +00001536 // Now display any stopped state changes after any STDIO
1537 if (got_state_changed && state_is_stopped)
1538 {
1539 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
1540 }
1541
1542 output_stream_sp->Flush();
1543 error_stream_sp->Flush();
1544
Greg Claytonb4874f12014-02-28 18:22:24 +00001545 if (pop_process_io_handler)
1546 process_sp->PopProcessIOHandler();
1547 }
Greg Clayton44d93782014-01-27 23:43:24 +00001548}
1549
1550void
1551Debugger::HandleThreadEvent (const EventSP &event_sp)
1552{
1553 // At present the only thread event we handle is the Frame Changed event,
1554 // and all we do for that is just reprint the thread status for that thread.
1555 using namespace lldb;
1556 const uint32_t event_type = event_sp->GetType();
1557 if (event_type == Thread::eBroadcastBitStackChanged ||
1558 event_type == Thread::eBroadcastBitThreadSelected )
1559 {
1560 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
1561 if (thread_sp)
1562 {
Pavel Labath44464872015-05-27 12:40:32 +00001563 thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00001564 }
1565 }
1566}
1567
1568bool
1569Debugger::IsForwardingEvents ()
1570{
1571 return (bool)m_forward_listener_sp;
1572}
1573
1574void
1575Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
1576{
1577 m_forward_listener_sp = listener_sp;
1578}
1579
1580void
1581Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
1582{
1583 m_forward_listener_sp.reset();
1584}
1585
1586
1587void
1588Debugger::DefaultEventHandler()
1589{
1590 Listener& listener(GetListener());
1591 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
1592 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
1593 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
1594 BroadcastEventSpec target_event_spec (broadcaster_class_target,
1595 Target::eBroadcastBitBreakpointChanged);
1596
1597 BroadcastEventSpec process_event_spec (broadcaster_class_process,
1598 Process::eBroadcastBitStateChanged |
1599 Process::eBroadcastBitSTDOUT |
1600 Process::eBroadcastBitSTDERR);
1601
1602 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
1603 Thread::eBroadcastBitStackChanged |
1604 Thread::eBroadcastBitThreadSelected );
1605
1606 listener.StartListeningForEventSpec (*this, target_event_spec);
1607 listener.StartListeningForEventSpec (*this, process_event_spec);
1608 listener.StartListeningForEventSpec (*this, thread_event_spec);
1609 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
1610 CommandInterpreter::eBroadcastBitQuitCommandReceived |
1611 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
1612 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
Greg Claytonafa91e332014-12-01 22:41:27 +00001613
1614 // Let the thread that spawned us know that we have started up and
1615 // that we are now listening to all required events so no events get missed
1616 m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
1617
Greg Clayton44d93782014-01-27 23:43:24 +00001618 bool done = false;
1619 while (!done)
1620 {
Greg Clayton44d93782014-01-27 23:43:24 +00001621 EventSP event_sp;
1622 if (listener.WaitForEvent(NULL, event_sp))
1623 {
1624 if (event_sp)
1625 {
1626 Broadcaster *broadcaster = event_sp->GetBroadcaster();
1627 if (broadcaster)
1628 {
1629 uint32_t event_type = event_sp->GetType();
1630 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
1631 if (broadcaster_class == broadcaster_class_process)
1632 {
1633 HandleProcessEvent (event_sp);
1634 }
1635 else if (broadcaster_class == broadcaster_class_target)
1636 {
1637 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
1638 {
1639 HandleBreakpointEvent (event_sp);
1640 }
1641 }
1642 else if (broadcaster_class == broadcaster_class_thread)
1643 {
1644 HandleThreadEvent (event_sp);
1645 }
1646 else if (broadcaster == m_command_interpreter_ap.get())
1647 {
1648 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
1649 {
1650 done = true;
1651 }
1652 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
1653 {
1654 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1655 if (data && data[0])
1656 {
Pavel Labath44464872015-05-27 12:40:32 +00001657 StreamSP error_sp (GetAsyncErrorStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001658 if (error_sp)
1659 {
Greg Clayton44d93782014-01-27 23:43:24 +00001660 error_sp->PutCString(data);
1661 error_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001662 }
1663 }
1664 }
1665 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
1666 {
1667 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1668 if (data && data[0])
1669 {
Pavel Labath44464872015-05-27 12:40:32 +00001670 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001671 if (output_sp)
1672 {
Greg Clayton44d93782014-01-27 23:43:24 +00001673 output_sp->PutCString(data);
1674 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001675 }
1676 }
1677 }
1678 }
1679 }
1680
1681 if (m_forward_listener_sp)
1682 m_forward_listener_sp->AddEvent(event_sp);
1683 }
1684 }
1685 }
1686}
1687
1688lldb::thread_result_t
1689Debugger::EventHandlerThread (lldb::thread_arg_t arg)
1690{
1691 ((Debugger *)arg)->DefaultEventHandler();
1692 return NULL;
1693}
1694
1695bool
1696Debugger::StartEventHandlerThread()
1697{
Zachary Turneracee96a2014-09-23 18:32:09 +00001698 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001699 {
Greg Claytonafa91e332014-12-01 22:41:27 +00001700 // We must synchronize with the DefaultEventHandler() thread to ensure
1701 // it is up and running and listening to events before we return from
1702 // this function. We do this by listening to events for the
1703 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
1704 Listener listener("lldb.debugger.event-handler");
1705 listener.StartListeningForEvents(&m_sync_broadcaster, eBroadcastBitEventThreadIsListening);
1706
Zachary Turner7c2896a2014-10-24 22:06:29 +00001707 // Use larger 8MB stack for this thread
Greg Claytonafa91e332014-12-01 22:41:27 +00001708 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread,
1709 this,
1710 NULL,
Zachary Turner7c2896a2014-10-24 22:06:29 +00001711 g_debugger_event_thread_stack_bytes);
Greg Claytonafa91e332014-12-01 22:41:27 +00001712
1713 // Make sure DefaultEventHandler() is running and listening to events before we return
1714 // from this function. We are only listening for events of type
1715 // eBroadcastBitEventThreadIsListening so we don't need to check the event, we just need
1716 // to wait an infinite amount of time for it (NULL timeout as the first parameter)
1717 lldb::EventSP event_sp;
1718 listener.WaitForEvent(NULL, event_sp);
Greg Clayton807b6b32014-10-15 18:03:59 +00001719 }
Zachary Turneracee96a2014-09-23 18:32:09 +00001720 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001721}
1722
1723void
1724Debugger::StopEventHandlerThread()
1725{
Zachary Turneracee96a2014-09-23 18:32:09 +00001726 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001727 {
1728 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00001729 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001730 }
1731}
1732
1733
1734lldb::thread_result_t
1735Debugger::IOHandlerThread (lldb::thread_arg_t arg)
1736{
1737 Debugger *debugger = (Debugger *)arg;
Siva Chandra9aaab552015-02-26 19:26:36 +00001738 debugger->ExecuteIOHandlers();
Greg Clayton44d93782014-01-27 23:43:24 +00001739 debugger->StopEventHandlerThread();
1740 return NULL;
1741}
1742
1743bool
Sean Callanan66810412015-10-19 23:11:07 +00001744Debugger::HasIOHandlerThread()
1745{
1746 return m_io_handler_thread.IsJoinable();
1747}
1748
1749bool
Greg Clayton44d93782014-01-27 23:43:24 +00001750Debugger::StartIOHandlerThread()
1751{
Zachary Turneracee96a2014-09-23 18:32:09 +00001752 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001753 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
1754 IOHandlerThread,
1755 this,
1756 NULL,
1757 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00001758 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001759}
1760
1761void
1762Debugger::StopIOHandlerThread()
1763{
Zachary Turneracee96a2014-09-23 18:32:09 +00001764 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001765 {
1766 if (m_input_file_sp)
1767 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00001768 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001769 }
1770}
1771
Sean Callanan66810412015-10-19 23:11:07 +00001772void
1773Debugger::JoinIOHandlerThread()
1774{
1775 if (HasIOHandlerThread())
1776 {
1777 thread_result_t result;
1778 m_io_handler_thread.Join(&result);
1779 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
1780 }
1781}
1782
Jim Ingham893c9322014-11-22 01:42:44 +00001783Target *
1784Debugger::GetDummyTarget()
1785{
1786 return m_target_list.GetDummyTarget (*this).get();
1787}
1788
1789Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001790Debugger::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001791{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001792 Target *target = nullptr;
1793 if (!prefer_dummy)
1794 {
1795 target = m_target_list.GetSelectedTarget().get();
1796 if (target)
1797 return target;
1798 }
1799
Jim Ingham893c9322014-11-22 01:42:44 +00001800 return GetDummyTarget();
1801}
Greg Clayton44d93782014-01-27 23:43:24 +00001802
Sean Callanan3e7e9152015-10-20 00:23:46 +00001803Error
1804Debugger::RunREPL (LanguageType language, const char *repl_options)
1805{
1806 Error err;
1807 FileSpec repl_executable;
Sean Callanan3e7e9152015-10-20 00:23:46 +00001808
Sean Callanan97f84e82015-10-21 19:31:17 +00001809 if (language == eLanguageTypeUnknown)
1810 {
1811 std::set<LanguageType> repl_languages;
1812
1813 Language::GetLanguagesSupportingREPLs(repl_languages);
1814
1815 if (repl_languages.size() == 1)
1816 {
1817 language = *repl_languages.begin();
1818 }
1819 else if (repl_languages.size() == 0)
1820 {
1821 err.SetErrorStringWithFormat("LLDB isn't configured with support support for any REPLs.");
1822 return err;
1823 }
1824 else
1825 {
1826 err.SetErrorStringWithFormat("Multiple possible REPL languages. Please specify a language.");
1827 return err;
1828 }
1829 }
1830
Sean Callanan3e7e9152015-10-20 00:23:46 +00001831 Target *const target = nullptr; // passing in an empty target means the REPL must create one
1832
Sean Callanan3b682de2015-10-21 00:28:44 +00001833 REPLSP repl_sp(REPL::Create(err, language, this, target, repl_options));
Sean Callanan3e7e9152015-10-20 00:23:46 +00001834
1835 if (!err.Success())
1836 {
1837 return err;
1838 }
1839
1840 if (!repl_sp)
1841 {
1842 err.SetErrorStringWithFormat("couldn't find a REPL for %s", Language::GetNameForLanguageType(language));
1843 return err;
1844 }
1845
1846 repl_sp->SetCompilerOptions(repl_options);
1847 repl_sp->RunLoop();
1848
1849 return err;
1850}
1851