blob: dfff06d11a93e636d518d6cb96b96902508ac2b1 [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
Eugene Zelenkodf370552016-03-02 02:18:18 +000012// C Includes
13// C++ Includes
Greg Clayton4a33d312011-06-23 17:59:56 +000014#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000015#include <mutex>
Greg Clayton4a33d312011-06-23 17:59:56 +000016
Eugene Zelenkodf370552016-03-02 02:18:18 +000017// Other libraries and framework includes
Jason Molenda705b1802014-06-13 02:37:02 +000018#include "llvm/ADT/StringRef.h"
Eugene Zelenkodf370552016-03-02 02:18:18 +000019#include "llvm/Support/DynamicLibrary.h"
Enrico Granata4becb372011-06-29 22:27:15 +000020
Eugene Zelenkodf370552016-03-02 02:18:18 +000021// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/lldb-private.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000023#include "lldb/Core/FormatEntity.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Core/Module.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000025#include "lldb/Core/PluginInterface.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000026#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000027#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000029#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000030#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000031#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000032#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000033#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000035#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000036#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000037#include "lldb/DataFormatters/DataVisualization.h"
38#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000039#include "lldb/DataFormatters/TypeSummary.h"
Sean Callanan3e7e9152015-10-20 00:23:46 +000040#include "lldb/Expression/REPL.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000041#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000042#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000043#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000044#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000045#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000046#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000047#include "lldb/Interpreter/OptionValueSInt64.h"
48#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000049#include "lldb/Symbol/CompileUnit.h"
50#include "lldb/Symbol/Function.h"
51#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000052#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/TargetList.h"
Sean Callanan3e7e9152015-10-20 00:23:46 +000054#include "lldb/Target/Language.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000056#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000057#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000058#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000059#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000061#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
63using namespace lldb;
64using namespace lldb_private;
65
Caroline Ticeebc1bb22010-06-30 16:22:25 +000066static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000067static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000068
Greg Clayton1b654882010-09-19 02:33:57 +000069#pragma mark Static Functions
70
Greg Clayton1b654882010-09-19 02:33:57 +000071typedef std::vector<DebuggerSP> DebuggerList;
Greg Clayton6c42e062016-05-26 16:51:23 +000072static std::recursive_mutex *g_debugger_list_mutex_ptr = nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
73static DebuggerList *g_debugger_list_ptr = nullptr; // NOTE: intentional leak to avoid issues with C++ destructor chain
Greg Claytone372b982011-11-21 21:44:34 +000074
75OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000076g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000077{
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +000078 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
79 { Debugger::eStopDisassemblyTypeNoDebugInfo, "no-debuginfo", "Show disassembly when there is no debug information."},
80 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
81 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Eugene Zelenkodf370552016-03-02 02:18:18 +000082 { 0, nullptr, nullptr }
Greg Claytone372b982011-11-21 21:44:34 +000083};
84
Greg Clayton67cc0632012-08-22 17:17:09 +000085OptionEnumValueElement
86g_language_enumerators[] =
87{
88 { eScriptLanguageNone, "none", "Disable scripting languages."},
89 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
90 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Eugene Zelenkodf370552016-03-02 02:18:18 +000091 { 0, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +000092};
Greg Claytone372b982011-11-21 21:44:34 +000093
Greg Clayton67cc0632012-08-22 17:17:09 +000094#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
95#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Jason Molenda6ab659a2015-07-29 00:42:47 +000096#define IS_OPTIMIZED "{${function.is-optimized} [opt]}"
Greg Clayton67cc0632012-08-22 17:17:09 +000097
Michael Sartain0769b2b2013-07-30 16:44:36 +000098#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +000099 "{, ${frame.pc}}"\
100 MODULE_WITH_FUNC\
101 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000102 "{, name = '${thread.name}'}"\
103 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000104 "{, activity = '${thread.info.activity.name}'}" \
105 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000106 "{, stop reason = ${thread.stop-reason}}"\
107 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000108 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000109 "\\n"
110
111#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
112 MODULE_WITH_FUNC\
113 FILE_AND_LINE\
Jason Molenda6ab659a2015-07-29 00:42:47 +0000114 IS_OPTIMIZED\
Greg Clayton67cc0632012-08-22 17:17:09 +0000115 "\\n"
116
Jason Molendac980fa92015-02-13 23:24:21 +0000117// Three parts to this disassembly format specification:
118// 1. If this is a new function/symbol (no previous symbol/function), print
119// dylib`funcname:\n
120// 2. If this is a symbol context change (different from previous symbol/function), print
121// dylib`funcname:\n
122// 3. print
123// address <+offset>:
124#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}>}: "
125
126// gdb's disassembly format can be emulated with
127// ${current-pc-arrow}${addr-file-or-load}{ <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>}:
128
129// lldb's original format for disassembly would look like this format string -
130// {${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}}:
131
Greg Clayton754a9362012-08-23 00:22:02 +0000132static PropertyDefinition
133g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000134{
Eugene Zelenkodf370552016-03-02 02:18:18 +0000135{ "auto-confirm", OptionValue::eTypeBoolean , true, false, nullptr, nullptr, "If true all confirmation prompts will receive their default reply." },
136{ "disassembly-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_DISASSEMBLY_FORMAT, nullptr, "The default disassembly format string to use when disassembling instruction sequences." },
137{ "frame-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_FRAME_FORMAT, nullptr, "The default frame format string to use when displaying stack frame information for threads." },
138{ "notify-void", OptionValue::eTypeBoolean , true, false, nullptr, nullptr, "Notify the user explicitly if an expression returns void (default: false)." },
139{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", nullptr, "The debugger command line prompt displayed for the user." },
140{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, nullptr, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
141{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , nullptr, nullptr, "The number of disassembly lines to show when displaying a stopped context." },
142{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoDebugInfo, nullptr, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
143{ "stop-line-count-after", OptionValue::eTypeSInt64 , true, 3 , nullptr, nullptr, "The number of sources lines to display that come after the current source line when displaying a stopped context." },
144{ "stop-line-count-before", OptionValue::eTypeSInt64 , true, 3 , nullptr, nullptr, "The number of sources lines to display that come before the current source line when displaying a stopped context." },
145{ "term-width", OptionValue::eTypeSInt64 , true, 80 , nullptr, nullptr, "The maximum number of columns to use for displaying text." },
146{ "thread-format", OptionValue::eTypeFormatEntity, true, 0 , DEFAULT_THREAD_FORMAT, nullptr, "The default thread format string to use when displaying thread information." },
147{ "use-external-editor", OptionValue::eTypeBoolean , true, false, nullptr, nullptr, "Whether to use an external editor or not." },
148{ "use-color", OptionValue::eTypeBoolean , true, true , nullptr, nullptr, "Whether to use Ansi color codes or not." },
149{ "auto-one-line-summaries", OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
150{ "auto-indent", OptionValue::eTypeBoolean , true, true , nullptr, nullptr, "If true, LLDB will auto indent/outdent code. Currently only supported in the REPL (default: true)." },
151{ "print-decls", OptionValue::eTypeBoolean , true, true , nullptr, nullptr, "If true, LLDB will print the values of variables declared in an expression. Currently only supported in the REPL (default: true)." },
152{ "tab-size", OptionValue::eTypeUInt64 , true, 4 , nullptr, nullptr, "The tab size to use when indenting code in multi-line input mode (default: 4)." },
153{ "escape-non-printables", OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "If true, LLDB will automatically escape non-printable and escape characters when formatting strings." },
154{ nullptr, OptionValue::eTypeInvalid , true, 0 , nullptr, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +0000155};
156
157enum
158{
159 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000160 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000161 ePropertyFrameFormat,
162 ePropertyNotiftVoid,
163 ePropertyPrompt,
164 ePropertyScriptLanguage,
165 ePropertyStopDisassemblyCount,
166 ePropertyStopDisassemblyDisplay,
167 ePropertyStopLineCountAfter,
168 ePropertyStopLineCountBefore,
169 ePropertyTerminalWidth,
170 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000171 ePropertyUseExternalEditor,
172 ePropertyUseColor,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000173 ePropertyAutoOneLineSummaries,
Sean Callanan66810412015-10-19 23:11:07 +0000174 ePropertyAutoIndent,
175 ePropertyPrintDecls,
176 ePropertyTabSize,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000177 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000178};
179
Eugene Zelenkodf370552016-03-02 02:18:18 +0000180LoadPluginCallbackType Debugger::g_load_plugin_callback = nullptr;
Greg Clayton4c054102012-09-01 00:38:36 +0000181
182Error
183Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
184 VarSetOperationType op,
185 const char *property_path,
186 const char *value)
187{
Enrico Granata84a53df2013-05-20 22:29:23 +0000188 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000189 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000190 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000191 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000192 if (is_load_script && exe_ctx->GetTargetSP())
193 {
194 target_sp = exe_ctx->GetTargetSP();
195 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
196 }
Greg Clayton4c054102012-09-01 00:38:36 +0000197 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
198 if (error.Success())
199 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000200 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000201 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
202 {
203 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000204 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
205 if (str.length())
206 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000207 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000208 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
209 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
210 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000211 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
212 {
213 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
214 SetPrompt (GetPrompt());
215 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000216 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000217 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000218 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000219 {
220 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000221 StreamString feedback_stream;
222 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000223 {
Greg Clayton44d93782014-01-27 23:43:24 +0000224 StreamFileSP stream_sp (GetErrorFile());
225 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000226 {
Greg Clayton44d93782014-01-27 23:43:24 +0000227 for (auto error : errors)
228 {
229 stream_sp->Printf("%s\n",error.AsCString());
230 }
231 if (feedback_stream.GetSize())
232 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000233 }
234 }
235 }
236 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000237 else if (is_escape_non_printables)
238 {
239 DataVisualization::ForceUpdate();
240 }
Greg Clayton4c054102012-09-01 00:38:36 +0000241 }
242 return error;
243}
244
Greg Clayton67cc0632012-08-22 17:17:09 +0000245bool
246Debugger::GetAutoConfirm () const
247{
248 const uint32_t idx = ePropertyAutoConfirm;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000249 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000250}
251
Greg Clayton554f68d2015-02-04 22:00:53 +0000252const FormatEntity::Entry *
Jason Molendaaff1b352014-10-10 23:07:36 +0000253Debugger::GetDisassemblyFormat() const
254{
255 const uint32_t idx = ePropertyDisassemblyFormat;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000256 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx);
Jason Molendaaff1b352014-10-10 23:07:36 +0000257}
258
Greg Clayton554f68d2015-02-04 22:00:53 +0000259const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000260Debugger::GetFrameFormat() const
261{
262 const uint32_t idx = ePropertyFrameFormat;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000263 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000264}
265
266bool
267Debugger::GetNotifyVoid () const
268{
269 const uint32_t idx = ePropertyNotiftVoid;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000270 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000271}
272
273const char *
274Debugger::GetPrompt() const
275{
276 const uint32_t idx = ePropertyPrompt;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000277 return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000278}
279
280void
281Debugger::SetPrompt(const char *p)
282{
283 const uint32_t idx = ePropertyPrompt;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000284 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +0000285 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000286 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
287 if (str.length())
288 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000289 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000290}
291
Greg Clayton554f68d2015-02-04 22:00:53 +0000292const FormatEntity::Entry *
Greg Clayton67cc0632012-08-22 17:17:09 +0000293Debugger::GetThreadFormat() const
294{
295 const uint32_t idx = ePropertyThreadFormat;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000296 return m_collection_sp->GetPropertyAtIndexAsFormatEntity(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000297}
298
299lldb::ScriptLanguage
300Debugger::GetScriptLanguage() const
301{
302 const uint32_t idx = ePropertyScriptLanguage;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000303 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000304}
305
306bool
307Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
308{
309 const uint32_t idx = ePropertyScriptLanguage;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000310 return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, script_lang);
Greg Clayton67cc0632012-08-22 17:17:09 +0000311}
312
313uint32_t
314Debugger::GetTerminalWidth () const
315{
316 const uint32_t idx = ePropertyTerminalWidth;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000317 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000318}
319
320bool
321Debugger::SetTerminalWidth (uint32_t term_width)
322{
323 const uint32_t idx = ePropertyTerminalWidth;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000324 return m_collection_sp->SetPropertyAtIndexAsSInt64(nullptr, idx, term_width);
Greg Clayton67cc0632012-08-22 17:17:09 +0000325}
326
327bool
328Debugger::GetUseExternalEditor () const
329{
330 const uint32_t idx = ePropertyUseExternalEditor;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000331 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000332}
333
334bool
335Debugger::SetUseExternalEditor (bool b)
336{
337 const uint32_t idx = ePropertyUseExternalEditor;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000338 return m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +0000339}
340
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000341bool
342Debugger::GetUseColor () const
343{
344 const uint32_t idx = ePropertyUseColor;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000345 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000346}
347
348bool
349Debugger::SetUseColor (bool b)
350{
351 const uint32_t idx = ePropertyUseColor;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000352 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000353 SetPrompt (GetPrompt());
354 return ret;
355}
356
Greg Clayton67cc0632012-08-22 17:17:09 +0000357uint32_t
358Debugger::GetStopSourceLineCount (bool before) const
359{
360 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000361 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000362}
363
364Debugger::StopDisassemblyType
365Debugger::GetStopDisassemblyDisplay () const
366{
367 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000368 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000369}
370
371uint32_t
372Debugger::GetDisassemblyLineCount () const
373{
374 const uint32_t idx = ePropertyStopDisassemblyCount;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000375 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000376}
Greg Claytone372b982011-11-21 21:44:34 +0000377
Enrico Granata553fad52013-10-25 23:09:40 +0000378bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000379Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000380{
Enrico Granata90a8db32013-10-31 21:01:07 +0000381 const uint32_t idx = ePropertyAutoOneLineSummaries;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000382 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000383}
Enrico Granata553fad52013-10-25 23:09:40 +0000384
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000385bool
386Debugger::GetEscapeNonPrintables () const
387{
388 const uint32_t idx = ePropertyEscapeNonPrintables;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000389 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000390}
391
Sean Callanan66810412015-10-19 23:11:07 +0000392bool
393Debugger::GetAutoIndent () const
394{
395 const uint32_t idx = ePropertyAutoIndent;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000396 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
Sean Callanan66810412015-10-19 23:11:07 +0000397}
398
399bool
400Debugger::SetAutoIndent (bool b)
401{
402 const uint32_t idx = ePropertyAutoIndent;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000403 return m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Sean Callanan66810412015-10-19 23:11:07 +0000404}
405
406bool
407Debugger::GetPrintDecls () const
408{
409 const uint32_t idx = ePropertyPrintDecls;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000410 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
Sean Callanan66810412015-10-19 23:11:07 +0000411}
412
413bool
414Debugger::SetPrintDecls (bool b)
415{
416 const uint32_t idx = ePropertyPrintDecls;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000417 return m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Sean Callanan66810412015-10-19 23:11:07 +0000418}
419
420uint32_t
421Debugger::GetTabSize () const
422{
423 const uint32_t idx = ePropertyTabSize;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000424 return m_collection_sp->GetPropertyAtIndexAsUInt64(nullptr, idx, g_properties[idx].default_uint_value);
Sean Callanan66810412015-10-19 23:11:07 +0000425}
426
427bool
428Debugger::SetTabSize (uint32_t tab_size)
429{
430 const uint32_t idx = ePropertyTabSize;
Eugene Zelenkodf370552016-03-02 02:18:18 +0000431 return m_collection_sp->SetPropertyAtIndexAsUInt64(nullptr, idx, tab_size);
Sean Callanan66810412015-10-19 23:11:07 +0000432}
433
Greg Clayton1b654882010-09-19 02:33:57 +0000434#pragma mark Debugger
435
Greg Clayton67cc0632012-08-22 17:17:09 +0000436//const DebuggerPropertiesSP &
437//Debugger::GetSettings() const
438//{
439// return m_properties_sp;
440//}
441//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000442
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443void
Zachary Turner3a006912015-03-19 22:00:21 +0000444Debugger::Initialize(LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445{
Greg Clayton6c42e062016-05-26 16:51:23 +0000446 assert(g_debugger_list_ptr == nullptr && "Debugger::Initialize called more than once!");
447 g_debugger_list_mutex_ptr = new std::recursive_mutex();
448 g_debugger_list_ptr = new DebuggerList();
Greg Clayton5fb8f792013-12-02 19:35:49 +0000449 g_load_plugin_callback = load_plugin_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450}
451
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000452void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453Debugger::Terminate ()
454{
Greg Clayton6c42e062016-05-26 16:51:23 +0000455 assert(g_debugger_list_ptr && "Debugger::Terminate called without a matching Debugger::Initialize!");
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000456
Greg Clayton6c42e062016-05-26 16:51:23 +0000457 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
458 {
459 // Clear our master list of debugger objects
460 {
461 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
462 for (const auto& debugger: *g_debugger_list_ptr)
463 debugger->Clear();
464 g_debugger_list_ptr->clear();
465 delete g_debugger_list_ptr;
466 g_debugger_list_ptr = nullptr;
467 }
468 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469}
470
Caroline Tice20bd37f2011-03-10 22:14:10 +0000471void
472Debugger::SettingsInitialize ()
473{
Greg Clayton6920b522012-08-22 18:39:03 +0000474 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000475}
476
477void
478Debugger::SettingsTerminate ()
479{
Greg Clayton6920b522012-08-22 18:39:03 +0000480 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000481}
482
Enrico Granata21dfcd92012-09-28 23:57:51 +0000483bool
Enrico Granatae743c782013-04-24 21:29:08 +0000484Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000485{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000486 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000487 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000488 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
489 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000490 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000491 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000492 return true;
493 }
Enrico Granatae743c782013-04-24 21:29:08 +0000494 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000495 else
Enrico Granatae743c782013-04-24 21:29:08 +0000496 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000497 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
498 // and if the public API layer isn't available (code is linking against
499 // all of the internal LLDB static libraries), then we can't load plugins
500 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000501 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000502 return false;
503}
504
505static FileSpec::EnumerateDirectoryResult
Eugene Zelenkodf370552016-03-02 02:18:18 +0000506LoadPluginCallback(void *baton,
507 FileSpec::FileType file_type,
508 const FileSpec &file_spec)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000509{
510 Error error;
511
512 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000513 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000514
515 if (!baton)
516 return FileSpec::eEnumerateDirectoryResultQuit;
517
518 Debugger *debugger = (Debugger*)baton;
519
520 // If we have a regular file, a symbolic link or unknown file type, try
521 // and process the file. We must handle unknown as sometimes the directory
522 // enumeration might be enumerating a file system that doesn't have correct
523 // file type information.
524 if (file_type == FileSpec::eFileTypeRegular ||
525 file_type == FileSpec::eFileTypeSymbolicLink ||
526 file_type == FileSpec::eFileTypeUnknown )
527 {
528 FileSpec plugin_file_spec (file_spec);
529 plugin_file_spec.ResolvePath ();
530
Michael Sartain3cf443d2013-07-17 00:26:30 +0000531 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
532 plugin_file_spec.GetFileNameExtension() != g_solibext)
533 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000534 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000535 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000536
Enrico Granatae743c782013-04-24 21:29:08 +0000537 Error plugin_load_error;
538 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000539
540 return FileSpec::eEnumerateDirectoryResultNext;
541 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000542 else if (file_type == FileSpec::eFileTypeUnknown ||
543 file_type == FileSpec::eFileTypeDirectory ||
544 file_type == FileSpec::eFileTypeSymbolicLink )
545 {
546 // Try and recurse into anything that a directory or symbolic link.
547 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000548 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000549 // information.
550 return FileSpec::eEnumerateDirectoryResultEnter;
551 }
552
553 return FileSpec::eEnumerateDirectoryResultNext;
554}
555
556void
557Debugger::InstanceInitialize ()
558{
559 FileSpec dir_spec;
560 const bool find_directories = true;
561 const bool find_files = true;
562 const bool find_other = true;
563 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000564 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000565 {
566 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
567 {
568 FileSpec::EnumerateDirectory (dir_path,
569 find_directories,
570 find_files,
571 find_other,
572 LoadPluginCallback,
573 this);
574 }
575 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000576
577 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, 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 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000589
590 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000591}
592
Greg Clayton66111032010-06-23 01:19:29 +0000593DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000594Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000595{
Jim Ingham228063c2012-02-21 02:23:08 +0000596 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Clayton6c42e062016-05-26 16:51:23 +0000597 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000598 {
Greg Clayton6c42e062016-05-26 16:51:23 +0000599 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
600 g_debugger_list_ptr->push_back(debugger_sp);
Greg Clayton66111032010-06-23 01:19:29 +0000601 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000602 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000603 return debugger_sp;
604}
605
Caroline Ticee02657b2011-01-22 01:02:07 +0000606void
Greg Clayton4d122c42011-09-17 08:33:22 +0000607Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000608{
Eugene Zelenkodf370552016-03-02 02:18:18 +0000609 if (!debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000610 return;
611
Jim Ingham8314c522011-09-15 21:36:42 +0000612 debugger_sp->Clear();
613
Greg Clayton6c42e062016-05-26 16:51:23 +0000614 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Caroline Ticee02657b2011-01-22 01:02:07 +0000615 {
Greg Clayton6c42e062016-05-26 16:51:23 +0000616 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
617 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
618 for (pos = g_debugger_list_ptr->begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000619 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000620 if ((*pos).get() == debugger_sp.get())
621 {
Greg Clayton6c42e062016-05-26 16:51:23 +0000622 g_debugger_list_ptr->erase (pos);
Greg Claytonc15f55e2012-03-30 20:53:46 +0000623 return;
624 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000625 }
626 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000627}
628
Greg Clayton4d122c42011-09-17 08:33:22 +0000629DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000630Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
631{
Greg Clayton4d122c42011-09-17 08:33:22 +0000632 DebuggerSP debugger_sp;
Greg Clayton6c42e062016-05-26 16:51:23 +0000633 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Greg Clayton6920b522012-08-22 18:39:03 +0000634 {
Greg Clayton6c42e062016-05-26 16:51:23 +0000635 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
636 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
637 for (pos = g_debugger_list_ptr->begin (); pos != end; ++pos)
Greg Clayton6920b522012-08-22 18:39:03 +0000638 {
Eugene Zelenkodf370552016-03-02 02:18:18 +0000639 if ((*pos)->m_instance_name == instance_name)
Greg Clayton6920b522012-08-22 18:39:03 +0000640 {
641 debugger_sp = *pos;
642 break;
643 }
644 }
645 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000646 return debugger_sp;
647}
Greg Clayton66111032010-06-23 01:19:29 +0000648
649TargetSP
650Debugger::FindTargetWithProcessID (lldb::pid_t pid)
651{
Greg Clayton4d122c42011-09-17 08:33:22 +0000652 TargetSP target_sp;
Greg Clayton6c42e062016-05-26 16:51:23 +0000653 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000654 {
Greg Clayton6c42e062016-05-26 16:51:23 +0000655 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
656 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
657 for (pos = g_debugger_list_ptr->begin (); pos != end; ++pos)
Greg Claytonc15f55e2012-03-30 20:53:46 +0000658 {
659 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
660 if (target_sp)
661 break;
662 }
Greg Clayton66111032010-06-23 01:19:29 +0000663 }
664 return target_sp;
665}
666
Greg Claytone4e45922011-11-16 05:37:56 +0000667TargetSP
668Debugger::FindTargetWithProcess (Process *process)
669{
670 TargetSP target_sp;
Greg Clayton6c42e062016-05-26 16:51:23 +0000671 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Greg Claytone4e45922011-11-16 05:37:56 +0000672 {
Greg Clayton6c42e062016-05-26 16:51:23 +0000673 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
674 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
675 for (pos = g_debugger_list_ptr->begin (); pos != end; ++pos)
Greg Claytonc15f55e2012-03-30 20:53:46 +0000676 {
677 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
678 if (target_sp)
679 break;
680 }
Greg Claytone4e45922011-11-16 05:37:56 +0000681 }
682 return target_sp;
683}
684
Jason Molendae6481c72014-09-12 01:50:46 +0000685Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
686 UserID(g_unique_id++),
687 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
688 m_input_file_sp(new StreamFile(stdin, false)),
689 m_output_file_sp(new StreamFile(stdout, false)),
690 m_error_file_sp(new StreamFile(stderr, false)),
Jim Ingham583bbb12016-03-07 21:50:25 +0000691 m_broadcaster_manager_sp(BroadcasterManager::MakeBroadcasterManager()),
Jason Molendae6481c72014-09-12 01:50:46 +0000692 m_terminal_state(),
693 m_target_list(*this),
694 m_platform_list(),
Jim Ingham583bbb12016-03-07 21:50:25 +0000695 m_listener_sp(Listener::MakeListener("lldb.Debugger")),
Jason Molendae6481c72014-09-12 01:50:46 +0000696 m_source_manager_ap(),
697 m_source_file_cache(),
698 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
699 m_input_reader_stack(),
700 m_instance_name(),
Greg Claytonafa91e332014-12-01 22:41:27 +0000701 m_loaded_plugins(),
Eugene Zelenkodf370552016-03-02 02:18:18 +0000702 m_event_handler_thread(),
703 m_io_handler_thread(),
Greg Clayton4329fe42016-05-04 22:26:42 +0000704 m_sync_broadcaster(nullptr, "lldb.debugger.sync"),
705 m_forward_listener_sp(),
706 m_clear_once()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707{
Greg Clayton67cc0632012-08-22 17:17:09 +0000708 char instance_cstr[256];
709 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
710 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000711 if (log_callback)
712 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000713 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000714 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000715 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Eugene Zelenkodf370552016-03-02 02:18:18 +0000716 assert(default_platform_sp);
Greg Claytonded470d2011-03-19 01:12:21 +0000717 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000718
Greg Clayton754a9362012-08-23 00:22:02 +0000719 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000720 m_collection_sp->AppendProperty (ConstString("target"),
721 ConstString("Settings specify to debugging targets."),
722 true,
723 Target::GetGlobalProperties()->GetValueProperties());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000724 m_collection_sp->AppendProperty (ConstString("platform"),
725 ConstString("Platform settings."),
726 true,
727 Platform::GetGlobalPlatformProperties()->GetValueProperties());
Eugene Zelenkodf370552016-03-02 02:18:18 +0000728 if (m_command_interpreter_ap)
Greg Clayton754a9362012-08-23 00:22:02 +0000729 {
730 m_collection_sp->AppendProperty (ConstString("interpreter"),
731 ConstString("Settings specify to the debugger's command interpreter."),
732 true,
733 m_command_interpreter_ap->GetValueProperties());
734 }
Eugene Zelenkodf370552016-03-02 02:18:18 +0000735 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64(nullptr, ePropertyTerminalWidth);
Greg Clayton67cc0632012-08-22 17:17:09 +0000736 term_width->SetMinimumValue(10);
737 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000738
739 // Turn off use-color if this is a dumb terminal.
740 const char *term = getenv ("TERM");
741 if (term && !strcmp (term, "dumb"))
742 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743}
744
745Debugger::~Debugger ()
746{
Jim Ingham8314c522011-09-15 21:36:42 +0000747 Clear();
748}
749
750void
751Debugger::Clear()
752{
Greg Clayton4329fe42016-05-04 22:26:42 +0000753 //----------------------------------------------------------------------
754 // Make sure we call this function only once. With the C++ global
755 // destructor chain having a list of debuggers and with code that can be
756 // running on other threads, we need to ensure this doesn't happen
757 // multiple times.
758 //
759 // The following functions call Debugger::Clear():
760 // Debugger::~Debugger();
761 // static void Debugger::Destroy(lldb::DebuggerSP &debugger_sp);
762 // static void Debugger::Terminate();
763 //----------------------------------------------------------------------
764 std::call_once(m_clear_once, [this]() {
765 ClearIOHandlers();
766 StopIOHandlerThread();
767 StopEventHandlerThread();
768 m_listener_sp->Clear();
769 int num_targets = m_target_list.GetNumTargets();
770 for (int i = 0; i < num_targets; i++)
Jim Ingham8314c522011-09-15 21:36:42 +0000771 {
Greg Clayton4329fe42016-05-04 22:26:42 +0000772 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
773 if (target_sp)
774 {
775 ProcessSP process_sp (target_sp->GetProcessSP());
776 if (process_sp)
777 process_sp->Finalize();
778 target_sp->Destroy();
779 }
Jim Ingham8314c522011-09-15 21:36:42 +0000780 }
Greg Clayton4329fe42016-05-04 22:26:42 +0000781 m_broadcaster_manager_sp->Clear ();
782
783 // Close the input file _before_ we close the input read communications class
784 // as it does NOT own the input file, our m_input_file does.
785 m_terminal_state.Clear();
786 if (m_input_file_sp)
787 m_input_file_sp->GetFile().Close ();
788
789 m_command_interpreter_ap->Clear();
790 });
Jim Ingham8314c522011-09-15 21:36:42 +0000791}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792
793bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000794Debugger::GetCloseInputOnEOF () const
795{
Greg Clayton44d93782014-01-27 23:43:24 +0000796// return m_input_comm.GetCloseOnEOF();
797 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000798}
799
800void
801Debugger::SetCloseInputOnEOF (bool b)
802{
Greg Clayton44d93782014-01-27 23:43:24 +0000803// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000804}
805
806bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807Debugger::GetAsyncExecution ()
808{
Greg Clayton66111032010-06-23 01:19:29 +0000809 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810}
811
812void
813Debugger::SetAsyncExecution (bool async_execution)
814{
Greg Clayton66111032010-06-23 01:19:29 +0000815 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816}
817
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818void
819Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
820{
Greg Clayton44d93782014-01-27 23:43:24 +0000821 if (m_input_file_sp)
822 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
823 else
824 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
825
826 File &in_file = m_input_file_sp->GetFile();
Eugene Zelenkodf370552016-03-02 02:18:18 +0000827 if (!in_file.IsValid())
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000828 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829
Jim Inghamc5917d92012-11-30 20:23:19 +0000830 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
831 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832}
833
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834void
835Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
836{
Greg Clayton44d93782014-01-27 23:43:24 +0000837 if (m_output_file_sp)
838 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
839 else
840 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
841
842 File &out_file = m_output_file_sp->GetFile();
Eugene Zelenkodf370552016-03-02 02:18:18 +0000843 if (!out_file.IsValid())
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000844 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000845
Enrico Granatab5887262012-10-29 21:18:03 +0000846 // do not create the ScriptInterpreter just for setting the output file handle
847 // as the constructor will know how to do the right thing on its own
848 const bool can_create = false;
849 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
850 if (script_interpreter)
851 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852}
853
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854void
855Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
856{
Greg Clayton44d93782014-01-27 23:43:24 +0000857 if (m_error_file_sp)
858 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
859 else
860 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
861
862 File &err_file = m_error_file_sp->GetFile();
Eugene Zelenkodf370552016-03-02 02:18:18 +0000863 if (!err_file.IsValid())
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000864 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865}
866
Jim Inghamc5917d92012-11-30 20:23:19 +0000867void
868Debugger::SaveInputTerminalState ()
869{
Greg Clayton44d93782014-01-27 23:43:24 +0000870 if (m_input_file_sp)
871 {
872 File &in_file = m_input_file_sp->GetFile();
873 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
874 m_terminal_state.Save(in_file.GetDescriptor(), true);
875 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000876}
877
878void
879Debugger::RestoreInputTerminalState ()
880{
881 m_terminal_state.Restore();
882}
883
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000885Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886{
887 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000888 TargetSP target_sp(GetSelectedTarget());
889 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890
891 if (target_sp)
892 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000893 ProcessSP process_sp (target_sp->GetProcessSP());
894 exe_ctx.SetProcessSP (process_sp);
Eugene Zelenkodf370552016-03-02 02:18:18 +0000895 if (process_sp && !process_sp->IsRunning())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000897 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
898 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000900 exe_ctx.SetThreadSP (thread_sp);
901 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
Eugene Zelenkodf370552016-03-02 02:18:18 +0000902 if (exe_ctx.GetFramePtr() == nullptr)
Greg Claytonc14ee322011-09-22 04:58:26 +0000903 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 }
905 }
906 }
907 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910void
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000911Debugger::DispatchInputInterrupt()
Caroline Ticeefed6132010-11-19 20:47:54 +0000912{
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000913 std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex());
914 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000915 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000916 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000917}
918
919void
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000920Debugger::DispatchInputEndOfFile()
Caroline Ticeefed6132010-11-19 20:47:54 +0000921{
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000922 std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex());
923 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000924 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000925 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000926}
927
928void
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000929Debugger::ClearIOHandlers()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000930{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000931 // The bottom input reader should be the main debugger input reader. We do not want to close that one here.
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000932 std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000933 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000934 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000935 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000936 if (reader_sp)
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000937 PopIOHandler(reader_sp);
Caroline Tice3d6086f2010-12-20 18:35:50 +0000938 }
939}
940
941void
Siva Chandra9aaab552015-02-26 19:26:36 +0000942Debugger::ExecuteIOHandlers()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000943{
Eugene Zelenkodf370552016-03-02 02:18:18 +0000944 while (true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945 {
Greg Clayton44d93782014-01-27 23:43:24 +0000946 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947 if (!reader_sp)
948 break;
949
Greg Clayton44d93782014-01-27 23:43:24 +0000950 reader_sp->Run();
Greg Clayton44d93782014-01-27 23:43:24 +0000951
952 // Remove all input readers that are done from the top of the stack
Eugene Zelenkodf370552016-03-02 02:18:18 +0000953 while (true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954 {
Greg Clayton44d93782014-01-27 23:43:24 +0000955 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
956 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +0000957 PopIOHandler (top_reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000958 else
959 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960 }
961 }
Greg Clayton44d93782014-01-27 23:43:24 +0000962 ClearIOHandlers();
963}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964
Greg Clayton44d93782014-01-27 23:43:24 +0000965bool
966Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
967{
968 return m_input_reader_stack.IsTop (reader_sp);
969}
970
Sean Callanan66810412015-10-19 23:11:07 +0000971bool
972Debugger::CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
973{
974 return m_input_reader_stack.CheckTopIOHandlerTypes (top_type, second_top_type);
975}
976
Pavel Labath44464872015-05-27 12:40:32 +0000977void
978Debugger::PrintAsync (const char *s, size_t len, bool is_stdout)
979{
980 lldb::StreamFileSP stream = is_stdout ? GetOutputFile() : GetErrorFile();
981 m_input_reader_stack.PrintAsync(stream.get(), s, len);
982}
Greg Clayton44d93782014-01-27 23:43:24 +0000983
984ConstString
985Debugger::GetTopIOHandlerControlSequence(char ch)
986{
987 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988}
989
Kate Stonea487aa42015-01-15 00:52:41 +0000990const char *
991Debugger::GetIOHandlerCommandPrefix()
992{
993 return m_input_reader_stack.GetTopIOHandlerCommandPrefix();
994}
995
996const char *
997Debugger::GetIOHandlerHelpPrologue()
998{
999 return m_input_reader_stack.GetTopIOHandlerHelpPrologue();
1000}
1001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002void
Greg Clayton44d93782014-01-27 23:43:24 +00001003Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
1004{
Greg Clayton44d93782014-01-27 23:43:24 +00001005 PushIOHandler (reader_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001006
Greg Clayton577508d2014-06-20 00:23:57 +00001007 IOHandlerSP top_reader_sp = reader_sp;
1008 while (top_reader_sp)
1009 {
Greg Clayton577508d2014-06-20 00:23:57 +00001010 top_reader_sp->Run();
Pavel Labath44464872015-05-27 12:40:32 +00001011
Greg Clayton577508d2014-06-20 00:23:57 +00001012 if (top_reader_sp.get() == reader_sp.get())
1013 {
1014 if (PopIOHandler (reader_sp))
1015 break;
1016 }
Pavel Labath44464872015-05-27 12:40:32 +00001017
Eugene Zelenkodf370552016-03-02 02:18:18 +00001018 while (true)
Greg Clayton577508d2014-06-20 00:23:57 +00001019 {
1020 top_reader_sp = m_input_reader_stack.Top();
1021 if (top_reader_sp && top_reader_sp->GetIsDone())
Pavel Labath44464872015-05-27 12:40:32 +00001022 PopIOHandler (top_reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +00001023 else
1024 break;
1025 }
1026 }
Greg Clayton44d93782014-01-27 23:43:24 +00001027}
1028
1029void
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001030Debugger::AdoptTopIOHandlerFilesIfInvalid(StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
Greg Clayton44d93782014-01-27 23:43:24 +00001031{
1032 // Before an IOHandler runs, it must have in/out/err streams.
1033 // This function is called when one ore more of the streams
Eugene Zelenkodf370552016-03-02 02:18:18 +00001034 // are nullptr. We use the top input reader's in/out/err streams,
Greg Clayton44d93782014-01-27 23:43:24 +00001035 // or fall back to the debugger file handles, or we fall back
1036 // onto stdin/stdout/stderr as a last resort.
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001037
1038 std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex());
1039 IOHandlerSP top_reader_sp(m_input_reader_stack.Top());
Greg Clayton44d93782014-01-27 23:43:24 +00001040 // If no STDIN has been set, then set it appropriately
1041 if (!in)
1042 {
1043 if (top_reader_sp)
1044 in = top_reader_sp->GetInputStreamFile();
1045 else
1046 in = GetInputFile();
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001047
Greg Clayton44d93782014-01-27 23:43:24 +00001048 // If there is nothing, use stdin
1049 if (!in)
1050 in = StreamFileSP(new StreamFile(stdin, false));
1051 }
1052 // If no STDOUT has been set, then set it appropriately
1053 if (!out)
1054 {
1055 if (top_reader_sp)
1056 out = top_reader_sp->GetOutputStreamFile();
1057 else
1058 out = GetOutputFile();
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001059
Greg Clayton44d93782014-01-27 23:43:24 +00001060 // If there is nothing, use stdout
1061 if (!out)
1062 out = StreamFileSP(new StreamFile(stdout, false));
1063 }
1064 // If no STDERR has been set, then set it appropriately
1065 if (!err)
1066 {
1067 if (top_reader_sp)
1068 err = top_reader_sp->GetErrorStreamFile();
1069 else
1070 err = GetErrorFile();
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001071
Greg Clayton44d93782014-01-27 23:43:24 +00001072 // If there is nothing, use stderr
1073 if (!err)
1074 err = StreamFileSP(new StreamFile(stdout, false));
Greg Clayton44d93782014-01-27 23:43:24 +00001075 }
1076}
1077
1078void
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001079Debugger::PushIOHandler(const IOHandlerSP &reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080{
1081 if (!reader_sp)
1082 return;
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001083
1084 std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex());
Pavel Labath44464872015-05-27 12:40:32 +00001085
1086 // Get the current top input reader...
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001087 IOHandlerSP top_reader_sp(m_input_reader_stack.Top());
1088
Greg Claytonb4874f12014-02-28 18:22:24 +00001089 // Don't push the same IO handler twice...
Pavel Labath44464872015-05-27 12:40:32 +00001090 if (reader_sp == top_reader_sp)
1091 return;
Greg Clayton44d93782014-01-27 23:43:24 +00001092
Pavel Labath44464872015-05-27 12:40:32 +00001093 // Push our new input reader
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001094 m_input_reader_stack.Push(reader_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001095 reader_sp->Activate();
1096
1097 // Interrupt the top input reader to it will exit its Run() function
1098 // and let this new input reader take over
1099 if (top_reader_sp)
1100 {
1101 top_reader_sp->Deactivate();
1102 top_reader_sp->Cancel();
Greg Claytonb4874f12014-02-28 18:22:24 +00001103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104}
1105
1106bool
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001107Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108{
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001109 if (!pop_reader_sp)
Pavel Labath44464872015-05-27 12:40:32 +00001110 return false;
1111
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001112 std::lock_guard<std::recursive_mutex> guard(m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113
1114 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001115 // read on the stack refresh its prompt and if there is one...
Pavel Labath44464872015-05-27 12:40:32 +00001116 if (m_input_reader_stack.IsEmpty())
1117 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118
Greg Clayton44d93782014-01-27 23:43:24 +00001119 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Greg Clayton66111032010-06-23 01:19:29 +00001120
Pavel Labath44464872015-05-27 12:40:32 +00001121 if (pop_reader_sp != reader_sp)
1122 return false;
1123
1124 reader_sp->Deactivate();
1125 reader_sp->Cancel();
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001126 m_input_reader_stack.Pop();
Pavel Labath44464872015-05-27 12:40:32 +00001127
1128 reader_sp = m_input_reader_stack.Top();
1129 if (reader_sp)
1130 reader_sp->Activate();
1131
1132 return true;
1133}
Greg Clayton44d93782014-01-27 23:43:24 +00001134
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001135StreamSP
1136Debugger::GetAsyncOutputStream ()
1137{
Pavel Labath44464872015-05-27 12:40:32 +00001138 return StreamSP (new StreamAsynchronousIO (*this, true));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001139}
1140
1141StreamSP
1142Debugger::GetAsyncErrorStream ()
1143{
Pavel Labath44464872015-05-27 12:40:32 +00001144 return StreamSP (new StreamAsynchronousIO (*this, false));
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001145}
1146
Greg Claytonc7bece562013-01-25 18:06:21 +00001147size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001148Debugger::GetNumDebuggers()
1149{
Greg Clayton6c42e062016-05-26 16:51:23 +00001150 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001151 {
Greg Clayton6c42e062016-05-26 16:51:23 +00001152 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1153 return g_debugger_list_ptr->size();
Greg Claytonc15f55e2012-03-30 20:53:46 +00001154 }
1155 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001156}
1157
1158lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001159Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001160{
1161 DebuggerSP debugger_sp;
1162
Greg Clayton6c42e062016-05-26 16:51:23 +00001163 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001164 {
Greg Clayton6c42e062016-05-26 16:51:23 +00001165 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1166 if (index < g_debugger_list_ptr->size())
1167 debugger_sp = g_debugger_list_ptr->at(index);
Greg Claytonc15f55e2012-03-30 20:53:46 +00001168 }
1169
Enrico Granata061858c2012-02-15 02:34:21 +00001170 return debugger_sp;
1171}
1172
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001173DebuggerSP
1174Debugger::FindDebuggerWithID (lldb::user_id_t id)
1175{
Greg Clayton4d122c42011-09-17 08:33:22 +00001176 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001177
Greg Clayton6c42e062016-05-26 16:51:23 +00001178 if (g_debugger_list_ptr && g_debugger_list_mutex_ptr)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001179 {
Greg Clayton6c42e062016-05-26 16:51:23 +00001180 std::lock_guard<std::recursive_mutex> guard(*g_debugger_list_mutex_ptr);
1181 DebuggerList::iterator pos, end = g_debugger_list_ptr->end();
1182 for (pos = g_debugger_list_ptr->begin (); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001183 {
Eugene Zelenkodf370552016-03-02 02:18:18 +00001184 if ((*pos)->GetID() == id)
Greg Claytonc15f55e2012-03-30 20:53:46 +00001185 {
1186 debugger_sp = *pos;
1187 break;
1188 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001189 }
1190 }
1191 return debugger_sp;
1192}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001193
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001194#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001195static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001196TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001197{
Eugene Zelenkodf370552016-03-02 02:18:18 +00001198 if (frame == nullptr)
Greg Clayton1b654882010-09-19 02:33:57 +00001199 return;
1200
1201 StreamString s;
1202 const char *prompt_format =
1203 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001204 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1205 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001206 "{process.id = '${process.id}'\n}"
1207 "{process.name = '${process.name}'\n}"
1208 "{process.file.basename = '${process.file.basename}'\n}"
1209 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1210 "{thread.id = '${thread.id}'\n}"
1211 "{thread.index = '${thread.index}'\n}"
1212 "{thread.name = '${thread.name}'\n}"
1213 "{thread.queue = '${thread.queue}'\n}"
1214 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1215 "{target.arch = '${target.arch}'\n}"
1216 "{module.file.basename = '${module.file.basename}'\n}"
1217 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1218 "{file.basename = '${file.basename}'\n}"
1219 "{file.fullpath = '${file.fullpath}'\n}"
1220 "{frame.index = '${frame.index}'\n}"
1221 "{frame.pc = '${frame.pc}'\n}"
1222 "{frame.sp = '${frame.sp}'\n}"
1223 "{frame.fp = '${frame.fp}'\n}"
1224 "{frame.flags = '${frame.flags}'\n}"
1225 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1226 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1227 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1228 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1229 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1230 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1231 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1232 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001233 "{function.changed = '${function.changed}'\n}"
1234 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001235 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001236 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001237 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001238 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001239 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001240 "{function.line-offset = '${function.line-offset}'\n}"
1241 "{function.pc-offset = '${function.pc-offset}'\n}"
1242 "{line.file.basename = '${line.file.basename}'\n}"
1243 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1244 "{line.number = '${line.number}'\n}"
1245 "{line.start-addr = '${line.start-addr}'\n}"
1246 "{line.end-addr = '${line.end-addr}'\n}"
1247;
1248
1249 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1250 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001251 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001252 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001253 {
1254 printf("%s\n", s.GetData());
1255 }
1256 else
1257 {
Greg Clayton1b654882010-09-19 02:33:57 +00001258 printf ("what we got: %s\n", s.GetData());
1259 }
1260}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001261#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001262
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001263bool
Greg Clayton554f68d2015-02-04 22:00:53 +00001264Debugger::FormatDisassemblerAddress (const FormatEntity::Entry *format,
Jason Molendaaff1b352014-10-10 23:07:36 +00001265 const SymbolContext *sc,
1266 const SymbolContext *prev_sc,
1267 const ExecutionContext *exe_ctx,
1268 const Address *addr,
1269 Stream &s)
1270{
Greg Clayton554f68d2015-02-04 22:00:53 +00001271 FormatEntity::Entry format_entry;
1272
Eugene Zelenkodf370552016-03-02 02:18:18 +00001273 if (format == nullptr)
Jason Molendaaff1b352014-10-10 23:07:36 +00001274 {
Eugene Zelenkodf370552016-03-02 02:18:18 +00001275 if (exe_ctx != nullptr && exe_ctx->HasTargetScope())
Greg Clayton554f68d2015-02-04 22:00:53 +00001276 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
Eugene Zelenkodf370552016-03-02 02:18:18 +00001277 if (format == nullptr)
Greg Clayton554f68d2015-02-04 22:00:53 +00001278 {
1279 FormatEntity::Parse("${addr}: ", format_entry);
1280 format = &format_entry;
1281 }
Jason Molendaaff1b352014-10-10 23:07:36 +00001282 }
1283 bool function_changed = false;
1284 bool initial_function = false;
1285 if (prev_sc && (prev_sc->function || prev_sc->symbol))
1286 {
1287 if (sc && (sc->function || sc->symbol))
1288 {
1289 if (prev_sc->symbol && sc->symbol)
1290 {
1291 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
1292 {
1293 function_changed = true;
1294 }
1295 }
1296 else if (prev_sc->function && sc->function)
1297 {
1298 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
1299 {
1300 function_changed = true;
1301 }
1302 }
1303 }
1304 }
1305 // The first context on a list of instructions will have a prev_sc that
1306 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
1307 // would return false. But we do get a prev_sc pointer.
1308 if ((sc && (sc->function || sc->symbol))
Eugene Zelenkodf370552016-03-02 02:18:18 +00001309 && prev_sc && (prev_sc->function == nullptr && prev_sc->symbol == nullptr))
Jason Molendaaff1b352014-10-10 23:07:36 +00001310 {
1311 initial_function = true;
1312 }
Eugene Zelenkodf370552016-03-02 02:18:18 +00001313 return FormatEntity::Format(*format, s, sc, exe_ctx, addr, nullptr, function_changed, initial_function);
Jason Molendaaff1b352014-10-10 23:07:36 +00001314}
1315
Jim Ingham228063c2012-02-21 02:23:08 +00001316void
1317Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1318{
Jim Ingham4f02b222012-02-22 22:49:20 +00001319 // For simplicity's sake, I am not going to deal with how to close down any
1320 // open logging streams, I just redirect everything from here on out to the
1321 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00001322 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
1323}
1324
1325bool
1326Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
1327{
Jim Ingham228063c2012-02-21 02:23:08 +00001328 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001329 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001330 {
1331 log_stream_sp = m_log_callback_stream_sp;
1332 // For now when using the callback mode you always get thread & timestamp.
1333 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1334 }
Eugene Zelenkodf370552016-03-02 02:18:18 +00001335 else if (log_file == nullptr || *log_file == '\0')
Jim Ingham228063c2012-02-21 02:23:08 +00001336 {
Greg Clayton44d93782014-01-27 23:43:24 +00001337 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00001338 }
1339 else
1340 {
1341 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00001342 if (pos != m_log_streams.end())
1343 log_stream_sp = pos->second.lock();
1344 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00001345 {
Pavel Labath8ac06992015-03-20 09:43:20 +00001346 uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate
1347 | File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
1348 if (! (log_options & LLDB_LOG_OPTION_APPEND))
1349 options |= File::eOpenOptionTruncate;
1350
1351 log_stream_sp.reset (new StreamFile (log_file, options));
Jim Ingham228063c2012-02-21 02:23:08 +00001352 m_log_streams[log_file] = log_stream_sp;
1353 }
Jim Ingham228063c2012-02-21 02:23:08 +00001354 }
Eugene Zelenkodf370552016-03-02 02:18:18 +00001355 assert(log_stream_sp);
Jim Ingham228063c2012-02-21 02:23:08 +00001356
1357 if (log_options == 0)
1358 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
1359
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +00001360 return Log::EnableLogChannel(log_stream_sp, log_options, channel, categories, error_stream);
Jim Ingham228063c2012-02-21 02:23:08 +00001361}
1362
Greg Clayton9585fbf2013-03-19 00:20:55 +00001363SourceManager &
1364Debugger::GetSourceManager ()
1365{
Eugene Zelenkodf370552016-03-02 02:18:18 +00001366 if (!m_source_manager_ap)
Greg Clayton9585fbf2013-03-19 00:20:55 +00001367 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
1368 return *m_source_manager_ap;
1369}
1370
Greg Clayton44d93782014-01-27 23:43:24 +00001371// This function handles events that were broadcast by the process.
1372void
1373Debugger::HandleBreakpointEvent (const EventSP &event_sp)
1374{
1375 using namespace lldb;
1376 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
1377
1378// if (event_type & eBreakpointEventTypeAdded
1379// || event_type & eBreakpointEventTypeRemoved
1380// || event_type & eBreakpointEventTypeEnabled
1381// || event_type & eBreakpointEventTypeDisabled
1382// || event_type & eBreakpointEventTypeCommandChanged
1383// || event_type & eBreakpointEventTypeConditionChanged
1384// || event_type & eBreakpointEventTypeIgnoreChanged
1385// || event_type & eBreakpointEventTypeLocationsResolved)
1386// {
1387// // Don't do anything about these events, since the breakpoint commands already echo these actions.
1388// }
1389//
1390 if (event_type & eBreakpointEventTypeLocationsAdded)
1391 {
1392 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
1393 if (num_new_locations > 0)
1394 {
1395 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
Pavel Labath44464872015-05-27 12:40:32 +00001396 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001397 if (output_sp)
1398 {
1399 output_sp->Printf("%d location%s added to breakpoint %d\n",
1400 num_new_locations,
1401 num_new_locations == 1 ? "" : "s",
1402 breakpoint->GetID());
Pavel Labath44464872015-05-27 12:40:32 +00001403 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001404 }
1405 }
1406 }
1407// else if (event_type & eBreakpointEventTypeLocationsRemoved)
1408// {
1409// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
1410// }
1411// else if (event_type & eBreakpointEventTypeLocationsResolved)
1412// {
1413// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
1414// }
1415}
1416
1417size_t
1418Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
1419{
1420 size_t total_bytes = 0;
Eugene Zelenkodf370552016-03-02 02:18:18 +00001421 if (stream == nullptr)
Greg Clayton44d93782014-01-27 23:43:24 +00001422 stream = GetOutputFile().get();
1423
1424 if (stream)
1425 {
1426 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
Eugene Zelenkodf370552016-03-02 02:18:18 +00001427 if (process == nullptr)
Greg Clayton44d93782014-01-27 23:43:24 +00001428 {
1429 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1430 if (target_sp)
1431 process = target_sp->GetProcessSP().get();
1432 }
1433 if (process)
1434 {
1435 Error error;
1436 size_t len;
1437 char stdio_buffer[1024];
1438 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1439 {
1440 stream->Write(stdio_buffer, len);
1441 total_bytes += len;
1442 }
1443 }
1444 stream->Flush();
1445 }
1446 return total_bytes;
1447}
1448
1449size_t
1450Debugger::GetProcessSTDERR (Process *process, Stream *stream)
1451{
1452 size_t total_bytes = 0;
Eugene Zelenkodf370552016-03-02 02:18:18 +00001453 if (stream == nullptr)
Greg Clayton44d93782014-01-27 23:43:24 +00001454 stream = GetOutputFile().get();
1455
1456 if (stream)
1457 {
1458 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
Eugene Zelenkodf370552016-03-02 02:18:18 +00001459 if (process == nullptr)
Greg Clayton44d93782014-01-27 23:43:24 +00001460 {
1461 TargetSP target_sp = GetTargetList().GetSelectedTarget();
1462 if (target_sp)
1463 process = target_sp->GetProcessSP().get();
1464 }
1465 if (process)
1466 {
1467 Error error;
1468 size_t len;
1469 char stdio_buffer[1024];
1470 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
1471 {
1472 stream->Write(stdio_buffer, len);
1473 total_bytes += len;
1474 }
1475 }
1476 stream->Flush();
1477 }
1478 return total_bytes;
1479}
1480
Greg Claytondc6224e2014-10-21 01:00:42 +00001481
Greg Clayton44d93782014-01-27 23:43:24 +00001482// This function handles events that were broadcast by the process.
1483void
1484Debugger::HandleProcessEvent (const EventSP &event_sp)
1485{
1486 using namespace lldb;
1487 const uint32_t event_type = event_sp->GetType();
1488 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00001489
Pavel Labath44464872015-05-27 12:40:32 +00001490 StreamSP output_stream_sp = GetAsyncOutputStream();
1491 StreamSP error_stream_sp = GetAsyncErrorStream();
Greg Clayton44d93782014-01-27 23:43:24 +00001492 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00001493
Greg Claytonb4874f12014-02-28 18:22:24 +00001494 if (!gui_enabled)
1495 {
1496 bool pop_process_io_handler = false;
1497 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00001498
Pavel Labath44464872015-05-27 12:40:32 +00001499 bool state_is_stopped = false;
1500 const bool got_state_changed = (event_type & Process::eBroadcastBitStateChanged) != 0;
1501 const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
1502 const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
1503 if (got_state_changed)
Greg Clayton44d93782014-01-27 23:43:24 +00001504 {
Pavel Labath44464872015-05-27 12:40:32 +00001505 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
1506 state_is_stopped = StateIsStoppedState(event_state, false);
Greg Claytonb4874f12014-02-28 18:22:24 +00001507 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001508
Pavel Labath44464872015-05-27 12:40:32 +00001509 // Display running state changes first before any STDIO
1510 if (got_state_changed && !state_is_stopped)
Greg Claytonb4874f12014-02-28 18:22:24 +00001511 {
Pavel Labath44464872015-05-27 12:40:32 +00001512 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
Greg Claytonb4874f12014-02-28 18:22:24 +00001513 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001514
Pavel Labath44464872015-05-27 12:40:32 +00001515 // Now display and STDOUT
1516 if (got_stdout || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001517 {
Pavel Labath44464872015-05-27 12:40:32 +00001518 GetProcessSTDOUT (process_sp.get(), output_stream_sp.get());
Greg Clayton44d93782014-01-27 23:43:24 +00001519 }
Greg Claytondc6224e2014-10-21 01:00:42 +00001520
Pavel Labath44464872015-05-27 12:40:32 +00001521 // Now display and STDERR
1522 if (got_stderr || got_state_changed)
Greg Claytonb4874f12014-02-28 18:22:24 +00001523 {
Pavel Labath44464872015-05-27 12:40:32 +00001524 GetProcessSTDERR (process_sp.get(), error_stream_sp.get());
Greg Claytonb4874f12014-02-28 18:22:24 +00001525 }
1526
Pavel Labath44464872015-05-27 12:40:32 +00001527 // Now display any stopped state changes after any STDIO
1528 if (got_state_changed && state_is_stopped)
1529 {
1530 Process::HandleProcessStateChangedEvent (event_sp, output_stream_sp.get(), pop_process_io_handler);
1531 }
1532
1533 output_stream_sp->Flush();
1534 error_stream_sp->Flush();
1535
Greg Claytonb4874f12014-02-28 18:22:24 +00001536 if (pop_process_io_handler)
1537 process_sp->PopProcessIOHandler();
1538 }
Greg Clayton44d93782014-01-27 23:43:24 +00001539}
1540
1541void
1542Debugger::HandleThreadEvent (const EventSP &event_sp)
1543{
1544 // At present the only thread event we handle is the Frame Changed event,
1545 // and all we do for that is just reprint the thread status for that thread.
1546 using namespace lldb;
1547 const uint32_t event_type = event_sp->GetType();
1548 if (event_type == Thread::eBroadcastBitStackChanged ||
1549 event_type == Thread::eBroadcastBitThreadSelected )
1550 {
1551 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
1552 if (thread_sp)
1553 {
Pavel Labath44464872015-05-27 12:40:32 +00001554 thread_sp->GetStatus(*GetAsyncOutputStream(), 0, 1, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00001555 }
1556 }
1557}
1558
1559bool
1560Debugger::IsForwardingEvents ()
1561{
1562 return (bool)m_forward_listener_sp;
1563}
1564
1565void
1566Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
1567{
1568 m_forward_listener_sp = listener_sp;
1569}
1570
1571void
1572Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
1573{
1574 m_forward_listener_sp.reset();
1575}
1576
1577
1578void
1579Debugger::DefaultEventHandler()
1580{
Jim Ingham583bbb12016-03-07 21:50:25 +00001581 ListenerSP listener_sp(GetListener());
Greg Clayton44d93782014-01-27 23:43:24 +00001582 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
1583 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
1584 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
1585 BroadcastEventSpec target_event_spec (broadcaster_class_target,
1586 Target::eBroadcastBitBreakpointChanged);
1587
1588 BroadcastEventSpec process_event_spec (broadcaster_class_process,
1589 Process::eBroadcastBitStateChanged |
1590 Process::eBroadcastBitSTDOUT |
1591 Process::eBroadcastBitSTDERR);
1592
1593 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
1594 Thread::eBroadcastBitStackChanged |
1595 Thread::eBroadcastBitThreadSelected );
1596
Jim Ingham583bbb12016-03-07 21:50:25 +00001597 listener_sp->StartListeningForEventSpec (m_broadcaster_manager_sp, target_event_spec);
1598 listener_sp->StartListeningForEventSpec (m_broadcaster_manager_sp, process_event_spec);
1599 listener_sp->StartListeningForEventSpec (m_broadcaster_manager_sp, thread_event_spec);
1600 listener_sp->StartListeningForEvents (m_command_interpreter_ap.get(),
Greg Clayton44d93782014-01-27 23:43:24 +00001601 CommandInterpreter::eBroadcastBitQuitCommandReceived |
1602 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
1603 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
Greg Claytonafa91e332014-12-01 22:41:27 +00001604
1605 // Let the thread that spawned us know that we have started up and
1606 // that we are now listening to all required events so no events get missed
1607 m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
1608
Greg Clayton44d93782014-01-27 23:43:24 +00001609 bool done = false;
1610 while (!done)
1611 {
Greg Clayton44d93782014-01-27 23:43:24 +00001612 EventSP event_sp;
Jim Ingham583bbb12016-03-07 21:50:25 +00001613 if (listener_sp->WaitForEvent(nullptr, event_sp))
Greg Clayton44d93782014-01-27 23:43:24 +00001614 {
1615 if (event_sp)
1616 {
1617 Broadcaster *broadcaster = event_sp->GetBroadcaster();
1618 if (broadcaster)
1619 {
1620 uint32_t event_type = event_sp->GetType();
1621 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
1622 if (broadcaster_class == broadcaster_class_process)
1623 {
1624 HandleProcessEvent (event_sp);
1625 }
1626 else if (broadcaster_class == broadcaster_class_target)
1627 {
1628 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
1629 {
1630 HandleBreakpointEvent (event_sp);
1631 }
1632 }
1633 else if (broadcaster_class == broadcaster_class_thread)
1634 {
1635 HandleThreadEvent (event_sp);
1636 }
1637 else if (broadcaster == m_command_interpreter_ap.get())
1638 {
1639 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
1640 {
1641 done = true;
1642 }
1643 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
1644 {
1645 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1646 if (data && data[0])
1647 {
Pavel Labath44464872015-05-27 12:40:32 +00001648 StreamSP error_sp (GetAsyncErrorStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001649 if (error_sp)
1650 {
Greg Clayton44d93782014-01-27 23:43:24 +00001651 error_sp->PutCString(data);
1652 error_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001653 }
1654 }
1655 }
1656 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
1657 {
1658 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
1659 if (data && data[0])
1660 {
Pavel Labath44464872015-05-27 12:40:32 +00001661 StreamSP output_sp (GetAsyncOutputStream());
Greg Clayton44d93782014-01-27 23:43:24 +00001662 if (output_sp)
1663 {
Greg Clayton44d93782014-01-27 23:43:24 +00001664 output_sp->PutCString(data);
1665 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00001666 }
1667 }
1668 }
1669 }
1670 }
1671
1672 if (m_forward_listener_sp)
1673 m_forward_listener_sp->AddEvent(event_sp);
1674 }
1675 }
1676 }
1677}
1678
1679lldb::thread_result_t
1680Debugger::EventHandlerThread (lldb::thread_arg_t arg)
1681{
1682 ((Debugger *)arg)->DefaultEventHandler();
1683 return NULL;
1684}
1685
1686bool
1687Debugger::StartEventHandlerThread()
1688{
Zachary Turneracee96a2014-09-23 18:32:09 +00001689 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00001690 {
Greg Claytonafa91e332014-12-01 22:41:27 +00001691 // We must synchronize with the DefaultEventHandler() thread to ensure
1692 // it is up and running and listening to events before we return from
1693 // this function. We do this by listening to events for the
1694 // eBroadcastBitEventThreadIsListening from the m_sync_broadcaster
Jim Ingham583bbb12016-03-07 21:50:25 +00001695 ListenerSP listener_sp(Listener::MakeListener("lldb.debugger.event-handler"));
1696 listener_sp->StartListeningForEvents(&m_sync_broadcaster, eBroadcastBitEventThreadIsListening);
Greg Claytonafa91e332014-12-01 22:41:27 +00001697
Zachary Turner7c2896a2014-10-24 22:06:29 +00001698 // Use larger 8MB stack for this thread
Eugene Zelenkodf370552016-03-02 02:18:18 +00001699 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler",
1700 EventHandlerThread,
Greg Claytonafa91e332014-12-01 22:41:27 +00001701 this,
Eugene Zelenkodf370552016-03-02 02:18:18 +00001702 nullptr,
Zachary Turner7c2896a2014-10-24 22:06:29 +00001703 g_debugger_event_thread_stack_bytes);
Greg Claytonafa91e332014-12-01 22:41:27 +00001704
1705 // Make sure DefaultEventHandler() is running and listening to events before we return
1706 // from this function. We are only listening for events of type
1707 // eBroadcastBitEventThreadIsListening so we don't need to check the event, we just need
Eugene Zelenkodf370552016-03-02 02:18:18 +00001708 // to wait an infinite amount of time for it (nullptr timeout as the first parameter)
Greg Claytonafa91e332014-12-01 22:41:27 +00001709 lldb::EventSP event_sp;
Jim Ingham583bbb12016-03-07 21:50:25 +00001710 listener_sp->WaitForEvent(nullptr, event_sp);
Greg Clayton807b6b32014-10-15 18:03:59 +00001711 }
Zachary Turneracee96a2014-09-23 18:32:09 +00001712 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001713}
1714
1715void
1716Debugger::StopEventHandlerThread()
1717{
Zachary Turneracee96a2014-09-23 18:32:09 +00001718 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001719 {
1720 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00001721 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001722 }
1723}
1724
Greg Clayton44d93782014-01-27 23:43:24 +00001725lldb::thread_result_t
1726Debugger::IOHandlerThread (lldb::thread_arg_t arg)
1727{
1728 Debugger *debugger = (Debugger *)arg;
Siva Chandra9aaab552015-02-26 19:26:36 +00001729 debugger->ExecuteIOHandlers();
Greg Clayton44d93782014-01-27 23:43:24 +00001730 debugger->StopEventHandlerThread();
1731 return NULL;
1732}
1733
1734bool
Sean Callanan66810412015-10-19 23:11:07 +00001735Debugger::HasIOHandlerThread()
1736{
1737 return m_io_handler_thread.IsJoinable();
1738}
1739
1740bool
Greg Clayton44d93782014-01-27 23:43:24 +00001741Debugger::StartIOHandlerThread()
1742{
Zachary Turneracee96a2014-09-23 18:32:09 +00001743 if (!m_io_handler_thread.IsJoinable())
Eugene Zelenkodf370552016-03-02 02:18:18 +00001744 m_io_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.io-handler",
1745 IOHandlerThread,
1746 this,
1747 nullptr,
1748 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00001749 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00001750}
1751
1752void
1753Debugger::StopIOHandlerThread()
1754{
Zachary Turneracee96a2014-09-23 18:32:09 +00001755 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00001756 {
1757 if (m_input_file_sp)
1758 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00001759 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00001760 }
1761}
1762
Sean Callanan66810412015-10-19 23:11:07 +00001763void
1764Debugger::JoinIOHandlerThread()
1765{
1766 if (HasIOHandlerThread())
1767 {
1768 thread_result_t result;
1769 m_io_handler_thread.Join(&result);
1770 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
1771 }
1772}
1773
Jim Ingham893c9322014-11-22 01:42:44 +00001774Target *
1775Debugger::GetDummyTarget()
1776{
1777 return m_target_list.GetDummyTarget (*this).get();
1778}
1779
1780Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001781Debugger::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001782{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001783 Target *target = nullptr;
1784 if (!prefer_dummy)
1785 {
1786 target = m_target_list.GetSelectedTarget().get();
1787 if (target)
1788 return target;
1789 }
1790
Jim Ingham893c9322014-11-22 01:42:44 +00001791 return GetDummyTarget();
1792}
Greg Clayton44d93782014-01-27 23:43:24 +00001793
Sean Callanan3e7e9152015-10-20 00:23:46 +00001794Error
1795Debugger::RunREPL (LanguageType language, const char *repl_options)
1796{
1797 Error err;
1798 FileSpec repl_executable;
Sean Callanan3e7e9152015-10-20 00:23:46 +00001799
Sean Callanan97f84e82015-10-21 19:31:17 +00001800 if (language == eLanguageTypeUnknown)
1801 {
1802 std::set<LanguageType> repl_languages;
1803
1804 Language::GetLanguagesSupportingREPLs(repl_languages);
1805
1806 if (repl_languages.size() == 1)
1807 {
1808 language = *repl_languages.begin();
1809 }
Eugene Zelenkodf370552016-03-02 02:18:18 +00001810 else if (repl_languages.empty())
Sean Callanan97f84e82015-10-21 19:31:17 +00001811 {
Kate Stone07612022016-04-21 00:56:08 +00001812 err.SetErrorStringWithFormat("LLDB isn't configured with REPL support for any languages.");
Sean Callanan97f84e82015-10-21 19:31:17 +00001813 return err;
1814 }
1815 else
1816 {
1817 err.SetErrorStringWithFormat("Multiple possible REPL languages. Please specify a language.");
1818 return err;
1819 }
1820 }
1821
Sean Callanan3e7e9152015-10-20 00:23:46 +00001822 Target *const target = nullptr; // passing in an empty target means the REPL must create one
1823
Sean Callanan3b682de2015-10-21 00:28:44 +00001824 REPLSP repl_sp(REPL::Create(err, language, this, target, repl_options));
Sean Callanan3e7e9152015-10-20 00:23:46 +00001825
1826 if (!err.Success())
1827 {
1828 return err;
1829 }
1830
1831 if (!repl_sp)
1832 {
1833 err.SetErrorStringWithFormat("couldn't find a REPL for %s", Language::GetNameForLanguageType(language));
1834 return err;
1835 }
1836
1837 repl_sp->SetCompilerOptions(repl_options);
1838 repl_sp->RunLoop();
1839
1840 return err;
1841}