blob: 11328e4a53f2a70e71e836c75b97c08d322fdb58 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Debugger.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Clayton4a33d312011-06-23 17:59:56 +000012#include "lldb/Core/Debugger.h"
13
14#include <map>
15
Enrico Granata4becb372011-06-29 22:27:15 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/Type.h"
Jason Molenda705b1802014-06-13 02:37:02 +000018#include "llvm/ADT/StringRef.h"
Enrico Granata4becb372011-06-29 22:27:15 +000019
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/lldb-private.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000022#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000023#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000026#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000027#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000028#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000029#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000031#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000032#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000033#include "lldb/DataFormatters/DataVisualization.h"
34#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000035#include "lldb/DataFormatters/TypeSummary.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000036#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000037#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000038#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000039#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000040#include "lldb/Interpreter/CommandInterpreter.h"
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/ClangASTContext.h"
44#include "lldb/Symbol/CompileUnit.h"
45#include "lldb/Symbol/Function.h"
46#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000047#include "lldb/Symbol/VariableList.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000048#include "lldb/Target/CPPLanguageRuntime.h"
49#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Target/TargetList.h"
51#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000052#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000053#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000054#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000055#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000057#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Zachary Turner58a559c2014-08-27 20:15:09 +000059#include "llvm/Support/DynamicLibrary.h"
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061using namespace lldb;
62using namespace lldb_private;
63
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
Greg Clayton1b654882010-09-19 02:33:57 +000065static uint32_t g_shared_debugger_refcount = 0;
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
71static Mutex &
72GetDebuggerListMutex ()
73{
74 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
75 return g_mutex;
76}
77
78typedef std::vector<DebuggerSP> DebuggerList;
79
80static DebuggerList &
81GetDebuggerList()
82{
83 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000084 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000085 static DebuggerList g_list;
86 return g_list;
87}
Greg Claytone372b982011-11-21 21:44:34 +000088
89OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000090g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000091{
Greg Clayton67cc0632012-08-22 17:17:09 +000092 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
93 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
94 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000095 { 0, NULL, NULL }
96};
97
Greg Clayton67cc0632012-08-22 17:17:09 +000098OptionEnumValueElement
99g_language_enumerators[] =
100{
101 { eScriptLanguageNone, "none", "Disable scripting languages."},
102 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
103 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +0000104 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000105};
Greg Claytone372b982011-11-21 21:44:34 +0000106
Greg Clayton67cc0632012-08-22 17:17:09 +0000107#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
108#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
109
Michael Sartain0769b2b2013-07-30 16:44:36 +0000110#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000111 "{, ${frame.pc}}"\
112 MODULE_WITH_FUNC\
113 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000114 "{, name = '${thread.name}'}"\
115 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000116 "{, activity = '${thread.info.activity.name}'}" \
117 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000118 "{, stop reason = ${thread.stop-reason}}"\
119 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000120 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000121 "\\n"
122
123#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
124 MODULE_WITH_FUNC\
125 FILE_AND_LINE\
126 "\\n"
127
Jason Molendaaff1b352014-10-10 23:07:36 +0000128#define DEFAULT_DISASSEMBLY_FORMAT "${addr-file-or-load} <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>: "
Greg Clayton67cc0632012-08-22 17:17:09 +0000129
130
Greg Clayton754a9362012-08-23 00:22:02 +0000131static PropertyDefinition
132g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000133{
134{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
Jason Molendaaff1b352014-10-10 23:07:36 +0000135{ "disassembly-format", OptionValue::eTypeString , true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000136{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
137{ "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
Greg Clayton4c054102012-09-01 00:38:36 +0000138{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000139{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
140{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
141{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
142{ "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." },
143{ "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." },
144{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
145{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
146{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000147{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
Enrico Granata90a8db32013-10-31 21:01:07 +0000148{ "auto-one-line-summaries", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000149
150 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000151};
152
153enum
154{
155 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000156 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000157 ePropertyFrameFormat,
158 ePropertyNotiftVoid,
159 ePropertyPrompt,
160 ePropertyScriptLanguage,
161 ePropertyStopDisassemblyCount,
162 ePropertyStopDisassemblyDisplay,
163 ePropertyStopLineCountAfter,
164 ePropertyStopLineCountBefore,
165 ePropertyTerminalWidth,
166 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000167 ePropertyUseExternalEditor,
168 ePropertyUseColor,
Enrico Granata90a8db32013-10-31 21:01:07 +0000169 ePropertyAutoOneLineSummaries
Greg Clayton67cc0632012-08-22 17:17:09 +0000170};
171
Greg Clayton5fb8f792013-12-02 19:35:49 +0000172Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000173
174Error
175Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
176 VarSetOperationType op,
177 const char *property_path,
178 const char *value)
179{
Enrico Granata84a53df2013-05-20 22:29:23 +0000180 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
181 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000182 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000183 if (is_load_script && exe_ctx->GetTargetSP())
184 {
185 target_sp = exe_ctx->GetTargetSP();
186 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
187 }
Greg Clayton4c054102012-09-01 00:38:36 +0000188 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
189 if (error.Success())
190 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000191 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000192 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
193 {
194 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000195 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
196 if (str.length())
197 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000198 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000199 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
200 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
201 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000202 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
203 {
204 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
205 SetPrompt (GetPrompt());
206 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000207 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000208 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000209 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000210 {
211 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000212 StreamString feedback_stream;
213 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000214 {
Greg Clayton44d93782014-01-27 23:43:24 +0000215 StreamFileSP stream_sp (GetErrorFile());
216 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000217 {
Greg Clayton44d93782014-01-27 23:43:24 +0000218 for (auto error : errors)
219 {
220 stream_sp->Printf("%s\n",error.AsCString());
221 }
222 if (feedback_stream.GetSize())
223 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000224 }
225 }
226 }
227 }
Greg Clayton4c054102012-09-01 00:38:36 +0000228 }
229 return error;
230}
231
Greg Clayton67cc0632012-08-22 17:17:09 +0000232bool
233Debugger::GetAutoConfirm () const
234{
235 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000236 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000237}
238
239const char *
Jason Molendaaff1b352014-10-10 23:07:36 +0000240Debugger::GetDisassemblyFormat() const
241{
242 const uint32_t idx = ePropertyDisassemblyFormat;
243 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
244}
245
246const char *
Greg Clayton67cc0632012-08-22 17:17:09 +0000247Debugger::GetFrameFormat() const
248{
249 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000250 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000251}
252
253bool
254Debugger::GetNotifyVoid () const
255{
256 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000257 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000258}
259
260const char *
261Debugger::GetPrompt() const
262{
263 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000264 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000265}
266
267void
268Debugger::SetPrompt(const char *p)
269{
270 const uint32_t idx = ePropertyPrompt;
271 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
272 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000273 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
274 if (str.length())
275 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000276 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000277}
278
279const char *
280Debugger::GetThreadFormat() const
281{
282 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000283 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000284}
285
286lldb::ScriptLanguage
287Debugger::GetScriptLanguage() const
288{
289 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000290 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000291}
292
293bool
294Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
295{
296 const uint32_t idx = ePropertyScriptLanguage;
297 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
298}
299
300uint32_t
301Debugger::GetTerminalWidth () const
302{
303 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000304 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000305}
306
307bool
308Debugger::SetTerminalWidth (uint32_t term_width)
309{
310 const uint32_t idx = ePropertyTerminalWidth;
311 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
312}
313
314bool
315Debugger::GetUseExternalEditor () const
316{
317 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000318 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000319}
320
321bool
322Debugger::SetUseExternalEditor (bool b)
323{
324 const uint32_t idx = ePropertyUseExternalEditor;
325 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
326}
327
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000328bool
329Debugger::GetUseColor () const
330{
331 const uint32_t idx = ePropertyUseColor;
332 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
333}
334
335bool
336Debugger::SetUseColor (bool b)
337{
338 const uint32_t idx = ePropertyUseColor;
339 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
340 SetPrompt (GetPrompt());
341 return ret;
342}
343
Greg Clayton67cc0632012-08-22 17:17:09 +0000344uint32_t
345Debugger::GetStopSourceLineCount (bool before) const
346{
347 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000348 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000349}
350
351Debugger::StopDisassemblyType
352Debugger::GetStopDisassemblyDisplay () const
353{
354 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000355 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000356}
357
358uint32_t
359Debugger::GetDisassemblyLineCount () const
360{
361 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000362 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000363}
Greg Claytone372b982011-11-21 21:44:34 +0000364
Enrico Granata553fad52013-10-25 23:09:40 +0000365bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000366Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000367{
Enrico Granata90a8db32013-10-31 21:01:07 +0000368 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000369 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
370
371}
372
Greg Clayton1b654882010-09-19 02:33:57 +0000373#pragma mark Debugger
374
Greg Clayton67cc0632012-08-22 17:17:09 +0000375//const DebuggerPropertiesSP &
376//Debugger::GetSettings() const
377//{
378// return m_properties_sp;
379//}
380//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000381
Caroline Tice2f88aad2011-01-14 00:29:16 +0000382int
383Debugger::TestDebuggerRefCount ()
384{
385 return g_shared_debugger_refcount;
386}
387
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000389Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000391 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000392 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000393 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
396void
397Debugger::Terminate ()
398{
Greg Clayton66111032010-06-23 01:19:29 +0000399 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 {
Greg Clayton66111032010-06-23 01:19:29 +0000401 g_shared_debugger_refcount--;
402 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 {
Greg Claytondbe54502010-11-19 03:46:01 +0000404 lldb_private::WillTerminate();
405 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000406
407 // Clear our master list of debugger objects
408 Mutex::Locker locker (GetDebuggerListMutex ());
409 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 }
412}
413
Caroline Tice20bd37f2011-03-10 22:14:10 +0000414void
415Debugger::SettingsInitialize ()
416{
Greg Clayton6920b522012-08-22 18:39:03 +0000417 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000418}
419
420void
421Debugger::SettingsTerminate ()
422{
Greg Clayton6920b522012-08-22 18:39:03 +0000423 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000424}
425
Enrico Granata21dfcd92012-09-28 23:57:51 +0000426bool
Enrico Granatae743c782013-04-24 21:29:08 +0000427Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000428{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000429 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000430 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000431 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
432 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000433 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000434 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000435 return true;
436 }
Enrico Granatae743c782013-04-24 21:29:08 +0000437 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000438 else
Enrico Granatae743c782013-04-24 21:29:08 +0000439 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000440 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
441 // and if the public API layer isn't available (code is linking against
442 // all of the internal LLDB static libraries), then we can't load plugins
443 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000444 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000445 return false;
446}
447
448static FileSpec::EnumerateDirectoryResult
449LoadPluginCallback
450(
451 void *baton,
452 FileSpec::FileType file_type,
453 const FileSpec &file_spec
454 )
455{
456 Error error;
457
458 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000459 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000460
461 if (!baton)
462 return FileSpec::eEnumerateDirectoryResultQuit;
463
464 Debugger *debugger = (Debugger*)baton;
465
466 // If we have a regular file, a symbolic link or unknown file type, try
467 // and process the file. We must handle unknown as sometimes the directory
468 // enumeration might be enumerating a file system that doesn't have correct
469 // file type information.
470 if (file_type == FileSpec::eFileTypeRegular ||
471 file_type == FileSpec::eFileTypeSymbolicLink ||
472 file_type == FileSpec::eFileTypeUnknown )
473 {
474 FileSpec plugin_file_spec (file_spec);
475 plugin_file_spec.ResolvePath ();
476
Michael Sartain3cf443d2013-07-17 00:26:30 +0000477 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
478 plugin_file_spec.GetFileNameExtension() != g_solibext)
479 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000480 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000481 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000482
Enrico Granatae743c782013-04-24 21:29:08 +0000483 Error plugin_load_error;
484 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000485
486 return FileSpec::eEnumerateDirectoryResultNext;
487 }
488
489 else if (file_type == FileSpec::eFileTypeUnknown ||
490 file_type == FileSpec::eFileTypeDirectory ||
491 file_type == FileSpec::eFileTypeSymbolicLink )
492 {
493 // Try and recurse into anything that a directory or symbolic link.
494 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000495 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000496 // information.
497 return FileSpec::eEnumerateDirectoryResultEnter;
498 }
499
500 return FileSpec::eEnumerateDirectoryResultNext;
501}
502
503void
504Debugger::InstanceInitialize ()
505{
506 FileSpec dir_spec;
507 const bool find_directories = true;
508 const bool find_files = true;
509 const bool find_other = true;
510 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000511 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000512 {
513 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
514 {
515 FileSpec::EnumerateDirectory (dir_path,
516 find_directories,
517 find_files,
518 find_other,
519 LoadPluginCallback,
520 this);
521 }
522 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000523
524 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000525 {
526 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
527 {
528 FileSpec::EnumerateDirectory (dir_path,
529 find_directories,
530 find_files,
531 find_other,
532 LoadPluginCallback,
533 this);
534 }
535 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000536
537 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000538}
539
Greg Clayton66111032010-06-23 01:19:29 +0000540DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000541Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000542{
Jim Ingham228063c2012-02-21 02:23:08 +0000543 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000544 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000545 {
546 Mutex::Locker locker (GetDebuggerListMutex ());
547 GetDebuggerList().push_back(debugger_sp);
548 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000549 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000550 return debugger_sp;
551}
552
Caroline Ticee02657b2011-01-22 01:02:07 +0000553void
Greg Clayton4d122c42011-09-17 08:33:22 +0000554Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000555{
556 if (debugger_sp.get() == NULL)
557 return;
558
Jim Ingham8314c522011-09-15 21:36:42 +0000559 debugger_sp->Clear();
560
Greg Claytonc15f55e2012-03-30 20:53:46 +0000561 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000562 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000563 Mutex::Locker locker (GetDebuggerListMutex ());
564 DebuggerList &debugger_list = GetDebuggerList ();
565 DebuggerList::iterator pos, end = debugger_list.end();
566 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000567 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000568 if ((*pos).get() == debugger_sp.get())
569 {
570 debugger_list.erase (pos);
571 return;
572 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000573 }
574 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000575}
576
Greg Clayton4d122c42011-09-17 08:33:22 +0000577DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000578Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
579{
Greg Clayton4d122c42011-09-17 08:33:22 +0000580 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000581 if (g_shared_debugger_refcount > 0)
582 {
583 Mutex::Locker locker (GetDebuggerListMutex ());
584 DebuggerList &debugger_list = GetDebuggerList();
585 DebuggerList::iterator pos, end = debugger_list.end();
586
587 for (pos = debugger_list.begin(); pos != end; ++pos)
588 {
589 if ((*pos).get()->m_instance_name == instance_name)
590 {
591 debugger_sp = *pos;
592 break;
593 }
594 }
595 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000596 return debugger_sp;
597}
Greg Clayton66111032010-06-23 01:19:29 +0000598
599TargetSP
600Debugger::FindTargetWithProcessID (lldb::pid_t pid)
601{
Greg Clayton4d122c42011-09-17 08:33:22 +0000602 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000603 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000604 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000605 Mutex::Locker locker (GetDebuggerListMutex ());
606 DebuggerList &debugger_list = GetDebuggerList();
607 DebuggerList::iterator pos, end = debugger_list.end();
608 for (pos = debugger_list.begin(); pos != end; ++pos)
609 {
610 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
611 if (target_sp)
612 break;
613 }
Greg Clayton66111032010-06-23 01:19:29 +0000614 }
615 return target_sp;
616}
617
Greg Claytone4e45922011-11-16 05:37:56 +0000618TargetSP
619Debugger::FindTargetWithProcess (Process *process)
620{
621 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000622 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000623 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000624 Mutex::Locker locker (GetDebuggerListMutex ());
625 DebuggerList &debugger_list = GetDebuggerList();
626 DebuggerList::iterator pos, end = debugger_list.end();
627 for (pos = debugger_list.begin(); pos != end; ++pos)
628 {
629 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
630 if (target_sp)
631 break;
632 }
Greg Claytone4e45922011-11-16 05:37:56 +0000633 }
634 return target_sp;
635}
636
Jason Molendae6481c72014-09-12 01:50:46 +0000637Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
638 UserID(g_unique_id++),
639 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
640 m_input_file_sp(new StreamFile(stdin, false)),
641 m_output_file_sp(new StreamFile(stdout, false)),
642 m_error_file_sp(new StreamFile(stderr, false)),
643 m_terminal_state(),
644 m_target_list(*this),
645 m_platform_list(),
646 m_listener("lldb.Debugger"),
647 m_source_manager_ap(),
648 m_source_file_cache(),
649 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
650 m_input_reader_stack(),
651 m_instance_name(),
652 m_loaded_plugins()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653{
Greg Clayton67cc0632012-08-22 17:17:09 +0000654 char instance_cstr[256];
655 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
656 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000657 if (log_callback)
658 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000659 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000660 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000661 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000662 assert (default_platform_sp.get());
663 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000664
Greg Clayton754a9362012-08-23 00:22:02 +0000665 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000666 m_collection_sp->AppendProperty (ConstString("target"),
667 ConstString("Settings specify to debugging targets."),
668 true,
669 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000670 if (m_command_interpreter_ap.get())
671 {
672 m_collection_sp->AppendProperty (ConstString("interpreter"),
673 ConstString("Settings specify to the debugger's command interpreter."),
674 true,
675 m_command_interpreter_ap->GetValueProperties());
676 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000677 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
678 term_width->SetMinimumValue(10);
679 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000680
681 // Turn off use-color if this is a dumb terminal.
682 const char *term = getenv ("TERM");
683 if (term && !strcmp (term, "dumb"))
684 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685}
686
687Debugger::~Debugger ()
688{
Jim Ingham8314c522011-09-15 21:36:42 +0000689 Clear();
690}
691
692void
693Debugger::Clear()
694{
Greg Clayton44d93782014-01-27 23:43:24 +0000695 ClearIOHandlers();
696 StopIOHandlerThread();
697 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000698 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000699 int num_targets = m_target_list.GetNumTargets();
700 for (int i = 0; i < num_targets; i++)
701 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000702 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
703 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000704 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000705 ProcessSP process_sp (target_sp->GetProcessSP());
706 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000707 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000708 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000709 }
Greg Clayton66111032010-06-23 01:19:29 +0000710 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000711 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000712
713 // Close the input file _before_ we close the input read communications class
714 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000715 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000716 if (m_input_file_sp)
717 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000718
719 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000720}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721
722bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000723Debugger::GetCloseInputOnEOF () const
724{
Greg Clayton44d93782014-01-27 23:43:24 +0000725// return m_input_comm.GetCloseOnEOF();
726 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000727}
728
729void
730Debugger::SetCloseInputOnEOF (bool b)
731{
Greg Clayton44d93782014-01-27 23:43:24 +0000732// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000733}
734
735bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736Debugger::GetAsyncExecution ()
737{
Greg Clayton66111032010-06-23 01:19:29 +0000738 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739}
740
741void
742Debugger::SetAsyncExecution (bool async_execution)
743{
Greg Clayton66111032010-06-23 01:19:29 +0000744 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745}
746
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747
748void
749Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
750{
Greg Clayton44d93782014-01-27 23:43:24 +0000751 if (m_input_file_sp)
752 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
753 else
754 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
755
756 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000757 if (in_file.IsValid() == false)
758 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759
Jim Inghamc5917d92012-11-30 20:23:19 +0000760 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
761 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764void
765Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
766{
Greg Clayton44d93782014-01-27 23:43:24 +0000767 if (m_output_file_sp)
768 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
769 else
770 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
771
772 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000773 if (out_file.IsValid() == false)
774 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000775
Enrico Granatab5887262012-10-29 21:18:03 +0000776 // do not create the ScriptInterpreter just for setting the output file handle
777 // as the constructor will know how to do the right thing on its own
778 const bool can_create = false;
779 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
780 if (script_interpreter)
781 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782}
783
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784void
785Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
786{
Greg Clayton44d93782014-01-27 23:43:24 +0000787 if (m_error_file_sp)
788 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
789 else
790 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
791
792 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000793 if (err_file.IsValid() == false)
794 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795}
796
Jim Inghamc5917d92012-11-30 20:23:19 +0000797void
798Debugger::SaveInputTerminalState ()
799{
Greg Clayton44d93782014-01-27 23:43:24 +0000800 if (m_input_file_sp)
801 {
802 File &in_file = m_input_file_sp->GetFile();
803 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
804 m_terminal_state.Save(in_file.GetDescriptor(), true);
805 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000806}
807
808void
809Debugger::RestoreInputTerminalState ()
810{
811 m_terminal_state.Restore();
812}
813
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000815Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816{
817 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000818 TargetSP target_sp(GetSelectedTarget());
819 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820
821 if (target_sp)
822 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000823 ProcessSP process_sp (target_sp->GetProcessSP());
824 exe_ctx.SetProcessSP (process_sp);
825 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000827 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
828 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000830 exe_ctx.SetThreadSP (thread_sp);
831 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
832 if (exe_ctx.GetFramePtr() == NULL)
833 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834 }
835 }
836 }
837 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838}
839
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840void
Caroline Ticeefed6132010-11-19 20:47:54 +0000841Debugger::DispatchInputInterrupt ()
842{
Greg Clayton44d93782014-01-27 23:43:24 +0000843 Mutex::Locker locker (m_input_reader_stack.GetMutex());
844 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000845 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000846 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000847}
848
849void
850Debugger::DispatchInputEndOfFile ()
851{
Greg Clayton44d93782014-01-27 23:43:24 +0000852 Mutex::Locker locker (m_input_reader_stack.GetMutex());
853 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000854 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000855 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000856}
857
858void
Greg Clayton44d93782014-01-27 23:43:24 +0000859Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000860{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000861 // 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 +0000862 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000863 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000864 {
Greg Clayton44d93782014-01-27 23:43:24 +0000865 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000866 if (reader_sp)
867 {
Greg Clayton44d93782014-01-27 23:43:24 +0000868 m_input_reader_stack.Pop();
869 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000870 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000871 }
872 }
873}
874
875void
Greg Clayton44d93782014-01-27 23:43:24 +0000876Debugger::ExecuteIOHanders()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000877{
Caroline Tice9088b062011-05-09 23:06:58 +0000878
Greg Clayton44d93782014-01-27 23:43:24 +0000879 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880 {
Greg Clayton44d93782014-01-27 23:43:24 +0000881 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 if (!reader_sp)
883 break;
884
Greg Clayton44d93782014-01-27 23:43:24 +0000885 reader_sp->Activate();
886 reader_sp->Run();
887 reader_sp->Deactivate();
888
889 // Remove all input readers that are done from the top of the stack
890 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891 {
Greg Clayton44d93782014-01-27 23:43:24 +0000892 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
893 if (top_reader_sp && top_reader_sp->GetIsDone())
894 m_input_reader_stack.Pop();
895 else
896 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 }
898 }
Greg Clayton44d93782014-01-27 23:43:24 +0000899 ClearIOHandlers();
900}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901
Greg Clayton44d93782014-01-27 23:43:24 +0000902bool
903Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
904{
905 return m_input_reader_stack.IsTop (reader_sp);
906}
907
908
909ConstString
910Debugger::GetTopIOHandlerControlSequence(char ch)
911{
912 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913}
914
915void
Greg Clayton44d93782014-01-27 23:43:24 +0000916Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
917{
Greg Clayton44d93782014-01-27 23:43:24 +0000918 PushIOHandler (reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +0000919
920 IOHandlerSP top_reader_sp = reader_sp;
921 while (top_reader_sp)
922 {
923 top_reader_sp->Activate();
924 top_reader_sp->Run();
925 top_reader_sp->Deactivate();
926
927 if (top_reader_sp.get() == reader_sp.get())
928 {
929 if (PopIOHandler (reader_sp))
930 break;
931 }
932
933 while (1)
934 {
935 top_reader_sp = m_input_reader_stack.Top();
936 if (top_reader_sp && top_reader_sp->GetIsDone())
937 m_input_reader_stack.Pop();
938 else
939 break;
940 }
941 }
Greg Clayton44d93782014-01-27 23:43:24 +0000942}
943
944void
945Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
946{
947 // Before an IOHandler runs, it must have in/out/err streams.
948 // This function is called when one ore more of the streams
949 // are NULL. We use the top input reader's in/out/err streams,
950 // or fall back to the debugger file handles, or we fall back
951 // onto stdin/stdout/stderr as a last resort.
952
953 Mutex::Locker locker (m_input_reader_stack.GetMutex());
954 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
955 // If no STDIN has been set, then set it appropriately
956 if (!in)
957 {
958 if (top_reader_sp)
959 in = top_reader_sp->GetInputStreamFile();
960 else
961 in = GetInputFile();
962
963 // If there is nothing, use stdin
964 if (!in)
965 in = StreamFileSP(new StreamFile(stdin, false));
966 }
967 // If no STDOUT has been set, then set it appropriately
968 if (!out)
969 {
970 if (top_reader_sp)
971 out = top_reader_sp->GetOutputStreamFile();
972 else
973 out = GetOutputFile();
974
975 // If there is nothing, use stdout
976 if (!out)
977 out = StreamFileSP(new StreamFile(stdout, false));
978 }
979 // If no STDERR has been set, then set it appropriately
980 if (!err)
981 {
982 if (top_reader_sp)
983 err = top_reader_sp->GetErrorStreamFile();
984 else
985 err = GetErrorFile();
986
987 // If there is nothing, use stderr
988 if (!err)
989 err = StreamFileSP(new StreamFile(stdout, false));
990
991 }
992}
993
994void
995Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996{
997 if (!reader_sp)
998 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000999
Greg Clayton44d93782014-01-27 23:43:24 +00001000 // Got the current top input reader...
1001 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001002
Greg Claytonb4874f12014-02-28 18:22:24 +00001003 // Don't push the same IO handler twice...
1004 if (reader_sp.get() != top_reader_sp.get())
1005 {
1006 // Push our new input reader
1007 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00001008
Greg Claytonb4874f12014-02-28 18:22:24 +00001009 // Interrupt the top input reader to it will exit its Run() function
1010 // and let this new input reader take over
1011 if (top_reader_sp)
1012 top_reader_sp->Deactivate();
1013 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014}
1015
1016bool
Greg Clayton44d93782014-01-27 23:43:24 +00001017Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018{
1019 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +00001020
1021 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022
1023 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001024 // read on the stack refresh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001025 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 {
Greg Clayton44d93782014-01-27 23:43:24 +00001027 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028
1029 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
1030 {
Greg Clayton44d93782014-01-27 23:43:24 +00001031 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +00001032 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001033 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +00001034
1035 reader_sp = m_input_reader_stack.Top();
1036 if (reader_sp)
1037 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038
Greg Clayton44d93782014-01-27 23:43:24 +00001039 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040 }
1041 }
1042 return result;
1043}
1044
1045bool
Greg Clayton44d93782014-01-27 23:43:24 +00001046Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047{
Greg Clayton44d93782014-01-27 23:43:24 +00001048 Mutex::Locker locker;
1049
1050 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 {
Greg Clayton44d93782014-01-27 23:43:24 +00001052 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1053 if (reader_sp)
1054 reader_sp->Hide();
1055 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 }
Greg Clayton44d93782014-01-27 23:43:24 +00001057 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058}
1059
1060void
Greg Clayton44d93782014-01-27 23:43:24 +00001061Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062{
Greg Clayton44d93782014-01-27 23:43:24 +00001063 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1064 if (reader_sp)
1065 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066}
Greg Clayton66111032010-06-23 01:19:29 +00001067
Greg Clayton44d93782014-01-27 23:43:24 +00001068
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001069StreamSP
1070Debugger::GetAsyncOutputStream ()
1071{
1072 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1073 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1074}
1075
1076StreamSP
1077Debugger::GetAsyncErrorStream ()
1078{
1079 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1080 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1081}
1082
Greg Claytonc7bece562013-01-25 18:06:21 +00001083size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001084Debugger::GetNumDebuggers()
1085{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001086 if (g_shared_debugger_refcount > 0)
1087 {
1088 Mutex::Locker locker (GetDebuggerListMutex ());
1089 return GetDebuggerList().size();
1090 }
1091 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001092}
1093
1094lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001095Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001096{
1097 DebuggerSP debugger_sp;
1098
Greg Claytonc15f55e2012-03-30 20:53:46 +00001099 if (g_shared_debugger_refcount > 0)
1100 {
1101 Mutex::Locker locker (GetDebuggerListMutex ());
1102 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001103
Greg Claytonc15f55e2012-03-30 20:53:46 +00001104 if (index < debugger_list.size())
1105 debugger_sp = debugger_list[index];
1106 }
1107
Enrico Granata061858c2012-02-15 02:34:21 +00001108 return debugger_sp;
1109}
1110
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001111DebuggerSP
1112Debugger::FindDebuggerWithID (lldb::user_id_t id)
1113{
Greg Clayton4d122c42011-09-17 08:33:22 +00001114 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001115
Greg Claytonc15f55e2012-03-30 20:53:46 +00001116 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001117 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001118 Mutex::Locker locker (GetDebuggerListMutex ());
1119 DebuggerList &debugger_list = GetDebuggerList();
1120 DebuggerList::iterator pos, end = debugger_list.end();
1121 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001122 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001123 if ((*pos).get()->GetID() == id)
1124 {
1125 debugger_sp = *pos;
1126 break;
1127 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001128 }
1129 }
1130 return debugger_sp;
1131}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001132
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001133#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001134static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001135TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001136{
1137 if (frame == NULL)
1138 return;
1139
1140 StreamString s;
1141 const char *prompt_format =
1142 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001143 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1144 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001145 "{process.id = '${process.id}'\n}"
1146 "{process.name = '${process.name}'\n}"
1147 "{process.file.basename = '${process.file.basename}'\n}"
1148 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1149 "{thread.id = '${thread.id}'\n}"
1150 "{thread.index = '${thread.index}'\n}"
1151 "{thread.name = '${thread.name}'\n}"
1152 "{thread.queue = '${thread.queue}'\n}"
1153 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1154 "{target.arch = '${target.arch}'\n}"
1155 "{module.file.basename = '${module.file.basename}'\n}"
1156 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1157 "{file.basename = '${file.basename}'\n}"
1158 "{file.fullpath = '${file.fullpath}'\n}"
1159 "{frame.index = '${frame.index}'\n}"
1160 "{frame.pc = '${frame.pc}'\n}"
1161 "{frame.sp = '${frame.sp}'\n}"
1162 "{frame.fp = '${frame.fp}'\n}"
1163 "{frame.flags = '${frame.flags}'\n}"
1164 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1165 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1166 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1167 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1168 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1169 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1170 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1171 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001172 "{function.changed = '${function.changed}'\n}"
1173 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001174 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001175 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001176 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001177 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001178 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001179 "{function.line-offset = '${function.line-offset}'\n}"
1180 "{function.pc-offset = '${function.pc-offset}'\n}"
1181 "{line.file.basename = '${line.file.basename}'\n}"
1182 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1183 "{line.number = '${line.number}'\n}"
1184 "{line.start-addr = '${line.start-addr}'\n}"
1185 "{line.end-addr = '${line.end-addr}'\n}"
1186;
1187
1188 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1189 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001190 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001191 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001192 {
1193 printf("%s\n", s.GetData());
1194 }
1195 else
1196 {
Greg Clayton1b654882010-09-19 02:33:57 +00001197 printf ("what we got: %s\n", s.GetData());
1198 }
1199}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001200#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001201
Enrico Granata9fc19442011-07-06 02:13:41 +00001202static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001203ScanFormatDescriptor (const char* var_name_begin,
1204 const char* var_name_end,
1205 const char** var_name_final,
1206 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001207 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001208 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001209{
Greg Clayton5160ce52013-03-27 23:08:40 +00001210 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001211 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001212 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001213 {
1214 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001215 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001216 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001217 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001218 else
1219 {
1220 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001221 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001222 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001223 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001224 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001225 true,
1226 *custom_format) )
1227 {
Enrico Granatae992a082011-07-22 17:03:19 +00001228 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001229 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001230
1231 switch (format_name.front())
1232 {
1233 case '@': // if this is an @ sign, print ObjC description
1234 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1235 break;
1236 case 'V': // if this is a V, print the value using the default format
1237 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1238 break;
1239 case 'L': // if this is an L, print the location of the value
1240 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1241 break;
1242 case 'S': // if this is an S, print the summary after all
1243 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1244 break;
1245 case '#': // if this is a '#', print the number of children
1246 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1247 break;
1248 case 'T': // if this is a 'T', print the type
1249 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1250 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001251 case 'N': // if this is a 'N', print the name
1252 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1253 break;
1254 case '>': // if this is a '>', print the name
1255 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1256 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001257 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001258 if (log)
1259 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001260 break;
1261 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001262 }
1263 // a good custom format tells us to print the value using it
1264 else
Enrico Granatae992a082011-07-22 17:03:19 +00001265 {
1266 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001267 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001268 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001269 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001270 }
Enrico Granatae992a082011-07-22 17:03:19 +00001271 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001272 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001273 *custom_format,
1274 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001275 return true;
1276}
1277
1278static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001279ScanBracketedRange (const char* var_name_begin,
1280 const char* var_name_end,
1281 const char* var_name_final,
1282 const char** open_bracket_position,
1283 const char** separator_position,
1284 const char** close_bracket_position,
1285 const char** var_name_final_if_array_range,
1286 int64_t* index_lower,
1287 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001288{
Greg Clayton5160ce52013-03-27 23:08:40 +00001289 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001290 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001291 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001292 {
1293 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1294 *close_bracket_position = ::strchr(*open_bracket_position,']');
1295 // as usual, we assume that [] will come before %
1296 //printf("trying to expand a []\n");
1297 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001298 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001299 {
Enrico Granatae992a082011-07-22 17:03:19 +00001300 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001301 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001302 *index_lower = 0;
1303 }
1304 else if (*separator_position == NULL || *separator_position > var_name_end)
1305 {
1306 char *end = NULL;
1307 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1308 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001309 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001310 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001311 }
Greg Clayton34132752011-07-06 04:07:21 +00001312 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001313 {
1314 char *end = NULL;
1315 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1316 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001317 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001318 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001319 }
1320 else
Enrico Granatae992a082011-07-22 17:03:19 +00001321 {
1322 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001323 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001324 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001325 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001326 if (*index_lower > *index_higher && *index_higher > 0)
1327 {
Enrico Granatae992a082011-07-22 17:03:19 +00001328 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001329 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001330 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001331 *index_lower = *index_higher;
1332 *index_higher = temp;
1333 }
1334 }
Enrico Granatae992a082011-07-22 17:03:19 +00001335 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001336 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001337 return true;
1338}
1339
Michael Sartain0769b2b2013-07-30 16:44:36 +00001340template <typename T>
1341static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1342{
1343 if (script_interpreter)
1344 {
1345 Error script_error;
1346 std::string script_output;
1347
1348 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1349 {
1350 s.Printf("%s", script_output.c_str());
1351 return true;
1352 }
1353 else
1354 {
1355 s.Printf("<error: %s>",script_error.AsCString());
1356 }
1357 }
1358 return false;
1359}
1360
Enrico Granata9fc19442011-07-06 02:13:41 +00001361static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001362ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001363 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001364 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001365 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001366{
Greg Clayton5160ce52013-03-27 23:08:40 +00001367 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001368 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001369 std::string ptr_deref_buffer(10,0);
1370 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001371 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001372 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001373 const char* first_unparsed;
1374 ValueObject::GetValueForExpressionPathOptions options;
1375 ValueObject::ExpressionPathEndResultType final_value_type;
1376 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001377 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001378 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001379 &first_unparsed,
1380 &reason_to_stop,
1381 &final_value_type,
1382 options,
1383 &what_next);
1384 if (!item)
1385 {
Enrico Granatae992a082011-07-22 17:03:19 +00001386 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001387 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001388 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001389 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001390 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001391 else
1392 {
Enrico Granatae992a082011-07-22 17:03:19 +00001393 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001394 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001395 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001396 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001397 }
1398 return item;
1399}
1400
Michael Sartain0769b2b2013-07-30 16:44:36 +00001401static inline bool
1402IsToken(const char *var_name_begin, const char *var)
1403{
1404 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1405}
1406
1407static bool
1408IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1409 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1410{
1411 int var_len = strlen(var);
1412 if (::strncmp (var_name_begin, var, var_len) == 0)
1413 {
1414 var_name_begin += var_len;
1415 if (*var_name_begin == '}')
1416 {
1417 format = default_format;
1418 return true;
1419 }
1420 else if (*var_name_begin == '%')
1421 {
1422 // Allow format specifiers: x|X|u with optional width specifiers.
1423 // ${thread.id%x} ; hex
1424 // ${thread.id%X} ; uppercase hex
1425 // ${thread.id%u} ; unsigned decimal
1426 // ${thread.id%8.8X} ; width.precision + specifier
1427 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1428 int dot_count = 0;
1429 const char *specifier = NULL;
1430 int width_precision_length = 0;
1431 const char *width_precision = ++var_name_begin;
1432 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1433 {
1434 dot_count += (*var_name_begin == '.');
1435 if (dot_count > 1)
1436 break;
1437 var_name_begin++;
1438 width_precision_length++;
1439 }
1440
1441 if (IsToken (var_name_begin, "tid}"))
1442 {
1443 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1444 if (target)
1445 {
1446 ArchSpec arch (target->GetArchitecture ());
1447 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1448 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1449 specifier = PRIu64;
1450 }
1451 if (!specifier)
1452 {
1453 format = default_format;
1454 return true;
1455 }
1456 }
1457 else if (IsToken (var_name_begin, "x}"))
1458 specifier = PRIx64;
1459 else if (IsToken (var_name_begin, "X}"))
1460 specifier = PRIX64;
1461 else if (IsToken (var_name_begin, "u}"))
1462 specifier = PRIu64;
1463
1464 if (specifier)
1465 {
1466 format = "%";
1467 if (width_precision_length)
1468 format += std::string(width_precision, width_precision_length);
1469 format += specifier;
1470 return true;
1471 }
1472 }
1473 }
1474 return false;
1475}
1476
Jason Molenda705b1802014-06-13 02:37:02 +00001477// Find information for the "thread.info.*" specifiers in a format string
1478static bool
1479FormatThreadExtendedInfoRecurse
1480(
1481 const char *var_name_begin,
1482 StructuredData::ObjectSP thread_info_dictionary,
1483 const SymbolContext *sc,
1484 const ExecutionContext *exe_ctx,
1485 Stream &s
1486)
1487{
1488 bool var_success = false;
1489 std::string token_format;
1490
1491 llvm::StringRef var_name(var_name_begin);
1492 size_t percent_idx = var_name.find('%');
1493 size_t close_curly_idx = var_name.find('}');
1494 llvm::StringRef path = var_name;
1495 llvm::StringRef formatter = var_name;
1496
1497 // 'path' will be the dot separated list of objects to transverse up until we hit
1498 // a close curly brace, a percent sign, or an end of string.
1499 if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos)
1500 {
1501 if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos)
1502 {
1503 if (percent_idx < close_curly_idx)
1504 {
1505 path = var_name.slice(0, percent_idx);
1506 formatter = var_name.substr (percent_idx);
1507 }
1508 else
1509 {
1510 path = var_name.slice(0, close_curly_idx);
1511 formatter = var_name.substr (close_curly_idx);
1512 }
1513 }
1514 else if (percent_idx != llvm::StringRef::npos)
1515 {
1516 path = var_name.slice(0, percent_idx);
1517 formatter = var_name.substr (percent_idx);
1518 }
1519 else if (close_curly_idx != llvm::StringRef::npos)
1520 {
1521 path = var_name.slice(0, close_curly_idx);
1522 formatter = var_name.substr (close_curly_idx);
1523 }
1524 }
1525
1526 StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1527
1528 if (value.get())
1529 {
1530 if (value->GetType() == StructuredData::Type::eTypeInteger)
1531 {
1532 if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
1533 {
1534 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue());
1535 var_success = true;
1536 }
1537 }
1538 else if (value->GetType() == StructuredData::Type::eTypeFloat)
1539 {
1540 s.Printf ("%f", value->GetAsFloat()->GetValue());
1541 var_success = true;
1542 }
1543 else if (value->GetType() == StructuredData::Type::eTypeString)
1544 {
1545 s.Printf("%s", value->GetAsString()->GetValue().c_str());
1546 var_success = true;
1547 }
1548 else if (value->GetType() == StructuredData::Type::eTypeArray)
1549 {
1550 if (value->GetAsArray()->GetSize() > 0)
1551 {
1552 s.Printf ("%zu", value->GetAsArray()->GetSize());
1553 var_success = true;
1554 }
1555 }
1556 else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1557 {
1558 s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1559 var_success = true;
1560 }
1561 }
1562
1563 return var_success;
1564}
1565
1566
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001567static bool
1568FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001569(
1570 const char *format,
1571 const SymbolContext *sc,
1572 const ExecutionContext *exe_ctx,
1573 const Address *addr,
1574 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001575 const char **end,
Jason Molendaaff1b352014-10-10 23:07:36 +00001576 ValueObject* valobj,
1577 bool function_changed,
1578 bool initial_function
Greg Clayton1b654882010-09-19 02:33:57 +00001579)
1580{
Enrico Granatac482a192011-08-17 22:13:59 +00001581 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001582 bool success = true;
1583 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001584 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001585
Greg Clayton1b654882010-09-19 02:33:57 +00001586 for (p = format; *p != '\0'; ++p)
1587 {
Enrico Granatac482a192011-08-17 22:13:59 +00001588 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001589 {
Enrico Granatac482a192011-08-17 22:13:59 +00001590 valobj = realvalobj;
1591 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001592 }
Greg Clayton1b654882010-09-19 02:33:57 +00001593 size_t non_special_chars = ::strcspn (p, "${}\\");
1594 if (non_special_chars > 0)
1595 {
1596 if (success)
1597 s.Write (p, non_special_chars);
1598 p += non_special_chars;
1599 }
1600
1601 if (*p == '\0')
1602 {
1603 break;
1604 }
1605 else if (*p == '{')
1606 {
1607 // Start a new scope that must have everything it needs if it is to
1608 // to make it into the final output stream "s". If you want to make
1609 // a format that only prints out the function or symbol name if there
1610 // is one in the symbol context you can use:
1611 // "{function =${function.name}}"
1612 // The first '{' starts a new scope that end with the matching '}' at
1613 // the end of the string. The contents "function =${function.name}"
1614 // will then be evaluated and only be output if there is a function
1615 // or symbol with a valid name.
1616 StreamString sub_strm;
1617
1618 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001619
Jason Molendaaff1b352014-10-10 23:07:36 +00001620 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj, function_changed, initial_function))
Greg Clayton1b654882010-09-19 02:33:57 +00001621 {
1622 // The stream had all it needed
1623 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1624 }
1625 if (*p != '}')
1626 {
1627 success = false;
1628 break;
1629 }
1630 }
1631 else if (*p == '}')
1632 {
1633 // End of a enclosing scope
1634 break;
1635 }
1636 else if (*p == '$')
1637 {
1638 // We have a prompt variable to print
1639 ++p;
1640 if (*p == '{')
1641 {
1642 ++p;
1643 const char *var_name_begin = p;
1644 const char *var_name_end = ::strchr (p, '}');
1645
1646 if (var_name_end && var_name_begin < var_name_end)
1647 {
1648 // if we have already failed to parse, skip this variable
1649 if (success)
1650 {
1651 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001652 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001653 Address format_addr;
Jason Molendaaff1b352014-10-10 23:07:36 +00001654
1655 // normally "addr" means print a raw address but
1656 // "file-addr-or-load-addr" means print a module + file addr if there's no load addr
1657 bool print_file_addr_or_load_addr = false;
1658 bool addr_offset_concrete_func_only = false;
1659 bool addr_offset_print_with_no_padding = false;
Greg Clayton1b654882010-09-19 02:33:57 +00001660 bool calculate_format_addr_function_offset = false;
1661 // Set reg_kind and reg_num to invalid values
1662 RegisterKind reg_kind = kNumRegisterKinds;
1663 uint32_t reg_num = LLDB_INVALID_REGNUM;
1664 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001665 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001666 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001667 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001668 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1669 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001670
Greg Clayton1b654882010-09-19 02:33:57 +00001671 // Each variable must set success to true below...
1672 bool var_success = false;
1673 switch (var_name_begin[0])
1674 {
Enrico Granata4becb372011-06-29 22:27:15 +00001675 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001676 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001677 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001678 {
Enrico Granatac482a192011-08-17 22:13:59 +00001679 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001680 break;
1681
Enrico Granatac3e320a2011-08-02 17:27:39 +00001682 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001683 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001684
Enrico Granata6f3533f2011-07-29 19:53:35 +00001685 // check for *var and *svar
1686 if (*var_name_begin == '*')
1687 {
1688 do_deref_pointer = true;
1689 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001690 if (log)
1691 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001692 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001693
Enrico Granata6f3533f2011-07-29 19:53:35 +00001694 if (*var_name_begin == 's')
1695 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001696 if (!valobj->IsSynthetic())
1697 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001698 if (!valobj)
1699 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001700 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001701 if (log)
1702 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001703 }
1704
1705 // should be a 'v' by now
1706 if (*var_name_begin != 'v')
1707 break;
1708
Enrico Granatac3e320a2011-08-02 17:27:39 +00001709 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001710 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001711
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001712 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001713 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001714 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001715 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001716 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001717 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001718 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001719 const char* var_name_final = NULL;
1720 const char* var_name_final_if_array_range = NULL;
1721 const char* close_bracket_position = NULL;
1722 int64_t index_lower = -1;
1723 int64_t index_higher = -1;
1724 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001725 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001726 bool was_plain_var = false;
1727 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001728 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001729
Enrico Granatac482a192011-08-17 22:13:59 +00001730 if (!valobj) break;
1731 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001732 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001733 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001734 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001735 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001736 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001737 }
Enrico Granata88282c62014-10-28 21:07:00 +00001738 else if (IsToken (var_name_begin, "var.script:"))
1739 {
1740 var_name_begin += ::strlen("var.script:");
1741 std::string script_name(var_name_begin,var_name_end);
1742 ScriptInterpreter* script_interpreter = valobj->GetTargetSP()->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1743 if (RunScriptFormatKeyword (s, script_interpreter, valobj, script_name))
1744 var_success = true;
1745 break;
1746 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001747 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001748 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001749 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001750 // this is a variable with some custom format applied to it
1751 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001752 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001753 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001754 ScanFormatDescriptor (var_name_begin,
1755 var_name_end,
1756 &var_name_final,
1757 &percent_position,
1758 &custom_format,
1759 &val_obj_display);
1760 }
1761 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001762 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001763 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001764 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001765 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001766 const char* percent_position;
1767 ScanFormatDescriptor (var_name_begin,
1768 var_name_end,
1769 &var_name_final,
1770 &percent_position,
1771 &custom_format,
1772 &val_obj_display);
1773
1774 const char* open_bracket_position;
1775 const char* separator_position;
1776 ScanBracketedRange (var_name_begin,
1777 var_name_end,
1778 var_name_final,
1779 &open_bracket_position,
1780 &separator_position,
1781 &close_bracket_position,
1782 &var_name_final_if_array_range,
1783 &index_lower,
1784 &index_higher);
1785
1786 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001787
Enrico Granata599171a2013-02-01 23:59:44 +00001788 std::string expr_path(var_name_final-var_name_begin-1,0);
1789 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1790
1791 if (log)
1792 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1793
1794 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001795 &first_unparsed,
1796 &reason_to_stop,
1797 &final_value_type,
1798 options,
1799 &what_next).get();
1800
1801 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001802 {
Enrico Granatae992a082011-07-22 17:03:19 +00001803 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001804 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001805 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001806 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001807 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001808 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001809 else
1810 {
Enrico Granatae992a082011-07-22 17:03:19 +00001811 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001812 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001813 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001814 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata50bed5e2014-10-22 20:14:09 +00001815 target = target->GetQualifiedRepresentationIfAvailable(target->GetDynamicValueType(), true).get();
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001816 }
Enrico Granata4becb372011-06-29 22:27:15 +00001817 }
Greg Clayton34132752011-07-06 04:07:21 +00001818 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001819 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001820
Enrico Granata86cc9822012-03-19 22:58:49 +00001821 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1822 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001823
Enrico Granata86cc9822012-03-19 22:58:49 +00001824 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001825
Enrico Granataa7187d02011-07-06 19:27:11 +00001826 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001827 {
Greg Clayton34132752011-07-06 04:07:21 +00001828 // I have not deref-ed yet, let's do it
1829 // this happens when we are not going through GetValueForVariableExpressionPath
1830 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001831 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001832 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001833 if (error.Fail())
1834 {
1835 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001836 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001837 break;
1838 }
Greg Clayton34132752011-07-06 04:07:21 +00001839 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001840 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001841
Jim Inghamf164d942014-03-11 18:17:23 +00001842 if (!target)
1843 {
1844 if (log)
1845 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1846 break;
1847 }
1848
Enrico Granataa777dc22012-05-08 21:49:57 +00001849 // we do not want to use the summary for a bitfield of type T:n
1850 // if we were originally dealing with just a T - that would get
1851 // us into an endless recursion
1852 if (target->IsBitfield() && was_var_indexed)
1853 {
1854 // TODO: check for a (T:n)-specific summary - we should still obey that
1855 StreamString bitfield_name;
1856 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1857 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1858 if (!DataVisualization::GetSummaryForType(type_sp))
1859 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1860 }
1861
Enrico Granata85933ed2011-08-18 16:38:26 +00001862 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001863 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
Enrico Granata622be232014-10-21 20:52:14 +00001864 bool is_array = (type_info_flags & eTypeIsArray) != 0;
1865 bool is_pointer = (type_info_flags & eTypeIsPointer) != 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001866 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001867
Enrico Granata86cc9822012-03-19 22:58:49 +00001868 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
Enrico Granataf4efecd2011-07-12 22:56:10 +00001869 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001870 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001871 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001872 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001873
Greg Clayton5088c482013-03-25 21:06:13 +00001874 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001875 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001876 // try to use the special cases
1877 var_success = target->DumpPrintableRepresentation(str_temp,
1878 val_obj_display,
1879 custom_format);
1880 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001881 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001882
1883 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001884 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001885 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001886 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001887 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001888 }
1889 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001890 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001891 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001892 {
1893 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1894 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001895 else if (is_pointer) // if pointer, value is the address stored
1896 {
Greg Clayton23f59502012-07-17 03:23:13 +00001897 target->DumpPrintableRepresentation (s,
1898 val_obj_display,
1899 custom_format,
1900 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001901 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001902 var_success = true;
1903 break;
1904 }
1905 }
1906
1907 // if directly trying to print ${var}, and this is an aggregate, display a nice
1908 // type @ location message
1909 if (is_aggregate && was_plain_var)
1910 {
1911 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1912 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001913 break;
1914 }
1915
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001916 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
Enrico Granata86cc9822012-03-19 22:58:49 +00001917 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001918 {
1919 s << "<invalid use of aggregate type>";
1920 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001921 break;
1922 }
Greg Clayton34132752011-07-06 04:07:21 +00001923
1924 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001925 {
1926 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001927 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001928 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001929 }
Greg Clayton34132752011-07-06 04:07:21 +00001930 else
Enrico Granatae992a082011-07-22 17:03:19 +00001931 {
1932 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001933 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001934 if (!is_array && !is_pointer)
1935 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001936 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001937 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001938 const char* special_directions = NULL;
1939 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001940 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1941 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001942 ConstString additional_data;
1943 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1944 special_directions_writer.Printf("${%svar%s}",
1945 do_deref_pointer ? "*" : "",
1946 additional_data.GetCString());
1947 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001948 }
1949
1950 // let us display items index_lower thru index_higher of this array
1951 s.PutChar('[');
1952 var_success = true;
1953
1954 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001955 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001956
Greg Claytoncc4d0142012-02-17 07:49:44 +00001957 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001958
Greg Clayton34132752011-07-06 04:07:21 +00001959 for (;index_lower<=index_higher;index_lower++)
1960 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001961 ValueObject* item = ExpandIndexedExpression (target,
1962 index_lower,
1963 exe_ctx->GetFramePtr(),
1964 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001965
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001966 if (!item)
1967 {
Enrico Granatae992a082011-07-22 17:03:19 +00001968 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001969 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001970 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001971 else
1972 {
Enrico Granatae992a082011-07-22 17:03:19 +00001973 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001974 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001975 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001976
Greg Clayton34132752011-07-06 04:07:21 +00001977 if (!special_directions)
1978 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1979 else
Jason Molendaaff1b352014-10-10 23:07:36 +00001980 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item, function_changed, initial_function);
Greg Clayton34132752011-07-06 04:07:21 +00001981
Enrico Granata22c55d12011-08-12 02:00:06 +00001982 if (--max_num_children == 0)
1983 {
1984 s.PutCString(", ...");
1985 break;
1986 }
1987
Greg Clayton34132752011-07-06 04:07:21 +00001988 if (index_lower < index_higher)
1989 s.PutChar(',');
1990 }
1991 s.PutChar(']');
1992 }
Enrico Granata4becb372011-06-29 22:27:15 +00001993 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001994 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001995 case 'a':
Jason Molendaaff1b352014-10-10 23:07:36 +00001996 if (IsToken (var_name_begin, "addr-file-or-load}"))
1997 {
1998 print_file_addr_or_load_addr = true;
1999 }
2000 if (IsToken (var_name_begin, "addr}")
2001 || IsToken (var_name_begin, "addr-file-or-load}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002002 {
2003 if (addr && addr->IsValid())
2004 {
2005 var_success = true;
2006 format_addr = *addr;
2007 }
2008 }
2009 break;
2010
2011 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002012 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00002013 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002014 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002015 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002016 Process *process = exe_ctx->GetProcessPtr();
2017 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00002018 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002019 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002020 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002021 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002022 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002023 var_success = true;
2024 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002025 else if ((IsToken (var_name_begin, "name}")) ||
2026 (IsToken (var_name_begin, "file.basename}")) ||
2027 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00002028 {
2029 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
2030 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00002031 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002032 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
2033 {
2034 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002035 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002036 }
2037 else
2038 {
2039 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002040 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002041 }
Greg Clayton1b654882010-09-19 02:33:57 +00002042 }
2043 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002044 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002045 {
2046 var_name_begin += ::strlen("script:");
2047 std::string script_name(var_name_begin,var_name_end);
2048 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002049 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
2050 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002051 }
Greg Clayton1b654882010-09-19 02:33:57 +00002052 }
Greg Claytonc14ee322011-09-22 04:58:26 +00002053 }
Greg Clayton1b654882010-09-19 02:33:57 +00002054 }
2055 break;
2056
2057 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002058 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00002059 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002060 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002061 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002062 Thread *thread = exe_ctx->GetThreadPtr();
2063 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00002064 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002065 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002066 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002067 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002068 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002069 var_success = true;
2070 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002071 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00002072 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002073 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00002074 var_success = true;
2075 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002076 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00002077 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002078 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002079 var_success = true;
2080 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002081 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002082 {
2083 cstr = thread->GetName();
2084 var_success = cstr && cstr[0];
2085 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002086 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00002087 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002088 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002089 {
2090 cstr = thread->GetQueueName();
2091 var_success = cstr && cstr[0];
2092 if (var_success)
2093 s.PutCString(cstr);
2094 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002095 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002096 {
2097 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002098 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00002099 {
2100 cstr = stop_info_sp->GetDescription();
2101 if (cstr && cstr[0])
2102 {
2103 s.PutCString(cstr);
2104 var_success = true;
2105 }
Greg Clayton1b654882010-09-19 02:33:57 +00002106 }
2107 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002108 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00002109 {
2110 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002111 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00002112 {
2113 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2114 if (return_valobj_sp)
2115 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00002116 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00002117 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00002118 }
2119 }
2120 }
Jim Ingham30fadaf2014-07-08 01:07:32 +00002121 else if (IsToken (var_name_begin, "completed-expression}"))
2122 {
2123 StopInfoSP stop_info_sp = thread->GetStopInfo ();
2124 if (stop_info_sp && stop_info_sp->IsValid())
2125 {
2126 ClangExpressionVariableSP expression_var_sp = StopInfo::GetExpressionVariable (stop_info_sp);
2127 if (expression_var_sp && expression_var_sp->GetValueObject())
2128 {
2129 expression_var_sp->GetValueObject()->Dump(s);
2130 var_success = true;
2131 }
2132 }
2133 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002134 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002135 {
2136 var_name_begin += ::strlen("script:");
2137 std::string script_name(var_name_begin,var_name_end);
2138 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002139 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
2140 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002141 }
Jason Molenda705b1802014-06-13 02:37:02 +00002142 else if (IsToken (var_name_begin, "info."))
2143 {
2144 var_name_begin += ::strlen("info.");
2145 StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
2146 if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
2147 {
2148 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s);
2149 }
2150 }
Greg Clayton1b654882010-09-19 02:33:57 +00002151 }
2152 }
2153 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002154 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00002155 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002156 // TODO: hookup properties
2157// if (!target_properties_sp)
2158// {
2159// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2160// if (target)
2161// target_properties_sp = target->GetProperties();
2162// }
2163//
2164// if (target_properties_sp)
2165// {
2166// var_name_begin += ::strlen ("target.");
2167// const char *end_property = strchr(var_name_begin, '}');
2168// if (end_property)
2169// {
2170// ConstString property_name(var_name_begin, end_property - var_name_begin);
2171// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2172// if (!property_value.empty())
2173// {
2174// s.PutCString (property_value.c_str());
2175// var_success = true;
2176// }
2177// }
2178// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002179 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2180 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002181 {
Greg Clayton1b654882010-09-19 02:33:57 +00002182 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002183 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002184 {
2185 ArchSpec arch (target->GetArchitecture ());
2186 if (arch.IsValid())
2187 {
Greg Clayton64195a22011-02-23 00:35:02 +00002188 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002189 var_success = true;
2190 }
2191 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002192 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002193 {
2194 var_name_begin += ::strlen("script:");
2195 std::string script_name(var_name_begin,var_name_end);
2196 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002197 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2198 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002199 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002200 }
Greg Clayton1b654882010-09-19 02:33:57 +00002201 }
2202 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002203
Greg Clayton1b654882010-09-19 02:33:57 +00002204 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002205 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002206 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002207 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002208 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002209 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002210 var_name_begin += ::strlen ("module.");
2211
Michael Sartain0769b2b2013-07-30 16:44:36 +00002212 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002213 {
2214 if (module->GetFileSpec())
2215 {
2216 var_name_begin += ::strlen ("file.");
2217
Michael Sartain0769b2b2013-07-30 16:44:36 +00002218 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002219 {
2220 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002221 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002222 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002223 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002224 {
2225 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002226 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002227 }
2228 }
2229 }
2230 }
2231 }
2232 break;
2233
2234
2235 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002236 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002237 {
2238 if (sc && sc->comp_unit != NULL)
2239 {
2240 var_name_begin += ::strlen ("file.");
2241
Michael Sartain0769b2b2013-07-30 16:44:36 +00002242 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002243 {
2244 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002245 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002246 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002247 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002248 {
2249 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002250 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002251 }
2252 }
2253 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002254 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002255 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002256 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002257 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002258 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002259 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002260 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002261 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002262 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002263 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002264 s.Printf("%u", frame->GetFrameIndex());
2265 var_success = true;
2266 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002267 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002268 {
2269 reg_kind = eRegisterKindGeneric;
2270 reg_num = LLDB_REGNUM_GENERIC_PC;
2271 var_success = true;
2272 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002273 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002274 {
2275 reg_kind = eRegisterKindGeneric;
2276 reg_num = LLDB_REGNUM_GENERIC_SP;
2277 var_success = true;
2278 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002279 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002280 {
2281 reg_kind = eRegisterKindGeneric;
2282 reg_num = LLDB_REGNUM_GENERIC_FP;
2283 var_success = true;
2284 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002285 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002286 {
2287 reg_kind = eRegisterKindGeneric;
2288 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2289 var_success = true;
2290 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002291 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002292 {
2293 reg_ctx = frame->GetRegisterContext().get();
2294 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002295 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002296 var_name_begin += ::strlen ("reg.");
2297 if (var_name_begin < var_name_end)
2298 {
2299 std::string reg_name (var_name_begin, var_name_end);
2300 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2301 if (reg_info)
2302 var_success = true;
2303 }
Greg Clayton1b654882010-09-19 02:33:57 +00002304 }
2305 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002306 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002307 {
2308 var_name_begin += ::strlen("script:");
2309 std::string script_name(var_name_begin,var_name_end);
2310 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002311 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2312 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002313 }
Greg Clayton1b654882010-09-19 02:33:57 +00002314 }
2315 }
2316 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002317 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002318 {
2319 if (sc && (sc->function != NULL || sc->symbol != NULL))
2320 {
2321 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002322 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002323 {
2324 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002325 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002326 else
2327 s.Printf("symbol[%u]", sc->symbol->GetID());
2328
2329 var_success = true;
2330 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002331 if (IsToken (var_name_begin, "changed}") && function_changed)
2332 {
2333 var_success = true;
2334 }
2335 if (IsToken (var_name_begin, "initial-function}") && initial_function)
2336 {
2337 var_success = true;
2338 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002339 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002340 {
2341 if (sc->function)
2342 cstr = sc->function->GetName().AsCString (NULL);
2343 else if (sc->symbol)
2344 cstr = sc->symbol->GetName().AsCString (NULL);
2345 if (cstr)
2346 {
2347 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002348
2349 if (sc->block)
2350 {
2351 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2352 if (inline_block)
2353 {
2354 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2355 if (inline_info)
2356 {
2357 s.PutCString(" [inlined] ");
2358 inline_info->GetName().Dump(&s);
2359 }
2360 }
2361 }
Greg Clayton1b654882010-09-19 02:33:57 +00002362 var_success = true;
2363 }
2364 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002365 else if (IsToken (var_name_begin, "name-without-args}"))
2366 {
2367 ConstString name;
2368 if (sc->function)
2369 name = sc->function->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2370 else if (sc->symbol)
2371 name = sc->symbol->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2372 if (name)
2373 {
2374 s.PutCString(name.GetCString());
2375 var_success = true;
2376 }
2377 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002378 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002379 {
2380 // Print the function name with arguments in it
2381
2382 if (sc->function)
2383 {
2384 var_success = true;
2385 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2386 cstr = sc->function->GetName().AsCString (NULL);
2387 if (cstr)
2388 {
2389 const InlineFunctionInfo *inline_info = NULL;
2390 VariableListSP variable_list_sp;
2391 bool get_function_vars = true;
2392 if (sc->block)
2393 {
2394 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2395
2396 if (inline_block)
2397 {
2398 get_function_vars = false;
2399 inline_info = sc->block->GetInlinedFunctionInfo();
2400 if (inline_info)
2401 variable_list_sp = inline_block->GetBlockVariableList (true);
2402 }
2403 }
2404
2405 if (get_function_vars)
2406 {
2407 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2408 }
2409
2410 if (inline_info)
2411 {
2412 s.PutCString (cstr);
2413 s.PutCString (" [inlined] ");
2414 cstr = inline_info->GetName().GetCString();
2415 }
2416
2417 VariableList args;
2418 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002419 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002420 if (args.GetSize() > 0)
2421 {
2422 const char *open_paren = strchr (cstr, '(');
Enrico Granatae4a4f5d2014-08-16 00:56:04 +00002423 const char *close_paren = nullptr;
2424 const char *generic = strchr(cstr, '<');
2425 // if before the arguments list begins there is a template sign
2426 // then scan to the end of the generic args before you try to find
2427 // the arguments list
2428 if (generic && open_paren && generic < open_paren)
2429 {
2430 int generic_depth = 1;
2431 ++generic;
2432 for (;
2433 *generic && generic_depth > 0;
2434 generic++)
2435 {
2436 if (*generic == '<')
2437 generic_depth++;
2438 if (*generic == '>')
2439 generic_depth--;
2440 }
2441 if (*generic)
2442 open_paren = strchr(generic, '(');
2443 else
2444 open_paren = nullptr;
2445 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002446 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002447 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002448 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002449 {
2450 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2451 if (open_paren)
2452 close_paren = strchr (open_paren, ')');
2453 }
2454 else
2455 close_paren = strchr (open_paren, ')');
2456 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002457
2458 if (open_paren)
2459 s.Write(cstr, open_paren - cstr + 1);
2460 else
2461 {
2462 s.PutCString (cstr);
2463 s.PutChar ('(');
2464 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002465 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002466 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2467 {
Enrico Granata894f7352014-03-25 22:03:52 +00002468 std::string buffer;
2469
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002470 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2471 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
Enrico Granata894f7352014-03-25 22:03:52 +00002472 const char *var_representation = nullptr;
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002473 const char *var_name = var_value_sp->GetName().GetCString();
Enrico Granata894f7352014-03-25 22:03:52 +00002474 if (var_value_sp->GetClangType().IsAggregateType() &&
2475 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2476 {
2477 static StringSummaryFormat format(TypeSummaryImpl::Flags()
2478 .SetHideItemNames(false)
2479 .SetShowMembersOneLiner(true),
2480 "");
2481 format.FormatObject(var_value_sp.get(), buffer);
2482 var_representation = buffer.c_str();
2483 }
2484 else
2485 var_representation = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002486 if (arg_idx > 0)
2487 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002488 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002489 {
Enrico Granata894f7352014-03-25 22:03:52 +00002490 if (var_representation)
2491 s.Printf ("%s=%s", var_name, var_representation);
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002492 else
2493 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2494 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002495 else
2496 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002497 }
2498
2499 if (close_paren)
2500 s.PutCString (close_paren);
2501 else
2502 s.PutChar(')');
2503
2504 }
2505 else
2506 {
2507 s.PutCString(cstr);
2508 }
2509 }
2510 }
2511 else if (sc->symbol)
2512 {
2513 cstr = sc->symbol->GetName().AsCString (NULL);
2514 if (cstr)
2515 {
2516 s.PutCString(cstr);
2517 var_success = true;
2518 }
2519 }
2520 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002521 else if (IsToken (var_name_begin, "addr-offset}")
2522 || IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002523 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002524 if (IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
2525 {
2526 addr_offset_print_with_no_padding = true;
2527 addr_offset_concrete_func_only = true;
2528 }
Greg Clayton1b654882010-09-19 02:33:57 +00002529 var_success = addr != NULL;
2530 if (var_success)
2531 {
2532 format_addr = *addr;
2533 calculate_format_addr_function_offset = true;
2534 }
2535 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002536 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002537 {
2538 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2539 if (var_success)
2540 {
2541 format_addr = sc->line_entry.range.GetBaseAddress();
2542 calculate_format_addr_function_offset = true;
2543 }
2544 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002545 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002546 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002547 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002548 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002549 if (var_success)
2550 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002551 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002552 calculate_format_addr_function_offset = true;
2553 }
2554 }
2555 }
2556 }
2557 break;
2558
2559 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002560 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002561 {
2562 if (sc && sc->line_entry.IsValid())
2563 {
2564 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002565 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002566 {
2567 var_name_begin += ::strlen ("file.");
2568
Michael Sartain0769b2b2013-07-30 16:44:36 +00002569 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002570 {
2571 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002572 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002573 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002574 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002575 {
2576 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002577 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002578 }
2579 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002580 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002581 {
2582 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002583 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002584 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002585 else if ((IsToken (var_name_begin, "start-addr}")) ||
2586 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002587 {
2588 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2589 if (var_success)
2590 {
2591 format_addr = sc->line_entry.range.GetBaseAddress();
2592 if (var_name_begin[0] == 'e')
2593 format_addr.Slide (sc->line_entry.range.GetByteSize());
2594 }
2595 }
2596 }
2597 }
2598 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002599 case 'c':
2600 if (IsToken (var_name_begin, "current-pc-arrow"))
2601 {
2602 if (addr && exe_ctx && exe_ctx->GetFramePtr())
2603 {
2604 RegisterContextSP reg_ctx = exe_ctx->GetFramePtr()->GetRegisterContextSP();
2605 if (reg_ctx.get())
2606 {
2607 addr_t pc_loadaddr = reg_ctx->GetPC();
2608 if (pc_loadaddr != LLDB_INVALID_ADDRESS)
2609 {
2610 Address pc;
2611 pc.SetLoadAddress (pc_loadaddr, exe_ctx->GetTargetPtr());
2612 if (pc == *addr)
2613 {
2614 s.Printf ("->");
2615 var_success = true;
2616 }
2617 else
2618 {
2619 s.Printf(" ");
2620 var_success = true;
2621 }
2622 }
2623 }
2624 }
2625 }
2626 break;
Greg Clayton1b654882010-09-19 02:33:57 +00002627 }
2628
2629 if (var_success)
2630 {
2631 // If format addr is valid, then we need to print an address
2632 if (reg_num != LLDB_INVALID_REGNUM)
2633 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002634 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002635 // We have a register value to display...
2636 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2637 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002638 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002639 }
2640 else
2641 {
2642 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002643 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002644
2645 if (reg_ctx)
2646 {
2647 if (reg_kind != kNumRegisterKinds)
2648 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2649 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2650 var_success = reg_info != NULL;
2651 }
2652 }
2653 }
2654
2655 if (reg_info != NULL)
2656 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002657 RegisterValue reg_value;
2658 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2659 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002660 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002661 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002662 }
2663 }
2664
2665 if (format_file_spec)
2666 {
2667 s << format_file_spec;
2668 }
2669
2670 // If format addr is valid, then we need to print an address
2671 if (format_addr.IsValid())
2672 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002673 var_success = false;
2674
Greg Clayton1b654882010-09-19 02:33:57 +00002675 if (calculate_format_addr_function_offset)
2676 {
2677 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002678
Greg Clayton0603aa92010-10-04 01:05:56 +00002679 if (sc)
2680 {
2681 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002682 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002683 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Jason Molendaaff1b352014-10-10 23:07:36 +00002684 if (sc->block && addr_offset_concrete_func_only == false)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002685 {
2686 // Check to make sure we aren't in an inline
2687 // function. If we are, use the inline block
2688 // range that contains "format_addr" since
2689 // blocks can be discontiguous.
2690 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2691 AddressRange inline_range;
2692 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2693 func_addr = inline_range.GetBaseAddress();
2694 }
2695 }
Greg Claytone7612132012-03-07 21:03:09 +00002696 else if (sc->symbol && sc->symbol->ValueIsAddress())
2697 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002698 }
2699
2700 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002701 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002702 const char *addr_offset_padding = " ";
2703 if (addr_offset_print_with_no_padding)
2704 {
2705 addr_offset_padding = "";
2706 }
Greg Clayton1b654882010-09-19 02:33:57 +00002707 if (func_addr.GetSection() == format_addr.GetSection())
2708 {
2709 addr_t func_file_addr = func_addr.GetFileAddress();
2710 addr_t addr_file_addr = format_addr.GetFileAddress();
2711 if (addr_file_addr > func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002712 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002713 else if (addr_file_addr < func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002714 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002715 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002716 }
2717 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002718 {
2719 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2720 if (target)
2721 {
2722 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2723 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2724 if (addr_load_addr > func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002725 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002726 else if (addr_load_addr < func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002727 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002728 var_success = true;
2729 }
2730 }
Greg Clayton1b654882010-09-19 02:33:57 +00002731 }
2732 }
2733 else
2734 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002735 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002736 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002737 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2738 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002739 if (vaddr == LLDB_INVALID_ADDRESS)
2740 vaddr = format_addr.GetFileAddress ();
2741
2742 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002743 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002744 int addr_width = 0;
2745 if (exe_ctx && target)
2746 {
Eric Christopherfd1a9362014-10-11 00:04:42 +00002747 addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Jason Molendaaff1b352014-10-10 23:07:36 +00002748 }
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002749 if (addr_width == 0)
2750 addr_width = 16;
Jason Molendaaff1b352014-10-10 23:07:36 +00002751 if (print_file_addr_or_load_addr)
2752 {
2753 format_addr.Dump (&s, exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress, 0);
2754 }
2755 else
2756 {
2757 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
2758 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002759 var_success = true;
2760 }
Greg Clayton1b654882010-09-19 02:33:57 +00002761 }
2762 }
2763 }
2764
2765 if (var_success == false)
2766 success = false;
2767 }
2768 p = var_name_end;
2769 }
2770 else
2771 break;
2772 }
2773 else
2774 {
2775 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2776 s.PutChar(*p);
2777 }
2778 }
2779 else if (*p == '\\')
2780 {
2781 ++p; // skip the slash
2782 switch (*p)
2783 {
2784 case 'a': s.PutChar ('\a'); break;
2785 case 'b': s.PutChar ('\b'); break;
2786 case 'f': s.PutChar ('\f'); break;
2787 case 'n': s.PutChar ('\n'); break;
2788 case 'r': s.PutChar ('\r'); break;
2789 case 't': s.PutChar ('\t'); break;
2790 case 'v': s.PutChar ('\v'); break;
2791 case '\'': s.PutChar ('\''); break;
2792 case '\\': s.PutChar ('\\'); break;
2793 case '0':
2794 // 1 to 3 octal chars
2795 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002796 // Make a string that can hold onto the initial zero char,
2797 // up to 3 octal digits, and a terminating NULL.
2798 char oct_str[5] = { 0, 0, 0, 0, 0 };
2799
2800 int i;
2801 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2802 oct_str[i] = p[i];
2803
2804 // We don't want to consume the last octal character since
2805 // the main for loop will do this for us, so we advance p by
2806 // one less than i (even if i is zero)
2807 p += i - 1;
2808 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2809 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002810 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002811 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002812 }
Greg Clayton1b654882010-09-19 02:33:57 +00002813 }
2814 break;
2815
2816 case 'x':
2817 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002818 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002819 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002820 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002821
Greg Clayton0603aa92010-10-04 01:05:56 +00002822 // Make a string that can hold onto two hex chars plus a
2823 // NULL terminator
2824 char hex_str[3] = { 0,0,0 };
2825 hex_str[0] = *p;
2826 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002827 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002828 ++p; // Skip the first of the two hex chars
2829 hex_str[1] = *p;
2830 }
2831
2832 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2833 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002834 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002835 }
2836 else
2837 {
2838 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002839 }
2840 break;
2841
2842 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002843 // Just desensitize any other character by just printing what
2844 // came after the '\'
2845 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002846 break;
2847
2848 }
2849
2850 }
2851 }
2852 if (end)
2853 *end = p;
2854 return success;
2855}
2856
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002857bool
2858Debugger::FormatPrompt
2859(
2860 const char *format,
2861 const SymbolContext *sc,
2862 const ExecutionContext *exe_ctx,
2863 const Address *addr,
2864 Stream &s,
2865 ValueObject* valobj
2866)
2867{
2868 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2869 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2870 if (format_str.length())
2871 format = format_str.c_str();
Jason Molendaaff1b352014-10-10 23:07:36 +00002872 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj, false, false);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002873}
2874
Jason Molendaaff1b352014-10-10 23:07:36 +00002875bool
2876Debugger::FormatDisassemblerAddress (const char *format,
2877 const SymbolContext *sc,
2878 const SymbolContext *prev_sc,
2879 const ExecutionContext *exe_ctx,
2880 const Address *addr,
2881 Stream &s)
2882{
2883 if (format == NULL && exe_ctx != NULL && exe_ctx->HasTargetScope())
2884 {
2885 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
2886 }
2887 bool function_changed = false;
2888 bool initial_function = false;
2889 if (prev_sc && (prev_sc->function || prev_sc->symbol))
2890 {
2891 if (sc && (sc->function || sc->symbol))
2892 {
2893 if (prev_sc->symbol && sc->symbol)
2894 {
2895 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
2896 {
2897 function_changed = true;
2898 }
2899 }
2900 else if (prev_sc->function && sc->function)
2901 {
2902 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
2903 {
2904 function_changed = true;
2905 }
2906 }
2907 }
2908 }
2909 // The first context on a list of instructions will have a prev_sc that
2910 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
2911 // would return false. But we do get a prev_sc pointer.
2912 if ((sc && (sc->function || sc->symbol))
2913 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
2914 {
2915 initial_function = true;
2916 }
2917 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, NULL, function_changed, initial_function);
2918}
2919
2920
Jim Ingham228063c2012-02-21 02:23:08 +00002921void
2922Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2923{
Jim Ingham4f02b222012-02-22 22:49:20 +00002924 // For simplicity's sake, I am not going to deal with how to close down any
2925 // open logging streams, I just redirect everything from here on out to the
2926 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002927 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2928}
2929
2930bool
2931Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2932{
2933 Log::Callbacks log_callbacks;
2934
2935 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002936 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002937 {
2938 log_stream_sp = m_log_callback_stream_sp;
2939 // For now when using the callback mode you always get thread & timestamp.
2940 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2941 }
2942 else if (log_file == NULL || *log_file == '\0')
2943 {
Greg Clayton44d93782014-01-27 23:43:24 +00002944 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002945 }
2946 else
2947 {
2948 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002949 if (pos != m_log_streams.end())
2950 log_stream_sp = pos->second.lock();
2951 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002952 {
2953 log_stream_sp.reset (new StreamFile (log_file));
2954 m_log_streams[log_file] = log_stream_sp;
2955 }
Jim Ingham228063c2012-02-21 02:23:08 +00002956 }
2957 assert (log_stream_sp.get());
2958
2959 if (log_options == 0)
2960 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2961
Greg Clayton57abc5d2013-05-10 21:47:16 +00002962 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002963 {
2964 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2965 return true;
2966 }
2967 else
2968 {
2969 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2970 if (log_channel_sp)
2971 {
2972 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2973 {
2974 return true;
2975 }
2976 else
2977 {
2978 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2979 return false;
2980 }
2981 }
2982 else
2983 {
2984 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2985 return false;
2986 }
2987 }
2988 return false;
2989}
2990
Greg Clayton9585fbf2013-03-19 00:20:55 +00002991SourceManager &
2992Debugger::GetSourceManager ()
2993{
2994 if (m_source_manager_ap.get() == NULL)
2995 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2996 return *m_source_manager_ap;
2997}
2998
2999
Greg Clayton44d93782014-01-27 23:43:24 +00003000
3001// This function handles events that were broadcast by the process.
3002void
3003Debugger::HandleBreakpointEvent (const EventSP &event_sp)
3004{
3005 using namespace lldb;
3006 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
3007
3008// if (event_type & eBreakpointEventTypeAdded
3009// || event_type & eBreakpointEventTypeRemoved
3010// || event_type & eBreakpointEventTypeEnabled
3011// || event_type & eBreakpointEventTypeDisabled
3012// || event_type & eBreakpointEventTypeCommandChanged
3013// || event_type & eBreakpointEventTypeConditionChanged
3014// || event_type & eBreakpointEventTypeIgnoreChanged
3015// || event_type & eBreakpointEventTypeLocationsResolved)
3016// {
3017// // Don't do anything about these events, since the breakpoint commands already echo these actions.
3018// }
3019//
3020 if (event_type & eBreakpointEventTypeLocationsAdded)
3021 {
3022 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
3023 if (num_new_locations > 0)
3024 {
3025 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
3026 StreamFileSP output_sp (GetOutputFile());
3027 if (output_sp)
3028 {
3029 output_sp->Printf("%d location%s added to breakpoint %d\n",
3030 num_new_locations,
3031 num_new_locations == 1 ? "" : "s",
3032 breakpoint->GetID());
3033 RefreshTopIOHandler();
3034 }
3035 }
3036 }
3037// else if (event_type & eBreakpointEventTypeLocationsRemoved)
3038// {
3039// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
3040// }
3041// else if (event_type & eBreakpointEventTypeLocationsResolved)
3042// {
3043// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
3044// }
3045}
3046
3047size_t
3048Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
3049{
3050 size_t total_bytes = 0;
3051 if (stream == NULL)
3052 stream = GetOutputFile().get();
3053
3054 if (stream)
3055 {
3056 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
3057 if (process == NULL)
3058 {
3059 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3060 if (target_sp)
3061 process = target_sp->GetProcessSP().get();
3062 }
3063 if (process)
3064 {
3065 Error error;
3066 size_t len;
3067 char stdio_buffer[1024];
3068 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3069 {
3070 stream->Write(stdio_buffer, len);
3071 total_bytes += len;
3072 }
3073 }
3074 stream->Flush();
3075 }
3076 return total_bytes;
3077}
3078
3079size_t
3080Debugger::GetProcessSTDERR (Process *process, Stream *stream)
3081{
3082 size_t total_bytes = 0;
3083 if (stream == NULL)
3084 stream = GetOutputFile().get();
3085
3086 if (stream)
3087 {
3088 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
3089 if (process == NULL)
3090 {
3091 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3092 if (target_sp)
3093 process = target_sp->GetProcessSP().get();
3094 }
3095 if (process)
3096 {
3097 Error error;
3098 size_t len;
3099 char stdio_buffer[1024];
3100 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3101 {
3102 stream->Write(stdio_buffer, len);
3103 total_bytes += len;
3104 }
3105 }
3106 stream->Flush();
3107 }
3108 return total_bytes;
3109}
3110
Greg Claytondc6224e2014-10-21 01:00:42 +00003111
Greg Clayton44d93782014-01-27 23:43:24 +00003112// This function handles events that were broadcast by the process.
3113void
3114Debugger::HandleProcessEvent (const EventSP &event_sp)
3115{
3116 using namespace lldb;
3117 const uint32_t event_type = event_sp->GetType();
3118 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00003119
Greg Claytonb4874f12014-02-28 18:22:24 +00003120 StreamString output_stream;
3121 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00003122 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00003123
Greg Claytonb4874f12014-02-28 18:22:24 +00003124 if (!gui_enabled)
3125 {
3126 bool pop_process_io_handler = false;
3127 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00003128
Greg Claytonb4874f12014-02-28 18:22:24 +00003129 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00003130 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003131 GetProcessSTDOUT (process_sp.get(), &output_stream);
3132 }
Greg Claytondc6224e2014-10-21 01:00:42 +00003133
Greg Claytonb4874f12014-02-28 18:22:24 +00003134 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
3135 {
3136 GetProcessSTDERR (process_sp.get(), &error_stream);
3137 }
Greg Claytondc6224e2014-10-21 01:00:42 +00003138
Greg Claytonb4874f12014-02-28 18:22:24 +00003139 if (event_type & Process::eBroadcastBitStateChanged)
3140 {
Greg Claytondc6224e2014-10-21 01:00:42 +00003141 Process::HandleProcessStateChangedEvent (event_sp, &output_stream, pop_process_io_handler);
Greg Clayton44d93782014-01-27 23:43:24 +00003142 }
Greg Claytondc6224e2014-10-21 01:00:42 +00003143
Greg Claytonb4874f12014-02-28 18:22:24 +00003144 if (output_stream.GetSize() || error_stream.GetSize())
3145 {
3146 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00003147 bool top_io_handler_hid = false;
Greg Claytondc6224e2014-10-21 01:00:42 +00003148
Greg Clayton6fea17e2014-03-03 19:15:20 +00003149 if (process_sp->ProcessIOHandlerIsActive() == false)
3150 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00003151
3152 if (output_stream.GetSize())
3153 {
3154 StreamFileSP output_stream_sp (GetOutputFile());
3155 if (output_stream_sp)
3156 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3157 }
3158
3159 if (error_stream.GetSize())
3160 {
3161 StreamFileSP error_stream_sp (GetErrorFile());
3162 if (error_stream_sp)
3163 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3164 }
3165
3166 if (top_io_handler_hid)
3167 RefreshTopIOHandler();
3168 }
3169
3170 if (pop_process_io_handler)
3171 process_sp->PopProcessIOHandler();
3172 }
Greg Clayton44d93782014-01-27 23:43:24 +00003173}
3174
3175void
3176Debugger::HandleThreadEvent (const EventSP &event_sp)
3177{
3178 // At present the only thread event we handle is the Frame Changed event,
3179 // and all we do for that is just reprint the thread status for that thread.
3180 using namespace lldb;
3181 const uint32_t event_type = event_sp->GetType();
3182 if (event_type == Thread::eBroadcastBitStackChanged ||
3183 event_type == Thread::eBroadcastBitThreadSelected )
3184 {
3185 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3186 if (thread_sp)
3187 {
3188 HideTopIOHandler();
3189 StreamFileSP stream_sp (GetOutputFile());
3190 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3191 RefreshTopIOHandler();
3192 }
3193 }
3194}
3195
3196bool
3197Debugger::IsForwardingEvents ()
3198{
3199 return (bool)m_forward_listener_sp;
3200}
3201
3202void
3203Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3204{
3205 m_forward_listener_sp = listener_sp;
3206}
3207
3208void
3209Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3210{
3211 m_forward_listener_sp.reset();
3212}
3213
3214
3215void
3216Debugger::DefaultEventHandler()
3217{
3218 Listener& listener(GetListener());
3219 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3220 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3221 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3222 BroadcastEventSpec target_event_spec (broadcaster_class_target,
3223 Target::eBroadcastBitBreakpointChanged);
3224
3225 BroadcastEventSpec process_event_spec (broadcaster_class_process,
3226 Process::eBroadcastBitStateChanged |
3227 Process::eBroadcastBitSTDOUT |
3228 Process::eBroadcastBitSTDERR);
3229
3230 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3231 Thread::eBroadcastBitStackChanged |
3232 Thread::eBroadcastBitThreadSelected );
3233
3234 listener.StartListeningForEventSpec (*this, target_event_spec);
3235 listener.StartListeningForEventSpec (*this, process_event_spec);
3236 listener.StartListeningForEventSpec (*this, thread_event_spec);
3237 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3238 CommandInterpreter::eBroadcastBitQuitCommandReceived |
3239 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
3240 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
3241
3242 bool done = false;
3243 while (!done)
3244 {
3245// Mutex::Locker locker;
3246// if (locker.TryLock(m_input_reader_stack.GetMutex()))
3247// {
3248// if (m_input_reader_stack.IsEmpty())
3249// break;
3250// }
3251//
3252 EventSP event_sp;
3253 if (listener.WaitForEvent(NULL, event_sp))
3254 {
3255 if (event_sp)
3256 {
3257 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3258 if (broadcaster)
3259 {
3260 uint32_t event_type = event_sp->GetType();
3261 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3262 if (broadcaster_class == broadcaster_class_process)
3263 {
3264 HandleProcessEvent (event_sp);
3265 }
3266 else if (broadcaster_class == broadcaster_class_target)
3267 {
3268 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3269 {
3270 HandleBreakpointEvent (event_sp);
3271 }
3272 }
3273 else if (broadcaster_class == broadcaster_class_thread)
3274 {
3275 HandleThreadEvent (event_sp);
3276 }
3277 else if (broadcaster == m_command_interpreter_ap.get())
3278 {
3279 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3280 {
3281 done = true;
3282 }
3283 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3284 {
3285 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3286 if (data && data[0])
3287 {
3288 StreamFileSP error_sp (GetErrorFile());
3289 if (error_sp)
3290 {
3291 HideTopIOHandler();
3292 error_sp->PutCString(data);
3293 error_sp->Flush();
3294 RefreshTopIOHandler();
3295 }
3296 }
3297 }
3298 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3299 {
3300 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3301 if (data && data[0])
3302 {
3303 StreamFileSP output_sp (GetOutputFile());
3304 if (output_sp)
3305 {
3306 HideTopIOHandler();
3307 output_sp->PutCString(data);
3308 output_sp->Flush();
3309 RefreshTopIOHandler();
3310 }
3311 }
3312 }
3313 }
3314 }
3315
3316 if (m_forward_listener_sp)
3317 m_forward_listener_sp->AddEvent(event_sp);
3318 }
3319 }
3320 }
3321}
3322
3323lldb::thread_result_t
3324Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3325{
3326 ((Debugger *)arg)->DefaultEventHandler();
3327 return NULL;
3328}
3329
3330bool
3331Debugger::StartEventHandlerThread()
3332{
Zachary Turneracee96a2014-09-23 18:32:09 +00003333 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00003334 {
Zachary Turner7c2896a2014-10-24 22:06:29 +00003335 // Use larger 8MB stack for this thread
3336 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread, this, NULL,
3337 g_debugger_event_thread_stack_bytes);
Greg Clayton807b6b32014-10-15 18:03:59 +00003338 }
Zachary Turneracee96a2014-09-23 18:32:09 +00003339 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003340}
3341
3342void
3343Debugger::StopEventHandlerThread()
3344{
Zachary Turneracee96a2014-09-23 18:32:09 +00003345 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003346 {
3347 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00003348 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003349 }
3350}
3351
3352
3353lldb::thread_result_t
3354Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3355{
3356 Debugger *debugger = (Debugger *)arg;
3357 debugger->ExecuteIOHanders();
3358 debugger->StopEventHandlerThread();
3359 return NULL;
3360}
3361
3362bool
3363Debugger::StartIOHandlerThread()
3364{
Zachary Turneracee96a2014-09-23 18:32:09 +00003365 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00003366 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
3367 IOHandlerThread,
3368 this,
3369 NULL,
3370 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00003371 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003372}
3373
3374void
3375Debugger::StopIOHandlerThread()
3376{
Zachary Turneracee96a2014-09-23 18:32:09 +00003377 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003378 {
3379 if (m_input_file_sp)
3380 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00003381 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003382 }
3383}
3384
3385