blob: 17eb92469ab238fdcd413fc797661452871b67b0 [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Clayton4a33d312011-06-23 17:59:56 +000012#include "lldb/Core/Debugger.h"
13
14#include <map>
15
Enrico Granata4becb372011-06-29 22:27:15 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/Type.h"
Jason Molenda705b1802014-06-13 02:37:02 +000018#include "llvm/ADT/StringRef.h"
Enrico Granata4becb372011-06-29 22:27:15 +000019
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/lldb-private.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000021#include "lldb/Core/FormatEntity.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000023#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000024#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000026#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000027#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000028#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000029#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000030#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000032#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000033#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000034#include "lldb/DataFormatters/DataVisualization.h"
35#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000036#include "lldb/DataFormatters/TypeSummary.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000037#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000038#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000039#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000040#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000041#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000042#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValueSInt64.h"
44#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000045#include "lldb/Symbol/ClangASTContext.h"
46#include "lldb/Symbol/CompileUnit.h"
47#include "lldb/Symbol/Function.h"
48#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000049#include "lldb/Symbol/VariableList.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000050#include "lldb/Target/CPPLanguageRuntime.h"
51#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/TargetList.h"
53#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000054#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000055#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000056#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000057#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000059#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Zachary Turner58a559c2014-08-27 20:15:09 +000061#include "llvm/Support/DynamicLibrary.h"
62
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063using namespace lldb;
64using namespace lldb_private;
65
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Greg Clayton1b654882010-09-19 02:33:57 +000067static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000068static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000069static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000070
Greg Clayton1b654882010-09-19 02:33:57 +000071#pragma mark Static Functions
72
73static Mutex &
74GetDebuggerListMutex ()
75{
76 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
77 return g_mutex;
78}
79
80typedef std::vector<DebuggerSP> DebuggerList;
81
82static DebuggerList &
83GetDebuggerList()
84{
85 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000086 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000087 static DebuggerList g_list;
88 return g_list;
89}
Greg Claytone372b982011-11-21 21:44:34 +000090
91OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000092g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000093{
Greg Clayton67cc0632012-08-22 17:17:09 +000094 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
95 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
96 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000097 { 0, NULL, NULL }
98};
99
Greg Clayton67cc0632012-08-22 17:17:09 +0000100OptionEnumValueElement
101g_language_enumerators[] =
102{
103 { eScriptLanguageNone, "none", "Disable scripting languages."},
104 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
105 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +0000106 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000107};
Greg Claytone372b982011-11-21 21:44:34 +0000108
Greg Clayton67cc0632012-08-22 17:17:09 +0000109#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
110#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
111
Michael Sartain0769b2b2013-07-30 16:44:36 +0000112#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000113 "{, ${frame.pc}}"\
114 MODULE_WITH_FUNC\
115 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000116 "{, name = '${thread.name}'}"\
117 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000118 "{, activity = '${thread.info.activity.name}'}" \
119 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000120 "{, stop reason = ${thread.stop-reason}}"\
121 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000122 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000123 "\\n"
124
125#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
126 MODULE_WITH_FUNC\
127 FILE_AND_LINE\
128 "\\n"
129
Jason Molendac980fa92015-02-13 23:24:21 +0000130// Three parts to this disassembly format specification:
131// 1. If this is a new function/symbol (no previous symbol/function), print
132// dylib`funcname:\n
133// 2. If this is a symbol context change (different from previous symbol/function), print
134// dylib`funcname:\n
135// 3. print
136// address <+offset>:
137#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}>}: "
138
139// gdb's disassembly format can be emulated with
140// ${current-pc-arrow}${addr-file-or-load}{ <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>}:
141
142// lldb's original format for disassembly would look like this format string -
143// {${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}}:
144
Greg Clayton67cc0632012-08-22 17:17:09 +0000145
Greg Clayton754a9362012-08-23 00:22:02 +0000146static PropertyDefinition
147g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000148{
Greg Clayton554f68d2015-02-04 22:00:53 +0000149{ "auto-confirm", OptionValue::eTypeBoolean , true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
150{ "disassembly-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." },
151{ "frame-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
152{ "notify-void", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
153{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
154{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
155{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
156{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
157{ "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." },
158{ "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." },
159{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
160{ "thread-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
161{ "use-external-editor", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Whether to use an external editor or not." },
162{ "use-color", OptionValue::eTypeBoolean , true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
163{ "auto-one-line-summaries", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
164{ "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,
185 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000186};
187
Greg Clayton5fb8f792013-12-02 19:35:49 +0000188Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000189
190Error
191Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
192 VarSetOperationType op,
193 const char *property_path,
194 const char *value)
195{
Enrico Granata84a53df2013-05-20 22:29:23 +0000196 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000197 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000198 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000199 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000200 if (is_load_script && exe_ctx->GetTargetSP())
201 {
202 target_sp = exe_ctx->GetTargetSP();
203 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
204 }
Greg Clayton4c054102012-09-01 00:38:36 +0000205 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
206 if (error.Success())
207 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000208 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000209 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
210 {
211 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000212 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
213 if (str.length())
214 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000215 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000216 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
217 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
218 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000219 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
220 {
221 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
222 SetPrompt (GetPrompt());
223 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000224 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000225 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000226 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000227 {
228 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000229 StreamString feedback_stream;
230 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000231 {
Greg Clayton44d93782014-01-27 23:43:24 +0000232 StreamFileSP stream_sp (GetErrorFile());
233 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000234 {
Greg Clayton44d93782014-01-27 23:43:24 +0000235 for (auto error : errors)
236 {
237 stream_sp->Printf("%s\n",error.AsCString());
238 }
239 if (feedback_stream.GetSize())
240 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000241 }
242 }
243 }
244 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000245 else if (is_escape_non_printables)
246 {
247 DataVisualization::ForceUpdate();
248 }
Greg Clayton4c054102012-09-01 00:38:36 +0000249 }
250 return error;
251}
252
Greg Clayton67cc0632012-08-22 17:17:09 +0000253bool
254Debugger::GetAutoConfirm () const
255{
256 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000257 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000258}
259
Greg Clayton554f68d2015-02-04 22:00:53 +0000260const FormatEntity::Entry *
Jason Molendaaff1b352014-10-10 23:07:36 +0000261Debugger::GetDisassemblyFormat() const
262{
263 const uint32_t idx = ePropertyDisassemblyFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000264 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Jason Molendaaff1b352014-10-10 23:07:36 +0000265}
266
Greg Clayton554f68d2015-02-04 22:00:53 +0000267const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000268Debugger::GetFrameFormat() const
269{
270 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000271 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000272}
273
274bool
275Debugger::GetNotifyVoid () const
276{
277 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000278 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000279}
280
281const char *
282Debugger::GetPrompt() const
283{
284 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000285 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000286}
287
288void
289Debugger::SetPrompt(const char *p)
290{
291 const uint32_t idx = ePropertyPrompt;
292 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
293 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000294 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
295 if (str.length())
296 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000297 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000298}
299
Greg Clayton554f68d2015-02-04 22:00:53 +0000300const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000301Debugger::GetThreadFormat() const
302{
303 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000304 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000305}
306
307lldb::ScriptLanguage
308Debugger::GetScriptLanguage() const
309{
310 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000311 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000312}
313
314bool
315Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
316{
317 const uint32_t idx = ePropertyScriptLanguage;
318 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
319}
320
321uint32_t
322Debugger::GetTerminalWidth () const
323{
324 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000325 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000326}
327
328bool
329Debugger::SetTerminalWidth (uint32_t term_width)
330{
331 const uint32_t idx = ePropertyTerminalWidth;
332 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
333}
334
335bool
336Debugger::GetUseExternalEditor () const
337{
338 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000339 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000340}
341
342bool
343Debugger::SetUseExternalEditor (bool b)
344{
345 const uint32_t idx = ePropertyUseExternalEditor;
346 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
347}
348
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000349bool
350Debugger::GetUseColor () const
351{
352 const uint32_t idx = ePropertyUseColor;
353 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
354}
355
356bool
357Debugger::SetUseColor (bool b)
358{
359 const uint32_t idx = ePropertyUseColor;
360 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
361 SetPrompt (GetPrompt());
362 return ret;
363}
364
Greg Clayton67cc0632012-08-22 17:17:09 +0000365uint32_t
366Debugger::GetStopSourceLineCount (bool before) const
367{
368 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000369 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000370}
371
372Debugger::StopDisassemblyType
373Debugger::GetStopDisassemblyDisplay () const
374{
375 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000376 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000377}
378
379uint32_t
380Debugger::GetDisassemblyLineCount () const
381{
382 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000383 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000384}
Greg Claytone372b982011-11-21 21:44:34 +0000385
Enrico Granata553fad52013-10-25 23:09:40 +0000386bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000387Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000388{
Enrico Granata90a8db32013-10-31 21:01:07 +0000389 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000390 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000391}
Enrico Granata553fad52013-10-25 23:09:40 +0000392
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000393bool
394Debugger::GetEscapeNonPrintables () const
395{
396 const uint32_t idx = ePropertyEscapeNonPrintables;
397 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000398}
399
Greg Clayton1b654882010-09-19 02:33:57 +0000400#pragma mark Debugger
401
Greg Clayton67cc0632012-08-22 17:17:09 +0000402//const DebuggerPropertiesSP &
403//Debugger::GetSettings() const
404//{
405// return m_properties_sp;
406//}
407//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000408
Caroline Tice2f88aad2011-01-14 00:29:16 +0000409int
410Debugger::TestDebuggerRefCount ()
411{
412 return g_shared_debugger_refcount;
413}
414
Robert Flackf196c932015-03-10 18:07:47 +0000415static bool lldb_initialized_for_llgs = false;
416void
417Debugger::InitializeForLLGS (LoadPluginCallbackType load_plugin_callback)
418{
419 lldb_initialized_for_llgs = true;
420 g_shared_debugger_refcount++;
421 g_load_plugin_callback = load_plugin_callback;
422 lldb_private::InitializeForLLGS();
423}
424
425static bool lldb_initialized = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000427Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428{
Robert Flackf196c932015-03-10 18:07:47 +0000429 lldb_initialized = true;
430 g_shared_debugger_refcount++;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000431 g_load_plugin_callback = load_plugin_callback;
Robert Flackf196c932015-03-10 18:07:47 +0000432 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
435void
436Debugger::Terminate ()
437{
Greg Clayton66111032010-06-23 01:19:29 +0000438 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 {
Greg Clayton66111032010-06-23 01:19:29 +0000440 g_shared_debugger_refcount--;
441 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 {
Greg Claytondbe54502010-11-19 03:46:01 +0000443 lldb_private::WillTerminate();
Robert Flackf196c932015-03-10 18:07:47 +0000444 if (lldb_initialized_for_llgs) {
445 lldb_initialized_for_llgs = false;
446 lldb_private::TerminateLLGS();
447 }
448 if (lldb_initialized) {
449 lldb_initialized = false;
450 lldb_private::Terminate();
451 }
Caroline Tice6760a512011-01-17 21:55:19 +0000452
453 // Clear our master list of debugger objects
454 Mutex::Locker locker (GetDebuggerListMutex ());
455 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 }
458}
459
Caroline Tice20bd37f2011-03-10 22:14:10 +0000460void
461Debugger::SettingsInitialize ()
462{
Greg Clayton6920b522012-08-22 18:39:03 +0000463 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000464}
465
466void
467Debugger::SettingsTerminate ()
468{
Greg Clayton6920b522012-08-22 18:39:03 +0000469 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000470}
471
Enrico Granata21dfcd92012-09-28 23:57:51 +0000472bool
Enrico Granatae743c782013-04-24 21:29:08 +0000473Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000474{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000475 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000476 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000477 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
478 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000479 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000480 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000481 return true;
482 }
Enrico Granatae743c782013-04-24 21:29:08 +0000483 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000484 else
Enrico Granatae743c782013-04-24 21:29:08 +0000485 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000486 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
487 // and if the public API layer isn't available (code is linking against
488 // all of the internal LLDB static libraries), then we can't load plugins
489 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000490 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000491 return false;
492}
493
494static FileSpec::EnumerateDirectoryResult
495LoadPluginCallback
496(
497 void *baton,
498 FileSpec::FileType file_type,
499 const FileSpec &file_spec
500 )
501{
502 Error error;
503
504 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000505 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000506
507 if (!baton)
508 return FileSpec::eEnumerateDirectoryResultQuit;
509
510 Debugger *debugger = (Debugger*)baton;
511
512 // If we have a regular file, a symbolic link or unknown file type, try
513 // and process the file. We must handle unknown as sometimes the directory
514 // enumeration might be enumerating a file system that doesn't have correct
515 // file type information.
516 if (file_type == FileSpec::eFileTypeRegular ||
517 file_type == FileSpec::eFileTypeSymbolicLink ||
518 file_type == FileSpec::eFileTypeUnknown )
519 {
520 FileSpec plugin_file_spec (file_spec);
521 plugin_file_spec.ResolvePath ();
522
Michael Sartain3cf443d2013-07-17 00:26:30 +0000523 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
524 plugin_file_spec.GetFileNameExtension() != g_solibext)
525 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000526 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000527 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000528
Enrico Granatae743c782013-04-24 21:29:08 +0000529 Error plugin_load_error;
530 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000531
532 return FileSpec::eEnumerateDirectoryResultNext;
533 }
534
535 else if (file_type == FileSpec::eFileTypeUnknown ||
536 file_type == FileSpec::eFileTypeDirectory ||
537 file_type == FileSpec::eFileTypeSymbolicLink )
538 {
539 // Try and recurse into anything that a directory or symbolic link.
540 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000541 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000542 // information.
543 return FileSpec::eEnumerateDirectoryResultEnter;
544 }
545
546 return FileSpec::eEnumerateDirectoryResultNext;
547}
548
549void
550Debugger::InstanceInitialize ()
551{
552 FileSpec dir_spec;
553 const bool find_directories = true;
554 const bool find_files = true;
555 const bool find_other = true;
556 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000557 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000558 {
559 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
560 {
561 FileSpec::EnumerateDirectory (dir_path,
562 find_directories,
563 find_files,
564 find_other,
565 LoadPluginCallback,
566 this);
567 }
568 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000569
570 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000571 {
572 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
573 {
574 FileSpec::EnumerateDirectory (dir_path,
575 find_directories,
576 find_files,
577 find_other,
578 LoadPluginCallback,
579 this);
580 }
581 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000582
583 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000584}
585
Greg Clayton66111032010-06-23 01:19:29 +0000586DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000587Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000588{
Jim Ingham228063c2012-02-21 02:23:08 +0000589 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000590 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000591 {
592 Mutex::Locker locker (GetDebuggerListMutex ());
593 GetDebuggerList().push_back(debugger_sp);
594 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000595 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000596 return debugger_sp;
597}
598
Caroline Ticee02657b2011-01-22 01:02:07 +0000599void
Greg Clayton4d122c42011-09-17 08:33:22 +0000600Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000601{
602 if (debugger_sp.get() == NULL)
603 return;
604
Jim Ingham8314c522011-09-15 21:36:42 +0000605 debugger_sp->Clear();
606
Greg Claytonc15f55e2012-03-30 20:53:46 +0000607 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000608 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000609 Mutex::Locker locker (GetDebuggerListMutex ());
610 DebuggerList &debugger_list = GetDebuggerList ();
611 DebuggerList::iterator pos, end = debugger_list.end();
612 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000613 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000614 if ((*pos).get() == debugger_sp.get())
615 {
616 debugger_list.erase (pos);
617 return;
618 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000619 }
620 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000621}
622
Greg Clayton4d122c42011-09-17 08:33:22 +0000623DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000624Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
625{
Greg Clayton4d122c42011-09-17 08:33:22 +0000626 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000627 if (g_shared_debugger_refcount > 0)
628 {
629 Mutex::Locker locker (GetDebuggerListMutex ());
630 DebuggerList &debugger_list = GetDebuggerList();
631 DebuggerList::iterator pos, end = debugger_list.end();
632
633 for (pos = debugger_list.begin(); pos != end; ++pos)
634 {
635 if ((*pos).get()->m_instance_name == instance_name)
636 {
637 debugger_sp = *pos;
638 break;
639 }
640 }
641 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000642 return debugger_sp;
643}
Greg Clayton66111032010-06-23 01:19:29 +0000644
645TargetSP
646Debugger::FindTargetWithProcessID (lldb::pid_t pid)
647{
Greg Clayton4d122c42011-09-17 08:33:22 +0000648 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000649 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000650 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000651 Mutex::Locker locker (GetDebuggerListMutex ());
652 DebuggerList &debugger_list = GetDebuggerList();
653 DebuggerList::iterator pos, end = debugger_list.end();
654 for (pos = debugger_list.begin(); pos != end; ++pos)
655 {
656 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
657 if (target_sp)
658 break;
659 }
Greg Clayton66111032010-06-23 01:19:29 +0000660 }
661 return target_sp;
662}
663
Greg Claytone4e45922011-11-16 05:37:56 +0000664TargetSP
665Debugger::FindTargetWithProcess (Process *process)
666{
667 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000668 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +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().FindTargetWithProcess (process);
676 if (target_sp)
677 break;
678 }
Greg Claytone4e45922011-11-16 05:37:56 +0000679 }
680 return target_sp;
681}
682
Jason Molendae6481c72014-09-12 01:50:46 +0000683Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
684 UserID(g_unique_id++),
685 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
686 m_input_file_sp(new StreamFile(stdin, false)),
687 m_output_file_sp(new StreamFile(stdout, false)),
688 m_error_file_sp(new StreamFile(stderr, false)),
689 m_terminal_state(),
690 m_target_list(*this),
691 m_platform_list(),
692 m_listener("lldb.Debugger"),
693 m_source_manager_ap(),
694 m_source_file_cache(),
695 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
696 m_input_reader_stack(),
697 m_instance_name(),
Greg Claytonafa91e332014-12-01 22:41:27 +0000698 m_loaded_plugins(),
699 m_event_handler_thread (),
700 m_io_handler_thread (),
701 m_sync_broadcaster (NULL, "lldb.debugger.sync")
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702{
Greg Clayton67cc0632012-08-22 17:17:09 +0000703 char instance_cstr[256];
704 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
705 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000706 if (log_callback)
707 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000708 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000709 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000710 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000711 assert (default_platform_sp.get());
712 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000713
Greg Clayton754a9362012-08-23 00:22:02 +0000714 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000715 m_collection_sp->AppendProperty (ConstString("target"),
716 ConstString("Settings specify to debugging targets."),
717 true,
718 Target::GetGlobalProperties()->GetValueProperties());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000719 m_collection_sp->AppendProperty (ConstString("platform"),
720 ConstString("Platform settings."),
721 true,
722 Platform::GetGlobalPlatformProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000723 if (m_command_interpreter_ap.get())
724 {
725 m_collection_sp->AppendProperty (ConstString("interpreter"),
726 ConstString("Settings specify to the debugger's command interpreter."),
727 true,
728 m_command_interpreter_ap->GetValueProperties());
729 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000730 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
731 term_width->SetMinimumValue(10);
732 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000733
734 // Turn off use-color if this is a dumb terminal.
735 const char *term = getenv ("TERM");
736 if (term && !strcmp (term, "dumb"))
737 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738}
739
740Debugger::~Debugger ()
741{
Jim Ingham8314c522011-09-15 21:36:42 +0000742 Clear();
743}
744
745void
746Debugger::Clear()
747{
Greg Clayton44d93782014-01-27 23:43:24 +0000748 ClearIOHandlers();
749 StopIOHandlerThread();
750 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000751 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000752 int num_targets = m_target_list.GetNumTargets();
753 for (int i = 0; i < num_targets; i++)
754 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000755 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
756 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000757 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000758 ProcessSP process_sp (target_sp->GetProcessSP());
759 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000760 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000761 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000762 }
Greg Clayton66111032010-06-23 01:19:29 +0000763 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000764 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000765
766 // Close the input file _before_ we close the input read communications class
767 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000768 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000769 if (m_input_file_sp)
770 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000771
772 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000773}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774
775bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000776Debugger::GetCloseInputOnEOF () const
777{
Greg Clayton44d93782014-01-27 23:43:24 +0000778// return m_input_comm.GetCloseOnEOF();
779 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000780}
781
782void
783Debugger::SetCloseInputOnEOF (bool b)
784{
Greg Clayton44d93782014-01-27 23:43:24 +0000785// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000786}
787
788bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789Debugger::GetAsyncExecution ()
790{
Greg Clayton66111032010-06-23 01:19:29 +0000791 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792}
793
794void
795Debugger::SetAsyncExecution (bool async_execution)
796{
Greg Clayton66111032010-06-23 01:19:29 +0000797 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798}
799
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800
801void
802Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
803{
Greg Clayton44d93782014-01-27 23:43:24 +0000804 if (m_input_file_sp)
805 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
806 else
807 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
808
809 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000810 if (in_file.IsValid() == false)
811 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812
Jim Inghamc5917d92012-11-30 20:23:19 +0000813 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
814 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815}
816
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817void
818Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
819{
Greg Clayton44d93782014-01-27 23:43:24 +0000820 if (m_output_file_sp)
821 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
822 else
823 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
824
825 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000826 if (out_file.IsValid() == false)
827 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000828
Enrico Granatab5887262012-10-29 21:18:03 +0000829 // do not create the ScriptInterpreter just for setting the output file handle
830 // as the constructor will know how to do the right thing on its own
831 const bool can_create = false;
832 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
833 if (script_interpreter)
834 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835}
836
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837void
838Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
839{
Greg Clayton44d93782014-01-27 23:43:24 +0000840 if (m_error_file_sp)
841 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
842 else
843 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
844
845 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000846 if (err_file.IsValid() == false)
847 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848}
849
Jim Inghamc5917d92012-11-30 20:23:19 +0000850void
851Debugger::SaveInputTerminalState ()
852{
Greg Clayton44d93782014-01-27 23:43:24 +0000853 if (m_input_file_sp)
854 {
855 File &in_file = m_input_file_sp->GetFile();
856 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
857 m_terminal_state.Save(in_file.GetDescriptor(), true);
858 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000859}
860
861void
862Debugger::RestoreInputTerminalState ()
863{
864 m_terminal_state.Restore();
865}
866
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000868Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869{
870 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000871 TargetSP target_sp(GetSelectedTarget());
872 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873
874 if (target_sp)
875 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000876 ProcessSP process_sp (target_sp->GetProcessSP());
877 exe_ctx.SetProcessSP (process_sp);
878 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000880 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
881 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000883 exe_ctx.SetThreadSP (thread_sp);
884 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
885 if (exe_ctx.GetFramePtr() == NULL)
886 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887 }
888 }
889 }
890 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891}
892
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893void
Caroline Ticeefed6132010-11-19 20:47:54 +0000894Debugger::DispatchInputInterrupt ()
895{
Greg Clayton44d93782014-01-27 23:43:24 +0000896 Mutex::Locker locker (m_input_reader_stack.GetMutex());
897 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000898 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000899 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000900}
901
902void
903Debugger::DispatchInputEndOfFile ()
904{
Greg Clayton44d93782014-01-27 23:43:24 +0000905 Mutex::Locker locker (m_input_reader_stack.GetMutex());
906 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000907 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000908 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000909}
910
911void
Greg Clayton44d93782014-01-27 23:43:24 +0000912Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000913{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000914 // 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 +0000915 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000916 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000917 {
Greg Clayton44d93782014-01-27 23:43:24 +0000918 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000919 if (reader_sp)
920 {
Greg Clayton44d93782014-01-27 23:43:24 +0000921 m_input_reader_stack.Pop();
922 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000923 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000924 }
925 }
926}
927
928void
Siva Chandra9aaab552015-02-26 19:26:36 +0000929Debugger::ExecuteIOHandlers()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000930{
Caroline Tice9088b062011-05-09 23:06:58 +0000931
Greg Clayton44d93782014-01-27 23:43:24 +0000932 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 {
Greg Clayton44d93782014-01-27 23:43:24 +0000934 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935 if (!reader_sp)
936 break;
937
Greg Clayton44d93782014-01-27 23:43:24 +0000938 reader_sp->Activate();
939 reader_sp->Run();
940 reader_sp->Deactivate();
941
942 // Remove all input readers that are done from the top of the stack
943 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 {
Greg Clayton44d93782014-01-27 23:43:24 +0000945 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
946 if (top_reader_sp && top_reader_sp->GetIsDone())
947 m_input_reader_stack.Pop();
948 else
949 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950 }
951 }
Greg Clayton44d93782014-01-27 23:43:24 +0000952 ClearIOHandlers();
953}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954
Greg Clayton44d93782014-01-27 23:43:24 +0000955bool
956Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
957{
958 return m_input_reader_stack.IsTop (reader_sp);
959}
960
961
962ConstString
963Debugger::GetTopIOHandlerControlSequence(char ch)
964{
965 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966}
967
Kate Stonea487aa42015-01-15 00:52:41 +0000968const char *
969Debugger::GetIOHandlerCommandPrefix()
970{
971 return m_input_reader_stack.GetTopIOHandlerCommandPrefix();
972}
973
974const char *
975Debugger::GetIOHandlerHelpPrologue()
976{
977 return m_input_reader_stack.GetTopIOHandlerHelpPrologue();
978}
979
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980void
Greg Clayton44d93782014-01-27 23:43:24 +0000981Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
982{
Greg Clayton44d93782014-01-27 23:43:24 +0000983 PushIOHandler (reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +0000984
985 IOHandlerSP top_reader_sp = reader_sp;
986 while (top_reader_sp)
987 {
988 top_reader_sp->Activate();
989 top_reader_sp->Run();
990 top_reader_sp->Deactivate();
991
992 if (top_reader_sp.get() == reader_sp.get())
993 {
994 if (PopIOHandler (reader_sp))
995 break;
996 }
997
998 while (1)
999 {
1000 top_reader_sp = m_input_reader_stack.Top();
1001 if (top_reader_sp && top_reader_sp->GetIsDone())
1002 m_input_reader_stack.Pop();
1003 else
1004 break;
1005 }
1006 }
Greg Clayton44d93782014-01-27 23:43:24 +00001007}
1008
1009void
1010Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
1011{
1012 // Before an IOHandler runs, it must have in/out/err streams.
1013 // This function is called when one ore more of the streams
1014 // are NULL. We use the top input reader's in/out/err streams,
1015 // or fall back to the debugger file handles, or we fall back
1016 // onto stdin/stdout/stderr as a last resort.
1017
1018 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1019 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
1020 // If no STDIN has been set, then set it appropriately
1021 if (!in)
1022 {
1023 if (top_reader_sp)
1024 in = top_reader_sp->GetInputStreamFile();
1025 else
1026 in = GetInputFile();
1027
1028 // If there is nothing, use stdin
1029 if (!in)
1030 in = StreamFileSP(new StreamFile(stdin, false));
1031 }
1032 // If no STDOUT has been set, then set it appropriately
1033 if (!out)
1034 {
1035 if (top_reader_sp)
1036 out = top_reader_sp->GetOutputStreamFile();
1037 else
1038 out = GetOutputFile();
1039
1040 // If there is nothing, use stdout
1041 if (!out)
1042 out = StreamFileSP(new StreamFile(stdout, false));
1043 }
1044 // If no STDERR has been set, then set it appropriately
1045 if (!err)
1046 {
1047 if (top_reader_sp)
1048 err = top_reader_sp->GetErrorStreamFile();
1049 else
1050 err = GetErrorFile();
1051
1052 // If there is nothing, use stderr
1053 if (!err)
1054 err = StreamFileSP(new StreamFile(stdout, false));
1055
1056 }
1057}
1058
1059void
1060Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061{
1062 if (!reader_sp)
1063 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +00001064
Greg Clayton44d93782014-01-27 23:43:24 +00001065 // Got the current top input reader...
1066 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001067
Greg Claytonb4874f12014-02-28 18:22:24 +00001068 // Don't push the same IO handler twice...
1069 if (reader_sp.get() != top_reader_sp.get())
1070 {
1071 // Push our new input reader
1072 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00001073
Greg Claytonb4874f12014-02-28 18:22:24 +00001074 // Interrupt the top input reader to it will exit its Run() function
1075 // and let this new input reader take over
1076 if (top_reader_sp)
1077 top_reader_sp->Deactivate();
1078 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079}
1080
1081bool
Greg Clayton44d93782014-01-27 23:43:24 +00001082Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083{
1084 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +00001085
1086 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087
1088 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001089 // read on the stack refresh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001090 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091 {
Greg Clayton44d93782014-01-27 23:43:24 +00001092 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093
1094 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
1095 {
Greg Clayton44d93782014-01-27 23:43:24 +00001096 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +00001097 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001098 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +00001099
1100 reader_sp = m_input_reader_stack.Top();
1101 if (reader_sp)
1102 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103
Greg Clayton44d93782014-01-27 23:43:24 +00001104 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 }
1106 }
1107 return result;
1108}
1109
1110bool
Greg Clayton44d93782014-01-27 23:43:24 +00001111Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112{
Greg Clayton44d93782014-01-27 23:43:24 +00001113 Mutex::Locker locker;
1114
1115 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116 {
Greg Clayton44d93782014-01-27 23:43:24 +00001117 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1118 if (reader_sp)
1119 reader_sp->Hide();
1120 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121 }
Greg Clayton44d93782014-01-27 23:43:24 +00001122 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123}
1124
1125void
Greg Clayton44d93782014-01-27 23:43:24 +00001126Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127{
Greg Clayton44d93782014-01-27 23:43:24 +00001128 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1129 if (reader_sp)
1130 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131}
Greg Clayton66111032010-06-23 01:19:29 +00001132
Greg Clayton44d93782014-01-27 23:43:24 +00001133
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001134StreamSP
1135Debugger::GetAsyncOutputStream ()
1136{
1137 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1138 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1139}
1140
1141StreamSP
1142Debugger::GetAsyncErrorStream ()
1143{
1144 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1145 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1146}
1147
Greg Claytonc7bece562013-01-25 18:06:21 +00001148size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001149Debugger::GetNumDebuggers()
1150{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001151 if (g_shared_debugger_refcount > 0)
1152 {
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
Greg Claytonc15f55e2012-03-30 20:53:46 +00001164 if (g_shared_debugger_refcount > 0)
1165 {
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
Greg Claytonc15f55e2012-03-30 20:53:46 +00001181 if (g_shared_debugger_refcount > 0)
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{
1333 Log::Callbacks log_callbacks;
1334
1335 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 {
1353 log_stream_sp.reset (new StreamFile (log_file));
1354 m_log_streams[log_file] = log_stream_sp;
1355 }
Jim Ingham228063c2012-02-21 02:23:08 +00001356 }
1357 assert (log_stream_sp.get());
1358
1359 if (log_options == 0)
1360 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
1361
Greg Clayton57abc5d2013-05-10 21:47:16 +00001362 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00001363 {
1364 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
1365 return true;
1366 }
1367 else
1368 {
1369 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
1370 if (log_channel_sp)
1371 {
1372 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
1373 {
1374 return true;
1375 }
1376 else
1377 {
1378 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
1379 return false;
1380 }
1381 }
1382 else
1383 {
1384 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
1385 return false;
1386 }
1387 }
1388 return false;
1389}
1390
Greg Clayton9585fbf2013-03-19 00:20:55 +00001391SourceManager &
1392Debugger::GetSourceManager ()
1393{
1394 if (m_source_manager_ap.get() == NULL)
1395 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
1396 return *m_source_manager_ap;
1397}
1398
1399
Greg Clayton44d93782014-01-27 23:43:24 +00001400
1401// This function handles events that were broadcast by the process.
1402void
1403Debugger::HandleBreakpointEvent (const EventSP &event_sp)
1404{
1405 using namespace lldb;
1406 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
1407
1408// if (event_type & eBreakpointEventTypeAdded
1409// || event_type & eBreakpointEventTypeRemoved
1410// || event_type & eBreakpointEventTypeEnabled
1411// || event_type & eBreakpointEventTypeDisabled
1412// || event_type & eBreakpointEventTypeCommandChanged
1413// || event_type & eBreakpointEventTypeConditionChanged
1414// || event_type & eBreakpointEventTypeIgnoreChanged
1415// || event_type & eBreakpointEventTypeLocationsResolved)
1416// {
1417// // Don't do anything about these events, since the breakpoint commands already echo these actions.
1418// }
1419//
1420 if (event_type & eBreakpointEventTypeLocationsAdded)
1421 {
1422 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
1423 if (num_new_locations > 0)
1424 {
1425 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
1426 StreamFileSP output_sp (GetOutputFile());
1427 if (output_sp)
1428 {
1429 output_sp->Printf("%d location%s added to breakpoint %d\n",
1430 num_new_locations,
1431 num_new_locations == 1 ? "" : "s",
1432 breakpoint->GetID());
1433 RefreshTopIOHandler();
1434 }
1435 }
1436 }
1437// else if (event_type & eBreakpointEventTypeLocationsRemoved)
1438// {
1439// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
1440// }
1441// else if (event_type & eBreakpointEventTypeLocationsResolved)
1442// {
1443// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
1444// }
1445}
1446
1447size_t
1448Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
1449{
1450 size_t total_bytes = 0;
1451 if (stream == NULL)
1452 stream = GetOutputFile().get();
1453
1454 if (stream)
1455 {
1456 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
1457 if (process == NULL)
1458 {
1459 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1460 if (target_sp)
1461 process = target_sp->GetProcessSP().get();
1462 }
1463 if (process)
1464 {
1465 Error error;
1466 size_t len;
1467 char stdio_buffer[1024];
1468 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1469 {
1470 stream->Write(stdio_buffer, len);
1471 total_bytes += len;
1472 }
1473 }
1474 stream->Flush();
1475 }
1476 return total_bytes;
1477}
1478
1479size_t
1480Debugger::GetProcessSTDERR (Process *process, Stream *stream)
1481{
1482 size_t total_bytes = 0;
1483 if (stream == NULL)
1484 stream = GetOutputFile().get();
1485
1486 if (stream)
1487 {
1488 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
1489 if (process == NULL)
1490 {
1491 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1492 if (target_sp)
1493 process = target_sp->GetProcessSP().get();
1494 }
1495 if (process)
1496 {
1497 Error error;
1498 size_t len;
1499 char stdio_buffer[1024];
1500 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1501 {
1502 stream->Write(stdio_buffer, len);
1503 total_bytes += len;
1504 }
1505 }
1506 stream->Flush();
1507 }
1508 return total_bytes;
1509}
1510
Greg Claytondc6224e2014-10-21 01:00:42 +00001511
Greg Clayton44d93782014-01-27 23:43:24 +00001512// This function handles events that were broadcast by the process.
1513void
1514Debugger::HandleProcessEvent (const EventSP &event_sp)
1515{
1516 using namespace lldb;
1517 const uint32_t event_type = event_sp->GetType();
1518 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00001519
Greg Claytonb4874f12014-02-28 18:22:24 +00001520 StreamString output_stream;
1521 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00001522 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00001523
Greg Claytonb4874f12014-02-28 18:22:24 +00001524 if (!gui_enabled)
1525 {
1526 bool pop_process_io_handler = false;
1527 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00001528
Greg Claytonb4874f12014-02-28 18:22:24 +00001529 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00001530 {
Greg Claytonb4874f12014-02-28 18:22:24 +00001531 GetProcessSTDOUT (process_sp.get(), &output_stream);
1532 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001533
Greg Claytonb4874f12014-02-28 18:22:24 +00001534 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
1535 {
1536 GetProcessSTDERR (process_sp.get(), &error_stream);
1537 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001538
Greg Claytonb4874f12014-02-28 18:22:24 +00001539 if (event_type & Process::eBroadcastBitStateChanged)
1540 {
Greg Claytondc6224e2014-10-21 01:00:42 +00001541 Process::HandleProcessStateChangedEvent (event_sp, &output_stream, pop_process_io_handler);
Greg Clayton44d93782014-01-27 23:43:24 +00001542 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001543
Greg Claytonb4874f12014-02-28 18:22:24 +00001544 if (output_stream.GetSize() || error_stream.GetSize())
1545 {
1546 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00001547 bool top_io_handler_hid = false;
Greg Claytondc6224e2014-10-21 01:00:42 +00001548
Greg Clayton6fea17e2014-03-03 19:15:20 +00001549 if (process_sp->ProcessIOHandlerIsActive() == false)
1550 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00001551
1552 if (output_stream.GetSize())
1553 {
1554 StreamFileSP output_stream_sp (GetOutputFile());
1555 if (output_stream_sp)
1556 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
1557 }
1558
1559 if (error_stream.GetSize())
1560 {
1561 StreamFileSP error_stream_sp (GetErrorFile());
1562 if (error_stream_sp)
1563 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
1564 }
1565
1566 if (top_io_handler_hid)
1567 RefreshTopIOHandler();
1568 }
1569
1570 if (pop_process_io_handler)
1571 process_sp->PopProcessIOHandler();
1572 }
Greg Clayton44d93782014-01-27 23:43:24 +00001573}
1574
1575void
1576Debugger::HandleThreadEvent (const EventSP &event_sp)
1577{
1578 // At present the only thread event we handle is the Frame Changed event,
1579 // and all we do for that is just reprint the thread status for that thread.
1580 using namespace lldb;
1581 const uint32_t event_type = event_sp->GetType();
1582 if (event_type == Thread::eBroadcastBitStackChanged ||
1583 event_type == Thread::eBroadcastBitThreadSelected )
1584 {
1585 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
1586 if (thread_sp)
1587 {
1588 HideTopIOHandler();
1589 StreamFileSP stream_sp (GetOutputFile());
1590 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
1591 RefreshTopIOHandler();
1592 }
1593 }
1594}
1595
1596bool
1597Debugger::IsForwardingEvents ()
1598{
1599 return (bool)m_forward_listener_sp;
1600}
1601
1602void
1603Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
1604{
1605 m_forward_listener_sp = listener_sp;
1606}
1607
1608void
1609Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
1610{
1611 m_forward_listener_sp.reset();
1612}
1613
1614
1615void
1616Debugger::DefaultEventHandler()
1617{
1618 Listener& listener(GetListener());
1619 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
1620 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
1621 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
1622 BroadcastEventSpec target_event_spec (broadcaster_class_target,
1623 Target::eBroadcastBitBreakpointChanged);
1624
1625 BroadcastEventSpec process_event_spec (broadcaster_class_process,
1626 Process::eBroadcastBitStateChanged |
1627 Process::eBroadcastBitSTDOUT |
1628 Process::eBroadcastBitSTDERR);
1629
1630 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
1631 Thread::eBroadcastBitStackChanged |
1632 Thread::eBroadcastBitThreadSelected );
1633
1634 listener.StartListeningForEventSpec (*this, target_event_spec);
1635 listener.StartListeningForEventSpec (*this, process_event_spec);
1636 listener.StartListeningForEventSpec (*this, thread_event_spec);
1637 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
1638 CommandInterpreter::eBroadcastBitQuitCommandReceived |
1639 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
1640 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
Greg Claytonafa91e332014-12-01 22:41:27 +00001641
1642 // Let the thread that spawned us know that we have started up and
1643 // that we are now listening to all required events so no events get missed
1644 m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
1645
Greg Clayton44d93782014-01-27 23:43:24 +00001646 bool done = false;
1647 while (!done)
1648 {
Greg Clayton44d93782014-01-27 23:43:24 +00001649 EventSP event_sp;
1650 if (listener.WaitForEvent(NULL, event_sp))
1651 {
1652 if (event_sp)
1653 {
1654 Broadcaster *broadcaster = event_sp->GetBroadcaster();
1655 if (broadcaster)
1656 {
1657 uint32_t event_type = event_sp->GetType();
1658 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
1659 if (broadcaster_class == broadcaster_class_process)
1660 {
1661 HandleProcessEvent (event_sp);
1662 }
1663 else if (broadcaster_class == broadcaster_class_target)
1664 {
1665 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
1666 {
1667 HandleBreakpointEvent (event_sp);
1668 }
1669 }
1670 else if (broadcaster_class == broadcaster_class_thread)
1671 {
1672 HandleThreadEvent (event_sp);
1673 }
1674 else if (broadcaster == m_command_interpreter_ap.get())
1675 {
1676 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
1677 {
1678 done = true;
1679 }
1680 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
1681 {
1682 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1683 if (data && data[0])
1684 {
1685 StreamFileSP error_sp (GetErrorFile());
1686 if (error_sp)
1687 {
1688 HideTopIOHandler();
1689 error_sp->PutCString(data);
1690 error_sp->Flush();
1691 RefreshTopIOHandler();
1692 }
1693 }
1694 }
1695 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
1696 {
1697 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1698 if (data && data[0])
1699 {
1700 StreamFileSP output_sp (GetOutputFile());
1701 if (output_sp)
1702 {
1703 HideTopIOHandler();
1704 output_sp->PutCString(data);
1705 output_sp->Flush();
1706 RefreshTopIOHandler();
1707 }
1708 }
1709 }
1710 }
1711 }
1712
1713 if (m_forward_listener_sp)
1714 m_forward_listener_sp->AddEvent(event_sp);
1715 }
1716 }
1717 }
1718}
1719
1720lldb::thread_result_t
1721Debugger::EventHandlerThread (lldb::thread_arg_t arg)
1722{
1723 ((Debugger *)arg)->DefaultEventHandler();
1724 return NULL;
1725}
1726
1727bool
1728Debugger::StartEventHandlerThread()
1729{
Zachary Turneracee96a2014-09-23 18:32:09 +00001730 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001731 {
Greg Claytonafa91e332014-12-01 22:41:27 +00001732 // We must synchronize with the DefaultEventHandler() thread to ensure
1733 // it is up and running and listening to events before we return from
1734 // this function. We do this by listening to events for the
1735 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
1736 Listener listener("lldb.debugger.event-handler");
1737 listener.StartListeningForEvents(&m_sync_broadcaster, eBroadcastBitEventThreadIsListening);
1738
Zachary Turner7c2896a2014-10-24 22:06:29 +00001739 // Use larger 8MB stack for this thread
Greg Claytonafa91e332014-12-01 22:41:27 +00001740 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread,
1741 this,
1742 NULL,
Zachary Turner7c2896a2014-10-24 22:06:29 +00001743 g_debugger_event_thread_stack_bytes);
Greg Claytonafa91e332014-12-01 22:41:27 +00001744
1745 // Make sure DefaultEventHandler() is running and listening to events before we return
1746 // from this function. We are only listening for events of type
1747 // eBroadcastBitEventThreadIsListening so we don't need to check the event, we just need
1748 // to wait an infinite amount of time for it (NULL timeout as the first parameter)
1749 lldb::EventSP event_sp;
1750 listener.WaitForEvent(NULL, event_sp);
Greg Clayton807b6b32014-10-15 18:03:59 +00001751 }
Zachary Turneracee96a2014-09-23 18:32:09 +00001752 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001753}
1754
1755void
1756Debugger::StopEventHandlerThread()
1757{
Zachary Turneracee96a2014-09-23 18:32:09 +00001758 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001759 {
1760 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00001761 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001762 }
1763}
1764
1765
1766lldb::thread_result_t
1767Debugger::IOHandlerThread (lldb::thread_arg_t arg)
1768{
1769 Debugger *debugger = (Debugger *)arg;
Siva Chandra9aaab552015-02-26 19:26:36 +00001770 debugger->ExecuteIOHandlers();
Greg Clayton44d93782014-01-27 23:43:24 +00001771 debugger->StopEventHandlerThread();
1772 return NULL;
1773}
1774
1775bool
1776Debugger::StartIOHandlerThread()
1777{
Zachary Turneracee96a2014-09-23 18:32:09 +00001778 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001779 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
1780 IOHandlerThread,
1781 this,
1782 NULL,
1783 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00001784 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001785}
1786
1787void
1788Debugger::StopIOHandlerThread()
1789{
Zachary Turneracee96a2014-09-23 18:32:09 +00001790 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001791 {
1792 if (m_input_file_sp)
1793 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00001794 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001795 }
1796}
1797
Jim Ingham893c9322014-11-22 01:42:44 +00001798Target *
1799Debugger::GetDummyTarget()
1800{
1801 return m_target_list.GetDummyTarget (*this).get();
1802}
1803
1804Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001805Debugger::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001806{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001807 Target *target = nullptr;
1808 if (!prefer_dummy)
1809 {
1810 target = m_target_list.GetSelectedTarget().get();
1811 if (target)
1812 return target;
1813 }
1814
Jim Ingham893c9322014-11-22 01:42:44 +00001815 return GetDummyTarget();
1816}
Greg Clayton44d93782014-01-27 23:43:24 +00001817