blob: a4d8da960a88f7f8fc577f8cc6f2028133c09344 [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
Enrico Granata4becb372011-06-29 22:27:15 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/Type.h"
Jason Molenda705b1802014-06-13 02:37:02 +000016#include "llvm/ADT/StringRef.h"
Enrico Granata4becb372011-06-29 22:27:15 +000017
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/lldb-private.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000019#include "lldb/Core/FormatEntity.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Module.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000021#include "lldb/Core/PluginInterface.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000022#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000023#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000026#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000027#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000028#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000029#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000031#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000032#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000033#include "lldb/DataFormatters/DataVisualization.h"
34#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000035#include "lldb/DataFormatters/TypeSummary.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000036#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000037#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000038#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000039#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000040#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000041#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000042#include "lldb/Interpreter/OptionValueSInt64.h"
43#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000044#include "lldb/Symbol/ClangASTContext.h"
45#include "lldb/Symbol/CompileUnit.h"
46#include "lldb/Symbol/Function.h"
47#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000048#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Target/TargetList.h"
50#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000051#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000052#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000053#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000054#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000056#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Zachary Turner58a559c2014-08-27 20:15:09 +000058#include "llvm/Support/DynamicLibrary.h"
59
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060using namespace lldb;
61using namespace lldb_private;
62
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Caroline Ticeebc1bb22010-06-30 16:22:25 +000064static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000065static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000066
Greg Clayton1b654882010-09-19 02:33:57 +000067#pragma mark Static Functions
68
69static Mutex &
70GetDebuggerListMutex ()
71{
72 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
73 return g_mutex;
74}
75
76typedef std::vector<DebuggerSP> DebuggerList;
77
78static DebuggerList &
79GetDebuggerList()
80{
81 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000082 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000083 static DebuggerList g_list;
84 return g_list;
85}
Greg Claytone372b982011-11-21 21:44:34 +000086
87OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000088g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000089{
Greg Clayton67cc0632012-08-22 17:17:09 +000090 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
91 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
92 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000093 { 0, NULL, NULL }
94};
95
Greg Clayton67cc0632012-08-22 17:17:09 +000096OptionEnumValueElement
97g_language_enumerators[] =
98{
99 { eScriptLanguageNone, "none", "Disable scripting languages."},
100 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
101 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +0000102 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000103};
Greg Claytone372b982011-11-21 21:44:34 +0000104
Greg Clayton67cc0632012-08-22 17:17:09 +0000105#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
106#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Jason Molenda6ab659a2015-07-29 00:42:47 +0000107#define IS_OPTIMIZED "{${function.is-optimized} [opt]}"
Greg Clayton67cc0632012-08-22 17:17:09 +0000108
Michael Sartain0769b2b2013-07-30 16:44:36 +0000109#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000110 "{, ${frame.pc}}"\
111 MODULE_WITH_FUNC\
112 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000113 "{, name = '${thread.name}'}"\
114 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000115 "{, activity = '${thread.info.activity.name}'}" \
116 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000117 "{, stop reason = ${thread.stop-reason}}"\
118 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000119 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000120 "\\n"
121
122#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
123 MODULE_WITH_FUNC\
124 FILE_AND_LINE\
Jason Molenda6ab659a2015-07-29 00:42:47 +0000125 IS_OPTIMIZED\
Greg Clayton67cc0632012-08-22 17:17:09 +0000126 "\\n"
127
Jason Molendac980fa92015-02-13 23:24:21 +0000128// Three parts to this disassembly format specification:
129// 1. If this is a new function/symbol (no previous symbol/function), print
130// dylib`funcname:\n
131// 2. If this is a symbol context change (different from previous symbol/function), print
132// dylib`funcname:\n
133// 3. print
134// address <+offset>:
135#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}>}: "
136
137// gdb's disassembly format can be emulated with
138// ${current-pc-arrow}${addr-file-or-load}{ <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>}:
139
140// lldb's original format for disassembly would look like this format string -
141// {${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}}:
142
Greg Clayton67cc0632012-08-22 17:17:09 +0000143
Greg Clayton754a9362012-08-23 00:22:02 +0000144static PropertyDefinition
145g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000146{
Greg Clayton554f68d2015-02-04 22:00:53 +0000147{ "auto-confirm", OptionValue::eTypeBoolean , true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
148{ "disassembly-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." },
149{ "frame-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
150{ "notify-void", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
151{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
152{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
153{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
154{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
155{ "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." },
156{ "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." },
157{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
158{ "thread-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
159{ "use-external-editor", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Whether to use an external editor or not." },
160{ "use-color", OptionValue::eTypeBoolean , true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
161{ "auto-one-line-summaries", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
162{ "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,
183 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000184};
185
Zachary Turner3a006912015-03-19 22:00:21 +0000186LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000187
188Error
189Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
190 VarSetOperationType op,
191 const char *property_path,
192 const char *value)
193{
Enrico Granata84a53df2013-05-20 22:29:23 +0000194 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000195 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000196 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000197 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000198 if (is_load_script && exe_ctx->GetTargetSP())
199 {
200 target_sp = exe_ctx->GetTargetSP();
201 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
202 }
Greg Clayton4c054102012-09-01 00:38:36 +0000203 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
204 if (error.Success())
205 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000206 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000207 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
208 {
209 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000210 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
211 if (str.length())
212 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000213 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000214 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
215 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
216 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000217 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
218 {
219 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
220 SetPrompt (GetPrompt());
221 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000222 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000223 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000224 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000225 {
226 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000227 StreamString feedback_stream;
228 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000229 {
Greg Clayton44d93782014-01-27 23:43:24 +0000230 StreamFileSP stream_sp (GetErrorFile());
231 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000232 {
Greg Clayton44d93782014-01-27 23:43:24 +0000233 for (auto error : errors)
234 {
235 stream_sp->Printf("%s\n",error.AsCString());
236 }
237 if (feedback_stream.GetSize())
238 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000239 }
240 }
241 }
242 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000243 else if (is_escape_non_printables)
244 {
245 DataVisualization::ForceUpdate();
246 }
Greg Clayton4c054102012-09-01 00:38:36 +0000247 }
248 return error;
249}
250
Greg Clayton67cc0632012-08-22 17:17:09 +0000251bool
252Debugger::GetAutoConfirm () const
253{
254 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000255 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000256}
257
Greg Clayton554f68d2015-02-04 22:00:53 +0000258const FormatEntity::Entry *
Jason Molendaaff1b352014-10-10 23:07:36 +0000259Debugger::GetDisassemblyFormat() const
260{
261 const uint32_t idx = ePropertyDisassemblyFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000262 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Jason Molendaaff1b352014-10-10 23:07:36 +0000263}
264
Greg Clayton554f68d2015-02-04 22:00:53 +0000265const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000266Debugger::GetFrameFormat() const
267{
268 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000269 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000270}
271
272bool
273Debugger::GetNotifyVoid () const
274{
275 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000276 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000277}
278
279const char *
280Debugger::GetPrompt() const
281{
282 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000283 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000284}
285
286void
287Debugger::SetPrompt(const char *p)
288{
289 const uint32_t idx = ePropertyPrompt;
290 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
291 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000292 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
293 if (str.length())
294 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000295 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000296}
297
Greg Clayton554f68d2015-02-04 22:00:53 +0000298const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000299Debugger::GetThreadFormat() const
300{
301 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000302 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000303}
304
305lldb::ScriptLanguage
306Debugger::GetScriptLanguage() const
307{
308 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000309 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000310}
311
312bool
313Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
314{
315 const uint32_t idx = ePropertyScriptLanguage;
316 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
317}
318
319uint32_t
320Debugger::GetTerminalWidth () const
321{
322 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000323 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000324}
325
326bool
327Debugger::SetTerminalWidth (uint32_t term_width)
328{
329 const uint32_t idx = ePropertyTerminalWidth;
330 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
331}
332
333bool
334Debugger::GetUseExternalEditor () const
335{
336 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000337 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000338}
339
340bool
341Debugger::SetUseExternalEditor (bool b)
342{
343 const uint32_t idx = ePropertyUseExternalEditor;
344 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
345}
346
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000347bool
348Debugger::GetUseColor () const
349{
350 const uint32_t idx = ePropertyUseColor;
351 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
352}
353
354bool
355Debugger::SetUseColor (bool b)
356{
357 const uint32_t idx = ePropertyUseColor;
358 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
359 SetPrompt (GetPrompt());
360 return ret;
361}
362
Greg Clayton67cc0632012-08-22 17:17:09 +0000363uint32_t
364Debugger::GetStopSourceLineCount (bool before) const
365{
366 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000367 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000368}
369
370Debugger::StopDisassemblyType
371Debugger::GetStopDisassemblyDisplay () const
372{
373 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000374 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000375}
376
377uint32_t
378Debugger::GetDisassemblyLineCount () const
379{
380 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000381 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000382}
Greg Claytone372b982011-11-21 21:44:34 +0000383
Enrico Granata553fad52013-10-25 23:09:40 +0000384bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000385Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000386{
Enrico Granata90a8db32013-10-31 21:01:07 +0000387 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000388 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000389}
Enrico Granata553fad52013-10-25 23:09:40 +0000390
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000391bool
392Debugger::GetEscapeNonPrintables () const
393{
394 const uint32_t idx = ePropertyEscapeNonPrintables;
395 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000396}
397
Greg Clayton1b654882010-09-19 02:33:57 +0000398#pragma mark Debugger
399
Greg Clayton67cc0632012-08-22 17:17:09 +0000400//const DebuggerPropertiesSP &
401//Debugger::GetSettings() const
402//{
403// return m_properties_sp;
404//}
405//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000406
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000407static bool lldb_initialized = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408void
Zachary Turner3a006912015-03-19 22:00:21 +0000409Debugger::Initialize(LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000411 assert(!lldb_initialized && "Debugger::Initialize called more than once!");
412
Robert Flackf196c932015-03-10 18:07:47 +0000413 lldb_initialized = true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000414 g_load_plugin_callback = load_plugin_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415}
416
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000417void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418Debugger::Terminate ()
419{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000420 assert(lldb_initialized && "Debugger::Terminate called without a matching Debugger::Initialize!");
421
422 // Clear our master list of debugger objects
423 Mutex::Locker locker (GetDebuggerListMutex ());
Oleksiy Vyalovf3cd1812015-09-08 16:26:32 +0000424 auto& debuggers = GetDebuggerList();
425 for (const auto& debugger: debuggers)
426 debugger->Clear();
427
428 debuggers.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429}
430
Caroline Tice20bd37f2011-03-10 22:14:10 +0000431void
432Debugger::SettingsInitialize ()
433{
Greg Clayton6920b522012-08-22 18:39:03 +0000434 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000435}
436
437void
438Debugger::SettingsTerminate ()
439{
Greg Clayton6920b522012-08-22 18:39:03 +0000440 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000441}
442
Enrico Granata21dfcd92012-09-28 23:57:51 +0000443bool
Enrico Granatae743c782013-04-24 21:29:08 +0000444Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000445{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000446 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000447 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000448 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
449 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000450 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000451 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000452 return true;
453 }
Enrico Granatae743c782013-04-24 21:29:08 +0000454 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000455 else
Enrico Granatae743c782013-04-24 21:29:08 +0000456 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000457 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
458 // and if the public API layer isn't available (code is linking against
459 // all of the internal LLDB static libraries), then we can't load plugins
460 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000461 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000462 return false;
463}
464
465static FileSpec::EnumerateDirectoryResult
466LoadPluginCallback
467(
468 void *baton,
469 FileSpec::FileType file_type,
470 const FileSpec &file_spec
471 )
472{
473 Error error;
474
475 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000476 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000477
478 if (!baton)
479 return FileSpec::eEnumerateDirectoryResultQuit;
480
481 Debugger *debugger = (Debugger*)baton;
482
483 // If we have a regular file, a symbolic link or unknown file type, try
484 // and process the file. We must handle unknown as sometimes the directory
485 // enumeration might be enumerating a file system that doesn't have correct
486 // file type information.
487 if (file_type == FileSpec::eFileTypeRegular ||
488 file_type == FileSpec::eFileTypeSymbolicLink ||
489 file_type == FileSpec::eFileTypeUnknown )
490 {
491 FileSpec plugin_file_spec (file_spec);
492 plugin_file_spec.ResolvePath ();
493
Michael Sartain3cf443d2013-07-17 00:26:30 +0000494 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
495 plugin_file_spec.GetFileNameExtension() != g_solibext)
496 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000497 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000498 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000499
Enrico Granatae743c782013-04-24 21:29:08 +0000500 Error plugin_load_error;
501 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000502
503 return FileSpec::eEnumerateDirectoryResultNext;
504 }
505
506 else if (file_type == FileSpec::eFileTypeUnknown ||
507 file_type == FileSpec::eFileTypeDirectory ||
508 file_type == FileSpec::eFileTypeSymbolicLink )
509 {
510 // Try and recurse into anything that a directory or symbolic link.
511 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000512 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000513 // information.
514 return FileSpec::eEnumerateDirectoryResultEnter;
515 }
516
517 return FileSpec::eEnumerateDirectoryResultNext;
518}
519
520void
521Debugger::InstanceInitialize ()
522{
523 FileSpec dir_spec;
524 const bool find_directories = true;
525 const bool find_files = true;
526 const bool find_other = true;
527 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000528 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000529 {
530 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
531 {
532 FileSpec::EnumerateDirectory (dir_path,
533 find_directories,
534 find_files,
535 find_other,
536 LoadPluginCallback,
537 this);
538 }
539 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000540
541 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000542 {
543 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
544 {
545 FileSpec::EnumerateDirectory (dir_path,
546 find_directories,
547 find_files,
548 find_other,
549 LoadPluginCallback,
550 this);
551 }
552 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000553
554 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000555}
556
Greg Clayton66111032010-06-23 01:19:29 +0000557DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000558Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000559{
Jim Ingham228063c2012-02-21 02:23:08 +0000560 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000561 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000562 {
563 Mutex::Locker locker (GetDebuggerListMutex ());
564 GetDebuggerList().push_back(debugger_sp);
565 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000566 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000567 return debugger_sp;
568}
569
Caroline Ticee02657b2011-01-22 01:02:07 +0000570void
Greg Clayton4d122c42011-09-17 08:33:22 +0000571Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000572{
573 if (debugger_sp.get() == NULL)
574 return;
575
Jim Ingham8314c522011-09-15 21:36:42 +0000576 debugger_sp->Clear();
577
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000578 if (lldb_initialized)
Caroline Ticee02657b2011-01-22 01:02:07 +0000579 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000580 Mutex::Locker locker (GetDebuggerListMutex ());
581 DebuggerList &debugger_list = GetDebuggerList ();
582 DebuggerList::iterator pos, end = debugger_list.end();
583 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000584 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000585 if ((*pos).get() == debugger_sp.get())
586 {
587 debugger_list.erase (pos);
588 return;
589 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000590 }
591 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000592}
593
Greg Clayton4d122c42011-09-17 08:33:22 +0000594DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000595Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
596{
Greg Clayton4d122c42011-09-17 08:33:22 +0000597 DebuggerSP debugger_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000598 if (lldb_initialized)
Greg Clayton6920b522012-08-22 18:39:03 +0000599 {
600 Mutex::Locker locker (GetDebuggerListMutex ());
601 DebuggerList &debugger_list = GetDebuggerList();
602 DebuggerList::iterator pos, end = debugger_list.end();
603
604 for (pos = debugger_list.begin(); pos != end; ++pos)
605 {
606 if ((*pos).get()->m_instance_name == instance_name)
607 {
608 debugger_sp = *pos;
609 break;
610 }
611 }
612 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000613 return debugger_sp;
614}
Greg Clayton66111032010-06-23 01:19:29 +0000615
616TargetSP
617Debugger::FindTargetWithProcessID (lldb::pid_t pid)
618{
Greg Clayton4d122c42011-09-17 08:33:22 +0000619 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000620 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000621 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000622 Mutex::Locker locker (GetDebuggerListMutex ());
623 DebuggerList &debugger_list = GetDebuggerList();
624 DebuggerList::iterator pos, end = debugger_list.end();
625 for (pos = debugger_list.begin(); pos != end; ++pos)
626 {
627 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
628 if (target_sp)
629 break;
630 }
Greg Clayton66111032010-06-23 01:19:29 +0000631 }
632 return target_sp;
633}
634
Greg Claytone4e45922011-11-16 05:37:56 +0000635TargetSP
636Debugger::FindTargetWithProcess (Process *process)
637{
638 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000639 if (lldb_initialized)
Greg Claytone4e45922011-11-16 05:37:56 +0000640 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000641 Mutex::Locker locker (GetDebuggerListMutex ());
642 DebuggerList &debugger_list = GetDebuggerList();
643 DebuggerList::iterator pos, end = debugger_list.end();
644 for (pos = debugger_list.begin(); pos != end; ++pos)
645 {
646 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
647 if (target_sp)
648 break;
649 }
Greg Claytone4e45922011-11-16 05:37:56 +0000650 }
651 return target_sp;
652}
653
Jason Molendae6481c72014-09-12 01:50:46 +0000654Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
655 UserID(g_unique_id++),
656 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
657 m_input_file_sp(new StreamFile(stdin, false)),
658 m_output_file_sp(new StreamFile(stdout, false)),
659 m_error_file_sp(new StreamFile(stderr, false)),
660 m_terminal_state(),
661 m_target_list(*this),
662 m_platform_list(),
663 m_listener("lldb.Debugger"),
664 m_source_manager_ap(),
665 m_source_file_cache(),
666 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
667 m_input_reader_stack(),
668 m_instance_name(),
Greg Claytonafa91e332014-12-01 22:41:27 +0000669 m_loaded_plugins(),
670 m_event_handler_thread (),
671 m_io_handler_thread (),
672 m_sync_broadcaster (NULL, "lldb.debugger.sync")
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673{
Greg Clayton67cc0632012-08-22 17:17:09 +0000674 char instance_cstr[256];
675 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
676 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000677 if (log_callback)
678 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000679 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000680 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000681 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000682 assert (default_platform_sp.get());
683 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000684
Greg Clayton754a9362012-08-23 00:22:02 +0000685 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000686 m_collection_sp->AppendProperty (ConstString("target"),
687 ConstString("Settings specify to debugging targets."),
688 true,
689 Target::GetGlobalProperties()->GetValueProperties());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000690 m_collection_sp->AppendProperty (ConstString("platform"),
691 ConstString("Platform settings."),
692 true,
693 Platform::GetGlobalPlatformProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000694 if (m_command_interpreter_ap.get())
695 {
696 m_collection_sp->AppendProperty (ConstString("interpreter"),
697 ConstString("Settings specify to the debugger's command interpreter."),
698 true,
699 m_command_interpreter_ap->GetValueProperties());
700 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000701 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
702 term_width->SetMinimumValue(10);
703 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000704
705 // Turn off use-color if this is a dumb terminal.
706 const char *term = getenv ("TERM");
707 if (term && !strcmp (term, "dumb"))
708 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709}
710
711Debugger::~Debugger ()
712{
Jim Ingham8314c522011-09-15 21:36:42 +0000713 Clear();
714}
715
716void
717Debugger::Clear()
718{
Greg Clayton44d93782014-01-27 23:43:24 +0000719 ClearIOHandlers();
720 StopIOHandlerThread();
721 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000722 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000723 int num_targets = m_target_list.GetNumTargets();
724 for (int i = 0; i < num_targets; i++)
725 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000726 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
727 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000728 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000729 ProcessSP process_sp (target_sp->GetProcessSP());
730 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000731 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000732 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000733 }
Greg Clayton66111032010-06-23 01:19:29 +0000734 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000735 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000736
737 // Close the input file _before_ we close the input read communications class
738 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000739 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000740 if (m_input_file_sp)
741 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000742
743 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000744}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745
746bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000747Debugger::GetCloseInputOnEOF () const
748{
Greg Clayton44d93782014-01-27 23:43:24 +0000749// return m_input_comm.GetCloseOnEOF();
750 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000751}
752
753void
754Debugger::SetCloseInputOnEOF (bool b)
755{
Greg Clayton44d93782014-01-27 23:43:24 +0000756// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000757}
758
759bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760Debugger::GetAsyncExecution ()
761{
Greg Clayton66111032010-06-23 01:19:29 +0000762 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763}
764
765void
766Debugger::SetAsyncExecution (bool async_execution)
767{
Greg Clayton66111032010-06-23 01:19:29 +0000768 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769}
770
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771
772void
773Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
774{
Greg Clayton44d93782014-01-27 23:43:24 +0000775 if (m_input_file_sp)
776 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
777 else
778 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
779
780 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000781 if (in_file.IsValid() == false)
782 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783
Jim Inghamc5917d92012-11-30 20:23:19 +0000784 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
785 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786}
787
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788void
789Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
790{
Greg Clayton44d93782014-01-27 23:43:24 +0000791 if (m_output_file_sp)
792 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
793 else
794 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
795
796 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000797 if (out_file.IsValid() == false)
798 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000799
Enrico Granatab5887262012-10-29 21:18:03 +0000800 // do not create the ScriptInterpreter just for setting the output file handle
801 // as the constructor will know how to do the right thing on its own
802 const bool can_create = false;
803 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
804 if (script_interpreter)
805 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806}
807
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808void
809Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
810{
Greg Clayton44d93782014-01-27 23:43:24 +0000811 if (m_error_file_sp)
812 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
813 else
814 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
815
816 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000817 if (err_file.IsValid() == false)
818 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819}
820
Jim Inghamc5917d92012-11-30 20:23:19 +0000821void
822Debugger::SaveInputTerminalState ()
823{
Greg Clayton44d93782014-01-27 23:43:24 +0000824 if (m_input_file_sp)
825 {
826 File &in_file = m_input_file_sp->GetFile();
827 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
828 m_terminal_state.Save(in_file.GetDescriptor(), true);
829 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000830}
831
832void
833Debugger::RestoreInputTerminalState ()
834{
835 m_terminal_state.Restore();
836}
837
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000839Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840{
841 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000842 TargetSP target_sp(GetSelectedTarget());
843 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844
845 if (target_sp)
846 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000847 ProcessSP process_sp (target_sp->GetProcessSP());
848 exe_ctx.SetProcessSP (process_sp);
849 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000851 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
852 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000854 exe_ctx.SetThreadSP (thread_sp);
855 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
856 if (exe_ctx.GetFramePtr() == NULL)
857 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858 }
859 }
860 }
861 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862}
863
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864void
Caroline Ticeefed6132010-11-19 20:47:54 +0000865Debugger::DispatchInputInterrupt ()
866{
Greg Clayton44d93782014-01-27 23:43:24 +0000867 Mutex::Locker locker (m_input_reader_stack.GetMutex());
868 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000869 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000870 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000871}
872
873void
874Debugger::DispatchInputEndOfFile ()
875{
Greg Clayton44d93782014-01-27 23:43:24 +0000876 Mutex::Locker locker (m_input_reader_stack.GetMutex());
877 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000878 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000879 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000880}
881
882void
Greg Clayton44d93782014-01-27 23:43:24 +0000883Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000884{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000885 // 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 +0000886 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000887 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000888 {
Greg Clayton44d93782014-01-27 23:43:24 +0000889 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000890 if (reader_sp)
Pavel Labath44464872015-05-27 12:40:32 +0000891 PopIOHandler (reader_sp);
Caroline Tice3d6086f2010-12-20 18:35:50 +0000892 }
893}
894
895void
Siva Chandra9aaab552015-02-26 19:26:36 +0000896Debugger::ExecuteIOHandlers()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000897{
Greg Clayton44d93782014-01-27 23:43:24 +0000898 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 {
Greg Clayton44d93782014-01-27 23:43:24 +0000900 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 if (!reader_sp)
902 break;
903
Greg Clayton44d93782014-01-27 23:43:24 +0000904 reader_sp->Run();
Greg Clayton44d93782014-01-27 23:43:24 +0000905
906 // Remove all input readers that are done from the top of the stack
907 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 {
Greg Clayton44d93782014-01-27 23:43:24 +0000909 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
910 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +0000911 PopIOHandler (top_reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000912 else
913 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 }
915 }
Greg Clayton44d93782014-01-27 23:43:24 +0000916 ClearIOHandlers();
917}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918
Greg Clayton44d93782014-01-27 23:43:24 +0000919bool
920Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
921{
922 return m_input_reader_stack.IsTop (reader_sp);
923}
924
Pavel Labath44464872015-05-27 12:40:32 +0000925void
926Debugger::PrintAsync (const char *s, size_t len, bool is_stdout)
927{
928 lldb::StreamFileSP stream = is_stdout ? GetOutputFile() : GetErrorFile();
929 m_input_reader_stack.PrintAsync(stream.get(), s, len);
930}
Greg Clayton44d93782014-01-27 23:43:24 +0000931
932ConstString
933Debugger::GetTopIOHandlerControlSequence(char ch)
934{
935 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936}
937
Kate Stonea487aa42015-01-15 00:52:41 +0000938const char *
939Debugger::GetIOHandlerCommandPrefix()
940{
941 return m_input_reader_stack.GetTopIOHandlerCommandPrefix();
942}
943
944const char *
945Debugger::GetIOHandlerHelpPrologue()
946{
947 return m_input_reader_stack.GetTopIOHandlerHelpPrologue();
948}
949
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950void
Greg Clayton44d93782014-01-27 23:43:24 +0000951Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
952{
Greg Clayton44d93782014-01-27 23:43:24 +0000953 PushIOHandler (reader_sp);
Pavel Labath44464872015-05-27 12:40:32 +0000954
Greg Clayton577508d2014-06-20 00:23:57 +0000955 IOHandlerSP top_reader_sp = reader_sp;
956 while (top_reader_sp)
957 {
Greg Clayton577508d2014-06-20 00:23:57 +0000958 top_reader_sp->Run();
Pavel Labath44464872015-05-27 12:40:32 +0000959
Greg Clayton577508d2014-06-20 00:23:57 +0000960 if (top_reader_sp.get() == reader_sp.get())
961 {
962 if (PopIOHandler (reader_sp))
963 break;
964 }
Pavel Labath44464872015-05-27 12:40:32 +0000965
Greg Clayton577508d2014-06-20 00:23:57 +0000966 while (1)
967 {
968 top_reader_sp = m_input_reader_stack.Top();
969 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +0000970 PopIOHandler (top_reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +0000971 else
972 break;
973 }
974 }
Greg Clayton44d93782014-01-27 23:43:24 +0000975}
976
977void
978Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
979{
980 // Before an IOHandler runs, it must have in/out/err streams.
981 // This function is called when one ore more of the streams
982 // are NULL. We use the top input reader's in/out/err streams,
983 // or fall back to the debugger file handles, or we fall back
984 // onto stdin/stdout/stderr as a last resort.
985
986 Mutex::Locker locker (m_input_reader_stack.GetMutex());
987 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
988 // If no STDIN has been set, then set it appropriately
989 if (!in)
990 {
991 if (top_reader_sp)
992 in = top_reader_sp->GetInputStreamFile();
993 else
994 in = GetInputFile();
995
996 // If there is nothing, use stdin
997 if (!in)
998 in = StreamFileSP(new StreamFile(stdin, false));
999 }
1000 // If no STDOUT has been set, then set it appropriately
1001 if (!out)
1002 {
1003 if (top_reader_sp)
1004 out = top_reader_sp->GetOutputStreamFile();
1005 else
1006 out = GetOutputFile();
1007
1008 // If there is nothing, use stdout
1009 if (!out)
1010 out = StreamFileSP(new StreamFile(stdout, false));
1011 }
1012 // If no STDERR has been set, then set it appropriately
1013 if (!err)
1014 {
1015 if (top_reader_sp)
1016 err = top_reader_sp->GetErrorStreamFile();
1017 else
1018 err = GetErrorFile();
1019
1020 // If there is nothing, use stderr
1021 if (!err)
1022 err = StreamFileSP(new StreamFile(stdout, false));
1023
1024 }
1025}
1026
1027void
1028Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029{
1030 if (!reader_sp)
1031 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +00001032
Pavel Labath44464872015-05-27 12:40:32 +00001033 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1034
1035 // Get the current top input reader...
Greg Clayton44d93782014-01-27 23:43:24 +00001036 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001037
Greg Claytonb4874f12014-02-28 18:22:24 +00001038 // Don't push the same IO handler twice...
Pavel Labath44464872015-05-27 12:40:32 +00001039 if (reader_sp == top_reader_sp)
1040 return;
Greg Clayton44d93782014-01-27 23:43:24 +00001041
Pavel Labath44464872015-05-27 12:40:32 +00001042 // Push our new input reader
1043 m_input_reader_stack.Push (reader_sp);
1044 reader_sp->Activate();
1045
1046 // Interrupt the top input reader to it will exit its Run() function
1047 // and let this new input reader take over
1048 if (top_reader_sp)
1049 {
1050 top_reader_sp->Deactivate();
1051 top_reader_sp->Cancel();
Greg Claytonb4874f12014-02-28 18:22:24 +00001052 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053}
1054
1055bool
Greg Clayton44d93782014-01-27 23:43:24 +00001056Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057{
Pavel Labath44464872015-05-27 12:40:32 +00001058 if (! pop_reader_sp)
1059 return false;
1060
Greg Clayton44d93782014-01-27 23:43:24 +00001061 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062
1063 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001064 // read on the stack refresh its prompt and if there is one...
Pavel Labath44464872015-05-27 12:40:32 +00001065 if (m_input_reader_stack.IsEmpty())
1066 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067
Greg Clayton44d93782014-01-27 23:43:24 +00001068 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Greg Clayton66111032010-06-23 01:19:29 +00001069
Pavel Labath44464872015-05-27 12:40:32 +00001070 if (pop_reader_sp != reader_sp)
1071 return false;
1072
1073 reader_sp->Deactivate();
1074 reader_sp->Cancel();
1075 m_input_reader_stack.Pop ();
1076
1077 reader_sp = m_input_reader_stack.Top();
1078 if (reader_sp)
1079 reader_sp->Activate();
1080
1081 return true;
1082}
Greg Clayton44d93782014-01-27 23:43:24 +00001083
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001084StreamSP
1085Debugger::GetAsyncOutputStream ()
1086{
Pavel Labath44464872015-05-27 12:40:32 +00001087 return StreamSP (new StreamAsynchronousIO (*this, true));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001088}
1089
1090StreamSP
1091Debugger::GetAsyncErrorStream ()
1092{
Pavel Labath44464872015-05-27 12:40:32 +00001093 return StreamSP (new StreamAsynchronousIO (*this, false));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001094}
1095
Greg Claytonc7bece562013-01-25 18:06:21 +00001096size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001097Debugger::GetNumDebuggers()
1098{
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001099 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001100 {
1101 Mutex::Locker locker (GetDebuggerListMutex ());
1102 return GetDebuggerList().size();
1103 }
1104 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001105}
1106
1107lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001108Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001109{
1110 DebuggerSP debugger_sp;
1111
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001112 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001113 {
1114 Mutex::Locker locker (GetDebuggerListMutex ());
1115 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001116
Greg Claytonc15f55e2012-03-30 20:53:46 +00001117 if (index < debugger_list.size())
1118 debugger_sp = debugger_list[index];
1119 }
1120
Enrico Granata061858c2012-02-15 02:34:21 +00001121 return debugger_sp;
1122}
1123
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001124DebuggerSP
1125Debugger::FindDebuggerWithID (lldb::user_id_t id)
1126{
Greg Clayton4d122c42011-09-17 08:33:22 +00001127 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001128
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001129 if (lldb_initialized)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001130 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001131 Mutex::Locker locker (GetDebuggerListMutex ());
1132 DebuggerList &debugger_list = GetDebuggerList();
1133 DebuggerList::iterator pos, end = debugger_list.end();
1134 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001135 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001136 if ((*pos).get()->GetID() == id)
1137 {
1138 debugger_sp = *pos;
1139 break;
1140 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001141 }
1142 }
1143 return debugger_sp;
1144}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001145
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001146#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001147static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001148TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001149{
1150 if (frame == NULL)
1151 return;
1152
1153 StreamString s;
1154 const char *prompt_format =
1155 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001156 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1157 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001158 "{process.id = '${process.id}'\n}"
1159 "{process.name = '${process.name}'\n}"
1160 "{process.file.basename = '${process.file.basename}'\n}"
1161 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1162 "{thread.id = '${thread.id}'\n}"
1163 "{thread.index = '${thread.index}'\n}"
1164 "{thread.name = '${thread.name}'\n}"
1165 "{thread.queue = '${thread.queue}'\n}"
1166 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1167 "{target.arch = '${target.arch}'\n}"
1168 "{module.file.basename = '${module.file.basename}'\n}"
1169 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1170 "{file.basename = '${file.basename}'\n}"
1171 "{file.fullpath = '${file.fullpath}'\n}"
1172 "{frame.index = '${frame.index}'\n}"
1173 "{frame.pc = '${frame.pc}'\n}"
1174 "{frame.sp = '${frame.sp}'\n}"
1175 "{frame.fp = '${frame.fp}'\n}"
1176 "{frame.flags = '${frame.flags}'\n}"
1177 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1178 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1179 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1180 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1181 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1182 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1183 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1184 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001185 "{function.changed = '${function.changed}'\n}"
1186 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001187 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001188 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001189 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001190 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001191 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001192 "{function.line-offset = '${function.line-offset}'\n}"
1193 "{function.pc-offset = '${function.pc-offset}'\n}"
1194 "{line.file.basename = '${line.file.basename}'\n}"
1195 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1196 "{line.number = '${line.number}'\n}"
1197 "{line.start-addr = '${line.start-addr}'\n}"
1198 "{line.end-addr = '${line.end-addr}'\n}"
1199;
1200
1201 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1202 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001203 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001204 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001205 {
1206 printf("%s\n", s.GetData());
1207 }
1208 else
1209 {
Greg Clayton1b654882010-09-19 02:33:57 +00001210 printf ("what we got: %s\n", s.GetData());
1211 }
1212}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001213#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001214
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001215bool
Greg Clayton554f68d2015-02-04 22:00:53 +00001216Debugger::FormatDisassemblerAddress (const FormatEntity::Entry *format,
Jason Molendaaff1b352014-10-10 23:07:36 +00001217 const SymbolContext *sc,
1218 const SymbolContext *prev_sc,
1219 const ExecutionContext *exe_ctx,
1220 const Address *addr,
1221 Stream &s)
1222{
Greg Clayton554f68d2015-02-04 22:00:53 +00001223 FormatEntity::Entry format_entry;
1224
1225 if (format == NULL)
Jason Molendaaff1b352014-10-10 23:07:36 +00001226 {
Greg Clayton554f68d2015-02-04 22:00:53 +00001227 if (exe_ctx != NULL && exe_ctx->HasTargetScope())
1228 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1229 if (format == NULL)
1230 {
1231 FormatEntity::Parse("${addr}: ", format_entry);
1232 format = &format_entry;
1233 }
Jason Molendaaff1b352014-10-10 23:07:36 +00001234 }
1235 bool function_changed = false;
1236 bool initial_function = false;
1237 if (prev_sc && (prev_sc->function || prev_sc->symbol))
1238 {
1239 if (sc && (sc->function || sc->symbol))
1240 {
1241 if (prev_sc->symbol && sc->symbol)
1242 {
1243 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
1244 {
1245 function_changed = true;
1246 }
1247 }
1248 else if (prev_sc->function && sc->function)
1249 {
1250 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
1251 {
1252 function_changed = true;
1253 }
1254 }
1255 }
1256 }
1257 // The first context on a list of instructions will have a prev_sc that
1258 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
1259 // would return false. But we do get a prev_sc pointer.
1260 if ((sc && (sc->function || sc->symbol))
1261 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
1262 {
1263 initial_function = true;
1264 }
Greg Clayton554f68d2015-02-04 22:00:53 +00001265 return FormatEntity::Format(*format, s, sc, exe_ctx, addr, NULL, function_changed, initial_function);
Jason Molendaaff1b352014-10-10 23:07:36 +00001266}
1267
1268
Jim Ingham228063c2012-02-21 02:23:08 +00001269void
1270Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1271{
Jim Ingham4f02b222012-02-22 22:49:20 +00001272 // For simplicity's sake, I am not going to deal with how to close down any
1273 // open logging streams, I just redirect everything from here on out to the
1274 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00001275 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
1276}
1277
1278bool
1279Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
1280{
Jim Ingham228063c2012-02-21 02:23:08 +00001281 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001282 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001283 {
1284 log_stream_sp = m_log_callback_stream_sp;
1285 // For now when using the callback mode you always get thread & timestamp.
1286 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1287 }
1288 else if (log_file == NULL || *log_file == '\0')
1289 {
Greg Clayton44d93782014-01-27 23:43:24 +00001290 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00001291 }
1292 else
1293 {
1294 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001295 if (pos != m_log_streams.end())
1296 log_stream_sp = pos->second.lock();
1297 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001298 {
Pavel Labath8ac06992015-03-20 09:43:20 +00001299 uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate
1300 | File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
1301 if (! (log_options & LLDB_LOG_OPTION_APPEND))
1302 options |= File::eOpenOptionTruncate;
1303
1304 log_stream_sp.reset (new StreamFile (log_file, options));
Jim Ingham228063c2012-02-21 02:23:08 +00001305 m_log_streams[log_file] = log_stream_sp;
1306 }
Jim Ingham228063c2012-02-21 02:23:08 +00001307 }
1308 assert (log_stream_sp.get());
1309
1310 if (log_options == 0)
1311 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
1312
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +00001313 return Log::EnableLogChannel(log_stream_sp, log_options, channel, categories, error_stream);
Jim Ingham228063c2012-02-21 02:23:08 +00001314}
1315
Greg Clayton9585fbf2013-03-19 00:20:55 +00001316SourceManager &
1317Debugger::GetSourceManager ()
1318{
1319 if (m_source_manager_ap.get() == NULL)
1320 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
1321 return *m_source_manager_ap;
1322}
1323
1324
Greg Clayton44d93782014-01-27 23:43:24 +00001325
1326// This function handles events that were broadcast by the process.
1327void
1328Debugger::HandleBreakpointEvent (const EventSP &event_sp)
1329{
1330 using namespace lldb;
1331 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
1332
1333// if (event_type & eBreakpointEventTypeAdded
1334// || event_type & eBreakpointEventTypeRemoved
1335// || event_type & eBreakpointEventTypeEnabled
1336// || event_type & eBreakpointEventTypeDisabled
1337// || event_type & eBreakpointEventTypeCommandChanged
1338// || event_type & eBreakpointEventTypeConditionChanged
1339// || event_type & eBreakpointEventTypeIgnoreChanged
1340// || event_type & eBreakpointEventTypeLocationsResolved)
1341// {
1342// // Don't do anything about these events, since the breakpoint commands already echo these actions.
1343// }
1344//
1345 if (event_type & eBreakpointEventTypeLocationsAdded)
1346 {
1347 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
1348 if (num_new_locations > 0)
1349 {
1350 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001351 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001352 if (output_sp)
1353 {
1354 output_sp->Printf("%d location%s added to breakpoint %d\n",
1355 num_new_locations,
1356 num_new_locations == 1 ? "" : "s",
1357 breakpoint->GetID());
Pavel Labath44464872015-05-27 12:40:32 +00001358 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001359 }
1360 }
1361 }
1362// else if (event_type & eBreakpointEventTypeLocationsRemoved)
1363// {
1364// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
1365// }
1366// else if (event_type & eBreakpointEventTypeLocationsResolved)
1367// {
1368// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
1369// }
1370}
1371
1372size_t
1373Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
1374{
1375 size_t total_bytes = 0;
1376 if (stream == NULL)
1377 stream = GetOutputFile().get();
1378
1379 if (stream)
1380 {
1381 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
1382 if (process == NULL)
1383 {
1384 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1385 if (target_sp)
1386 process = target_sp->GetProcessSP().get();
1387 }
1388 if (process)
1389 {
1390 Error error;
1391 size_t len;
1392 char stdio_buffer[1024];
1393 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1394 {
1395 stream->Write(stdio_buffer, len);
1396 total_bytes += len;
1397 }
1398 }
1399 stream->Flush();
1400 }
1401 return total_bytes;
1402}
1403
1404size_t
1405Debugger::GetProcessSTDERR (Process *process, Stream *stream)
1406{
1407 size_t total_bytes = 0;
1408 if (stream == NULL)
1409 stream = GetOutputFile().get();
1410
1411 if (stream)
1412 {
1413 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
1414 if (process == NULL)
1415 {
1416 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1417 if (target_sp)
1418 process = target_sp->GetProcessSP().get();
1419 }
1420 if (process)
1421 {
1422 Error error;
1423 size_t len;
1424 char stdio_buffer[1024];
1425 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1426 {
1427 stream->Write(stdio_buffer, len);
1428 total_bytes += len;
1429 }
1430 }
1431 stream->Flush();
1432 }
1433 return total_bytes;
1434}
1435
Greg Claytondc6224e2014-10-21 01:00:42 +00001436
Greg Clayton44d93782014-01-27 23:43:24 +00001437// This function handles events that were broadcast by the process.
1438void
1439Debugger::HandleProcessEvent (const EventSP &event_sp)
1440{
1441 using namespace lldb;
1442 const uint32_t event_type = event_sp->GetType();
1443 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00001444
Pavel Labath44464872015-05-27 12:40:32 +00001445 StreamSP output_stream_sp = GetAsyncOutputStream();
1446 StreamSP error_stream_sp = GetAsyncErrorStream();
Greg Clayton44d93782014-01-27 23:43:24 +00001447 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00001448
Greg Claytonb4874f12014-02-28 18:22:24 +00001449 if (!gui_enabled)
1450 {
1451 bool pop_process_io_handler = false;
1452 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00001453
Pavel Labath44464872015-05-27 12:40:32 +00001454 bool state_is_stopped = false;
1455 const bool got_state_changed = (event_type & Process::eBroadcastBitStateChanged) != 0;
1456 const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
1457 const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
1458 if (got_state_changed)
Greg Clayton44d93782014-01-27 23:43:24 +00001459 {
Pavel Labath44464872015-05-27 12:40:32 +00001460 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
1461 state_is_stopped = StateIsStoppedState(event_state, false);
Greg Claytonb4874f12014-02-28 18:22:24 +00001462 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001463
Pavel Labath44464872015-05-27 12:40:32 +00001464 // Display running state changes first before any STDIO
1465 if (got_state_changed && !state_is_stopped)
Greg Claytonb4874f12014-02-28 18:22:24 +00001466 {
Pavel Labath44464872015-05-27 12:40:32 +00001467 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
Greg Claytonb4874f12014-02-28 18:22:24 +00001468 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001469
Pavel Labath44464872015-05-27 12:40:32 +00001470 // Now display and STDOUT
1471 if (got_stdout || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001472 {
Pavel Labath44464872015-05-27 12:40:32 +00001473 GetProcessSTDOUT (process_sp.get(), output_stream_sp.get());
Greg Clayton44d93782014-01-27 23:43:24 +00001474 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001475
Pavel Labath44464872015-05-27 12:40:32 +00001476 // Now display and STDERR
1477 if (got_stderr || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001478 {
Pavel Labath44464872015-05-27 12:40:32 +00001479 GetProcessSTDERR (process_sp.get(), error_stream_sp.get());
Greg Claytonb4874f12014-02-28 18:22:24 +00001480 }
1481
Pavel Labath44464872015-05-27 12:40:32 +00001482 // Now display any stopped state changes after any STDIO
1483 if (got_state_changed && state_is_stopped)
1484 {
1485 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
1486 }
1487
1488 output_stream_sp->Flush();
1489 error_stream_sp->Flush();
1490
Greg Claytonb4874f12014-02-28 18:22:24 +00001491 if (pop_process_io_handler)
1492 process_sp->PopProcessIOHandler();
1493 }
Greg Clayton44d93782014-01-27 23:43:24 +00001494}
1495
1496void
1497Debugger::HandleThreadEvent (const EventSP &event_sp)
1498{
1499 // At present the only thread event we handle is the Frame Changed event,
1500 // and all we do for that is just reprint the thread status for that thread.
1501 using namespace lldb;
1502 const uint32_t event_type = event_sp->GetType();
1503 if (event_type == Thread::eBroadcastBitStackChanged ||
1504 event_type == Thread::eBroadcastBitThreadSelected )
1505 {
1506 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
1507 if (thread_sp)
1508 {
Pavel Labath44464872015-05-27 12:40:32 +00001509 thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00001510 }
1511 }
1512}
1513
1514bool
1515Debugger::IsForwardingEvents ()
1516{
1517 return (bool)m_forward_listener_sp;
1518}
1519
1520void
1521Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
1522{
1523 m_forward_listener_sp = listener_sp;
1524}
1525
1526void
1527Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
1528{
1529 m_forward_listener_sp.reset();
1530}
1531
1532
1533void
1534Debugger::DefaultEventHandler()
1535{
1536 Listener& listener(GetListener());
1537 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
1538 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
1539 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
1540 BroadcastEventSpec target_event_spec (broadcaster_class_target,
1541 Target::eBroadcastBitBreakpointChanged);
1542
1543 BroadcastEventSpec process_event_spec (broadcaster_class_process,
1544 Process::eBroadcastBitStateChanged |
1545 Process::eBroadcastBitSTDOUT |
1546 Process::eBroadcastBitSTDERR);
1547
1548 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
1549 Thread::eBroadcastBitStackChanged |
1550 Thread::eBroadcastBitThreadSelected );
1551
1552 listener.StartListeningForEventSpec (*this, target_event_spec);
1553 listener.StartListeningForEventSpec (*this, process_event_spec);
1554 listener.StartListeningForEventSpec (*this, thread_event_spec);
1555 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
1556 CommandInterpreter::eBroadcastBitQuitCommandReceived |
1557 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
1558 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
Greg Claytonafa91e332014-12-01 22:41:27 +00001559
1560 // Let the thread that spawned us know that we have started up and
1561 // that we are now listening to all required events so no events get missed
1562 m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
1563
Greg Clayton44d93782014-01-27 23:43:24 +00001564 bool done = false;
1565 while (!done)
1566 {
Greg Clayton44d93782014-01-27 23:43:24 +00001567 EventSP event_sp;
1568 if (listener.WaitForEvent(NULL, event_sp))
1569 {
1570 if (event_sp)
1571 {
1572 Broadcaster *broadcaster = event_sp->GetBroadcaster();
1573 if (broadcaster)
1574 {
1575 uint32_t event_type = event_sp->GetType();
1576 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
1577 if (broadcaster_class == broadcaster_class_process)
1578 {
1579 HandleProcessEvent (event_sp);
1580 }
1581 else if (broadcaster_class == broadcaster_class_target)
1582 {
1583 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
1584 {
1585 HandleBreakpointEvent (event_sp);
1586 }
1587 }
1588 else if (broadcaster_class == broadcaster_class_thread)
1589 {
1590 HandleThreadEvent (event_sp);
1591 }
1592 else if (broadcaster == m_command_interpreter_ap.get())
1593 {
1594 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
1595 {
1596 done = true;
1597 }
1598 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
1599 {
1600 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1601 if (data && data[0])
1602 {
Pavel Labath44464872015-05-27 12:40:32 +00001603 StreamSP error_sp (GetAsyncErrorStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001604 if (error_sp)
1605 {
Greg Clayton44d93782014-01-27 23:43:24 +00001606 error_sp->PutCString(data);
1607 error_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001608 }
1609 }
1610 }
1611 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
1612 {
1613 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1614 if (data && data[0])
1615 {
Pavel Labath44464872015-05-27 12:40:32 +00001616 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001617 if (output_sp)
1618 {
Greg Clayton44d93782014-01-27 23:43:24 +00001619 output_sp->PutCString(data);
1620 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001621 }
1622 }
1623 }
1624 }
1625 }
1626
1627 if (m_forward_listener_sp)
1628 m_forward_listener_sp->AddEvent(event_sp);
1629 }
1630 }
1631 }
1632}
1633
1634lldb::thread_result_t
1635Debugger::EventHandlerThread (lldb::thread_arg_t arg)
1636{
1637 ((Debugger *)arg)->DefaultEventHandler();
1638 return NULL;
1639}
1640
1641bool
1642Debugger::StartEventHandlerThread()
1643{
Zachary Turneracee96a2014-09-23 18:32:09 +00001644 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001645 {
Greg Claytonafa91e332014-12-01 22:41:27 +00001646 // We must synchronize with the DefaultEventHandler() thread to ensure
1647 // it is up and running and listening to events before we return from
1648 // this function. We do this by listening to events for the
1649 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
1650 Listener listener("lldb.debugger.event-handler");
1651 listener.StartListeningForEvents(&m_sync_broadcaster, eBroadcastBitEventThreadIsListening);
1652
Zachary Turner7c2896a2014-10-24 22:06:29 +00001653 // Use larger 8MB stack for this thread
Greg Claytonafa91e332014-12-01 22:41:27 +00001654 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread,
1655 this,
1656 NULL,
Zachary Turner7c2896a2014-10-24 22:06:29 +00001657 g_debugger_event_thread_stack_bytes);
Greg Claytonafa91e332014-12-01 22:41:27 +00001658
1659 // Make sure DefaultEventHandler() is running and listening to events before we return
1660 // from this function. We are only listening for events of type
1661 // eBroadcastBitEventThreadIsListening so we don't need to check the event, we just need
1662 // to wait an infinite amount of time for it (NULL timeout as the first parameter)
1663 lldb::EventSP event_sp;
1664 listener.WaitForEvent(NULL, event_sp);
Greg Clayton807b6b32014-10-15 18:03:59 +00001665 }
Zachary Turneracee96a2014-09-23 18:32:09 +00001666 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001667}
1668
1669void
1670Debugger::StopEventHandlerThread()
1671{
Zachary Turneracee96a2014-09-23 18:32:09 +00001672 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001673 {
1674 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00001675 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001676 }
1677}
1678
1679
1680lldb::thread_result_t
1681Debugger::IOHandlerThread (lldb::thread_arg_t arg)
1682{
1683 Debugger *debugger = (Debugger *)arg;
Siva Chandra9aaab552015-02-26 19:26:36 +00001684 debugger->ExecuteIOHandlers();
Greg Clayton44d93782014-01-27 23:43:24 +00001685 debugger->StopEventHandlerThread();
1686 return NULL;
1687}
1688
1689bool
1690Debugger::StartIOHandlerThread()
1691{
Zachary Turneracee96a2014-09-23 18:32:09 +00001692 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001693 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
1694 IOHandlerThread,
1695 this,
1696 NULL,
1697 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00001698 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001699}
1700
1701void
1702Debugger::StopIOHandlerThread()
1703{
Zachary Turneracee96a2014-09-23 18:32:09 +00001704 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001705 {
1706 if (m_input_file_sp)
1707 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00001708 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001709 }
1710}
1711
Jim Ingham893c9322014-11-22 01:42:44 +00001712Target *
1713Debugger::GetDummyTarget()
1714{
1715 return m_target_list.GetDummyTarget (*this).get();
1716}
1717
1718Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001719Debugger::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001720{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001721 Target *target = nullptr;
1722 if (!prefer_dummy)
1723 {
1724 target = m_target_list.GetSelectedTarget().get();
1725 if (target)
1726 return target;
1727 }
1728
Jim Ingham893c9322014-11-22 01:42:44 +00001729 return GetDummyTarget();
1730}
Greg Clayton44d93782014-01-27 23:43:24 +00001731