blob: 2eedb0f844a8977c31c653a7123b51c278d706ab [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
Caroline Ticeebc1bb22010-06-30 16:22:25 +000067static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000068static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000069
Greg Clayton1b654882010-09-19 02:33:57 +000070#pragma mark Static Functions
71
72static Mutex &
73GetDebuggerListMutex ()
74{
75 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
76 return g_mutex;
77}
78
79typedef std::vector<DebuggerSP> DebuggerList;
80
81static DebuggerList &
82GetDebuggerList()
83{
84 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000085 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000086 static DebuggerList g_list;
87 return g_list;
88}
Greg Claytone372b982011-11-21 21:44:34 +000089
90OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000091g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000092{
Greg Clayton67cc0632012-08-22 17:17:09 +000093 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
94 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
95 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000096 { 0, NULL, NULL }
97};
98
Greg Clayton67cc0632012-08-22 17:17:09 +000099OptionEnumValueElement
100g_language_enumerators[] =
101{
102 { eScriptLanguageNone, "none", "Disable scripting languages."},
103 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
104 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +0000105 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000106};
Greg Claytone372b982011-11-21 21:44:34 +0000107
Greg Clayton67cc0632012-08-22 17:17:09 +0000108#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
109#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
110
Michael Sartain0769b2b2013-07-30 16:44:36 +0000111#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000112 "{, ${frame.pc}}"\
113 MODULE_WITH_FUNC\
114 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000115 "{, name = '${thread.name}'}"\
116 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000117 "{, activity = '${thread.info.activity.name}'}" \
118 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000119 "{, stop reason = ${thread.stop-reason}}"\
120 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000121 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000122 "\\n"
123
124#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
125 MODULE_WITH_FUNC\
126 FILE_AND_LINE\
127 "\\n"
128
Jason Molendac980fa92015-02-13 23:24:21 +0000129// Three parts to this disassembly format specification:
130// 1. If this is a new function/symbol (no previous symbol/function), print
131// dylib`funcname:\n
132// 2. If this is a symbol context change (different from previous symbol/function), print
133// dylib`funcname:\n
134// 3. print
135// address <+offset>:
136#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}>}: "
137
138// gdb's disassembly format can be emulated with
139// ${current-pc-arrow}${addr-file-or-load}{ <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>}:
140
141// lldb's original format for disassembly would look like this format string -
142// {${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}}:
143
Greg Clayton67cc0632012-08-22 17:17:09 +0000144
Greg Clayton754a9362012-08-23 00:22:02 +0000145static PropertyDefinition
146g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000147{
Greg Clayton554f68d2015-02-04 22:00:53 +0000148{ "auto-confirm", OptionValue::eTypeBoolean , true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
149{ "disassembly-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." },
150{ "frame-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
151{ "notify-void", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
152{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
153{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
154{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
155{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
156{ "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." },
157{ "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." },
158{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
159{ "thread-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
160{ "use-external-editor", OptionValue::eTypeBoolean , true, false, NULL, NULL, "Whether to use an external editor or not." },
161{ "use-color", OptionValue::eTypeBoolean , true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
162{ "auto-one-line-summaries", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
163{ "escape-non-printables", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically escape non-printable and escape characters when formatting strings." },
164{ NULL, OptionValue::eTypeInvalid , true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000165};
166
167enum
168{
169 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000170 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000171 ePropertyFrameFormat,
172 ePropertyNotiftVoid,
173 ePropertyPrompt,
174 ePropertyScriptLanguage,
175 ePropertyStopDisassemblyCount,
176 ePropertyStopDisassemblyDisplay,
177 ePropertyStopLineCountAfter,
178 ePropertyStopLineCountBefore,
179 ePropertyTerminalWidth,
180 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000181 ePropertyUseExternalEditor,
182 ePropertyUseColor,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000183 ePropertyAutoOneLineSummaries,
184 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000185};
186
Zachary Turner3a006912015-03-19 22:00:21 +0000187LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000188
189Error
190Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
191 VarSetOperationType op,
192 const char *property_path,
193 const char *value)
194{
Enrico Granata84a53df2013-05-20 22:29:23 +0000195 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000196 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000197 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000198 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000199 if (is_load_script && exe_ctx->GetTargetSP())
200 {
201 target_sp = exe_ctx->GetTargetSP();
202 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
203 }
Greg Clayton4c054102012-09-01 00:38:36 +0000204 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
205 if (error.Success())
206 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000207 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000208 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
209 {
210 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000211 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
212 if (str.length())
213 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000214 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000215 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
216 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
217 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000218 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
219 {
220 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
221 SetPrompt (GetPrompt());
222 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000223 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000224 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000225 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000226 {
227 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000228 StreamString feedback_stream;
229 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000230 {
Greg Clayton44d93782014-01-27 23:43:24 +0000231 StreamFileSP stream_sp (GetErrorFile());
232 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000233 {
Greg Clayton44d93782014-01-27 23:43:24 +0000234 for (auto error : errors)
235 {
236 stream_sp->Printf("%s\n",error.AsCString());
237 }
238 if (feedback_stream.GetSize())
239 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000240 }
241 }
242 }
243 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000244 else if (is_escape_non_printables)
245 {
246 DataVisualization::ForceUpdate();
247 }
Greg Clayton4c054102012-09-01 00:38:36 +0000248 }
249 return error;
250}
251
Greg Clayton67cc0632012-08-22 17:17:09 +0000252bool
253Debugger::GetAutoConfirm () const
254{
255 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000256 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000257}
258
Greg Clayton554f68d2015-02-04 22:00:53 +0000259const FormatEntity::Entry *
Jason Molendaaff1b352014-10-10 23:07:36 +0000260Debugger::GetDisassemblyFormat() const
261{
262 const uint32_t idx = ePropertyDisassemblyFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000263 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Jason Molendaaff1b352014-10-10 23:07:36 +0000264}
265
Greg Clayton554f68d2015-02-04 22:00:53 +0000266const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000267Debugger::GetFrameFormat() const
268{
269 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000270 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000271}
272
273bool
274Debugger::GetNotifyVoid () const
275{
276 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000277 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000278}
279
280const char *
281Debugger::GetPrompt() const
282{
283 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000284 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000285}
286
287void
288Debugger::SetPrompt(const char *p)
289{
290 const uint32_t idx = ePropertyPrompt;
291 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
292 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000293 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
294 if (str.length())
295 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000296 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000297}
298
Greg Clayton554f68d2015-02-04 22:00:53 +0000299const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000300Debugger::GetThreadFormat() const
301{
302 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000303 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000304}
305
306lldb::ScriptLanguage
307Debugger::GetScriptLanguage() const
308{
309 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000310 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000311}
312
313bool
314Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
315{
316 const uint32_t idx = ePropertyScriptLanguage;
317 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
318}
319
320uint32_t
321Debugger::GetTerminalWidth () const
322{
323 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000324 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000325}
326
327bool
328Debugger::SetTerminalWidth (uint32_t term_width)
329{
330 const uint32_t idx = ePropertyTerminalWidth;
331 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
332}
333
334bool
335Debugger::GetUseExternalEditor () const
336{
337 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000338 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000339}
340
341bool
342Debugger::SetUseExternalEditor (bool b)
343{
344 const uint32_t idx = ePropertyUseExternalEditor;
345 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
346}
347
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000348bool
349Debugger::GetUseColor () const
350{
351 const uint32_t idx = ePropertyUseColor;
352 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
353}
354
355bool
356Debugger::SetUseColor (bool b)
357{
358 const uint32_t idx = ePropertyUseColor;
359 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
360 SetPrompt (GetPrompt());
361 return ret;
362}
363
Greg Clayton67cc0632012-08-22 17:17:09 +0000364uint32_t
365Debugger::GetStopSourceLineCount (bool before) const
366{
367 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000368 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000369}
370
371Debugger::StopDisassemblyType
372Debugger::GetStopDisassemblyDisplay () const
373{
374 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000375 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000376}
377
378uint32_t
379Debugger::GetDisassemblyLineCount () const
380{
381 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000382 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000383}
Greg Claytone372b982011-11-21 21:44:34 +0000384
Enrico Granata553fad52013-10-25 23:09:40 +0000385bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000386Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000387{
Enrico Granata90a8db32013-10-31 21:01:07 +0000388 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000389 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000390}
Enrico Granata553fad52013-10-25 23:09:40 +0000391
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000392bool
393Debugger::GetEscapeNonPrintables () const
394{
395 const uint32_t idx = ePropertyEscapeNonPrintables;
396 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000397}
398
Greg Clayton1b654882010-09-19 02:33:57 +0000399#pragma mark Debugger
400
Greg Clayton67cc0632012-08-22 17:17:09 +0000401//const DebuggerPropertiesSP &
402//Debugger::GetSettings() const
403//{
404// return m_properties_sp;
405//}
406//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000407
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000408static bool lldb_initialized = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409void
Zachary Turner3a006912015-03-19 22:00:21 +0000410Debugger::Initialize(LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000412 assert(!lldb_initialized && "Debugger::Initialize called more than once!");
413
Robert Flackf196c932015-03-10 18:07:47 +0000414 lldb_initialized = true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000415 g_load_plugin_callback = load_plugin_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416}
417
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000418void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419Debugger::Terminate ()
420{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000421 assert(lldb_initialized && "Debugger::Terminate called without a matching Debugger::Initialize!");
422
423 // Clear our master list of debugger objects
424 Mutex::Locker locker (GetDebuggerListMutex ());
425 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426}
427
Caroline Tice20bd37f2011-03-10 22:14:10 +0000428void
429Debugger::SettingsInitialize ()
430{
Greg Clayton6920b522012-08-22 18:39:03 +0000431 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000432}
433
434void
435Debugger::SettingsTerminate ()
436{
Greg Clayton6920b522012-08-22 18:39:03 +0000437 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000438}
439
Enrico Granata21dfcd92012-09-28 23:57:51 +0000440bool
Enrico Granatae743c782013-04-24 21:29:08 +0000441Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000442{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000443 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000444 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000445 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
446 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000447 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000448 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000449 return true;
450 }
Enrico Granatae743c782013-04-24 21:29:08 +0000451 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000452 else
Enrico Granatae743c782013-04-24 21:29:08 +0000453 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000454 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
455 // and if the public API layer isn't available (code is linking against
456 // all of the internal LLDB static libraries), then we can't load plugins
457 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000458 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000459 return false;
460}
461
462static FileSpec::EnumerateDirectoryResult
463LoadPluginCallback
464(
465 void *baton,
466 FileSpec::FileType file_type,
467 const FileSpec &file_spec
468 )
469{
470 Error error;
471
472 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000473 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000474
475 if (!baton)
476 return FileSpec::eEnumerateDirectoryResultQuit;
477
478 Debugger *debugger = (Debugger*)baton;
479
480 // If we have a regular file, a symbolic link or unknown file type, try
481 // and process the file. We must handle unknown as sometimes the directory
482 // enumeration might be enumerating a file system that doesn't have correct
483 // file type information.
484 if (file_type == FileSpec::eFileTypeRegular ||
485 file_type == FileSpec::eFileTypeSymbolicLink ||
486 file_type == FileSpec::eFileTypeUnknown )
487 {
488 FileSpec plugin_file_spec (file_spec);
489 plugin_file_spec.ResolvePath ();
490
Michael Sartain3cf443d2013-07-17 00:26:30 +0000491 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
492 plugin_file_spec.GetFileNameExtension() != g_solibext)
493 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000494 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000495 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000496
Enrico Granatae743c782013-04-24 21:29:08 +0000497 Error plugin_load_error;
498 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000499
500 return FileSpec::eEnumerateDirectoryResultNext;
501 }
502
503 else if (file_type == FileSpec::eFileTypeUnknown ||
504 file_type == FileSpec::eFileTypeDirectory ||
505 file_type == FileSpec::eFileTypeSymbolicLink )
506 {
507 // Try and recurse into anything that a directory or symbolic link.
508 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000509 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000510 // information.
511 return FileSpec::eEnumerateDirectoryResultEnter;
512 }
513
514 return FileSpec::eEnumerateDirectoryResultNext;
515}
516
517void
518Debugger::InstanceInitialize ()
519{
520 FileSpec dir_spec;
521 const bool find_directories = true;
522 const bool find_files = true;
523 const bool find_other = true;
524 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000525 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000526 {
527 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
528 {
529 FileSpec::EnumerateDirectory (dir_path,
530 find_directories,
531 find_files,
532 find_other,
533 LoadPluginCallback,
534 this);
535 }
536 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000537
538 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000539 {
540 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
541 {
542 FileSpec::EnumerateDirectory (dir_path,
543 find_directories,
544 find_files,
545 find_other,
546 LoadPluginCallback,
547 this);
548 }
549 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000550
551 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000552}
553
Greg Clayton66111032010-06-23 01:19:29 +0000554DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000555Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000556{
Jim Ingham228063c2012-02-21 02:23:08 +0000557 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000558 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000559 {
560 Mutex::Locker locker (GetDebuggerListMutex ());
561 GetDebuggerList().push_back(debugger_sp);
562 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000563 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000564 return debugger_sp;
565}
566
Caroline Ticee02657b2011-01-22 01:02:07 +0000567void
Greg Clayton4d122c42011-09-17 08:33:22 +0000568Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000569{
570 if (debugger_sp.get() == NULL)
571 return;
572
Jim Ingham8314c522011-09-15 21:36:42 +0000573 debugger_sp->Clear();
574
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000575 if (lldb_initialized)
Caroline Ticee02657b2011-01-22 01:02:07 +0000576 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000577 Mutex::Locker locker (GetDebuggerListMutex ());
578 DebuggerList &debugger_list = GetDebuggerList ();
579 DebuggerList::iterator pos, end = debugger_list.end();
580 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000581 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000582 if ((*pos).get() == debugger_sp.get())
583 {
584 debugger_list.erase (pos);
585 return;
586 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000587 }
588 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000589}
590
Greg Clayton4d122c42011-09-17 08:33:22 +0000591DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000592Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
593{
Greg Clayton4d122c42011-09-17 08:33:22 +0000594 DebuggerSP debugger_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000595 if (lldb_initialized)
Greg Clayton6920b522012-08-22 18:39:03 +0000596 {
597 Mutex::Locker locker (GetDebuggerListMutex ());
598 DebuggerList &debugger_list = GetDebuggerList();
599 DebuggerList::iterator pos, end = debugger_list.end();
600
601 for (pos = debugger_list.begin(); pos != end; ++pos)
602 {
603 if ((*pos).get()->m_instance_name == instance_name)
604 {
605 debugger_sp = *pos;
606 break;
607 }
608 }
609 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000610 return debugger_sp;
611}
Greg Clayton66111032010-06-23 01:19:29 +0000612
613TargetSP
614Debugger::FindTargetWithProcessID (lldb::pid_t pid)
615{
Greg Clayton4d122c42011-09-17 08:33:22 +0000616 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000617 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000618 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000619 Mutex::Locker locker (GetDebuggerListMutex ());
620 DebuggerList &debugger_list = GetDebuggerList();
621 DebuggerList::iterator pos, end = debugger_list.end();
622 for (pos = debugger_list.begin(); pos != end; ++pos)
623 {
624 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
625 if (target_sp)
626 break;
627 }
Greg Clayton66111032010-06-23 01:19:29 +0000628 }
629 return target_sp;
630}
631
Greg Claytone4e45922011-11-16 05:37:56 +0000632TargetSP
633Debugger::FindTargetWithProcess (Process *process)
634{
635 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000636 if (lldb_initialized)
Greg Claytone4e45922011-11-16 05:37:56 +0000637 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000638 Mutex::Locker locker (GetDebuggerListMutex ());
639 DebuggerList &debugger_list = GetDebuggerList();
640 DebuggerList::iterator pos, end = debugger_list.end();
641 for (pos = debugger_list.begin(); pos != end; ++pos)
642 {
643 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
644 if (target_sp)
645 break;
646 }
Greg Claytone4e45922011-11-16 05:37:56 +0000647 }
648 return target_sp;
649}
650
Jason Molendae6481c72014-09-12 01:50:46 +0000651Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
652 UserID(g_unique_id++),
653 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
654 m_input_file_sp(new StreamFile(stdin, false)),
655 m_output_file_sp(new StreamFile(stdout, false)),
656 m_error_file_sp(new StreamFile(stderr, false)),
657 m_terminal_state(),
658 m_target_list(*this),
659 m_platform_list(),
660 m_listener("lldb.Debugger"),
661 m_source_manager_ap(),
662 m_source_file_cache(),
663 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
664 m_input_reader_stack(),
665 m_instance_name(),
Greg Claytonafa91e332014-12-01 22:41:27 +0000666 m_loaded_plugins(),
667 m_event_handler_thread (),
668 m_io_handler_thread (),
669 m_sync_broadcaster (NULL, "lldb.debugger.sync")
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670{
Greg Clayton67cc0632012-08-22 17:17:09 +0000671 char instance_cstr[256];
672 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
673 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000674 if (log_callback)
675 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000676 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000677 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000678 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000679 assert (default_platform_sp.get());
680 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000681
Greg Clayton754a9362012-08-23 00:22:02 +0000682 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000683 m_collection_sp->AppendProperty (ConstString("target"),
684 ConstString("Settings specify to debugging targets."),
685 true,
686 Target::GetGlobalProperties()->GetValueProperties());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000687 m_collection_sp->AppendProperty (ConstString("platform"),
688 ConstString("Platform settings."),
689 true,
690 Platform::GetGlobalPlatformProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000691 if (m_command_interpreter_ap.get())
692 {
693 m_collection_sp->AppendProperty (ConstString("interpreter"),
694 ConstString("Settings specify to the debugger's command interpreter."),
695 true,
696 m_command_interpreter_ap->GetValueProperties());
697 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000698 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
699 term_width->SetMinimumValue(10);
700 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000701
702 // Turn off use-color if this is a dumb terminal.
703 const char *term = getenv ("TERM");
704 if (term && !strcmp (term, "dumb"))
705 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706}
707
708Debugger::~Debugger ()
709{
Jim Ingham8314c522011-09-15 21:36:42 +0000710 Clear();
711}
712
713void
714Debugger::Clear()
715{
Greg Clayton44d93782014-01-27 23:43:24 +0000716 ClearIOHandlers();
717 StopIOHandlerThread();
718 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000719 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000720 int num_targets = m_target_list.GetNumTargets();
721 for (int i = 0; i < num_targets; i++)
722 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000723 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
724 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000725 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000726 ProcessSP process_sp (target_sp->GetProcessSP());
727 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000728 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000729 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000730 }
Greg Clayton66111032010-06-23 01:19:29 +0000731 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000732 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000733
734 // Close the input file _before_ we close the input read communications class
735 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000736 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000737 if (m_input_file_sp)
738 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000739
740 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000741}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742
743bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000744Debugger::GetCloseInputOnEOF () const
745{
Greg Clayton44d93782014-01-27 23:43:24 +0000746// return m_input_comm.GetCloseOnEOF();
747 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000748}
749
750void
751Debugger::SetCloseInputOnEOF (bool b)
752{
Greg Clayton44d93782014-01-27 23:43:24 +0000753// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000754}
755
756bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757Debugger::GetAsyncExecution ()
758{
Greg Clayton66111032010-06-23 01:19:29 +0000759 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760}
761
762void
763Debugger::SetAsyncExecution (bool async_execution)
764{
Greg Clayton66111032010-06-23 01:19:29 +0000765 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766}
767
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768
769void
770Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
771{
Greg Clayton44d93782014-01-27 23:43:24 +0000772 if (m_input_file_sp)
773 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
774 else
775 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
776
777 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000778 if (in_file.IsValid() == false)
779 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780
Jim Inghamc5917d92012-11-30 20:23:19 +0000781 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
782 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783}
784
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785void
786Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
787{
Greg Clayton44d93782014-01-27 23:43:24 +0000788 if (m_output_file_sp)
789 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
790 else
791 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
792
793 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000794 if (out_file.IsValid() == false)
795 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000796
Enrico Granatab5887262012-10-29 21:18:03 +0000797 // do not create the ScriptInterpreter just for setting the output file handle
798 // as the constructor will know how to do the right thing on its own
799 const bool can_create = false;
800 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
801 if (script_interpreter)
802 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803}
804
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805void
806Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
807{
Greg Clayton44d93782014-01-27 23:43:24 +0000808 if (m_error_file_sp)
809 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
810 else
811 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
812
813 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000814 if (err_file.IsValid() == false)
815 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816}
817
Jim Inghamc5917d92012-11-30 20:23:19 +0000818void
819Debugger::SaveInputTerminalState ()
820{
Greg Clayton44d93782014-01-27 23:43:24 +0000821 if (m_input_file_sp)
822 {
823 File &in_file = m_input_file_sp->GetFile();
824 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
825 m_terminal_state.Save(in_file.GetDescriptor(), true);
826 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000827}
828
829void
830Debugger::RestoreInputTerminalState ()
831{
832 m_terminal_state.Restore();
833}
834
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000836Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837{
838 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000839 TargetSP target_sp(GetSelectedTarget());
840 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841
842 if (target_sp)
843 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000844 ProcessSP process_sp (target_sp->GetProcessSP());
845 exe_ctx.SetProcessSP (process_sp);
846 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000848 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
849 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000851 exe_ctx.SetThreadSP (thread_sp);
852 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
853 if (exe_ctx.GetFramePtr() == NULL)
854 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 }
856 }
857 }
858 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859}
860
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861void
Caroline Ticeefed6132010-11-19 20:47:54 +0000862Debugger::DispatchInputInterrupt ()
863{
Greg Clayton44d93782014-01-27 23:43:24 +0000864 Mutex::Locker locker (m_input_reader_stack.GetMutex());
865 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000866 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000867 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000868}
869
870void
871Debugger::DispatchInputEndOfFile ()
872{
Greg Clayton44d93782014-01-27 23:43:24 +0000873 Mutex::Locker locker (m_input_reader_stack.GetMutex());
874 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000875 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000876 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000877}
878
879void
Greg Clayton44d93782014-01-27 23:43:24 +0000880Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000881{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000882 // 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 +0000883 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000884 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000885 {
Greg Clayton44d93782014-01-27 23:43:24 +0000886 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000887 if (reader_sp)
888 {
Greg Clayton44d93782014-01-27 23:43:24 +0000889 m_input_reader_stack.Pop();
890 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000891 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000892 }
893 }
894}
895
896void
Siva Chandra9aaab552015-02-26 19:26:36 +0000897Debugger::ExecuteIOHandlers()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000898{
Caroline Tice9088b062011-05-09 23:06:58 +0000899
Greg Clayton44d93782014-01-27 23:43:24 +0000900 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 {
Greg Clayton44d93782014-01-27 23:43:24 +0000902 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903 if (!reader_sp)
904 break;
905
Greg Clayton44d93782014-01-27 23:43:24 +0000906 reader_sp->Activate();
907 reader_sp->Run();
908 reader_sp->Deactivate();
909
910 // Remove all input readers that are done from the top of the stack
911 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912 {
Greg Clayton44d93782014-01-27 23:43:24 +0000913 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
914 if (top_reader_sp && top_reader_sp->GetIsDone())
915 m_input_reader_stack.Pop();
916 else
917 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918 }
919 }
Greg Clayton44d93782014-01-27 23:43:24 +0000920 ClearIOHandlers();
921}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922
Greg Clayton44d93782014-01-27 23:43:24 +0000923bool
924Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
925{
926 return m_input_reader_stack.IsTop (reader_sp);
927}
928
929
930ConstString
931Debugger::GetTopIOHandlerControlSequence(char ch)
932{
933 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934}
935
Kate Stonea487aa42015-01-15 00:52:41 +0000936const char *
937Debugger::GetIOHandlerCommandPrefix()
938{
939 return m_input_reader_stack.GetTopIOHandlerCommandPrefix();
940}
941
942const char *
943Debugger::GetIOHandlerHelpPrologue()
944{
945 return m_input_reader_stack.GetTopIOHandlerHelpPrologue();
946}
947
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948void
Greg Clayton44d93782014-01-27 23:43:24 +0000949Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
950{
Greg Clayton44d93782014-01-27 23:43:24 +0000951 PushIOHandler (reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +0000952
953 IOHandlerSP top_reader_sp = reader_sp;
954 while (top_reader_sp)
955 {
956 top_reader_sp->Activate();
957 top_reader_sp->Run();
958 top_reader_sp->Deactivate();
959
960 if (top_reader_sp.get() == reader_sp.get())
961 {
962 if (PopIOHandler (reader_sp))
963 break;
964 }
965
966 while (1)
967 {
968 top_reader_sp = m_input_reader_stack.Top();
969 if (top_reader_sp && top_reader_sp->GetIsDone())
970 m_input_reader_stack.Pop();
971 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
Greg Clayton44d93782014-01-27 23:43:24 +00001033 // Got the current top input reader...
1034 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001035
Greg Claytonb4874f12014-02-28 18:22:24 +00001036 // Don't push the same IO handler twice...
1037 if (reader_sp.get() != top_reader_sp.get())
1038 {
1039 // Push our new input reader
1040 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00001041
Greg Claytonb4874f12014-02-28 18:22:24 +00001042 // Interrupt the top input reader to it will exit its Run() function
1043 // and let this new input reader take over
1044 if (top_reader_sp)
1045 top_reader_sp->Deactivate();
1046 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047}
1048
1049bool
Greg Clayton44d93782014-01-27 23:43:24 +00001050Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051{
1052 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +00001053
1054 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055
1056 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001057 // read on the stack refresh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001058 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059 {
Greg Clayton44d93782014-01-27 23:43:24 +00001060 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061
1062 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
1063 {
Greg Clayton44d93782014-01-27 23:43:24 +00001064 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +00001065 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001066 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +00001067
1068 reader_sp = m_input_reader_stack.Top();
1069 if (reader_sp)
1070 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071
Greg Clayton44d93782014-01-27 23:43:24 +00001072 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073 }
1074 }
1075 return result;
1076}
1077
1078bool
Greg Clayton44d93782014-01-27 23:43:24 +00001079Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080{
Greg Clayton44d93782014-01-27 23:43:24 +00001081 Mutex::Locker locker;
1082
1083 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 {
Greg Clayton44d93782014-01-27 23:43:24 +00001085 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1086 if (reader_sp)
1087 reader_sp->Hide();
1088 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 }
Greg Clayton44d93782014-01-27 23:43:24 +00001090 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091}
1092
1093void
Greg Clayton44d93782014-01-27 23:43:24 +00001094Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095{
Greg Clayton44d93782014-01-27 23:43:24 +00001096 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1097 if (reader_sp)
1098 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099}
Greg Clayton66111032010-06-23 01:19:29 +00001100
Greg Clayton44d93782014-01-27 23:43:24 +00001101
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001102StreamSP
1103Debugger::GetAsyncOutputStream ()
1104{
1105 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1106 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1107}
1108
1109StreamSP
1110Debugger::GetAsyncErrorStream ()
1111{
1112 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1113 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1114}
1115
Greg Claytonc7bece562013-01-25 18:06:21 +00001116size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001117Debugger::GetNumDebuggers()
1118{
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001119 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001120 {
1121 Mutex::Locker locker (GetDebuggerListMutex ());
1122 return GetDebuggerList().size();
1123 }
1124 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001125}
1126
1127lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001128Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001129{
1130 DebuggerSP debugger_sp;
1131
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001132 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001133 {
1134 Mutex::Locker locker (GetDebuggerListMutex ());
1135 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001136
Greg Claytonc15f55e2012-03-30 20:53:46 +00001137 if (index < debugger_list.size())
1138 debugger_sp = debugger_list[index];
1139 }
1140
Enrico Granata061858c2012-02-15 02:34:21 +00001141 return debugger_sp;
1142}
1143
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001144DebuggerSP
1145Debugger::FindDebuggerWithID (lldb::user_id_t id)
1146{
Greg Clayton4d122c42011-09-17 08:33:22 +00001147 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001148
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001149 if (lldb_initialized)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001150 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001151 Mutex::Locker locker (GetDebuggerListMutex ());
1152 DebuggerList &debugger_list = GetDebuggerList();
1153 DebuggerList::iterator pos, end = debugger_list.end();
1154 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001155 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001156 if ((*pos).get()->GetID() == id)
1157 {
1158 debugger_sp = *pos;
1159 break;
1160 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001161 }
1162 }
1163 return debugger_sp;
1164}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001165
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001166#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001167static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001168TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001169{
1170 if (frame == NULL)
1171 return;
1172
1173 StreamString s;
1174 const char *prompt_format =
1175 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001176 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1177 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001178 "{process.id = '${process.id}'\n}"
1179 "{process.name = '${process.name}'\n}"
1180 "{process.file.basename = '${process.file.basename}'\n}"
1181 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1182 "{thread.id = '${thread.id}'\n}"
1183 "{thread.index = '${thread.index}'\n}"
1184 "{thread.name = '${thread.name}'\n}"
1185 "{thread.queue = '${thread.queue}'\n}"
1186 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1187 "{target.arch = '${target.arch}'\n}"
1188 "{module.file.basename = '${module.file.basename}'\n}"
1189 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1190 "{file.basename = '${file.basename}'\n}"
1191 "{file.fullpath = '${file.fullpath}'\n}"
1192 "{frame.index = '${frame.index}'\n}"
1193 "{frame.pc = '${frame.pc}'\n}"
1194 "{frame.sp = '${frame.sp}'\n}"
1195 "{frame.fp = '${frame.fp}'\n}"
1196 "{frame.flags = '${frame.flags}'\n}"
1197 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1198 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1199 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1200 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1201 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1202 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1203 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1204 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001205 "{function.changed = '${function.changed}'\n}"
1206 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001207 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001208 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001209 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001210 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001211 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001212 "{function.line-offset = '${function.line-offset}'\n}"
1213 "{function.pc-offset = '${function.pc-offset}'\n}"
1214 "{line.file.basename = '${line.file.basename}'\n}"
1215 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1216 "{line.number = '${line.number}'\n}"
1217 "{line.start-addr = '${line.start-addr}'\n}"
1218 "{line.end-addr = '${line.end-addr}'\n}"
1219;
1220
1221 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1222 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001223 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001224 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001225 {
1226 printf("%s\n", s.GetData());
1227 }
1228 else
1229 {
Greg Clayton1b654882010-09-19 02:33:57 +00001230 printf ("what we got: %s\n", s.GetData());
1231 }
1232}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001233#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001234
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001235bool
Greg Clayton554f68d2015-02-04 22:00:53 +00001236Debugger::FormatDisassemblerAddress (const FormatEntity::Entry *format,
Jason Molendaaff1b352014-10-10 23:07:36 +00001237 const SymbolContext *sc,
1238 const SymbolContext *prev_sc,
1239 const ExecutionContext *exe_ctx,
1240 const Address *addr,
1241 Stream &s)
1242{
Greg Clayton554f68d2015-02-04 22:00:53 +00001243 FormatEntity::Entry format_entry;
1244
1245 if (format == NULL)
Jason Molendaaff1b352014-10-10 23:07:36 +00001246 {
Greg Clayton554f68d2015-02-04 22:00:53 +00001247 if (exe_ctx != NULL && exe_ctx->HasTargetScope())
1248 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1249 if (format == NULL)
1250 {
1251 FormatEntity::Parse("${addr}: ", format_entry);
1252 format = &format_entry;
1253 }
Jason Molendaaff1b352014-10-10 23:07:36 +00001254 }
1255 bool function_changed = false;
1256 bool initial_function = false;
1257 if (prev_sc && (prev_sc->function || prev_sc->symbol))
1258 {
1259 if (sc && (sc->function || sc->symbol))
1260 {
1261 if (prev_sc->symbol && sc->symbol)
1262 {
1263 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
1264 {
1265 function_changed = true;
1266 }
1267 }
1268 else if (prev_sc->function && sc->function)
1269 {
1270 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
1271 {
1272 function_changed = true;
1273 }
1274 }
1275 }
1276 }
1277 // The first context on a list of instructions will have a prev_sc that
1278 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
1279 // would return false. But we do get a prev_sc pointer.
1280 if ((sc && (sc->function || sc->symbol))
1281 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
1282 {
1283 initial_function = true;
1284 }
Greg Clayton554f68d2015-02-04 22:00:53 +00001285 return FormatEntity::Format(*format, s, sc, exe_ctx, addr, NULL, function_changed, initial_function);
Jason Molendaaff1b352014-10-10 23:07:36 +00001286}
1287
1288
Jim Ingham228063c2012-02-21 02:23:08 +00001289void
1290Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1291{
Jim Ingham4f02b222012-02-22 22:49:20 +00001292 // For simplicity's sake, I am not going to deal with how to close down any
1293 // open logging streams, I just redirect everything from here on out to the
1294 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00001295 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
1296}
1297
1298bool
1299Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
1300{
1301 Log::Callbacks log_callbacks;
1302
1303 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001304 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001305 {
1306 log_stream_sp = m_log_callback_stream_sp;
1307 // For now when using the callback mode you always get thread & timestamp.
1308 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1309 }
1310 else if (log_file == NULL || *log_file == '\0')
1311 {
Greg Clayton44d93782014-01-27 23:43:24 +00001312 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00001313 }
1314 else
1315 {
1316 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001317 if (pos != m_log_streams.end())
1318 log_stream_sp = pos->second.lock();
1319 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001320 {
Pavel Labath8ac06992015-03-20 09:43:20 +00001321 uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate
1322 | File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
1323 if (! (log_options & LLDB_LOG_OPTION_APPEND))
1324 options |= File::eOpenOptionTruncate;
1325
1326 log_stream_sp.reset (new StreamFile (log_file, options));
Jim Ingham228063c2012-02-21 02:23:08 +00001327 m_log_streams[log_file] = log_stream_sp;
1328 }
Jim Ingham228063c2012-02-21 02:23:08 +00001329 }
1330 assert (log_stream_sp.get());
1331
1332 if (log_options == 0)
1333 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
1334
Greg Clayton57abc5d2013-05-10 21:47:16 +00001335 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00001336 {
1337 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
1338 return true;
1339 }
1340 else
1341 {
1342 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
1343 if (log_channel_sp)
1344 {
1345 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
1346 {
1347 return true;
1348 }
1349 else
1350 {
1351 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
1352 return false;
1353 }
1354 }
1355 else
1356 {
1357 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
1358 return false;
1359 }
1360 }
1361 return false;
1362}
1363
Greg Clayton9585fbf2013-03-19 00:20:55 +00001364SourceManager &
1365Debugger::GetSourceManager ()
1366{
1367 if (m_source_manager_ap.get() == NULL)
1368 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
1369 return *m_source_manager_ap;
1370}
1371
1372
Greg Clayton44d93782014-01-27 23:43:24 +00001373
1374// This function handles events that were broadcast by the process.
1375void
1376Debugger::HandleBreakpointEvent (const EventSP &event_sp)
1377{
1378 using namespace lldb;
1379 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
1380
1381// if (event_type & eBreakpointEventTypeAdded
1382// || event_type & eBreakpointEventTypeRemoved
1383// || event_type & eBreakpointEventTypeEnabled
1384// || event_type & eBreakpointEventTypeDisabled
1385// || event_type & eBreakpointEventTypeCommandChanged
1386// || event_type & eBreakpointEventTypeConditionChanged
1387// || event_type & eBreakpointEventTypeIgnoreChanged
1388// || event_type & eBreakpointEventTypeLocationsResolved)
1389// {
1390// // Don't do anything about these events, since the breakpoint commands already echo these actions.
1391// }
1392//
1393 if (event_type & eBreakpointEventTypeLocationsAdded)
1394 {
1395 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
1396 if (num_new_locations > 0)
1397 {
1398 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
1399 StreamFileSP output_sp (GetOutputFile());
1400 if (output_sp)
1401 {
1402 output_sp->Printf("%d location%s added to breakpoint %d\n",
1403 num_new_locations,
1404 num_new_locations == 1 ? "" : "s",
1405 breakpoint->GetID());
1406 RefreshTopIOHandler();
1407 }
1408 }
1409 }
1410// else if (event_type & eBreakpointEventTypeLocationsRemoved)
1411// {
1412// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
1413// }
1414// else if (event_type & eBreakpointEventTypeLocationsResolved)
1415// {
1416// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
1417// }
1418}
1419
1420size_t
1421Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
1422{
1423 size_t total_bytes = 0;
1424 if (stream == NULL)
1425 stream = GetOutputFile().get();
1426
1427 if (stream)
1428 {
1429 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
1430 if (process == NULL)
1431 {
1432 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1433 if (target_sp)
1434 process = target_sp->GetProcessSP().get();
1435 }
1436 if (process)
1437 {
1438 Error error;
1439 size_t len;
1440 char stdio_buffer[1024];
1441 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1442 {
1443 stream->Write(stdio_buffer, len);
1444 total_bytes += len;
1445 }
1446 }
1447 stream->Flush();
1448 }
1449 return total_bytes;
1450}
1451
1452size_t
1453Debugger::GetProcessSTDERR (Process *process, Stream *stream)
1454{
1455 size_t total_bytes = 0;
1456 if (stream == NULL)
1457 stream = GetOutputFile().get();
1458
1459 if (stream)
1460 {
1461 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
1462 if (process == NULL)
1463 {
1464 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1465 if (target_sp)
1466 process = target_sp->GetProcessSP().get();
1467 }
1468 if (process)
1469 {
1470 Error error;
1471 size_t len;
1472 char stdio_buffer[1024];
1473 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1474 {
1475 stream->Write(stdio_buffer, len);
1476 total_bytes += len;
1477 }
1478 }
1479 stream->Flush();
1480 }
1481 return total_bytes;
1482}
1483
Greg Claytondc6224e2014-10-21 01:00:42 +00001484
Greg Clayton44d93782014-01-27 23:43:24 +00001485// This function handles events that were broadcast by the process.
1486void
1487Debugger::HandleProcessEvent (const EventSP &event_sp)
1488{
1489 using namespace lldb;
1490 const uint32_t event_type = event_sp->GetType();
1491 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00001492
Greg Claytonb4874f12014-02-28 18:22:24 +00001493 StreamString output_stream;
1494 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00001495 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00001496
Greg Claytonb4874f12014-02-28 18:22:24 +00001497 if (!gui_enabled)
1498 {
1499 bool pop_process_io_handler = false;
1500 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00001501
Greg Claytonb4874f12014-02-28 18:22:24 +00001502 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00001503 {
Greg Claytonb4874f12014-02-28 18:22:24 +00001504 GetProcessSTDOUT (process_sp.get(), &output_stream);
1505 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001506
Greg Claytonb4874f12014-02-28 18:22:24 +00001507 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
1508 {
1509 GetProcessSTDERR (process_sp.get(), &error_stream);
1510 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001511
Greg Claytonb4874f12014-02-28 18:22:24 +00001512 if (event_type & Process::eBroadcastBitStateChanged)
1513 {
Greg Claytondc6224e2014-10-21 01:00:42 +00001514 Process::HandleProcessStateChangedEvent (event_sp, &output_stream, pop_process_io_handler);
Greg Clayton44d93782014-01-27 23:43:24 +00001515 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001516
Greg Claytonb4874f12014-02-28 18:22:24 +00001517 if (output_stream.GetSize() || error_stream.GetSize())
1518 {
1519 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00001520 bool top_io_handler_hid = false;
Greg Claytondc6224e2014-10-21 01:00:42 +00001521
Greg Clayton6fea17e2014-03-03 19:15:20 +00001522 if (process_sp->ProcessIOHandlerIsActive() == false)
1523 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00001524
1525 if (output_stream.GetSize())
1526 {
1527 StreamFileSP output_stream_sp (GetOutputFile());
1528 if (output_stream_sp)
1529 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
1530 }
1531
1532 if (error_stream.GetSize())
1533 {
1534 StreamFileSP error_stream_sp (GetErrorFile());
1535 if (error_stream_sp)
1536 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
1537 }
1538
1539 if (top_io_handler_hid)
1540 RefreshTopIOHandler();
1541 }
1542
1543 if (pop_process_io_handler)
1544 process_sp->PopProcessIOHandler();
1545 }
Greg Clayton44d93782014-01-27 23:43:24 +00001546}
1547
1548void
1549Debugger::HandleThreadEvent (const EventSP &event_sp)
1550{
1551 // At present the only thread event we handle is the Frame Changed event,
1552 // and all we do for that is just reprint the thread status for that thread.
1553 using namespace lldb;
1554 const uint32_t event_type = event_sp->GetType();
1555 if (event_type == Thread::eBroadcastBitStackChanged ||
1556 event_type == Thread::eBroadcastBitThreadSelected )
1557 {
1558 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
1559 if (thread_sp)
1560 {
1561 HideTopIOHandler();
1562 StreamFileSP stream_sp (GetOutputFile());
1563 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
1564 RefreshTopIOHandler();
1565 }
1566 }
1567}
1568
1569bool
1570Debugger::IsForwardingEvents ()
1571{
1572 return (bool)m_forward_listener_sp;
1573}
1574
1575void
1576Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
1577{
1578 m_forward_listener_sp = listener_sp;
1579}
1580
1581void
1582Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
1583{
1584 m_forward_listener_sp.reset();
1585}
1586
1587
1588void
1589Debugger::DefaultEventHandler()
1590{
1591 Listener& listener(GetListener());
1592 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
1593 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
1594 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
1595 BroadcastEventSpec target_event_spec (broadcaster_class_target,
1596 Target::eBroadcastBitBreakpointChanged);
1597
1598 BroadcastEventSpec process_event_spec (broadcaster_class_process,
1599 Process::eBroadcastBitStateChanged |
1600 Process::eBroadcastBitSTDOUT |
1601 Process::eBroadcastBitSTDERR);
1602
1603 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
1604 Thread::eBroadcastBitStackChanged |
1605 Thread::eBroadcastBitThreadSelected );
1606
1607 listener.StartListeningForEventSpec (*this, target_event_spec);
1608 listener.StartListeningForEventSpec (*this, process_event_spec);
1609 listener.StartListeningForEventSpec (*this, thread_event_spec);
1610 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
1611 CommandInterpreter::eBroadcastBitQuitCommandReceived |
1612 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
1613 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
Greg Claytonafa91e332014-12-01 22:41:27 +00001614
1615 // Let the thread that spawned us know that we have started up and
1616 // that we are now listening to all required events so no events get missed
1617 m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
1618
Greg Clayton44d93782014-01-27 23:43:24 +00001619 bool done = false;
1620 while (!done)
1621 {
Greg Clayton44d93782014-01-27 23:43:24 +00001622 EventSP event_sp;
1623 if (listener.WaitForEvent(NULL, event_sp))
1624 {
1625 if (event_sp)
1626 {
1627 Broadcaster *broadcaster = event_sp->GetBroadcaster();
1628 if (broadcaster)
1629 {
1630 uint32_t event_type = event_sp->GetType();
1631 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
1632 if (broadcaster_class == broadcaster_class_process)
1633 {
1634 HandleProcessEvent (event_sp);
1635 }
1636 else if (broadcaster_class == broadcaster_class_target)
1637 {
1638 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
1639 {
1640 HandleBreakpointEvent (event_sp);
1641 }
1642 }
1643 else if (broadcaster_class == broadcaster_class_thread)
1644 {
1645 HandleThreadEvent (event_sp);
1646 }
1647 else if (broadcaster == m_command_interpreter_ap.get())
1648 {
1649 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
1650 {
1651 done = true;
1652 }
1653 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
1654 {
1655 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1656 if (data && data[0])
1657 {
1658 StreamFileSP error_sp (GetErrorFile());
1659 if (error_sp)
1660 {
1661 HideTopIOHandler();
1662 error_sp->PutCString(data);
1663 error_sp->Flush();
1664 RefreshTopIOHandler();
1665 }
1666 }
1667 }
1668 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
1669 {
1670 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1671 if (data && data[0])
1672 {
1673 StreamFileSP output_sp (GetOutputFile());
1674 if (output_sp)
1675 {
1676 HideTopIOHandler();
1677 output_sp->PutCString(data);
1678 output_sp->Flush();
1679 RefreshTopIOHandler();
1680 }
1681 }
1682 }
1683 }
1684 }
1685
1686 if (m_forward_listener_sp)
1687 m_forward_listener_sp->AddEvent(event_sp);
1688 }
1689 }
1690 }
1691}
1692
1693lldb::thread_result_t
1694Debugger::EventHandlerThread (lldb::thread_arg_t arg)
1695{
1696 ((Debugger *)arg)->DefaultEventHandler();
1697 return NULL;
1698}
1699
1700bool
1701Debugger::StartEventHandlerThread()
1702{
Zachary Turneracee96a2014-09-23 18:32:09 +00001703 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001704 {
Greg Claytonafa91e332014-12-01 22:41:27 +00001705 // We must synchronize with the DefaultEventHandler() thread to ensure
1706 // it is up and running and listening to events before we return from
1707 // this function. We do this by listening to events for the
1708 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
1709 Listener listener("lldb.debugger.event-handler");
1710 listener.StartListeningForEvents(&m_sync_broadcaster, eBroadcastBitEventThreadIsListening);
1711
Zachary Turner7c2896a2014-10-24 22:06:29 +00001712 // Use larger 8MB stack for this thread
Greg Claytonafa91e332014-12-01 22:41:27 +00001713 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread,
1714 this,
1715 NULL,
Zachary Turner7c2896a2014-10-24 22:06:29 +00001716 g_debugger_event_thread_stack_bytes);
Greg Claytonafa91e332014-12-01 22:41:27 +00001717
1718 // Make sure DefaultEventHandler() is running and listening to events before we return
1719 // from this function. We are only listening for events of type
1720 // eBroadcastBitEventThreadIsListening so we don't need to check the event, we just need
1721 // to wait an infinite amount of time for it (NULL timeout as the first parameter)
1722 lldb::EventSP event_sp;
1723 listener.WaitForEvent(NULL, event_sp);
Greg Clayton807b6b32014-10-15 18:03:59 +00001724 }
Zachary Turneracee96a2014-09-23 18:32:09 +00001725 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001726}
1727
1728void
1729Debugger::StopEventHandlerThread()
1730{
Zachary Turneracee96a2014-09-23 18:32:09 +00001731 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001732 {
1733 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00001734 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001735 }
1736}
1737
1738
1739lldb::thread_result_t
1740Debugger::IOHandlerThread (lldb::thread_arg_t arg)
1741{
1742 Debugger *debugger = (Debugger *)arg;
Siva Chandra9aaab552015-02-26 19:26:36 +00001743 debugger->ExecuteIOHandlers();
Greg Clayton44d93782014-01-27 23:43:24 +00001744 debugger->StopEventHandlerThread();
1745 return NULL;
1746}
1747
1748bool
1749Debugger::StartIOHandlerThread()
1750{
Zachary Turneracee96a2014-09-23 18:32:09 +00001751 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001752 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
1753 IOHandlerThread,
1754 this,
1755 NULL,
1756 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00001757 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001758}
1759
1760void
1761Debugger::StopIOHandlerThread()
1762{
Zachary Turneracee96a2014-09-23 18:32:09 +00001763 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001764 {
1765 if (m_input_file_sp)
1766 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00001767 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001768 }
1769}
1770
Jim Ingham893c9322014-11-22 01:42:44 +00001771Target *
1772Debugger::GetDummyTarget()
1773{
1774 return m_target_list.GetDummyTarget (*this).get();
1775}
1776
1777Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001778Debugger::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001779{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001780 Target *target = nullptr;
1781 if (!prefer_dummy)
1782 {
1783 target = m_target_list.GetSelectedTarget().get();
1784 if (target)
1785 return target;
1786 }
1787
Jim Ingham893c9322014-11-22 01:42:44 +00001788 return GetDummyTarget();
1789}
Greg Clayton44d93782014-01-27 23:43:24 +00001790