blob: d36800e20bc0f13f737148316d2cd6c8346f6b11 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Debugger.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton4a33d312011-06-23 17:59:56 +000010#include "lldb/Core/Debugger.h"
11
12#include <map>
13
Jason Molenda705b1802014-06-13 02:37:02 +000014#include "llvm/ADT/StringRef.h"
Enrico Granata4becb372011-06-29 22:27:15 +000015
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/lldb-private.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000017#include "lldb/Core/FormatEntity.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/Module.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000019#include "lldb/Core/PluginInterface.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000020#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000021#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000024#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000025#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000026#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000027#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000029#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000030#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000031#include "lldb/DataFormatters/DataVisualization.h"
32#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000033#include "lldb/DataFormatters/TypeSummary.h"
Sean Callanan3e7e9152015-10-20 00:23:46 +000034#include "lldb/Expression/REPL.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000035#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000036#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000037#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000038#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000039#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000040#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000041#include "lldb/Interpreter/OptionValueSInt64.h"
42#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/Function.h"
45#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000046#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Target/TargetList.h"
Sean Callanan3e7e9152015-10-20 00:23:46 +000048#include "lldb/Target/Language.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000050#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000051#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000052#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000053#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000055#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Zachary Turner58a559c2014-08-27 20:15:09 +000057#include "llvm/Support/DynamicLibrary.h"
58
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059using namespace lldb;
60using namespace lldb_private;
61
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Caroline Ticeebc1bb22010-06-30 16:22:25 +000063static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000064static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000065
Greg Clayton1b654882010-09-19 02:33:57 +000066#pragma mark Static Functions
67
68static Mutex &
69GetDebuggerListMutex ()
70{
71 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
72 return g_mutex;
73}
74
75typedef std::vector<DebuggerSP> DebuggerList;
76
77static DebuggerList &
78GetDebuggerList()
79{
80 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000081 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000082 static DebuggerList g_list;
83 return g_list;
84}
Greg Claytone372b982011-11-21 21:44:34 +000085
86OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000087g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000088{
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +000089 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
90 { Debugger::eStopDisassemblyTypeNoDebugInfo, "no-debuginfo", "Show disassembly when there is no debug information."},
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." },
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +0000154{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoDebugInfo, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
Greg Clayton554f68d2015-02-04 22:00:53 +0000155{ "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)." },
Sean Callanan66810412015-10-19 23:11:07 +0000162{ "auto-indent", OptionValue::eTypeBoolean , true, true , NULL, NULL, "If true, LLDB will auto indent/outdent code. Currently only supported in the REPL (default: true)." },
163{ "print-decls", OptionValue::eTypeBoolean , true, true , NULL, NULL, "If true, LLDB will print the values of variables declared in an expression. Currently only supported in the REPL (default: true)." },
164{ "tab-size", OptionValue::eTypeUInt64 , true, 4 , NULL, NULL, "The tab size to use when indenting code in multi-line input mode (default: 4)." },
Greg Clayton554f68d2015-02-04 22:00:53 +0000165{ "escape-non-printables", OptionValue::eTypeBoolean , true, true, NULL, NULL, "If true, LLDB will automatically escape non-printable and escape characters when formatting strings." },
166{ NULL, OptionValue::eTypeInvalid , true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000167};
168
169enum
170{
171 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000172 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000173 ePropertyFrameFormat,
174 ePropertyNotiftVoid,
175 ePropertyPrompt,
176 ePropertyScriptLanguage,
177 ePropertyStopDisassemblyCount,
178 ePropertyStopDisassemblyDisplay,
179 ePropertyStopLineCountAfter,
180 ePropertyStopLineCountBefore,
181 ePropertyTerminalWidth,
182 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000183 ePropertyUseExternalEditor,
184 ePropertyUseColor,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000185 ePropertyAutoOneLineSummaries,
Sean Callanan66810412015-10-19 23:11:07 +0000186 ePropertyAutoIndent,
187 ePropertyPrintDecls,
188 ePropertyTabSize,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000189 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000190};
191
Zachary Turner3a006912015-03-19 22:00:21 +0000192LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000193
194Error
195Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
196 VarSetOperationType op,
197 const char *property_path,
198 const char *value)
199{
Enrico Granata84a53df2013-05-20 22:29:23 +0000200 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000201 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000202 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000203 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000204 if (is_load_script && exe_ctx->GetTargetSP())
205 {
206 target_sp = exe_ctx->GetTargetSP();
207 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
208 }
Greg Clayton4c054102012-09-01 00:38:36 +0000209 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
210 if (error.Success())
211 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000212 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000213 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
214 {
215 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000216 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
217 if (str.length())
218 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000219 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000220 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
221 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
222 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000223 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
224 {
225 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
226 SetPrompt (GetPrompt());
227 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000228 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000229 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000230 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000231 {
232 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000233 StreamString feedback_stream;
234 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000235 {
Greg Clayton44d93782014-01-27 23:43:24 +0000236 StreamFileSP stream_sp (GetErrorFile());
237 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000238 {
Greg Clayton44d93782014-01-27 23:43:24 +0000239 for (auto error : errors)
240 {
241 stream_sp->Printf("%s\n",error.AsCString());
242 }
243 if (feedback_stream.GetSize())
244 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000245 }
246 }
247 }
248 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000249 else if (is_escape_non_printables)
250 {
251 DataVisualization::ForceUpdate();
252 }
Greg Clayton4c054102012-09-01 00:38:36 +0000253 }
254 return error;
255}
256
Greg Clayton67cc0632012-08-22 17:17:09 +0000257bool
258Debugger::GetAutoConfirm () const
259{
260 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000261 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000262}
263
Greg Clayton554f68d2015-02-04 22:00:53 +0000264const FormatEntity::Entry *
Jason Molendaaff1b352014-10-10 23:07:36 +0000265Debugger::GetDisassemblyFormat() const
266{
267 const uint32_t idx = ePropertyDisassemblyFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000268 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Jason Molendaaff1b352014-10-10 23:07:36 +0000269}
270
Greg Clayton554f68d2015-02-04 22:00:53 +0000271const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000272Debugger::GetFrameFormat() const
273{
274 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000275 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000276}
277
278bool
279Debugger::GetNotifyVoid () const
280{
281 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000282 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000283}
284
285const char *
286Debugger::GetPrompt() const
287{
288 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000289 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000290}
291
292void
293Debugger::SetPrompt(const char *p)
294{
295 const uint32_t idx = ePropertyPrompt;
296 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
297 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000298 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
299 if (str.length())
300 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000301 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000302}
303
Greg Clayton554f68d2015-02-04 22:00:53 +0000304const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000305Debugger::GetThreadFormat() const
306{
307 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton554f68d2015-02-04 22:00:53 +0000308 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(NULL, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000309}
310
311lldb::ScriptLanguage
312Debugger::GetScriptLanguage() const
313{
314 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000315 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000316}
317
318bool
319Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
320{
321 const uint32_t idx = ePropertyScriptLanguage;
322 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
323}
324
325uint32_t
326Debugger::GetTerminalWidth () const
327{
328 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000329 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000330}
331
332bool
333Debugger::SetTerminalWidth (uint32_t term_width)
334{
335 const uint32_t idx = ePropertyTerminalWidth;
336 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
337}
338
339bool
340Debugger::GetUseExternalEditor () const
341{
342 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000343 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000344}
345
346bool
347Debugger::SetUseExternalEditor (bool b)
348{
349 const uint32_t idx = ePropertyUseExternalEditor;
350 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
351}
352
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000353bool
354Debugger::GetUseColor () const
355{
356 const uint32_t idx = ePropertyUseColor;
357 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
358}
359
360bool
361Debugger::SetUseColor (bool b)
362{
363 const uint32_t idx = ePropertyUseColor;
364 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
365 SetPrompt (GetPrompt());
366 return ret;
367}
368
Greg Clayton67cc0632012-08-22 17:17:09 +0000369uint32_t
370Debugger::GetStopSourceLineCount (bool before) const
371{
372 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000373 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000374}
375
376Debugger::StopDisassemblyType
377Debugger::GetStopDisassemblyDisplay () const
378{
379 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000380 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000381}
382
383uint32_t
384Debugger::GetDisassemblyLineCount () const
385{
386 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000387 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000388}
Greg Claytone372b982011-11-21 21:44:34 +0000389
Enrico Granata553fad52013-10-25 23:09:40 +0000390bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000391Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000392{
Enrico Granata90a8db32013-10-31 21:01:07 +0000393 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000394 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000395}
Enrico Granata553fad52013-10-25 23:09:40 +0000396
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000397bool
398Debugger::GetEscapeNonPrintables () const
399{
400 const uint32_t idx = ePropertyEscapeNonPrintables;
401 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000402}
403
Sean Callanan66810412015-10-19 23:11:07 +0000404bool
405Debugger::GetAutoIndent () const
406{
407 const uint32_t idx = ePropertyAutoIndent;
408 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
409}
410
411bool
412Debugger::SetAutoIndent (bool b)
413{
414 const uint32_t idx = ePropertyAutoIndent;
415 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
416}
417
418bool
419Debugger::GetPrintDecls () const
420{
421 const uint32_t idx = ePropertyPrintDecls;
422 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
423}
424
425bool
426Debugger::SetPrintDecls (bool b)
427{
428 const uint32_t idx = ePropertyPrintDecls;
429 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
430}
431
432uint32_t
433Debugger::GetTabSize () const
434{
435 const uint32_t idx = ePropertyTabSize;
436 return m_collection_sp->GetPropertyAtIndexAsUInt64 (NULL, idx, g_properties[idx].default_uint_value);
437}
438
439bool
440Debugger::SetTabSize (uint32_t tab_size)
441{
442 const uint32_t idx = ePropertyTabSize;
443 return m_collection_sp->SetPropertyAtIndexAsUInt64 (NULL, idx, tab_size);
444}
445
446
Greg Clayton1b654882010-09-19 02:33:57 +0000447#pragma mark Debugger
448
Greg Clayton67cc0632012-08-22 17:17:09 +0000449//const DebuggerPropertiesSP &
450//Debugger::GetSettings() const
451//{
452// return m_properties_sp;
453//}
454//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000455
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000456static bool lldb_initialized = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457void
Zachary Turner3a006912015-03-19 22:00:21 +0000458Debugger::Initialize(LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000460 assert(!lldb_initialized && "Debugger::Initialize called more than once!");
461
Robert Flackf196c932015-03-10 18:07:47 +0000462 lldb_initialized = true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000463 g_load_plugin_callback = load_plugin_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464}
465
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000466void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467Debugger::Terminate ()
468{
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000469 assert(lldb_initialized && "Debugger::Terminate called without a matching Debugger::Initialize!");
470
471 // Clear our master list of debugger objects
472 Mutex::Locker locker (GetDebuggerListMutex ());
Oleksiy Vyalovf3cd1812015-09-08 16:26:32 +0000473 auto& debuggers = GetDebuggerList();
474 for (const auto& debugger: debuggers)
475 debugger->Clear();
476
477 debuggers.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478}
479
Caroline Tice20bd37f2011-03-10 22:14:10 +0000480void
481Debugger::SettingsInitialize ()
482{
Greg Clayton6920b522012-08-22 18:39:03 +0000483 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000484}
485
486void
487Debugger::SettingsTerminate ()
488{
Greg Clayton6920b522012-08-22 18:39:03 +0000489 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000490}
491
Enrico Granata21dfcd92012-09-28 23:57:51 +0000492bool
Enrico Granatae743c782013-04-24 21:29:08 +0000493Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000494{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000495 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000496 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000497 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
498 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000499 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000500 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000501 return true;
502 }
Enrico Granatae743c782013-04-24 21:29:08 +0000503 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000504 else
Enrico Granatae743c782013-04-24 21:29:08 +0000505 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000506 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
507 // and if the public API layer isn't available (code is linking against
508 // all of the internal LLDB static libraries), then we can't load plugins
509 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000510 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000511 return false;
512}
513
514static FileSpec::EnumerateDirectoryResult
515LoadPluginCallback
516(
517 void *baton,
518 FileSpec::FileType file_type,
519 const FileSpec &file_spec
520 )
521{
522 Error error;
523
524 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000525 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000526
527 if (!baton)
528 return FileSpec::eEnumerateDirectoryResultQuit;
529
530 Debugger *debugger = (Debugger*)baton;
531
532 // If we have a regular file, a symbolic link or unknown file type, try
533 // and process the file. We must handle unknown as sometimes the directory
534 // enumeration might be enumerating a file system that doesn't have correct
535 // file type information.
536 if (file_type == FileSpec::eFileTypeRegular ||
537 file_type == FileSpec::eFileTypeSymbolicLink ||
538 file_type == FileSpec::eFileTypeUnknown )
539 {
540 FileSpec plugin_file_spec (file_spec);
541 plugin_file_spec.ResolvePath ();
542
Michael Sartain3cf443d2013-07-17 00:26:30 +0000543 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
544 plugin_file_spec.GetFileNameExtension() != g_solibext)
545 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000546 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000547 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000548
Enrico Granatae743c782013-04-24 21:29:08 +0000549 Error plugin_load_error;
550 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000551
552 return FileSpec::eEnumerateDirectoryResultNext;
553 }
554
555 else if (file_type == FileSpec::eFileTypeUnknown ||
556 file_type == FileSpec::eFileTypeDirectory ||
557 file_type == FileSpec::eFileTypeSymbolicLink )
558 {
559 // Try and recurse into anything that a directory or symbolic link.
560 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000561 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000562 // information.
563 return FileSpec::eEnumerateDirectoryResultEnter;
564 }
565
566 return FileSpec::eEnumerateDirectoryResultNext;
567}
568
569void
570Debugger::InstanceInitialize ()
571{
572 FileSpec dir_spec;
573 const bool find_directories = true;
574 const bool find_files = true;
575 const bool find_other = true;
576 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000577 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000578 {
579 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
580 {
581 FileSpec::EnumerateDirectory (dir_path,
582 find_directories,
583 find_files,
584 find_other,
585 LoadPluginCallback,
586 this);
587 }
588 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000589
590 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000591 {
592 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
593 {
594 FileSpec::EnumerateDirectory (dir_path,
595 find_directories,
596 find_files,
597 find_other,
598 LoadPluginCallback,
599 this);
600 }
601 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000602
603 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000604}
605
Greg Clayton66111032010-06-23 01:19:29 +0000606DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000607Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000608{
Jim Ingham228063c2012-02-21 02:23:08 +0000609 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000610 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000611 {
612 Mutex::Locker locker (GetDebuggerListMutex ());
613 GetDebuggerList().push_back(debugger_sp);
614 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000615 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000616 return debugger_sp;
617}
618
Caroline Ticee02657b2011-01-22 01:02:07 +0000619void
Greg Clayton4d122c42011-09-17 08:33:22 +0000620Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000621{
622 if (debugger_sp.get() == NULL)
623 return;
624
Jim Ingham8314c522011-09-15 21:36:42 +0000625 debugger_sp->Clear();
626
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000627 if (lldb_initialized)
Caroline Ticee02657b2011-01-22 01:02:07 +0000628 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000629 Mutex::Locker locker (GetDebuggerListMutex ());
630 DebuggerList &debugger_list = GetDebuggerList ();
631 DebuggerList::iterator pos, end = debugger_list.end();
632 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000633 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000634 if ((*pos).get() == debugger_sp.get())
635 {
636 debugger_list.erase (pos);
637 return;
638 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000639 }
640 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000641}
642
Greg Clayton4d122c42011-09-17 08:33:22 +0000643DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000644Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
645{
Greg Clayton4d122c42011-09-17 08:33:22 +0000646 DebuggerSP debugger_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000647 if (lldb_initialized)
Greg Clayton6920b522012-08-22 18:39:03 +0000648 {
649 Mutex::Locker locker (GetDebuggerListMutex ());
650 DebuggerList &debugger_list = GetDebuggerList();
651 DebuggerList::iterator pos, end = debugger_list.end();
652
653 for (pos = debugger_list.begin(); pos != end; ++pos)
654 {
655 if ((*pos).get()->m_instance_name == instance_name)
656 {
657 debugger_sp = *pos;
658 break;
659 }
660 }
661 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000662 return debugger_sp;
663}
Greg Clayton66111032010-06-23 01:19:29 +0000664
665TargetSP
666Debugger::FindTargetWithProcessID (lldb::pid_t pid)
667{
Greg Clayton4d122c42011-09-17 08:33:22 +0000668 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000669 if (lldb_initialized)
Greg Clayton66111032010-06-23 01:19:29 +0000670 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000671 Mutex::Locker locker (GetDebuggerListMutex ());
672 DebuggerList &debugger_list = GetDebuggerList();
673 DebuggerList::iterator pos, end = debugger_list.end();
674 for (pos = debugger_list.begin(); pos != end; ++pos)
675 {
676 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
677 if (target_sp)
678 break;
679 }
Greg Clayton66111032010-06-23 01:19:29 +0000680 }
681 return target_sp;
682}
683
Greg Claytone4e45922011-11-16 05:37:56 +0000684TargetSP
685Debugger::FindTargetWithProcess (Process *process)
686{
687 TargetSP target_sp;
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000688 if (lldb_initialized)
Greg Claytone4e45922011-11-16 05:37:56 +0000689 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000690 Mutex::Locker locker (GetDebuggerListMutex ());
691 DebuggerList &debugger_list = GetDebuggerList();
692 DebuggerList::iterator pos, end = debugger_list.end();
693 for (pos = debugger_list.begin(); pos != end; ++pos)
694 {
695 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
696 if (target_sp)
697 break;
698 }
Greg Claytone4e45922011-11-16 05:37:56 +0000699 }
700 return target_sp;
701}
702
Jason Molendae6481c72014-09-12 01:50:46 +0000703Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
704 UserID(g_unique_id++),
705 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
706 m_input_file_sp(new StreamFile(stdin, false)),
707 m_output_file_sp(new StreamFile(stdout, false)),
708 m_error_file_sp(new StreamFile(stderr, false)),
709 m_terminal_state(),
710 m_target_list(*this),
711 m_platform_list(),
712 m_listener("lldb.Debugger"),
713 m_source_manager_ap(),
714 m_source_file_cache(),
715 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
716 m_input_reader_stack(),
717 m_instance_name(),
Greg Claytonafa91e332014-12-01 22:41:27 +0000718 m_loaded_plugins(),
719 m_event_handler_thread (),
720 m_io_handler_thread (),
721 m_sync_broadcaster (NULL, "lldb.debugger.sync")
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722{
Greg Clayton67cc0632012-08-22 17:17:09 +0000723 char instance_cstr[256];
724 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
725 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000726 if (log_callback)
727 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000728 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000729 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000730 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000731 assert (default_platform_sp.get());
732 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000733
Greg Clayton754a9362012-08-23 00:22:02 +0000734 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000735 m_collection_sp->AppendProperty (ConstString("target"),
736 ConstString("Settings specify to debugging targets."),
737 true,
738 Target::GetGlobalProperties()->GetValueProperties());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000739 m_collection_sp->AppendProperty (ConstString("platform"),
740 ConstString("Platform settings."),
741 true,
742 Platform::GetGlobalPlatformProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000743 if (m_command_interpreter_ap.get())
744 {
745 m_collection_sp->AppendProperty (ConstString("interpreter"),
746 ConstString("Settings specify to the debugger's command interpreter."),
747 true,
748 m_command_interpreter_ap->GetValueProperties());
749 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000750 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
751 term_width->SetMinimumValue(10);
752 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000753
754 // Turn off use-color if this is a dumb terminal.
755 const char *term = getenv ("TERM");
756 if (term && !strcmp (term, "dumb"))
757 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
760Debugger::~Debugger ()
761{
Jim Ingham8314c522011-09-15 21:36:42 +0000762 Clear();
763}
764
765void
766Debugger::Clear()
767{
Greg Clayton44d93782014-01-27 23:43:24 +0000768 ClearIOHandlers();
769 StopIOHandlerThread();
770 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000771 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000772 int num_targets = m_target_list.GetNumTargets();
773 for (int i = 0; i < num_targets; i++)
774 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000775 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
776 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000777 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000778 ProcessSP process_sp (target_sp->GetProcessSP());
779 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000780 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000781 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000782 }
Greg Clayton66111032010-06-23 01:19:29 +0000783 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000784 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000785
786 // Close the input file _before_ we close the input read communications class
787 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000788 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000789 if (m_input_file_sp)
790 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000791
792 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000793}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794
795bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000796Debugger::GetCloseInputOnEOF () const
797{
Greg Clayton44d93782014-01-27 23:43:24 +0000798// return m_input_comm.GetCloseOnEOF();
799 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000800}
801
802void
803Debugger::SetCloseInputOnEOF (bool b)
804{
Greg Clayton44d93782014-01-27 23:43:24 +0000805// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000806}
807
808bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809Debugger::GetAsyncExecution ()
810{
Greg Clayton66111032010-06-23 01:19:29 +0000811 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812}
813
814void
815Debugger::SetAsyncExecution (bool async_execution)
816{
Greg Clayton66111032010-06-23 01:19:29 +0000817 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818}
819
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820
821void
822Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
823{
Greg Clayton44d93782014-01-27 23:43:24 +0000824 if (m_input_file_sp)
825 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
826 else
827 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
828
829 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000830 if (in_file.IsValid() == false)
831 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832
Jim Inghamc5917d92012-11-30 20:23:19 +0000833 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
834 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835}
836
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837void
838Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
839{
Greg Clayton44d93782014-01-27 23:43:24 +0000840 if (m_output_file_sp)
841 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
842 else
843 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
844
845 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000846 if (out_file.IsValid() == false)
847 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000848
Enrico Granatab5887262012-10-29 21:18:03 +0000849 // do not create the ScriptInterpreter just for setting the output file handle
850 // as the constructor will know how to do the right thing on its own
851 const bool can_create = false;
852 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
853 if (script_interpreter)
854 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855}
856
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857void
858Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
859{
Greg Clayton44d93782014-01-27 23:43:24 +0000860 if (m_error_file_sp)
861 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
862 else
863 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
864
865 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000866 if (err_file.IsValid() == false)
867 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868}
869
Jim Inghamc5917d92012-11-30 20:23:19 +0000870void
871Debugger::SaveInputTerminalState ()
872{
Greg Clayton44d93782014-01-27 23:43:24 +0000873 if (m_input_file_sp)
874 {
875 File &in_file = m_input_file_sp->GetFile();
876 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
877 m_terminal_state.Save(in_file.GetDescriptor(), true);
878 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000879}
880
881void
882Debugger::RestoreInputTerminalState ()
883{
884 m_terminal_state.Restore();
885}
886
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000888Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889{
890 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000891 TargetSP target_sp(GetSelectedTarget());
892 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893
894 if (target_sp)
895 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000896 ProcessSP process_sp (target_sp->GetProcessSP());
897 exe_ctx.SetProcessSP (process_sp);
898 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000900 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
901 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000903 exe_ctx.SetThreadSP (thread_sp);
904 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
905 if (exe_ctx.GetFramePtr() == NULL)
906 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 }
908 }
909 }
910 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911}
912
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913void
Caroline Ticeefed6132010-11-19 20:47:54 +0000914Debugger::DispatchInputInterrupt ()
915{
Greg Clayton44d93782014-01-27 23:43:24 +0000916 Mutex::Locker locker (m_input_reader_stack.GetMutex());
917 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000918 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000919 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000920}
921
922void
923Debugger::DispatchInputEndOfFile ()
924{
Greg Clayton44d93782014-01-27 23:43:24 +0000925 Mutex::Locker locker (m_input_reader_stack.GetMutex());
926 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000927 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000928 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000929}
930
931void
Greg Clayton44d93782014-01-27 23:43:24 +0000932Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000933{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000934 // 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 +0000935 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000936 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000937 {
Greg Clayton44d93782014-01-27 23:43:24 +0000938 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000939 if (reader_sp)
Pavel Labath44464872015-05-27 12:40:32 +0000940 PopIOHandler (reader_sp);
Caroline Tice3d6086f2010-12-20 18:35:50 +0000941 }
942}
943
944void
Siva Chandra9aaab552015-02-26 19:26:36 +0000945Debugger::ExecuteIOHandlers()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000946{
Greg Clayton44d93782014-01-27 23:43:24 +0000947 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948 {
Greg Clayton44d93782014-01-27 23:43:24 +0000949 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950 if (!reader_sp)
951 break;
952
Greg Clayton44d93782014-01-27 23:43:24 +0000953 reader_sp->Run();
Greg Clayton44d93782014-01-27 23:43:24 +0000954
955 // Remove all input readers that are done from the top of the stack
956 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957 {
Greg Clayton44d93782014-01-27 23:43:24 +0000958 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
959 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +0000960 PopIOHandler (top_reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000961 else
962 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 }
964 }
Greg Clayton44d93782014-01-27 23:43:24 +0000965 ClearIOHandlers();
966}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967
Greg Clayton44d93782014-01-27 23:43:24 +0000968bool
969Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
970{
971 return m_input_reader_stack.IsTop (reader_sp);
972}
973
Sean Callanan66810412015-10-19 23:11:07 +0000974bool
975Debugger::CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
976{
977 return m_input_reader_stack.CheckTopIOHandlerTypes (top_type, second_top_type);
978}
979
Pavel Labath44464872015-05-27 12:40:32 +0000980void
981Debugger::PrintAsync (const char *s, size_t len, bool is_stdout)
982{
983 lldb::StreamFileSP stream = is_stdout ? GetOutputFile() : GetErrorFile();
984 m_input_reader_stack.PrintAsync(stream.get(), s, len);
985}
Greg Clayton44d93782014-01-27 23:43:24 +0000986
987ConstString
988Debugger::GetTopIOHandlerControlSequence(char ch)
989{
990 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991}
992
Kate Stonea487aa42015-01-15 00:52:41 +0000993const char *
994Debugger::GetIOHandlerCommandPrefix()
995{
996 return m_input_reader_stack.GetTopIOHandlerCommandPrefix();
997}
998
999const char *
1000Debugger::GetIOHandlerHelpPrologue()
1001{
1002 return m_input_reader_stack.GetTopIOHandlerHelpPrologue();
1003}
1004
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005void
Greg Clayton44d93782014-01-27 23:43:24 +00001006Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
1007{
Greg Clayton44d93782014-01-27 23:43:24 +00001008 PushIOHandler (reader_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001009
Greg Clayton577508d2014-06-20 00:23:57 +00001010 IOHandlerSP top_reader_sp = reader_sp;
1011 while (top_reader_sp)
1012 {
Greg Clayton577508d2014-06-20 00:23:57 +00001013 top_reader_sp->Run();
Pavel Labath44464872015-05-27 12:40:32 +00001014
Greg Clayton577508d2014-06-20 00:23:57 +00001015 if (top_reader_sp.get() == reader_sp.get())
1016 {
1017 if (PopIOHandler (reader_sp))
1018 break;
1019 }
Pavel Labath44464872015-05-27 12:40:32 +00001020
Greg Clayton577508d2014-06-20 00:23:57 +00001021 while (1)
1022 {
1023 top_reader_sp = m_input_reader_stack.Top();
1024 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +00001025 PopIOHandler (top_reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +00001026 else
1027 break;
1028 }
1029 }
Greg Clayton44d93782014-01-27 23:43:24 +00001030}
1031
1032void
1033Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
1034{
1035 // Before an IOHandler runs, it must have in/out/err streams.
1036 // This function is called when one ore more of the streams
1037 // are NULL. We use the top input reader's in/out/err streams,
1038 // or fall back to the debugger file handles, or we fall back
1039 // onto stdin/stdout/stderr as a last resort.
1040
1041 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1042 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
1043 // If no STDIN has been set, then set it appropriately
1044 if (!in)
1045 {
1046 if (top_reader_sp)
1047 in = top_reader_sp->GetInputStreamFile();
1048 else
1049 in = GetInputFile();
1050
1051 // If there is nothing, use stdin
1052 if (!in)
1053 in = StreamFileSP(new StreamFile(stdin, false));
1054 }
1055 // If no STDOUT has been set, then set it appropriately
1056 if (!out)
1057 {
1058 if (top_reader_sp)
1059 out = top_reader_sp->GetOutputStreamFile();
1060 else
1061 out = GetOutputFile();
1062
1063 // If there is nothing, use stdout
1064 if (!out)
1065 out = StreamFileSP(new StreamFile(stdout, false));
1066 }
1067 // If no STDERR has been set, then set it appropriately
1068 if (!err)
1069 {
1070 if (top_reader_sp)
1071 err = top_reader_sp->GetErrorStreamFile();
1072 else
1073 err = GetErrorFile();
1074
1075 // If there is nothing, use stderr
1076 if (!err)
1077 err = StreamFileSP(new StreamFile(stdout, false));
1078
1079 }
1080}
1081
1082void
1083Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084{
1085 if (!reader_sp)
1086 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +00001087
Pavel Labath44464872015-05-27 12:40:32 +00001088 Mutex::Locker locker (m_input_reader_stack.GetMutex());
1089
1090 // Get the current top input reader...
Greg Clayton44d93782014-01-27 23:43:24 +00001091 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001092
Greg Claytonb4874f12014-02-28 18:22:24 +00001093 // Don't push the same IO handler twice...
Pavel Labath44464872015-05-27 12:40:32 +00001094 if (reader_sp == top_reader_sp)
1095 return;
Greg Clayton44d93782014-01-27 23:43:24 +00001096
Pavel Labath44464872015-05-27 12:40:32 +00001097 // Push our new input reader
1098 m_input_reader_stack.Push (reader_sp);
1099 reader_sp->Activate();
1100
1101 // Interrupt the top input reader to it will exit its Run() function
1102 // and let this new input reader take over
1103 if (top_reader_sp)
1104 {
1105 top_reader_sp->Deactivate();
1106 top_reader_sp->Cancel();
Greg Claytonb4874f12014-02-28 18:22:24 +00001107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108}
1109
1110bool
Greg Clayton44d93782014-01-27 23:43:24 +00001111Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112{
Pavel Labath44464872015-05-27 12:40:32 +00001113 if (! pop_reader_sp)
1114 return false;
1115
Greg Clayton44d93782014-01-27 23:43:24 +00001116 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117
1118 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001119 // read on the stack refresh its prompt and if there is one...
Pavel Labath44464872015-05-27 12:40:32 +00001120 if (m_input_reader_stack.IsEmpty())
1121 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122
Greg Clayton44d93782014-01-27 23:43:24 +00001123 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Greg Clayton66111032010-06-23 01:19:29 +00001124
Pavel Labath44464872015-05-27 12:40:32 +00001125 if (pop_reader_sp != reader_sp)
1126 return false;
1127
1128 reader_sp->Deactivate();
1129 reader_sp->Cancel();
1130 m_input_reader_stack.Pop ();
1131
1132 reader_sp = m_input_reader_stack.Top();
1133 if (reader_sp)
1134 reader_sp->Activate();
1135
1136 return true;
1137}
Greg Clayton44d93782014-01-27 23:43:24 +00001138
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001139StreamSP
1140Debugger::GetAsyncOutputStream ()
1141{
Pavel Labath44464872015-05-27 12:40:32 +00001142 return StreamSP (new StreamAsynchronousIO (*this, true));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001143}
1144
1145StreamSP
1146Debugger::GetAsyncErrorStream ()
1147{
Pavel Labath44464872015-05-27 12:40:32 +00001148 return StreamSP (new StreamAsynchronousIO (*this, false));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001149}
1150
Greg Claytonc7bece562013-01-25 18:06:21 +00001151size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001152Debugger::GetNumDebuggers()
1153{
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001154 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001155 {
1156 Mutex::Locker locker (GetDebuggerListMutex ());
1157 return GetDebuggerList().size();
1158 }
1159 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001160}
1161
1162lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001163Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001164{
1165 DebuggerSP debugger_sp;
1166
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001167 if (lldb_initialized)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001168 {
1169 Mutex::Locker locker (GetDebuggerListMutex ());
1170 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001171
Greg Claytonc15f55e2012-03-30 20:53:46 +00001172 if (index < debugger_list.size())
1173 debugger_sp = debugger_list[index];
1174 }
1175
Enrico Granata061858c2012-02-15 02:34:21 +00001176 return debugger_sp;
1177}
1178
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001179DebuggerSP
1180Debugger::FindDebuggerWithID (lldb::user_id_t id)
1181{
Greg Clayton4d122c42011-09-17 08:33:22 +00001182 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001183
Zachary Turnere6e2bb32015-03-31 21:03:22 +00001184 if (lldb_initialized)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001185 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001186 Mutex::Locker locker (GetDebuggerListMutex ());
1187 DebuggerList &debugger_list = GetDebuggerList();
1188 DebuggerList::iterator pos, end = debugger_list.end();
1189 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001190 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001191 if ((*pos).get()->GetID() == id)
1192 {
1193 debugger_sp = *pos;
1194 break;
1195 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001196 }
1197 }
1198 return debugger_sp;
1199}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001200
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001201#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001202static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001203TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001204{
1205 if (frame == NULL)
1206 return;
1207
1208 StreamString s;
1209 const char *prompt_format =
1210 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001211 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1212 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001213 "{process.id = '${process.id}'\n}"
1214 "{process.name = '${process.name}'\n}"
1215 "{process.file.basename = '${process.file.basename}'\n}"
1216 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1217 "{thread.id = '${thread.id}'\n}"
1218 "{thread.index = '${thread.index}'\n}"
1219 "{thread.name = '${thread.name}'\n}"
1220 "{thread.queue = '${thread.queue}'\n}"
1221 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1222 "{target.arch = '${target.arch}'\n}"
1223 "{module.file.basename = '${module.file.basename}'\n}"
1224 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1225 "{file.basename = '${file.basename}'\n}"
1226 "{file.fullpath = '${file.fullpath}'\n}"
1227 "{frame.index = '${frame.index}'\n}"
1228 "{frame.pc = '${frame.pc}'\n}"
1229 "{frame.sp = '${frame.sp}'\n}"
1230 "{frame.fp = '${frame.fp}'\n}"
1231 "{frame.flags = '${frame.flags}'\n}"
1232 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1233 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1234 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1235 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1236 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1237 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1238 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1239 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001240 "{function.changed = '${function.changed}'\n}"
1241 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001242 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001243 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001244 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001245 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001246 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001247 "{function.line-offset = '${function.line-offset}'\n}"
1248 "{function.pc-offset = '${function.pc-offset}'\n}"
1249 "{line.file.basename = '${line.file.basename}'\n}"
1250 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1251 "{line.number = '${line.number}'\n}"
1252 "{line.start-addr = '${line.start-addr}'\n}"
1253 "{line.end-addr = '${line.end-addr}'\n}"
1254;
1255
1256 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1257 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001258 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001259 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001260 {
1261 printf("%s\n", s.GetData());
1262 }
1263 else
1264 {
Greg Clayton1b654882010-09-19 02:33:57 +00001265 printf ("what we got: %s\n", s.GetData());
1266 }
1267}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001268#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001269
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001270bool
Greg Clayton554f68d2015-02-04 22:00:53 +00001271Debugger::FormatDisassemblerAddress (const FormatEntity::Entry *format,
Jason Molendaaff1b352014-10-10 23:07:36 +00001272 const SymbolContext *sc,
1273 const SymbolContext *prev_sc,
1274 const ExecutionContext *exe_ctx,
1275 const Address *addr,
1276 Stream &s)
1277{
Greg Clayton554f68d2015-02-04 22:00:53 +00001278 FormatEntity::Entry format_entry;
1279
1280 if (format == NULL)
Jason Molendaaff1b352014-10-10 23:07:36 +00001281 {
Greg Clayton554f68d2015-02-04 22:00:53 +00001282 if (exe_ctx != NULL && exe_ctx->HasTargetScope())
1283 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1284 if (format == NULL)
1285 {
1286 FormatEntity::Parse("${addr}: ", format_entry);
1287 format = &format_entry;
1288 }
Jason Molendaaff1b352014-10-10 23:07:36 +00001289 }
1290 bool function_changed = false;
1291 bool initial_function = false;
1292 if (prev_sc && (prev_sc->function || prev_sc->symbol))
1293 {
1294 if (sc && (sc->function || sc->symbol))
1295 {
1296 if (prev_sc->symbol && sc->symbol)
1297 {
1298 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
1299 {
1300 function_changed = true;
1301 }
1302 }
1303 else if (prev_sc->function && sc->function)
1304 {
1305 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
1306 {
1307 function_changed = true;
1308 }
1309 }
1310 }
1311 }
1312 // The first context on a list of instructions will have a prev_sc that
1313 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
1314 // would return false. But we do get a prev_sc pointer.
1315 if ((sc && (sc->function || sc->symbol))
1316 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
1317 {
1318 initial_function = true;
1319 }
Greg Clayton554f68d2015-02-04 22:00:53 +00001320 return FormatEntity::Format(*format, s, sc, exe_ctx, addr, NULL, function_changed, initial_function);
Jason Molendaaff1b352014-10-10 23:07:36 +00001321}
1322
1323
Jim Ingham228063c2012-02-21 02:23:08 +00001324void
1325Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1326{
Jim Ingham4f02b222012-02-22 22:49:20 +00001327 // For simplicity's sake, I am not going to deal with how to close down any
1328 // open logging streams, I just redirect everything from here on out to the
1329 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00001330 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
1331}
1332
1333bool
1334Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
1335{
Jim Ingham228063c2012-02-21 02:23:08 +00001336 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001337 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001338 {
1339 log_stream_sp = m_log_callback_stream_sp;
1340 // For now when using the callback mode you always get thread & timestamp.
1341 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1342 }
1343 else if (log_file == NULL || *log_file == '\0')
1344 {
Greg Clayton44d93782014-01-27 23:43:24 +00001345 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00001346 }
1347 else
1348 {
1349 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001350 if (pos != m_log_streams.end())
1351 log_stream_sp = pos->second.lock();
1352 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001353 {
Pavel Labath8ac06992015-03-20 09:43:20 +00001354 uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate
1355 | File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
1356 if (! (log_options & LLDB_LOG_OPTION_APPEND))
1357 options |= File::eOpenOptionTruncate;
1358
1359 log_stream_sp.reset (new StreamFile (log_file, options));
Jim Ingham228063c2012-02-21 02:23:08 +00001360 m_log_streams[log_file] = log_stream_sp;
1361 }
Jim Ingham228063c2012-02-21 02:23:08 +00001362 }
1363 assert (log_stream_sp.get());
1364
1365 if (log_options == 0)
1366 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
1367
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +00001368 return Log::EnableLogChannel(log_stream_sp, log_options, channel, categories, error_stream);
Jim Ingham228063c2012-02-21 02:23:08 +00001369}
1370
Greg Clayton9585fbf2013-03-19 00:20:55 +00001371SourceManager &
1372Debugger::GetSourceManager ()
1373{
1374 if (m_source_manager_ap.get() == NULL)
1375 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
1376 return *m_source_manager_ap;
1377}
1378
1379
Greg Clayton44d93782014-01-27 23:43:24 +00001380
1381// This function handles events that were broadcast by the process.
1382void
1383Debugger::HandleBreakpointEvent (const EventSP &event_sp)
1384{
1385 using namespace lldb;
1386 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
1387
1388// if (event_type & eBreakpointEventTypeAdded
1389// || event_type & eBreakpointEventTypeRemoved
1390// || event_type & eBreakpointEventTypeEnabled
1391// || event_type & eBreakpointEventTypeDisabled
1392// || event_type & eBreakpointEventTypeCommandChanged
1393// || event_type & eBreakpointEventTypeConditionChanged
1394// || event_type & eBreakpointEventTypeIgnoreChanged
1395// || event_type & eBreakpointEventTypeLocationsResolved)
1396// {
1397// // Don't do anything about these events, since the breakpoint commands already echo these actions.
1398// }
1399//
1400 if (event_type & eBreakpointEventTypeLocationsAdded)
1401 {
1402 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
1403 if (num_new_locations > 0)
1404 {
1405 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001406 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001407 if (output_sp)
1408 {
1409 output_sp->Printf("%d location%s added to breakpoint %d\n",
1410 num_new_locations,
1411 num_new_locations == 1 ? "" : "s",
1412 breakpoint->GetID());
Pavel Labath44464872015-05-27 12:40:32 +00001413 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001414 }
1415 }
1416 }
1417// else if (event_type & eBreakpointEventTypeLocationsRemoved)
1418// {
1419// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
1420// }
1421// else if (event_type & eBreakpointEventTypeLocationsResolved)
1422// {
1423// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
1424// }
1425}
1426
1427size_t
1428Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
1429{
1430 size_t total_bytes = 0;
1431 if (stream == NULL)
1432 stream = GetOutputFile().get();
1433
1434 if (stream)
1435 {
1436 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
1437 if (process == NULL)
1438 {
1439 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1440 if (target_sp)
1441 process = target_sp->GetProcessSP().get();
1442 }
1443 if (process)
1444 {
1445 Error error;
1446 size_t len;
1447 char stdio_buffer[1024];
1448 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1449 {
1450 stream->Write(stdio_buffer, len);
1451 total_bytes += len;
1452 }
1453 }
1454 stream->Flush();
1455 }
1456 return total_bytes;
1457}
1458
1459size_t
1460Debugger::GetProcessSTDERR (Process *process, Stream *stream)
1461{
1462 size_t total_bytes = 0;
1463 if (stream == NULL)
1464 stream = GetOutputFile().get();
1465
1466 if (stream)
1467 {
1468 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
1469 if (process == NULL)
1470 {
1471 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1472 if (target_sp)
1473 process = target_sp->GetProcessSP().get();
1474 }
1475 if (process)
1476 {
1477 Error error;
1478 size_t len;
1479 char stdio_buffer[1024];
1480 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1481 {
1482 stream->Write(stdio_buffer, len);
1483 total_bytes += len;
1484 }
1485 }
1486 stream->Flush();
1487 }
1488 return total_bytes;
1489}
1490
Greg Claytondc6224e2014-10-21 01:00:42 +00001491
Greg Clayton44d93782014-01-27 23:43:24 +00001492// This function handles events that were broadcast by the process.
1493void
1494Debugger::HandleProcessEvent (const EventSP &event_sp)
1495{
1496 using namespace lldb;
1497 const uint32_t event_type = event_sp->GetType();
1498 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00001499
Pavel Labath44464872015-05-27 12:40:32 +00001500 StreamSP output_stream_sp = GetAsyncOutputStream();
1501 StreamSP error_stream_sp = GetAsyncErrorStream();
Greg Clayton44d93782014-01-27 23:43:24 +00001502 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00001503
Greg Claytonb4874f12014-02-28 18:22:24 +00001504 if (!gui_enabled)
1505 {
1506 bool pop_process_io_handler = false;
1507 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00001508
Pavel Labath44464872015-05-27 12:40:32 +00001509 bool state_is_stopped = false;
1510 const bool got_state_changed = (event_type & Process::eBroadcastBitStateChanged) != 0;
1511 const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
1512 const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
1513 if (got_state_changed)
Greg Clayton44d93782014-01-27 23:43:24 +00001514 {
Pavel Labath44464872015-05-27 12:40:32 +00001515 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
1516 state_is_stopped = StateIsStoppedState(event_state, false);
Greg Claytonb4874f12014-02-28 18:22:24 +00001517 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001518
Pavel Labath44464872015-05-27 12:40:32 +00001519 // Display running state changes first before any STDIO
1520 if (got_state_changed && !state_is_stopped)
Greg Claytonb4874f12014-02-28 18:22:24 +00001521 {
Pavel Labath44464872015-05-27 12:40:32 +00001522 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
Greg Claytonb4874f12014-02-28 18:22:24 +00001523 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001524
Pavel Labath44464872015-05-27 12:40:32 +00001525 // Now display and STDOUT
1526 if (got_stdout || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001527 {
Pavel Labath44464872015-05-27 12:40:32 +00001528 GetProcessSTDOUT (process_sp.get(), output_stream_sp.get());
Greg Clayton44d93782014-01-27 23:43:24 +00001529 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001530
Pavel Labath44464872015-05-27 12:40:32 +00001531 // Now display and STDERR
1532 if (got_stderr || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001533 {
Pavel Labath44464872015-05-27 12:40:32 +00001534 GetProcessSTDERR (process_sp.get(), error_stream_sp.get());
Greg Claytonb4874f12014-02-28 18:22:24 +00001535 }
1536
Pavel Labath44464872015-05-27 12:40:32 +00001537 // Now display any stopped state changes after any STDIO
1538 if (got_state_changed && state_is_stopped)
1539 {
1540 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
1541 }
1542
1543 output_stream_sp->Flush();
1544 error_stream_sp->Flush();
1545
Greg Claytonb4874f12014-02-28 18:22:24 +00001546 if (pop_process_io_handler)
1547 process_sp->PopProcessIOHandler();
1548 }
Greg Clayton44d93782014-01-27 23:43:24 +00001549}
1550
1551void
1552Debugger::HandleThreadEvent (const EventSP &event_sp)
1553{
1554 // At present the only thread event we handle is the Frame Changed event,
1555 // and all we do for that is just reprint the thread status for that thread.
1556 using namespace lldb;
1557 const uint32_t event_type = event_sp->GetType();
1558 if (event_type == Thread::eBroadcastBitStackChanged ||
1559 event_type == Thread::eBroadcastBitThreadSelected )
1560 {
1561 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
1562 if (thread_sp)
1563 {
Pavel Labath44464872015-05-27 12:40:32 +00001564 thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00001565 }
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 {
Pavel Labath44464872015-05-27 12:40:32 +00001658 StreamSP error_sp (GetAsyncErrorStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001659 if (error_sp)
1660 {
Greg Clayton44d93782014-01-27 23:43:24 +00001661 error_sp->PutCString(data);
1662 error_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001663 }
1664 }
1665 }
1666 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
1667 {
1668 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1669 if (data && data[0])
1670 {
Pavel Labath44464872015-05-27 12:40:32 +00001671 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001672 if (output_sp)
1673 {
Greg Clayton44d93782014-01-27 23:43:24 +00001674 output_sp->PutCString(data);
1675 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001676 }
1677 }
1678 }
1679 }
1680 }
1681
1682 if (m_forward_listener_sp)
1683 m_forward_listener_sp->AddEvent(event_sp);
1684 }
1685 }
1686 }
1687}
1688
1689lldb::thread_result_t
1690Debugger::EventHandlerThread (lldb::thread_arg_t arg)
1691{
1692 ((Debugger *)arg)->DefaultEventHandler();
1693 return NULL;
1694}
1695
1696bool
1697Debugger::StartEventHandlerThread()
1698{
Zachary Turneracee96a2014-09-23 18:32:09 +00001699 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001700 {
Greg Claytonafa91e332014-12-01 22:41:27 +00001701 // We must synchronize with the DefaultEventHandler() thread to ensure
1702 // it is up and running and listening to events before we return from
1703 // this function. We do this by listening to events for the
1704 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
1705 Listener listener("lldb.debugger.event-handler");
1706 listener.StartListeningForEvents(&m_sync_broadcaster, eBroadcastBitEventThreadIsListening);
1707
Zachary Turner7c2896a2014-10-24 22:06:29 +00001708 // Use larger 8MB stack for this thread
Greg Claytonafa91e332014-12-01 22:41:27 +00001709 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread,
1710 this,
1711 NULL,
Zachary Turner7c2896a2014-10-24 22:06:29 +00001712 g_debugger_event_thread_stack_bytes);
Greg Claytonafa91e332014-12-01 22:41:27 +00001713
1714 // Make sure DefaultEventHandler() is running and listening to events before we return
1715 // from this function. We are only listening for events of type
1716 // eBroadcastBitEventThreadIsListening so we don't need to check the event, we just need
1717 // to wait an infinite amount of time for it (NULL timeout as the first parameter)
1718 lldb::EventSP event_sp;
1719 listener.WaitForEvent(NULL, event_sp);
Greg Clayton807b6b32014-10-15 18:03:59 +00001720 }
Zachary Turneracee96a2014-09-23 18:32:09 +00001721 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001722}
1723
1724void
1725Debugger::StopEventHandlerThread()
1726{
Zachary Turneracee96a2014-09-23 18:32:09 +00001727 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001728 {
1729 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00001730 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001731 }
1732}
1733
1734
1735lldb::thread_result_t
1736Debugger::IOHandlerThread (lldb::thread_arg_t arg)
1737{
1738 Debugger *debugger = (Debugger *)arg;
Siva Chandra9aaab552015-02-26 19:26:36 +00001739 debugger->ExecuteIOHandlers();
Greg Clayton44d93782014-01-27 23:43:24 +00001740 debugger->StopEventHandlerThread();
1741 return NULL;
1742}
1743
1744bool
Sean Callanan66810412015-10-19 23:11:07 +00001745Debugger::HasIOHandlerThread()
1746{
1747 return m_io_handler_thread.IsJoinable();
1748}
1749
1750bool
Greg Clayton44d93782014-01-27 23:43:24 +00001751Debugger::StartIOHandlerThread()
1752{
Zachary Turneracee96a2014-09-23 18:32:09 +00001753 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001754 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
1755 IOHandlerThread,
1756 this,
1757 NULL,
1758 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00001759 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001760}
1761
1762void
1763Debugger::StopIOHandlerThread()
1764{
Zachary Turneracee96a2014-09-23 18:32:09 +00001765 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001766 {
1767 if (m_input_file_sp)
1768 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00001769 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001770 }
1771}
1772
Sean Callanan66810412015-10-19 23:11:07 +00001773void
1774Debugger::JoinIOHandlerThread()
1775{
1776 if (HasIOHandlerThread())
1777 {
1778 thread_result_t result;
1779 m_io_handler_thread.Join(&result);
1780 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
1781 }
1782}
1783
Jim Ingham893c9322014-11-22 01:42:44 +00001784Target *
1785Debugger::GetDummyTarget()
1786{
1787 return m_target_list.GetDummyTarget (*this).get();
1788}
1789
1790Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001791Debugger::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001792{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001793 Target *target = nullptr;
1794 if (!prefer_dummy)
1795 {
1796 target = m_target_list.GetSelectedTarget().get();
1797 if (target)
1798 return target;
1799 }
1800
Jim Ingham893c9322014-11-22 01:42:44 +00001801 return GetDummyTarget();
1802}
Greg Clayton44d93782014-01-27 23:43:24 +00001803
Sean Callanan3e7e9152015-10-20 00:23:46 +00001804Error
1805Debugger::RunREPL (LanguageType language, const char *repl_options)
1806{
1807 Error err;
1808 FileSpec repl_executable;
Sean Callanan3e7e9152015-10-20 00:23:46 +00001809
Sean Callanan97f84e82015-10-21 19:31:17 +00001810 if (language == eLanguageTypeUnknown)
1811 {
1812 std::set<LanguageType> repl_languages;
1813
1814 Language::GetLanguagesSupportingREPLs(repl_languages);
1815
1816 if (repl_languages.size() == 1)
1817 {
1818 language = *repl_languages.begin();
1819 }
1820 else if (repl_languages.size() == 0)
1821 {
1822 err.SetErrorStringWithFormat("LLDB isn't configured with support support for any REPLs.");
1823 return err;
1824 }
1825 else
1826 {
1827 err.SetErrorStringWithFormat("Multiple possible REPL languages. Please specify a language.");
1828 return err;
1829 }
1830 }
1831
Sean Callanan3e7e9152015-10-20 00:23:46 +00001832 Target *const target = nullptr; // passing in an empty target means the REPL must create one
1833
Sean Callanan3b682de2015-10-21 00:28:44 +00001834 REPLSP repl_sp(REPL::Create(err, language, this, target, repl_options));
Sean Callanan3e7e9152015-10-20 00:23:46 +00001835
1836 if (!err.Success())
1837 {
1838 return err;
1839 }
1840
1841 if (!repl_sp)
1842 {
1843 err.SetErrorStringWithFormat("couldn't find a REPL for %s", Language::GetNameForLanguageType(language));
1844 return err;
1845 }
1846
1847 repl_sp->SetCompilerOptions(repl_options);
1848 repl_sp->RunLoop();
1849
1850 return err;
1851}
1852