blob: e1779abb9616b042930085889c5d11a096e06f41 [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"
Zachary Turner93a66fc2014-10-06 21:22:36 +000034#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000035#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000036#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000037#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000038#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000039#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000040#include "lldb/Interpreter/OptionValueSInt64.h"
41#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000042#include "lldb/Symbol/CompileUnit.h"
43#include "lldb/Symbol/Function.h"
44#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000045#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Target/TargetList.h"
47#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000048#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000049#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000050#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000051#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000053#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Zachary Turner58a559c2014-08-27 20:15:09 +000055#include "llvm/Support/DynamicLibrary.h"
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057using namespace lldb;
58using namespace lldb_private;
59
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Caroline Ticeebc1bb22010-06-30 16:22:25 +000061static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000062static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000063
Greg Clayton1b654882010-09-19 02:33:57 +000064#pragma mark Static Functions
65
66static Mutex &
67GetDebuggerListMutex ()
68{
69 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
70 return g_mutex;
71}
72
73typedef std::vector<DebuggerSP> DebuggerList;
74
75static DebuggerList &
76GetDebuggerList()
77{
78 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000079 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000080 static DebuggerList g_list;
81 return g_list;
82}
Greg Claytone372b982011-11-21 21:44:34 +000083
84OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000085g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000086{
Greg Clayton67cc0632012-08-22 17:17:09 +000087 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
88 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
89 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000090 { 0, NULL, NULL }
91};
92
Greg Clayton67cc0632012-08-22 17:17:09 +000093OptionEnumValueElement
94g_language_enumerators[] =
95{
96 { eScriptLanguageNone, "none", "Disable scripting languages."},
97 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
98 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000099 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000100};
Greg Claytone372b982011-11-21 21:44:34 +0000101
Greg Clayton67cc0632012-08-22 17:17:09 +0000102#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
103#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Jason Molenda6ab659a2015-07-29 00:42:47 +0000104#define IS_OPTIMIZED "{${function.is-optimized} [opt]}"
Greg Clayton67cc0632012-08-22 17:17:09 +0000105
Michael Sartain0769b2b2013-07-30 16:44:36 +0000106#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000107 "{, ${frame.pc}}"\
108 MODULE_WITH_FUNC\
109 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000110 "{, name = '${thread.name}'}"\
111 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000112 "{, activity = '${thread.info.activity.name}'}" \
113 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000114 "{, stop reason = ${thread.stop-reason}}"\
115 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000116 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000117 "\\n"
118
119#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
120 MODULE_WITH_FUNC\
121 FILE_AND_LINE\
Jason Molenda6ab659a2015-07-29 00:42:47 +0000122 IS_OPTIMIZED\
Greg Clayton67cc0632012-08-22 17:17:09 +0000123 "\\n"
124
Jason Molendac980fa92015-02-13 23:24:21 +0000125// Three parts to this disassembly format specification:
126// 1. If this is a new function/symbol (no previous symbol/function), print
127// dylib`funcname:\n
128// 2. If this is a symbol context change (different from previous symbol/function), print
129// dylib`funcname:\n
130// 3. print
131// address <+offset>:
132#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}>}: "
133
134// gdb's disassembly format can be emulated with
135// ${current-pc-arrow}${addr-file-or-load}{ <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>}:
136
137// lldb's original format for disassembly would look like this format string -
138// {${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}}:
139
Greg Clayton67cc0632012-08-22 17:17:09 +0000140
Greg Clayton754a9362012-08-23 00:22:02 +0000141static PropertyDefinition
142g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000143{
Greg Clayton554f68d2015-02-04 22:00:53 +0000144{ "auto-confirm", OptionValue::eTypeBoolean , true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
145{ "disassembly-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." },
146{ "frame-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
147{ "notify-void", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
148{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
149{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
150{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
151{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
152{ "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." },
153{ "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." },
154{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
155{ "thread-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
156{ "use-external-editor", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Whether to use an external editor or not." },
157{ "use-color", OptionValue::eTypeBoolean , true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
158{ "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 +0000159{ "auto-indent", OptionValue::eTypeBoolean , true, true , NULL, NULL, "If true, LLDB will auto indent/outdent code. Currently only supported in the REPL (default: true)." },
160{ "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)." },
161{ "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 +0000162{ "escape-non-printables", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically escape non-printable and escape characters when formatting strings." },
163{ NULL, OptionValue::eTypeInvalid , true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000164};
165
166enum
167{
168 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000169 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000170 ePropertyFrameFormat,
171 ePropertyNotiftVoid,
172 ePropertyPrompt,
173 ePropertyScriptLanguage,
174 ePropertyStopDisassemblyCount,
175 ePropertyStopDisassemblyDisplay,
176 ePropertyStopLineCountAfter,
177 ePropertyStopLineCountBefore,
178 ePropertyTerminalWidth,
179 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000180 ePropertyUseExternalEditor,
181 ePropertyUseColor,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000182 ePropertyAutoOneLineSummaries,
Sean Callanan66810412015-10-19 23:11:07 +0000183 ePropertyAutoIndent,
184 ePropertyPrintDecls,
185 ePropertyTabSize,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000186 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000187};
188
Zachary Turner3a006912015-03-19 22:00:21 +0000189LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000190
191Error
192Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
193 VarSetOperationType op,
194 const char *property_path,
195 const char *value)
196{
Enrico Granata84a53df2013-05-20 22:29:23 +0000197 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000198 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000199 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000200 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000201 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 Clayton4c054102012-09-01 00:38:36 +0000206 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
207 if (error.Success())
208 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000209 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000210 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
211 {
212 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000213 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
214 if (str.length())
215 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000216 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000217 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
218 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
219 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000220 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 Granata397ddd52013-05-21 20:13:34 +0000225 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000226 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000227 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000228 {
229 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000230 StreamString feedback_stream;
231 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000232 {
Greg Clayton44d93782014-01-27 23:43:24 +0000233 StreamFileSP stream_sp (GetErrorFile());
234 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000235 {
Greg Clayton44d93782014-01-27 23:43:24 +0000236 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 Granata84a53df2013-05-20 22:29:23 +0000242 }
243 }
244 }
245 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000246 else if (is_escape_non_printables)
247 {
248 DataVisualization::ForceUpdate();
249 }
Greg Clayton4c054102012-09-01 00:38:36 +0000250 }
251 return error;
252}
253
Greg Clayton67cc0632012-08-22 17:17:09 +0000254bool
255Debugger::GetAutoConfirm () const
256{
257 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000258 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000259}
260
Greg Clayton554f68d2015-02-04 22:00:53 +0000261const FormatEntity::Entry *
Jason Molendaaff1b352014-10-10 23:07:36 +0000262Debugger::GetDisassemblyFormat() const
263{
264 const uint32_t idx = ePropertyDisassemblyFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000265 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Jason Molendaaff1b352014-10-10 23:07:36 +0000266}
267
Greg Clayton554f68d2015-02-04 22:00:53 +0000268const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000269Debugger::GetFrameFormat() const
270{
271 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000272 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000273}
274
275bool
276Debugger::GetNotifyVoid () const
277{
278 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000279 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000280}
281
282const char *
283Debugger::GetPrompt() const
284{
285 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000286 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000287}
288
289void
290Debugger::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 Sartainc3ce7f272013-05-23 20:47:45 +0000295 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
296 if (str.length())
297 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000298 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000299}
300
Greg Clayton554f68d2015-02-04 22:00:53 +0000301const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000302Debugger::GetThreadFormat() const
303{
304 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000305 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000306}
307
308lldb::ScriptLanguage
309Debugger::GetScriptLanguage() const
310{
311 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000312 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000313}
314
315bool
316Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
317{
318 const uint32_t idx = ePropertyScriptLanguage;
319 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
320}
321
322uint32_t
323Debugger::GetTerminalWidth () const
324{
325 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000326 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000327}
328
329bool
330Debugger::SetTerminalWidth (uint32_t term_width)
331{
332 const uint32_t idx = ePropertyTerminalWidth;
333 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
334}
335
336bool
337Debugger::GetUseExternalEditor () const
338{
339 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000340 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000341}
342
343bool
344Debugger::SetUseExternalEditor (bool b)
345{
346 const uint32_t idx = ePropertyUseExternalEditor;
347 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
348}
349
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000350bool
351Debugger::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
357bool
358Debugger::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 Clayton67cc0632012-08-22 17:17:09 +0000366uint32_t
367Debugger::GetStopSourceLineCount (bool before) const
368{
369 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000370 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000371}
372
373Debugger::StopDisassemblyType
374Debugger::GetStopDisassemblyDisplay () const
375{
376 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000377 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000378}
379
380uint32_t
381Debugger::GetDisassemblyLineCount () const
382{
383 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000384 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000385}
Greg Claytone372b982011-11-21 21:44:34 +0000386
Enrico Granata553fad52013-10-25 23:09:40 +0000387bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000388Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000389{
Enrico Granata90a8db32013-10-31 21:01:07 +0000390 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000391 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000392}
Enrico Granata553fad52013-10-25 23:09:40 +0000393
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000394bool
395Debugger::GetEscapeNonPrintables () const
396{
397 const uint32_t idx = ePropertyEscapeNonPrintables;
398 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000399}
400
Sean Callanan66810412015-10-19 23:11:07 +0000401bool
402Debugger::GetAutoIndent () const
403{
404 const uint32_t idx = ePropertyAutoIndent;
405 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
406}
407
408bool
409Debugger::SetAutoIndent (bool b)
410{
411 const uint32_t idx = ePropertyAutoIndent;
412 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
413}
414
415bool
416Debugger::GetPrintDecls () const
417{
418 const uint32_t idx = ePropertyPrintDecls;
419 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
420}
421
422bool
423Debugger::SetPrintDecls (bool b)
424{
425 const uint32_t idx = ePropertyPrintDecls;
426 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
427}
428
429uint32_t
430Debugger::GetTabSize () const
431{
432 const uint32_t idx = ePropertyTabSize;
433 return m_collection_sp->GetPropertyAtIndexAsUInt64 (NULL, idx, g_properties[idx].default_uint_value);
434}
435
436bool
437Debugger::SetTabSize (uint32_t tab_size)
438{
439 const uint32_t idx = ePropertyTabSize;
440 return m_collection_sp->SetPropertyAtIndexAsUInt64 (NULL, idx, tab_size);
441}
442
443
Greg Clayton1b654882010-09-19 02:33:57 +0000444#pragma mark Debugger
445
Greg Clayton67cc0632012-08-22 17:17:09 +0000446//const DebuggerPropertiesSP &
447//Debugger::GetSettings() const
448//{
449// return m_properties_sp;
450//}
451//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000452
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000453static bool lldb_initialized = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454void
Zachary Turner3a006912015-03-19 22:00:21 +0000455Debugger::Initialize(LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000457 assert(!lldb_initialized && "Debugger::Initialize called more than once!");
458
Robert Flackf196c932015-03-10 18:07:47 +0000459 lldb_initialized = true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000460 g_load_plugin_callback = load_plugin_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000463void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464Debugger::Terminate ()
465{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000466 assert(lldb_initialized && "Debugger::Terminate called without a matching Debugger::Initialize!");
467
468 // Clear our master list of debugger objects
469 Mutex::Locker locker (GetDebuggerListMutex ());
Oleksiy Vyalovf3cd1812015-09-08 16:26:32 +0000470 auto& debuggers = GetDebuggerList();
471 for (const auto& debugger: debuggers)
472 debugger->Clear();
473
474 debuggers.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475}
476
Caroline Tice20bd37f2011-03-10 22:14:10 +0000477void
478Debugger::SettingsInitialize ()
479{
Greg Clayton6920b522012-08-22 18:39:03 +0000480 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000481}
482
483void
484Debugger::SettingsTerminate ()
485{
Greg Clayton6920b522012-08-22 18:39:03 +0000486 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000487}
488
Enrico Granata21dfcd92012-09-28 23:57:51 +0000489bool
Enrico Granatae743c782013-04-24 21:29:08 +0000490Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000491{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000492 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000493 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000494 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
495 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000496 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000497 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000498 return true;
499 }
Enrico Granatae743c782013-04-24 21:29:08 +0000500 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000501 else
Enrico Granatae743c782013-04-24 21:29:08 +0000502 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000503 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
504 // and if the public API layer isn't available (code is linking against
505 // all of the internal LLDB static libraries), then we can't load plugins
506 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000507 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000508 return false;
509}
510
511static FileSpec::EnumerateDirectoryResult
512LoadPluginCallback
513(
514 void *baton,
515 FileSpec::FileType file_type,
516 const FileSpec &file_spec
517 )
518{
519 Error error;
520
521 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000522 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000523
524 if (!baton)
525 return FileSpec::eEnumerateDirectoryResultQuit;
526
527 Debugger *debugger = (Debugger*)baton;
528
529 // If we have a regular file, a symbolic link or unknown file type, try
530 // and process the file. We must handle unknown as sometimes the directory
531 // enumeration might be enumerating a file system that doesn't have correct
532 // file type information.
533 if (file_type == FileSpec::eFileTypeRegular ||
534 file_type == FileSpec::eFileTypeSymbolicLink ||
535 file_type == FileSpec::eFileTypeUnknown )
536 {
537 FileSpec plugin_file_spec (file_spec);
538 plugin_file_spec.ResolvePath ();
539
Michael Sartain3cf443d2013-07-17 00:26:30 +0000540 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
541 plugin_file_spec.GetFileNameExtension() != g_solibext)
542 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000543 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000544 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000545
Enrico Granatae743c782013-04-24 21:29:08 +0000546 Error plugin_load_error;
547 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000548
549 return FileSpec::eEnumerateDirectoryResultNext;
550 }
551
552 else if (file_type == FileSpec::eFileTypeUnknown ||
553 file_type == FileSpec::eFileTypeDirectory ||
554 file_type == FileSpec::eFileTypeSymbolicLink )
555 {
556 // Try and recurse into anything that a directory or symbolic link.
557 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000558 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000559 // information.
560 return FileSpec::eEnumerateDirectoryResultEnter;
561 }
562
563 return FileSpec::eEnumerateDirectoryResultNext;
564}
565
566void
567Debugger::InstanceInitialize ()
568{
569 FileSpec dir_spec;
570 const bool find_directories = true;
571 const bool find_files = true;
572 const bool find_other = true;
573 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000574 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000575 {
576 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
577 {
578 FileSpec::EnumerateDirectory (dir_path,
579 find_directories,
580 find_files,
581 find_other,
582 LoadPluginCallback,
583 this);
584 }
585 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000586
587 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000588 {
589 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
590 {
591 FileSpec::EnumerateDirectory (dir_path,
592 find_directories,
593 find_files,
594 find_other,
595 LoadPluginCallback,
596 this);
597 }
598 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000599
600 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000601}
602
Greg Clayton66111032010-06-23 01:19:29 +0000603DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000604Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000605{
Jim Ingham228063c2012-02-21 02:23:08 +0000606 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000607 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000608 {
609 Mutex::Locker locker (GetDebuggerListMutex ());
610 GetDebuggerList().push_back(debugger_sp);
611 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000612 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000613 return debugger_sp;
614}
615
Caroline Ticee02657b2011-01-22 01:02:07 +0000616void
Greg Clayton4d122c42011-09-17 08:33:22 +0000617Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000618{
619 if (debugger_sp.get() == NULL)
620 return;
621
Jim Ingham8314c522011-09-15 21:36:42 +0000622 debugger_sp->Clear();
623
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000624 if (lldb_initialized)
Caroline Ticee02657b2011-01-22 01:02:07 +0000625 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000626 Mutex::Locker locker (GetDebuggerListMutex ());
627 DebuggerList &debugger_list = GetDebuggerList ();
628 DebuggerList::iterator pos, end = debugger_list.end();
629 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000630 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000631 if ((*pos).get() == debugger_sp.get())
632 {
633 debugger_list.erase (pos);
634 return;
635 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000636 }
637 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000638}
639
Greg Clayton4d122c42011-09-17 08:33:22 +0000640DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000641Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
642{
Greg Clayton4d122c42011-09-17 08:33:22 +0000643 DebuggerSP debugger_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000644 if (lldb_initialized)
Greg Clayton6920b522012-08-22 18:39:03 +0000645 {
646 Mutex::Locker locker (GetDebuggerListMutex ());
647 DebuggerList &debugger_list = GetDebuggerList();
648 DebuggerList::iterator pos, end = debugger_list.end();
649
650 for (pos = debugger_list.begin(); pos != end; ++pos)
651 {
652 if ((*pos).get()->m_instance_name == instance_name)
653 {
654 debugger_sp = *pos;
655 break;
656 }
657 }
658 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000659 return debugger_sp;
660}
Greg Clayton66111032010-06-23 01:19:29 +0000661
662TargetSP
663Debugger::FindTargetWithProcessID (lldb::pid_t pid)
664{
Greg Clayton4d122c42011-09-17 08:33:22 +0000665 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000666 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000667 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000668 Mutex::Locker locker (GetDebuggerListMutex ());
669 DebuggerList &debugger_list = GetDebuggerList();
670 DebuggerList::iterator pos, end = debugger_list.end();
671 for (pos = debugger_list.begin(); pos != end; ++pos)
672 {
673 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
674 if (target_sp)
675 break;
676 }
Greg Clayton66111032010-06-23 01:19:29 +0000677 }
678 return target_sp;
679}
680
Greg Claytone4e45922011-11-16 05:37:56 +0000681TargetSP
682Debugger::FindTargetWithProcess (Process *process)
683{
684 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000685 if (lldb_initialized)
Greg Claytone4e45922011-11-16 05:37:56 +0000686 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000687 Mutex::Locker locker (GetDebuggerListMutex ());
688 DebuggerList &debugger_list = GetDebuggerList();
689 DebuggerList::iterator pos, end = debugger_list.end();
690 for (pos = debugger_list.begin(); pos != end; ++pos)
691 {
692 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
693 if (target_sp)
694 break;
695 }
Greg Claytone4e45922011-11-16 05:37:56 +0000696 }
697 return target_sp;
698}
699
Jason Molendae6481c72014-09-12 01:50:46 +0000700Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
701 UserID(g_unique_id++),
702 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
703 m_input_file_sp(new StreamFile(stdin, false)),
704 m_output_file_sp(new StreamFile(stdout, false)),
705 m_error_file_sp(new StreamFile(stderr, false)),
706 m_terminal_state(),
707 m_target_list(*this),
708 m_platform_list(),
709 m_listener("lldb.Debugger"),
710 m_source_manager_ap(),
711 m_source_file_cache(),
712 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
713 m_input_reader_stack(),
714 m_instance_name(),
Greg Claytonafa91e332014-12-01 22:41:27 +0000715 m_loaded_plugins(),
716 m_event_handler_thread (),
717 m_io_handler_thread (),
718 m_sync_broadcaster (NULL, "lldb.debugger.sync")
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719{
Greg Clayton67cc0632012-08-22 17:17:09 +0000720 char instance_cstr[256];
721 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
722 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000723 if (log_callback)
724 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000725 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000726 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000727 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000728 assert (default_platform_sp.get());
729 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000730
Greg Clayton754a9362012-08-23 00:22:02 +0000731 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000732 m_collection_sp->AppendProperty (ConstString("target"),
733 ConstString("Settings specify to debugging targets."),
734 true,
735 Target::GetGlobalProperties()->GetValueProperties());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000736 m_collection_sp->AppendProperty (ConstString("platform"),
737 ConstString("Platform settings."),
738 true,
739 Platform::GetGlobalPlatformProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000740 if (m_command_interpreter_ap.get())
741 {
742 m_collection_sp->AppendProperty (ConstString("interpreter"),
743 ConstString("Settings specify to the debugger's command interpreter."),
744 true,
745 m_command_interpreter_ap->GetValueProperties());
746 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000747 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
748 term_width->SetMinimumValue(10);
749 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000750
751 // Turn off use-color if this is a dumb terminal.
752 const char *term = getenv ("TERM");
753 if (term && !strcmp (term, "dumb"))
754 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755}
756
757Debugger::~Debugger ()
758{
Jim Ingham8314c522011-09-15 21:36:42 +0000759 Clear();
760}
761
762void
763Debugger::Clear()
764{
Greg Clayton44d93782014-01-27 23:43:24 +0000765 ClearIOHandlers();
766 StopIOHandlerThread();
767 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000768 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000769 int num_targets = m_target_list.GetNumTargets();
770 for (int i = 0; i < num_targets; i++)
771 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000772 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
773 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000774 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000775 ProcessSP process_sp (target_sp->GetProcessSP());
776 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000777 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000778 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000779 }
Greg Clayton66111032010-06-23 01:19:29 +0000780 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000781 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000782
783 // Close the input file _before_ we close the input read communications class
784 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000785 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000786 if (m_input_file_sp)
787 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000788
789 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000790}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791
792bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000793Debugger::GetCloseInputOnEOF () const
794{
Greg Clayton44d93782014-01-27 23:43:24 +0000795// return m_input_comm.GetCloseOnEOF();
796 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000797}
798
799void
800Debugger::SetCloseInputOnEOF (bool b)
801{
Greg Clayton44d93782014-01-27 23:43:24 +0000802// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000803}
804
805bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806Debugger::GetAsyncExecution ()
807{
Greg Clayton66111032010-06-23 01:19:29 +0000808 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809}
810
811void
812Debugger::SetAsyncExecution (bool async_execution)
813{
Greg Clayton66111032010-06-23 01:19:29 +0000814 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815}
816
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817
818void
819Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
820{
Greg Clayton44d93782014-01-27 23:43:24 +0000821 if (m_input_file_sp)
822 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
823 else
824 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
825
826 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000827 if (in_file.IsValid() == false)
828 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829
Jim Inghamc5917d92012-11-30 20:23:19 +0000830 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
831 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832}
833
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834void
835Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
836{
Greg Clayton44d93782014-01-27 23:43:24 +0000837 if (m_output_file_sp)
838 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
839 else
840 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
841
842 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000843 if (out_file.IsValid() == false)
844 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000845
Enrico Granatab5887262012-10-29 21:18:03 +0000846 // do not create the ScriptInterpreter just for setting the output file handle
847 // as the constructor will know how to do the right thing on its own
848 const bool can_create = false;
849 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
850 if (script_interpreter)
851 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852}
853
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854void
855Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
856{
Greg Clayton44d93782014-01-27 23:43:24 +0000857 if (m_error_file_sp)
858 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
859 else
860 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
861
862 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000863 if (err_file.IsValid() == false)
864 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865}
866
Jim Inghamc5917d92012-11-30 20:23:19 +0000867void
868Debugger::SaveInputTerminalState ()
869{
Greg Clayton44d93782014-01-27 23:43:24 +0000870 if (m_input_file_sp)
871 {
872 File &in_file = m_input_file_sp->GetFile();
873 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
874 m_terminal_state.Save(in_file.GetDescriptor(), true);
875 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000876}
877
878void
879Debugger::RestoreInputTerminalState ()
880{
881 m_terminal_state.Restore();
882}
883
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000885Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886{
887 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000888 TargetSP target_sp(GetSelectedTarget());
889 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890
891 if (target_sp)
892 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000893 ProcessSP process_sp (target_sp->GetProcessSP());
894 exe_ctx.SetProcessSP (process_sp);
895 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000897 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
898 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000900 exe_ctx.SetThreadSP (thread_sp);
901 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
902 if (exe_ctx.GetFramePtr() == NULL)
903 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 }
905 }
906 }
907 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910void
Caroline Ticeefed6132010-11-19 20:47:54 +0000911Debugger::DispatchInputInterrupt ()
912{
Greg Clayton44d93782014-01-27 23:43:24 +0000913 Mutex::Locker locker (m_input_reader_stack.GetMutex());
914 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000915 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000916 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000917}
918
919void
920Debugger::DispatchInputEndOfFile ()
921{
Greg Clayton44d93782014-01-27 23:43:24 +0000922 Mutex::Locker locker (m_input_reader_stack.GetMutex());
923 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000924 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000925 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000926}
927
928void
Greg Clayton44d93782014-01-27 23:43:24 +0000929Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000930{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000931 // 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 +0000932 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000933 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000934 {
Greg Clayton44d93782014-01-27 23:43:24 +0000935 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000936 if (reader_sp)
Pavel Labath44464872015-05-27 12:40:32 +0000937 PopIOHandler (reader_sp);
Caroline Tice3d6086f2010-12-20 18:35:50 +0000938 }
939}
940
941void
Siva Chandra9aaab552015-02-26 19:26:36 +0000942Debugger::ExecuteIOHandlers()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000943{
Greg Clayton44d93782014-01-27 23:43:24 +0000944 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945 {
Greg Clayton44d93782014-01-27 23:43:24 +0000946 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947 if (!reader_sp)
948 break;
949
Greg Clayton44d93782014-01-27 23:43:24 +0000950 reader_sp->Run();
Greg Clayton44d93782014-01-27 23:43:24 +0000951
952 // Remove all input readers that are done from the top of the stack
953 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954 {
Greg Clayton44d93782014-01-27 23:43:24 +0000955 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
956 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +0000957 PopIOHandler (top_reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000958 else
959 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960 }
961 }
Greg Clayton44d93782014-01-27 23:43:24 +0000962 ClearIOHandlers();
963}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964
Greg Clayton44d93782014-01-27 23:43:24 +0000965bool
966Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
967{
968 return m_input_reader_stack.IsTop (reader_sp);
969}
970
Sean Callanan66810412015-10-19 23:11:07 +0000971bool
972Debugger::CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
973{
974 return m_input_reader_stack.CheckTopIOHandlerTypes (top_type, second_top_type);
975}
976
Pavel Labath44464872015-05-27 12:40:32 +0000977void
978Debugger::PrintAsync (const char *s, size_t len, bool is_stdout)
979{
980 lldb::StreamFileSP stream = is_stdout ? GetOutputFile() : GetErrorFile();
981 m_input_reader_stack.PrintAsync(stream.get(), s, len);
982}
Greg Clayton44d93782014-01-27 23:43:24 +0000983
984ConstString
985Debugger::GetTopIOHandlerControlSequence(char ch)
986{
987 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988}
989
Kate Stonea487aa42015-01-15 00:52:41 +0000990const char *
991Debugger::GetIOHandlerCommandPrefix()
992{
993 return m_input_reader_stack.GetTopIOHandlerCommandPrefix();
994}
995
996const char *
997Debugger::GetIOHandlerHelpPrologue()
998{
999 return m_input_reader_stack.GetTopIOHandlerHelpPrologue();
1000}
1001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002void
Greg Clayton44d93782014-01-27 23:43:24 +00001003Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
1004{
Greg Clayton44d93782014-01-27 23:43:24 +00001005 PushIOHandler (reader_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001006
Greg Clayton577508d2014-06-20 00:23:57 +00001007 IOHandlerSP top_reader_sp = reader_sp;
1008 while (top_reader_sp)
1009 {
Greg Clayton577508d2014-06-20 00:23:57 +00001010 top_reader_sp->Run();
Pavel Labath44464872015-05-27 12:40:32 +00001011
Greg Clayton577508d2014-06-20 00:23:57 +00001012 if (top_reader_sp.get() == reader_sp.get())
1013 {
1014 if (PopIOHandler (reader_sp))
1015 break;
1016 }
Pavel Labath44464872015-05-27 12:40:32 +00001017
Greg Clayton577508d2014-06-20 00:23:57 +00001018 while (1)
1019 {
1020 top_reader_sp = m_input_reader_stack.Top();
1021 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +00001022 PopIOHandler (top_reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +00001023 else
1024 break;
1025 }
1026 }
Greg Clayton44d93782014-01-27 23:43:24 +00001027}
1028
1029void
1030Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
1031{
1032 // Before an IOHandler runs, it must have in/out/err streams.
1033 // This function is called when one ore more of the streams
1034 // are NULL. We use the top input reader's in/out/err streams,
1035 // or fall back to the debugger file handles, or we fall back
1036 // onto stdin/stdout/stderr as a last resort.
1037
1038 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1039 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
1040 // If no STDIN has been set, then set it appropriately
1041 if (!in)
1042 {
1043 if (top_reader_sp)
1044 in = top_reader_sp->GetInputStreamFile();
1045 else
1046 in = GetInputFile();
1047
1048 // If there is nothing, use stdin
1049 if (!in)
1050 in = StreamFileSP(new StreamFile(stdin, false));
1051 }
1052 // If no STDOUT has been set, then set it appropriately
1053 if (!out)
1054 {
1055 if (top_reader_sp)
1056 out = top_reader_sp->GetOutputStreamFile();
1057 else
1058 out = GetOutputFile();
1059
1060 // If there is nothing, use stdout
1061 if (!out)
1062 out = StreamFileSP(new StreamFile(stdout, false));
1063 }
1064 // If no STDERR has been set, then set it appropriately
1065 if (!err)
1066 {
1067 if (top_reader_sp)
1068 err = top_reader_sp->GetErrorStreamFile();
1069 else
1070 err = GetErrorFile();
1071
1072 // If there is nothing, use stderr
1073 if (!err)
1074 err = StreamFileSP(new StreamFile(stdout, false));
1075
1076 }
1077}
1078
1079void
1080Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081{
1082 if (!reader_sp)
1083 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +00001084
Pavel Labath44464872015-05-27 12:40:32 +00001085 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1086
1087 // Get the current top input reader...
Greg Clayton44d93782014-01-27 23:43:24 +00001088 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001089
Greg Claytonb4874f12014-02-28 18:22:24 +00001090 // Don't push the same IO handler twice...
Pavel Labath44464872015-05-27 12:40:32 +00001091 if (reader_sp == top_reader_sp)
1092 return;
Greg Clayton44d93782014-01-27 23:43:24 +00001093
Pavel Labath44464872015-05-27 12:40:32 +00001094 // Push our new input reader
1095 m_input_reader_stack.Push (reader_sp);
1096 reader_sp->Activate();
1097
1098 // Interrupt the top input reader to it will exit its Run() function
1099 // and let this new input reader take over
1100 if (top_reader_sp)
1101 {
1102 top_reader_sp->Deactivate();
1103 top_reader_sp->Cancel();
Greg Claytonb4874f12014-02-28 18:22:24 +00001104 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105}
1106
1107bool
Greg Clayton44d93782014-01-27 23:43:24 +00001108Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109{
Pavel Labath44464872015-05-27 12:40:32 +00001110 if (! pop_reader_sp)
1111 return false;
1112
Greg Clayton44d93782014-01-27 23:43:24 +00001113 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114
1115 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001116 // read on the stack refresh its prompt and if there is one...
Pavel Labath44464872015-05-27 12:40:32 +00001117 if (m_input_reader_stack.IsEmpty())
1118 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119
Greg Clayton44d93782014-01-27 23:43:24 +00001120 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Greg Clayton66111032010-06-23 01:19:29 +00001121
Pavel Labath44464872015-05-27 12:40:32 +00001122 if (pop_reader_sp != reader_sp)
1123 return false;
1124
1125 reader_sp->Deactivate();
1126 reader_sp->Cancel();
1127 m_input_reader_stack.Pop ();
1128
1129 reader_sp = m_input_reader_stack.Top();
1130 if (reader_sp)
1131 reader_sp->Activate();
1132
1133 return true;
1134}
Greg Clayton44d93782014-01-27 23:43:24 +00001135
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001136StreamSP
1137Debugger::GetAsyncOutputStream ()
1138{
Pavel Labath44464872015-05-27 12:40:32 +00001139 return StreamSP (new StreamAsynchronousIO (*this, true));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001140}
1141
1142StreamSP
1143Debugger::GetAsyncErrorStream ()
1144{
Pavel Labath44464872015-05-27 12:40:32 +00001145 return StreamSP (new StreamAsynchronousIO (*this, false));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001146}
1147
Greg Claytonc7bece562013-01-25 18:06:21 +00001148size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001149Debugger::GetNumDebuggers()
1150{
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001151 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001152 {
1153 Mutex::Locker locker (GetDebuggerListMutex ());
1154 return GetDebuggerList().size();
1155 }
1156 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001157}
1158
1159lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001160Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001161{
1162 DebuggerSP debugger_sp;
1163
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001164 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001165 {
1166 Mutex::Locker locker (GetDebuggerListMutex ());
1167 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001168
Greg Claytonc15f55e2012-03-30 20:53:46 +00001169 if (index < debugger_list.size())
1170 debugger_sp = debugger_list[index];
1171 }
1172
Enrico Granata061858c2012-02-15 02:34:21 +00001173 return debugger_sp;
1174}
1175
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001176DebuggerSP
1177Debugger::FindDebuggerWithID (lldb::user_id_t id)
1178{
Greg Clayton4d122c42011-09-17 08:33:22 +00001179 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001180
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001181 if (lldb_initialized)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001182 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001183 Mutex::Locker locker (GetDebuggerListMutex ());
1184 DebuggerList &debugger_list = GetDebuggerList();
1185 DebuggerList::iterator pos, end = debugger_list.end();
1186 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001187 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001188 if ((*pos).get()->GetID() == id)
1189 {
1190 debugger_sp = *pos;
1191 break;
1192 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001193 }
1194 }
1195 return debugger_sp;
1196}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001197
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001198#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001199static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001200TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001201{
1202 if (frame == NULL)
1203 return;
1204
1205 StreamString s;
1206 const char *prompt_format =
1207 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001208 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1209 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001210 "{process.id = '${process.id}'\n}"
1211 "{process.name = '${process.name}'\n}"
1212 "{process.file.basename = '${process.file.basename}'\n}"
1213 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1214 "{thread.id = '${thread.id}'\n}"
1215 "{thread.index = '${thread.index}'\n}"
1216 "{thread.name = '${thread.name}'\n}"
1217 "{thread.queue = '${thread.queue}'\n}"
1218 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1219 "{target.arch = '${target.arch}'\n}"
1220 "{module.file.basename = '${module.file.basename}'\n}"
1221 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1222 "{file.basename = '${file.basename}'\n}"
1223 "{file.fullpath = '${file.fullpath}'\n}"
1224 "{frame.index = '${frame.index}'\n}"
1225 "{frame.pc = '${frame.pc}'\n}"
1226 "{frame.sp = '${frame.sp}'\n}"
1227 "{frame.fp = '${frame.fp}'\n}"
1228 "{frame.flags = '${frame.flags}'\n}"
1229 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1230 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1231 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1232 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1233 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1234 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1235 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1236 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001237 "{function.changed = '${function.changed}'\n}"
1238 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001239 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001240 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001241 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001242 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001243 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001244 "{function.line-offset = '${function.line-offset}'\n}"
1245 "{function.pc-offset = '${function.pc-offset}'\n}"
1246 "{line.file.basename = '${line.file.basename}'\n}"
1247 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1248 "{line.number = '${line.number}'\n}"
1249 "{line.start-addr = '${line.start-addr}'\n}"
1250 "{line.end-addr = '${line.end-addr}'\n}"
1251;
1252
1253 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1254 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001255 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001256 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001257 {
1258 printf("%s\n", s.GetData());
1259 }
1260 else
1261 {
Greg Clayton1b654882010-09-19 02:33:57 +00001262 printf ("what we got: %s\n", s.GetData());
1263 }
1264}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001265#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001266
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001267bool
Greg Clayton554f68d2015-02-04 22:00:53 +00001268Debugger::FormatDisassemblerAddress (const FormatEntity::Entry *format,
Jason Molendaaff1b352014-10-10 23:07:36 +00001269 const SymbolContext *sc,
1270 const SymbolContext *prev_sc,
1271 const ExecutionContext *exe_ctx,
1272 const Address *addr,
1273 Stream &s)
1274{
Greg Clayton554f68d2015-02-04 22:00:53 +00001275 FormatEntity::Entry format_entry;
1276
1277 if (format == NULL)
Jason Molendaaff1b352014-10-10 23:07:36 +00001278 {
Greg Clayton554f68d2015-02-04 22:00:53 +00001279 if (exe_ctx != NULL && exe_ctx->HasTargetScope())
1280 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1281 if (format == NULL)
1282 {
1283 FormatEntity::Parse("${addr}: ", format_entry);
1284 format = &format_entry;
1285 }
Jason Molendaaff1b352014-10-10 23:07:36 +00001286 }
1287 bool function_changed = false;
1288 bool initial_function = false;
1289 if (prev_sc && (prev_sc->function || prev_sc->symbol))
1290 {
1291 if (sc && (sc->function || sc->symbol))
1292 {
1293 if (prev_sc->symbol && sc->symbol)
1294 {
1295 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
1296 {
1297 function_changed = true;
1298 }
1299 }
1300 else if (prev_sc->function && sc->function)
1301 {
1302 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
1303 {
1304 function_changed = true;
1305 }
1306 }
1307 }
1308 }
1309 // The first context on a list of instructions will have a prev_sc that
1310 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
1311 // would return false. But we do get a prev_sc pointer.
1312 if ((sc && (sc->function || sc->symbol))
1313 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
1314 {
1315 initial_function = true;
1316 }
Greg Clayton554f68d2015-02-04 22:00:53 +00001317 return FormatEntity::Format(*format, s, sc, exe_ctx, addr, NULL, function_changed, initial_function);
Jason Molendaaff1b352014-10-10 23:07:36 +00001318}
1319
1320
Jim Ingham228063c2012-02-21 02:23:08 +00001321void
1322Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1323{
Jim Ingham4f02b222012-02-22 22:49:20 +00001324 // For simplicity's sake, I am not going to deal with how to close down any
1325 // open logging streams, I just redirect everything from here on out to the
1326 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00001327 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
1328}
1329
1330bool
1331Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
1332{
Jim Ingham228063c2012-02-21 02:23:08 +00001333 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001334 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001335 {
1336 log_stream_sp = m_log_callback_stream_sp;
1337 // For now when using the callback mode you always get thread & timestamp.
1338 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1339 }
1340 else if (log_file == NULL || *log_file == '\0')
1341 {
Greg Clayton44d93782014-01-27 23:43:24 +00001342 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00001343 }
1344 else
1345 {
1346 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001347 if (pos != m_log_streams.end())
1348 log_stream_sp = pos->second.lock();
1349 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001350 {
Pavel Labath8ac06992015-03-20 09:43:20 +00001351 uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate
1352 | File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
1353 if (! (log_options & LLDB_LOG_OPTION_APPEND))
1354 options |= File::eOpenOptionTruncate;
1355
1356 log_stream_sp.reset (new StreamFile (log_file, options));
Jim Ingham228063c2012-02-21 02:23:08 +00001357 m_log_streams[log_file] = log_stream_sp;
1358 }
Jim Ingham228063c2012-02-21 02:23:08 +00001359 }
1360 assert (log_stream_sp.get());
1361
1362 if (log_options == 0)
1363 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
1364
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +00001365 return Log::EnableLogChannel(log_stream_sp, log_options, channel, categories, error_stream);
Jim Ingham228063c2012-02-21 02:23:08 +00001366}
1367
Greg Clayton9585fbf2013-03-19 00:20:55 +00001368SourceManager &
1369Debugger::GetSourceManager ()
1370{
1371 if (m_source_manager_ap.get() == NULL)
1372 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
1373 return *m_source_manager_ap;
1374}
1375
1376
Greg Clayton44d93782014-01-27 23:43:24 +00001377
1378// This function handles events that were broadcast by the process.
1379void
1380Debugger::HandleBreakpointEvent (const EventSP &event_sp)
1381{
1382 using namespace lldb;
1383 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
1384
1385// if (event_type & eBreakpointEventTypeAdded
1386// || event_type & eBreakpointEventTypeRemoved
1387// || event_type & eBreakpointEventTypeEnabled
1388// || event_type & eBreakpointEventTypeDisabled
1389// || event_type & eBreakpointEventTypeCommandChanged
1390// || event_type & eBreakpointEventTypeConditionChanged
1391// || event_type & eBreakpointEventTypeIgnoreChanged
1392// || event_type & eBreakpointEventTypeLocationsResolved)
1393// {
1394// // Don't do anything about these events, since the breakpoint commands already echo these actions.
1395// }
1396//
1397 if (event_type & eBreakpointEventTypeLocationsAdded)
1398 {
1399 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
1400 if (num_new_locations > 0)
1401 {
1402 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001403 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001404 if (output_sp)
1405 {
1406 output_sp->Printf("%d location%s added to breakpoint %d\n",
1407 num_new_locations,
1408 num_new_locations == 1 ? "" : "s",
1409 breakpoint->GetID());
Pavel Labath44464872015-05-27 12:40:32 +00001410 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001411 }
1412 }
1413 }
1414// else if (event_type & eBreakpointEventTypeLocationsRemoved)
1415// {
1416// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
1417// }
1418// else if (event_type & eBreakpointEventTypeLocationsResolved)
1419// {
1420// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
1421// }
1422}
1423
1424size_t
1425Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
1426{
1427 size_t total_bytes = 0;
1428 if (stream == NULL)
1429 stream = GetOutputFile().get();
1430
1431 if (stream)
1432 {
1433 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
1434 if (process == NULL)
1435 {
1436 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1437 if (target_sp)
1438 process = target_sp->GetProcessSP().get();
1439 }
1440 if (process)
1441 {
1442 Error error;
1443 size_t len;
1444 char stdio_buffer[1024];
1445 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1446 {
1447 stream->Write(stdio_buffer, len);
1448 total_bytes += len;
1449 }
1450 }
1451 stream->Flush();
1452 }
1453 return total_bytes;
1454}
1455
1456size_t
1457Debugger::GetProcessSTDERR (Process *process, Stream *stream)
1458{
1459 size_t total_bytes = 0;
1460 if (stream == NULL)
1461 stream = GetOutputFile().get();
1462
1463 if (stream)
1464 {
1465 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
1466 if (process == NULL)
1467 {
1468 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1469 if (target_sp)
1470 process = target_sp->GetProcessSP().get();
1471 }
1472 if (process)
1473 {
1474 Error error;
1475 size_t len;
1476 char stdio_buffer[1024];
1477 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1478 {
1479 stream->Write(stdio_buffer, len);
1480 total_bytes += len;
1481 }
1482 }
1483 stream->Flush();
1484 }
1485 return total_bytes;
1486}
1487
Greg Claytondc6224e2014-10-21 01:00:42 +00001488
Greg Clayton44d93782014-01-27 23:43:24 +00001489// This function handles events that were broadcast by the process.
1490void
1491Debugger::HandleProcessEvent (const EventSP &event_sp)
1492{
1493 using namespace lldb;
1494 const uint32_t event_type = event_sp->GetType();
1495 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00001496
Pavel Labath44464872015-05-27 12:40:32 +00001497 StreamSP output_stream_sp = GetAsyncOutputStream();
1498 StreamSP error_stream_sp = GetAsyncErrorStream();
Greg Clayton44d93782014-01-27 23:43:24 +00001499 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00001500
Greg Claytonb4874f12014-02-28 18:22:24 +00001501 if (!gui_enabled)
1502 {
1503 bool pop_process_io_handler = false;
1504 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00001505
Pavel Labath44464872015-05-27 12:40:32 +00001506 bool state_is_stopped = false;
1507 const bool got_state_changed = (event_type & Process::eBroadcastBitStateChanged) != 0;
1508 const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
1509 const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
1510 if (got_state_changed)
Greg Clayton44d93782014-01-27 23:43:24 +00001511 {
Pavel Labath44464872015-05-27 12:40:32 +00001512 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
1513 state_is_stopped = StateIsStoppedState(event_state, false);
Greg Claytonb4874f12014-02-28 18:22:24 +00001514 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001515
Pavel Labath44464872015-05-27 12:40:32 +00001516 // Display running state changes first before any STDIO
1517 if (got_state_changed && !state_is_stopped)
Greg Claytonb4874f12014-02-28 18:22:24 +00001518 {
Pavel Labath44464872015-05-27 12:40:32 +00001519 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
Greg Claytonb4874f12014-02-28 18:22:24 +00001520 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001521
Pavel Labath44464872015-05-27 12:40:32 +00001522 // Now display and STDOUT
1523 if (got_stdout || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001524 {
Pavel Labath44464872015-05-27 12:40:32 +00001525 GetProcessSTDOUT (process_sp.get(), output_stream_sp.get());
Greg Clayton44d93782014-01-27 23:43:24 +00001526 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001527
Pavel Labath44464872015-05-27 12:40:32 +00001528 // Now display and STDERR
1529 if (got_stderr || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001530 {
Pavel Labath44464872015-05-27 12:40:32 +00001531 GetProcessSTDERR (process_sp.get(), error_stream_sp.get());
Greg Claytonb4874f12014-02-28 18:22:24 +00001532 }
1533
Pavel Labath44464872015-05-27 12:40:32 +00001534 // Now display any stopped state changes after any STDIO
1535 if (got_state_changed && state_is_stopped)
1536 {
1537 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
1538 }
1539
1540 output_stream_sp->Flush();
1541 error_stream_sp->Flush();
1542
Greg Claytonb4874f12014-02-28 18:22:24 +00001543 if (pop_process_io_handler)
1544 process_sp->PopProcessIOHandler();
1545 }
Greg Clayton44d93782014-01-27 23:43:24 +00001546}
1547
1548void
1549Debugger::HandleThreadEvent (const EventSP &event_sp)
1550{
1551 // At present the only thread event we handle is the Frame Changed event,
1552 // and all we do for that is just reprint the thread status for that thread.
1553 using namespace lldb;
1554 const uint32_t event_type = event_sp->GetType();
1555 if (event_type == Thread::eBroadcastBitStackChanged ||
1556 event_type == Thread::eBroadcastBitThreadSelected )
1557 {
1558 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
1559 if (thread_sp)
1560 {
Pavel Labath44464872015-05-27 12:40:32 +00001561 thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00001562 }
1563 }
1564}
1565
1566bool
1567Debugger::IsForwardingEvents ()
1568{
1569 return (bool)m_forward_listener_sp;
1570}
1571
1572void
1573Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
1574{
1575 m_forward_listener_sp = listener_sp;
1576}
1577
1578void
1579Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
1580{
1581 m_forward_listener_sp.reset();
1582}
1583
1584
1585void
1586Debugger::DefaultEventHandler()
1587{
1588 Listener& listener(GetListener());
1589 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
1590 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
1591 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
1592 BroadcastEventSpec target_event_spec (broadcaster_class_target,
1593 Target::eBroadcastBitBreakpointChanged);
1594
1595 BroadcastEventSpec process_event_spec (broadcaster_class_process,
1596 Process::eBroadcastBitStateChanged |
1597 Process::eBroadcastBitSTDOUT |
1598 Process::eBroadcastBitSTDERR);
1599
1600 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
1601 Thread::eBroadcastBitStackChanged |
1602 Thread::eBroadcastBitThreadSelected );
1603
1604 listener.StartListeningForEventSpec (*this, target_event_spec);
1605 listener.StartListeningForEventSpec (*this, process_event_spec);
1606 listener.StartListeningForEventSpec (*this, thread_event_spec);
1607 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
1608 CommandInterpreter::eBroadcastBitQuitCommandReceived |
1609 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
1610 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
Greg Claytonafa91e332014-12-01 22:41:27 +00001611
1612 // Let the thread that spawned us know that we have started up and
1613 // that we are now listening to all required events so no events get missed
1614 m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
1615
Greg Clayton44d93782014-01-27 23:43:24 +00001616 bool done = false;
1617 while (!done)
1618 {
Greg Clayton44d93782014-01-27 23:43:24 +00001619 EventSP event_sp;
1620 if (listener.WaitForEvent(NULL, event_sp))
1621 {
1622 if (event_sp)
1623 {
1624 Broadcaster *broadcaster = event_sp->GetBroadcaster();
1625 if (broadcaster)
1626 {
1627 uint32_t event_type = event_sp->GetType();
1628 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
1629 if (broadcaster_class == broadcaster_class_process)
1630 {
1631 HandleProcessEvent (event_sp);
1632 }
1633 else if (broadcaster_class == broadcaster_class_target)
1634 {
1635 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
1636 {
1637 HandleBreakpointEvent (event_sp);
1638 }
1639 }
1640 else if (broadcaster_class == broadcaster_class_thread)
1641 {
1642 HandleThreadEvent (event_sp);
1643 }
1644 else if (broadcaster == m_command_interpreter_ap.get())
1645 {
1646 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
1647 {
1648 done = true;
1649 }
1650 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
1651 {
1652 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1653 if (data && data[0])
1654 {
Pavel Labath44464872015-05-27 12:40:32 +00001655 StreamSP error_sp (GetAsyncErrorStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001656 if (error_sp)
1657 {
Greg Clayton44d93782014-01-27 23:43:24 +00001658 error_sp->PutCString(data);
1659 error_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001660 }
1661 }
1662 }
1663 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
1664 {
1665 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1666 if (data && data[0])
1667 {
Pavel Labath44464872015-05-27 12:40:32 +00001668 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001669 if (output_sp)
1670 {
Greg Clayton44d93782014-01-27 23:43:24 +00001671 output_sp->PutCString(data);
1672 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001673 }
1674 }
1675 }
1676 }
1677 }
1678
1679 if (m_forward_listener_sp)
1680 m_forward_listener_sp->AddEvent(event_sp);
1681 }
1682 }
1683 }
1684}
1685
1686lldb::thread_result_t
1687Debugger::EventHandlerThread (lldb::thread_arg_t arg)
1688{
1689 ((Debugger *)arg)->DefaultEventHandler();
1690 return NULL;
1691}
1692
1693bool
1694Debugger::StartEventHandlerThread()
1695{
Zachary Turneracee96a2014-09-23 18:32:09 +00001696 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001697 {
Greg Claytonafa91e332014-12-01 22:41:27 +00001698 // We must synchronize with the DefaultEventHandler() thread to ensure
1699 // it is up and running and listening to events before we return from
1700 // this function. We do this by listening to events for the
1701 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
1702 Listener listener("lldb.debugger.event-handler");
1703 listener.StartListeningForEvents(&m_sync_broadcaster, eBroadcastBitEventThreadIsListening);
1704
Zachary Turner7c2896a2014-10-24 22:06:29 +00001705 // Use larger 8MB stack for this thread
Greg Claytonafa91e332014-12-01 22:41:27 +00001706 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread,
1707 this,
1708 NULL,
Zachary Turner7c2896a2014-10-24 22:06:29 +00001709 g_debugger_event_thread_stack_bytes);
Greg Claytonafa91e332014-12-01 22:41:27 +00001710
1711 // Make sure DefaultEventHandler() is running and listening to events before we return
1712 // from this function. We are only listening for events of type
1713 // eBroadcastBitEventThreadIsListening so we don't need to check the event, we just need
1714 // to wait an infinite amount of time for it (NULL timeout as the first parameter)
1715 lldb::EventSP event_sp;
1716 listener.WaitForEvent(NULL, event_sp);
Greg Clayton807b6b32014-10-15 18:03:59 +00001717 }
Zachary Turneracee96a2014-09-23 18:32:09 +00001718 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001719}
1720
1721void
1722Debugger::StopEventHandlerThread()
1723{
Zachary Turneracee96a2014-09-23 18:32:09 +00001724 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001725 {
1726 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00001727 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001728 }
1729}
1730
1731
1732lldb::thread_result_t
1733Debugger::IOHandlerThread (lldb::thread_arg_t arg)
1734{
1735 Debugger *debugger = (Debugger *)arg;
Siva Chandra9aaab552015-02-26 19:26:36 +00001736 debugger->ExecuteIOHandlers();
Greg Clayton44d93782014-01-27 23:43:24 +00001737 debugger->StopEventHandlerThread();
1738 return NULL;
1739}
1740
1741bool
Sean Callanan66810412015-10-19 23:11:07 +00001742Debugger::HasIOHandlerThread()
1743{
1744 return m_io_handler_thread.IsJoinable();
1745}
1746
1747bool
Greg Clayton44d93782014-01-27 23:43:24 +00001748Debugger::StartIOHandlerThread()
1749{
Zachary Turneracee96a2014-09-23 18:32:09 +00001750 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001751 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
1752 IOHandlerThread,
1753 this,
1754 NULL,
1755 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00001756 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001757}
1758
1759void
1760Debugger::StopIOHandlerThread()
1761{
Zachary Turneracee96a2014-09-23 18:32:09 +00001762 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001763 {
1764 if (m_input_file_sp)
1765 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00001766 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001767 }
1768}
1769
Sean Callanan66810412015-10-19 23:11:07 +00001770void
1771Debugger::JoinIOHandlerThread()
1772{
1773 if (HasIOHandlerThread())
1774 {
1775 thread_result_t result;
1776 m_io_handler_thread.Join(&result);
1777 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
1778 }
1779}
1780
Jim Ingham893c9322014-11-22 01:42:44 +00001781Target *
1782Debugger::GetDummyTarget()
1783{
1784 return m_target_list.GetDummyTarget (*this).get();
1785}
1786
1787Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001788Debugger::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001789{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001790 Target *target = nullptr;
1791 if (!prefer_dummy)
1792 {
1793 target = m_target_list.GetSelectedTarget().get();
1794 if (target)
1795 return target;
1796 }
1797
Jim Ingham893c9322014-11-22 01:42:44 +00001798 return GetDummyTarget();
1799}
Greg Clayton44d93782014-01-27 23:43:24 +00001800