blob: 4a159145c6ec3039d0109bf4fd62af4181f4398b [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"
18
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/lldb-private.h"
20#include "lldb/Core/ConnectionFileDescriptor.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000030#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000031#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000032#include "lldb/DataFormatters/DataVisualization.h"
33#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000034#include "lldb/DataFormatters/TypeSummary.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000035#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000036#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000037#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000038#include "lldb/Interpreter/OptionValueSInt64.h"
39#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000040#include "lldb/Symbol/ClangASTContext.h"
41#include "lldb/Symbol/CompileUnit.h"
42#include "lldb/Symbol/Function.h"
43#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000044#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Target/TargetList.h"
46#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000047#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000048#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000049#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000050#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000052#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Greg Clayton1b654882010-09-19 02:33:57 +000058static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000059static lldb::user_id_t g_unique_id = 1;
60
Greg Clayton1b654882010-09-19 02:33:57 +000061#pragma mark Static Functions
62
63static Mutex &
64GetDebuggerListMutex ()
65{
66 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
67 return g_mutex;
68}
69
70typedef std::vector<DebuggerSP> DebuggerList;
71
72static DebuggerList &
73GetDebuggerList()
74{
75 // hide the static debugger list inside a singleton accessor to avoid
76 // global init contructors
77 static DebuggerList g_list;
78 return g_list;
79}
Greg Claytone372b982011-11-21 21:44:34 +000080
81OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000082g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000083{
Greg Clayton67cc0632012-08-22 17:17:09 +000084 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
85 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
86 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000087 { 0, NULL, NULL }
88};
89
Greg Clayton67cc0632012-08-22 17:17:09 +000090OptionEnumValueElement
91g_language_enumerators[] =
92{
93 { eScriptLanguageNone, "none", "Disable scripting languages."},
94 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
95 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000096 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000097};
Greg Claytone372b982011-11-21 21:44:34 +000098
Greg Clayton67cc0632012-08-22 17:17:09 +000099#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
100#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
101
Michael Sartain0769b2b2013-07-30 16:44:36 +0000102#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000103 "{, ${frame.pc}}"\
104 MODULE_WITH_FUNC\
105 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000106 "{, name = '${thread.name}'}"\
107 "{, queue = '${thread.queue}'}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000108 "{, stop reason = ${thread.stop-reason}}"\
109 "{\\nReturn value: ${thread.return-value}}"\
110 "\\n"
111
112#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
113 MODULE_WITH_FUNC\
114 FILE_AND_LINE\
115 "\\n"
116
117
118
Greg Clayton754a9362012-08-23 00:22:02 +0000119static PropertyDefinition
120g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000121{
122{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
123{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
124{ "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 +0000125{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000126{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
127{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
128{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
129{ "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." },
130{ "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." },
131{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
132{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
133{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000134{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
Enrico Granata90a8db32013-10-31 21:01:07 +0000135{ "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 +0000136
137 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000138};
139
140enum
141{
142 ePropertyAutoConfirm = 0,
143 ePropertyFrameFormat,
144 ePropertyNotiftVoid,
145 ePropertyPrompt,
146 ePropertyScriptLanguage,
147 ePropertyStopDisassemblyCount,
148 ePropertyStopDisassemblyDisplay,
149 ePropertyStopLineCountAfter,
150 ePropertyStopLineCountBefore,
151 ePropertyTerminalWidth,
152 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000153 ePropertyUseExternalEditor,
154 ePropertyUseColor,
Enrico Granata90a8db32013-10-31 21:01:07 +0000155 ePropertyAutoOneLineSummaries
Greg Clayton67cc0632012-08-22 17:17:09 +0000156};
157
Greg Clayton5fb8f792013-12-02 19:35:49 +0000158Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000159
160Error
161Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
162 VarSetOperationType op,
163 const char *property_path,
164 const char *value)
165{
Enrico Granata84a53df2013-05-20 22:29:23 +0000166 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
167 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000168 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000169 if (is_load_script && exe_ctx->GetTargetSP())
170 {
171 target_sp = exe_ctx->GetTargetSP();
172 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
173 }
Greg Clayton4c054102012-09-01 00:38:36 +0000174 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
175 if (error.Success())
176 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000177 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000178 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
179 {
180 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000181 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
182 if (str.length())
183 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000184 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000185 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
186 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
187 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000188 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
189 {
190 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
191 SetPrompt (GetPrompt());
192 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000193 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000194 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000195 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000196 {
197 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000198 StreamString feedback_stream;
199 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000200 {
Greg Clayton44d93782014-01-27 23:43:24 +0000201 StreamFileSP stream_sp (GetErrorFile());
202 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000203 {
Greg Clayton44d93782014-01-27 23:43:24 +0000204 for (auto error : errors)
205 {
206 stream_sp->Printf("%s\n",error.AsCString());
207 }
208 if (feedback_stream.GetSize())
209 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000210 }
211 }
212 }
213 }
Greg Clayton4c054102012-09-01 00:38:36 +0000214 }
215 return error;
216}
217
Greg Clayton67cc0632012-08-22 17:17:09 +0000218bool
219Debugger::GetAutoConfirm () const
220{
221 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000222 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000223}
224
225const char *
226Debugger::GetFrameFormat() const
227{
228 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000229 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000230}
231
232bool
233Debugger::GetNotifyVoid () const
234{
235 const uint32_t idx = ePropertyNotiftVoid;
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 *
240Debugger::GetPrompt() const
241{
242 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000243 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000244}
245
246void
247Debugger::SetPrompt(const char *p)
248{
249 const uint32_t idx = ePropertyPrompt;
250 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
251 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000252 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
253 if (str.length())
254 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000255 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000256}
257
258const char *
259Debugger::GetThreadFormat() const
260{
261 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000262 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000263}
264
265lldb::ScriptLanguage
266Debugger::GetScriptLanguage() const
267{
268 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000269 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000270}
271
272bool
273Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
274{
275 const uint32_t idx = ePropertyScriptLanguage;
276 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
277}
278
279uint32_t
280Debugger::GetTerminalWidth () const
281{
282 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000283 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000284}
285
286bool
287Debugger::SetTerminalWidth (uint32_t term_width)
288{
289 const uint32_t idx = ePropertyTerminalWidth;
290 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
291}
292
293bool
294Debugger::GetUseExternalEditor () const
295{
296 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000297 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000298}
299
300bool
301Debugger::SetUseExternalEditor (bool b)
302{
303 const uint32_t idx = ePropertyUseExternalEditor;
304 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
305}
306
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000307bool
308Debugger::GetUseColor () const
309{
310 const uint32_t idx = ePropertyUseColor;
311 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
312}
313
314bool
315Debugger::SetUseColor (bool b)
316{
317 const uint32_t idx = ePropertyUseColor;
318 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
319 SetPrompt (GetPrompt());
320 return ret;
321}
322
Greg Clayton67cc0632012-08-22 17:17:09 +0000323uint32_t
324Debugger::GetStopSourceLineCount (bool before) const
325{
326 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000327 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000328}
329
330Debugger::StopDisassemblyType
331Debugger::GetStopDisassemblyDisplay () const
332{
333 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000334 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000335}
336
337uint32_t
338Debugger::GetDisassemblyLineCount () const
339{
340 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000341 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000342}
Greg Claytone372b982011-11-21 21:44:34 +0000343
Enrico Granata553fad52013-10-25 23:09:40 +0000344bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000345Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000346{
Enrico Granata90a8db32013-10-31 21:01:07 +0000347 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000348 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
349
350}
351
Greg Clayton1b654882010-09-19 02:33:57 +0000352#pragma mark Debugger
353
Greg Clayton67cc0632012-08-22 17:17:09 +0000354//const DebuggerPropertiesSP &
355//Debugger::GetSettings() const
356//{
357// return m_properties_sp;
358//}
359//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000360
Caroline Tice2f88aad2011-01-14 00:29:16 +0000361int
362Debugger::TestDebuggerRefCount ()
363{
364 return g_shared_debugger_refcount;
365}
366
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000368Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000370 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000371 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000372 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373}
374
375void
376Debugger::Terminate ()
377{
Greg Clayton66111032010-06-23 01:19:29 +0000378 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 {
Greg Clayton66111032010-06-23 01:19:29 +0000380 g_shared_debugger_refcount--;
381 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 {
Greg Claytondbe54502010-11-19 03:46:01 +0000383 lldb_private::WillTerminate();
384 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000385
386 // Clear our master list of debugger objects
387 Mutex::Locker locker (GetDebuggerListMutex ());
388 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 }
391}
392
Caroline Tice20bd37f2011-03-10 22:14:10 +0000393void
394Debugger::SettingsInitialize ()
395{
Greg Clayton6920b522012-08-22 18:39:03 +0000396 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000397}
398
399void
400Debugger::SettingsTerminate ()
401{
Greg Clayton6920b522012-08-22 18:39:03 +0000402 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000403}
404
Enrico Granata21dfcd92012-09-28 23:57:51 +0000405bool
Enrico Granatae743c782013-04-24 21:29:08 +0000406Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000407{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000408 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000409 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000410 lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error);
411 if (dynlib_sp)
412 {
413 m_loaded_plugins.push_back(dynlib_sp);
414 return true;
415 }
Enrico Granatae743c782013-04-24 21:29:08 +0000416 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000417 else
Enrico Granatae743c782013-04-24 21:29:08 +0000418 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000419 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
420 // and if the public API layer isn't available (code is linking against
421 // all of the internal LLDB static libraries), then we can't load plugins
422 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000423 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000424 return false;
425}
426
427static FileSpec::EnumerateDirectoryResult
428LoadPluginCallback
429(
430 void *baton,
431 FileSpec::FileType file_type,
432 const FileSpec &file_spec
433 )
434{
435 Error error;
436
437 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000438 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000439
440 if (!baton)
441 return FileSpec::eEnumerateDirectoryResultQuit;
442
443 Debugger *debugger = (Debugger*)baton;
444
445 // If we have a regular file, a symbolic link or unknown file type, try
446 // and process the file. We must handle unknown as sometimes the directory
447 // enumeration might be enumerating a file system that doesn't have correct
448 // file type information.
449 if (file_type == FileSpec::eFileTypeRegular ||
450 file_type == FileSpec::eFileTypeSymbolicLink ||
451 file_type == FileSpec::eFileTypeUnknown )
452 {
453 FileSpec plugin_file_spec (file_spec);
454 plugin_file_spec.ResolvePath ();
455
Michael Sartain3cf443d2013-07-17 00:26:30 +0000456 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
457 plugin_file_spec.GetFileNameExtension() != g_solibext)
458 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000459 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000460 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000461
Enrico Granatae743c782013-04-24 21:29:08 +0000462 Error plugin_load_error;
463 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000464
465 return FileSpec::eEnumerateDirectoryResultNext;
466 }
467
468 else if (file_type == FileSpec::eFileTypeUnknown ||
469 file_type == FileSpec::eFileTypeDirectory ||
470 file_type == FileSpec::eFileTypeSymbolicLink )
471 {
472 // Try and recurse into anything that a directory or symbolic link.
473 // We must also do this for unknown as sometimes the directory enumeration
474 // might be enurating a file system that doesn't have correct file type
475 // information.
476 return FileSpec::eEnumerateDirectoryResultEnter;
477 }
478
479 return FileSpec::eEnumerateDirectoryResultNext;
480}
481
482void
483Debugger::InstanceInitialize ()
484{
485 FileSpec dir_spec;
486 const bool find_directories = true;
487 const bool find_files = true;
488 const bool find_other = true;
489 char dir_path[PATH_MAX];
490 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
491 {
492 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
493 {
494 FileSpec::EnumerateDirectory (dir_path,
495 find_directories,
496 find_files,
497 find_other,
498 LoadPluginCallback,
499 this);
500 }
501 }
502
503 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
504 {
505 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
506 {
507 FileSpec::EnumerateDirectory (dir_path,
508 find_directories,
509 find_files,
510 find_other,
511 LoadPluginCallback,
512 this);
513 }
514 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000515
516 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000517}
518
Greg Clayton66111032010-06-23 01:19:29 +0000519DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000520Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000521{
Jim Ingham228063c2012-02-21 02:23:08 +0000522 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000523 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000524 {
525 Mutex::Locker locker (GetDebuggerListMutex ());
526 GetDebuggerList().push_back(debugger_sp);
527 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000528 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000529 return debugger_sp;
530}
531
Caroline Ticee02657b2011-01-22 01:02:07 +0000532void
Greg Clayton4d122c42011-09-17 08:33:22 +0000533Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000534{
535 if (debugger_sp.get() == NULL)
536 return;
537
Jim Ingham8314c522011-09-15 21:36:42 +0000538 debugger_sp->Clear();
539
Greg Claytonc15f55e2012-03-30 20:53:46 +0000540 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000541 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000542 Mutex::Locker locker (GetDebuggerListMutex ());
543 DebuggerList &debugger_list = GetDebuggerList ();
544 DebuggerList::iterator pos, end = debugger_list.end();
545 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000546 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000547 if ((*pos).get() == debugger_sp.get())
548 {
549 debugger_list.erase (pos);
550 return;
551 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000552 }
553 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000554}
555
Greg Clayton4d122c42011-09-17 08:33:22 +0000556DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000557Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
558{
Greg Clayton4d122c42011-09-17 08:33:22 +0000559 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000560 if (g_shared_debugger_refcount > 0)
561 {
562 Mutex::Locker locker (GetDebuggerListMutex ());
563 DebuggerList &debugger_list = GetDebuggerList();
564 DebuggerList::iterator pos, end = debugger_list.end();
565
566 for (pos = debugger_list.begin(); pos != end; ++pos)
567 {
568 if ((*pos).get()->m_instance_name == instance_name)
569 {
570 debugger_sp = *pos;
571 break;
572 }
573 }
574 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000575 return debugger_sp;
576}
Greg Clayton66111032010-06-23 01:19:29 +0000577
578TargetSP
579Debugger::FindTargetWithProcessID (lldb::pid_t pid)
580{
Greg Clayton4d122c42011-09-17 08:33:22 +0000581 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000582 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000583 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000584 Mutex::Locker locker (GetDebuggerListMutex ());
585 DebuggerList &debugger_list = GetDebuggerList();
586 DebuggerList::iterator pos, end = debugger_list.end();
587 for (pos = debugger_list.begin(); pos != end; ++pos)
588 {
589 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
590 if (target_sp)
591 break;
592 }
Greg Clayton66111032010-06-23 01:19:29 +0000593 }
594 return target_sp;
595}
596
Greg Claytone4e45922011-11-16 05:37:56 +0000597TargetSP
598Debugger::FindTargetWithProcess (Process *process)
599{
600 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000601 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000602 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000603 Mutex::Locker locker (GetDebuggerListMutex ());
604 DebuggerList &debugger_list = GetDebuggerList();
605 DebuggerList::iterator pos, end = debugger_list.end();
606 for (pos = debugger_list.begin(); pos != end; ++pos)
607 {
608 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
609 if (target_sp)
610 break;
611 }
Greg Claytone4e45922011-11-16 05:37:56 +0000612 }
613 return target_sp;
614}
615
Jim Ingham228063c2012-02-21 02:23:08 +0000616Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000617 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000618 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Clayton44d93782014-01-27 23:43:24 +0000619 m_input_file_sp (new StreamFile (stdin, false)),
620 m_output_file_sp (new StreamFile (stdout, false)),
621 m_error_file_sp (new StreamFile (stderr, false)),
Jim Inghamc5917d92012-11-30 20:23:19 +0000622 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000623 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000624 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000626 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000627 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000628 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000629 m_input_reader_stack (),
Greg Clayton44d93782014-01-27 23:43:24 +0000630 m_instance_name (),
631 m_loaded_plugins (),
632 m_event_handler_thread (LLDB_INVALID_HOST_THREAD),
Greg Claytonb4874f12014-02-28 18:22:24 +0000633 m_io_handler_thread (LLDB_INVALID_HOST_THREAD)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634{
Greg Clayton67cc0632012-08-22 17:17:09 +0000635 char instance_cstr[256];
636 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
637 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000638 if (log_callback)
639 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000640 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000641 // Always add our default platform to the platform list
642 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
643 assert (default_platform_sp.get());
644 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000645
Greg Clayton754a9362012-08-23 00:22:02 +0000646 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000647 m_collection_sp->AppendProperty (ConstString("target"),
648 ConstString("Settings specify to debugging targets."),
649 true,
650 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000651 if (m_command_interpreter_ap.get())
652 {
653 m_collection_sp->AppendProperty (ConstString("interpreter"),
654 ConstString("Settings specify to the debugger's command interpreter."),
655 true,
656 m_command_interpreter_ap->GetValueProperties());
657 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000658 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
659 term_width->SetMinimumValue(10);
660 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000661
662 // Turn off use-color if this is a dumb terminal.
663 const char *term = getenv ("TERM");
664 if (term && !strcmp (term, "dumb"))
665 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666}
667
668Debugger::~Debugger ()
669{
Jim Ingham8314c522011-09-15 21:36:42 +0000670 Clear();
671}
672
673void
674Debugger::Clear()
675{
Greg Clayton44d93782014-01-27 23:43:24 +0000676 ClearIOHandlers();
677 StopIOHandlerThread();
678 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000679 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000680 int num_targets = m_target_list.GetNumTargets();
681 for (int i = 0; i < num_targets; i++)
682 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000683 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
684 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000685 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000686 ProcessSP process_sp (target_sp->GetProcessSP());
687 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000688 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000689 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000690 }
Greg Clayton66111032010-06-23 01:19:29 +0000691 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000692 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000693
694 // Close the input file _before_ we close the input read communications class
695 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000696 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000697 if (m_input_file_sp)
698 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000699
700 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000701}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702
703bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000704Debugger::GetCloseInputOnEOF () const
705{
Greg Clayton44d93782014-01-27 23:43:24 +0000706// return m_input_comm.GetCloseOnEOF();
707 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000708}
709
710void
711Debugger::SetCloseInputOnEOF (bool b)
712{
Greg Clayton44d93782014-01-27 23:43:24 +0000713// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000714}
715
716bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717Debugger::GetAsyncExecution ()
718{
Greg Clayton66111032010-06-23 01:19:29 +0000719 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720}
721
722void
723Debugger::SetAsyncExecution (bool async_execution)
724{
Greg Clayton66111032010-06-23 01:19:29 +0000725 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726}
727
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
729void
730Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
731{
Greg Clayton44d93782014-01-27 23:43:24 +0000732 if (m_input_file_sp)
733 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
734 else
735 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
736
737 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000738 if (in_file.IsValid() == false)
739 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740
Jim Inghamc5917d92012-11-30 20:23:19 +0000741 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
742 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743}
744
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745void
746Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
747{
Greg Clayton44d93782014-01-27 23:43:24 +0000748 if (m_output_file_sp)
749 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
750 else
751 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
752
753 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000754 if (out_file.IsValid() == false)
755 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000756
Enrico Granatab5887262012-10-29 21:18:03 +0000757 // do not create the ScriptInterpreter just for setting the output file handle
758 // as the constructor will know how to do the right thing on its own
759 const bool can_create = false;
760 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
761 if (script_interpreter)
762 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763}
764
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765void
766Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
767{
Greg Clayton44d93782014-01-27 23:43:24 +0000768 if (m_error_file_sp)
769 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
770 else
771 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
772
773 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000774 if (err_file.IsValid() == false)
775 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776}
777
Jim Inghamc5917d92012-11-30 20:23:19 +0000778void
779Debugger::SaveInputTerminalState ()
780{
Greg Clayton44d93782014-01-27 23:43:24 +0000781 if (m_input_file_sp)
782 {
783 File &in_file = m_input_file_sp->GetFile();
784 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
785 m_terminal_state.Save(in_file.GetDescriptor(), true);
786 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000787}
788
789void
790Debugger::RestoreInputTerminalState ()
791{
792 m_terminal_state.Restore();
793}
794
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000796Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797{
798 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000799 TargetSP target_sp(GetSelectedTarget());
800 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801
802 if (target_sp)
803 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000804 ProcessSP process_sp (target_sp->GetProcessSP());
805 exe_ctx.SetProcessSP (process_sp);
806 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000808 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
809 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000811 exe_ctx.SetThreadSP (thread_sp);
812 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
813 if (exe_ctx.GetFramePtr() == NULL)
814 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 }
816 }
817 }
818 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819}
820
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821void
Caroline Ticeefed6132010-11-19 20:47:54 +0000822Debugger::DispatchInputInterrupt ()
823{
Greg Clayton44d93782014-01-27 23:43:24 +0000824 Mutex::Locker locker (m_input_reader_stack.GetMutex());
825 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000826 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000827 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000828}
829
830void
831Debugger::DispatchInputEndOfFile ()
832{
Greg Clayton44d93782014-01-27 23:43:24 +0000833 Mutex::Locker locker (m_input_reader_stack.GetMutex());
834 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000835 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000836 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000837}
838
839void
Greg Clayton44d93782014-01-27 23:43:24 +0000840Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000841{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000842 // 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 +0000843 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000844 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000845 {
Greg Clayton44d93782014-01-27 23:43:24 +0000846 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000847 if (reader_sp)
848 {
Greg Clayton44d93782014-01-27 23:43:24 +0000849 m_input_reader_stack.Pop();
850 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000851 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000852 }
853 }
854}
855
856void
Greg Clayton44d93782014-01-27 23:43:24 +0000857Debugger::ExecuteIOHanders()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000858{
Caroline Tice9088b062011-05-09 23:06:58 +0000859
Greg Clayton44d93782014-01-27 23:43:24 +0000860 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 {
Greg Clayton44d93782014-01-27 23:43:24 +0000862 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863 if (!reader_sp)
864 break;
865
Greg Clayton44d93782014-01-27 23:43:24 +0000866 reader_sp->Activate();
867 reader_sp->Run();
868 reader_sp->Deactivate();
869
870 // Remove all input readers that are done from the top of the stack
871 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 {
Greg Clayton44d93782014-01-27 23:43:24 +0000873 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
874 if (top_reader_sp && top_reader_sp->GetIsDone())
875 m_input_reader_stack.Pop();
876 else
877 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 }
879 }
Greg Clayton44d93782014-01-27 23:43:24 +0000880 ClearIOHandlers();
881}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882
Greg Clayton44d93782014-01-27 23:43:24 +0000883bool
884Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
885{
886 return m_input_reader_stack.IsTop (reader_sp);
887}
888
889
890ConstString
891Debugger::GetTopIOHandlerControlSequence(char ch)
892{
893 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894}
895
896void
Greg Clayton44d93782014-01-27 23:43:24 +0000897Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
898{
899 Mutex::Locker locker (m_input_reader_stack.GetMutex());
900 PushIOHandler (reader_sp);
901 reader_sp->Activate();
902 reader_sp->Run();
903 PopIOHandler (reader_sp);
904}
905
906void
907Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
908{
909 // Before an IOHandler runs, it must have in/out/err streams.
910 // This function is called when one ore more of the streams
911 // are NULL. We use the top input reader's in/out/err streams,
912 // or fall back to the debugger file handles, or we fall back
913 // onto stdin/stdout/stderr as a last resort.
914
915 Mutex::Locker locker (m_input_reader_stack.GetMutex());
916 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
917 // If no STDIN has been set, then set it appropriately
918 if (!in)
919 {
920 if (top_reader_sp)
921 in = top_reader_sp->GetInputStreamFile();
922 else
923 in = GetInputFile();
924
925 // If there is nothing, use stdin
926 if (!in)
927 in = StreamFileSP(new StreamFile(stdin, false));
928 }
929 // If no STDOUT has been set, then set it appropriately
930 if (!out)
931 {
932 if (top_reader_sp)
933 out = top_reader_sp->GetOutputStreamFile();
934 else
935 out = GetOutputFile();
936
937 // If there is nothing, use stdout
938 if (!out)
939 out = StreamFileSP(new StreamFile(stdout, false));
940 }
941 // If no STDERR has been set, then set it appropriately
942 if (!err)
943 {
944 if (top_reader_sp)
945 err = top_reader_sp->GetErrorStreamFile();
946 else
947 err = GetErrorFile();
948
949 // If there is nothing, use stderr
950 if (!err)
951 err = StreamFileSP(new StreamFile(stdout, false));
952
953 }
954}
955
956void
957Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958{
959 if (!reader_sp)
960 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000961
Greg Clayton44d93782014-01-27 23:43:24 +0000962 // Got the current top input reader...
963 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000964
Greg Claytonb4874f12014-02-28 18:22:24 +0000965 // Don't push the same IO handler twice...
966 if (reader_sp.get() != top_reader_sp.get())
967 {
968 // Push our new input reader
969 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000970
Greg Claytonb4874f12014-02-28 18:22:24 +0000971 // Interrupt the top input reader to it will exit its Run() function
972 // and let this new input reader take over
973 if (top_reader_sp)
974 top_reader_sp->Deactivate();
975 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976}
977
978bool
Greg Clayton44d93782014-01-27 23:43:24 +0000979Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980{
981 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +0000982
983 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984
985 // The reader on the stop of the stack is done, so let the next
986 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000987 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988 {
Greg Clayton44d93782014-01-27 23:43:24 +0000989 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990
991 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
992 {
Greg Clayton44d93782014-01-27 23:43:24 +0000993 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +0000994 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000995 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +0000996
997 reader_sp = m_input_reader_stack.Top();
998 if (reader_sp)
999 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000
Greg Clayton44d93782014-01-27 23:43:24 +00001001 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002 }
1003 }
1004 return result;
1005}
1006
1007bool
Greg Clayton44d93782014-01-27 23:43:24 +00001008Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009{
Greg Clayton44d93782014-01-27 23:43:24 +00001010 Mutex::Locker locker;
1011
1012 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 {
Greg Clayton44d93782014-01-27 23:43:24 +00001014 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1015 if (reader_sp)
1016 reader_sp->Hide();
1017 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018 }
Greg Clayton44d93782014-01-27 23:43:24 +00001019 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020}
1021
1022void
Greg Clayton44d93782014-01-27 23:43:24 +00001023Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024{
Greg Clayton44d93782014-01-27 23:43:24 +00001025 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1026 if (reader_sp)
1027 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028}
Greg Clayton66111032010-06-23 01:19:29 +00001029
Greg Clayton44d93782014-01-27 23:43:24 +00001030
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001031StreamSP
1032Debugger::GetAsyncOutputStream ()
1033{
1034 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1035 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1036}
1037
1038StreamSP
1039Debugger::GetAsyncErrorStream ()
1040{
1041 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1042 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1043}
1044
Greg Claytonc7bece562013-01-25 18:06:21 +00001045size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001046Debugger::GetNumDebuggers()
1047{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001048 if (g_shared_debugger_refcount > 0)
1049 {
1050 Mutex::Locker locker (GetDebuggerListMutex ());
1051 return GetDebuggerList().size();
1052 }
1053 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001054}
1055
1056lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001057Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001058{
1059 DebuggerSP debugger_sp;
1060
Greg Claytonc15f55e2012-03-30 20:53:46 +00001061 if (g_shared_debugger_refcount > 0)
1062 {
1063 Mutex::Locker locker (GetDebuggerListMutex ());
1064 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001065
Greg Claytonc15f55e2012-03-30 20:53:46 +00001066 if (index < debugger_list.size())
1067 debugger_sp = debugger_list[index];
1068 }
1069
Enrico Granata061858c2012-02-15 02:34:21 +00001070 return debugger_sp;
1071}
1072
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001073DebuggerSP
1074Debugger::FindDebuggerWithID (lldb::user_id_t id)
1075{
Greg Clayton4d122c42011-09-17 08:33:22 +00001076 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001077
Greg Claytonc15f55e2012-03-30 20:53:46 +00001078 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001079 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001080 Mutex::Locker locker (GetDebuggerListMutex ());
1081 DebuggerList &debugger_list = GetDebuggerList();
1082 DebuggerList::iterator pos, end = debugger_list.end();
1083 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001084 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001085 if ((*pos).get()->GetID() == id)
1086 {
1087 debugger_sp = *pos;
1088 break;
1089 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001090 }
1091 }
1092 return debugger_sp;
1093}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001094
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001095#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001096static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001097TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001098{
1099 if (frame == NULL)
1100 return;
1101
1102 StreamString s;
1103 const char *prompt_format =
1104 "{addr = '${addr}'\n}"
1105 "{process.id = '${process.id}'\n}"
1106 "{process.name = '${process.name}'\n}"
1107 "{process.file.basename = '${process.file.basename}'\n}"
1108 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1109 "{thread.id = '${thread.id}'\n}"
1110 "{thread.index = '${thread.index}'\n}"
1111 "{thread.name = '${thread.name}'\n}"
1112 "{thread.queue = '${thread.queue}'\n}"
1113 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1114 "{target.arch = '${target.arch}'\n}"
1115 "{module.file.basename = '${module.file.basename}'\n}"
1116 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1117 "{file.basename = '${file.basename}'\n}"
1118 "{file.fullpath = '${file.fullpath}'\n}"
1119 "{frame.index = '${frame.index}'\n}"
1120 "{frame.pc = '${frame.pc}'\n}"
1121 "{frame.sp = '${frame.sp}'\n}"
1122 "{frame.fp = '${frame.fp}'\n}"
1123 "{frame.flags = '${frame.flags}'\n}"
1124 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1125 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1126 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1127 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1128 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1129 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1130 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1131 "{function.id = '${function.id}'\n}"
1132 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001133 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001134 "{function.addr-offset = '${function.addr-offset}'\n}"
1135 "{function.line-offset = '${function.line-offset}'\n}"
1136 "{function.pc-offset = '${function.pc-offset}'\n}"
1137 "{line.file.basename = '${line.file.basename}'\n}"
1138 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1139 "{line.number = '${line.number}'\n}"
1140 "{line.start-addr = '${line.start-addr}'\n}"
1141 "{line.end-addr = '${line.end-addr}'\n}"
1142;
1143
1144 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1145 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001146 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001147 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001148 {
1149 printf("%s\n", s.GetData());
1150 }
1151 else
1152 {
Greg Clayton1b654882010-09-19 02:33:57 +00001153 printf ("what we got: %s\n", s.GetData());
1154 }
1155}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001156#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001157
Enrico Granata9fc19442011-07-06 02:13:41 +00001158static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001159ScanFormatDescriptor (const char* var_name_begin,
1160 const char* var_name_end,
1161 const char** var_name_final,
1162 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001163 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001164 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001165{
Greg Clayton5160ce52013-03-27 23:08:40 +00001166 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001167 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001168 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001169 {
1170 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001171 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001172 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001173 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001174 else
1175 {
1176 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001177 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001178 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001179 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001180 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001181 true,
1182 *custom_format) )
1183 {
Enrico Granatae992a082011-07-22 17:03:19 +00001184 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001185 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001186
1187 switch (format_name.front())
1188 {
1189 case '@': // if this is an @ sign, print ObjC description
1190 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1191 break;
1192 case 'V': // if this is a V, print the value using the default format
1193 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1194 break;
1195 case 'L': // if this is an L, print the location of the value
1196 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1197 break;
1198 case 'S': // if this is an S, print the summary after all
1199 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1200 break;
1201 case '#': // if this is a '#', print the number of children
1202 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1203 break;
1204 case 'T': // if this is a 'T', print the type
1205 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1206 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001207 case 'N': // if this is a 'N', print the name
1208 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1209 break;
1210 case '>': // if this is a '>', print the name
1211 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1212 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001213 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001214 if (log)
1215 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001216 break;
1217 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001218 }
1219 // a good custom format tells us to print the value using it
1220 else
Enrico Granatae992a082011-07-22 17:03:19 +00001221 {
1222 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001223 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001224 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001225 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001226 }
Enrico Granatae992a082011-07-22 17:03:19 +00001227 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001228 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001229 *custom_format,
1230 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001231 return true;
1232}
1233
1234static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001235ScanBracketedRange (const char* var_name_begin,
1236 const char* var_name_end,
1237 const char* var_name_final,
1238 const char** open_bracket_position,
1239 const char** separator_position,
1240 const char** close_bracket_position,
1241 const char** var_name_final_if_array_range,
1242 int64_t* index_lower,
1243 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001244{
Greg Clayton5160ce52013-03-27 23:08:40 +00001245 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001246 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001247 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001248 {
1249 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1250 *close_bracket_position = ::strchr(*open_bracket_position,']');
1251 // as usual, we assume that [] will come before %
1252 //printf("trying to expand a []\n");
1253 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001254 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001255 {
Enrico Granatae992a082011-07-22 17:03:19 +00001256 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001257 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001258 *index_lower = 0;
1259 }
1260 else if (*separator_position == NULL || *separator_position > var_name_end)
1261 {
1262 char *end = NULL;
1263 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1264 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001265 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001266 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001267 }
Greg Clayton34132752011-07-06 04:07:21 +00001268 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001269 {
1270 char *end = NULL;
1271 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1272 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001273 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001274 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001275 }
1276 else
Enrico Granatae992a082011-07-22 17:03:19 +00001277 {
1278 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001279 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001280 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001281 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001282 if (*index_lower > *index_higher && *index_higher > 0)
1283 {
Enrico Granatae992a082011-07-22 17:03:19 +00001284 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001285 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001286 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001287 *index_lower = *index_higher;
1288 *index_higher = temp;
1289 }
1290 }
Enrico Granatae992a082011-07-22 17:03:19 +00001291 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001292 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001293 return true;
1294}
1295
Michael Sartain0769b2b2013-07-30 16:44:36 +00001296template <typename T>
1297static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1298{
1299 if (script_interpreter)
1300 {
1301 Error script_error;
1302 std::string script_output;
1303
1304 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1305 {
1306 s.Printf("%s", script_output.c_str());
1307 return true;
1308 }
1309 else
1310 {
1311 s.Printf("<error: %s>",script_error.AsCString());
1312 }
1313 }
1314 return false;
1315}
1316
Enrico Granata9fc19442011-07-06 02:13:41 +00001317static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001318ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001319 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001320 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001321 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001322{
Greg Clayton5160ce52013-03-27 23:08:40 +00001323 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001324 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001325 std::string ptr_deref_buffer(10,0);
1326 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001327 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001328 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001329 const char* first_unparsed;
1330 ValueObject::GetValueForExpressionPathOptions options;
1331 ValueObject::ExpressionPathEndResultType final_value_type;
1332 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001333 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001334 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001335 &first_unparsed,
1336 &reason_to_stop,
1337 &final_value_type,
1338 options,
1339 &what_next);
1340 if (!item)
1341 {
Enrico Granatae992a082011-07-22 17:03:19 +00001342 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001343 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001344 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001345 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001346 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001347 else
1348 {
Enrico Granatae992a082011-07-22 17:03:19 +00001349 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001350 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001351 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001352 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001353 }
1354 return item;
1355}
1356
Michael Sartain0769b2b2013-07-30 16:44:36 +00001357static inline bool
1358IsToken(const char *var_name_begin, const char *var)
1359{
1360 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1361}
1362
1363static bool
1364IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1365 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1366{
1367 int var_len = strlen(var);
1368 if (::strncmp (var_name_begin, var, var_len) == 0)
1369 {
1370 var_name_begin += var_len;
1371 if (*var_name_begin == '}')
1372 {
1373 format = default_format;
1374 return true;
1375 }
1376 else if (*var_name_begin == '%')
1377 {
1378 // Allow format specifiers: x|X|u with optional width specifiers.
1379 // ${thread.id%x} ; hex
1380 // ${thread.id%X} ; uppercase hex
1381 // ${thread.id%u} ; unsigned decimal
1382 // ${thread.id%8.8X} ; width.precision + specifier
1383 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1384 int dot_count = 0;
1385 const char *specifier = NULL;
1386 int width_precision_length = 0;
1387 const char *width_precision = ++var_name_begin;
1388 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1389 {
1390 dot_count += (*var_name_begin == '.');
1391 if (dot_count > 1)
1392 break;
1393 var_name_begin++;
1394 width_precision_length++;
1395 }
1396
1397 if (IsToken (var_name_begin, "tid}"))
1398 {
1399 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1400 if (target)
1401 {
1402 ArchSpec arch (target->GetArchitecture ());
1403 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1404 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1405 specifier = PRIu64;
1406 }
1407 if (!specifier)
1408 {
1409 format = default_format;
1410 return true;
1411 }
1412 }
1413 else if (IsToken (var_name_begin, "x}"))
1414 specifier = PRIx64;
1415 else if (IsToken (var_name_begin, "X}"))
1416 specifier = PRIX64;
1417 else if (IsToken (var_name_begin, "u}"))
1418 specifier = PRIu64;
1419
1420 if (specifier)
1421 {
1422 format = "%";
1423 if (width_precision_length)
1424 format += std::string(width_precision, width_precision_length);
1425 format += specifier;
1426 return true;
1427 }
1428 }
1429 }
1430 return false;
1431}
1432
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001433static bool
1434FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001435(
1436 const char *format,
1437 const SymbolContext *sc,
1438 const ExecutionContext *exe_ctx,
1439 const Address *addr,
1440 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001441 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001442 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001443)
1444{
Enrico Granatac482a192011-08-17 22:13:59 +00001445 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001446 bool success = true;
1447 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001448 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001449
Greg Clayton1b654882010-09-19 02:33:57 +00001450 for (p = format; *p != '\0'; ++p)
1451 {
Enrico Granatac482a192011-08-17 22:13:59 +00001452 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001453 {
Enrico Granatac482a192011-08-17 22:13:59 +00001454 valobj = realvalobj;
1455 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001456 }
Greg Clayton1b654882010-09-19 02:33:57 +00001457 size_t non_special_chars = ::strcspn (p, "${}\\");
1458 if (non_special_chars > 0)
1459 {
1460 if (success)
1461 s.Write (p, non_special_chars);
1462 p += non_special_chars;
1463 }
1464
1465 if (*p == '\0')
1466 {
1467 break;
1468 }
1469 else if (*p == '{')
1470 {
1471 // Start a new scope that must have everything it needs if it is to
1472 // to make it into the final output stream "s". If you want to make
1473 // a format that only prints out the function or symbol name if there
1474 // is one in the symbol context you can use:
1475 // "{function =${function.name}}"
1476 // The first '{' starts a new scope that end with the matching '}' at
1477 // the end of the string. The contents "function =${function.name}"
1478 // will then be evaluated and only be output if there is a function
1479 // or symbol with a valid name.
1480 StreamString sub_strm;
1481
1482 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001483
1484 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001485 {
1486 // The stream had all it needed
1487 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1488 }
1489 if (*p != '}')
1490 {
1491 success = false;
1492 break;
1493 }
1494 }
1495 else if (*p == '}')
1496 {
1497 // End of a enclosing scope
1498 break;
1499 }
1500 else if (*p == '$')
1501 {
1502 // We have a prompt variable to print
1503 ++p;
1504 if (*p == '{')
1505 {
1506 ++p;
1507 const char *var_name_begin = p;
1508 const char *var_name_end = ::strchr (p, '}');
1509
1510 if (var_name_end && var_name_begin < var_name_end)
1511 {
1512 // if we have already failed to parse, skip this variable
1513 if (success)
1514 {
1515 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001516 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001517 Address format_addr;
1518 bool calculate_format_addr_function_offset = false;
1519 // Set reg_kind and reg_num to invalid values
1520 RegisterKind reg_kind = kNumRegisterKinds;
1521 uint32_t reg_num = LLDB_INVALID_REGNUM;
1522 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001523 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001524 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001525 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001526 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1527 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001528
Greg Clayton1b654882010-09-19 02:33:57 +00001529 // Each variable must set success to true below...
1530 bool var_success = false;
1531 switch (var_name_begin[0])
1532 {
Enrico Granata4becb372011-06-29 22:27:15 +00001533 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001534 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001535 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001536 {
Enrico Granatac482a192011-08-17 22:13:59 +00001537 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001538 break;
1539
Enrico Granatac3e320a2011-08-02 17:27:39 +00001540 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001541 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001542
Enrico Granata6f3533f2011-07-29 19:53:35 +00001543 // check for *var and *svar
1544 if (*var_name_begin == '*')
1545 {
1546 do_deref_pointer = true;
1547 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001548 if (log)
1549 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001550 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001551
Enrico Granata6f3533f2011-07-29 19:53:35 +00001552 if (*var_name_begin == 's')
1553 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001554 if (!valobj->IsSynthetic())
1555 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001556 if (!valobj)
1557 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001558 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001559 if (log)
1560 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001561 }
1562
1563 // should be a 'v' by now
1564 if (*var_name_begin != 'v')
1565 break;
1566
Enrico Granatac3e320a2011-08-02 17:27:39 +00001567 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001568 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001569
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001570 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001571 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001572 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001573 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001574 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001575 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001576 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001577 const char* var_name_final = NULL;
1578 const char* var_name_final_if_array_range = NULL;
1579 const char* close_bracket_position = NULL;
1580 int64_t index_lower = -1;
1581 int64_t index_higher = -1;
1582 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001583 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001584 bool was_plain_var = false;
1585 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001586 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001587
Enrico Granatac482a192011-08-17 22:13:59 +00001588 if (!valobj) break;
1589 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001590 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001591 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001592 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001593 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001594 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001595 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001596 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001597 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001598 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001599 // this is a variable with some custom format applied to it
1600 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001601 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001602 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001603 ScanFormatDescriptor (var_name_begin,
1604 var_name_end,
1605 &var_name_final,
1606 &percent_position,
1607 &custom_format,
1608 &val_obj_display);
1609 }
1610 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001611 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001612 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001613 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001614 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001615 const char* percent_position;
1616 ScanFormatDescriptor (var_name_begin,
1617 var_name_end,
1618 &var_name_final,
1619 &percent_position,
1620 &custom_format,
1621 &val_obj_display);
1622
1623 const char* open_bracket_position;
1624 const char* separator_position;
1625 ScanBracketedRange (var_name_begin,
1626 var_name_end,
1627 var_name_final,
1628 &open_bracket_position,
1629 &separator_position,
1630 &close_bracket_position,
1631 &var_name_final_if_array_range,
1632 &index_lower,
1633 &index_higher);
1634
1635 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001636
Enrico Granata599171a2013-02-01 23:59:44 +00001637 std::string expr_path(var_name_final-var_name_begin-1,0);
1638 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1639
1640 if (log)
1641 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1642
1643 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001644 &first_unparsed,
1645 &reason_to_stop,
1646 &final_value_type,
1647 options,
1648 &what_next).get();
1649
1650 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001651 {
Enrico Granatae992a082011-07-22 17:03:19 +00001652 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001653 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001654 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001655 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001656 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001657 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001658 else
1659 {
Enrico Granatae992a082011-07-22 17:03:19 +00001660 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001661 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001662 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001663 first_unparsed, reason_to_stop, final_value_type);
1664 }
Enrico Granata4becb372011-06-29 22:27:15 +00001665 }
Greg Clayton34132752011-07-06 04:07:21 +00001666 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001667 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001668
Enrico Granata86cc9822012-03-19 22:58:49 +00001669 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1670 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001671
Enrico Granata86cc9822012-03-19 22:58:49 +00001672 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001673
Enrico Granataa7187d02011-07-06 19:27:11 +00001674 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001675 {
Greg Clayton34132752011-07-06 04:07:21 +00001676 // I have not deref-ed yet, let's do it
1677 // this happens when we are not going through GetValueForVariableExpressionPath
1678 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001679 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001680 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001681 if (error.Fail())
1682 {
1683 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001684 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001685 break;
1686 }
Greg Clayton34132752011-07-06 04:07:21 +00001687 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001688 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001689
Jim Inghamf164d942014-03-11 18:17:23 +00001690 if (!target)
1691 {
1692 if (log)
1693 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1694 break;
1695 }
1696
Enrico Granataa777dc22012-05-08 21:49:57 +00001697 // we do not want to use the summary for a bitfield of type T:n
1698 // if we were originally dealing with just a T - that would get
1699 // us into an endless recursion
1700 if (target->IsBitfield() && was_var_indexed)
1701 {
1702 // TODO: check for a (T:n)-specific summary - we should still obey that
1703 StreamString bitfield_name;
1704 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1705 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1706 if (!DataVisualization::GetSummaryForType(type_sp))
1707 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1708 }
1709
Enrico Granata85933ed2011-08-18 16:38:26 +00001710 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001711 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1712 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1713 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1714 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001715
Enrico Granata86cc9822012-03-19 22:58:49 +00001716 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 +00001717 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001718 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001719 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001720 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001721
Greg Clayton5088c482013-03-25 21:06:13 +00001722 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001723 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001724 // try to use the special cases
1725 var_success = target->DumpPrintableRepresentation(str_temp,
1726 val_obj_display,
1727 custom_format);
1728 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001729 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001730
1731 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001732 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001733 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001734 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001735 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001736 }
1737 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001738 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001739 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001740 {
1741 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1742 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001743 else if (is_pointer) // if pointer, value is the address stored
1744 {
Greg Clayton23f59502012-07-17 03:23:13 +00001745 target->DumpPrintableRepresentation (s,
1746 val_obj_display,
1747 custom_format,
1748 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001749 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001750 var_success = true;
1751 break;
1752 }
1753 }
1754
1755 // if directly trying to print ${var}, and this is an aggregate, display a nice
1756 // type @ location message
1757 if (is_aggregate && was_plain_var)
1758 {
1759 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1760 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001761 break;
1762 }
1763
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001764 // 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 +00001765 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001766 {
1767 s << "<invalid use of aggregate type>";
1768 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001769 break;
1770 }
Greg Clayton34132752011-07-06 04:07:21 +00001771
1772 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001773 {
1774 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001775 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001776 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001777 }
Greg Clayton34132752011-07-06 04:07:21 +00001778 else
Enrico Granatae992a082011-07-22 17:03:19 +00001779 {
1780 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001781 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001782 if (!is_array && !is_pointer)
1783 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001784 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001785 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001786 const char* special_directions = NULL;
1787 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001788 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1789 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001790 ConstString additional_data;
1791 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1792 special_directions_writer.Printf("${%svar%s}",
1793 do_deref_pointer ? "*" : "",
1794 additional_data.GetCString());
1795 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001796 }
1797
1798 // let us display items index_lower thru index_higher of this array
1799 s.PutChar('[');
1800 var_success = true;
1801
1802 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001803 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001804
Greg Claytoncc4d0142012-02-17 07:49:44 +00001805 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001806
Greg Clayton34132752011-07-06 04:07:21 +00001807 for (;index_lower<=index_higher;index_lower++)
1808 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001809 ValueObject* item = ExpandIndexedExpression (target,
1810 index_lower,
1811 exe_ctx->GetFramePtr(),
1812 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001813
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001814 if (!item)
1815 {
Enrico Granatae992a082011-07-22 17:03:19 +00001816 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001817 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001818 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001819 else
1820 {
Enrico Granatae992a082011-07-22 17:03:19 +00001821 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001822 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001823 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001824
Greg Clayton34132752011-07-06 04:07:21 +00001825 if (!special_directions)
1826 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1827 else
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001828 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
Greg Clayton34132752011-07-06 04:07:21 +00001829
Enrico Granata22c55d12011-08-12 02:00:06 +00001830 if (--max_num_children == 0)
1831 {
1832 s.PutCString(", ...");
1833 break;
1834 }
1835
Greg Clayton34132752011-07-06 04:07:21 +00001836 if (index_lower < index_higher)
1837 s.PutChar(',');
1838 }
1839 s.PutChar(']');
1840 }
Enrico Granata4becb372011-06-29 22:27:15 +00001841 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001842 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001843 case 'a':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001844 if (IsToken (var_name_begin, "addr}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001845 {
1846 if (addr && addr->IsValid())
1847 {
1848 var_success = true;
1849 format_addr = *addr;
1850 }
1851 }
1852 break;
1853
1854 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001855 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00001856 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001857 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001858 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001859 Process *process = exe_ctx->GetProcessPtr();
1860 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001861 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001862 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001863 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001864 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001865 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001866 var_success = true;
1867 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001868 else if ((IsToken (var_name_begin, "name}")) ||
1869 (IsToken (var_name_begin, "file.basename}")) ||
1870 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00001871 {
1872 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1873 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001874 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001875 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1876 {
1877 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001878 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001879 }
1880 else
1881 {
1882 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001883 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001884 }
Greg Clayton1b654882010-09-19 02:33:57 +00001885 }
1886 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001887 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001888 {
1889 var_name_begin += ::strlen("script:");
1890 std::string script_name(var_name_begin,var_name_end);
1891 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001892 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
1893 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001894 }
Greg Clayton1b654882010-09-19 02:33:57 +00001895 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001896 }
Greg Clayton1b654882010-09-19 02:33:57 +00001897 }
1898 break;
1899
1900 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001901 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00001902 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001903 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001904 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001905 Thread *thread = exe_ctx->GetThreadPtr();
1906 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001907 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001908 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001909 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001910 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001911 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001912 var_success = true;
1913 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001914 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00001915 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001916 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00001917 var_success = true;
1918 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001919 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00001920 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001921 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001922 var_success = true;
1923 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001924 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001925 {
1926 cstr = thread->GetName();
1927 var_success = cstr && cstr[0];
1928 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001929 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001930 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001931 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001932 {
1933 cstr = thread->GetQueueName();
1934 var_success = cstr && cstr[0];
1935 if (var_success)
1936 s.PutCString(cstr);
1937 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001938 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001939 {
1940 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001941 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001942 {
1943 cstr = stop_info_sp->GetDescription();
1944 if (cstr && cstr[0])
1945 {
1946 s.PutCString(cstr);
1947 var_success = true;
1948 }
Greg Clayton1b654882010-09-19 02:33:57 +00001949 }
1950 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001951 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00001952 {
1953 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001954 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001955 {
1956 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1957 if (return_valobj_sp)
1958 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00001959 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00001960 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001961 }
1962 }
1963 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001964 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001965 {
1966 var_name_begin += ::strlen("script:");
1967 std::string script_name(var_name_begin,var_name_end);
1968 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001969 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
1970 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001971 }
Greg Clayton1b654882010-09-19 02:33:57 +00001972 }
1973 }
1974 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001975 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00001976 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001977 // TODO: hookup properties
1978// if (!target_properties_sp)
1979// {
1980// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1981// if (target)
1982// target_properties_sp = target->GetProperties();
1983// }
1984//
1985// if (target_properties_sp)
1986// {
1987// var_name_begin += ::strlen ("target.");
1988// const char *end_property = strchr(var_name_begin, '}');
1989// if (end_property)
1990// {
1991// ConstString property_name(var_name_begin, end_property - var_name_begin);
1992// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
1993// if (!property_value.empty())
1994// {
1995// s.PutCString (property_value.c_str());
1996// var_success = true;
1997// }
1998// }
1999// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002000 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2001 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002002 {
Greg Clayton1b654882010-09-19 02:33:57 +00002003 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002004 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002005 {
2006 ArchSpec arch (target->GetArchitecture ());
2007 if (arch.IsValid())
2008 {
Greg Clayton64195a22011-02-23 00:35:02 +00002009 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002010 var_success = true;
2011 }
2012 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002013 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002014 {
2015 var_name_begin += ::strlen("script:");
2016 std::string script_name(var_name_begin,var_name_end);
2017 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002018 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2019 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002020 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002021 }
Greg Clayton1b654882010-09-19 02:33:57 +00002022 }
2023 break;
2024
2025
2026 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002027 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002028 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002029 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002030 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002031 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002032 var_name_begin += ::strlen ("module.");
2033
Michael Sartain0769b2b2013-07-30 16:44:36 +00002034 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002035 {
2036 if (module->GetFileSpec())
2037 {
2038 var_name_begin += ::strlen ("file.");
2039
Michael Sartain0769b2b2013-07-30 16:44:36 +00002040 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002041 {
2042 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002043 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002044 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002045 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002046 {
2047 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002048 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002049 }
2050 }
2051 }
2052 }
2053 }
2054 break;
2055
2056
2057 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002058 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002059 {
2060 if (sc && sc->comp_unit != NULL)
2061 {
2062 var_name_begin += ::strlen ("file.");
2063
Michael Sartain0769b2b2013-07-30 16:44:36 +00002064 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002065 {
2066 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002067 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002068 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002069 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002070 {
2071 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002072 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002073 }
2074 }
2075 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002076 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002077 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002078 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002079 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002080 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002081 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002082 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002083 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002084 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002085 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002086 s.Printf("%u", frame->GetFrameIndex());
2087 var_success = true;
2088 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002089 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002090 {
2091 reg_kind = eRegisterKindGeneric;
2092 reg_num = LLDB_REGNUM_GENERIC_PC;
2093 var_success = true;
2094 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002095 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002096 {
2097 reg_kind = eRegisterKindGeneric;
2098 reg_num = LLDB_REGNUM_GENERIC_SP;
2099 var_success = true;
2100 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002101 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002102 {
2103 reg_kind = eRegisterKindGeneric;
2104 reg_num = LLDB_REGNUM_GENERIC_FP;
2105 var_success = true;
2106 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002107 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002108 {
2109 reg_kind = eRegisterKindGeneric;
2110 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2111 var_success = true;
2112 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002113 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002114 {
2115 reg_ctx = frame->GetRegisterContext().get();
2116 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002117 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002118 var_name_begin += ::strlen ("reg.");
2119 if (var_name_begin < var_name_end)
2120 {
2121 std::string reg_name (var_name_begin, var_name_end);
2122 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2123 if (reg_info)
2124 var_success = true;
2125 }
Greg Clayton1b654882010-09-19 02:33:57 +00002126 }
2127 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002128 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002129 {
2130 var_name_begin += ::strlen("script:");
2131 std::string script_name(var_name_begin,var_name_end);
2132 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002133 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2134 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002135 }
Greg Clayton1b654882010-09-19 02:33:57 +00002136 }
2137 }
2138 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002139 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002140 {
2141 if (sc && (sc->function != NULL || sc->symbol != NULL))
2142 {
2143 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002144 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002145 {
2146 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002147 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002148 else
2149 s.Printf("symbol[%u]", sc->symbol->GetID());
2150
2151 var_success = true;
2152 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002153 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002154 {
2155 if (sc->function)
2156 cstr = sc->function->GetName().AsCString (NULL);
2157 else if (sc->symbol)
2158 cstr = sc->symbol->GetName().AsCString (NULL);
2159 if (cstr)
2160 {
2161 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002162
2163 if (sc->block)
2164 {
2165 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2166 if (inline_block)
2167 {
2168 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2169 if (inline_info)
2170 {
2171 s.PutCString(" [inlined] ");
2172 inline_info->GetName().Dump(&s);
2173 }
2174 }
2175 }
Greg Clayton1b654882010-09-19 02:33:57 +00002176 var_success = true;
2177 }
2178 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002179 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002180 {
2181 // Print the function name with arguments in it
2182
2183 if (sc->function)
2184 {
2185 var_success = true;
2186 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2187 cstr = sc->function->GetName().AsCString (NULL);
2188 if (cstr)
2189 {
2190 const InlineFunctionInfo *inline_info = NULL;
2191 VariableListSP variable_list_sp;
2192 bool get_function_vars = true;
2193 if (sc->block)
2194 {
2195 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2196
2197 if (inline_block)
2198 {
2199 get_function_vars = false;
2200 inline_info = sc->block->GetInlinedFunctionInfo();
2201 if (inline_info)
2202 variable_list_sp = inline_block->GetBlockVariableList (true);
2203 }
2204 }
2205
2206 if (get_function_vars)
2207 {
2208 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2209 }
2210
2211 if (inline_info)
2212 {
2213 s.PutCString (cstr);
2214 s.PutCString (" [inlined] ");
2215 cstr = inline_info->GetName().GetCString();
2216 }
2217
2218 VariableList args;
2219 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002220 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002221 if (args.GetSize() > 0)
2222 {
2223 const char *open_paren = strchr (cstr, '(');
2224 const char *close_paren = NULL;
2225 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002226 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002227 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002228 {
2229 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2230 if (open_paren)
2231 close_paren = strchr (open_paren, ')');
2232 }
2233 else
2234 close_paren = strchr (open_paren, ')');
2235 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002236
2237 if (open_paren)
2238 s.Write(cstr, open_paren - cstr + 1);
2239 else
2240 {
2241 s.PutCString (cstr);
2242 s.PutChar ('(');
2243 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002244 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002245 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2246 {
Enrico Granata894f7352014-03-25 22:03:52 +00002247 std::string buffer;
2248
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002249 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2250 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
Enrico Granata894f7352014-03-25 22:03:52 +00002251 const char *var_representation = nullptr;
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002252 const char *var_name = var_value_sp->GetName().GetCString();
Enrico Granata894f7352014-03-25 22:03:52 +00002253 if (var_value_sp->GetClangType().IsAggregateType() &&
2254 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2255 {
2256 static StringSummaryFormat format(TypeSummaryImpl::Flags()
2257 .SetHideItemNames(false)
2258 .SetShowMembersOneLiner(true),
2259 "");
2260 format.FormatObject(var_value_sp.get(), buffer);
2261 var_representation = buffer.c_str();
2262 }
2263 else
2264 var_representation = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002265 if (arg_idx > 0)
2266 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002267 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002268 {
Enrico Granata894f7352014-03-25 22:03:52 +00002269 if (var_representation)
2270 s.Printf ("%s=%s", var_name, var_representation);
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002271 else
2272 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2273 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002274 else
2275 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002276 }
2277
2278 if (close_paren)
2279 s.PutCString (close_paren);
2280 else
2281 s.PutChar(')');
2282
2283 }
2284 else
2285 {
2286 s.PutCString(cstr);
2287 }
2288 }
2289 }
2290 else if (sc->symbol)
2291 {
2292 cstr = sc->symbol->GetName().AsCString (NULL);
2293 if (cstr)
2294 {
2295 s.PutCString(cstr);
2296 var_success = true;
2297 }
2298 }
2299 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002300 else if (IsToken (var_name_begin, "addr-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002301 {
2302 var_success = addr != NULL;
2303 if (var_success)
2304 {
2305 format_addr = *addr;
2306 calculate_format_addr_function_offset = true;
2307 }
2308 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002309 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002310 {
2311 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2312 if (var_success)
2313 {
2314 format_addr = sc->line_entry.range.GetBaseAddress();
2315 calculate_format_addr_function_offset = true;
2316 }
2317 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002318 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002319 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002320 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002321 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002322 if (var_success)
2323 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002324 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002325 calculate_format_addr_function_offset = true;
2326 }
2327 }
2328 }
2329 }
2330 break;
2331
2332 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002333 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002334 {
2335 if (sc && sc->line_entry.IsValid())
2336 {
2337 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002338 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002339 {
2340 var_name_begin += ::strlen ("file.");
2341
Michael Sartain0769b2b2013-07-30 16:44:36 +00002342 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002343 {
2344 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002345 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002346 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002347 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002348 {
2349 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002350 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002351 }
2352 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002353 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002354 {
2355 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002356 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002357 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002358 else if ((IsToken (var_name_begin, "start-addr}")) ||
2359 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002360 {
2361 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2362 if (var_success)
2363 {
2364 format_addr = sc->line_entry.range.GetBaseAddress();
2365 if (var_name_begin[0] == 'e')
2366 format_addr.Slide (sc->line_entry.range.GetByteSize());
2367 }
2368 }
2369 }
2370 }
2371 break;
2372 }
2373
2374 if (var_success)
2375 {
2376 // If format addr is valid, then we need to print an address
2377 if (reg_num != LLDB_INVALID_REGNUM)
2378 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002379 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002380 // We have a register value to display...
2381 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2382 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002383 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002384 }
2385 else
2386 {
2387 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002388 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002389
2390 if (reg_ctx)
2391 {
2392 if (reg_kind != kNumRegisterKinds)
2393 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2394 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2395 var_success = reg_info != NULL;
2396 }
2397 }
2398 }
2399
2400 if (reg_info != NULL)
2401 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002402 RegisterValue reg_value;
2403 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2404 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002405 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002406 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002407 }
2408 }
2409
2410 if (format_file_spec)
2411 {
2412 s << format_file_spec;
2413 }
2414
2415 // If format addr is valid, then we need to print an address
2416 if (format_addr.IsValid())
2417 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002418 var_success = false;
2419
Greg Clayton1b654882010-09-19 02:33:57 +00002420 if (calculate_format_addr_function_offset)
2421 {
2422 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002423
Greg Clayton0603aa92010-10-04 01:05:56 +00002424 if (sc)
2425 {
2426 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002427 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002428 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002429 if (sc->block)
2430 {
2431 // Check to make sure we aren't in an inline
2432 // function. If we are, use the inline block
2433 // range that contains "format_addr" since
2434 // blocks can be discontiguous.
2435 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2436 AddressRange inline_range;
2437 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2438 func_addr = inline_range.GetBaseAddress();
2439 }
2440 }
Greg Claytone7612132012-03-07 21:03:09 +00002441 else if (sc->symbol && sc->symbol->ValueIsAddress())
2442 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002443 }
2444
2445 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002446 {
2447 if (func_addr.GetSection() == format_addr.GetSection())
2448 {
2449 addr_t func_file_addr = func_addr.GetFileAddress();
2450 addr_t addr_file_addr = format_addr.GetFileAddress();
2451 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002452 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002453 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002454 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002455 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002456 }
2457 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002458 {
2459 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2460 if (target)
2461 {
2462 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2463 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2464 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002465 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002466 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002467 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002468 var_success = true;
2469 }
2470 }
Greg Clayton1b654882010-09-19 02:33:57 +00002471 }
2472 }
2473 else
2474 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002475 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002476 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002477 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2478 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002479 if (vaddr == LLDB_INVALID_ADDRESS)
2480 vaddr = format_addr.GetFileAddress ();
2481
2482 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002483 {
Greg Clayton514487e2011-02-15 21:59:32 +00002484 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002485 if (addr_width == 0)
2486 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002487 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002488 var_success = true;
2489 }
Greg Clayton1b654882010-09-19 02:33:57 +00002490 }
2491 }
2492 }
2493
2494 if (var_success == false)
2495 success = false;
2496 }
2497 p = var_name_end;
2498 }
2499 else
2500 break;
2501 }
2502 else
2503 {
2504 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2505 s.PutChar(*p);
2506 }
2507 }
2508 else if (*p == '\\')
2509 {
2510 ++p; // skip the slash
2511 switch (*p)
2512 {
2513 case 'a': s.PutChar ('\a'); break;
2514 case 'b': s.PutChar ('\b'); break;
2515 case 'f': s.PutChar ('\f'); break;
2516 case 'n': s.PutChar ('\n'); break;
2517 case 'r': s.PutChar ('\r'); break;
2518 case 't': s.PutChar ('\t'); break;
2519 case 'v': s.PutChar ('\v'); break;
2520 case '\'': s.PutChar ('\''); break;
2521 case '\\': s.PutChar ('\\'); break;
2522 case '0':
2523 // 1 to 3 octal chars
2524 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002525 // Make a string that can hold onto the initial zero char,
2526 // up to 3 octal digits, and a terminating NULL.
2527 char oct_str[5] = { 0, 0, 0, 0, 0 };
2528
2529 int i;
2530 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2531 oct_str[i] = p[i];
2532
2533 // We don't want to consume the last octal character since
2534 // the main for loop will do this for us, so we advance p by
2535 // one less than i (even if i is zero)
2536 p += i - 1;
2537 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2538 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002539 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002540 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002541 }
Greg Clayton1b654882010-09-19 02:33:57 +00002542 }
2543 break;
2544
2545 case 'x':
2546 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002547 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002548 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002549 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002550
Greg Clayton0603aa92010-10-04 01:05:56 +00002551 // Make a string that can hold onto two hex chars plus a
2552 // NULL terminator
2553 char hex_str[3] = { 0,0,0 };
2554 hex_str[0] = *p;
2555 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002556 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002557 ++p; // Skip the first of the two hex chars
2558 hex_str[1] = *p;
2559 }
2560
2561 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2562 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002563 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002564 }
2565 else
2566 {
2567 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002568 }
2569 break;
2570
2571 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002572 // Just desensitize any other character by just printing what
2573 // came after the '\'
2574 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002575 break;
2576
2577 }
2578
2579 }
2580 }
2581 if (end)
2582 *end = p;
2583 return success;
2584}
2585
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002586bool
2587Debugger::FormatPrompt
2588(
2589 const char *format,
2590 const SymbolContext *sc,
2591 const ExecutionContext *exe_ctx,
2592 const Address *addr,
2593 Stream &s,
2594 ValueObject* valobj
2595)
2596{
2597 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2598 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2599 if (format_str.length())
2600 format = format_str.c_str();
2601 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2602}
2603
Jim Ingham228063c2012-02-21 02:23:08 +00002604void
2605Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2606{
Jim Ingham4f02b222012-02-22 22:49:20 +00002607 // For simplicity's sake, I am not going to deal with how to close down any
2608 // open logging streams, I just redirect everything from here on out to the
2609 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002610 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2611}
2612
2613bool
2614Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2615{
2616 Log::Callbacks log_callbacks;
2617
2618 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002619 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002620 {
2621 log_stream_sp = m_log_callback_stream_sp;
2622 // For now when using the callback mode you always get thread & timestamp.
2623 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2624 }
2625 else if (log_file == NULL || *log_file == '\0')
2626 {
Greg Clayton44d93782014-01-27 23:43:24 +00002627 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002628 }
2629 else
2630 {
2631 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002632 if (pos != m_log_streams.end())
2633 log_stream_sp = pos->second.lock();
2634 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002635 {
2636 log_stream_sp.reset (new StreamFile (log_file));
2637 m_log_streams[log_file] = log_stream_sp;
2638 }
Jim Ingham228063c2012-02-21 02:23:08 +00002639 }
2640 assert (log_stream_sp.get());
2641
2642 if (log_options == 0)
2643 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2644
Greg Clayton57abc5d2013-05-10 21:47:16 +00002645 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002646 {
2647 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2648 return true;
2649 }
2650 else
2651 {
2652 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2653 if (log_channel_sp)
2654 {
2655 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2656 {
2657 return true;
2658 }
2659 else
2660 {
2661 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2662 return false;
2663 }
2664 }
2665 else
2666 {
2667 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2668 return false;
2669 }
2670 }
2671 return false;
2672}
2673
Greg Clayton9585fbf2013-03-19 00:20:55 +00002674SourceManager &
2675Debugger::GetSourceManager ()
2676{
2677 if (m_source_manager_ap.get() == NULL)
2678 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2679 return *m_source_manager_ap;
2680}
2681
2682
Greg Clayton44d93782014-01-27 23:43:24 +00002683
2684// This function handles events that were broadcast by the process.
2685void
2686Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2687{
2688 using namespace lldb;
2689 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2690
2691// if (event_type & eBreakpointEventTypeAdded
2692// || event_type & eBreakpointEventTypeRemoved
2693// || event_type & eBreakpointEventTypeEnabled
2694// || event_type & eBreakpointEventTypeDisabled
2695// || event_type & eBreakpointEventTypeCommandChanged
2696// || event_type & eBreakpointEventTypeConditionChanged
2697// || event_type & eBreakpointEventTypeIgnoreChanged
2698// || event_type & eBreakpointEventTypeLocationsResolved)
2699// {
2700// // Don't do anything about these events, since the breakpoint commands already echo these actions.
2701// }
2702//
2703 if (event_type & eBreakpointEventTypeLocationsAdded)
2704 {
2705 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
2706 if (num_new_locations > 0)
2707 {
2708 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
2709 StreamFileSP output_sp (GetOutputFile());
2710 if (output_sp)
2711 {
2712 output_sp->Printf("%d location%s added to breakpoint %d\n",
2713 num_new_locations,
2714 num_new_locations == 1 ? "" : "s",
2715 breakpoint->GetID());
2716 RefreshTopIOHandler();
2717 }
2718 }
2719 }
2720// else if (event_type & eBreakpointEventTypeLocationsRemoved)
2721// {
2722// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
2723// }
2724// else if (event_type & eBreakpointEventTypeLocationsResolved)
2725// {
2726// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
2727// }
2728}
2729
2730size_t
2731Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
2732{
2733 size_t total_bytes = 0;
2734 if (stream == NULL)
2735 stream = GetOutputFile().get();
2736
2737 if (stream)
2738 {
2739 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
2740 if (process == NULL)
2741 {
2742 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2743 if (target_sp)
2744 process = target_sp->GetProcessSP().get();
2745 }
2746 if (process)
2747 {
2748 Error error;
2749 size_t len;
2750 char stdio_buffer[1024];
2751 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2752 {
2753 stream->Write(stdio_buffer, len);
2754 total_bytes += len;
2755 }
2756 }
2757 stream->Flush();
2758 }
2759 return total_bytes;
2760}
2761
2762size_t
2763Debugger::GetProcessSTDERR (Process *process, Stream *stream)
2764{
2765 size_t total_bytes = 0;
2766 if (stream == NULL)
2767 stream = GetOutputFile().get();
2768
2769 if (stream)
2770 {
2771 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2772 if (process == NULL)
2773 {
2774 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2775 if (target_sp)
2776 process = target_sp->GetProcessSP().get();
2777 }
2778 if (process)
2779 {
2780 Error error;
2781 size_t len;
2782 char stdio_buffer[1024];
2783 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2784 {
2785 stream->Write(stdio_buffer, len);
2786 total_bytes += len;
2787 }
2788 }
2789 stream->Flush();
2790 }
2791 return total_bytes;
2792}
2793
2794// This function handles events that were broadcast by the process.
2795void
2796Debugger::HandleProcessEvent (const EventSP &event_sp)
2797{
2798 using namespace lldb;
2799 const uint32_t event_type = event_sp->GetType();
2800 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
2801
Greg Claytonb4874f12014-02-28 18:22:24 +00002802 StreamString output_stream;
2803 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00002804 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00002805
Greg Claytonb4874f12014-02-28 18:22:24 +00002806 if (!gui_enabled)
2807 {
2808 bool pop_process_io_handler = false;
2809 assert (process_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00002810
Greg Claytonb4874f12014-02-28 18:22:24 +00002811 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00002812 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002813 GetProcessSTDOUT (process_sp.get(), &output_stream);
2814 }
2815
2816 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
2817 {
2818 GetProcessSTDERR (process_sp.get(), &error_stream);
2819 }
2820
2821 if (event_type & Process::eBroadcastBitStateChanged)
2822 {
2823
2824 // Drain all stout and stderr so we don't see any output come after
2825 // we print our prompts
Greg Clayton44d93782014-01-27 23:43:24 +00002826 // Something changed in the process; get the event and report the process's current status and location to
2827 // the user.
2828 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
2829 if (event_state == eStateInvalid)
2830 return;
2831
2832 switch (event_state)
2833 {
2834 case eStateInvalid:
2835 case eStateUnloaded:
2836 case eStateConnected:
2837 case eStateAttaching:
2838 case eStateLaunching:
2839 case eStateStepping:
2840 case eStateDetached:
2841 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002842 output_stream.Printf("Process %" PRIu64 " %s\n",
2843 process_sp->GetID(),
2844 StateAsCString (event_state));
2845
2846 if (event_state == eStateDetached)
2847 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002848 }
2849 break;
2850
2851 case eStateRunning:
2852 // Don't be chatty when we run...
2853 break;
2854
2855 case eStateExited:
Greg Claytonb4874f12014-02-28 18:22:24 +00002856 process_sp->GetStatus(output_stream);
2857 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002858 break;
2859
2860 case eStateStopped:
2861 case eStateCrashed:
2862 case eStateSuspended:
2863 // Make sure the program hasn't been auto-restarted:
2864 if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
2865 {
2866 size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
2867 if (num_reasons > 0)
2868 {
2869 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
2870 if (num_reasons == 1)
2871 {
2872 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
Greg Claytonb4874f12014-02-28 18:22:24 +00002873 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n",
2874 process_sp->GetID(),
2875 reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00002876 }
2877 else
2878 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002879 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
2880 process_sp->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00002881
2882
2883 for (size_t i = 0; i < num_reasons; i++)
2884 {
2885 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
Greg Claytonb4874f12014-02-28 18:22:24 +00002886 output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00002887 }
2888 }
2889 }
2890 }
2891 else
2892 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002893 // Lock the thread list so it doesn't change on us, this is the scope for the locker:
Greg Clayton44d93782014-01-27 23:43:24 +00002894 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002895 ThreadList &thread_list = process_sp->GetThreadList();
2896 Mutex::Locker locker (thread_list.GetMutex());
2897
2898 ThreadSP curr_thread (thread_list.GetSelectedThread());
2899 ThreadSP thread;
2900 StopReason curr_thread_stop_reason = eStopReasonInvalid;
2901 if (curr_thread)
2902 curr_thread_stop_reason = curr_thread->GetStopReason();
2903 if (!curr_thread ||
2904 !curr_thread->IsValid() ||
2905 curr_thread_stop_reason == eStopReasonInvalid ||
2906 curr_thread_stop_reason == eStopReasonNone)
Greg Clayton44d93782014-01-27 23:43:24 +00002907 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002908 // Prefer a thread that has just completed its plan over another thread as current thread.
2909 ThreadSP plan_thread;
2910 ThreadSP other_thread;
2911 const size_t num_threads = thread_list.GetSize();
2912 size_t i;
2913 for (i = 0; i < num_threads; ++i)
Greg Clayton44d93782014-01-27 23:43:24 +00002914 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002915 thread = thread_list.GetThreadAtIndex(i);
2916 StopReason thread_stop_reason = thread->GetStopReason();
2917 switch (thread_stop_reason)
2918 {
2919 case eStopReasonInvalid:
2920 case eStopReasonNone:
2921 break;
2922
2923 case eStopReasonTrace:
2924 case eStopReasonBreakpoint:
2925 case eStopReasonWatchpoint:
2926 case eStopReasonSignal:
2927 case eStopReasonException:
2928 case eStopReasonExec:
2929 case eStopReasonThreadExiting:
2930 if (!other_thread)
2931 other_thread = thread;
2932 break;
2933 case eStopReasonPlanComplete:
2934 if (!plan_thread)
2935 plan_thread = thread;
2936 break;
2937 }
2938 }
2939 if (plan_thread)
2940 thread_list.SetSelectedThreadByID (plan_thread->GetID());
2941 else if (other_thread)
2942 thread_list.SetSelectedThreadByID (other_thread->GetID());
2943 else
2944 {
2945 if (curr_thread && curr_thread->IsValid())
2946 thread = curr_thread;
2947 else
2948 thread = thread_list.GetThreadAtIndex(0);
2949
2950 if (thread)
2951 thread_list.SetSelectedThreadByID (thread->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00002952 }
2953 }
Greg Clayton44d93782014-01-27 23:43:24 +00002954 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00002955 // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code,
2956 // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to
2957 // have a hard time restarting the process.
Greg Clayton44d93782014-01-27 23:43:24 +00002958
2959 if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget())
2960 {
2961 const bool only_threads_with_stop_reason = true;
2962 const uint32_t start_frame = 0;
2963 const uint32_t num_frames = 1;
2964 const uint32_t num_frames_with_source = 1;
Greg Claytonb4874f12014-02-28 18:22:24 +00002965 process_sp->GetStatus(output_stream);
2966 process_sp->GetThreadStatus (output_stream,
Greg Clayton44d93782014-01-27 23:43:24 +00002967 only_threads_with_stop_reason,
2968 start_frame,
2969 num_frames,
2970 num_frames_with_source);
2971 }
2972 else
2973 {
2974 uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this());
2975 if (target_idx != UINT32_MAX)
Greg Claytonb4874f12014-02-28 18:22:24 +00002976 output_stream.Printf ("Target %d: (", target_idx);
Greg Clayton44d93782014-01-27 23:43:24 +00002977 else
Greg Claytonb4874f12014-02-28 18:22:24 +00002978 output_stream.Printf ("Target <unknown index>: (");
2979 process_sp->GetTarget().Dump (&output_stream, eDescriptionLevelBrief);
2980 output_stream.Printf (") stopped.\n");
Greg Clayton44d93782014-01-27 23:43:24 +00002981 }
Greg Claytonb4874f12014-02-28 18:22:24 +00002982
2983 // Pop the process IO handler
2984 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002985 }
2986 break;
2987 }
2988 }
Greg Clayton44d93782014-01-27 23:43:24 +00002989
Greg Claytonb4874f12014-02-28 18:22:24 +00002990 if (output_stream.GetSize() || error_stream.GetSize())
2991 {
2992 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00002993 bool top_io_handler_hid = false;
2994
2995 if (process_sp->ProcessIOHandlerIsActive() == false)
2996 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00002997
2998 if (output_stream.GetSize())
2999 {
3000 StreamFileSP output_stream_sp (GetOutputFile());
3001 if (output_stream_sp)
3002 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3003 }
3004
3005 if (error_stream.GetSize())
3006 {
3007 StreamFileSP error_stream_sp (GetErrorFile());
3008 if (error_stream_sp)
3009 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3010 }
3011
3012 if (top_io_handler_hid)
3013 RefreshTopIOHandler();
3014 }
3015
3016 if (pop_process_io_handler)
3017 process_sp->PopProcessIOHandler();
3018 }
Greg Clayton44d93782014-01-27 23:43:24 +00003019}
3020
3021void
3022Debugger::HandleThreadEvent (const EventSP &event_sp)
3023{
3024 // At present the only thread event we handle is the Frame Changed event,
3025 // and all we do for that is just reprint the thread status for that thread.
3026 using namespace lldb;
3027 const uint32_t event_type = event_sp->GetType();
3028 if (event_type == Thread::eBroadcastBitStackChanged ||
3029 event_type == Thread::eBroadcastBitThreadSelected )
3030 {
3031 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3032 if (thread_sp)
3033 {
3034 HideTopIOHandler();
3035 StreamFileSP stream_sp (GetOutputFile());
3036 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3037 RefreshTopIOHandler();
3038 }
3039 }
3040}
3041
3042bool
3043Debugger::IsForwardingEvents ()
3044{
3045 return (bool)m_forward_listener_sp;
3046}
3047
3048void
3049Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3050{
3051 m_forward_listener_sp = listener_sp;
3052}
3053
3054void
3055Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3056{
3057 m_forward_listener_sp.reset();
3058}
3059
3060
3061void
3062Debugger::DefaultEventHandler()
3063{
3064 Listener& listener(GetListener());
3065 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3066 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3067 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3068 BroadcastEventSpec target_event_spec (broadcaster_class_target,
3069 Target::eBroadcastBitBreakpointChanged);
3070
3071 BroadcastEventSpec process_event_spec (broadcaster_class_process,
3072 Process::eBroadcastBitStateChanged |
3073 Process::eBroadcastBitSTDOUT |
3074 Process::eBroadcastBitSTDERR);
3075
3076 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3077 Thread::eBroadcastBitStackChanged |
3078 Thread::eBroadcastBitThreadSelected );
3079
3080 listener.StartListeningForEventSpec (*this, target_event_spec);
3081 listener.StartListeningForEventSpec (*this, process_event_spec);
3082 listener.StartListeningForEventSpec (*this, thread_event_spec);
3083 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3084 CommandInterpreter::eBroadcastBitQuitCommandReceived |
3085 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
3086 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
3087
3088 bool done = false;
3089 while (!done)
3090 {
3091// Mutex::Locker locker;
3092// if (locker.TryLock(m_input_reader_stack.GetMutex()))
3093// {
3094// if (m_input_reader_stack.IsEmpty())
3095// break;
3096// }
3097//
3098 EventSP event_sp;
3099 if (listener.WaitForEvent(NULL, event_sp))
3100 {
3101 if (event_sp)
3102 {
3103 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3104 if (broadcaster)
3105 {
3106 uint32_t event_type = event_sp->GetType();
3107 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3108 if (broadcaster_class == broadcaster_class_process)
3109 {
3110 HandleProcessEvent (event_sp);
3111 }
3112 else if (broadcaster_class == broadcaster_class_target)
3113 {
3114 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3115 {
3116 HandleBreakpointEvent (event_sp);
3117 }
3118 }
3119 else if (broadcaster_class == broadcaster_class_thread)
3120 {
3121 HandleThreadEvent (event_sp);
3122 }
3123 else if (broadcaster == m_command_interpreter_ap.get())
3124 {
3125 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3126 {
3127 done = true;
3128 }
3129 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3130 {
3131 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3132 if (data && data[0])
3133 {
3134 StreamFileSP error_sp (GetErrorFile());
3135 if (error_sp)
3136 {
3137 HideTopIOHandler();
3138 error_sp->PutCString(data);
3139 error_sp->Flush();
3140 RefreshTopIOHandler();
3141 }
3142 }
3143 }
3144 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3145 {
3146 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3147 if (data && data[0])
3148 {
3149 StreamFileSP output_sp (GetOutputFile());
3150 if (output_sp)
3151 {
3152 HideTopIOHandler();
3153 output_sp->PutCString(data);
3154 output_sp->Flush();
3155 RefreshTopIOHandler();
3156 }
3157 }
3158 }
3159 }
3160 }
3161
3162 if (m_forward_listener_sp)
3163 m_forward_listener_sp->AddEvent(event_sp);
3164 }
3165 }
3166 }
3167}
3168
3169lldb::thread_result_t
3170Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3171{
3172 ((Debugger *)arg)->DefaultEventHandler();
3173 return NULL;
3174}
3175
3176bool
3177Debugger::StartEventHandlerThread()
3178{
3179 if (!IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3180 m_event_handler_thread = Host::ThreadCreate("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
3181 return IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread);
3182}
3183
3184void
3185Debugger::StopEventHandlerThread()
3186{
3187 if (IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3188 {
3189 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
3190 Host::ThreadJoin(m_event_handler_thread, NULL, NULL);
3191 m_event_handler_thread = LLDB_INVALID_HOST_THREAD;
3192 }
3193}
3194
3195
3196lldb::thread_result_t
3197Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3198{
3199 Debugger *debugger = (Debugger *)arg;
3200 debugger->ExecuteIOHanders();
3201 debugger->StopEventHandlerThread();
3202 return NULL;
3203}
3204
3205bool
3206Debugger::StartIOHandlerThread()
3207{
3208 if (!IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3209 m_io_handler_thread = Host::ThreadCreate("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
3210 return IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread);
3211}
3212
3213void
3214Debugger::StopIOHandlerThread()
3215{
3216 if (IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3217 {
3218 if (m_input_file_sp)
3219 m_input_file_sp->GetFile().Close();
3220 Host::ThreadJoin(m_io_handler_thread, NULL, NULL);
3221 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
3222 }
3223}
3224
3225