1 //===-- 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 
10 #include "lldb/lldb-python.h"
11 
12 #include "lldb/Core/Debugger.h"
13 
14 #include <map>
15 
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/Type.h"
18 #include "llvm/ADT/StringRef.h"
19 
20 #include "lldb/lldb-private.h"
21 #include "lldb/Core/ConnectionFileDescriptor.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/PluginManager.h"
24 #include "lldb/Core/RegisterValue.h"
25 #include "lldb/Core/State.h"
26 #include "lldb/Core/StreamAsynchronousIO.h"
27 #include "lldb/Core/StreamCallback.h"
28 #include "lldb/Core/StreamFile.h"
29 #include "lldb/Core/StreamString.h"
30 #include "lldb/Core/StructuredData.h"
31 #include "lldb/Core/Timer.h"
32 #include "lldb/Core/ValueObject.h"
33 #include "lldb/Core/ValueObjectVariable.h"
34 #include "lldb/DataFormatters/DataVisualization.h"
35 #include "lldb/DataFormatters/FormatManager.h"
36 #include "lldb/DataFormatters/TypeSummary.h"
37 #include "lldb/Host/DynamicLibrary.h"
38 #include "lldb/Host/HostInfo.h"
39 #include "lldb/Host/Terminal.h"
40 #include "lldb/Interpreter/CommandInterpreter.h"
41 #include "lldb/Interpreter/OptionValueSInt64.h"
42 #include "lldb/Interpreter/OptionValueString.h"
43 #include "lldb/Symbol/ClangASTContext.h"
44 #include "lldb/Symbol/CompileUnit.h"
45 #include "lldb/Symbol/Function.h"
46 #include "lldb/Symbol/Symbol.h"
47 #include "lldb/Symbol/VariableList.h"
48 #include "lldb/Target/TargetList.h"
49 #include "lldb/Target/Process.h"
50 #include "lldb/Target/RegisterContext.h"
51 #include "lldb/Target/SectionLoadList.h"
52 #include "lldb/Target/StopInfo.h"
53 #include "lldb/Target/Target.h"
54 #include "lldb/Target/Thread.h"
55 #include "lldb/Utility/AnsiTerminal.h"
56 
57 using namespace lldb;
58 using namespace lldb_private;
59 
60 
61 static uint32_t g_shared_debugger_refcount = 0;
62 static lldb::user_id_t g_unique_id = 1;
63 
64 #pragma mark Static Functions
65 
66 static Mutex &
67 GetDebuggerListMutex ()
68 {
69     static Mutex g_mutex(Mutex::eMutexTypeRecursive);
70     return g_mutex;
71 }
72 
73 typedef std::vector<DebuggerSP> DebuggerList;
74 
75 static DebuggerList &
76 GetDebuggerList()
77 {
78     // hide the static debugger list inside a singleton accessor to avoid
79     // global init constructors
80     static DebuggerList g_list;
81     return g_list;
82 }
83 
84 OptionEnumValueElement
85 g_show_disassembly_enum_values[] =
86 {
87     { Debugger::eStopDisassemblyTypeNever,    "never",     "Never show disassembly when displaying a stop context."},
88     { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
89     { Debugger::eStopDisassemblyTypeAlways,   "always",    "Always show disassembly when displaying a stop context."},
90     { 0, NULL, NULL }
91 };
92 
93 OptionEnumValueElement
94 g_language_enumerators[] =
95 {
96     { eScriptLanguageNone,      "none",     "Disable scripting languages."},
97     { eScriptLanguagePython,    "python",   "Select python as the default scripting language."},
98     { eScriptLanguageDefault,   "default",  "Select the lldb default as the default scripting language."},
99     { 0, NULL, NULL }
100 };
101 
102 #define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
103 #define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
104 
105 #define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
106     "{, ${frame.pc}}"\
107     MODULE_WITH_FUNC\
108     FILE_AND_LINE\
109     "{, name = '${thread.name}'}"\
110     "{, queue = '${thread.queue}'}"\
111     "{, activity = '${thread.info.activity.name}'}" \
112     "{, ${thread.info.trace_messages} messages}" \
113     "{, stop reason = ${thread.stop-reason}}"\
114     "{\\nReturn value: ${thread.return-value}}"\
115     "{\\nCompleted expression: ${thread.completed-expression}}"\
116     "\\n"
117 
118 #define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
119     MODULE_WITH_FUNC\
120     FILE_AND_LINE\
121     "\\n"
122 
123 
124 
125 static PropertyDefinition
126 g_properties[] =
127 {
128 {   "auto-confirm",             OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
129 {   "frame-format",             OptionValue::eTypeString , true, 0    , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
130 {   "notify-void",              OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
131 {   "prompt",                   OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
132 {   "script-lang",              OptionValue::eTypeEnum   , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
133 {   "stop-disassembly-count",   OptionValue::eTypeSInt64 , true, 4    , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
134 {   "stop-disassembly-display", OptionValue::eTypeEnum   , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
135 {   "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." },
136 {   "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." },
137 {   "term-width",               OptionValue::eTypeSInt64 , true, 80   , NULL, NULL, "The maximum number of columns to use for displaying text." },
138 {   "thread-format",            OptionValue::eTypeString , true, 0    , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
139 {   "use-external-editor",      OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
140 {   "use-color",                OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
141 {   "auto-one-line-summaries",     OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
142 
143     {   NULL,                       OptionValue::eTypeInvalid, true, 0    , NULL, NULL, NULL }
144 };
145 
146 enum
147 {
148     ePropertyAutoConfirm = 0,
149     ePropertyFrameFormat,
150     ePropertyNotiftVoid,
151     ePropertyPrompt,
152     ePropertyScriptLanguage,
153     ePropertyStopDisassemblyCount,
154     ePropertyStopDisassemblyDisplay,
155     ePropertyStopLineCountAfter,
156     ePropertyStopLineCountBefore,
157     ePropertyTerminalWidth,
158     ePropertyThreadFormat,
159     ePropertyUseExternalEditor,
160     ePropertyUseColor,
161     ePropertyAutoOneLineSummaries
162 };
163 
164 Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
165 
166 Error
167 Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
168                             VarSetOperationType op,
169                             const char *property_path,
170                             const char *value)
171 {
172     bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
173     TargetSP target_sp;
174     LoadScriptFromSymFile load_script_old_value;
175     if (is_load_script && exe_ctx->GetTargetSP())
176     {
177         target_sp = exe_ctx->GetTargetSP();
178         load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
179     }
180     Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
181     if (error.Success())
182     {
183         // FIXME it would be nice to have "on-change" callbacks for properties
184         if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
185         {
186             const char *new_prompt = GetPrompt();
187             std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
188             if (str.length())
189                 new_prompt = str.c_str();
190             GetCommandInterpreter().UpdatePrompt(new_prompt);
191             EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
192             GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
193         }
194         else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
195         {
196 			// use-color changed. Ping the prompt so it can reset the ansi terminal codes.
197             SetPrompt (GetPrompt());
198         }
199         else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
200         {
201             if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
202             {
203                 std::list<Error> errors;
204                 StreamString feedback_stream;
205                 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
206                 {
207                     StreamFileSP stream_sp (GetErrorFile());
208                     if (stream_sp)
209                     {
210                         for (auto error : errors)
211                         {
212                             stream_sp->Printf("%s\n",error.AsCString());
213                         }
214                         if (feedback_stream.GetSize())
215                             stream_sp->Printf("%s",feedback_stream.GetData());
216                     }
217                 }
218             }
219         }
220     }
221     return error;
222 }
223 
224 bool
225 Debugger::GetAutoConfirm () const
226 {
227     const uint32_t idx = ePropertyAutoConfirm;
228     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
229 }
230 
231 const char *
232 Debugger::GetFrameFormat() const
233 {
234     const uint32_t idx = ePropertyFrameFormat;
235     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
236 }
237 
238 bool
239 Debugger::GetNotifyVoid () const
240 {
241     const uint32_t idx = ePropertyNotiftVoid;
242     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
243 }
244 
245 const char *
246 Debugger::GetPrompt() const
247 {
248     const uint32_t idx = ePropertyPrompt;
249     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
250 }
251 
252 void
253 Debugger::SetPrompt(const char *p)
254 {
255     const uint32_t idx = ePropertyPrompt;
256     m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
257     const char *new_prompt = GetPrompt();
258     std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
259     if (str.length())
260         new_prompt = str.c_str();
261     GetCommandInterpreter().UpdatePrompt(new_prompt);
262 }
263 
264 const char *
265 Debugger::GetThreadFormat() const
266 {
267     const uint32_t idx = ePropertyThreadFormat;
268     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
269 }
270 
271 lldb::ScriptLanguage
272 Debugger::GetScriptLanguage() const
273 {
274     const uint32_t idx = ePropertyScriptLanguage;
275     return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
276 }
277 
278 bool
279 Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
280 {
281     const uint32_t idx = ePropertyScriptLanguage;
282     return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
283 }
284 
285 uint32_t
286 Debugger::GetTerminalWidth () const
287 {
288     const uint32_t idx = ePropertyTerminalWidth;
289     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
290 }
291 
292 bool
293 Debugger::SetTerminalWidth (uint32_t term_width)
294 {
295     const uint32_t idx = ePropertyTerminalWidth;
296     return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
297 }
298 
299 bool
300 Debugger::GetUseExternalEditor () const
301 {
302     const uint32_t idx = ePropertyUseExternalEditor;
303     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
304 }
305 
306 bool
307 Debugger::SetUseExternalEditor (bool b)
308 {
309     const uint32_t idx = ePropertyUseExternalEditor;
310     return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
311 }
312 
313 bool
314 Debugger::GetUseColor () const
315 {
316     const uint32_t idx = ePropertyUseColor;
317     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
318 }
319 
320 bool
321 Debugger::SetUseColor (bool b)
322 {
323     const uint32_t idx = ePropertyUseColor;
324     bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
325     SetPrompt (GetPrompt());
326     return ret;
327 }
328 
329 uint32_t
330 Debugger::GetStopSourceLineCount (bool before) const
331 {
332     const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
333     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
334 }
335 
336 Debugger::StopDisassemblyType
337 Debugger::GetStopDisassemblyDisplay () const
338 {
339     const uint32_t idx = ePropertyStopDisassemblyDisplay;
340     return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
341 }
342 
343 uint32_t
344 Debugger::GetDisassemblyLineCount () const
345 {
346     const uint32_t idx = ePropertyStopDisassemblyCount;
347     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
348 }
349 
350 bool
351 Debugger::GetAutoOneLineSummaries () const
352 {
353     const uint32_t idx = ePropertyAutoOneLineSummaries;
354     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
355 
356 }
357 
358 #pragma mark Debugger
359 
360 //const DebuggerPropertiesSP &
361 //Debugger::GetSettings() const
362 //{
363 //    return m_properties_sp;
364 //}
365 //
366 
367 int
368 Debugger::TestDebuggerRefCount ()
369 {
370     return g_shared_debugger_refcount;
371 }
372 
373 void
374 Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
375 {
376     g_load_plugin_callback = load_plugin_callback;
377     if (g_shared_debugger_refcount++ == 0)
378         lldb_private::Initialize();
379 }
380 
381 void
382 Debugger::Terminate ()
383 {
384     if (g_shared_debugger_refcount > 0)
385     {
386         g_shared_debugger_refcount--;
387         if (g_shared_debugger_refcount == 0)
388         {
389             lldb_private::WillTerminate();
390             lldb_private::Terminate();
391 
392             // Clear our master list of debugger objects
393             Mutex::Locker locker (GetDebuggerListMutex ());
394             GetDebuggerList().clear();
395         }
396     }
397 }
398 
399 void
400 Debugger::SettingsInitialize ()
401 {
402     Target::SettingsInitialize ();
403 }
404 
405 void
406 Debugger::SettingsTerminate ()
407 {
408     Target::SettingsTerminate ();
409 }
410 
411 bool
412 Debugger::LoadPlugin (const FileSpec& spec, Error& error)
413 {
414     if (g_load_plugin_callback)
415     {
416         lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error);
417         if (dynlib_sp)
418         {
419             m_loaded_plugins.push_back(dynlib_sp);
420             return true;
421         }
422     }
423     else
424     {
425         // The g_load_plugin_callback is registered in SBDebugger::Initialize()
426         // and if the public API layer isn't available (code is linking against
427         // all of the internal LLDB static libraries), then we can't load plugins
428         error.SetErrorString("Public API layer is not available");
429     }
430     return false;
431 }
432 
433 static FileSpec::EnumerateDirectoryResult
434 LoadPluginCallback
435 (
436  void *baton,
437  FileSpec::FileType file_type,
438  const FileSpec &file_spec
439  )
440 {
441     Error error;
442 
443     static ConstString g_dylibext("dylib");
444     static ConstString g_solibext("so");
445 
446     if (!baton)
447         return FileSpec::eEnumerateDirectoryResultQuit;
448 
449     Debugger *debugger = (Debugger*)baton;
450 
451     // If we have a regular file, a symbolic link or unknown file type, try
452     // and process the file. We must handle unknown as sometimes the directory
453     // enumeration might be enumerating a file system that doesn't have correct
454     // file type information.
455     if (file_type == FileSpec::eFileTypeRegular         ||
456         file_type == FileSpec::eFileTypeSymbolicLink    ||
457         file_type == FileSpec::eFileTypeUnknown          )
458     {
459         FileSpec plugin_file_spec (file_spec);
460         plugin_file_spec.ResolvePath ();
461 
462         if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
463             plugin_file_spec.GetFileNameExtension() != g_solibext)
464         {
465             return FileSpec::eEnumerateDirectoryResultNext;
466         }
467 
468         Error plugin_load_error;
469         debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
470 
471         return FileSpec::eEnumerateDirectoryResultNext;
472     }
473 
474     else if (file_type == FileSpec::eFileTypeUnknown     ||
475         file_type == FileSpec::eFileTypeDirectory   ||
476         file_type == FileSpec::eFileTypeSymbolicLink )
477     {
478         // Try and recurse into anything that a directory or symbolic link.
479         // We must also do this for unknown as sometimes the directory enumeration
480         // might be enumerating a file system that doesn't have correct file type
481         // information.
482         return FileSpec::eEnumerateDirectoryResultEnter;
483     }
484 
485     return FileSpec::eEnumerateDirectoryResultNext;
486 }
487 
488 void
489 Debugger::InstanceInitialize ()
490 {
491     FileSpec dir_spec;
492     const bool find_directories = true;
493     const bool find_files = true;
494     const bool find_other = true;
495     char dir_path[PATH_MAX];
496     if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
497     {
498         if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
499         {
500             FileSpec::EnumerateDirectory (dir_path,
501                                           find_directories,
502                                           find_files,
503                                           find_other,
504                                           LoadPluginCallback,
505                                           this);
506         }
507     }
508 
509     if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
510     {
511         if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
512         {
513             FileSpec::EnumerateDirectory (dir_path,
514                                           find_directories,
515                                           find_files,
516                                           find_other,
517                                           LoadPluginCallback,
518                                           this);
519         }
520     }
521 
522     PluginManager::DebuggerInitialize (*this);
523 }
524 
525 DebuggerSP
526 Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
527 {
528     DebuggerSP debugger_sp (new Debugger(log_callback, baton));
529     if (g_shared_debugger_refcount > 0)
530     {
531         Mutex::Locker locker (GetDebuggerListMutex ());
532         GetDebuggerList().push_back(debugger_sp);
533     }
534     debugger_sp->InstanceInitialize ();
535     return debugger_sp;
536 }
537 
538 void
539 Debugger::Destroy (DebuggerSP &debugger_sp)
540 {
541     if (debugger_sp.get() == NULL)
542         return;
543 
544     debugger_sp->Clear();
545 
546     if (g_shared_debugger_refcount > 0)
547     {
548         Mutex::Locker locker (GetDebuggerListMutex ());
549         DebuggerList &debugger_list = GetDebuggerList ();
550         DebuggerList::iterator pos, end = debugger_list.end();
551         for (pos = debugger_list.begin (); pos != end; ++pos)
552         {
553             if ((*pos).get() == debugger_sp.get())
554             {
555                 debugger_list.erase (pos);
556                 return;
557             }
558         }
559     }
560 }
561 
562 DebuggerSP
563 Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
564 {
565     DebuggerSP debugger_sp;
566     if (g_shared_debugger_refcount > 0)
567     {
568         Mutex::Locker locker (GetDebuggerListMutex ());
569         DebuggerList &debugger_list = GetDebuggerList();
570         DebuggerList::iterator pos, end = debugger_list.end();
571 
572         for (pos = debugger_list.begin(); pos != end; ++pos)
573         {
574             if ((*pos).get()->m_instance_name == instance_name)
575             {
576                 debugger_sp = *pos;
577                 break;
578             }
579         }
580     }
581     return debugger_sp;
582 }
583 
584 TargetSP
585 Debugger::FindTargetWithProcessID (lldb::pid_t pid)
586 {
587     TargetSP target_sp;
588     if (g_shared_debugger_refcount > 0)
589     {
590         Mutex::Locker locker (GetDebuggerListMutex ());
591         DebuggerList &debugger_list = GetDebuggerList();
592         DebuggerList::iterator pos, end = debugger_list.end();
593         for (pos = debugger_list.begin(); pos != end; ++pos)
594         {
595             target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
596             if (target_sp)
597                 break;
598         }
599     }
600     return target_sp;
601 }
602 
603 TargetSP
604 Debugger::FindTargetWithProcess (Process *process)
605 {
606     TargetSP target_sp;
607     if (g_shared_debugger_refcount > 0)
608     {
609         Mutex::Locker locker (GetDebuggerListMutex ());
610         DebuggerList &debugger_list = GetDebuggerList();
611         DebuggerList::iterator pos, end = debugger_list.end();
612         for (pos = debugger_list.begin(); pos != end; ++pos)
613         {
614             target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
615             if (target_sp)
616                 break;
617         }
618     }
619     return target_sp;
620 }
621 
622 Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
623     UserID (g_unique_id++),
624     Properties(OptionValuePropertiesSP(new OptionValueProperties())),
625     m_input_file_sp (new StreamFile (stdin, false)),
626     m_output_file_sp (new StreamFile (stdout, false)),
627     m_error_file_sp (new StreamFile (stderr, false)),
628     m_terminal_state (),
629     m_target_list (*this),
630     m_platform_list (),
631     m_listener ("lldb.Debugger"),
632     m_source_manager_ap(),
633     m_source_file_cache(),
634     m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
635     m_input_reader_stack (),
636     m_instance_name (),
637     m_loaded_plugins (),
638     m_event_handler_thread (LLDB_INVALID_HOST_THREAD),
639     m_io_handler_thread (LLDB_INVALID_HOST_THREAD)
640 {
641     char instance_cstr[256];
642     snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
643     m_instance_name.SetCString(instance_cstr);
644     if (log_callback)
645         m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
646     m_command_interpreter_ap->Initialize ();
647     // Always add our default platform to the platform list
648     PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
649     assert (default_platform_sp.get());
650     m_platform_list.Append (default_platform_sp, true);
651 
652     m_collection_sp->Initialize (g_properties);
653     m_collection_sp->AppendProperty (ConstString("target"),
654                                      ConstString("Settings specify to debugging targets."),
655                                      true,
656                                      Target::GetGlobalProperties()->GetValueProperties());
657     if (m_command_interpreter_ap.get())
658     {
659         m_collection_sp->AppendProperty (ConstString("interpreter"),
660                                          ConstString("Settings specify to the debugger's command interpreter."),
661                                          true,
662                                          m_command_interpreter_ap->GetValueProperties());
663     }
664     OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
665     term_width->SetMinimumValue(10);
666     term_width->SetMaximumValue(1024);
667 
668     // Turn off use-color if this is a dumb terminal.
669     const char *term = getenv ("TERM");
670     if (term && !strcmp (term, "dumb"))
671         SetUseColor (false);
672 }
673 
674 Debugger::~Debugger ()
675 {
676     Clear();
677 }
678 
679 void
680 Debugger::Clear()
681 {
682     ClearIOHandlers();
683     StopIOHandlerThread();
684     StopEventHandlerThread();
685     m_listener.Clear();
686     int num_targets = m_target_list.GetNumTargets();
687     for (int i = 0; i < num_targets; i++)
688     {
689         TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
690         if (target_sp)
691         {
692             ProcessSP process_sp (target_sp->GetProcessSP());
693             if (process_sp)
694                 process_sp->Finalize();
695             target_sp->Destroy();
696         }
697     }
698     BroadcasterManager::Clear ();
699 
700     // Close the input file _before_ we close the input read communications class
701     // as it does NOT own the input file, our m_input_file does.
702     m_terminal_state.Clear();
703     if (m_input_file_sp)
704         m_input_file_sp->GetFile().Close ();
705 
706     m_command_interpreter_ap->Clear();
707 }
708 
709 bool
710 Debugger::GetCloseInputOnEOF () const
711 {
712 //    return m_input_comm.GetCloseOnEOF();
713     return false;
714 }
715 
716 void
717 Debugger::SetCloseInputOnEOF (bool b)
718 {
719 //    m_input_comm.SetCloseOnEOF(b);
720 }
721 
722 bool
723 Debugger::GetAsyncExecution ()
724 {
725     return !m_command_interpreter_ap->GetSynchronous();
726 }
727 
728 void
729 Debugger::SetAsyncExecution (bool async_execution)
730 {
731     m_command_interpreter_ap->SetSynchronous (!async_execution);
732 }
733 
734 
735 void
736 Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
737 {
738     if (m_input_file_sp)
739         m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
740     else
741         m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
742 
743     File &in_file = m_input_file_sp->GetFile();
744     if (in_file.IsValid() == false)
745         in_file.SetStream (stdin, true);
746 
747     // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
748     SaveInputTerminalState ();
749 }
750 
751 void
752 Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
753 {
754     if (m_output_file_sp)
755         m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
756     else
757         m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
758 
759     File &out_file = m_output_file_sp->GetFile();
760     if (out_file.IsValid() == false)
761         out_file.SetStream (stdout, false);
762 
763     // do not create the ScriptInterpreter just for setting the output file handle
764     // as the constructor will know how to do the right thing on its own
765     const bool can_create = false;
766     ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
767     if (script_interpreter)
768         script_interpreter->ResetOutputFileHandle (fh);
769 }
770 
771 void
772 Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
773 {
774     if (m_error_file_sp)
775         m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
776     else
777         m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
778 
779     File &err_file = m_error_file_sp->GetFile();
780     if (err_file.IsValid() == false)
781         err_file.SetStream (stderr, false);
782 }
783 
784 void
785 Debugger::SaveInputTerminalState ()
786 {
787     if (m_input_file_sp)
788     {
789         File &in_file = m_input_file_sp->GetFile();
790         if (in_file.GetDescriptor() != File::kInvalidDescriptor)
791             m_terminal_state.Save(in_file.GetDescriptor(), true);
792     }
793 }
794 
795 void
796 Debugger::RestoreInputTerminalState ()
797 {
798     m_terminal_state.Restore();
799 }
800 
801 ExecutionContext
802 Debugger::GetSelectedExecutionContext ()
803 {
804     ExecutionContext exe_ctx;
805     TargetSP target_sp(GetSelectedTarget());
806     exe_ctx.SetTargetSP (target_sp);
807 
808     if (target_sp)
809     {
810         ProcessSP process_sp (target_sp->GetProcessSP());
811         exe_ctx.SetProcessSP (process_sp);
812         if (process_sp && process_sp->IsRunning() == false)
813         {
814             ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
815             if (thread_sp)
816             {
817                 exe_ctx.SetThreadSP (thread_sp);
818                 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
819                 if (exe_ctx.GetFramePtr() == NULL)
820                     exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
821             }
822         }
823     }
824     return exe_ctx;
825 }
826 
827 void
828 Debugger::DispatchInputInterrupt ()
829 {
830     Mutex::Locker locker (m_input_reader_stack.GetMutex());
831     IOHandlerSP reader_sp (m_input_reader_stack.Top());
832     if (reader_sp)
833         reader_sp->Interrupt();
834 }
835 
836 void
837 Debugger::DispatchInputEndOfFile ()
838 {
839     Mutex::Locker locker (m_input_reader_stack.GetMutex());
840     IOHandlerSP reader_sp (m_input_reader_stack.Top());
841     if (reader_sp)
842         reader_sp->GotEOF();
843 }
844 
845 void
846 Debugger::ClearIOHandlers ()
847 {
848     // The bottom input reader should be the main debugger input reader.  We do not want to close that one here.
849     Mutex::Locker locker (m_input_reader_stack.GetMutex());
850     while (m_input_reader_stack.GetSize() > 1)
851     {
852         IOHandlerSP reader_sp (m_input_reader_stack.Top());
853         if (reader_sp)
854         {
855             m_input_reader_stack.Pop();
856             reader_sp->SetIsDone(true);
857             reader_sp->Cancel();
858         }
859     }
860 }
861 
862 void
863 Debugger::ExecuteIOHanders()
864 {
865 
866     while (1)
867     {
868         IOHandlerSP reader_sp(m_input_reader_stack.Top());
869         if (!reader_sp)
870             break;
871 
872         reader_sp->Activate();
873         reader_sp->Run();
874         reader_sp->Deactivate();
875 
876         // Remove all input readers that are done from the top of the stack
877         while (1)
878         {
879             IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
880             if (top_reader_sp && top_reader_sp->GetIsDone())
881                 m_input_reader_stack.Pop();
882             else
883                 break;
884         }
885     }
886     ClearIOHandlers();
887 }
888 
889 bool
890 Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
891 {
892     return m_input_reader_stack.IsTop (reader_sp);
893 }
894 
895 
896 ConstString
897 Debugger::GetTopIOHandlerControlSequence(char ch)
898 {
899     return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
900 }
901 
902 void
903 Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
904 {
905     Mutex::Locker locker (m_input_reader_stack.GetMutex());
906     PushIOHandler (reader_sp);
907 
908     IOHandlerSP top_reader_sp = reader_sp;
909     while (top_reader_sp)
910     {
911         top_reader_sp->Activate();
912         top_reader_sp->Run();
913         top_reader_sp->Deactivate();
914 
915         if (top_reader_sp.get() == reader_sp.get())
916         {
917             if (PopIOHandler (reader_sp))
918                 break;
919         }
920 
921         while (1)
922         {
923             top_reader_sp = m_input_reader_stack.Top();
924             if (top_reader_sp && top_reader_sp->GetIsDone())
925                 m_input_reader_stack.Pop();
926             else
927                 break;
928         }
929     }
930 }
931 
932 void
933 Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
934 {
935     // Before an IOHandler runs, it must have in/out/err streams.
936     // This function is called when one ore more of the streams
937     // are NULL. We use the top input reader's in/out/err streams,
938     // or fall back to the debugger file handles, or we fall back
939     // onto stdin/stdout/stderr as a last resort.
940 
941     Mutex::Locker locker (m_input_reader_stack.GetMutex());
942     IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
943     // If no STDIN has been set, then set it appropriately
944     if (!in)
945     {
946         if (top_reader_sp)
947             in = top_reader_sp->GetInputStreamFile();
948         else
949             in = GetInputFile();
950 
951         // If there is nothing, use stdin
952         if (!in)
953             in = StreamFileSP(new StreamFile(stdin, false));
954     }
955     // If no STDOUT has been set, then set it appropriately
956     if (!out)
957     {
958         if (top_reader_sp)
959             out = top_reader_sp->GetOutputStreamFile();
960         else
961             out = GetOutputFile();
962 
963         // If there is nothing, use stdout
964         if (!out)
965             out = StreamFileSP(new StreamFile(stdout, false));
966     }
967     // If no STDERR has been set, then set it appropriately
968     if (!err)
969     {
970         if (top_reader_sp)
971             err = top_reader_sp->GetErrorStreamFile();
972         else
973             err = GetErrorFile();
974 
975         // If there is nothing, use stderr
976         if (!err)
977             err = StreamFileSP(new StreamFile(stdout, false));
978 
979     }
980 }
981 
982 void
983 Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
984 {
985     if (!reader_sp)
986         return;
987 
988     // Got the current top input reader...
989     IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
990 
991     // Don't push the same IO handler twice...
992     if (reader_sp.get() != top_reader_sp.get())
993     {
994         // Push our new input reader
995         m_input_reader_stack.Push (reader_sp);
996 
997         // Interrupt the top input reader to it will exit its Run() function
998         // and let this new input reader take over
999         if (top_reader_sp)
1000             top_reader_sp->Deactivate();
1001     }
1002 }
1003 
1004 bool
1005 Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
1006 {
1007     bool result = false;
1008 
1009     Mutex::Locker locker (m_input_reader_stack.GetMutex());
1010 
1011     // The reader on the stop of the stack is done, so let the next
1012     // read on the stack refresh its prompt and if there is one...
1013     if (!m_input_reader_stack.IsEmpty())
1014     {
1015         IOHandlerSP reader_sp(m_input_reader_stack.Top());
1016 
1017         if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
1018         {
1019             reader_sp->Deactivate();
1020             reader_sp->Cancel();
1021             m_input_reader_stack.Pop ();
1022 
1023             reader_sp = m_input_reader_stack.Top();
1024             if (reader_sp)
1025                 reader_sp->Activate();
1026 
1027             result = true;
1028         }
1029     }
1030     return result;
1031 }
1032 
1033 bool
1034 Debugger::HideTopIOHandler()
1035 {
1036     Mutex::Locker locker;
1037 
1038     if (locker.TryLock(m_input_reader_stack.GetMutex()))
1039     {
1040         IOHandlerSP reader_sp(m_input_reader_stack.Top());
1041         if (reader_sp)
1042             reader_sp->Hide();
1043         return true;
1044     }
1045     return false;
1046 }
1047 
1048 void
1049 Debugger::RefreshTopIOHandler()
1050 {
1051     IOHandlerSP reader_sp(m_input_reader_stack.Top());
1052     if (reader_sp)
1053         reader_sp->Refresh();
1054 }
1055 
1056 
1057 StreamSP
1058 Debugger::GetAsyncOutputStream ()
1059 {
1060     return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1061                                                CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1062 }
1063 
1064 StreamSP
1065 Debugger::GetAsyncErrorStream ()
1066 {
1067     return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1068                                                CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1069 }
1070 
1071 size_t
1072 Debugger::GetNumDebuggers()
1073 {
1074     if (g_shared_debugger_refcount > 0)
1075     {
1076         Mutex::Locker locker (GetDebuggerListMutex ());
1077         return GetDebuggerList().size();
1078     }
1079     return 0;
1080 }
1081 
1082 lldb::DebuggerSP
1083 Debugger::GetDebuggerAtIndex (size_t index)
1084 {
1085     DebuggerSP debugger_sp;
1086 
1087     if (g_shared_debugger_refcount > 0)
1088     {
1089         Mutex::Locker locker (GetDebuggerListMutex ());
1090         DebuggerList &debugger_list = GetDebuggerList();
1091 
1092         if (index < debugger_list.size())
1093             debugger_sp = debugger_list[index];
1094     }
1095 
1096     return debugger_sp;
1097 }
1098 
1099 DebuggerSP
1100 Debugger::FindDebuggerWithID (lldb::user_id_t id)
1101 {
1102     DebuggerSP debugger_sp;
1103 
1104     if (g_shared_debugger_refcount > 0)
1105     {
1106         Mutex::Locker locker (GetDebuggerListMutex ());
1107         DebuggerList &debugger_list = GetDebuggerList();
1108         DebuggerList::iterator pos, end = debugger_list.end();
1109         for (pos = debugger_list.begin(); pos != end; ++pos)
1110         {
1111             if ((*pos).get()->GetID() == id)
1112             {
1113                 debugger_sp = *pos;
1114                 break;
1115             }
1116         }
1117     }
1118     return debugger_sp;
1119 }
1120 
1121 #if 0
1122 static void
1123 TestPromptFormats (StackFrame *frame)
1124 {
1125     if (frame == NULL)
1126         return;
1127 
1128     StreamString s;
1129     const char *prompt_format =
1130     "{addr = '${addr}'\n}"
1131     "{process.id = '${process.id}'\n}"
1132     "{process.name = '${process.name}'\n}"
1133     "{process.file.basename = '${process.file.basename}'\n}"
1134     "{process.file.fullpath = '${process.file.fullpath}'\n}"
1135     "{thread.id = '${thread.id}'\n}"
1136     "{thread.index = '${thread.index}'\n}"
1137     "{thread.name = '${thread.name}'\n}"
1138     "{thread.queue = '${thread.queue}'\n}"
1139     "{thread.stop-reason = '${thread.stop-reason}'\n}"
1140     "{target.arch = '${target.arch}'\n}"
1141     "{module.file.basename = '${module.file.basename}'\n}"
1142     "{module.file.fullpath = '${module.file.fullpath}'\n}"
1143     "{file.basename = '${file.basename}'\n}"
1144     "{file.fullpath = '${file.fullpath}'\n}"
1145     "{frame.index = '${frame.index}'\n}"
1146     "{frame.pc = '${frame.pc}'\n}"
1147     "{frame.sp = '${frame.sp}'\n}"
1148     "{frame.fp = '${frame.fp}'\n}"
1149     "{frame.flags = '${frame.flags}'\n}"
1150     "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1151     "{frame.reg.rip = '${frame.reg.rip}'\n}"
1152     "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1153     "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1154     "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1155     "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1156     "{frame.reg.carp = '${frame.reg.carp}'\n}"
1157     "{function.id = '${function.id}'\n}"
1158     "{function.name = '${function.name}'\n}"
1159     "{function.name-with-args = '${function.name-with-args}'\n}"
1160     "{function.addr-offset = '${function.addr-offset}'\n}"
1161     "{function.line-offset = '${function.line-offset}'\n}"
1162     "{function.pc-offset = '${function.pc-offset}'\n}"
1163     "{line.file.basename = '${line.file.basename}'\n}"
1164     "{line.file.fullpath = '${line.file.fullpath}'\n}"
1165     "{line.number = '${line.number}'\n}"
1166     "{line.start-addr = '${line.start-addr}'\n}"
1167     "{line.end-addr = '${line.end-addr}'\n}"
1168 ;
1169 
1170     SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1171     ExecutionContext exe_ctx;
1172     frame->CalculateExecutionContext(exe_ctx);
1173     if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
1174     {
1175         printf("%s\n", s.GetData());
1176     }
1177     else
1178     {
1179         printf ("what we got: %s\n", s.GetData());
1180     }
1181 }
1182 #endif
1183 
1184 static bool
1185 ScanFormatDescriptor (const char* var_name_begin,
1186                       const char* var_name_end,
1187                       const char** var_name_final,
1188                       const char** percent_position,
1189                       Format* custom_format,
1190                       ValueObject::ValueObjectRepresentationStyle* val_obj_display)
1191 {
1192     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1193     *percent_position = ::strchr(var_name_begin,'%');
1194     if (!*percent_position || *percent_position > var_name_end)
1195     {
1196         if (log)
1197             log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
1198         *var_name_final = var_name_end;
1199     }
1200     else
1201     {
1202         *var_name_final = *percent_position;
1203         std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
1204         if (log)
1205             log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
1206         if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
1207                                                   true,
1208                                                   *custom_format) )
1209         {
1210             if (log)
1211                 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
1212 
1213             switch (format_name.front())
1214             {
1215                 case '@':             // if this is an @ sign, print ObjC description
1216                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1217                     break;
1218                 case 'V': // if this is a V, print the value using the default format
1219                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1220                     break;
1221                 case 'L': // if this is an L, print the location of the value
1222                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1223                     break;
1224                 case 'S': // if this is an S, print the summary after all
1225                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1226                     break;
1227                 case '#': // if this is a '#', print the number of children
1228                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1229                     break;
1230                 case 'T': // if this is a 'T', print the type
1231                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1232                     break;
1233                 case 'N': // if this is a 'N', print the name
1234                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1235                     break;
1236                 case '>': // if this is a '>', print the name
1237                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1238                     break;
1239                 default:
1240                     if (log)
1241                         log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
1242                     break;
1243             }
1244         }
1245         // a good custom format tells us to print the value using it
1246         else
1247         {
1248             if (log)
1249                 log->Printf("[ScanFormatDescriptor] will display value for this VO");
1250             *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1251         }
1252     }
1253     if (log)
1254         log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
1255                     *custom_format,
1256                     *val_obj_display);
1257     return true;
1258 }
1259 
1260 static bool
1261 ScanBracketedRange (const char* var_name_begin,
1262                     const char* var_name_end,
1263                     const char* var_name_final,
1264                     const char** open_bracket_position,
1265                     const char** separator_position,
1266                     const char** close_bracket_position,
1267                     const char** var_name_final_if_array_range,
1268                     int64_t* index_lower,
1269                     int64_t* index_higher)
1270 {
1271     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1272     *open_bracket_position = ::strchr(var_name_begin,'[');
1273     if (*open_bracket_position && *open_bracket_position < var_name_final)
1274     {
1275         *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1276         *close_bracket_position = ::strchr(*open_bracket_position,']');
1277         // as usual, we assume that [] will come before %
1278         //printf("trying to expand a []\n");
1279         *var_name_final_if_array_range = *open_bracket_position;
1280         if (*close_bracket_position - *open_bracket_position == 1)
1281         {
1282             if (log)
1283                 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
1284             *index_lower = 0;
1285         }
1286         else if (*separator_position == NULL || *separator_position > var_name_end)
1287         {
1288             char *end = NULL;
1289             *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1290             *index_higher = *index_lower;
1291             if (log)
1292                 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
1293         }
1294         else if (*close_bracket_position && *close_bracket_position < var_name_end)
1295         {
1296             char *end = NULL;
1297             *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1298             *index_higher = ::strtoul (*separator_position+1, &end, 0);
1299             if (log)
1300                 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
1301         }
1302         else
1303         {
1304             if (log)
1305                 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
1306             return false;
1307         }
1308         if (*index_lower > *index_higher && *index_higher > 0)
1309         {
1310             if (log)
1311                 log->Printf("[ScanBracketedRange] swapping indices");
1312             int64_t temp = *index_lower;
1313             *index_lower = *index_higher;
1314             *index_higher = temp;
1315         }
1316     }
1317     else if (log)
1318             log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
1319     return true;
1320 }
1321 
1322 template <typename T>
1323 static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1324 {
1325     if (script_interpreter)
1326     {
1327         Error script_error;
1328         std::string script_output;
1329 
1330         if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1331         {
1332             s.Printf("%s", script_output.c_str());
1333             return true;
1334         }
1335         else
1336         {
1337             s.Printf("<error: %s>",script_error.AsCString());
1338         }
1339     }
1340     return false;
1341 }
1342 
1343 static ValueObjectSP
1344 ExpandIndexedExpression (ValueObject* valobj,
1345                          size_t index,
1346                          StackFrame* frame,
1347                          bool deref_pointer)
1348 {
1349     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1350     const char* ptr_deref_format = "[%d]";
1351     std::string ptr_deref_buffer(10,0);
1352     ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
1353     if (log)
1354         log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
1355     const char* first_unparsed;
1356     ValueObject::GetValueForExpressionPathOptions options;
1357     ValueObject::ExpressionPathEndResultType final_value_type;
1358     ValueObject::ExpressionPathScanEndReason reason_to_stop;
1359     ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
1360     ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
1361                                                           &first_unparsed,
1362                                                           &reason_to_stop,
1363                                                           &final_value_type,
1364                                                           options,
1365                                                           &what_next);
1366     if (!item)
1367     {
1368         if (log)
1369             log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
1370                " final_value_type %d",
1371                first_unparsed, reason_to_stop, final_value_type);
1372     }
1373     else
1374     {
1375         if (log)
1376             log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1377                " final_value_type %d",
1378                first_unparsed, reason_to_stop, final_value_type);
1379     }
1380     return item;
1381 }
1382 
1383 static inline bool
1384 IsToken(const char *var_name_begin, const char *var)
1385 {
1386     return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1387 }
1388 
1389 static bool
1390 IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1391     const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1392 {
1393     int var_len = strlen(var);
1394     if (::strncmp (var_name_begin, var, var_len) == 0)
1395     {
1396         var_name_begin += var_len;
1397         if (*var_name_begin == '}')
1398         {
1399             format = default_format;
1400             return true;
1401         }
1402         else if (*var_name_begin == '%')
1403         {
1404             // Allow format specifiers: x|X|u with optional width specifiers.
1405             //   ${thread.id%x}    ; hex
1406             //   ${thread.id%X}    ; uppercase hex
1407             //   ${thread.id%u}    ; unsigned decimal
1408             //   ${thread.id%8.8X} ; width.precision + specifier
1409             //   ${thread.id%tid}  ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1410             int dot_count = 0;
1411             const char *specifier = NULL;
1412             int width_precision_length = 0;
1413             const char *width_precision = ++var_name_begin;
1414             while (isdigit(*var_name_begin) || *var_name_begin == '.')
1415             {
1416                 dot_count += (*var_name_begin == '.');
1417                 if (dot_count > 1)
1418                     break;
1419                 var_name_begin++;
1420                 width_precision_length++;
1421             }
1422 
1423             if (IsToken (var_name_begin, "tid}"))
1424             {
1425                 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1426                 if (target)
1427                 {
1428                     ArchSpec arch (target->GetArchitecture ());
1429                     llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1430                     if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1431                         specifier = PRIu64;
1432                 }
1433                 if (!specifier)
1434                 {
1435                     format = default_format;
1436                     return true;
1437                 }
1438             }
1439             else if (IsToken (var_name_begin, "x}"))
1440                 specifier = PRIx64;
1441             else if (IsToken (var_name_begin, "X}"))
1442                 specifier = PRIX64;
1443             else if (IsToken (var_name_begin, "u}"))
1444                 specifier = PRIu64;
1445 
1446             if (specifier)
1447             {
1448                 format = "%";
1449                 if (width_precision_length)
1450                     format += std::string(width_precision, width_precision_length);
1451                 format += specifier;
1452                 return true;
1453             }
1454         }
1455     }
1456     return false;
1457 }
1458 
1459 // Find information for the "thread.info.*" specifiers in a format string
1460 static bool
1461 FormatThreadExtendedInfoRecurse
1462 (
1463     const char *var_name_begin,
1464     StructuredData::ObjectSP thread_info_dictionary,
1465     const SymbolContext *sc,
1466     const ExecutionContext *exe_ctx,
1467     Stream &s
1468 )
1469 {
1470     bool var_success = false;
1471     std::string token_format;
1472 
1473     llvm::StringRef var_name(var_name_begin);
1474     size_t percent_idx = var_name.find('%');
1475     size_t close_curly_idx = var_name.find('}');
1476     llvm::StringRef path = var_name;
1477     llvm::StringRef formatter = var_name;
1478 
1479     // 'path' will be the dot separated list of objects to transverse up until we hit
1480     // a close curly brace, a percent sign, or an end of string.
1481     if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos)
1482     {
1483         if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos)
1484         {
1485             if (percent_idx < close_curly_idx)
1486             {
1487                 path = var_name.slice(0, percent_idx);
1488                 formatter = var_name.substr (percent_idx);
1489             }
1490             else
1491             {
1492                 path = var_name.slice(0, close_curly_idx);
1493                 formatter = var_name.substr (close_curly_idx);
1494             }
1495         }
1496         else if (percent_idx != llvm::StringRef::npos)
1497         {
1498             path = var_name.slice(0, percent_idx);
1499             formatter = var_name.substr (percent_idx);
1500         }
1501         else if (close_curly_idx != llvm::StringRef::npos)
1502         {
1503             path = var_name.slice(0, close_curly_idx);
1504             formatter = var_name.substr (close_curly_idx);
1505         }
1506     }
1507 
1508     StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1509 
1510     if (value.get())
1511     {
1512         if (value->GetType() == StructuredData::Type::eTypeInteger)
1513         {
1514             if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
1515             {
1516                 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue());
1517                 var_success = true;
1518             }
1519         }
1520         else if (value->GetType() == StructuredData::Type::eTypeFloat)
1521         {
1522             s.Printf ("%f", value->GetAsFloat()->GetValue());
1523             var_success = true;
1524         }
1525         else if (value->GetType() == StructuredData::Type::eTypeString)
1526         {
1527             s.Printf("%s", value->GetAsString()->GetValue().c_str());
1528             var_success = true;
1529         }
1530         else if (value->GetType() == StructuredData::Type::eTypeArray)
1531         {
1532             if (value->GetAsArray()->GetSize() > 0)
1533             {
1534                 s.Printf ("%zu", value->GetAsArray()->GetSize());
1535                 var_success = true;
1536             }
1537         }
1538         else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1539         {
1540             s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1541             var_success = true;
1542         }
1543     }
1544 
1545     return var_success;
1546 }
1547 
1548 
1549 static bool
1550 FormatPromptRecurse
1551 (
1552     const char *format,
1553     const SymbolContext *sc,
1554     const ExecutionContext *exe_ctx,
1555     const Address *addr,
1556     Stream &s,
1557     const char **end,
1558     ValueObject* valobj
1559 )
1560 {
1561     ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
1562     bool success = true;
1563     const char *p;
1564     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1565 
1566     for (p = format; *p != '\0'; ++p)
1567     {
1568         if (realvalobj)
1569         {
1570             valobj = realvalobj;
1571             realvalobj = NULL;
1572         }
1573         size_t non_special_chars = ::strcspn (p, "${}\\");
1574         if (non_special_chars > 0)
1575         {
1576             if (success)
1577                 s.Write (p, non_special_chars);
1578             p += non_special_chars;
1579         }
1580 
1581         if (*p == '\0')
1582         {
1583             break;
1584         }
1585         else if (*p == '{')
1586         {
1587             // Start a new scope that must have everything it needs if it is to
1588             // to make it into the final output stream "s". If you want to make
1589             // a format that only prints out the function or symbol name if there
1590             // is one in the symbol context you can use:
1591             //      "{function =${function.name}}"
1592             // The first '{' starts a new scope that end with the matching '}' at
1593             // the end of the string. The contents "function =${function.name}"
1594             // will then be evaluated and only be output if there is a function
1595             // or symbol with a valid name.
1596             StreamString sub_strm;
1597 
1598             ++p;  // Skip the '{'
1599 
1600             if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
1601             {
1602                 // The stream had all it needed
1603                 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1604             }
1605             if (*p != '}')
1606             {
1607                 success = false;
1608                 break;
1609             }
1610         }
1611         else if (*p == '}')
1612         {
1613             // End of a enclosing scope
1614             break;
1615         }
1616         else if (*p == '$')
1617         {
1618             // We have a prompt variable to print
1619             ++p;
1620             if (*p == '{')
1621             {
1622                 ++p;
1623                 const char *var_name_begin = p;
1624                 const char *var_name_end = ::strchr (p, '}');
1625 
1626                 if (var_name_end && var_name_begin < var_name_end)
1627                 {
1628                     // if we have already failed to parse, skip this variable
1629                     if (success)
1630                     {
1631                         const char *cstr = NULL;
1632                         std::string token_format;
1633                         Address format_addr;
1634                         bool calculate_format_addr_function_offset = false;
1635                         // Set reg_kind and reg_num to invalid values
1636                         RegisterKind reg_kind = kNumRegisterKinds;
1637                         uint32_t reg_num = LLDB_INVALID_REGNUM;
1638                         FileSpec format_file_spec;
1639                         const RegisterInfo *reg_info = NULL;
1640                         RegisterContext *reg_ctx = NULL;
1641                         bool do_deref_pointer = false;
1642                         ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1643                         ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
1644 
1645                         // Each variable must set success to true below...
1646                         bool var_success = false;
1647                         switch (var_name_begin[0])
1648                         {
1649                         case '*':
1650                         case 'v':
1651                         case 's':
1652                             {
1653                                 if (!valobj)
1654                                     break;
1655 
1656                                 if (log)
1657                                     log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
1658 
1659                                 // check for *var and *svar
1660                                 if (*var_name_begin == '*')
1661                                 {
1662                                     do_deref_pointer = true;
1663                                     var_name_begin++;
1664                                     if (log)
1665                                         log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
1666                                 }
1667 
1668                                 if (*var_name_begin == 's')
1669                                 {
1670                                     if (!valobj->IsSynthetic())
1671                                         valobj = valobj->GetSyntheticValue().get();
1672                                     if (!valobj)
1673                                         break;
1674                                     var_name_begin++;
1675                                     if (log)
1676                                         log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
1677                                 }
1678 
1679                                 // should be a 'v' by now
1680                                 if (*var_name_begin != 'v')
1681                                     break;
1682 
1683                                 if (log)
1684                                     log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
1685 
1686                                 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
1687                                                                                   ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
1688                                 ValueObject::GetValueForExpressionPathOptions options;
1689                                 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
1690                                 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1691                                 ValueObject* target = NULL;
1692                                 Format custom_format = eFormatInvalid;
1693                                 const char* var_name_final = NULL;
1694                                 const char* var_name_final_if_array_range = NULL;
1695                                 const char* close_bracket_position = NULL;
1696                                 int64_t index_lower = -1;
1697                                 int64_t index_higher = -1;
1698                                 bool is_array_range = false;
1699                                 const char* first_unparsed;
1700                                 bool was_plain_var = false;
1701                                 bool was_var_format = false;
1702                                 bool was_var_indexed = false;
1703 
1704                                 if (!valobj) break;
1705                                 // simplest case ${var}, just print valobj's value
1706                                 if (IsToken (var_name_begin, "var}"))
1707                                 {
1708                                     was_plain_var = true;
1709                                     target = valobj;
1710                                     val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1711                                 }
1712                                 else if (IsToken (var_name_begin,"var%"))
1713                                 {
1714                                     was_var_format = true;
1715                                     // this is a variable with some custom format applied to it
1716                                     const char* percent_position;
1717                                     target = valobj;
1718                                     val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1719                                     ScanFormatDescriptor (var_name_begin,
1720                                                           var_name_end,
1721                                                           &var_name_final,
1722                                                           &percent_position,
1723                                                           &custom_format,
1724                                                           &val_obj_display);
1725                                 }
1726                                     // this is ${var.something} or multiple .something nested
1727                                 else if (IsToken (var_name_begin, "var"))
1728                                 {
1729                                     if (IsToken (var_name_begin, "var["))
1730                                         was_var_indexed = true;
1731                                     const char* percent_position;
1732                                     ScanFormatDescriptor (var_name_begin,
1733                                                           var_name_end,
1734                                                           &var_name_final,
1735                                                           &percent_position,
1736                                                           &custom_format,
1737                                                           &val_obj_display);
1738 
1739                                     const char* open_bracket_position;
1740                                     const char* separator_position;
1741                                     ScanBracketedRange (var_name_begin,
1742                                                         var_name_end,
1743                                                         var_name_final,
1744                                                         &open_bracket_position,
1745                                                         &separator_position,
1746                                                         &close_bracket_position,
1747                                                         &var_name_final_if_array_range,
1748                                                         &index_lower,
1749                                                         &index_higher);
1750 
1751                                     Error error;
1752 
1753                                     std::string expr_path(var_name_final-var_name_begin-1,0);
1754                                     memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1755 
1756                                     if (log)
1757                                         log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1758 
1759                                     target = valobj->GetValueForExpressionPath(expr_path.c_str(),
1760                                                                              &first_unparsed,
1761                                                                              &reason_to_stop,
1762                                                                              &final_value_type,
1763                                                                              options,
1764                                                                              &what_next).get();
1765 
1766                                     if (!target)
1767                                     {
1768                                         if (log)
1769                                             log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
1770                                                " final_value_type %d",
1771                                                first_unparsed, reason_to_stop, final_value_type);
1772                                         break;
1773                                     }
1774                                     else
1775                                     {
1776                                         if (log)
1777                                             log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1778                                                " final_value_type %d",
1779                                                first_unparsed, reason_to_stop, final_value_type);
1780                                     }
1781                                 }
1782                                 else
1783                                     break;
1784 
1785                                 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1786                                                   final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
1787 
1788                                 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
1789 
1790                                 if (do_deref_pointer && !is_array_range)
1791                                 {
1792                                     // I have not deref-ed yet, let's do it
1793                                     // this happens when we are not going through GetValueForVariableExpressionPath
1794                                     // to get to the target ValueObject
1795                                     Error error;
1796                                     target = target->Dereference(error).get();
1797                                     if (error.Fail())
1798                                     {
1799                                         if (log)
1800                                             log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
1801                                         break;
1802                                     }
1803                                     do_deref_pointer = false;
1804                                 }
1805 
1806                                 if (!target)
1807                                 {
1808                                     if (log)
1809                                         log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1810                                     break;
1811                                 }
1812 
1813                                 // we do not want to use the summary for a bitfield of type T:n
1814                                 // if we were originally dealing with just a T - that would get
1815                                 // us into an endless recursion
1816                                 if (target->IsBitfield() && was_var_indexed)
1817                                 {
1818                                     // TODO: check for a (T:n)-specific summary - we should still obey that
1819                                     StreamString bitfield_name;
1820                                     bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1821                                     lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1822                                     if (!DataVisualization::GetSummaryForType(type_sp))
1823                                         val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1824                                 }
1825 
1826                                 // TODO use flags for these
1827                                 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1828                                 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1829                                 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1830                                 bool is_aggregate = target->GetClangType().IsAggregateType();
1831 
1832                                 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
1833                                 {
1834                                     StreamString str_temp;
1835                                     if (log)
1836                                         log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
1837 
1838                                     if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
1839                                     {
1840                                         // try to use the special cases
1841                                         var_success = target->DumpPrintableRepresentation(str_temp,
1842                                                                                           val_obj_display,
1843                                                                                           custom_format);
1844                                         if (log)
1845                                             log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
1846 
1847                                         // should not happen
1848                                         if (var_success)
1849                                             s << str_temp.GetData();
1850                                         var_success = true;
1851                                         break;
1852                                     }
1853                                     else
1854                                     {
1855                                         if (was_plain_var) // if ${var}
1856                                         {
1857                                             s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1858                                         }
1859                                         else if (is_pointer) // if pointer, value is the address stored
1860                                         {
1861                                             target->DumpPrintableRepresentation (s,
1862                                                                                  val_obj_display,
1863                                                                                  custom_format,
1864                                                                                  ValueObject::ePrintableRepresentationSpecialCasesDisable);
1865                                         }
1866                                         var_success = true;
1867                                         break;
1868                                     }
1869                                 }
1870 
1871                                 // if directly trying to print ${var}, and this is an aggregate, display a nice
1872                                 // type @ location message
1873                                 if (is_aggregate && was_plain_var)
1874                                 {
1875                                     s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1876                                     var_success = true;
1877                                     break;
1878                                 }
1879 
1880                                 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
1881                                 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
1882                                 {
1883                                     s << "<invalid use of aggregate type>";
1884                                     var_success = true;
1885                                     break;
1886                                 }
1887 
1888                                 if (!is_array_range)
1889                                 {
1890                                     if (log)
1891                                         log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
1892                                     var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1893                                 }
1894                                 else
1895                                 {
1896                                     if (log)
1897                                         log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
1898                                     if (!is_array && !is_pointer)
1899                                         break;
1900                                     if (log)
1901                                         log->Printf("[Debugger::FormatPrompt] handle as array");
1902                                     const char* special_directions = NULL;
1903                                     StreamString special_directions_writer;
1904                                     if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1905                                     {
1906                                         ConstString additional_data;
1907                                         additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1908                                         special_directions_writer.Printf("${%svar%s}",
1909                                                                          do_deref_pointer ? "*" : "",
1910                                                                          additional_data.GetCString());
1911                                         special_directions = special_directions_writer.GetData();
1912                                     }
1913 
1914                                     // let us display items index_lower thru index_higher of this array
1915                                     s.PutChar('[');
1916                                     var_success = true;
1917 
1918                                     if (index_higher < 0)
1919                                         index_higher = valobj->GetNumChildren() - 1;
1920 
1921                                     uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
1922 
1923                                     for (;index_lower<=index_higher;index_lower++)
1924                                     {
1925                                         ValueObject* item = ExpandIndexedExpression (target,
1926                                                                                      index_lower,
1927                                                                                      exe_ctx->GetFramePtr(),
1928                                                                                      false).get();
1929 
1930                                         if (!item)
1931                                         {
1932                                             if (log)
1933                                                 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
1934                                         }
1935                                         else
1936                                         {
1937                                             if (log)
1938                                                 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
1939                                         }
1940 
1941                                         if (!special_directions)
1942                                             var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1943                                         else
1944                                             var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
1945 
1946                                         if (--max_num_children == 0)
1947                                         {
1948                                             s.PutCString(", ...");
1949                                             break;
1950                                         }
1951 
1952                                         if (index_lower < index_higher)
1953                                             s.PutChar(',');
1954                                     }
1955                                     s.PutChar(']');
1956                                 }
1957                             }
1958                             break;
1959                         case 'a':
1960                             if (IsToken (var_name_begin, "addr}"))
1961                             {
1962                                 if (addr && addr->IsValid())
1963                                 {
1964                                     var_success = true;
1965                                     format_addr = *addr;
1966                                 }
1967                             }
1968                             break;
1969 
1970                         case 'p':
1971                             if (IsToken (var_name_begin, "process."))
1972                             {
1973                                 if (exe_ctx)
1974                                 {
1975                                     Process *process = exe_ctx->GetProcessPtr();
1976                                     if (process)
1977                                     {
1978                                         var_name_begin += ::strlen ("process.");
1979                                         if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
1980                                         {
1981                                             s.Printf(token_format.c_str(), process->GetID());
1982                                             var_success = true;
1983                                         }
1984                                         else if ((IsToken (var_name_begin, "name}")) ||
1985                                                 (IsToken (var_name_begin, "file.basename}")) ||
1986                                                 (IsToken (var_name_begin, "file.fullpath}")))
1987                                         {
1988                                             Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1989                                             if (exe_module)
1990                                             {
1991                                                 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1992                                                 {
1993                                                     format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1994                                                     var_success = (bool)format_file_spec;
1995                                                 }
1996                                                 else
1997                                                 {
1998                                                     format_file_spec = exe_module->GetFileSpec();
1999                                                     var_success = (bool)format_file_spec;
2000                                                 }
2001                                             }
2002                                         }
2003                                         else if (IsToken (var_name_begin, "script:"))
2004                                         {
2005                                             var_name_begin += ::strlen("script:");
2006                                             std::string script_name(var_name_begin,var_name_end);
2007                                             ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
2008                                             if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
2009                                                 var_success = true;
2010                                         }
2011                                     }
2012                                 }
2013                             }
2014                             break;
2015 
2016                         case 't':
2017                            if (IsToken (var_name_begin, "thread."))
2018                             {
2019                                 if (exe_ctx)
2020                                 {
2021                                     Thread *thread = exe_ctx->GetThreadPtr();
2022                                     if (thread)
2023                                     {
2024                                         var_name_begin += ::strlen ("thread.");
2025                                         if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
2026                                         {
2027                                             s.Printf(token_format.c_str(), thread->GetID());
2028                                             var_success = true;
2029                                         }
2030                                         else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
2031                                         {
2032                                             s.Printf(token_format.c_str(), thread->GetProtocolID());
2033                                             var_success = true;
2034                                         }
2035                                         else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
2036                                         {
2037                                             s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
2038                                             var_success = true;
2039                                         }
2040                                         else if (IsToken (var_name_begin, "name}"))
2041                                         {
2042                                             cstr = thread->GetName();
2043                                             var_success = cstr && cstr[0];
2044                                             if (var_success)
2045                                                 s.PutCString(cstr);
2046                                         }
2047                                         else if (IsToken (var_name_begin, "queue}"))
2048                                         {
2049                                             cstr = thread->GetQueueName();
2050                                             var_success = cstr && cstr[0];
2051                                             if (var_success)
2052                                                 s.PutCString(cstr);
2053                                         }
2054                                         else if (IsToken (var_name_begin, "stop-reason}"))
2055                                         {
2056                                             StopInfoSP stop_info_sp = thread->GetStopInfo ();
2057                                             if (stop_info_sp && stop_info_sp->IsValid())
2058                                             {
2059                                                 cstr = stop_info_sp->GetDescription();
2060                                                 if (cstr && cstr[0])
2061                                                 {
2062                                                     s.PutCString(cstr);
2063                                                     var_success = true;
2064                                                 }
2065                                             }
2066                                         }
2067                                         else if (IsToken (var_name_begin, "return-value}"))
2068                                         {
2069                                             StopInfoSP stop_info_sp = thread->GetStopInfo ();
2070                                             if (stop_info_sp && stop_info_sp->IsValid())
2071                                             {
2072                                                 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2073                                                 if (return_valobj_sp)
2074                                                 {
2075                                                     return_valobj_sp->Dump(s);
2076                                                     var_success = true;
2077                                                 }
2078                                             }
2079                                         }
2080                                         else if (IsToken (var_name_begin, "completed-expression}"))
2081                                         {
2082                                             StopInfoSP stop_info_sp = thread->GetStopInfo ();
2083                                             if (stop_info_sp && stop_info_sp->IsValid())
2084                                             {
2085                                                 ClangExpressionVariableSP expression_var_sp = StopInfo::GetExpressionVariable (stop_info_sp);
2086                                                 if (expression_var_sp && expression_var_sp->GetValueObject())
2087                                                 {
2088                                                     expression_var_sp->GetValueObject()->Dump(s);
2089                                                     var_success = true;
2090                                                 }
2091                                             }
2092                                         }
2093                                         else if (IsToken (var_name_begin, "script:"))
2094                                         {
2095                                             var_name_begin += ::strlen("script:");
2096                                             std::string script_name(var_name_begin,var_name_end);
2097                                             ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
2098                                             if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
2099                                                 var_success = true;
2100                                         }
2101                                         else if (IsToken (var_name_begin, "info."))
2102                                         {
2103                                             var_name_begin += ::strlen("info.");
2104                                             StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
2105                                             if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
2106                                             {
2107                                                 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s);
2108                                             }
2109                                         }
2110                                     }
2111                                 }
2112                             }
2113                             else if (IsToken (var_name_begin, "target."))
2114                             {
2115                                 // TODO: hookup properties
2116 //                                if (!target_properties_sp)
2117 //                                {
2118 //                                    Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2119 //                                    if (target)
2120 //                                        target_properties_sp = target->GetProperties();
2121 //                                }
2122 //
2123 //                                if (target_properties_sp)
2124 //                                {
2125 //                                    var_name_begin += ::strlen ("target.");
2126 //                                    const char *end_property = strchr(var_name_begin, '}');
2127 //                                    if (end_property)
2128 //                                    {
2129 //                                        ConstString property_name(var_name_begin, end_property - var_name_begin);
2130 //                                        std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2131 //                                        if (!property_value.empty())
2132 //                                        {
2133 //                                            s.PutCString (property_value.c_str());
2134 //                                            var_success = true;
2135 //                                        }
2136 //                                    }
2137 //                                }
2138                                 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2139                                 if (target)
2140                                 {
2141                                     var_name_begin += ::strlen ("target.");
2142                                     if (IsToken (var_name_begin, "arch}"))
2143                                     {
2144                                         ArchSpec arch (target->GetArchitecture ());
2145                                         if (arch.IsValid())
2146                                         {
2147                                             s.PutCString (arch.GetArchitectureName());
2148                                             var_success = true;
2149                                         }
2150                                     }
2151                                     else if (IsToken (var_name_begin, "script:"))
2152                                     {
2153                                         var_name_begin += ::strlen("script:");
2154                                         std::string script_name(var_name_begin,var_name_end);
2155                                         ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
2156                                         if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2157                                             var_success = true;
2158                                     }
2159                                 }
2160                             }
2161                             break;
2162 
2163 
2164                         case 'm':
2165                            if (IsToken (var_name_begin, "module."))
2166                             {
2167                                 if (sc && sc->module_sp.get())
2168                                 {
2169                                     Module *module = sc->module_sp.get();
2170                                     var_name_begin += ::strlen ("module.");
2171 
2172                                     if (IsToken (var_name_begin, "file."))
2173                                     {
2174                                         if (module->GetFileSpec())
2175                                         {
2176                                             var_name_begin += ::strlen ("file.");
2177 
2178                                             if (IsToken (var_name_begin, "basename}"))
2179                                             {
2180                                                 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2181                                                 var_success = (bool)format_file_spec;
2182                                             }
2183                                             else if (IsToken (var_name_begin, "fullpath}"))
2184                                             {
2185                                                 format_file_spec = module->GetFileSpec();
2186                                                 var_success = (bool)format_file_spec;
2187                                             }
2188                                         }
2189                                     }
2190                                 }
2191                             }
2192                             break;
2193 
2194 
2195                         case 'f':
2196                            if (IsToken (var_name_begin, "file."))
2197                             {
2198                                 if (sc && sc->comp_unit != NULL)
2199                                 {
2200                                     var_name_begin += ::strlen ("file.");
2201 
2202                                     if (IsToken (var_name_begin, "basename}"))
2203                                     {
2204                                         format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2205                                         var_success = (bool)format_file_spec;
2206                                     }
2207                                     else if (IsToken (var_name_begin, "fullpath}"))
2208                                     {
2209                                         format_file_spec = *sc->comp_unit;
2210                                         var_success = (bool)format_file_spec;
2211                                     }
2212                                 }
2213                             }
2214                            else if (IsToken (var_name_begin, "frame."))
2215                             {
2216                                 if (exe_ctx)
2217                                 {
2218                                     StackFrame *frame = exe_ctx->GetFramePtr();
2219                                     if (frame)
2220                                     {
2221                                         var_name_begin += ::strlen ("frame.");
2222                                         if (IsToken (var_name_begin, "index}"))
2223                                         {
2224                                             s.Printf("%u", frame->GetFrameIndex());
2225                                             var_success = true;
2226                                         }
2227                                         else if (IsToken (var_name_begin, "pc}"))
2228                                         {
2229                                             reg_kind = eRegisterKindGeneric;
2230                                             reg_num = LLDB_REGNUM_GENERIC_PC;
2231                                             var_success = true;
2232                                         }
2233                                         else if (IsToken (var_name_begin, "sp}"))
2234                                         {
2235                                             reg_kind = eRegisterKindGeneric;
2236                                             reg_num = LLDB_REGNUM_GENERIC_SP;
2237                                             var_success = true;
2238                                         }
2239                                         else if (IsToken (var_name_begin, "fp}"))
2240                                         {
2241                                             reg_kind = eRegisterKindGeneric;
2242                                             reg_num = LLDB_REGNUM_GENERIC_FP;
2243                                             var_success = true;
2244                                         }
2245                                         else if (IsToken (var_name_begin, "flags}"))
2246                                         {
2247                                             reg_kind = eRegisterKindGeneric;
2248                                             reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2249                                             var_success = true;
2250                                         }
2251                                         else if (IsToken (var_name_begin, "reg."))
2252                                         {
2253                                             reg_ctx = frame->GetRegisterContext().get();
2254                                             if (reg_ctx)
2255                                             {
2256                                                 var_name_begin += ::strlen ("reg.");
2257                                                 if (var_name_begin < var_name_end)
2258                                                 {
2259                                                     std::string reg_name (var_name_begin, var_name_end);
2260                                                     reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2261                                                     if (reg_info)
2262                                                         var_success = true;
2263                                                 }
2264                                             }
2265                                         }
2266                                         else if (IsToken (var_name_begin, "script:"))
2267                                         {
2268                                             var_name_begin += ::strlen("script:");
2269                                             std::string script_name(var_name_begin,var_name_end);
2270                                             ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
2271                                             if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2272                                                 var_success = true;
2273                                         }
2274                                     }
2275                                 }
2276                             }
2277                             else if (IsToken (var_name_begin, "function."))
2278                             {
2279                                 if (sc && (sc->function != NULL || sc->symbol != NULL))
2280                                 {
2281                                     var_name_begin += ::strlen ("function.");
2282                                     if (IsToken (var_name_begin, "id}"))
2283                                     {
2284                                         if (sc->function)
2285                                             s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
2286                                         else
2287                                             s.Printf("symbol[%u]", sc->symbol->GetID());
2288 
2289                                         var_success = true;
2290                                     }
2291                                     else if (IsToken (var_name_begin, "name}"))
2292                                     {
2293                                         if (sc->function)
2294                                             cstr = sc->function->GetName().AsCString (NULL);
2295                                         else if (sc->symbol)
2296                                             cstr = sc->symbol->GetName().AsCString (NULL);
2297                                         if (cstr)
2298                                         {
2299                                             s.PutCString(cstr);
2300 
2301                                             if (sc->block)
2302                                             {
2303                                                 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2304                                                 if (inline_block)
2305                                                 {
2306                                                     const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2307                                                     if (inline_info)
2308                                                     {
2309                                                         s.PutCString(" [inlined] ");
2310                                                         inline_info->GetName().Dump(&s);
2311                                                     }
2312                                                 }
2313                                             }
2314                                             var_success = true;
2315                                         }
2316                                     }
2317                                     else if (IsToken (var_name_begin, "name-with-args}"))
2318                                     {
2319                                         // Print the function name with arguments in it
2320 
2321                                         if (sc->function)
2322                                         {
2323                                             var_success = true;
2324                                             ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2325                                             cstr = sc->function->GetName().AsCString (NULL);
2326                                             if (cstr)
2327                                             {
2328                                                 const InlineFunctionInfo *inline_info = NULL;
2329                                                 VariableListSP variable_list_sp;
2330                                                 bool get_function_vars = true;
2331                                                 if (sc->block)
2332                                                 {
2333                                                     Block *inline_block = sc->block->GetContainingInlinedBlock ();
2334 
2335                                                     if (inline_block)
2336                                                     {
2337                                                         get_function_vars = false;
2338                                                         inline_info = sc->block->GetInlinedFunctionInfo();
2339                                                         if (inline_info)
2340                                                             variable_list_sp = inline_block->GetBlockVariableList (true);
2341                                                     }
2342                                                 }
2343 
2344                                                 if (get_function_vars)
2345                                                 {
2346                                                     variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2347                                                 }
2348 
2349                                                 if (inline_info)
2350                                                 {
2351                                                     s.PutCString (cstr);
2352                                                     s.PutCString (" [inlined] ");
2353                                                     cstr = inline_info->GetName().GetCString();
2354                                                 }
2355 
2356                                                 VariableList args;
2357                                                 if (variable_list_sp)
2358                                                     variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
2359                                                 if (args.GetSize() > 0)
2360                                                 {
2361                                                     const char *open_paren = strchr (cstr, '(');
2362                                                     const char *close_paren = nullptr;
2363                                                     const char *generic = strchr(cstr, '<');
2364                                                     // if before the arguments list begins there is a template sign
2365                                                     // then scan to the end of the generic args before you try to find
2366                                                     // the arguments list
2367                                                     if (generic && open_paren && generic < open_paren)
2368                                                     {
2369                                                         int generic_depth = 1;
2370                                                         ++generic;
2371                                                         for (;
2372                                                              *generic && generic_depth > 0;
2373                                                              generic++)
2374                                                         {
2375                                                             if (*generic == '<')
2376                                                                 generic_depth++;
2377                                                             if (*generic == '>')
2378                                                                 generic_depth--;
2379                                                         }
2380                                                         if (*generic)
2381                                                             open_paren = strchr(generic, '(');
2382                                                         else
2383                                                             open_paren = nullptr;
2384                                                     }
2385                                                     if (open_paren)
2386                                                     {
2387                                                         if (IsToken (open_paren, "(anonymous namespace)"))
2388                                                         {
2389                                                             open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2390                                                             if (open_paren)
2391                                                                 close_paren = strchr (open_paren, ')');
2392                                                         }
2393                                                         else
2394                                                             close_paren = strchr (open_paren, ')');
2395                                                     }
2396 
2397                                                     if (open_paren)
2398                                                         s.Write(cstr, open_paren - cstr + 1);
2399                                                     else
2400                                                     {
2401                                                         s.PutCString (cstr);
2402                                                         s.PutChar ('(');
2403                                                     }
2404                                                     const size_t num_args = args.GetSize();
2405                                                     for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2406                                                     {
2407                                                         std::string buffer;
2408 
2409                                                         VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2410                                                         ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2411                                                         const char *var_representation = nullptr;
2412                                                         const char *var_name = var_value_sp->GetName().GetCString();
2413                                                         if (var_value_sp->GetClangType().IsAggregateType() &&
2414                                                             DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2415                                                         {
2416                                                             static StringSummaryFormat format(TypeSummaryImpl::Flags()
2417                                                                                               .SetHideItemNames(false)
2418                                                                                               .SetShowMembersOneLiner(true),
2419                                                                                               "");
2420                                                             format.FormatObject(var_value_sp.get(), buffer);
2421                                                             var_representation = buffer.c_str();
2422                                                         }
2423                                                         else
2424                                                             var_representation = var_value_sp->GetValueAsCString();
2425                                                         if (arg_idx > 0)
2426                                                             s.PutCString (", ");
2427                                                         if (var_value_sp->GetError().Success())
2428                                                         {
2429                                                             if (var_representation)
2430                                                                 s.Printf ("%s=%s", var_name, var_representation);
2431                                                             else
2432                                                                 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2433                                                         }
2434                                                         else
2435                                                             s.Printf ("%s=<unavailable>", var_name);
2436                                                     }
2437 
2438                                                     if (close_paren)
2439                                                         s.PutCString (close_paren);
2440                                                     else
2441                                                         s.PutChar(')');
2442 
2443                                                 }
2444                                                 else
2445                                                 {
2446                                                     s.PutCString(cstr);
2447                                                 }
2448                                             }
2449                                         }
2450                                         else if (sc->symbol)
2451                                         {
2452                                             cstr = sc->symbol->GetName().AsCString (NULL);
2453                                             if (cstr)
2454                                             {
2455                                                 s.PutCString(cstr);
2456                                                 var_success = true;
2457                                             }
2458                                         }
2459                                     }
2460                                     else if (IsToken (var_name_begin, "addr-offset}"))
2461                                     {
2462                                         var_success = addr != NULL;
2463                                         if (var_success)
2464                                         {
2465                                             format_addr = *addr;
2466                                             calculate_format_addr_function_offset = true;
2467                                         }
2468                                     }
2469                                     else if (IsToken (var_name_begin, "line-offset}"))
2470                                     {
2471                                         var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2472                                         if (var_success)
2473                                         {
2474                                             format_addr = sc->line_entry.range.GetBaseAddress();
2475                                             calculate_format_addr_function_offset = true;
2476                                         }
2477                                     }
2478                                     else if (IsToken (var_name_begin, "pc-offset}"))
2479                                     {
2480                                         StackFrame *frame = exe_ctx->GetFramePtr();
2481                                         var_success = frame != NULL;
2482                                         if (var_success)
2483                                         {
2484                                             format_addr = frame->GetFrameCodeAddress();
2485                                             calculate_format_addr_function_offset = true;
2486                                         }
2487                                     }
2488                                 }
2489                             }
2490                             break;
2491 
2492                         case 'l':
2493                             if (IsToken (var_name_begin, "line."))
2494                             {
2495                                 if (sc && sc->line_entry.IsValid())
2496                                 {
2497                                     var_name_begin += ::strlen ("line.");
2498                                     if (IsToken (var_name_begin, "file."))
2499                                     {
2500                                         var_name_begin += ::strlen ("file.");
2501 
2502                                         if (IsToken (var_name_begin, "basename}"))
2503                                         {
2504                                             format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2505                                             var_success = (bool)format_file_spec;
2506                                         }
2507                                         else if (IsToken (var_name_begin, "fullpath}"))
2508                                         {
2509                                             format_file_spec = sc->line_entry.file;
2510                                             var_success = (bool)format_file_spec;
2511                                         }
2512                                     }
2513                                     else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
2514                                     {
2515                                         var_success = true;
2516                                         s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
2517                                     }
2518                                     else if ((IsToken (var_name_begin, "start-addr}")) ||
2519                                              (IsToken (var_name_begin, "end-addr}")))
2520                                     {
2521                                         var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2522                                         if (var_success)
2523                                         {
2524                                             format_addr = sc->line_entry.range.GetBaseAddress();
2525                                             if (var_name_begin[0] == 'e')
2526                                                 format_addr.Slide (sc->line_entry.range.GetByteSize());
2527                                         }
2528                                     }
2529                                 }
2530                             }
2531                             break;
2532                         }
2533 
2534                         if (var_success)
2535                         {
2536                             // If format addr is valid, then we need to print an address
2537                             if (reg_num != LLDB_INVALID_REGNUM)
2538                             {
2539                                 StackFrame *frame = exe_ctx->GetFramePtr();
2540                                 // We have a register value to display...
2541                                 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2542                                 {
2543                                     format_addr = frame->GetFrameCodeAddress();
2544                                 }
2545                                 else
2546                                 {
2547                                     if (reg_ctx == NULL)
2548                                         reg_ctx = frame->GetRegisterContext().get();
2549 
2550                                     if (reg_ctx)
2551                                     {
2552                                         if (reg_kind != kNumRegisterKinds)
2553                                             reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2554                                         reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2555                                         var_success = reg_info != NULL;
2556                                     }
2557                                 }
2558                             }
2559 
2560                             if (reg_info != NULL)
2561                             {
2562                                 RegisterValue reg_value;
2563                                 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2564                                 if (var_success)
2565                                 {
2566                                     reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
2567                                 }
2568                             }
2569 
2570                             if (format_file_spec)
2571                             {
2572                                 s << format_file_spec;
2573                             }
2574 
2575                             // If format addr is valid, then we need to print an address
2576                             if (format_addr.IsValid())
2577                             {
2578                                 var_success = false;
2579 
2580                                 if (calculate_format_addr_function_offset)
2581                                 {
2582                                     Address func_addr;
2583 
2584                                     if (sc)
2585                                     {
2586                                         if (sc->function)
2587                                         {
2588                                             func_addr = sc->function->GetAddressRange().GetBaseAddress();
2589                                             if (sc->block)
2590                                             {
2591                                                 // Check to make sure we aren't in an inline
2592                                                 // function. If we are, use the inline block
2593                                                 // range that contains "format_addr" since
2594                                                 // blocks can be discontiguous.
2595                                                 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2596                                                 AddressRange inline_range;
2597                                                 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2598                                                     func_addr = inline_range.GetBaseAddress();
2599                                             }
2600                                         }
2601                                         else if (sc->symbol && sc->symbol->ValueIsAddress())
2602                                             func_addr = sc->symbol->GetAddress();
2603                                     }
2604 
2605                                     if (func_addr.IsValid())
2606                                     {
2607                                         if (func_addr.GetSection() == format_addr.GetSection())
2608                                         {
2609                                             addr_t func_file_addr = func_addr.GetFileAddress();
2610                                             addr_t addr_file_addr = format_addr.GetFileAddress();
2611                                             if (addr_file_addr > func_file_addr)
2612                                                 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
2613                                             else if (addr_file_addr < func_file_addr)
2614                                                 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
2615                                             var_success = true;
2616                                         }
2617                                         else
2618                                         {
2619                                             Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2620                                             if (target)
2621                                             {
2622                                                 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2623                                                 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2624                                                 if (addr_load_addr > func_load_addr)
2625                                                     s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
2626                                                 else if (addr_load_addr < func_load_addr)
2627                                                     s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
2628                                                 var_success = true;
2629                                             }
2630                                         }
2631                                     }
2632                                 }
2633                                 else
2634                                 {
2635                                     Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2636                                     addr_t vaddr = LLDB_INVALID_ADDRESS;
2637                                     if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2638                                         vaddr = format_addr.GetLoadAddress (target);
2639                                     if (vaddr == LLDB_INVALID_ADDRESS)
2640                                         vaddr = format_addr.GetFileAddress ();
2641 
2642                                     if (vaddr != LLDB_INVALID_ADDRESS)
2643                                     {
2644                                         int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
2645                                         if (addr_width == 0)
2646                                             addr_width = 16;
2647                                         s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
2648                                         var_success = true;
2649                                     }
2650                                 }
2651                             }
2652                         }
2653 
2654                         if (var_success == false)
2655                             success = false;
2656                     }
2657                     p = var_name_end;
2658                 }
2659                 else
2660                     break;
2661             }
2662             else
2663             {
2664                 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2665                 s.PutChar(*p);
2666             }
2667         }
2668         else if (*p == '\\')
2669         {
2670             ++p; // skip the slash
2671             switch (*p)
2672             {
2673             case 'a': s.PutChar ('\a'); break;
2674             case 'b': s.PutChar ('\b'); break;
2675             case 'f': s.PutChar ('\f'); break;
2676             case 'n': s.PutChar ('\n'); break;
2677             case 'r': s.PutChar ('\r'); break;
2678             case 't': s.PutChar ('\t'); break;
2679             case 'v': s.PutChar ('\v'); break;
2680             case '\'': s.PutChar ('\''); break;
2681             case '\\': s.PutChar ('\\'); break;
2682             case '0':
2683                 // 1 to 3 octal chars
2684                 {
2685                     // Make a string that can hold onto the initial zero char,
2686                     // up to 3 octal digits, and a terminating NULL.
2687                     char oct_str[5] = { 0, 0, 0, 0, 0 };
2688 
2689                     int i;
2690                     for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2691                         oct_str[i] = p[i];
2692 
2693                     // We don't want to consume the last octal character since
2694                     // the main for loop will do this for us, so we advance p by
2695                     // one less than i (even if i is zero)
2696                     p += i - 1;
2697                     unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2698                     if (octal_value <= UINT8_MAX)
2699                     {
2700                         s.PutChar((char)octal_value);
2701                     }
2702                 }
2703                 break;
2704 
2705             case 'x':
2706                 // hex number in the format
2707                 if (isxdigit(p[1]))
2708                 {
2709                     ++p;    // Skip the 'x'
2710 
2711                     // Make a string that can hold onto two hex chars plus a
2712                     // NULL terminator
2713                     char hex_str[3] = { 0,0,0 };
2714                     hex_str[0] = *p;
2715                     if (isxdigit(p[1]))
2716                     {
2717                         ++p; // Skip the first of the two hex chars
2718                         hex_str[1] = *p;
2719                     }
2720 
2721                     unsigned long hex_value = strtoul (hex_str, NULL, 16);
2722                     if (hex_value <= UINT8_MAX)
2723                         s.PutChar ((char)hex_value);
2724                 }
2725                 else
2726                 {
2727                     s.PutChar('x');
2728                 }
2729                 break;
2730 
2731             default:
2732                 // Just desensitize any other character by just printing what
2733                 // came after the '\'
2734                 s << *p;
2735                 break;
2736 
2737             }
2738 
2739         }
2740     }
2741     if (end)
2742         *end = p;
2743     return success;
2744 }
2745 
2746 bool
2747 Debugger::FormatPrompt
2748 (
2749     const char *format,
2750     const SymbolContext *sc,
2751     const ExecutionContext *exe_ctx,
2752     const Address *addr,
2753     Stream &s,
2754     ValueObject* valobj
2755 )
2756 {
2757     bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2758     std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2759     if (format_str.length())
2760         format = format_str.c_str();
2761     return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2762 }
2763 
2764 void
2765 Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2766 {
2767     // For simplicity's sake, I am not going to deal with how to close down any
2768     // open logging streams, I just redirect everything from here on out to the
2769     // callback.
2770     m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2771 }
2772 
2773 bool
2774 Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2775 {
2776     Log::Callbacks log_callbacks;
2777 
2778     StreamSP log_stream_sp;
2779     if (m_log_callback_stream_sp)
2780     {
2781         log_stream_sp = m_log_callback_stream_sp;
2782         // For now when using the callback mode you always get thread & timestamp.
2783         log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2784     }
2785     else if (log_file == NULL || *log_file == '\0')
2786     {
2787         log_stream_sp = GetOutputFile();
2788     }
2789     else
2790     {
2791         LogStreamMap::iterator pos = m_log_streams.find(log_file);
2792         if (pos != m_log_streams.end())
2793             log_stream_sp = pos->second.lock();
2794         if (!log_stream_sp)
2795         {
2796             log_stream_sp.reset (new StreamFile (log_file));
2797             m_log_streams[log_file] = log_stream_sp;
2798         }
2799     }
2800     assert (log_stream_sp.get());
2801 
2802     if (log_options == 0)
2803         log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2804 
2805     if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
2806     {
2807         log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2808         return true;
2809     }
2810     else
2811     {
2812         LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2813         if (log_channel_sp)
2814         {
2815             if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2816             {
2817                 return true;
2818             }
2819             else
2820             {
2821                 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2822                 return false;
2823             }
2824         }
2825         else
2826         {
2827             error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2828             return false;
2829         }
2830     }
2831     return false;
2832 }
2833 
2834 SourceManager &
2835 Debugger::GetSourceManager ()
2836 {
2837     if (m_source_manager_ap.get() == NULL)
2838         m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2839     return *m_source_manager_ap;
2840 }
2841 
2842 
2843 
2844 // This function handles events that were broadcast by the process.
2845 void
2846 Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2847 {
2848     using namespace lldb;
2849     const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2850 
2851 //    if (event_type & eBreakpointEventTypeAdded
2852 //        || event_type & eBreakpointEventTypeRemoved
2853 //        || event_type & eBreakpointEventTypeEnabled
2854 //        || event_type & eBreakpointEventTypeDisabled
2855 //        || event_type & eBreakpointEventTypeCommandChanged
2856 //        || event_type & eBreakpointEventTypeConditionChanged
2857 //        || event_type & eBreakpointEventTypeIgnoreChanged
2858 //        || event_type & eBreakpointEventTypeLocationsResolved)
2859 //    {
2860 //        // Don't do anything about these events, since the breakpoint commands already echo these actions.
2861 //    }
2862 //
2863     if (event_type & eBreakpointEventTypeLocationsAdded)
2864     {
2865         uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
2866         if (num_new_locations > 0)
2867         {
2868             BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
2869             StreamFileSP output_sp (GetOutputFile());
2870             if (output_sp)
2871             {
2872                 output_sp->Printf("%d location%s added to breakpoint %d\n",
2873                                   num_new_locations,
2874                                   num_new_locations == 1 ? "" : "s",
2875                                   breakpoint->GetID());
2876                 RefreshTopIOHandler();
2877             }
2878         }
2879     }
2880 //    else if (event_type & eBreakpointEventTypeLocationsRemoved)
2881 //    {
2882 //        // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
2883 //    }
2884 //    else if (event_type & eBreakpointEventTypeLocationsResolved)
2885 //    {
2886 //        // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
2887 //    }
2888 }
2889 
2890 size_t
2891 Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
2892 {
2893     size_t total_bytes = 0;
2894     if (stream == NULL)
2895         stream = GetOutputFile().get();
2896 
2897     if (stream)
2898     {
2899         //  The process has stuff waiting for stdout; get it and write it out to the appropriate place.
2900         if (process == NULL)
2901         {
2902             TargetSP target_sp = GetTargetList().GetSelectedTarget();
2903             if (target_sp)
2904                 process = target_sp->GetProcessSP().get();
2905         }
2906         if (process)
2907         {
2908             Error error;
2909             size_t len;
2910             char stdio_buffer[1024];
2911             while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2912             {
2913                 stream->Write(stdio_buffer, len);
2914                 total_bytes += len;
2915             }
2916         }
2917         stream->Flush();
2918     }
2919     return total_bytes;
2920 }
2921 
2922 size_t
2923 Debugger::GetProcessSTDERR (Process *process, Stream *stream)
2924 {
2925     size_t total_bytes = 0;
2926     if (stream == NULL)
2927         stream = GetOutputFile().get();
2928 
2929     if (stream)
2930     {
2931         //  The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2932         if (process == NULL)
2933         {
2934             TargetSP target_sp = GetTargetList().GetSelectedTarget();
2935             if (target_sp)
2936                 process = target_sp->GetProcessSP().get();
2937         }
2938         if (process)
2939         {
2940             Error error;
2941             size_t len;
2942             char stdio_buffer[1024];
2943             while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2944             {
2945                 stream->Write(stdio_buffer, len);
2946                 total_bytes += len;
2947             }
2948         }
2949         stream->Flush();
2950     }
2951     return total_bytes;
2952 }
2953 
2954 // This function handles events that were broadcast by the process.
2955 void
2956 Debugger::HandleProcessEvent (const EventSP &event_sp)
2957 {
2958     using namespace lldb;
2959     const uint32_t event_type = event_sp->GetType();
2960     ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
2961 
2962     StreamString output_stream;
2963     StreamString error_stream;
2964     const bool gui_enabled = IsForwardingEvents();
2965 
2966     if (!gui_enabled)
2967     {
2968         bool pop_process_io_handler = false;
2969         assert (process_sp);
2970 
2971         if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
2972         {
2973             GetProcessSTDOUT (process_sp.get(), &output_stream);
2974         }
2975 
2976         if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
2977         {
2978             GetProcessSTDERR (process_sp.get(), &error_stream);
2979         }
2980 
2981         if (event_type & Process::eBroadcastBitStateChanged)
2982         {
2983 
2984             // Drain all stout and stderr so we don't see any output come after
2985             // we print our prompts
2986             // Something changed in the process;  get the event and report the process's current status and location to
2987             // the user.
2988             StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
2989             if (event_state == eStateInvalid)
2990                 return;
2991 
2992             switch (event_state)
2993             {
2994                 case eStateInvalid:
2995                 case eStateUnloaded:
2996                 case eStateConnected:
2997                 case eStateAttaching:
2998                 case eStateLaunching:
2999                 case eStateStepping:
3000                 case eStateDetached:
3001                     {
3002                         output_stream.Printf("Process %" PRIu64 " %s\n",
3003                                              process_sp->GetID(),
3004                                              StateAsCString (event_state));
3005 
3006                         if (event_state == eStateDetached)
3007                             pop_process_io_handler = true;
3008                     }
3009                     break;
3010 
3011                 case eStateRunning:
3012                     // Don't be chatty when we run...
3013                     break;
3014 
3015                 case eStateExited:
3016                     process_sp->GetStatus(output_stream);
3017                     pop_process_io_handler = true;
3018                     break;
3019 
3020                 case eStateStopped:
3021                 case eStateCrashed:
3022                 case eStateSuspended:
3023                     // Make sure the program hasn't been auto-restarted:
3024                     if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
3025                     {
3026                         size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
3027                         if (num_reasons > 0)
3028                         {
3029                             // FIXME: Do we want to report this, or would that just be annoyingly chatty?
3030                             if (num_reasons == 1)
3031                             {
3032                                 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
3033                                 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n",
3034                                                      process_sp->GetID(),
3035                                                      reason ? reason : "<UNKNOWN REASON>");
3036                             }
3037                             else
3038                             {
3039                                 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
3040                                                      process_sp->GetID());
3041 
3042 
3043                                 for (size_t i = 0; i < num_reasons; i++)
3044                                 {
3045                                     const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
3046                                     output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
3047                                 }
3048                             }
3049                         }
3050                     }
3051                     else
3052                     {
3053                         // Lock the thread list so it doesn't change on us, this is the scope for the locker:
3054                         {
3055                             ThreadList &thread_list = process_sp->GetThreadList();
3056                             Mutex::Locker locker (thread_list.GetMutex());
3057 
3058                             ThreadSP curr_thread (thread_list.GetSelectedThread());
3059                             ThreadSP thread;
3060                             StopReason curr_thread_stop_reason = eStopReasonInvalid;
3061                             if (curr_thread)
3062                                 curr_thread_stop_reason = curr_thread->GetStopReason();
3063                             if (!curr_thread ||
3064                                 !curr_thread->IsValid() ||
3065                                 curr_thread_stop_reason == eStopReasonInvalid ||
3066                                 curr_thread_stop_reason == eStopReasonNone)
3067                             {
3068                                 // Prefer a thread that has just completed its plan over another thread as current thread.
3069                                 ThreadSP plan_thread;
3070                                 ThreadSP other_thread;
3071                                 const size_t num_threads = thread_list.GetSize();
3072                                 size_t i;
3073                                 for (i = 0; i < num_threads; ++i)
3074                                 {
3075                                     thread = thread_list.GetThreadAtIndex(i);
3076                                     StopReason thread_stop_reason = thread->GetStopReason();
3077                                     switch (thread_stop_reason)
3078                                     {
3079                                         case eStopReasonInvalid:
3080                                         case eStopReasonNone:
3081                                             break;
3082 
3083                                         case eStopReasonTrace:
3084                                         case eStopReasonBreakpoint:
3085                                         case eStopReasonWatchpoint:
3086                                         case eStopReasonSignal:
3087                                         case eStopReasonException:
3088                                         case eStopReasonExec:
3089                                         case eStopReasonThreadExiting:
3090                                             if (!other_thread)
3091                                                 other_thread = thread;
3092                                             break;
3093                                         case eStopReasonPlanComplete:
3094                                             if (!plan_thread)
3095                                                 plan_thread = thread;
3096                                             break;
3097                                     }
3098                                 }
3099                                 if (plan_thread)
3100                                     thread_list.SetSelectedThreadByID (plan_thread->GetID());
3101                                 else if (other_thread)
3102                                     thread_list.SetSelectedThreadByID (other_thread->GetID());
3103                                 else
3104                                 {
3105                                     if (curr_thread && curr_thread->IsValid())
3106                                         thread = curr_thread;
3107                                     else
3108                                         thread = thread_list.GetThreadAtIndex(0);
3109 
3110                                     if (thread)
3111                                         thread_list.SetSelectedThreadByID (thread->GetID());
3112                                 }
3113                             }
3114                         }
3115                         // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code,
3116                         // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to
3117                         // have a hard time restarting the process.
3118 
3119                         if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget())
3120                         {
3121                             const bool only_threads_with_stop_reason = true;
3122                             const uint32_t start_frame = 0;
3123                             const uint32_t num_frames = 1;
3124                             const uint32_t num_frames_with_source = 1;
3125                             process_sp->GetStatus(output_stream);
3126                             process_sp->GetThreadStatus (output_stream,
3127                                                          only_threads_with_stop_reason,
3128                                                          start_frame,
3129                                                          num_frames,
3130                                                          num_frames_with_source);
3131                         }
3132                         else
3133                         {
3134                             uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this());
3135                             if (target_idx != UINT32_MAX)
3136                                 output_stream.Printf ("Target %d: (", target_idx);
3137                             else
3138                                 output_stream.Printf ("Target <unknown index>: (");
3139                             process_sp->GetTarget().Dump (&output_stream, eDescriptionLevelBrief);
3140                             output_stream.Printf (") stopped.\n");
3141                         }
3142 
3143                         // Pop the process IO handler
3144                         pop_process_io_handler = true;
3145                     }
3146                     break;
3147             }
3148         }
3149 
3150         if (output_stream.GetSize() || error_stream.GetSize())
3151         {
3152             StreamFileSP error_stream_sp (GetOutputFile());
3153             bool top_io_handler_hid = false;
3154 
3155             if (process_sp->ProcessIOHandlerIsActive() == false)
3156                 top_io_handler_hid = HideTopIOHandler();
3157 
3158             if (output_stream.GetSize())
3159             {
3160                 StreamFileSP output_stream_sp (GetOutputFile());
3161                 if (output_stream_sp)
3162                     output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3163             }
3164 
3165             if (error_stream.GetSize())
3166             {
3167                 StreamFileSP error_stream_sp (GetErrorFile());
3168                 if (error_stream_sp)
3169                     error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3170             }
3171 
3172             if (top_io_handler_hid)
3173                 RefreshTopIOHandler();
3174         }
3175 
3176         if (pop_process_io_handler)
3177             process_sp->PopProcessIOHandler();
3178     }
3179 }
3180 
3181 void
3182 Debugger::HandleThreadEvent (const EventSP &event_sp)
3183 {
3184     // At present the only thread event we handle is the Frame Changed event,
3185     // and all we do for that is just reprint the thread status for that thread.
3186     using namespace lldb;
3187     const uint32_t event_type = event_sp->GetType();
3188     if (event_type == Thread::eBroadcastBitStackChanged   ||
3189         event_type == Thread::eBroadcastBitThreadSelected )
3190     {
3191         ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3192         if (thread_sp)
3193         {
3194             HideTopIOHandler();
3195             StreamFileSP stream_sp (GetOutputFile());
3196             thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3197             RefreshTopIOHandler();
3198         }
3199     }
3200 }
3201 
3202 bool
3203 Debugger::IsForwardingEvents ()
3204 {
3205     return (bool)m_forward_listener_sp;
3206 }
3207 
3208 void
3209 Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3210 {
3211     m_forward_listener_sp = listener_sp;
3212 }
3213 
3214 void
3215 Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3216 {
3217     m_forward_listener_sp.reset();
3218 }
3219 
3220 
3221 void
3222 Debugger::DefaultEventHandler()
3223 {
3224     Listener& listener(GetListener());
3225     ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3226     ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3227     ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3228     BroadcastEventSpec target_event_spec (broadcaster_class_target,
3229                                           Target::eBroadcastBitBreakpointChanged);
3230 
3231     BroadcastEventSpec process_event_spec (broadcaster_class_process,
3232                                            Process::eBroadcastBitStateChanged   |
3233                                            Process::eBroadcastBitSTDOUT         |
3234                                            Process::eBroadcastBitSTDERR);
3235 
3236     BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3237                                           Thread::eBroadcastBitStackChanged     |
3238                                           Thread::eBroadcastBitThreadSelected   );
3239 
3240     listener.StartListeningForEventSpec (*this, target_event_spec);
3241     listener.StartListeningForEventSpec (*this, process_event_spec);
3242     listener.StartListeningForEventSpec (*this, thread_event_spec);
3243     listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3244                                       CommandInterpreter::eBroadcastBitQuitCommandReceived      |
3245                                       CommandInterpreter::eBroadcastBitAsynchronousOutputData   |
3246                                       CommandInterpreter::eBroadcastBitAsynchronousErrorData    );
3247 
3248     bool done = false;
3249     while (!done)
3250     {
3251 //        Mutex::Locker locker;
3252 //        if (locker.TryLock(m_input_reader_stack.GetMutex()))
3253 //        {
3254 //            if (m_input_reader_stack.IsEmpty())
3255 //                break;
3256 //        }
3257 //
3258         EventSP event_sp;
3259         if (listener.WaitForEvent(NULL, event_sp))
3260         {
3261             if (event_sp)
3262             {
3263                 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3264                 if (broadcaster)
3265                 {
3266                     uint32_t event_type = event_sp->GetType();
3267                     ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3268                     if (broadcaster_class == broadcaster_class_process)
3269                     {
3270                         HandleProcessEvent (event_sp);
3271                     }
3272                     else if (broadcaster_class == broadcaster_class_target)
3273                     {
3274                         if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3275                         {
3276                             HandleBreakpointEvent (event_sp);
3277                         }
3278                     }
3279                     else if (broadcaster_class == broadcaster_class_thread)
3280                     {
3281                         HandleThreadEvent (event_sp);
3282                     }
3283                     else if (broadcaster == m_command_interpreter_ap.get())
3284                     {
3285                         if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3286                         {
3287                             done = true;
3288                         }
3289                         else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3290                         {
3291                             const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3292                             if (data && data[0])
3293                             {
3294                                 StreamFileSP error_sp (GetErrorFile());
3295                                 if (error_sp)
3296                                 {
3297                                     HideTopIOHandler();
3298                                     error_sp->PutCString(data);
3299                                     error_sp->Flush();
3300                                     RefreshTopIOHandler();
3301                                 }
3302                             }
3303                         }
3304                         else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3305                         {
3306                             const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3307                             if (data && data[0])
3308                             {
3309                                 StreamFileSP output_sp (GetOutputFile());
3310                                 if (output_sp)
3311                                 {
3312                                     HideTopIOHandler();
3313                                     output_sp->PutCString(data);
3314                                     output_sp->Flush();
3315                                     RefreshTopIOHandler();
3316                                 }
3317                             }
3318                         }
3319                     }
3320                 }
3321 
3322                 if (m_forward_listener_sp)
3323                     m_forward_listener_sp->AddEvent(event_sp);
3324             }
3325         }
3326     }
3327 }
3328 
3329 lldb::thread_result_t
3330 Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3331 {
3332     ((Debugger *)arg)->DefaultEventHandler();
3333     return NULL;
3334 }
3335 
3336 bool
3337 Debugger::StartEventHandlerThread()
3338 {
3339     if (!IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3340         m_event_handler_thread = Host::ThreadCreate("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
3341     return IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread);
3342 }
3343 
3344 void
3345 Debugger::StopEventHandlerThread()
3346 {
3347     if (IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3348     {
3349         GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
3350         Host::ThreadJoin(m_event_handler_thread, NULL, NULL);
3351         m_event_handler_thread = LLDB_INVALID_HOST_THREAD;
3352     }
3353 }
3354 
3355 
3356 lldb::thread_result_t
3357 Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3358 {
3359     Debugger *debugger = (Debugger *)arg;
3360     debugger->ExecuteIOHanders();
3361     debugger->StopEventHandlerThread();
3362     return NULL;
3363 }
3364 
3365 bool
3366 Debugger::StartIOHandlerThread()
3367 {
3368     if (!IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3369         m_io_handler_thread = Host::ThreadCreate("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
3370     return IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread);
3371 }
3372 
3373 void
3374 Debugger::StopIOHandlerThread()
3375 {
3376     if (IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3377     {
3378         if (m_input_file_sp)
3379             m_input_file_sp->GetFile().Close();
3380         Host::ThreadJoin(m_io_handler_thread, NULL, NULL);
3381         m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
3382     }
3383 }
3384 
3385 
3386